Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,19 @@ x11 = [
"winit/x11",
]
x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]
f16 = ["half"]

[dependencies]
raw_window_handle = { package = "raw-window-handle", version = "0.6", features = [
"std",
] }
tracing = { version = "0.1.41", default-features = false }

# For f16 support in pixel conversions.
# TODO: Use standard library's f16 once stable:
# https://github.com/rust-lang/rust/issues/116909
half = { version = "2.7.1", optional = true }

[target.'cfg(target_os = "android")'.dependencies]
bytemuck = "1.12.3"
ndk = "0.9.0"
Expand Down
20 changes: 17 additions & 3 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Interface implemented by backends

use crate::{InitError, Rect, SoftBufferError};
use crate::{AlphaMode, InitError, PixelFormat, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::num::NonZeroU32;
Expand All @@ -22,12 +22,26 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
where
W: Sized,
Self: Sized;

/// 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>;

fn alpha_mode(&self) -> AlphaMode;

/// Reconfigure the internal buffer(s).
///
/// Returns `Some(byte_stride)` if the buffer was successfully resized, or `None` if the surface does not support the given pixel format.
fn configure(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
alpha_mode: AlphaMode,
pixel_format: PixelFormat,
) -> Result<Option<u32>, 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)
Expand Down
2 changes: 1 addition & 1 deletion src/backends/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<D: HasDisplayHandle, W: HasWindowHandle> SurfaceInterface<D, W> for CGImpl<
&self.window_handle
}

fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
fn configure(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.width = width.get() as usize;
self.height = height.get() as usize;
Ok(())
Expand Down
22 changes: 10 additions & 12 deletions src/backends/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ pub(super) struct WaylandBuffer {
}

impl WaylandBuffer {
pub fn new(shm: &wl_shm::WlShm, width: i32, height: i32, qh: &QueueHandle<State>) -> Self {
pub fn new(
shm: &wl_shm::WlShm,
width: i32,
height: i32,
format: wl_shm::Format,
qh: &QueueHandle<State>,
) -> Self {
// Calculate size to use for shm pool
let pool_size = get_pool_size(width, height);

Expand All @@ -94,15 +100,7 @@ impl WaylandBuffer {
// Create wayland shm pool and buffer
let pool = shm.create_pool(tempfile.as_fd(), pool_size, qh, ());
let released = Arc::new(AtomicBool::new(true));
let buffer = pool.create_buffer(
0,
width,
height,
width * 4,
wl_shm::Format::Xrgb8888,
qh,
released.clone(),
);
let buffer = pool.create_buffer(0, width, height, width * 4, format, qh, released.clone());

Self {
qh: qh.clone(),
Expand All @@ -118,7 +116,7 @@ impl WaylandBuffer {
}
}

pub fn resize(&mut self, width: i32, height: i32) {
pub fn configure(&mut self, width: i32, height: i32, format: wl_shm::Format) {
// If size is the same, there's nothing to do
if self.width != width || self.height != height {
// Destroy old buffer
Expand All @@ -139,7 +137,7 @@ impl WaylandBuffer {
width,
height,
width * 4,
wl_shm::Format::Xrgb8888,
format,
&self.qh,
self.released.clone(),
);
Expand Down
22 changes: 18 additions & 4 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
backend_interface::*,
error::{InitError, SwResultExt},
Rect, SoftBufferError,
AlphaMode, Rect, SoftBufferError,
};
use raw_window_handle::{HasDisplayHandle, HasWindowHandle, RawDisplayHandle, RawWindowHandle};
use std::{
Expand Down Expand Up @@ -81,6 +81,7 @@ pub struct WaylandImpl<D: ?Sized, W: ?Sized> {
surface: Option<wl_surface::WlSurface>,
buffers: Option<(WaylandBuffer, WaylandBuffer)>,
size: Option<(NonZeroI32, NonZeroI32)>,
format: wl_shm::Format,

/// The pointer to the window object.
///
Expand Down Expand Up @@ -128,7 +129,12 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
&self.window_handle
}

fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
fn configure(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
alpha_mode: AlphaMode,
) -> Result<AlphaMode, SoftBufferError> {
self.size = Some(
(|| {
let width = NonZeroI32::try_from(width).ok()?;
Expand All @@ -137,7 +143,13 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?,
);
Ok(())
let (format, chosen_alpha_mode) = match alpha_mode {
AlphaMode::Opaque => (wl_shm::Format::Xrgb8888, AlphaMode::Opaque),
AlphaMode::PreMultiplied => (wl_shm::Format::Argb8888, AlphaMode::PreMultiplied),
AlphaMode::PostMultiplied => (wl_shm::Format::Argb8888, AlphaMode::PostMultiplied),
};
self.format = format;
Ok(chosen_alpha_mode)
}

fn buffer_mut(&mut self) -> Result<BufferImpl<'_>, SoftBufferError> {
Expand All @@ -164,20 +176,22 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
}

// Resize, if buffer isn't large enough
back.resize(width.get(), height.get());
back.configure(width.get(), height.get(), self.format);
} else {
// Allocate front and back buffer
self.buffers = Some((
WaylandBuffer::new(
&self.display.shm,
width.get(),
height.get(),
self.format,
&self.display.qh,
),
WaylandBuffer::new(
&self.display.shm,
width.get(),
height.get(),
self.format,
&self.display.qh,
),
));
Expand Down
5 changes: 5 additions & 0 deletions src/backends/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use web_sys::{CanvasRenderingContext2d, HtmlCanvasElement};
use web_sys::{OffscreenCanvas, OffscreenCanvasRenderingContext2d};

use crate::backend_interface::*;
use crate::convert::FALLBACK_FORMAT;
use crate::error::{InitError, SwResultExt};
use crate::{util, NoDisplayHandle, NoWindowHandle, Rect, SoftBufferError};
use std::marker::PhantomData;
Expand Down Expand Up @@ -231,6 +232,8 @@ impl SurfaceExtWeb for crate::Surface<NoDisplayHandle, NoWindowHandle> {
let imple = crate::SurfaceDispatch::Web(WebImpl::from_canvas(canvas, NoWindowHandle(()))?);

Ok(Self {
pixel_format: FALLBACK_FORMAT,
fallback_buffer: None,
surface_impl: Box::new(imple),
_marker: PhantomData,
})
Expand All @@ -243,6 +246,8 @@ impl SurfaceExtWeb for crate::Surface<NoDisplayHandle, NoWindowHandle> {
)?);

Ok(Self {
pixel_format: FALLBACK_FORMAT,
fallback_buffer: None,
surface_impl: Box::new(imple),
_marker: PhantomData,
})
Expand Down
Loading
Loading