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