1
0
Fork 0
forked from wry/wry

Merge pull request #748 from mahkoh/jorth/gdm-dev-destroy

gbm: ensure that device is not destroyed before BOs
This commit is contained in:
mahkoh 2026-02-18 06:05:54 +01:00 committed by GitHub
commit b921c81e5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -124,9 +124,13 @@ unsafe extern "C" {
fn gbm_bo_unmap(bo: *mut Bo, map_data: *mut u8); fn gbm_bo_unmap(bo: *mut Bo, map_data: *mut u8);
} }
struct DeviceHolder {
dev: *mut Device,
}
pub struct GbmDevice { pub struct GbmDevice {
pub drm: Drm, pub drm: Drm,
dev: *mut Device, dev: Rc<DeviceHolder>,
} }
impl Debug for GbmDevice { impl Debug for GbmDevice {
@ -137,6 +141,7 @@ impl Debug for GbmDevice {
struct BoHolder { struct BoHolder {
bo: *mut Bo, bo: *mut Bo,
_dev: Rc<DeviceHolder>,
} }
pub struct GbmBo { pub struct GbmBo {
@ -214,12 +219,13 @@ impl GbmDevice {
if dev.is_null() { if dev.is_null() {
Err(GbmError::CreateDevice) Err(GbmError::CreateDevice)
} else { } else {
let dev = Rc::new(DeviceHolder { dev });
Ok(Self { drm, dev }) Ok(Self { drm, dev })
} }
} }
pub fn raw(&self) -> *mut Device { pub fn raw(&self) -> *mut Device {
self.dev self.dev.dev
} }
pub fn create_bo<'a>( pub fn create_bo<'a>(
@ -243,7 +249,7 @@ impl GbmDevice {
(modifiers.as_ptr() as _, modifiers.len() as _) (modifiers.as_ptr() as _, modifiers.len() as _)
}; };
let bo = gbm_bo_create_with_modifiers2( let bo = gbm_bo_create_with_modifiers2(
self.dev, self.dev.dev,
width as _, width as _,
height as _, height as _,
format.drm, format.drm,
@ -254,7 +260,10 @@ impl GbmDevice {
if bo.is_null() { if bo.is_null() {
return Err(GbmError::CreateBo(OsError::default())); return Err(GbmError::CreateBo(OsError::default()));
} }
let bo = BoHolder { bo }; let bo = BoHolder {
bo,
_dev: self.dev.clone(),
};
let mut dma = export_bo(dma_buf_ids, bo.bo)?; let mut dma = export_bo(dma_buf_ids, bo.bo)?;
if let [modifier] = *modifiers { if let [modifier] = *modifiers {
dma.modifier = modifier; dma.modifier = modifier;
@ -281,7 +290,7 @@ impl GbmDevice {
} }
unsafe { unsafe {
let bo = gbm_bo_import( let bo = gbm_bo_import(
self.dev, self.dev.dev,
GBM_BO_IMPORT_FD_MODIFIER, GBM_BO_IMPORT_FD_MODIFIER,
&mut import as *const _ as _, &mut import as *const _ as _,
usage, usage,
@ -289,7 +298,10 @@ impl GbmDevice {
if bo.is_null() { if bo.is_null() {
return Err(GbmError::CreateBo(OsError::default())); return Err(GbmError::CreateBo(OsError::default()));
} }
let bo = BoHolder { bo }; let bo = BoHolder {
bo,
_dev: self.dev.clone(),
};
Ok(GbmBo { Ok(GbmBo {
bo, bo,
dmabuf: dmabuf.clone(), dmabuf: dmabuf.clone(),
@ -348,7 +360,7 @@ fn map_usage(usage: BufferUsage) -> u32 {
gbm gbm
} }
impl Drop for GbmDevice { impl Drop for DeviceHolder {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
gbm_device_destroy(self.dev); gbm_device_destroy(self.dev);