smithay/backend/allocator/
mod.rs1pub mod dmabuf;
19#[cfg(feature = "backend_drm")]
20pub mod dumb;
21pub mod format;
22#[cfg(feature = "backend_gbm")]
23pub mod gbm;
24#[cfg(feature = "backend_vulkan")]
25pub mod vulkan;
26
27mod swapchain;
28use std::{
29 cell::RefCell,
30 rc::Rc,
31 sync::{Arc, Mutex},
32};
33
34use crate::utils::{Buffer as BufferCoords, Size};
35pub use swapchain::{Slot, Swapchain};
36
37pub use drm_fourcc::{
38 DrmFormat as Format, DrmFourcc as Fourcc, DrmModifier as Modifier, DrmVendor as Vendor,
39 UnrecognizedFourcc, UnrecognizedVendor,
40};
41
42pub trait Buffer {
44 fn width(&self) -> u32 {
46 self.size().w as u32
47 }
48 fn height(&self) -> u32 {
50 self.size().h as u32
51 }
52 fn size(&self) -> Size<i32, BufferCoords>;
54 fn format(&self) -> Format;
56}
57
58pub trait Allocator {
60 type Buffer: Buffer;
62 type Error: std::error::Error;
64
65 fn create_buffer(
67 &mut self,
68 width: u32,
69 height: u32,
70 fourcc: Fourcc,
71 modifiers: &[Modifier],
72 ) -> Result<Self::Buffer, Self::Error>;
73}
74
75impl<A: Allocator> Allocator for Arc<Mutex<A>> {
78 type Buffer = A::Buffer;
79 type Error = A::Error;
80
81 fn create_buffer(
82 &mut self,
83 width: u32,
84 height: u32,
85 fourcc: Fourcc,
86 modifiers: &[Modifier],
87 ) -> Result<Self::Buffer, Self::Error> {
88 let mut guard = self.lock().unwrap();
89 guard.create_buffer(width, height, fourcc, modifiers)
90 }
91}
92
93impl<A: Allocator> Allocator for Rc<RefCell<A>> {
94 type Buffer = A::Buffer;
95 type Error = A::Error;
96
97 fn create_buffer(
98 &mut self,
99 width: u32,
100 height: u32,
101 fourcc: Fourcc,
102 modifiers: &[Modifier],
103 ) -> Result<Self::Buffer, Self::Error> {
104 self.borrow_mut().create_buffer(width, height, fourcc, modifiers)
105 }
106}
107
108impl<B: Buffer, E: std::error::Error> Allocator for Box<dyn Allocator<Buffer = B, Error = E> + 'static> {
109 type Buffer = B;
110 type Error = E;
111
112 fn create_buffer(
113 &mut self,
114 width: u32,
115 height: u32,
116 fourcc: Fourcc,
117 modifiers: &[Modifier],
118 ) -> Result<B, E> {
119 (**self).create_buffer(width, height, fourcc, modifiers)
120 }
121}