Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Update to `objc2` 0.6.0.
- Bump MSRV to Rust 1.71.
- Added `Buffer::width` and `Buffer::height`.
Comment thread
madsmtm marked this conversation as resolved.
Outdated

# 0.4.6

Expand Down
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,23 @@ fn main() {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
let size = window.inner_size();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
NonZeroU32::new(size.width).unwrap(),
NonZeroU32::new(size.height).unwrap(),
)
.unwrap();

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

buffer[index as usize] = blue | (green << 8) | (red << 16);
for index in 0..(buffer.width() * buffer.height()) {
let y = index / buffer.width();
let x = index % buffer.width();
let red = x as u32 % 255;
let green = y as u32 % 255;
let blue = (x as u32 * y as u32) % 255;

buffer[index] = blue | (green << 8) | (red << 16);
}

buffer.present().unwrap();
Expand Down
25 changes: 11 additions & 14 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,20 @@ fn main() {
return;
};

let size = window.inner_size();
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
let elapsed = start.elapsed().as_secs_f64() % 1.0;
let elapsed = start.elapsed().as_secs_f64() % 1.0;

if (width.get(), height.get()) != *old_size {
*old_size = (width.get(), height.get());
*frames = pre_render_frames(width.get() as usize, height.get() as usize);
};
let mut buffer = surface.buffer_mut().unwrap();

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

let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
let size = (buffer.width(), buffer.height());
if size != *old_size {
*old_size = size;
*frames = pre_render_frames(size.0, size.1);
}

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

buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
Event::AboutToWait => {
window.request_redraw();
Expand Down
25 changes: 9 additions & 16 deletions examples/rectangle.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use softbuffer::Buffer;
use std::num::NonZeroU32;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
Expand All @@ -6,7 +8,9 @@ use winit::keyboard::{Key, NamedKey};
#[path = "utils/winit_app.rs"]
mod winit_app;

fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
fn redraw(buffer: &mut Buffer<'_, impl HasDisplayHandle, impl HasWindowHandle>, flag: bool) {
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 @@ -71,21 +75,10 @@ fn main() {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
// Grab the window's client area dimensions, and ensure they're valid
let size = window.inner_size();
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
// Draw something in the window
let mut buffer = surface.buffer_mut().unwrap();
redraw(
&mut buffer,
width.get() as usize,
height.get() as usize,
*flag,
);
buffer.present().unwrap();
}
// Draw something in the window
let mut buffer = surface.buffer_mut().unwrap();
redraw(&mut buffer, *flag);
buffer.present().unwrap();
}

Event::WindowEvent {
Expand Down
26 changes: 11 additions & 15 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,19 @@ pub(crate) fn entry(event_loop: EventLoop<()>) {
eprintln!("RedrawRequested fired before Resumed or after Suspended");
return;
};
let size = window.inner_size();
if let (Some(width), Some(height)) =
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
{
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..height.get() {
for x in 0..width.get() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y as usize * width.get() as usize + x as usize;
buffer[index] = blue | (green << 8) | (red << 16);
}
}

buffer.present().unwrap();
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x as u32 % 255;
let green = y as u32 % 255;
let blue = (x as u32 * y as u32) % 255;
let index = y * buffer.width() + x;
buffer[index] = blue | (green << 8) | (red << 16);
}
}

buffer.present().unwrap();
}
Event::WindowEvent {
event:
Expand Down
12 changes: 6 additions & 6 deletions examples/winit_multithread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ pub mod ex {
surface.resize(width, height).unwrap();

let mut buffer = surface.buffer_mut().unwrap();
for y in 0..height.get() {
for x in 0..width.get() {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y as usize * width.get() as usize + x as usize;
for y in 0..buffer.height() {
for x in 0..buffer.width() {
let red = x as u32 % 255;
let green = y as u32 % 255;
let blue = (x as u32 * y as u32) % 255;
let index = y * buffer.width() + x;
buffer[index] = blue | (green << 8) | (red << 16);
}
}
Expand Down
15 changes: 5 additions & 10 deletions examples/winit_wrong_sized_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ use winit::keyboard::{Key, NamedKey};
#[path = "utils/winit_app.rs"]
mod winit_app;

const BUFFER_WIDTH: usize = 256;
const BUFFER_HEIGHT: usize = 128;

fn main() {
let event_loop = EventLoop::new().unwrap();

Expand All @@ -24,10 +21,7 @@ fn main() {
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(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.resize(NonZeroU32::new(256).unwrap(), NonZeroU32::new(128).unwrap())
.unwrap();
surface
},
Expand All @@ -47,14 +41,15 @@ fn main() {
};

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

let color = blue | (green << 8) | (red << 16);
buffer[y * BUFFER_WIDTH + x] = color;
buffer[y * width + x] = color;
}
}
buffer.present().unwrap();
Expand Down
20 changes: 20 additions & 0 deletions src/backend_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,26 @@ macro_rules! make_dispatch {
}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferDispatch<'a, D, W> {
#[inline]
fn width(&self) -> usize {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.width(),
)*
}
}

#[inline]
fn height(&self) -> usize {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.height(),
)*
}
}

#[inline]
fn pixels(&self) -> &[u32] {
match self {
Expand Down
2 changes: 2 additions & 0 deletions src/backend_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub(crate) trait SurfaceInterface<D: HasDisplayHandle + ?Sized, W: HasWindowHand
}

pub(crate) trait BufferInterface {
fn width(&self) -> usize;
fn height(&self) -> usize;
fn pixels(&self) -> &[u32];
fn pixels_mut(&mut self) -> &mut [u32];
fn age(&self) -> u8;
Expand Down
8 changes: 8 additions & 0 deletions src/backends/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ pub struct BufferImpl<'a, D: ?Sized, W> {
unsafe impl<'a, D, W> Send for BufferImpl<'a, D, W> {}

impl<'a, D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'a, D, W> {
fn width(&self) -> usize {
self.native_window_buffer.width()
}

fn height(&self) -> usize {
self.native_window_buffer.height()
}

#[inline]
fn pixels(&self) -> &[u32] {
&self.buffer
Expand Down
8 changes: 8 additions & 0 deletions src/backends/cg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp.width
}

fn height(&self) -> usize {
self.imp.height
}

#[inline]
fn pixels(&self) -> &[u32] {
&self.buffer
Expand Down
8 changes: 8 additions & 0 deletions src/backends/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ impl<D: ?Sized, W: ?Sized> Drop for KmsImpl<D, W> {
}

impl<D: ?Sized, W: ?Sized> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.size.0.get() as usize
}

fn height(&self) -> usize {
self.size.1.get() as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
bytemuck::cast_slice(self.mapping.as_ref())
Expand Down
8 changes: 8 additions & 0 deletions src/backends/orbital.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,14 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp.width as usize
}

fn height(&self) -> usize {
self.imp.height as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
match &self.pixels {
Expand Down
4 changes: 2 additions & 2 deletions src/backends/wayland/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ pub(super) struct WaylandBuffer {
pool: wl_shm_pool::WlShmPool,
pool_size: i32,
buffer: wl_buffer::WlBuffer,
width: i32,
height: i32,
pub width: i32,
pub height: i32,
released: Arc<AtomicBool>,
pub age: u8,
}
Expand Down
14 changes: 14 additions & 0 deletions src/backends/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,15 @@ impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> SurfaceInterface<D, W>
));
};

let width = self.buffers.as_mut().unwrap().1.width;
let height = self.buffers.as_mut().unwrap().1.height;
let age = self.buffers.as_mut().unwrap().1.age;
Ok(BufferImpl {
stack: util::BorrowStack::new(self, |buffer| {
Ok(unsafe { buffer.buffers.as_mut().unwrap().1.mapped_mut() })
})?,
width,
height,
age,
})
}
Expand All @@ -259,10 +263,20 @@ impl<D: ?Sized, W: ?Sized> Drop for WaylandImpl<D, W> {

pub struct BufferImpl<'a, D: ?Sized, W> {
stack: util::BorrowStack<'a, WaylandImpl<D, W>, [u32]>,
width: i32,
height: i32,
age: u8,
}

impl<D: HasDisplayHandle + ?Sized, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.width as usize
}

fn height(&self) -> usize {
self.height as usize
}

#[inline]
fn pixels(&self) -> &[u32] {
self.stack.member()
Expand Down
16 changes: 16 additions & 0 deletions src/backends/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ pub struct BufferImpl<'a, D, W> {
}

impl<D: HasDisplayHandle, W: HasWindowHandle> BufferInterface for BufferImpl<'_, D, W> {
fn width(&self) -> usize {
self.imp
.size
.expect("must set size of surface before calling `width()` on the buffer")
.0
.get() as usize
}

fn height(&self) -> usize {
self.imp
.size
.expect("must set size of surface before calling `height()` on the buffer")
.1
.get() as usize
}

fn pixels(&self) -> &[u32] {
&self.imp.buffer
}
Expand Down
Loading