Add MemoryBuffer
Browse files at this commit7019172b677d6332de53f24471dc2edbab30b2d4Parent: be62bd13 changed files +82 -0
- src/render_helpers/memory.rs +63 -0
- src/render_helpers/mod.rs +1 -0
- src/render_helpers/texture.rs +18 -0
src/render_helpers/memory.rsadded+63 -0
| @@ -0,0 +1,63 @@ | |||
| 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 | +} | ||
src/render_helpers/mod.rsmodified+1 -0
| @@ -21,6 +21,7 @@ pub mod border; | |||
| 21 | pub mod clipped_surface; | 21 | pub mod clipped_surface; |
| 22 | pub mod damage; | 22 | pub mod damage; |
| 23 | pub mod debug; | 23 | pub mod debug; |
| 24 | +pub mod memory; | ||
| 24 | pub mod offscreen; | 25 | pub mod offscreen; |
| 25 | pub mod primary_gpu_texture; | 26 | pub mod primary_gpu_texture; |
| 26 | pub mod render_elements; | 27 | pub mod render_elements; |
src/render_helpers/texture.rsmodified+18 -0
| @@ -4,6 +4,8 @@ use smithay::backend::renderer::utils::{CommitCounter, OpaqueRegions}; | |||
| 4 | use smithay::backend::renderer::{Frame as _, ImportMem, Renderer, Texture}; | 4 | use smithay::backend::renderer::{Frame as _, ImportMem, Renderer, Texture}; |
| 5 | use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size, Transform}; | 5 | use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size, Transform}; |
| 6 | 6 | ||
| 7 | +use super::memory::MemoryBuffer; | ||
| 8 | + | ||
| 7 | /// Smithay's texture buffer, but with fractional scale. | 9 | /// Smithay's texture buffer, but with fractional scale. |
| 8 | #[derive(Debug, Clone)] | 10 | #[derive(Debug, Clone)] |
| 9 | pub struct TextureBuffer<T> { | 11 | pub struct TextureBuffer<T> { |
| @@ -67,6 +69,22 @@ impl<T> TextureBuffer<T> { | |||
| 67 | )) | 69 | )) |
| 68 | } | 70 | } |
| 69 | 71 | ||
| 72 | + pub fn from_memory_buffer<R: Renderer<TextureId = T> + ImportMem>( | ||
| 73 | + renderer: &mut R, | ||
| 74 | + buffer: &MemoryBuffer, | ||
| 75 | + ) -> Result<Self, <R as Renderer>::Error> { | ||
| 76 | + Self::from_memory( | ||
| 77 | + renderer, | ||
| 78 | + buffer.data(), | ||
| 79 | + buffer.format(), | ||
| 80 | + buffer.size(), | ||
| 81 | + false, | ||
| 82 | + buffer.scale(), | ||
| 83 | + buffer.transform(), | ||
| 84 | + Vec::new(), | ||
| 85 | + ) | ||
| 86 | + } | ||
| 87 | + | ||
| 70 | pub fn texture(&self) -> &T { | 88 | pub fn texture(&self) -> &T { |
| 71 | &self.texture | 89 | &self.texture |
| 72 | } | 90 | } |