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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

- **Breaking:** Use `u32` instead of `NonZeroU32`, and handle zero-sized buffers internally.

# 0.4.7

- Fix documentation building on `docs.rs`.
Expand Down
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ wayland = [
"memmap2",
"rustix",
"fastrand",
"winit/wayland",
]
wayland-dlopen = ["wayland-sys/dlopen"]
wayland-dlopen = ["wayland-sys/dlopen", "winit/wayland-dlopen"]
x11 = [
"as-raw-xcb-connection",
"bytemuck",
"fastrand",
"rustix",
"tiny-xlib",
"x11rb",
"winit/x11",
]
x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]

Expand Down Expand Up @@ -133,7 +135,10 @@ redox_syscall = "0.5"
[dev-dependencies]
colorous = "1.0.12"
web-time = "1.0.0"
winit = "0.30.0"
winit = { version = "0.30.0", default-features = false, features = [
"rwh_06",
"wayland-csd-adwaita",
] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

[target.'cfg(target_os = "android")'.dev-dependencies]
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ To run the Android-specific example on an Android phone: `cargo apk r --example
## Example

```rust,no_run
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand Down Expand Up @@ -102,12 +101,7 @@ fn main() {
return;
};
let size = window.inner_size();
surface
.resize(
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();
surface.resize(size.width, size.height).unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(buffer.width().get() * buffer.height().get()) {
Expand Down
8 changes: 1 addition & 7 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
fn buffer_mut(c: &mut criterion::Criterion) {
use criterion::black_box;
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use winit::event_loop::ControlFlow;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;

Expand All @@ -27,12 +26,7 @@ fn buffer_mut(c: &mut criterion::Criterion) {
let mut surface = Surface::new(&context, &window).unwrap();

let size = window.inner_size();
surface
.resize(
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();
surface.resize(size.width, size.height).unwrap();

c.bench_function("buffer_mut()", |b| {
b.iter(|| {
Expand Down
9 changes: 2 additions & 7 deletions examples/animation.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(not(target_family = "wasm"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use web_time::Instant;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand Down Expand Up @@ -46,11 +45,7 @@ fn main() {
return;
};

if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
surface.resize(width, height).unwrap();
}
surface.resize(size.width, size.height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
Expand All @@ -62,7 +57,7 @@ fn main() {

let mut buffer = surface.buffer_mut().unwrap();

let size = (buffer.width().get(), buffer.height().get());
let size = (buffer.width(), buffer.height());
if size != *old_size {
*old_size = size;
*frames = pre_render_frames(size.0, size.1);
Expand Down
6 changes: 1 addition & 5 deletions examples/drm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod imple {
use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
use softbuffer::{Context, Surface};

use std::num::NonZeroU32;
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
use std::path::Path;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -114,10 +113,7 @@ mod imple {

// Resize the surface.
let (width, height) = mode.size();
surface.resize(
NonZeroU32::new(width as u32).unwrap(),
NonZeroU32::new(height as u32).unwrap(),
)?;
surface.resize(width as u32, height as u32)?;

// Start drawing to it.
let start = Instant::now();
Expand Down
8 changes: 1 addition & 7 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
Expand Down Expand Up @@ -27,12 +26,7 @@ fn main() {
// Intentionally only set the size of the surface once, at creation.
// This is needed if the window chooses to ignore the size we passed in above, and for the
// platforms softbuffer supports that don't yet extract the size from the window.
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
surface.resize(width, height).unwrap();
surface
},
)
Expand Down
7 changes: 1 addition & 6 deletions examples/libxcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,7 @@ mod example {
match event {
Event::Expose(_) => {
// Draw a width x height red rectangle.
surface
.resize(
NonZeroU32::new(width.into()).unwrap(),
NonZeroU32::new(height.into()).unwrap(),
)
.unwrap();
surface.resize(width.into(), height.into()).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(RED);
buffer.present().unwrap();
Expand Down
13 changes: 4 additions & 9 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use softbuffer::Buffer;
use std::num::NonZeroU32;
use winit::event::{ElementState, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};

mod util;

fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>, flag: bool) {
let width = buffer.width().get();
let height = buffer.height().get();
let width = buffer.width();
let height = buffer.height();
for y in 0..height {
for x in 0..width {
let value = if flag && x >= 100 && x < width - 100 && y >= 100 && y < height - 100 {
Expand Down Expand Up @@ -59,12 +58,8 @@ fn main() {
return;
};

if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
// Resize surface
surface.resize(width, height).unwrap();
}
// Resize surface
surface.resize(size.width, size.height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
Expand Down
13 changes: 4 additions & 9 deletions examples/winit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::num::NonZeroU32;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
Expand Down Expand Up @@ -33,11 +32,7 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
return;
};

if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
surface.resize(width, height).unwrap();
}
surface.resize(size.width, size.height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
Expand All @@ -46,12 +41,12 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
};

let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y * buffer.width().get() + x;
let index = y * buffer.width() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
}
Expand Down
24 changes: 10 additions & 14 deletions examples/winit_multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ mod util;

#[cfg(not(target_family = "wasm"))]
pub mod ex {
use std::num::NonZeroU32;
use std::sync::{mpsc, Arc, Mutex};
use winit::dpi::PhysicalSize;
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, OwnedDisplayHandle};
use winit::keyboard::{Key, NamedKey};
Expand All @@ -16,28 +16,28 @@ pub mod ex {
type Surface = softbuffer::Surface<OwnedDisplayHandle, Arc<Window>>;

fn render_thread(
do_render: mpsc::Receiver<(Arc<Mutex<Surface>>, NonZeroU32, NonZeroU32)>,
do_render: mpsc::Receiver<(Arc<Mutex<Surface>>, PhysicalSize<u32>)>,
done: mpsc::Sender<()>,
) {
loop {
tracing::info!("waiting for render...");
let Ok((surface, width, height)) = do_render.recv() else {
let Ok((surface, size)) = do_render.recv() else {
tracing::info!("main thread destroyed");
break;
};

// Perform the rendering.
let mut surface = surface.lock().unwrap();
tracing::info!("resizing...");
surface.resize(width, height).unwrap();
surface.resize(size.width, size.height).unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y * buffer.width().get() + x;
let index = y * buffer.width() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
}
Expand Down Expand Up @@ -96,13 +96,9 @@ pub mod ex {

let size = window.inner_size();
tracing::info!("got size: {size:?}");
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
// Start the render and then finish it.
start_render.send((surface.clone(), width, height)).unwrap();
finish_render.recv().unwrap();
}
// Start the render and then finish it.
start_render.send((surface.clone(), size)).unwrap();
finish_render.recv().unwrap();
}
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
Expand Down
38 changes: 27 additions & 11 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::num::NonZeroU32;
//! A window with a surface that is 200 pixels less wide and 400 pixels less tall.
//!
//! This is useful for testing that zero-sized buffers work, as well as testing buffer vs. window
//! size discrepancies in general.
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};
Expand All @@ -14,11 +17,14 @@ fn main() {
let app = util::WinitAppBuilder::with_init(
|elwt| util::make_window(elwt, |w| w),
move |_elwt, window| {
let size = window.inner_size();

let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
// Intentionally set the size of the surface to something different than the size of the window.
surface
.resize(NonZeroU32::new(256).unwrap(), NonZeroU32::new(128).unwrap())
.unwrap();

let width = size.width.saturating_sub(200);
let height = size.height.saturating_sub(400);
tracing::info!("size initially at: {width}/{height}");
surface.resize(width, height).unwrap();
surface
},
)
Expand All @@ -30,24 +36,34 @@ fn main() {
}

match event {
WindowEvent::Resized(size) => {
let Some(surface) = surface else {
tracing::warn!("RedrawRequested fired before Resumed or after Suspended");
return;
};

let width = size.width.saturating_sub(200);
let height = size.height.saturating_sub(400);
tracing::info!("resized to: {width}/{height}");
surface.resize(width, height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
tracing::warn!("RedrawRequested fired before Resumed or after Suspended");
return;
};

let mut buffer = surface.buffer_mut().unwrap();
let width = buffer.width().get();
for y in 0..buffer.height().get() {
for x in 0..width {
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;

let color = blue | (green << 8) | (red << 16);
buffer[(y * width + x) as usize] = color;
let index = y * buffer.width() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
}

buffer.present().unwrap();
}
WindowEvent::CloseRequested
Expand Down
7 changes: 3 additions & 4 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::{backend_interface::*, backends, InitError, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use std::fmt;
use std::num::NonZeroU32;

/// A macro for creating the enum used to statically dispatch to the platform-specific implementation.
macro_rules! make_dispatch {
Expand Down Expand Up @@ -99,7 +98,7 @@ macro_rules! make_dispatch {
}
}

fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -147,7 +146,7 @@ macro_rules! make_dispatch {

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
#[inline]
fn width(&self) -> NonZeroU32 {
fn width(&self) -> u32 {
match self {
$(
$(#[$attr])*
Expand All @@ -157,7 +156,7 @@ macro_rules! make_dispatch {
}

#[inline]
fn height(&self) -> NonZeroU32 {
fn height(&self) -> u32 {
match self {
$(
$(#[$attr])*
Expand Down
Loading
Loading