anvil: add BufferUtils
A helper struct which currently contains only a method for retrieving buffer dimensions. Will be used for getting the buffer dimensions early, before rendering, to always have up-to-date size which is not zero.
This commit is contained in:
parent
46348367b1
commit
825995687a
|
@ -0,0 +1,59 @@
|
|||
use std::{cell::RefCell, rc::Rc};
|
||||
|
||||
use slog::Logger;
|
||||
|
||||
#[cfg(feature = "egl")]
|
||||
use smithay::backend::egl::{BufferAccessError, EGLDisplay};
|
||||
use smithay::{
|
||||
reexports::wayland_server::protocol::wl_buffer::WlBuffer,
|
||||
wayland::shm::with_buffer_contents as shm_buffer_contents,
|
||||
};
|
||||
|
||||
/// Utilities for working with `WlBuffer`s.
|
||||
#[derive(Clone)]
|
||||
pub struct BufferUtils {
|
||||
#[cfg(feature = "egl")]
|
||||
egl_display: Rc<RefCell<Option<EGLDisplay>>>,
|
||||
log: Logger,
|
||||
}
|
||||
|
||||
impl BufferUtils {
|
||||
/// Creates a new `BufferUtils`.
|
||||
#[cfg(feature = "egl")]
|
||||
pub fn new(egl_display: Rc<RefCell<Option<EGLDisplay>>>, log: Logger) -> Self {
|
||||
Self { egl_display, log }
|
||||
}
|
||||
|
||||
/// Creates a new `BufferUtils`.
|
||||
#[cfg(not(feature = "egl"))]
|
||||
pub fn new(log: Logger) -> Self {
|
||||
Self { log }
|
||||
}
|
||||
|
||||
/// Returns the dimensions of an image stored in the buffer.
|
||||
#[cfg(feature = "egl")]
|
||||
pub fn dimensions(&self, buffer: &WlBuffer) -> Option<(i32, i32)> {
|
||||
// Try to retrieve the EGL dimensions of this buffer, and, if that fails, the shm dimensions.
|
||||
self.egl_display
|
||||
.borrow()
|
||||
.as_ref()
|
||||
.and_then(|display| display.egl_buffer_dimensions(buffer))
|
||||
.or_else(|| self.shm_buffer_dimensions(buffer))
|
||||
}
|
||||
|
||||
/// Returns the dimensions of an image stored in the buffer.
|
||||
#[cfg(not(feature = "egl"))]
|
||||
pub fn dimensions(&self, buffer: &WlBuffer) -> Option<(i32, i32)> {
|
||||
self.shm_buffer_dimensions(buffer)
|
||||
}
|
||||
|
||||
/// Returns the dimensions of an image stored in the shm buffer.
|
||||
fn shm_buffer_dimensions(&self, buffer: &WlBuffer) -> Option<(i32, i32)> {
|
||||
shm_buffer_contents(buffer, |_, data| (data.width, data.height))
|
||||
.map_err(|err| {
|
||||
warn!(self.log, "Unable to load buffer contents"; "err" => format!("{:?}", err));
|
||||
err
|
||||
})
|
||||
.ok()
|
||||
}
|
||||
}
|
|
@ -12,6 +12,7 @@ use smithay::reexports::{calloop::EventLoop, wayland_server::Display};
|
|||
|
||||
#[macro_use]
|
||||
mod shaders;
|
||||
mod buffer_utils;
|
||||
mod glium_drawer;
|
||||
mod input_handler;
|
||||
mod shell;
|
||||
|
|
Loading…
Reference in New Issue