Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned pixel buffer for no-copy presentation #65

Merged
merged 26 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c12cba8
wayland: Use `mmap`
ids1024 Jan 10, 2023
8577275
WIP no-copy in public API
ids1024 Jan 11, 2023
7a5f61d
win32: Use owned buffer and `Gdi:BitBlt`
ids1024 Jan 11, 2023
de2f37d
Implement X11 owned buffer
notgull Jan 11, 2023
ce59528
fmt
notgull Jan 11, 2023
579656a
Add owned buffer for web
notgull Jan 11, 2023
256ff9e
cg: Port to owned buffer
ids1024 Jan 13, 2023
8c3275c
win32: Fix length of buffer slice
ids1024 Jan 14, 2023
b16e00e
Port more examples to owned buffers
ids1024 Jan 14, 2023
39cad4e
Merge remote-tracking branch 'origin/master' into owned-buffer
ids1024 Jan 18, 2023
5e8a09d
orbital: Implement new API (but with copy)
ids1024 Jan 18, 2023
1c08169
cg: Allocate correct buffer size
ids1024 Jan 18, 2023
b963c43
examples: Use owned buffer API for `fruit` and `winit_wrong_sized_buf…
ids1024 Jan 18, 2023
a922a39
Remove `set_buffer`, and move its documentation to the new methods
ids1024 Jan 19, 2023
1bb74dd
Make `Context`/`Surface` consistently `!Send`/`!Sync`
ids1024 Jan 19, 2023
34a2796
Don't use thread-safe types
ids1024 Jan 20, 2023
82ca6be
Make `Surface::present()` return a `Result`
ids1024 Jan 20, 2023
9ab6807
Merge remote-tracking branch 'origin/master' into owned-buffer
ids1024 Jan 25, 2023
bb8b1e5
examples: Unwrap present errors
ids1024 Jan 25, 2023
527e170
Make `resize` and `buffer_mut` return `Result`s
ids1024 Jan 25, 2023
e38809e
Separate `Buffer` type, with a `present` method
ids1024 Jan 26, 2023
a4931bf
x11: Use `swbuf_err` instead of `map_err`
ids1024 Feb 23, 2023
109e884
Make `deref` and `deref_mut` as `#[inline]`
ids1024 Feb 23, 2023
8d35f0e
Update documentation of public API
ids1024 Feb 23, 2023
20ab5fd
Updates to documentation for `Surface`
ids1024 Apr 1, 2023
58e277d
Use `NonZeroU32` for arguments to `Surface::resize`
ids1024 Apr 4, 2023
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
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ jobs:
- name: Check Formatting
run: cargo +stable fmt --all -- --check

miri-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: hecrj/setup-rust-action@v1
with:
rust-version: nightly
components: miri
- name: Run tests with miri
run: cargo +nightly miri test

tests:
name: Tests
strategy:
Expand Down
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rust-version = "1.60.0"

[features]
default = ["x11", "wayland", "wayland-dlopen"]
wayland = ["wayland-backend", "wayland-client", "nix", "fastrand"]
wayland = ["wayland-backend", "wayland-client", "memmap2", "nix", "fastrand"]
wayland-dlopen = ["wayland-sys/dlopen"]
x11 = ["bytemuck", "nix", "x11rb", "x11-dl"]

Expand All @@ -25,6 +25,7 @@ thiserror = "1.0.30"

[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
bytemuck = { version = "1.12.3", optional = true }
memmap2 = { version = "0.5.8", optional = true }
nix = { version = "0.26.1", optional = true }
wayland-backend = { version = "0.1.0", features = ["client_system"], optional = true }
wayland-client = { version = "0.30.0", optional = true }
Expand All @@ -40,6 +41,7 @@ version = "0.42.0"
features = ["Win32_Graphics_Gdi", "Win32_UI_WindowsAndMessaging", "Win32_Foundation"]

[target.'cfg(target_os = "macos")'.dependencies]
bytemuck = { version = "1.12.3", features = ["extern_crate_alloc"] }
cocoa = "0.24.0"
core-graphics = "0.22.3"
foreign-types = "0.3.0"
Expand Down
35 changes: 20 additions & 15 deletions 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,21 +73,25 @@ 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);

color as u32
})
.collect::<Vec<_>>();

surface.set_buffer(&buffer, width as u16, height as u16);
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(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);
}

buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
14 changes: 12 additions & 2 deletions 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 @@ -47,8 +48,17 @@ fn main() {
frames = pre_render_frames(width as usize, height as usize);
};

let buffer = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
surface.set_buffer(buffer.as_slice(), width as u16, height as u16);
let frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];

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();
}
Event::MainEventsCleared => {
window.request_redraw();
Expand Down
31 changes: 20 additions & 11 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

fn main() {
//see fruit.jpg.license for the license of fruit.jpg
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();
let buffer = fruit
.pixels()
.map(|(_x, _y, pixel)| {
let red = pixel.0[0] as u32;
let green = pixel.0[1] as u32;
let blue = pixel.0[2] as u32;

blue | (green << 8) | (red << 16)
})
.collect::<Vec<_>>();

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
Expand Down Expand Up @@ -45,7 +36,25 @@ fn main() {

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface.set_buffer(&buffer, fruit.width() as u16, fruit.height() as u16);
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;
for (x, y, pixel) in fruit.pixels() {
let red = pixel.0[0] as u32;
let green = pixel.0[1] as u32;
let blue = pixel.0[2] as u32;

let color = blue | (green << 8) | (red << 16);
buffer[y as usize * width + x as usize] = color;
}

buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
19 changes: 12 additions & 7 deletions 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 All @@ -12,6 +13,8 @@ mod example {
xcb_ffi::XCBConnection,
};

const RED: u32 = 255 << 16;

pub(crate) fn run() {
// Create a new XCB connection
let (conn, screen) = XCBConnection::connect(None).expect("Failed to connect to X server");
Expand Down Expand Up @@ -96,13 +99,15 @@ mod example {
match event {
Event::Expose(_) => {
// Draw a width x height red rectangle.
let red = 255 << 16;
let source = std::iter::repeat(red)
.take((width as usize * height as usize) as _)
.collect::<Vec<_>>();

// Draw the buffer.
surface.set_buffer(&source, width, height);
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();
}
Event::ConfigureNotify(configure_notify) => {
width = configure_notify.width;
Expand Down
26 changes: 14 additions & 12 deletions 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 @@ -43,7 +44,6 @@ fn main() {
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();

let mut buffer = Vec::new();
let mut flag = false;

event_loop.run(move |event, _, control_flow| {
Expand All @@ -54,19 +54,21 @@ fn main() {
// Grab the window's client area dimensions
let (width, height) = {
let size = window.inner_size();
(size.width as usize, size.height as usize)
(size.width, size.height)
};

// Resize the off-screen buffer if the window size has changed
if buffer.len() != width * height {
buffer.resize(width * height, 0);
}

// Draw something in the offscreen buffer
redraw(&mut buffer, width, height, flag);

// Blit the offscreen buffer to the window's client area
surface.set_buffer(&buffer, width as u16, height as u16);
// Resize surface if needed
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

// Draw something in the window
let mut buffer = surface.buffer_mut().unwrap();
redraw(&mut buffer, width as usize, height as usize, flag);
buffer.present().unwrap();
}

Event::WindowEvent {
Expand Down
30 changes: 18 additions & 12 deletions 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 @@ -32,21 +33,26 @@ 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(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.unwrap();

color as u32
})
.collect::<Vec<_>>();
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;

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

buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
31 changes: 18 additions & 13 deletions 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 @@ -31,21 +32,25 @@ fn main() {

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let buffer = (0..(BUFFER_WIDTH * BUFFER_HEIGHT))
.map(|index| {
let y = index / BUFFER_WIDTH;
let x = index % BUFFER_WIDTH;
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
surface
.resize(
NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.unwrap();

let color = blue | (green << 8) | (red << 16);

color as u32
})
.collect::<Vec<_>>();
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;

surface.set_buffer(&buffer, BUFFER_WIDTH as u16, BUFFER_HEIGHT as u16);
let color = blue | (green << 8) | (red << 16);
buffer[y * BUFFER_WIDTH + x] = color;
}
}
buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
Expand Down
Loading