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

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

- Update to `objc2` 0.6.0.
- Bump MSRV to Rust 1.71.
- Make `Context` cloneable.
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 @@ -13,7 +13,6 @@ fn buffer_mut(c: &mut 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 @@ -32,12 +31,7 @@ fn buffer_mut(c: &mut 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_arch = "wasm32"))]
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 @@ -45,11 +44,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 @@ -61,7 +56,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 @@ -17,7 +17,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 @@ -112,10 +111,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 All @@ -26,12 +25,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 @@ -115,12 +115,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,6 +1,5 @@
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};
Expand All @@ -9,8 +8,8 @@ use winit::keyboard::{Key, NamedKey};
mod winit_app;

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 @@ -58,12 +57,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 @@ -32,11 +31,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 @@ -45,12 +40,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
36 changes: 15 additions & 21 deletions examples/winit_multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ mod winit_app;

#[cfg(not(target_family = "wasm"))]
pub mod ex {
use std::num::NonZeroU32;
use std::sync::{mpsc, Arc, Mutex};
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop, OwnedDisplayHandle};
Expand All @@ -31,29 +30,24 @@ pub mod ex {

// Perform the rendering.
let mut surface = surface.lock().unwrap();
if let (Some(width), Some(height)) = {
let size = window.inner_size();
println!("got size: {size:?}");
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
println!("resizing...");
surface.resize(width, height).unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height().get() {
for x in 0..buffer.width().get() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y * buffer.width().get() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
let size = window.inner_size();
println!("resizing...");
surface.resize(size.width, size.height).unwrap();

let mut buffer = surface.buffer_mut().unwrap();
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() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}

println!("presenting...");
buffer.present().unwrap();
}

println!("presenting...");
buffer.present().unwrap();

// We're done, tell the main thread to keep going.
done.send(()).ok();
}
Expand Down
9 changes: 3 additions & 6 deletions examples/winit_wrong_sized_buffer.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 All @@ -15,9 +14,7 @@ fn main() {
move |_elwt, window| {
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();
surface.resize(256, 128).unwrap();
surface
},
)
Expand All @@ -36,8 +33,8 @@ fn main() {
};

let mut buffer = surface.buffer_mut().unwrap();
let width = buffer.width().get();
for y in 0..buffer.height().get() {
let width = buffer.width();
for y in 0..buffer.height() {
for x in 0..width {
let red = x % 255;
let green = y % 255;
Expand Down
73 changes: 73 additions & 0 deletions examples/zero-sized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use winit::event::{KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{Key, NamedKey};

#[path = "utils/winit_app.rs"]
mod winit_app;

fn main() {
let event_loop = EventLoop::new().unwrap();
let context = softbuffer::Context::new(event_loop.owned_display_handle()).unwrap();

let app = winit_app::WinitAppBuilder::with_init(
|elwt| winit_app::make_window(elwt, |w| w),
move |_elwt, window| {
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
surface.resize(0, 0).unwrap();
surface
},
)
.with_event_handler(|window, surface, window_id, event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

if window_id != window.id() {
return;
}

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

let width = size.width.saturating_sub(100);
let height = size.height.saturating_sub(100);
surface.resize(width, height).unwrap();
}
WindowEvent::RedrawRequested => {
let Some(surface) = surface else {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};

let mut buffer = surface.buffer_mut().unwrap();
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() + x;
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
}

buffer.present().unwrap();
}
WindowEvent::CloseRequested
| WindowEvent::KeyboardInput {
event:
KeyEvent {
logical_key: Key::Named(NamedKey::Escape),
..
},
..
} => {
elwt.exit();
}
_ => {}
}
});

winit_app::run_app(event_loop, app);
}
7 changes: 3 additions & 4 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use crate::{backend_interface::*, backends, InitError, Rect, SoftBufferError};

use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
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 @@ -87,7 +86,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 @@ -124,7 +123,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 @@ -134,7 +133,7 @@ macro_rules! make_dispatch {
}

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