1 //!
2 //! # DumbBuffer
3 //!
4 //! Memory-supported, slow, but easy & cross-platform buffer implementation
5 //!
6 
7 use crate::buffer;
8 
9 use std::borrow::{Borrow, BorrowMut};
10 use std::ops::{Deref, DerefMut};
11 
12 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
13 /// Slow, but generic [`buffer::Buffer`] implementation
14 pub struct DumbBuffer {
15     pub(crate) size: (u32, u32),
16     pub(crate) length: usize,
17     pub(crate) format: buffer::DrmFourcc,
18     pub(crate) pitch: u32,
19     pub(crate) handle: buffer::Handle,
20 }
21 
22 /// Mapping of a [`DumbBuffer`]
23 pub struct DumbMapping<'a> {
24     pub(crate) _phantom: core::marker::PhantomData<&'a ()>,
25     pub(crate) map: &'a mut [u8],
26 }
27 
28 impl AsRef<[u8]> for DumbMapping<'_> {
as_ref(&self) -> &[u8]29     fn as_ref(&self) -> &[u8] {
30         self.map
31     }
32 }
33 
34 impl AsMut<[u8]> for DumbMapping<'_> {
as_mut(&mut self) -> &mut [u8]35     fn as_mut(&mut self) -> &mut [u8] {
36         self.map
37     }
38 }
39 
40 impl Borrow<[u8]> for DumbMapping<'_> {
borrow(&self) -> &[u8]41     fn borrow(&self) -> &[u8] {
42         self.map
43     }
44 }
45 
46 impl BorrowMut<[u8]> for DumbMapping<'_> {
borrow_mut(&mut self) -> &mut [u8]47     fn borrow_mut(&mut self) -> &mut [u8] {
48         self.map
49     }
50 }
51 
52 impl Deref for DumbMapping<'_> {
53     type Target = [u8];
54 
deref(&self) -> &Self::Target55     fn deref(&self) -> &Self::Target {
56         self.map
57     }
58 }
59 
60 impl DerefMut for DumbMapping<'_> {
deref_mut(&mut self) -> &mut Self::Target61     fn deref_mut(&mut self) -> &mut Self::Target {
62         self.map
63     }
64 }
65 
66 impl<'a> Drop for DumbMapping<'a> {
drop(&mut self)67     fn drop(&mut self) {
68         unsafe {
69             rustix::mm::munmap(self.map.as_mut_ptr() as *mut _, self.map.len())
70                 .expect("Unmap failed");
71         }
72     }
73 }
74 
75 impl buffer::Buffer for DumbBuffer {
size(&self) -> (u32, u32)76     fn size(&self) -> (u32, u32) {
77         self.size
78     }
format(&self) -> buffer::DrmFourcc79     fn format(&self) -> buffer::DrmFourcc {
80         self.format
81     }
pitch(&self) -> u3282     fn pitch(&self) -> u32 {
83         self.pitch
84     }
handle(&self) -> buffer::Handle85     fn handle(&self) -> buffer::Handle {
86         self.handle
87     }
88 }
89