| 1 | use std::sync::Arc; |
| 2 | |
| 3 | use smithay::backend::allocator::format::get_bpp; |
| 4 | use smithay::backend::allocator::Fourcc; |
| 5 | use smithay::utils::{Buffer, Logical, Scale, Size, Transform}; |
| 6 | |
| 7 | #[derive(Clone)] |
| 8 | pub struct MemoryBuffer { |
| 9 | data: Arc<[u8]>, |
| 10 | format: Fourcc, |
| 11 | size: Size<i32, Buffer>, |
| 12 | scale: Scale<f64>, |
| 13 | transform: Transform, |
| 14 | } |
| 15 | |
| 16 | impl MemoryBuffer { |
| 17 | pub fn new( |
| 18 | data: impl Into<Arc<[u8]>>, |
| 19 | format: Fourcc, |
| 20 | size: impl Into<Size<i32, Buffer>>, |
| 21 | scale: impl Into<Scale<f64>>, |
| 22 | transform: Transform, |
| 23 | ) -> Self { |
| 24 | let data = data.into(); |
| 25 | |
| 26 | let size = size.into(); |
| 27 | let stride = |
| 28 | size.w * (get_bpp(format).expect("Format with unknown bits per pixel") / 8) as i32; |
| 29 | assert!(data.len() >= (stride * size.h) as usize); |
| 30 | |
| 31 | Self { |
| 32 | data, |
| 33 | format, |
| 34 | size, |
| 35 | scale: scale.into(), |
| 36 | transform, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | pub fn data(&self) -> &[u8] { |
| 41 | &self.data |
| 42 | } |
| 43 | |
| 44 | pub fn format(&self) -> Fourcc { |
| 45 | self.format |
| 46 | } |
| 47 | |
| 48 | pub fn size(&self) -> Size<i32, Buffer> { |
| 49 | self.size |
| 50 | } |
| 51 | |
| 52 | pub fn scale(&self) -> Scale<f64> { |
| 53 | self.scale |
| 54 | } |
| 55 | |
| 56 | pub fn transform(&self) -> Transform { |
| 57 | self.transform |
| 58 | } |
| 59 | |
| 60 | pub fn logical_size(&self) -> Size<f64, Logical> { |
| 61 | self.size.to_f64().to_logical(self.scale, self.transform) |
| 62 | } |
| 63 | } |