Skip to content

Commit

Permalink
Use NonZeroU32 for arguments to Surface::resize
Browse files Browse the repository at this point in the history
  • Loading branch information
ids1024 committed Apr 5, 2023
1 parent 20ab5fd commit 58e277d
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 42 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ For now, the priority for new platforms is:
Example
==
```rust,no_run
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -72,7 +73,12 @@ fn main() {
let size = window.inner_size();
(size.width, size.height)
};
surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) {
Expand Down
8 changes: 7 additions & 1 deletion examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use instant::Instant;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -49,7 +50,12 @@ fn main() {

let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];

surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -35,7 +36,12 @@ fn main() {

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface.resize(fruit.width(), fruit.height()).unwrap();
surface
.resize(
NonZeroU32::new(fruit.width()).unwrap(),
NonZeroU32::new(fruit.height()).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
let width = fruit.width() as usize;
Expand Down
8 changes: 7 additions & 1 deletion examples/libxcb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(all(feature = "x11", any(target_os = "linux", target_os = "freebsd")))]
mod example {
use raw_window_handle::{RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, XcbWindowHandle};
use std::num::NonZeroU32;
use x11rb::{
connection::Connection,
protocol::{
Expand Down Expand Up @@ -98,7 +99,12 @@ mod example {
match event {
Event::Expose(_) => {
// Draw a width x height red rectangle.
surface.resize(width.into(), height.into()).unwrap();
surface
.resize(
NonZeroU32::new(width.into()).unwrap(),
NonZeroU32::new(height.into()).unwrap(),
)
.unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.fill(RED);
buffer.present().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -57,7 +58,12 @@ fn main() {
};

// Resize surface if needed
surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

// Draw something in the window
let mut buffer = surface.buffer_mut().unwrap();
Expand Down
8 changes: 7 additions & 1 deletion examples/winit.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -33,7 +34,12 @@ fn main() {
(size.width, size.height)
};

surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for index in 0..(width * height) {
Expand Down
6 changes: 5 additions & 1 deletion examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand Down Expand Up @@ -32,7 +33,10 @@ fn main() {
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface
.resize(BUFFER_WIDTH as u32, BUFFER_HEIGHT as u32)
.resize(
NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.unwrap();

let mut buffer = surface.buffer_mut().unwrap();
Expand Down
7 changes: 4 additions & 3 deletions src/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use cocoa::base::{id, nil};
use cocoa::quartzcore::{transaction, CALayer, ContentsGravity};
use foreign_types::ForeignType;

use std::num::NonZeroU32;
use std::sync::Arc;

struct Buffer(Vec<u32>);
Expand Down Expand Up @@ -56,9 +57,9 @@ impl CGImpl {
})
}

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

Expand Down
6 changes: 5 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use std::error::Error;
use std::num::NonZeroU32;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -29,7 +30,10 @@ pub enum SoftBufferError {
IncompleteDisplayHandle,

#[error("Surface size {width}x{height} out of range for backend.")]
SizeOutOfRange { width: u32, height: u32 },
SizeOutOfRange {
width: NonZeroU32,
height: NonZeroU32,
},

#[error("Platform error")]
PlatformError(Option<String>, Option<Box<dyn Error>>),
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod error;
mod util;

use std::marker::PhantomData;
use std::num::NonZeroU32;
use std::ops;
#[cfg(any(wayland_platform, x11_platform))]
use std::rc::Rc;
Expand Down Expand Up @@ -76,7 +77,7 @@ macro_rules! make_dispatch {
}

impl SurfaceDispatch {
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
match self {
$(
$(#[$attr])*
Expand Down Expand Up @@ -297,7 +298,7 @@ impl Surface {
/// in the upper-left corner of the window. It is recommended in most production use cases
/// to have the buffer fill the entire window. Use your windowing library to find the size
/// of the window.
pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.surface_impl.resize(width, height)
}

Expand Down
8 changes: 4 additions & 4 deletions src/orbital.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use raw_window_handle::OrbitalWindowHandle;
use std::{cmp, slice, str};
use std::{cmp, num::NonZeroU32, slice, str};

use crate::SoftBufferError;

Expand Down Expand Up @@ -68,9 +68,9 @@ impl OrbitalImpl {
})
}

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

Expand Down
24 changes: 14 additions & 10 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{error::unwrap, util, SoftBufferError};
use raw_window_handle::{WaylandDisplayHandle, WaylandWindowHandle};
use std::{cell::RefCell, num::NonZeroI32, rc::Rc};
use std::{
cell::RefCell,
num::{NonZeroI32, NonZeroU32},
rc::Rc,
};
use wayland_client::{
backend::{Backend, ObjectId},
globals::{registry_queue_init, GlobalListContents},
Expand Down Expand Up @@ -77,11 +81,11 @@ impl WaylandImpl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
self.size = Some(
(|| {
let width = NonZeroI32::new(i32::try_from(width).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height).ok()?)?;
let width = NonZeroI32::try_from(width).ok()?;
let height = NonZeroI32::try_from(height).ok()?;
Some((width, height))
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?,
Expand Down Expand Up @@ -109,20 +113,20 @@ impl WaylandImpl {
}

// Resize, if buffer isn't large enough
back.resize(width.into(), height.into());
back.resize(width.get(), height.get());
} else {
// Allocate front and back buffer
self.buffers = Some((
WaylandBuffer::new(
&self.display.shm,
width.into(),
height.into(),
width.get(),
height.get(),
&self.display.qh,
),
WaylandBuffer::new(
&self.display.shm,
width.into(),
height.into(),
width.get(),
height.get(),
&self.display.qh,
),
));
Expand Down Expand Up @@ -177,7 +181,7 @@ impl<'a> BufferImpl<'a> {
imp.surface.damage(0, 0, i32::MAX, i32::MAX);
} else {
// Introduced in version 4, it is an error to use this request in version 3 or lower.
imp.surface.damage_buffer(0, 0, width.into(), height.into());
imp.surface.damage_buffer(0, 0, width.get(), height.get());
}

imp.surface.commit();
Expand Down
10 changes: 9 additions & 1 deletion src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use web_sys::ImageData;

use crate::SoftBufferError;
use std::convert::TryInto;
use std::num::NonZeroU32;

/// Display implementation for the web platform.
///
Expand Down Expand Up @@ -96,7 +97,14 @@ impl WebImpl {
}

/// Resize the canvas to the given dimensions.
pub(crate) fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub(crate) fn resize(
&mut self,
width: NonZeroU32,
height: NonZeroU32,
) -> Result<(), SoftBufferError> {
let width = width.get();
let height = height.get();

self.buffer.resize(total_len(width, height), 0);
self.canvas.set_width(width);
self.canvas.set_height(height);
Expand Down
16 changes: 8 additions & 8 deletions src/win32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use raw_window_handle::Win32WindowHandle;

use std::io;
use std::mem;
use std::num::NonZeroI32;
use std::num::{NonZeroI32, NonZeroU32};
use std::ptr::{self, NonNull};
use std::slice;

Expand Down Expand Up @@ -47,8 +47,8 @@ impl Buffer {
let bitmap_info = BitmapInfo {
bmi_header: Gdi::BITMAPINFOHEADER {
biSize: mem::size_of::<Gdi::BITMAPINFOHEADER>() as u32,
biWidth: width.into(),
biHeight: -i32::from(height),
biWidth: width.get(),
biHeight: -height.get(),
biPlanes: 1,
biBitCount: 32,
biCompression: Gdi::BI_BITFIELDS,
Expand Down Expand Up @@ -164,10 +164,10 @@ impl Win32Impl {
})
}

pub fn resize(&mut self, width: u32, height: u32) -> Result<(), SoftBufferError> {
pub fn resize(&mut self, width: NonZeroU32, height: NonZeroU32) -> Result<(), SoftBufferError> {
let (width, height) = (|| {
let width = NonZeroI32::new(i32::try_from(width).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height).ok()?)?;
let width = NonZeroI32::new(i32::try_from(width.get()).ok()?)?;
let height = NonZeroI32::new(i32::try_from(height.get()).ok()?)?;
Some((width, height))
})()
.ok_or(SoftBufferError::SizeOutOfRange { width, height })?;
Expand Down Expand Up @@ -215,8 +215,8 @@ impl<'a> BufferImpl<'a> {
imp.dc,
0,
0,
buffer.width.into(),
buffer.height.into(),
buffer.width.get(),
buffer.height.get(),
buffer.dc,
0,
0,
Expand Down
Loading

0 comments on commit 58e277d

Please sign in to comment.