Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use traits to define common backend interface #196

Merged
merged 5 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//! Implements `buffer_interface::*` traits for enums dispatching to backends

use crate::{backend_interface::*, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroU32;
#[cfg(any(wayland_platform, x11_platform, kms_platform))]
use std::rc::Rc;

/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
macro_rules! make_dispatch {
(
<$dgen: ident, $wgen: ident> =>
$(
$(#[$attr:meta])*
$name: ident
($context_inner: ty, $surface_inner: ty, $buffer_inner: ty),
)*
) => {
pub(crate) enum ContextDispatch<$dgen> {
$(
$(#[$attr])*
$name($context_inner),
)*
}

impl<D: HasDisplayHandle> ContextDispatch<D> {
pub fn variant_name(&self) -> &'static str {
match self {
$(
$(#[$attr])*
Self::$name(_) => stringify!($name),
)*
}
}
}

#[allow(clippy::large_enum_variant)] // it's boxed anyways
pub(crate) enum SurfaceDispatch<$dgen, $wgen> {
$(
$(#[$attr])*
$name($surface_inner),
)*
}

impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<W> for SurfaceDispatch<D, W> {
type Buffer<'a> = BufferDispatch<'a, D, W> where Self: 'a;

fn window(&self) -> &W {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.window(),
)*
}
}

fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.resize(width, height),
)*
}
}

fn buffer_mut(&mut self) -> Result<BufferDispatch<'_, D, W>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => Ok(BufferDispatch::$name(inner.buffer_mut()?)),
)*
}
}

fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.fetch(),
)*
}
}
}

pub(crate) enum BufferDispatch<'a, $dgen, $wgen> {
$(
$(#[$attr])*
$name($buffer_inner),
)*
}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
#[inline]
fn pixels(&self) -> &[u32] {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.pixels(),
)*
}
}

#[inline]
fn pixels_mut(&mut self) -> &mut [u32] {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.pixels_mut(),
)*
}
}

fn age(&self) -> u8 {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.age(),
)*
}
}

fn present(self) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.present(),
)*
}
}

fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.present_with_damage(damage),
)*
}
}
}
};
}

// XXX empty enum with generic bound is invalid?

make_dispatch! {
<D, W> =>
#[cfg(x11_platform)]
X11(Rc<crate::x11::X11DisplayImpl<D>>, crate::x11::X11Impl<D, W>, crate::x11::BufferImpl<'a, D, W>),
#[cfg(wayland_platform)]
Wayland(Rc<crate::wayland::WaylandDisplayImpl<D>>, crate::wayland::WaylandImpl<D, W>, crate::wayland::BufferImpl<'a, D, W>),
#[cfg(kms_platform)]
Kms(Rc<crate::kms::KmsDisplayImpl<D>>, crate::kms::KmsImpl<D, W>, crate::kms::BufferImpl<'a, D, W>),
#[cfg(target_os = "windows")]
Win32(D, crate::win32::Win32Impl<D, W>, crate::win32::BufferImpl<'a, D, W>),
#[cfg(target_os = "macos")]
CG(D, crate::cg::CGImpl<D, W>, crate::cg::BufferImpl<'a, D, W>),
#[cfg(target_arch = "wasm32")]
Web(crate::web::WebDisplayImpl<D>, crate::web::WebImpl<D, W>, crate::web::BufferImpl<'a, D, W>),
#[cfg(target_os = "redox")]
Orbital(D, crate::orbital::OrbitalImpl<D, W>, crate::orbital::BufferImpl<'a, D, W>),
}
31 changes: 31 additions & 0 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Interface implemented by backends

use crate::{Rect, SoftBufferError};

use raw_window_handle::HasWindowHandle;
use std::num::NonZeroU32;

pub(crate) trait SurfaceInterface<W: HasWindowHandle + ?Sized> {
type Buffer<'a>: BufferInterface
where
Self: 'a;

/// Get the inner window handle.
fn window(&self) -> &W;
/// Resize the internal buffer to the given width and height.
fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError>;
/// Get a mutable reference to the buffer.
fn buffer_mut(&mut self) -> Result<Self::Buffer<'_>, SoftBufferError>;
/// Fetch the buffer from the window.
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
Err(SoftBufferError::Unimplemented)
}
}

pub(crate) trait BufferInterface {
fn pixels(&self) -> &[u32];
fn pixels_mut(&mut self) -> &mut [u32];
fn age(&self) -> u8;
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError>;
fn present(self) -> Result<(), SoftBufferError>;
}
29 changes: 14 additions & 15 deletions src/cg.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::backend_interface::*;
use crate::error::InitError;
use crate::{Rect, SoftBufferError};
use core_graphics::base::{
Expand Down Expand Up @@ -65,19 +66,22 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> CGImpl<D, W> {
window_handle: window_src,
})
}
}

impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<W> for CGImpl<D, W> {
type Buffer<'a> = BufferImpl<'a, D, W> where Self: 'a;

/// Get the inner window handle.
#[inline]
pub fn window(&self) -> &W {
fn window(&self) -> &W {
&self.window_handle
}

pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.size = Some((width, height));
Ok(())
}

pub fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
let (width, height) = self
.size
.expect("Must set size of surface before calling `buffer_mut()`");
Expand All @@ -87,34 +91,29 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> CGImpl<D, W> {
imp: self,
})
}

/// Fetch the buffer from the window.
pub fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
Err(SoftBufferError::Unimplemented)
}
}

pub struct BufferImpl<'a, D, W> {
imp: &'a mut CGImpl<D, W>,
buffer: Vec<u32>,
}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferImpl<'a, D, W> {
impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> {
#[inline]
pub fn pixels(&self) -> &[u32] {
fn pixels(&self) -> &[u32] {
&self.buffer
}

#[inline]
pub fn pixels_mut(&mut self) -> &mut [u32] {
fn pixels_mut(&mut self) -> &mut [u32] {
&mut self.buffer
}

pub fn age(&self) -> u8 {
fn age(&self) -> u8 {
0
}

pub fn present(self) -> Result<(), SoftBufferError> {
fn present(self) -> Result<(), SoftBufferError> {
let data_provider = CGDataProvider::from_buffer(Arc::new(Buffer(self.buffer)));
let (width, height) = self.imp.size.unwrap();
let image = CGImage::new(
Expand Down Expand Up @@ -148,7 +147,7 @@ impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferImpl<'a, D, W> {
Ok(())
}

pub fn present_with_damage(self, _damage: &[Rect]) -> Result<(), SoftBufferError> {
fn present_with_damage(self, _damage: &[Rect]) -> Result<(), SoftBufferError> {
self.present()
}
}
Expand Down
36 changes: 17 additions & 19 deletions src/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, BorrowedFd};
use std::rc::Rc;

use crate::backend_interface::*;
use crate::error::{InitError, SoftBufferError, SwResultExt};

#[derive(Debug)]
Expand Down Expand Up @@ -203,19 +204,17 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
window_handle: window,
})
}
}

impl<D: ?Sized, W: HasWindowHandle> SurfaceInterface<W> for KmsImpl<D, W> {
type Buffer<'a> = BufferImpl<'a, D, W> where Self: 'a;

/// Get the inner window handle.
#[inline]
pub fn window(&self) -> &W {
fn window(&self) -> &W {
&self.window_handle
}

/// Resize the internal buffer to the given size.
pub(crate) fn resize(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
) -> Result<(), SoftBufferError> {
fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
// Don't resize if we don't have to.
if let Some(buffer) = &self.buffer {
let (buffer_width, buffer_height) = buffer.size();
Expand All @@ -237,14 +236,13 @@ impl<D: ?Sized, W: HasWindowHandle> KmsImpl<D, W> {
Ok(())
}

/// Fetch the buffer from the window.
pub(crate) fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
/*
fn fetch(&mut self) -> Result<Vec<u32>, SoftBufferError> {
// TODO: Implement this!
Err(SoftBufferError::Unimplemented)
}
*/

/// Get a mutable reference to the buffer.
pub(crate) fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
fn buffer_mut(&mut self) -> Result<BufferImpl<'_, D, W>, SoftBufferError> {
// Map the dumb buffer.
let set = self
.buffer
Expand Down Expand Up @@ -299,26 +297,26 @@ impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
}
}

impl<D: ?Sized, W: ?Sized> BufferImpl<'_, D, W> {
impl<D: ?Sized, W: ?Sized> BufferInterface for BufferImpl<'_, D, W> {
#[inline]
pub fn pixels(&self) -> &[u32] {
fn pixels(&self) -> &[u32] {
// drm-rs doesn't let us have the immutable reference... so just use a bunch of zeroes.
// TODO: There has to be a better way of doing this!
self.zeroes
}

#[inline]
pub fn pixels_mut(&mut self) -> &mut [u32] {
fn pixels_mut(&mut self) -> &mut [u32] {
bytemuck::cast_slice_mut(self.mapping.as_mut())
}

#[inline]
pub fn age(&self) -> u8 {
fn age(&self) -> u8 {
*self.front_age
}

#[inline]
pub fn present_with_damage(self, damage: &[crate::Rect]) -> Result<(), SoftBufferError> {
fn present_with_damage(self, damage: &[crate::Rect]) -> Result<(), SoftBufferError> {
let rectangles = damage
.iter()
.map(|&rect| {
Expand Down Expand Up @@ -374,7 +372,7 @@ impl<D: ?Sized, W: ?Sized> BufferImpl<'_, D, W> {
}

#[inline]
pub fn present(self) -> Result<(), SoftBufferError> {
fn present(self) -> Result<(), SoftBufferError> {
let (width, height) = self.size;
self.present_with_damage(&[crate::Rect {
x: 0,
Expand Down
Loading
Loading