diff --git a/src/backend/allocator/dmabuf.rs b/src/backend/allocator/dmabuf.rs index 1b810bc..8d2eca6 100644 --- a/src/backend/allocator/dmabuf.rs +++ b/src/backend/allocator/dmabuf.rs @@ -66,7 +66,7 @@ impl Buffer for Dmabuf { impl Dmabuf { pub(crate) fn new( - src: impl Buffer, + src: &impl Buffer, planes: usize, offsets: &[u32], strides: &[u32], @@ -158,3 +158,20 @@ impl Drop for DmabufInternal { } } } + +/// Buffer that can be exported as Dmabufs +pub trait AsDmabuf { + /// Error type returned, if exporting fails + type Error; + + /// Export this buffer as a new Dmabuf + fn export(&self) -> Result; +} + +impl AsDmabuf for Dmabuf { + type Error = (); + + fn export(&self) -> Result { + Ok(self.clone()) + } +} diff --git a/src/backend/allocator/gbm.rs b/src/backend/allocator/gbm.rs index e5ef0ea..2621d2f 100644 --- a/src/backend/allocator/gbm.rs +++ b/src/backend/allocator/gbm.rs @@ -1,6 +1,6 @@ //! Module for Buffers created using [libgbm](reexports::gbm) -use super::{dmabuf::Dmabuf, Allocator, Buffer, Format, Fourcc, Modifier}; +use super::{dmabuf::{AsDmabuf, Dmabuf}, Allocator, Buffer, Format, Fourcc, Modifier}; use gbm::{BufferObject as GbmBuffer, BufferObjectFlags, Device as GbmDevice}; use std::os::unix::io::AsRawFd; @@ -56,14 +56,14 @@ pub enum GbmConvertError { InvalidFD, } -impl std::convert::TryFrom> for Dmabuf { +impl AsDmabuf for GbmBuffer { type Error = GbmConvertError; - fn try_from(buf: GbmBuffer) -> Result { - let planes = buf.plane_count()? as i32; + fn export(&self) -> Result { + let planes = self.plane_count()? as i32; //TODO switch to gbm_bo_get_plane_fd when it lands - let mut iter = (0i32..planes).map(|i| buf.handle_for_plane(i)); + let mut iter = (0i32..planes).map(|i| self.handle_for_plane(i)); let first = iter.next().expect("Encountered a buffer with zero planes"); // check that all handles are the same let handle = iter.try_fold(first, |first, next| { @@ -82,20 +82,20 @@ impl std::convert::TryFrom> for Dmabuf { return Err(GbmConvertError::UnsupportedBuffer); //TODO } - let fds = [buf.fd()?, 0, 0, 0]; + let fds = [self.fd()?, 0, 0, 0]; //if fds.iter().any(|fd| fd == 0) { if fds[0] < 0 { return Err(GbmConvertError::InvalidFD); } let offsets = (0i32..planes) - .map(|i| buf.offset(i)) + .map(|i| self.offset(i)) .collect::, gbm::DeviceDestroyedError>>()?; let strides = (0i32..planes) - .map(|i| buf.stride_for_plane(i)) + .map(|i| self.stride_for_plane(i)) .collect::, gbm::DeviceDestroyedError>>()?; - Ok(Dmabuf::new(buf, planes as usize, &offsets, &strides, &fds).unwrap()) + Ok(Dmabuf::new(self, planes as usize, &offsets, &strides, &fds).unwrap()) } }