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:
Ivan Molodetskikh 2020-02-03 08:40:25 +03:00
parent 46348367b1
commit 825995687a
No known key found for this signature in database
GPG Key ID: 02CE38DA47E9D691
2 changed files with 60 additions and 0 deletions

59
anvil/src/buffer_utils.rs Normal file
View File

@ -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()
}
}

View File

@ -12,6 +12,7 @@ use smithay::reexports::{calloop::EventLoop, wayland_server::Display};
#[macro_use] #[macro_use]
mod shaders; mod shaders;
mod buffer_utils;
mod glium_drawer; mod glium_drawer;
mod input_handler; mod input_handler;
mod shell; mod shell;