Skip to content

Commit

Permalink
WIP no-copy in public API
Browse files Browse the repository at this point in the history
Currently only for `wayland` and the `winit` example.
  • Loading branch information
ids1024 committed Jan 11, 2023
1 parent c12cba8 commit 8577275
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
24 changes: 12 additions & 12 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@ fn main() {
let size = window.inner_size();
(size.width, size.height)
};
let buffer = (0..((width * height) as usize))
.map(|index| {
let y = index / (width as usize);
let x = index % (width as usize);
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;

let color = blue | (green << 8) | (red << 16);
surface.resize(width, height);

color as u32
})
.collect::<Vec<_>>();
let buffer = surface.buffer_mut();
for index in 0..(width * height) {
let y = index as u32 / width;
let x = index as u32 % width;
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;

surface.set_buffer(&buffer, width as u16, height as u16);
buffer[index as usize] = blue | (green << 8) | (red << 16);
}

surface.present();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
39 changes: 39 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,33 @@ macro_rules! make_dispatch {
}

impl SurfaceDispatch {
pub fn resize(&mut self, width: u32, height: u32) {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.resize(width, height),
)*
}
}

pub fn buffer_mut(&mut self) -> &mut [u32] {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.buffer_mut(),
)*
}
}

pub fn present(&mut self) {
match self {
$(
$(#[$attr])*
Self::$name(inner) => inner.present(),
)*
}
}

unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
match self {
$(
Expand Down Expand Up @@ -235,6 +262,18 @@ impl Surface {
})
}

pub fn resize(&mut self, width: u32, height: u32) {
self.surface_impl.resize(width, height);
}

pub fn buffer_mut(&mut self) -> &mut [u32] {
self.surface_impl.buffer_mut()
}

pub fn present(&mut self) {
self.surface_impl.present();
}

/// Shows the given buffer with the given width and height on the window corresponding to this
/// graphics context. Panics if buffer.len() ≠ width*height. If the size of the buffer does
/// not match the size of the window, the buffer is drawn in the upper-left corner of the window.
Expand Down
6 changes: 3 additions & 3 deletions src/wayland/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ impl WaylandImpl {
));
}

fn resize(&mut self, width: u32, height: u32) {
pub fn resize(&mut self, width: u32, height: u32) {
self.width = width as i32;
self.height = height as i32;
}

fn buffer_mut(&mut self) -> &mut [u32] {
pub fn buffer_mut(&mut self) -> &mut [u32] {
if let Some((_front, back)) = &mut self.buffers {
// Block if back buffer not released yet
if !back.released() {
Expand All @@ -111,7 +111,7 @@ impl WaylandImpl {
unsafe { self.buffers.as_mut().unwrap().1.mapped_mut() }
}

fn present(&mut self) {
pub fn present(&mut self) {
let _ = self
.display
.event_queue
Expand Down

0 comments on commit 8577275

Please sign in to comment.