Skip to content

Commit

Permalink
Update drm/gbm dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical authored and szecket committed Sep 13, 2024
1 parent 9d2a788 commit 447ded6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 140 deletions.
9 changes: 4 additions & 5 deletions internal/backends/linuxkms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ path = "lib.rs"
[features]
renderer-skia = ["renderer-skia-opengl"]
renderer-skia-vulkan = ["i-slint-renderer-skia/vulkan", "vulkano"]
renderer-skia-opengl = ["i-slint-renderer-skia/opengl", "drm", "gbm", "gbm-sys", "glutin", "raw-window-handle", "dep:memmap2"]
renderer-femtovg = ["i-slint-renderer-femtovg", "drm", "gbm", "gbm-sys", "glutin", "raw-window-handle"]
renderer-skia-opengl = ["i-slint-renderer-skia/opengl", "drm", "gbm", "glutin", "raw-window-handle", "dep:memmap2"]
renderer-femtovg = ["i-slint-renderer-femtovg", "drm", "gbm", "glutin", "raw-window-handle"]
renderer-software = ["i-slint-core/software-renderer-systemfonts", "drm", "dep:bytemuck", "dep:memmap2"]
libseat = ["dep:libseat"]

Expand All @@ -39,9 +39,8 @@ calloop = { version = "0.12.3" }
libseat = { version = "0.2.1", optional = true, default-features = false }
nix = { version = "0.27.0", features=["fs", "ioctl"] }
vulkano = { version = "0.34.0", optional = true, default-features = false }
drm = { version = "0.9.0", optional = true }
gbm = { version = "0.12.0", optional = true, default-features = false, features = ["drm-support"] }
gbm-sys = { version = "0.2.2", optional = true }
drm = { version = "0.12.0", optional = true }
gbm = { version = "0.15.0", optional = true, default-features = false, features = ["drm-support"] }
glutin = { workspace = true, optional = true, default-features = false, features = ["libloading", "egl"] }
raw-window-handle = { version = "0.6.2", optional = true }
bytemuck = { workspace = true, optional = true, features = ["derive"] }
Expand Down
138 changes: 4 additions & 134 deletions internal/backends/linuxkms/display/gbmdisplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,17 @@ impl super::Presenter for GbmDisplay {
// avoid the failing has_free_buffers() check on the vivante gbm backend.

let mut front_buffer = unsafe {
let surface = self.gbm_surface.as_raw() as _;
let bo = gbm_sys::gbm_surface_lock_front_buffer(surface);
if bo.is_null() {
return Err(format!("Could not lock gbm front buffer").into());
}
gbm_rs_workaround::GbmBo { bo, surface }
self.gbm_surface
.lock_front_buffer()
.map_err(|err| format!("Could not lock gbm front buffer: {err}"))?
};

// TODO: support modifiers
// TODO: consider falling back to the old non-planar API
let fb = self
.drm_output
.drm_device
.add_planar_framebuffer(&front_buffer, &[None, None, None, None], 0)
.add_planar_framebuffer(&front_buffer, drm::control::FbCmd2Flags::empty())
.map_err(|e| format!("Error adding gbm buffer as framebuffer: {e}"))?;

front_buffer
Expand Down Expand Up @@ -150,130 +147,3 @@ impl raw_window_handle::HasDisplayHandle for GbmDisplay {
})
}
}

// Workaround for https://github.com/Smithay/gbm.rs/issues/36 : A wrapper around gbm_sys::gbm_bo
// so that we can call gbm_sys::gbm_surface_lock_front_buffer() directly.
mod gbm_rs_workaround {
use super::OwnedFramebufferHandle;

pub(super) struct GbmBo {
pub(super) bo: *mut gbm_sys::gbm_bo,
pub(super) surface: *mut gbm_sys::gbm_surface,
}

impl Drop for GbmBo {
fn drop(&mut self) {
unsafe {
gbm_sys::gbm_surface_release_buffer(self.surface, self.bo);
}
}
}

impl GbmBo {
pub(super) fn set_userdata(
&mut self,
fb: OwnedFramebufferHandle,
) -> Result<(), Box<dyn std::error::Error>> {
// Take old user data first
let old_userdata = unsafe { gbm_sys::gbm_bo_get_user_data(self.bo) };

unsafe extern "C" fn destroy_helper(
_: *mut gbm_sys::gbm_bo,
user_data: *mut std::ffi::c_void,
) {
let fb_raw = user_data as *mut OwnedFramebufferHandle;
drop(Box::from_raw(fb_raw));
}

let boxed_fb = Box::new(fb);
unsafe {
gbm_sys::gbm_bo_set_user_data(
self.bo,
Box::into_raw(boxed_fb) as _,
Some(destroy_helper),
)
}

if !old_userdata.is_null() {
drop(unsafe { Box::from_raw(old_userdata as *mut OwnedFramebufferHandle) });
}

Ok(())
}
}

impl drm::buffer::Buffer for GbmBo {
fn size(&self) -> (u32, u32) {
unsafe { (gbm_sys::gbm_bo_get_width(self.bo), gbm_sys::gbm_bo_get_height(self.bo)) }
}

fn format(&self) -> gbm::Format {
unsafe { gbm_sys::gbm_bo_get_format(self.bo) }
.try_into()
.expect("gbm_bo_get_format returned invalid format")
}

fn pitch(&self) -> u32 {
unsafe { gbm_sys::gbm_bo_get_stride(self.bo) }
}

fn handle(&self) -> drm::buffer::Handle {
unsafe {
drm::buffer::Handle::from(std::num::NonZeroU32::new_unchecked(
gbm_sys::gbm_bo_get_handle(self.bo).u32_,
))
}
}
}

impl drm::buffer::PlanarBuffer for GbmBo {
fn size(&self) -> (u32, u32) {
unsafe { (gbm_sys::gbm_bo_get_width(self.bo), gbm_sys::gbm_bo_get_height(self.bo)) }
}

fn format(&self) -> gbm::Format {
unsafe { gbm_sys::gbm_bo_get_format(self.bo) }
.try_into()
.expect("gbm_bo_get_format returned invalid format")
}

fn pitches(&self) -> [u32; 4] {
let mut pitches = [0, 0, 0, 0];
let planes = unsafe { gbm_sys::gbm_bo_get_plane_count(self.bo) };

for i in 0..planes {
let pitch = unsafe { gbm_sys::gbm_bo_get_stride_for_plane(self.bo, i) };
pitches[i as usize] = pitch;
}

pitches
}

fn handles(&self) -> [Option<drm::buffer::Handle>; 4] {
let mut handles = [None, None, None, None];
let planes = unsafe { gbm_sys::gbm_bo_get_plane_count(self.bo) };

for i in 0..planes {
let handle = unsafe { gbm_sys::gbm_bo_get_handle_for_plane(self.bo, i) };
handles[i as usize] = Some(drm::buffer::Handle::from(
std::num::NonZeroU32::new(unsafe { handle.u32_ })
.expect("received invalid gbm bo plane handle"),
));
}

handles
}

fn offsets(&self) -> [u32; 4] {
let mut offsets = [0, 0, 0, 0];
let planes = unsafe { gbm_sys::gbm_bo_get_plane_count(self.bo) };

for i in 0..planes {
let offset = unsafe { gbm_sys::gbm_bo_get_offset(self.bo, i) };
offsets[i as usize] = offset;
}

offsets
}
}
}
2 changes: 1 addition & 1 deletion internal/backends/linuxkms/drmoutput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl DrmOutput {
return Ok(());
}

let source = calloop::generic::Generic::new_with_error::<drm::SystemError>(
let source = calloop::generic::Generic::new_with_error::<std::io::Error>(
self.drm_device.0.clone(),
calloop::Interest::READ,
calloop::Mode::Level,
Expand Down

0 comments on commit 447ded6

Please sign in to comment.