dmabuf's take ownership over their contents -> do not reference original buffer

This commit is contained in:
Victor Brekenfeld 2021-04-11 22:59:52 +02:00
parent 9d021d52a4
commit 58f22afa40
1 changed files with 28 additions and 15 deletions

View File

@ -4,13 +4,14 @@ use std::os::unix::io::RawFd;
const MAX_PLANES: usize = 4; const MAX_PLANES: usize = 4;
struct DmabufInternal { pub(crate) struct DmabufInternal {
src: Box<dyn Buffer + 'static>, pub num_planes: usize,
pub offsets: [u32; MAX_PLANES],
num_planes: usize, pub strides: [u32; MAX_PLANES],
offsets: [u32; MAX_PLANES], pub fds: [RawFd; MAX_PLANES],
strides: [u32; MAX_PLANES], pub width: u32,
fds: [RawFd; MAX_PLANES], pub height: u32,
pub format: Format,
} }
#[derive(Clone)] #[derive(Clone)]
@ -46,15 +47,15 @@ impl PartialEq for WeakDmabuf {
impl Buffer for Dmabuf { impl Buffer for Dmabuf {
fn width(&self) -> u32 { fn width(&self) -> u32 {
self.0.src.width() self.0.width
} }
fn height(&self) -> u32 { fn height(&self) -> u32 {
self.0.src.height() self.0.height
} }
fn format(&self) -> Format { fn format(&self) -> Format {
self.0.src.format() self.0.format
} }
} }
@ -81,12 +82,14 @@ impl Dmabuf {
let mut fds = fds.iter().take(planes).chain(end_fds.iter()); let mut fds = fds.iter().take(planes).chain(end_fds.iter());
Some(Dmabuf(Arc::new(DmabufInternal { Some(Dmabuf(Arc::new(DmabufInternal {
src: Box::new(src),
num_planes: planes, num_planes: planes,
offsets: [*offsets.next().unwrap(), *offsets.next().unwrap(), *offsets.next().unwrap(), *offsets.next().unwrap()], offsets: [*offsets.next().unwrap(), *offsets.next().unwrap(), *offsets.next().unwrap(), *offsets.next().unwrap()],
strides: [*strides.next().unwrap(), *strides.next().unwrap(), *strides.next().unwrap(), *strides.next().unwrap()], strides: [*strides.next().unwrap(), *strides.next().unwrap(), *strides.next().unwrap(), *strides.next().unwrap()],
fds: [*fds.next().unwrap(), *fds.next().unwrap(), *fds.next().unwrap(), *fds.next().unwrap()], fds: [*fds.next().unwrap(), *fds.next().unwrap(), *fds.next().unwrap(), *fds.next().unwrap()],
width: src.width(),
height: src.height(),
format: src.format(),
}))) })))
} }
@ -103,17 +106,27 @@ impl Dmabuf {
} }
pub fn has_modifier(&self) -> bool { pub fn has_modifier(&self) -> bool {
self.0.src.format().modifier != Modifier::Invalid && self.0.format.modifier != Modifier::Invalid &&
self.0.src.format().modifier != Modifier::Linear self.0.format.modifier != Modifier::Linear
} }
pub fn weak(&self) -> WeakDmabuf { pub fn weak(&self) -> WeakDmabuf {
WeakDmabuf(Arc::downgrade(&self.0)) WeakDmabuf(Arc::downgrade(&self.0))
} }
} }
impl WeakDmabuf { impl WeakDmabuf {
pub fn upgrade(&self) -> Option<Dmabuf> { pub fn upgrade(&self) -> Option<Dmabuf> {
self.0.upgrade().map(|internal| Dmabuf(internal)) self.0.upgrade().map(|internal| Dmabuf(internal))
} }
}
impl Drop for DmabufInternal {
fn drop(&mut self) {
for fd in self.fds.iter() {
if *fd != 0 {
let _ = nix::unistd::close(*fd);
}
}
}
} }