buffer: use Size instead of width/height

This commit is contained in:
Victor Brekenfeld 2021-07-13 21:11:30 +02:00
parent 7dadd63e35
commit 1cf1d4739b
9 changed files with 55 additions and 67 deletions

View File

@ -338,6 +338,10 @@ pub fn import_bitmap<C: std::ops::Deref<Target = [u8]>>(
); );
gl.BindTexture(ffi::TEXTURE_2D, 0); gl.BindTexture(ffi::TEXTURE_2D, 0);
Gles2Texture::from_raw(renderer, tex, image.width(), image.height()) Gles2Texture::from_raw(
renderer,
tex,
(image.width() as i32, image.height() as i32).into(),
)
}) })
} }

View File

@ -11,6 +11,7 @@
//! the lifetime of the buffer. E.g. when you are only caching associated resources for a dmabuf. //! the lifetime of the buffer. E.g. when you are only caching associated resources for a dmabuf.
use super::{Buffer, Format, Fourcc, Modifier}; use super::{Buffer, Format, Fourcc, Modifier};
use crate::utils::{Buffer as BufferCoords, Size};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::os::unix::io::{IntoRawFd, RawFd}; use std::os::unix::io::{IntoRawFd, RawFd};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
@ -22,10 +23,8 @@ pub const MAX_PLANES: usize = 4;
pub(crate) struct DmabufInternal { pub(crate) struct DmabufInternal {
/// The submitted planes /// The submitted planes
pub planes: Vec<Plane>, pub planes: Vec<Plane>,
/// The width of this buffer /// The size of this buffer
pub width: i32, pub size: Size<i32, BufferCoords>,
/// The height of this buffer
pub height: i32,
/// The format in use /// The format in use
pub format: Fourcc, pub format: Fourcc,
/// The flags applied to it /// The flags applied to it
@ -107,12 +106,8 @@ impl Hash for WeakDmabuf {
} }
impl Buffer for Dmabuf { impl Buffer for Dmabuf {
fn width(&self) -> u32 { fn size(&self) -> Size<i32, BufferCoords> {
self.0.width as u32 self.0.size
}
fn height(&self) -> u32 {
self.0.height as u32
} }
fn format(&self) -> Format { fn format(&self) -> Format {
@ -172,8 +167,7 @@ impl Dmabuf {
DmabufBuilder { DmabufBuilder {
internal: DmabufInternal { internal: DmabufInternal {
planes: Vec::with_capacity(MAX_PLANES), planes: Vec::with_capacity(MAX_PLANES),
width: src.width() as i32, size: src.size(),
height: src.height() as i32,
format: src.format().code, format: src.format().code,
flags, flags,
}, },
@ -181,12 +175,15 @@ impl Dmabuf {
} }
/// Create a new Dmabuf builder /// Create a new Dmabuf builder
pub fn builder(width: u32, height: u32, format: Fourcc, flags: DmabufFlags) -> DmabufBuilder { pub fn builder(
size: impl Into<Size<i32, BufferCoords>>,
format: Fourcc,
flags: DmabufFlags,
) -> DmabufBuilder {
DmabufBuilder { DmabufBuilder {
internal: DmabufInternal { internal: DmabufInternal {
planes: Vec::with_capacity(MAX_PLANES), planes: Vec::with_capacity(MAX_PLANES),
width: width as i32, size: size.into(),
height: height as i32,
format, format,
flags, flags,
}, },

View File

@ -9,6 +9,7 @@ use drm::control::{dumbbuffer::DumbBuffer as Handle, Device as ControlDevice};
use super::{Allocator, Buffer, Format, Fourcc, Modifier}; use super::{Allocator, Buffer, Format, Fourcc, Modifier};
use crate::backend::drm::device::{DrmDevice, DrmDeviceInternal, FdWrapper}; use crate::backend::drm::device::{DrmDevice, DrmDeviceInternal, FdWrapper};
use crate::utils::{Buffer as BufferCoords, Size};
/// Wrapper around raw DumbBuffer handles. /// Wrapper around raw DumbBuffer handles.
pub struct DumbBuffer<A: AsRawFd + 'static> { pub struct DumbBuffer<A: AsRawFd + 'static> {
@ -61,12 +62,9 @@ impl<A: AsRawFd + 'static> Allocator<DumbBuffer<A>> for DrmDevice<A> {
} }
impl<A: AsRawFd + 'static> Buffer for DumbBuffer<A> { impl<A: AsRawFd + 'static> Buffer for DumbBuffer<A> {
fn width(&self) -> u32 { fn size(&self) -> Size<i32, BufferCoords> {
self.handle.size().0 let (w, h) = self.handle.size();
} (w as i32, h as i32).into()
fn height(&self) -> u32 {
self.handle.size().1
} }
fn format(&self) -> Format { fn format(&self) -> Format {

View File

@ -8,6 +8,7 @@ use super::{
dmabuf::{AsDmabuf, Dmabuf, DmabufFlags, MAX_PLANES}, dmabuf::{AsDmabuf, Dmabuf, DmabufFlags, MAX_PLANES},
Allocator, Buffer, Format, Fourcc, Modifier, Allocator, Buffer, Format, Fourcc, Modifier,
}; };
use crate::utils::{Buffer as BufferCoords, Size};
pub use gbm::{BufferObject as GbmBuffer, BufferObjectFlags as GbmBufferFlags, Device as GbmDevice}; pub use gbm::{BufferObject as GbmBuffer, BufferObjectFlags as GbmBufferFlags, Device as GbmDevice};
use std::os::unix::io::AsRawFd; use std::os::unix::io::AsRawFd;
@ -39,12 +40,12 @@ impl<A: AsRawFd + 'static, T> Allocator<GbmBuffer<T>> for GbmDevice<A> {
} }
impl<T> Buffer for GbmBuffer<T> { impl<T> Buffer for GbmBuffer<T> {
fn width(&self) -> u32 { fn size(&self) -> Size<i32, BufferCoords> {
self.width().unwrap_or(0) (
} self.width().unwrap_or(0) as i32,
self.height().unwrap_or(0) as i32,
fn height(&self) -> u32 { )
self.height().unwrap_or(0) .into()
} }
fn format(&self) -> Format { fn format(&self) -> Format {

View File

@ -22,6 +22,7 @@ pub mod dumb;
pub mod gbm; pub mod gbm;
mod swapchain; mod swapchain;
use crate::utils::{Buffer as BufferCoords, Size};
pub use swapchain::{Slot, Swapchain}; pub use swapchain::{Slot, Swapchain};
pub use drm_fourcc::{ pub use drm_fourcc::{
@ -32,13 +33,15 @@ pub use drm_fourcc::{
/// Common trait describing common properties of most types of buffers. /// Common trait describing common properties of most types of buffers.
pub trait Buffer { pub trait Buffer {
/// Width of the two-dimensional buffer /// Width of the two-dimensional buffer
fn width(&self) -> u32; fn width(&self) -> u32 {
/// Height of the two-dimensional buffer self.size().w as u32
fn height(&self) -> u32;
/// Size (w x h) of the two-dimensional buffer
fn size(&self) -> (u32, u32) {
(self.width(), self.height())
} }
/// Height of the two-dimensional buffer
fn height(&self) -> u32 {
self.size().h as u32
}
/// Size of the two-dimensional buffer
fn size(&self) -> Size<i32, BufferCoords>;
/// Pixel format of the buffer /// Pixel format of the buffer
fn format(&self) -> Format; fn format(&self) -> Format;
} }

View File

@ -808,8 +808,7 @@ impl EGLBufferReader {
Ok(EGLBuffer { Ok(EGLBuffer {
display: self.display.clone(), display: self.display.clone(),
width: width as u32, size: (width, height).into(),
height: height as u32,
// y_inverted is negated here because the gles2 renderer // y_inverted is negated here because the gles2 renderer
// already inverts the buffer during rendering. // already inverts the buffer during rendering.
y_inverted: !y_inverted, y_inverted: !y_inverted,

View File

@ -32,6 +32,8 @@ pub use self::context::EGLContext;
mod error; mod error;
pub use self::error::*; pub use self::error::*;
use crate::backend::SwapBuffersError as GraphicsSwapBuffersError; use crate::backend::SwapBuffersError as GraphicsSwapBuffersError;
#[cfg(feature = "wayland_frontend")]
use crate::utils::{Buffer, Size};
use nix::libc::c_void; use nix::libc::c_void;
@ -248,10 +250,8 @@ impl Format {
#[derive(Debug)] #[derive(Debug)]
pub struct EGLBuffer { pub struct EGLBuffer {
display: Arc<EGLDisplayHandle>, display: Arc<EGLDisplayHandle>,
/// Width in pixels /// Size of the buffer
pub width: u32, pub size: Size<i32, Buffer>,
/// Height in pixels
pub height: u32,
/// If the y-axis is inverted or not /// If the y-axis is inverted or not
pub y_inverted: bool, pub y_inverted: bool,
/// Format of these images /// Format of these images

View File

@ -34,7 +34,6 @@ use super::ImportEgl;
use super::{ImportDma, ImportShm}; use super::{ImportDma, ImportShm};
#[cfg(all(feature = "wayland_frontend", feature = "use_system_lib"))] #[cfg(all(feature = "wayland_frontend", feature = "use_system_lib"))]
use crate::backend::egl::{display::EGLBufferReader, Format as EGLFormat}; use crate::backend::egl::{display::EGLBufferReader, Format as EGLFormat};
#[cfg(feature = "wayland_frontend")]
use crate::utils::{Buffer, Rectangle}; use crate::utils::{Buffer, Rectangle};
#[cfg(feature = "wayland_frontend")] #[cfg(feature = "wayland_frontend")]
use wayland_server::protocol::{wl_buffer, wl_shm}; use wayland_server::protocol::{wl_buffer, wl_shm};
@ -82,16 +81,14 @@ impl Gles2Texture {
pub unsafe fn from_raw( pub unsafe fn from_raw(
renderer: &Gles2Renderer, renderer: &Gles2Renderer,
tex: ffi::types::GLuint, tex: ffi::types::GLuint,
width: u32, size: Size<i32, Buffer>,
height: u32,
) -> Gles2Texture { ) -> Gles2Texture {
Gles2Texture(Rc::new(Gles2TextureInternal { Gles2Texture(Rc::new(Gles2TextureInternal {
texture: tex, texture: tex,
texture_kind: 0, texture_kind: 0,
is_external: false, is_external: false,
y_inverted: false, y_inverted: false,
width, size,
height,
egl_images: None, egl_images: None,
destruction_callback_sender: renderer.destruction_callback_sender.clone(), destruction_callback_sender: renderer.destruction_callback_sender.clone(),
})) }))
@ -104,8 +101,7 @@ struct Gles2TextureInternal {
texture_kind: usize, texture_kind: usize,
is_external: bool, is_external: bool,
y_inverted: bool, y_inverted: bool,
width: u32, size: Size<i32, Buffer>,
height: u32,
egl_images: Option<Vec<EGLImage>>, egl_images: Option<Vec<EGLImage>>,
destruction_callback_sender: Sender<CleanupResource>, destruction_callback_sender: Sender<CleanupResource>,
} }
@ -132,10 +128,13 @@ enum CleanupResource {
impl Texture for Gles2Texture { impl Texture for Gles2Texture {
fn width(&self) -> u32 { fn width(&self) -> u32 {
self.0.width self.0.size.w as u32
} }
fn height(&self) -> u32 { fn height(&self) -> u32 {
self.0.height self.0.size.h as u32
}
fn size(&self) -> Size<i32, Buffer> {
self.0.size
} }
} }
@ -590,8 +589,7 @@ impl ImportShm for Gles2Renderer {
texture_kind: shader_idx, texture_kind: shader_idx,
is_external: false, is_external: false,
y_inverted: false, y_inverted: false,
width: width as u32, size: (width, height).into(),
height: height as u32,
egl_images: None, egl_images: None,
destruction_callback_sender: self.destruction_callback_sender.clone(), destruction_callback_sender: self.destruction_callback_sender.clone(),
}) })
@ -720,8 +718,7 @@ impl ImportEgl for Gles2Renderer {
}, },
is_external: egl.format == EGLFormat::External, is_external: egl.format == EGLFormat::External,
y_inverted: egl.y_inverted, y_inverted: egl.y_inverted,
width: egl.width, size: egl.size,
height: egl.height,
egl_images: Some(egl.into_images()), egl_images: Some(egl.into_images()),
destruction_callback_sender: self.destruction_callback_sender.clone(), destruction_callback_sender: self.destruction_callback_sender.clone(),
})); }));
@ -754,8 +751,7 @@ impl ImportDma for Gles2Renderer {
texture_kind: if is_external { 2 } else { 0 }, texture_kind: if is_external { 2 } else { 0 },
is_external, is_external,
y_inverted: buffer.y_inverted(), y_inverted: buffer.y_inverted(),
width: buffer.width(), size: buffer.size(),
height: buffer.height(),
egl_images: Some(vec![image]), egl_images: Some(vec![image]),
destruction_callback_sender: self.destruction_callback_sender.clone(), destruction_callback_sender: self.destruction_callback_sender.clone(),
})); }));

View File

@ -260,12 +260,7 @@ where
return; return;
} }
let mut buf = Dmabuf::builder( let mut buf = Dmabuf::builder((width, height), format, DmabufFlags::from_bits_truncate(flags));
width as u32,
height as u32,
format,
DmabufFlags::from_bits_truncate(flags),
);
let planes = std::mem::take(&mut self.pending_planes); let planes = std::mem::take(&mut self.pending_planes);
for (i, plane) in planes.into_iter().enumerate() { for (i, plane) in planes.into_iter().enumerate() {
let offset = plane.offset; let offset = plane.offset;
@ -350,12 +345,7 @@ where
return; return;
} }
let mut buf = Dmabuf::builder( let mut buf = Dmabuf::builder((width, height), format, DmabufFlags::from_bits_truncate(flags));
width as u32,
height as u32,
format,
DmabufFlags::from_bits_truncate(flags),
);
let planes = ::std::mem::take(&mut self.pending_planes); let planes = ::std::mem::take(&mut self.pending_planes);
for (i, plane) in planes.into_iter().enumerate() { for (i, plane) in planes.into_iter().enumerate() {
let offset = plane.offset; let offset = plane.offset;