Add MemoryBuffer

This commit is contained in:
Ivan Molodetskikh 2024-06-09 10:24:09 +03:00
parent be62bd123a
commit 7019172b67
3 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,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)
}
}

View File

@ -21,6 +21,7 @@ pub mod border;
pub mod clipped_surface;
pub mod damage;
pub mod debug;
pub mod memory;
pub mod offscreen;
pub mod primary_gpu_texture;
pub mod render_elements;

View File

@ -4,6 +4,8 @@ use smithay::backend::renderer::utils::{CommitCounter, OpaqueRegions};
use smithay::backend::renderer::{Frame as _, ImportMem, Renderer, Texture};
use smithay::utils::{Buffer, Logical, Physical, Point, Rectangle, Scale, Size, Transform};
use super::memory::MemoryBuffer;
/// Smithay's texture buffer, but with fractional scale.
#[derive(Debug, Clone)]
pub struct TextureBuffer<T> {
@ -67,6 +69,22 @@ impl<T> TextureBuffer<T> {
))
}
pub fn from_memory_buffer<R: Renderer<TextureId = T> + ImportMem>(
renderer: &mut R,
buffer: &MemoryBuffer,
) -> Result<Self, <R as Renderer>::Error> {
Self::from_memory(
renderer,
buffer.data(),
buffer.format(),
buffer.size(),
false,
buffer.scale(),
buffer.transform(),
Vec::new(),
)
}
pub fn texture(&self) -> &T {
&self.texture
}