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",
|
"smallvec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "jay-allocator"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"jay-formats",
|
||||||
|
"jay-video-types",
|
||||||
|
"thiserror",
|
||||||
|
"uapi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jay-ash"
|
name = "jay-ash"
|
||||||
version = "0.3.0+1.4.344"
|
version = "0.3.0+1.4.344"
|
||||||
|
|
@ -710,6 +720,7 @@ dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
"isnt 0.2.0",
|
"isnt 0.2.0",
|
||||||
"jay-algorithms",
|
"jay-algorithms",
|
||||||
|
"jay-allocator",
|
||||||
"jay-ash",
|
"jay-ash",
|
||||||
"jay-async-engine",
|
"jay-async-engine",
|
||||||
"jay-bufio",
|
"jay-bufio",
|
||||||
|
|
|
||||||
|
|
@ -46,6 +46,7 @@ members = [
|
||||||
"gfx-types",
|
"gfx-types",
|
||||||
"theme",
|
"theme",
|
||||||
"clientmem",
|
"clientmem",
|
||||||
|
"allocator",
|
||||||
"pango",
|
"pango",
|
||||||
"libinput",
|
"libinput",
|
||||||
"toml-config",
|
"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-gfx-types = { version = "0.1.0", path = "gfx-types" }
|
||||||
jay-theme = { version = "0.1.0", path = "theme" }
|
jay-theme = { version = "0.1.0", path = "theme" }
|
||||||
jay-clientmem = { version = "0.1.0", path = "clientmem" }
|
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-pango = { version = "0.1.0", path = "pango" }
|
||||||
jay-libinput = { version = "0.1.0", path = "libinput" }
|
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 {
|
pub use jay_allocator::*;
|
||||||
crate::{
|
|
||||||
format::Format,
|
|
||||||
video::{
|
|
||||||
Modifier,
|
|
||||||
dmabuf::{DmaBuf, DmaBufIds},
|
|
||||||
drm::Drm,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
std::{error::Error, rc::Rc},
|
|
||||||
thiserror::Error,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
use crate::video::drm::Drm;
|
||||||
#[error(transparent)]
|
|
||||||
pub struct AllocatorError(#[from] pub Box<dyn Error + Send>);
|
|
||||||
|
|
||||||
bitflags! {
|
impl AllocatorDrm for Drm {
|
||||||
BufferUsage: u32;
|
fn dev(&self) -> uapi::c::dev_t {
|
||||||
BO_USE_SCANOUT,
|
self.dev()
|
||||||
BO_USE_CURSOR,
|
}
|
||||||
BO_USE_RENDERING,
|
|
||||||
BO_USE_WRITE,
|
fn dup_render_fd(&self) -> Result<std::rc::Rc<uapi::OwnedFd>, AllocatorError> {
|
||||||
BO_USE_LINEAR,
|
self.dup_render()
|
||||||
BO_USE_PROTECTED,
|
.map(|drm| drm.fd().clone())
|
||||||
}
|
.map_err(|e| AllocatorError(Box::new(e)))
|
||||||
|
}
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
use {
|
use {
|
||||||
crate::{
|
crate::{
|
||||||
allocator::{
|
allocator::{
|
||||||
Allocator, AllocatorError, BO_USE_RENDERING, BO_USE_WRITE, BufferObject, BufferUsage,
|
Allocator, AllocatorDrm, AllocatorError, BO_USE_RENDERING, BO_USE_WRITE, BufferObject,
|
||||||
MappedBuffer,
|
BufferUsage, MappedBuffer,
|
||||||
},
|
},
|
||||||
format::Format,
|
format::Format,
|
||||||
gfx_apis::vulkan::{
|
gfx_apis::vulkan::{
|
||||||
|
|
@ -429,7 +429,7 @@ impl VulkanBoAllocator {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Allocator for VulkanBoAllocator {
|
impl Allocator for VulkanBoAllocator {
|
||||||
fn drm(&self) -> Option<&Drm> {
|
fn drm(&self) -> Option<&dyn AllocatorDrm> {
|
||||||
Some(&self.data.drm)
|
Some(&self.data.drm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,8 +58,8 @@ impl JayRenderCtx {
|
||||||
}
|
}
|
||||||
let allocator = ctx.allocator();
|
let allocator = ctx.allocator();
|
||||||
match allocator.drm() {
|
match allocator.drm() {
|
||||||
Some(drm) => match drm.dup_render() {
|
Some(drm) => match drm.dup_render_fd() {
|
||||||
Ok(d) => fd = Some(d.fd().clone()),
|
Ok(d) => fd = Some(d),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Could not dup drm fd: {}", ErrorFmt(e));
|
log::error!("Could not dup drm fd: {}", ErrorFmt(e));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,6 @@ use {
|
||||||
scale::Scale,
|
scale::Scale,
|
||||||
state::State,
|
state::State,
|
||||||
tree::Transform,
|
tree::Transform,
|
||||||
video::drm::DrmError,
|
|
||||||
},
|
},
|
||||||
indexmap::IndexMap,
|
indexmap::IndexMap,
|
||||||
std::{ops::Deref, rc::Rc},
|
std::{ops::Deref, rc::Rc},
|
||||||
|
|
@ -24,8 +23,6 @@ pub enum ScreenshooterError {
|
||||||
AllocatorError(#[from] AllocatorError),
|
AllocatorError(#[from] AllocatorError),
|
||||||
#[error(transparent)]
|
#[error(transparent)]
|
||||||
RenderError(#[from] GfxError),
|
RenderError(#[from] GfxError),
|
||||||
#[error(transparent)]
|
|
||||||
DrmError(#[from] DrmError),
|
|
||||||
#[error("Render context does not support XRGB8888")]
|
#[error("Render context does not support XRGB8888")]
|
||||||
XRGB8888,
|
XRGB8888,
|
||||||
#[error("Render context supports no modifiers for XRGB8888 rendering")]
|
#[error("Render context supports no modifiers for XRGB8888 rendering")]
|
||||||
|
|
@ -93,7 +90,7 @@ pub fn take_screenshot(
|
||||||
state.color_manager.srgb_linear(),
|
state.color_manager.srgb_linear(),
|
||||||
)?;
|
)?;
|
||||||
let drm = match allocator.drm() {
|
let drm = match allocator.drm() {
|
||||||
Some(drm) => Some(drm.dup_render()?.fd().clone()),
|
Some(drm) => Some(drm.dup_render_fd()?),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
Ok(Screenshot { drm, bo })
|
Ok(Screenshot { drm, bo })
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@ use {
|
||||||
video::{
|
video::{
|
||||||
LINEAR_MODIFIER, LINEAR_STRIDE_ALIGN, Modifier,
|
LINEAR_MODIFIER, LINEAR_STRIDE_ALIGN, Modifier,
|
||||||
dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec},
|
dmabuf::{DmaBuf, DmaBufIds, DmaBufPlane, PlaneVec},
|
||||||
drm::Drm,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
std::{ptr, rc::Rc},
|
std::{ptr, rc::Rc},
|
||||||
|
|
@ -169,7 +168,7 @@ impl Udmabuf {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Allocator for Udmabuf {
|
impl Allocator for Udmabuf {
|
||||||
fn drm(&self) -> Option<&Drm> {
|
fn drm(&self) -> Option<&dyn crate::allocator::AllocatorDrm> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
use {
|
use {
|
||||||
crate::{
|
crate::{
|
||||||
allocator::{
|
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,
|
BO_USE_RENDERING, BO_USE_SCANOUT, BO_USE_WRITE, BufferObject, BufferUsage,
|
||||||
MappedBuffer,
|
MappedBuffer,
|
||||||
},
|
},
|
||||||
|
|
@ -323,7 +323,7 @@ impl GbmDevice {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Allocator for GbmDevice {
|
impl Allocator for GbmDevice {
|
||||||
fn drm(&self) -> Option<&Drm> {
|
fn drm(&self) -> Option<&dyn AllocatorDrm> {
|
||||||
Some(&self.drm)
|
Some(&self.drm)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue