allocator: Introdce AsDmabuf

This commit is contained in:
Victor Brekenfeld 2021-05-13 14:38:00 +02:00
parent 378f826c26
commit 5cf328a1b8
2 changed files with 27 additions and 10 deletions

View File

@ -66,7 +66,7 @@ impl Buffer for Dmabuf {
impl Dmabuf { impl Dmabuf {
pub(crate) fn new( pub(crate) fn new(
src: impl Buffer, src: &impl Buffer,
planes: usize, planes: usize,
offsets: &[u32], offsets: &[u32],
strides: &[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<Dmabuf, Self::Error>;
}
impl AsDmabuf for Dmabuf {
type Error = ();
fn export(&self) -> Result<Dmabuf, ()> {
Ok(self.clone())
}
}

View File

@ -1,6 +1,6 @@
//! Module for Buffers created using [libgbm](reexports::gbm) //! 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 gbm::{BufferObject as GbmBuffer, BufferObjectFlags, Device as GbmDevice};
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
@ -56,14 +56,14 @@ pub enum GbmConvertError {
InvalidFD, InvalidFD,
} }
impl<T> std::convert::TryFrom<GbmBuffer<T>> for Dmabuf { impl<T> AsDmabuf for GbmBuffer<T> {
type Error = GbmConvertError; type Error = GbmConvertError;
fn try_from(buf: GbmBuffer<T>) -> Result<Self, GbmConvertError> { fn export(&self) -> Result<Dmabuf, GbmConvertError> {
let planes = buf.plane_count()? as i32; let planes = self.plane_count()? as i32;
//TODO switch to gbm_bo_get_plane_fd when it lands //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"); let first = iter.next().expect("Encountered a buffer with zero planes");
// check that all handles are the same // check that all handles are the same
let handle = iter.try_fold(first, |first, next| { let handle = iter.try_fold(first, |first, next| {
@ -82,20 +82,20 @@ impl<T> std::convert::TryFrom<GbmBuffer<T>> for Dmabuf {
return Err(GbmConvertError::UnsupportedBuffer); //TODO 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.iter().any(|fd| fd == 0) {
if fds[0] < 0 { if fds[0] < 0 {
return Err(GbmConvertError::InvalidFD); return Err(GbmConvertError::InvalidFD);
} }
let offsets = (0i32..planes) let offsets = (0i32..planes)
.map(|i| buf.offset(i)) .map(|i| self.offset(i))
.collect::<Result<Vec<u32>, gbm::DeviceDestroyedError>>()?; .collect::<Result<Vec<u32>, gbm::DeviceDestroyedError>>()?;
let strides = (0i32..planes) let strides = (0i32..planes)
.map(|i| buf.stride_for_plane(i)) .map(|i| self.stride_for_plane(i))
.collect::<Result<Vec<u32>, gbm::DeviceDestroyedError>>()?; .collect::<Result<Vec<u32>, gbm::DeviceDestroyedError>>()?;
Ok(Dmabuf::new(buf, planes as usize, &offsets, &strides, &fds).unwrap()) Ok(Dmabuf::new(self, planes as usize, &offsets, &strides, &fds).unwrap())
} }
} }