allocator: move buffer allocation traits into workspace crate
This commit is contained in:
parent
663cfb3ca3
commit
11940fb6a5
10 changed files with 141 additions and 67 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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" }
|
||||
|
||||
|
|
|
|||
12
allocator/Cargo.toml
Normal file
12
allocator/Cargo.toml
Normal file
|
|
@ -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"
|
||||
95
allocator/src/lib.rs
Normal file
95
allocator/src/lib.rs
Normal file
|
|
@ -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<dyn Error + Send>);
|
||||
|
||||
#[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<Rc<dyn BufferObject>, AllocatorError>;
|
||||
fn import_dmabuf(
|
||||
&self,
|
||||
dmabuf: &DmaBuf,
|
||||
usage: BufferUsage,
|
||||
) -> Result<Rc<dyn BufferObject>, AllocatorError>;
|
||||
}
|
||||
|
||||
pub trait AllocatorDrm {
|
||||
fn dev(&self) -> c::dev_t;
|
||||
fn dup_render_fd(&self) -> Result<Rc<OwnedFd>, 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];
|
||||
fn data_ptr(&self) -> *mut u8;
|
||||
fn stride(&self) -> i32;
|
||||
}
|
||||
|
|
@ -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)))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 })
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue