1
0
Fork 0
forked from wry/wry

allocator: move buffer allocation traits into workspace crate

This commit is contained in:
kossLAN 2026-05-29 11:45:26 -04:00
parent 663cfb3ca3
commit 11940fb6a5
No known key found for this signature in database
10 changed files with 141 additions and 67 deletions

View file

@ -1,57 +1,15 @@
use {
crate::{
format::Format,
video::{
Modifier,
dmabuf::{DmaBuf, DmaBufIds},
drm::Drm,
},
},
std::{error::Error, rc::Rc},
thiserror::Error,
};
pub use jay_allocator::*;
#[derive(Debug, Error)]
#[error(transparent)]
pub struct AllocatorError(#[from] pub Box<dyn Error + Send>);
use crate::video::drm::Drm;
bitflags! {
BufferUsage: u32;
BO_USE_SCANOUT,
BO_USE_CURSOR,
BO_USE_RENDERING,
BO_USE_WRITE,
BO_USE_LINEAR,
BO_USE_PROTECTED,
}
pub trait Allocator {
fn drm(&self) -> Option<&Drm>;
fn create_bo(
&self,
dma_buf_ids: &DmaBufIds,
width: i32,
height: i32,
format: &'static Format,
modifiers: &[Modifier],
usage: BufferUsage,
) -> Result<Rc<dyn BufferObject>, AllocatorError>;
fn import_dmabuf(
&self,
dmabuf: &DmaBuf,
usage: BufferUsage,
) -> Result<Rc<dyn BufferObject>, AllocatorError>;
}
pub trait BufferObject {
fn dmabuf(&self) -> &DmaBuf;
fn map_read(self: Rc<Self>) -> Result<Box<dyn MappedBuffer>, AllocatorError>;
fn map_write(self: Rc<Self>) -> Result<Box<dyn MappedBuffer>, AllocatorError>;
}
pub trait MappedBuffer {
unsafe fn data(&self) -> &[u8];
#[cfg_attr(not(feature = "it"), expect(dead_code))]
fn data_ptr(&self) -> *mut u8;
fn stride(&self) -> i32;
impl AllocatorDrm for Drm {
fn dev(&self) -> uapi::c::dev_t {
self.dev()
}
fn dup_render_fd(&self) -> Result<std::rc::Rc<uapi::OwnedFd>, AllocatorError> {
self.dup_render()
.map(|drm| drm.fd().clone())
.map_err(|e| AllocatorError(Box::new(e)))
}
}

View file

@ -1,8 +1,8 @@
use {
crate::{
allocator::{
Allocator, AllocatorError, BO_USE_RENDERING, BO_USE_WRITE, BufferObject, BufferUsage,
MappedBuffer,
Allocator, AllocatorDrm, AllocatorError, BO_USE_RENDERING, BO_USE_WRITE, BufferObject,
BufferUsage, MappedBuffer,
},
format::Format,
gfx_apis::vulkan::{
@ -429,7 +429,7 @@ impl VulkanBoAllocator {
}
impl Allocator for VulkanBoAllocator {
fn drm(&self) -> Option<&Drm> {
fn drm(&self) -> Option<&dyn AllocatorDrm> {
Some(&self.data.drm)
}

View file

@ -58,8 +58,8 @@ impl JayRenderCtx {
}
let allocator = ctx.allocator();
match allocator.drm() {
Some(drm) => match drm.dup_render() {
Ok(d) => fd = Some(d.fd().clone()),
Some(drm) => match drm.dup_render_fd() {
Ok(d) => fd = Some(d),
Err(e) => {
log::error!("Could not dup drm fd: {}", ErrorFmt(e));
}

View file

@ -6,7 +6,6 @@ use {
scale::Scale,
state::State,
tree::Transform,
video::drm::DrmError,
},
indexmap::IndexMap,
std::{ops::Deref, rc::Rc},
@ -24,8 +23,6 @@ pub enum ScreenshooterError {
AllocatorError(#[from] AllocatorError),
#[error(transparent)]
RenderError(#[from] GfxError),
#[error(transparent)]
DrmError(#[from] DrmError),
#[error("Render context does not support XRGB8888")]
XRGB8888,
#[error("Render context supports no modifiers for XRGB8888 rendering")]
@ -93,7 +90,7 @@ pub fn take_screenshot(
state.color_manager.srgb_linear(),
)?;
let drm = match allocator.drm() {
Some(drm) => Some(drm.dup_render()?.fd().clone()),
Some(drm) => Some(drm.dup_render_fd()?),
_ => None,
};
Ok(Screenshot { drm, bo })

View file

@ -13,7 +13,6 @@ use {
video::{
LINEAR_MODIFIER, LINEAR_STRIDE_ALIGN, Modifier,
dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec},
drm::Drm,
},
},
std::{ptr, rc::Rc},
@ -169,7 +168,7 @@ impl Udmabuf {
}
impl Allocator for Udmabuf {
fn drm(&self) -> Option<&Drm> {
fn drm(&self) -> Option<&dyn crate::allocator::AllocatorDrm> {
None
}

View file

@ -3,7 +3,7 @@
use {
crate::{
allocator::{
Allocator, AllocatorError, BO_USE_CURSOR, BO_USE_LINEAR, BO_USE_PROTECTED,
Allocator, AllocatorDrm, AllocatorError, BO_USE_CURSOR, BO_USE_LINEAR, BO_USE_PROTECTED,
BO_USE_RENDERING, BO_USE_SCANOUT, BO_USE_WRITE, BufferObject, BufferUsage,
MappedBuffer,
},
@ -323,7 +323,7 @@ impl GbmDevice {
}
impl Allocator for GbmDevice {
fn drm(&self) -> Option<&Drm> {
fn drm(&self) -> Option<&dyn AllocatorDrm> {
Some(&self.drm)
}