gbm: allow to import dmabufs

This commit is contained in:
Victor Brekenfeld 2021-04-11 23:00:22 +02:00
parent 58f22afa40
commit bcf6a5c4b9
2 changed files with 32 additions and 5 deletions

View File

@ -15,10 +15,10 @@ pub(crate) struct DmabufInternal {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Dmabuf(Arc<DmabufInternal>); pub struct Dmabuf(pub(crate) Arc<DmabufInternal>);
#[derive(Clone)] #[derive(Clone)]
pub struct WeakDmabuf(Weak<DmabufInternal>); pub struct WeakDmabuf(pub(crate) Weak<DmabufInternal>);
impl PartialEq for Dmabuf { impl PartialEq for Dmabuf {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
@ -60,9 +60,8 @@ impl Buffer for Dmabuf {
} }
impl Dmabuf { impl Dmabuf {
pub fn new( pub(crate) fn new(
src: impl Buffer + 'static, src: impl Buffer,
planes: usize, planes: usize,
offsets: &[u32], offsets: &[u32],
strides: &[u32], strides: &[u32],

View File

@ -81,3 +81,31 @@ impl<T> std::convert::TryFrom<GbmBuffer<T>> for Dmabuf {
Ok(Dmabuf::new(buf, planes as usize, &offsets, &strides, &fds).unwrap()) Ok(Dmabuf::new(buf, planes as usize, &offsets, &strides, &fds).unwrap())
} }
} }
impl Dmabuf {
pub fn import<A: AsRawFd + 'static, T>(&self, gbm: &GbmDevice<A>, usage: BufferObjectFlags) -> std::io::Result<GbmBuffer<T>> {
let buf = &*self.0;
if self.has_modifier() || buf.num_planes > 1 || buf.offsets[0] != 0 {
gbm.import_buffer_object_from_dma_buf_with_modifiers(
buf.num_planes as u32,
buf.fds,
buf.width,
buf.height,
buf.format.code,
usage,
unsafe { std::mem::transmute::<[u32; 4], [i32; 4]>(buf.strides) },
unsafe { std::mem::transmute::<[u32; 4], [i32; 4]>(buf.offsets) },
buf.format.modifier,
)
} else {
gbm.import_buffer_object_from_dma_buf(
buf.fds[0],
buf.width,
buf.height,
buf.strides[0],
buf.format.code,
if buf.format.modifier == Modifier::Linear { usage | BufferObjectFlags::LINEAR } else { usage },
)
}
}
}