use super::{Handle, MemoryType};
use crate::dma_buf;
use crate::types::{Error, Result};
use crate::utils;
use std::os::fd::OwnedFd;
pub struct Backend {
fd: OwnedFd,
}
impl super::Backend for Backend {
fn bind_memory(
&self,
handle: &mut Handle,
mt: MemoryType,
dmabuf: Option<OwnedFd>,
) -> Result<()> {
let alloc = |size| {
let memfd = utils::memfd_create("udmabuf", size)?;
utils::udmabuf_alloc(&self.fd, memfd, size)
};
dma_buf::bind_memory(handle, mt, dmabuf, alloc)
}
}
#[derive(Default)]
pub struct Builder;
impl Builder {
pub fn new() -> Self {
Default::default()
}
pub fn build(self) -> Result<Backend> {
if !utils::udmabuf_exists() {
return Error::unsupported();
}
let fd = utils::udmabuf_open()?;
Ok(Backend { fd })
}
}