Skip to content

Commit

Permalink
breaking: Use raw-window-handle version 0.6
Browse files Browse the repository at this point in the history
Signed-off-by: John Nunley <[email protected]>
Co-Authored-By: dAxpeDDa <[email protected]>
  • Loading branch information
notgull and daxpedda authored Oct 27, 2023
1 parent 18c9447 commit 0bcd2e2
Show file tree
Hide file tree
Showing 22 changed files with 828 additions and 626 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Unreleased

* **Breaking:** Port to use `raw-window-handle` v0.6.

# 0.3.2

* Document that `present_with_damage` is supported on web platforms. (#152)
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ x11-dlopen = ["tiny-xlib/dlopen", "x11rb/dl-libxcb"]

[dependencies]
log = "0.4.17"
raw-window-handle = "0.5.0"
raw_window_handle = { package = "raw-window-handle", version = "0.6", features = ["std"] }

[target.'cfg(all(unix, not(any(target_vendor = "apple", target_os = "android", target_os = "redox"))))'.dependencies]
as-raw-xcb-connection = { version = "1.0.0", optional = true }
Expand Down Expand Up @@ -79,7 +79,7 @@ cfg_aliases = "0.1.1"
colorous = "1.0.12"
criterion = { version = "0.4.0", default-features = false, features = ["cargo_bench_support"] }
instant = "0.1.12"
winit = "0.28.1"
winit = "0.29.2"
winit-test = "0.1.0"

[dev-dependencies.image]
Expand Down Expand Up @@ -119,3 +119,4 @@ targets = [
"x86_64-unknown-linux-gnu",
"wasm32-unknown-unknown",
]

21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,22 @@ Example
==
```rust,no_run
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
event_loop.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);
match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
Event::WindowEvent { window_id, event: WindowEvent::RedrawRequested } if window_id == window.id() => {
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
Expand All @@ -96,17 +97,17 @@ fn main() {
buffer[index as usize] = blue | (green << 8) | (red << 16);
}
buffer.present().unwrap();
buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
elwt.exit();
}
_ => {}
}
});
}).unwrap();
}
```

Expand Down
18 changes: 10 additions & 8 deletions benches/buffer_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,22 @@ fn buffer_mut(c: &mut Criterion) {
use criterion::black_box;
use softbuffer::{Context, Surface};
use std::num::NonZeroU32;
use winit::platform::run_return::EventLoopExtRunReturn;
use winit::event_loop::ControlFlow;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;

let mut evl = winit::event_loop::EventLoop::new();
let mut evl = winit::event_loop::EventLoop::new().unwrap();
let window = winit::window::WindowBuilder::new()
.with_visible(false)
.build(&evl)
.unwrap();

evl.run_return(move |ev, elwt, control_flow| {
control_flow.set_poll();
evl.run_on_demand(move |ev, elwt| {
elwt.set_control_flow(ControlFlow::Poll);

if let winit::event::Event::RedrawEventsCleared = ev {
control_flow.set_exit();
if let winit::event::Event::AboutToWait = ev {
elwt.exit();

let mut surface = unsafe {
let mut surface = {
let context = Context::new(elwt).unwrap();
Surface::new(&context, &window).unwrap()
};
Expand Down Expand Up @@ -57,7 +58,8 @@ fn buffer_mut(c: &mut Criterion) {
});
});
}
});
})
.unwrap();
}
}

Expand Down
82 changes: 42 additions & 40 deletions examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ use instant::Instant;
use rayon::prelude::*;
use std::f64::consts::PI;
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;

fn main() {
let event_loop = EventLoop::new();
let window = WindowBuilder::new().build(&event_loop).unwrap();
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(WindowBuilder::new().build(&event_loop).unwrap());

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -21,57 +22,58 @@ fn main() {
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas())
.append_child(&window.canvas().unwrap())
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

let mut old_size = (0, 0);
let mut frames = pre_render_frames(0, 0);

let start = Instant::now();
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
event_loop
.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Poll);

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
let elapsed = start.elapsed().as_secs_f64() % 1.0;
let (width, height) = {
let size = window.inner_size();
(size.width, size.height)
};
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
if let (Some(width), Some(height)) = {
let size = window.inner_size();
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
let elapsed = start.elapsed().as_secs_f64() % 1.0;

if (width, height) != old_size {
old_size = (width, height);
frames = pre_render_frames(width as usize, height as usize);
};
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 frame = &frames[((elapsed * 60.0).round() as usize).clamp(0, 59)];
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();
surface.resize(width, height).unwrap();
let mut buffer = surface.buffer_mut().unwrap();
buffer.copy_from_slice(frame);
buffer.present().unwrap();
}
}
Event::AboutToWait => {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
Event::MainEventsCleared => {
window.request_redraw();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
})
.unwrap();
}

fn pre_render_frames(width: usize, height: usize) -> Vec<Vec<u32>> {
Expand Down
21 changes: 11 additions & 10 deletions examples/drm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod imple {
use drm::control::{connector, Device as CtrlDevice, Event, ModeTypeFlags, PlaneType};
use drm::Device;

use raw_window_handle::{DrmDisplayHandle, DrmWindowHandle};
use raw_window_handle::{DisplayHandle, DrmDisplayHandle, DrmWindowHandle, WindowHandle};
use softbuffer::{Context, Surface};

use std::num::NonZeroU32;
Expand All @@ -19,11 +19,10 @@ mod imple {

// Create the softbuffer context.
let context = unsafe {
Context::from_raw({
let mut handle = DrmDisplayHandle::empty();
handle.fd = device.as_fd().as_raw_fd();
Context::new(DisplayHandle::borrow_raw({
let handle = DrmDisplayHandle::new(device.as_fd().as_raw_fd());
handle.into()
})
}))
}?;

// Get the DRM handles.
Expand Down Expand Up @@ -93,11 +92,13 @@ mod imple {
// Create the surface on top of this plane.
// Note: This requires root on DRM/KMS.
let mut surface = unsafe {
Surface::from_raw(&context, {
let mut handle = DrmWindowHandle::empty();
handle.plane = (**plane).into();
handle.into()
})
Surface::new(
&context,
WindowHandle::borrow_raw({
let handle = DrmWindowHandle::new((**plane).into());
handle.into()
}),
)
}?;

// Resize the surface.
Expand Down
82 changes: 45 additions & 37 deletions examples/fruit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use image::GenericImageView;
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
Expand All @@ -8,11 +9,13 @@ fn main() {
//see fruit.jpg.license for the license of fruit.jpg
let fruit = image::load_from_memory(include_bytes!("fruit.jpg")).unwrap();

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height()))
.build(&event_loop)
.unwrap();
let event_loop = EventLoop::new().unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_inner_size(winit::dpi::PhysicalSize::new(fruit.width(), fruit.height()))
.build(&event_loop)
.unwrap(),
);

#[cfg(target_arch = "wasm32")]
{
Expand All @@ -24,45 +27,50 @@ fn main() {
.unwrap()
.body()
.unwrap()
.append_child(&window.canvas())
.append_child(&window.canvas().unwrap())
.unwrap();
}

let context = unsafe { softbuffer::Context::new(&window) }.unwrap();
let mut surface = unsafe { softbuffer::Surface::new(&context, &window) }.unwrap();
let context = softbuffer::Context::new(window.clone()).unwrap();
let mut surface = softbuffer::Surface::new(&context, window.clone()).unwrap();

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
event_loop
.run(move |event, elwt| {
elwt.set_control_flow(ControlFlow::Wait);

match event {
Event::RedrawRequested(window_id) if window_id == window.id() => {
surface
.resize(
NonZeroU32::new(fruit.width()).unwrap(),
NonZeroU32::new(fruit.height()).unwrap(),
)
.unwrap();
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::CloseRequested,
} if window_id == window.id() => {
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 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;
}
let color = blue | (green << 8) | (red << 16);
buffer[y as usize * width + x as usize] = color;
}

buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
buffer.present().unwrap();
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
_ => {}
}
});
})
.unwrap();
}
Loading

0 comments on commit 0bcd2e2

Please sign in to comment.