-
Notifications
You must be signed in to change notification settings - Fork 66
Add a DRM/KMS backend #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
cc1225f
Add a DRM/KMS backend
notgull 7765e6e
Fix basic compile errors
notgull e6449e2
Don't test KMS on netbsd
notgull da94cde
Add damage support
notgull 26f776b
Add example and fix KMS
notgull c10ae7a
Add double buffering
notgull a276f67
Fix implementation of double buffering
notgull 1716692
Fix usage of connectors
notgull 3e160a6
Address example review comments
notgull 526eabd
Add a strategy for finding the card without udev
notgull 83e3d97
fmt
notgull 09e80ad
Add comment to buffer_mut for DRM page flip
notgull 205657b
fmt
notgull a85b569
Test for connected connectors instead of amount
notgull c8ba5d4
Ignore ENOSYS for dirty_framebuffer
notgull File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| //! Example of using softbuffer with drm-rs. | ||
|
|
||
| #[cfg(kms_platform)] | ||
| mod imple { | ||
| use drm::control::{connector, Device as CtrlDevice, Event, ModeTypeFlags, PlaneType}; | ||
| use drm::Device; | ||
|
|
||
| use raw_window_handle::{DrmDisplayHandle, DrmWindowHandle}; | ||
| use softbuffer::{Context, Surface}; | ||
|
|
||
| use std::num::NonZeroU32; | ||
| use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd}; | ||
| use std::path::Path; | ||
| use std::time::{Duration, Instant}; | ||
|
|
||
| pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> { | ||
| // Open a new device. | ||
| let device = Card::find()?; | ||
|
|
||
| // Create the softbuffer context. | ||
| let context = unsafe { | ||
| Context::from_raw({ | ||
| let mut handle = DrmDisplayHandle::empty(); | ||
| handle.fd = device.as_fd().as_raw_fd(); | ||
| handle.into() | ||
| }) | ||
| }?; | ||
|
|
||
| // Get the DRM handles. | ||
| let handles = device.resource_handles()?; | ||
|
|
||
| // Get the list of connectors and CRTCs. | ||
| let connectors = handles | ||
| .connectors() | ||
| .iter() | ||
| .map(|&con| device.get_connector(con, true)) | ||
| .collect::<Result<Vec<_>, _>>()?; | ||
| let crtcs = handles | ||
| .crtcs() | ||
| .iter() | ||
| .map(|&crtc| device.get_crtc(crtc)) | ||
| .collect::<Result<Vec<_>, _>>()?; | ||
|
|
||
| // Find a connected crtc. | ||
| let con = connectors | ||
| .iter() | ||
| .find(|con| con.state() == connector::State::Connected) | ||
| .ok_or("No connected connectors")?; | ||
|
|
||
| // Get the first CRTC. | ||
| let crtc = crtcs.first().ok_or("No CRTCs")?; | ||
|
|
||
| // Find a mode to use. | ||
| let mode = con | ||
| .modes() | ||
| .iter() | ||
| .find(|mode| mode.mode_type().contains(ModeTypeFlags::PREFERRED)) | ||
| .or_else(|| con.modes().first()) | ||
| .ok_or("No modes")?; | ||
|
|
||
| // Look for a primary plane compatible with our CRTC. | ||
| let planes = device.plane_handles()?; | ||
| let planes = planes | ||
| .iter() | ||
| .filter(|&&plane| { | ||
| device.get_plane(plane).map_or(false, |plane| { | ||
| let crtcs = handles.filter_crtcs(plane.possible_crtcs()); | ||
| crtcs.contains(&crtc.handle()) | ||
| }) | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| // Find the first primary plane or take the first one period. | ||
| let plane = planes | ||
| .iter() | ||
| .find(|&&&plane| { | ||
| if let Ok(props) = device.get_properties(plane) { | ||
| let (ids, vals) = props.as_props_and_values(); | ||
| for (&id, &val) in ids.iter().zip(vals.iter()) { | ||
| if let Ok(info) = device.get_property(id) { | ||
| if info.name().to_str().map_or(false, |x| x == "type") { | ||
| return val == PlaneType::Primary as u32 as u64; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| false | ||
| }) | ||
| .or(planes.first()) | ||
| .ok_or("No planes")?; | ||
|
|
||
| // Create the surface on top of this plane. | ||
| // Note: This requires root on DRM/KMS. | ||
| let mut surface = unsafe { | ||
| Surface::from_raw(&context, { | ||
| let mut handle = DrmWindowHandle::empty(); | ||
| handle.plane = (**plane).into(); | ||
| handle.into() | ||
| }) | ||
| }?; | ||
|
|
||
| // Resize the surface. | ||
| let (width, height) = mode.size(); | ||
| surface.resize( | ||
| NonZeroU32::new(width as u32).unwrap(), | ||
| NonZeroU32::new(height as u32).unwrap(), | ||
| )?; | ||
|
|
||
| // Start drawing to it. | ||
| let start = Instant::now(); | ||
| let mut tick = 0; | ||
| while Instant::now().duration_since(start) < Duration::from_secs(2) { | ||
| tick += 1; | ||
| println!("Drawing tick {tick}"); | ||
|
|
||
| // Start drawing. | ||
| let mut buffer = surface.buffer_mut()?; | ||
| draw_to_buffer(&mut buffer, tick); | ||
| buffer.present()?; | ||
|
|
||
| // Wait for the page flip to happen. | ||
| rustix::event::poll( | ||
| &mut [rustix::event::PollFd::new( | ||
| &device, | ||
| rustix::event::PollFlags::IN, | ||
| )], | ||
| -1, | ||
| )?; | ||
|
|
||
| // Receive the events. | ||
| let events = device.receive_events()?; | ||
| println!("Got some events..."); | ||
| for event in events { | ||
| match event { | ||
| Event::PageFlip(_) => { | ||
| println!("Page flip event."); | ||
| } | ||
| Event::Vblank(_) => { | ||
| println!("Vblank event."); | ||
| } | ||
| _ => { | ||
| println!("Unknown event."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| fn draw_to_buffer(buf: &mut [u32], tick: usize) { | ||
| let scale = colorous::SINEBOW; | ||
| let mut i = (tick as f64) / 20.0; | ||
| while i > 1.0 { | ||
| i -= 1.0; | ||
| } | ||
|
|
||
| let color = scale.eval_continuous(i); | ||
| let pixel = (color.r as u32) << 16 | (color.g as u32) << 8 | (color.b as u32); | ||
| buf.fill(pixel); | ||
| } | ||
|
|
||
| struct Card(std::fs::File); | ||
|
|
||
| impl Card { | ||
| fn find() -> Result<Card, Box<dyn std::error::Error>> { | ||
| for i in 0..10 { | ||
| let path = format!("/dev/dri/card{i}"); | ||
| let device = Card::open(path)?; | ||
|
|
||
| // Only use it if it has connectors. | ||
| let handles = match device.resource_handles() { | ||
notgull marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Ok(handles) => handles, | ||
| Err(_) => continue, | ||
| }; | ||
|
|
||
| if handles | ||
| .connectors | ||
| .iter() | ||
| .filter_map(|c| device.get_connector(*c, false).ok()) | ||
| .any(|c| c.state() == connector::State::Connected) | ||
| { | ||
| return Ok(device); | ||
| } | ||
| } | ||
|
|
||
| Err("No DRM device found".into()) | ||
| } | ||
|
|
||
| fn open(path: impl AsRef<Path>) -> Result<Card, Box<dyn std::error::Error>> { | ||
| let file = std::fs::OpenOptions::new() | ||
| .read(true) | ||
| .write(true) | ||
| .open(path)?; | ||
| Ok(Card(file)) | ||
| } | ||
| } | ||
|
|
||
| impl AsFd for Card { | ||
| fn as_fd(&self) -> BorrowedFd<'_> { | ||
| self.0.as_fd() | ||
| } | ||
| } | ||
|
|
||
| impl Device for Card {} | ||
| impl CtrlDevice for Card {} | ||
| } | ||
|
|
||
| #[cfg(not(kms_platform))] | ||
| mod imple { | ||
| pub(super) fn entry() -> Result<(), Box<dyn std::error::Error>> { | ||
| eprintln!("This example requires the `kms` feature."); | ||
notgull marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| imple::entry() | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.