From 11940fb6a550afbcc4140a504a56e792f98db5a7 Mon Sep 17 00:00:00 2001 From: kossLAN Date: Fri, 29 May 2026 11:45:26 -0400 Subject: [PATCH] allocator: move buffer allocation traits into workspace crate --- Cargo.lock | 11 ++++ Cargo.toml | 2 + allocator/Cargo.toml | 12 ++++ allocator/src/lib.rs | 95 +++++++++++++++++++++++++++++ src/allocator.rs | 66 ++++---------------- src/gfx_apis/vulkan/bo_allocator.rs | 6 +- src/ifs/jay_render_ctx.rs | 4 +- src/screenshoter.rs | 5 +- src/udmabuf.rs | 3 +- src/video/gbm.rs | 4 +- 10 files changed, 141 insertions(+), 67 deletions(-) create mode 100644 allocator/Cargo.toml create mode 100644 allocator/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 8c906b72..0ebf8e26 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -630,6 +630,16 @@ dependencies = [ "smallvec", ] +[[package]] +name = "jay-allocator" +version = "0.1.0" +dependencies = [ + "jay-formats", + "jay-video-types", + "thiserror", + "uapi", +] + [[package]] name = "jay-ash" version = "0.3.0+1.4.344" @@ -710,6 +720,7 @@ dependencies = [ "indexmap", "isnt 0.2.0", "jay-algorithms", + "jay-allocator", "jay-ash", "jay-async-engine", "jay-bufio", diff --git a/Cargo.toml b/Cargo.toml index 308701d4..456f55be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,6 +46,7 @@ members = [ "gfx-types", "theme", "clientmem", + "allocator", "pango", "libinput", "toml-config", @@ -95,6 +96,7 @@ jay-video-types = { version = "0.1.0", path = "video-types" } jay-gfx-types = { version = "0.1.0", path = "gfx-types" } jay-theme = { version = "0.1.0", path = "theme" } jay-clientmem = { version = "0.1.0", path = "clientmem" } +jay-allocator = { version = "0.1.0", path = "allocator" } jay-pango = { version = "0.1.0", path = "pango" } jay-libinput = { version = "0.1.0", path = "libinput" } diff --git a/allocator/Cargo.toml b/allocator/Cargo.toml new file mode 100644 index 00000000..8a6259d9 --- /dev/null +++ b/allocator/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "jay-allocator" +version = "0.1.0" +edition = "2024" +license = "GPL-3.0-only" + +[dependencies] +jay-formats = { version = "0.1.0", path = "../formats" } +jay-video-types = { version = "0.1.0", path = "../video-types" } + +thiserror = "2.0.11" +uapi = "0.2.13" diff --git a/allocator/src/lib.rs b/allocator/src/lib.rs new file mode 100644 index 00000000..a5b86a4f --- /dev/null +++ b/allocator/src/lib.rs @@ -0,0 +1,95 @@ +use { + jay_formats::Format, + jay_video_types::{ + Modifier, + dmabuf::{DmaBuf, DmaBufIds}, + }, + std::{ + error::Error, + ops::{BitOr, BitOrAssign, Not}, + rc::Rc, + }, + thiserror::Error, + uapi::{OwnedFd, c}, +}; + +#[derive(Debug, Error)] +#[error(transparent)] +pub struct AllocatorError(#[from] pub Box); + +#[derive(Copy, Clone, Debug, Eq, PartialEq)] +pub struct BufferUsage(u32); + +impl BufferUsage { + pub fn none() -> Self { + Self(0) + } + + pub fn contains(self, other: Self) -> bool { + self.0 & other.0 == other.0 + } +} + +impl BitOr for BufferUsage { + type Output = Self; + + fn bitor(self, rhs: Self) -> Self::Output { + Self(self.0 | rhs.0) + } +} + +impl BitOrAssign for BufferUsage { + fn bitor_assign(&mut self, rhs: Self) { + self.0 |= rhs.0; + } +} + +impl Not for BufferUsage { + type Output = Self; + + fn not(self) -> Self::Output { + Self(!self.0) + } +} + +pub const BO_USE_SCANOUT: BufferUsage = BufferUsage(1 << 0); +pub const BO_USE_CURSOR: BufferUsage = BufferUsage(1 << 1); +pub const BO_USE_RENDERING: BufferUsage = BufferUsage(1 << 2); +pub const BO_USE_WRITE: BufferUsage = BufferUsage(1 << 3); +pub const BO_USE_LINEAR: BufferUsage = BufferUsage(1 << 4); +pub const BO_USE_PROTECTED: BufferUsage = BufferUsage(1 << 5); + +pub trait Allocator { + fn drm(&self) -> Option<&dyn AllocatorDrm>; + fn create_bo( + &self, + dma_buf_ids: &DmaBufIds, + width: i32, + height: i32, + format: &'static Format, + modifiers: &[Modifier], + usage: BufferUsage, + ) -> Result, AllocatorError>; + fn import_dmabuf( + &self, + dmabuf: &DmaBuf, + usage: BufferUsage, + ) -> Result, AllocatorError>; +} + +pub trait AllocatorDrm { + fn dev(&self) -> c::dev_t; + fn dup_render_fd(&self) -> Result, AllocatorError>; +} + +pub trait BufferObject { + fn dmabuf(&self) -> &DmaBuf; + fn map_read(self: Rc) -> Result, AllocatorError>; + fn map_write(self: Rc) -> Result, AllocatorError>; +} + +pub trait MappedBuffer { + unsafe fn data(&self) -> &[u8]; + fn data_ptr(&self) -> *mut u8; + fn stride(&self) -> i32; +} diff --git a/src/allocator.rs b/src/allocator.rs index 91ec23d0..d8d14aef 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -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); +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, AllocatorError>; - fn import_dmabuf( - &self, - dmabuf: &DmaBuf, - usage: BufferUsage, - ) -> Result, AllocatorError>; -} - -pub trait BufferObject { - fn dmabuf(&self) -> &DmaBuf; - fn map_read(self: Rc) -> Result, AllocatorError>; - fn map_write(self: Rc) -> Result, 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, AllocatorError> { + self.dup_render() + .map(|drm| drm.fd().clone()) + .map_err(|e| AllocatorError(Box::new(e))) + } } diff --git a/src/gfx_apis/vulkan/bo_allocator.rs b/src/gfx_apis/vulkan/bo_allocator.rs index 6a7ffcfa..a619534f 100644 --- a/src/gfx_apis/vulkan/bo_allocator.rs +++ b/src/gfx_apis/vulkan/bo_allocator.rs @@ -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) } diff --git a/src/ifs/jay_render_ctx.rs b/src/ifs/jay_render_ctx.rs index 74b058e4..8256bc8d 100644 --- a/src/ifs/jay_render_ctx.rs +++ b/src/ifs/jay_render_ctx.rs @@ -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)); } diff --git a/src/screenshoter.rs b/src/screenshoter.rs index 732aa9f5..5477f980 100644 --- a/src/screenshoter.rs +++ b/src/screenshoter.rs @@ -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 }) diff --git a/src/udmabuf.rs b/src/udmabuf.rs index 96acf0ac..7240e512 100644 --- a/src/udmabuf.rs +++ b/src/udmabuf.rs @@ -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 } diff --git a/src/video/gbm.rs b/src/video/gbm.rs index eb34ca71..ec924508 100644 --- a/src/video/gbm.rs +++ b/src/video/gbm.rs @@ -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) }