Skip to content

Commit 360321b

Browse files
committed
Handle zero-sized buffers internally
1 parent 6eec0c6 commit 360321b

25 files changed

+289
-307
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Unreleased
22

3+
- Use `u32` instead of `NonZeroU32`, and handle zero-sized buffers internally.
4+
35
- Update to `objc2` 0.6.0.
46
- Bump MSRV to Rust 1.71.
57
- Make `Context` cloneable.

README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ To run the Android-specific example on an Android phone: `cargo apk r --example
6868
## Example
6969

7070
```rust,no_run
71-
use std::num::NonZeroU32;
7271
use std::rc::Rc;
7372
use winit::event::{Event, WindowEvent};
7473
use winit::event_loop::{ControlFlow, EventLoop};
@@ -102,12 +101,7 @@ fn main() {
102101
return;
103102
};
104103
let size = window.inner_size();
105-
surface
106-
.resize(
107-
NonZeroU32::new(size.width).unwrap(),
108-
NonZeroU32::new(size.height).unwrap(),
109-
)
110-
.unwrap();
104+
surface.resize(size.width, size.height).unwrap();
111105
112106
let mut buffer = surface.buffer_mut().unwrap();
113107
for index in 0..(buffer.width().get() * buffer.height().get()) {

benches/buffer_mut.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ fn buffer_mut(c: &mut Criterion) {
1313
{
1414
use criterion::black_box;
1515
use softbuffer::{Context, Surface};
16-
use std::num::NonZeroU32;
1716
use winit::event_loop::ControlFlow;
1817
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
1918

@@ -32,12 +31,7 @@ fn buffer_mut(c: &mut Criterion) {
3231
let mut surface = Surface::new(&context, &window).unwrap();
3332

3433
let size = window.inner_size();
35-
surface
36-
.resize(
37-
NonZeroU32::new(size.width).unwrap(),
38-
NonZeroU32::new(size.height).unwrap(),
39-
)
40-
.unwrap();
34+
surface.resize(size.width, size.height).unwrap();
4135

4236
c.bench_function("buffer_mut()", |b| {
4337
b.iter(|| {

examples/animation.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#[cfg(not(target_arch = "wasm32"))]
22
use rayon::prelude::*;
33
use std::f64::consts::PI;
4-
use std::num::NonZeroU32;
54
use web_time::Instant;
65
use winit::event::{KeyEvent, WindowEvent};
76
use winit::event_loop::{ControlFlow, EventLoop};
@@ -45,11 +44,7 @@ fn main() {
4544
return;
4645
};
4746

48-
if let (Some(width), Some(height)) =
49-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
50-
{
51-
surface.resize(width, height).unwrap();
52-
}
47+
surface.resize(size.width, size.height).unwrap();
5348
}
5449
WindowEvent::RedrawRequested => {
5550
let Some(surface) = surface else {
@@ -61,7 +56,7 @@ fn main() {
6156

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

64-
let size = (buffer.width().get(), buffer.height().get());
59+
let size = (buffer.width(), buffer.height());
6560
if size != *old_size {
6661
*old_size = size;
6762
*frames = pre_render_frames(size.0, size.1);

examples/drm.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ mod imple {
1717
use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
1818
use softbuffer::{Context, Surface};
1919

20-
use std::num::NonZeroU32;
2120
use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd};
2221
use std::path::Path;
2322
use std::time::{Duration, Instant};
@@ -112,10 +111,7 @@ mod imple {
112111

113112
// Resize the surface.
114113
let (width, height) = mode.size();
115-
surface.resize(
116-
NonZeroU32::new(width as u32).unwrap(),
117-
NonZeroU32::new(height as u32).unwrap(),
118-
)?;
114+
surface.resize(width as u32, height as u32)?;
119115

120116
// Start drawing to it.
121117
let start = Instant::now();

examples/fruit.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use image::GenericImageView;
2-
use std::num::NonZeroU32;
32
use winit::event::{KeyEvent, WindowEvent};
43
use winit::event_loop::{ControlFlow, EventLoop};
54
use winit::keyboard::{Key, NamedKey};
@@ -26,12 +25,7 @@ fn main() {
2625
// Intentionally only set the size of the surface once, at creation.
2726
// This is needed if the window chooses to ignore the size we passed in above, and for the
2827
// platforms softbuffer supports that don't yet extract the size from the window.
29-
surface
30-
.resize(
31-
NonZeroU32::new(width).unwrap(),
32-
NonZeroU32::new(height).unwrap(),
33-
)
34-
.unwrap();
28+
surface.resize(width, height).unwrap();
3529
surface
3630
},
3731
)

examples/libxcb.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,7 @@ mod example {
115115
match event {
116116
Event::Expose(_) => {
117117
// Draw a width x height red rectangle.
118-
surface
119-
.resize(
120-
NonZeroU32::new(width.into()).unwrap(),
121-
NonZeroU32::new(height.into()).unwrap(),
122-
)
123-
.unwrap();
118+
surface.resize(width.into(), height.into()).unwrap();
124119
let mut buffer = surface.buffer_mut().unwrap();
125120
buffer.fill(RED);
126121
buffer.present().unwrap();

examples/rectangle.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
22
use softbuffer::Buffer;
3-
use std::num::NonZeroU32;
43
use winit::event::{ElementState, KeyEvent, WindowEvent};
54
use winit::event_loop::{ControlFlow, EventLoop};
65
use winit::keyboard::{Key, NamedKey};
@@ -9,8 +8,8 @@ use winit::keyboard::{Key, NamedKey};
98
mod winit_app;
109

1110
fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>, flag: bool) {
12-
let width = buffer.width().get();
13-
let height = buffer.height().get();
11+
let width = buffer.width();
12+
let height = buffer.height();
1413
for y in 0..height {
1514
for x in 0..width {
1615
let value = if flag && x >= 100 && x < width - 100 && y >= 100 && y < height - 100 {
@@ -58,12 +57,8 @@ fn main() {
5857
return;
5958
};
6059

61-
if let (Some(width), Some(height)) =
62-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
63-
{
64-
// Resize surface
65-
surface.resize(width, height).unwrap();
66-
}
60+
// Resize surface
61+
surface.resize(size.width, size.height).unwrap();
6762
}
6863
WindowEvent::RedrawRequested => {
6964
let Some(surface) = surface else {

examples/winit.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::num::NonZeroU32;
21
use winit::event::{KeyEvent, WindowEvent};
32
use winit::event_loop::{ControlFlow, EventLoop};
43
use winit::keyboard::{Key, NamedKey};
@@ -32,11 +31,7 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
3231
return;
3332
};
3433

35-
if let (Some(width), Some(height)) =
36-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
37-
{
38-
surface.resize(width, height).unwrap();
39-
}
34+
surface.resize(size.width, size.height).unwrap();
4035
}
4136
WindowEvent::RedrawRequested => {
4237
let Some(surface) = surface else {
@@ -45,12 +40,12 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
4540
};
4641

4742
let mut buffer = surface.buffer_mut().unwrap();
48-
for y in 0..buffer.height().get() {
49-
for x in 0..buffer.width().get() {
43+
for y in 0..buffer.height() {
44+
for x in 0..buffer.width() {
5045
let red = x % 255;
5146
let green = y % 255;
5247
let blue = (x * y) % 255;
53-
let index = y * buffer.width().get() + x;
48+
let index = y * buffer.width() + x;
5449
buffer[index as usize] = blue | (green << 8) | (red << 16);
5550
}
5651
}

examples/winit_multithread.rs

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ mod winit_app;
66

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

3231
// Perform the rendering.
3332
let mut surface = surface.lock().unwrap();
34-
if let (Some(width), Some(height)) = {
35-
let size = window.inner_size();
36-
println!("got size: {size:?}");
37-
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
38-
} {
39-
println!("resizing...");
40-
surface.resize(width, height).unwrap();
41-
42-
let mut buffer = surface.buffer_mut().unwrap();
43-
for y in 0..buffer.height().get() {
44-
for x in 0..buffer.width().get() {
45-
let red = x % 255;
46-
let green = y % 255;
47-
let blue = (x * y) % 255;
48-
let index = y * buffer.width().get() + x;
49-
buffer[index as usize] = blue | (green << 8) | (red << 16);
50-
}
33+
let size = window.inner_size();
34+
println!("resizing...");
35+
surface.resize(size.width, size.height).unwrap();
36+
37+
let mut buffer = surface.buffer_mut().unwrap();
38+
for y in 0..buffer.height() {
39+
for x in 0..buffer.width() {
40+
let red = x % 255;
41+
let green = y % 255;
42+
let blue = (x * y) % 255;
43+
let index = y * buffer.width() + x;
44+
buffer[index as usize] = blue | (green << 8) | (red << 16);
5145
}
52-
53-
println!("presenting...");
54-
buffer.present().unwrap();
5546
}
5647

48+
println!("presenting...");
49+
buffer.present().unwrap();
50+
5751
// We're done, tell the main thread to keep going.
5852
done.send(()).ok();
5953
}

0 commit comments

Comments
 (0)