breaking: Use raw-window-handle version 0.6

Signed-off-by: John Nunley <dev@notgull.net>
Co-Authored-By: dAxpeDDa <daxpedda@gmail.com>
This commit is contained in:
John Nunley 2023-10-26 19:15:51 -07:00 committed by GitHub
parent 18c944736e
commit 0bcd2e22a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 830 additions and 628 deletions

View file

@ -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")]
{
@ -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>> {

View file

@ -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;
@ -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.
@ -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.

View file

@ -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;
@ -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")]
{
@ -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();
}
buffer.present().unwrap();
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
})
.unwrap();
}

View file

@ -2,8 +2,11 @@
#[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 raw_window_handle::{
DisplayHandle, RawDisplayHandle, RawWindowHandle, WindowHandle, XcbDisplayHandle,
XcbWindowHandle,
};
use std::{num::NonZeroU32, ptr::NonNull};
use x11rb::{
connection::Connection,
protocol::{
@ -20,9 +23,10 @@ mod example {
let (conn, screen) = XCBConnection::connect(None).expect("Failed to connect to X server");
// x11rb doesn't use raw-window-handle yet, so just create our own.
let mut display_handle = XcbDisplayHandle::empty();
display_handle.connection = conn.get_raw_xcb_connection() as *mut _;
display_handle.screen = screen as _;
let display_handle = XcbDisplayHandle::new(
NonNull::new(conn.get_raw_xcb_connection() as *mut _),
screen as _,
);
// Create a new window.
let mut width = 640u16;
@ -50,18 +54,17 @@ mod example {
.check()
.unwrap();
let mut window_handle = XcbWindowHandle::empty();
window_handle.window = window as _;
window_handle.visual_id = root_visual as _;
let mut window_handle = XcbWindowHandle::new(NonZeroU32::new(window).unwrap());
window_handle.visual_id = NonZeroU32::new(root_visual);
// Create a new softbuffer context.
// SAFETY: The display and window handles outlive the context.
let context =
unsafe { softbuffer::Context::from_raw(RawDisplayHandle::Xcb(display_handle)) }
.unwrap();
let mut surface =
unsafe { softbuffer::Surface::from_raw(&context, RawWindowHandle::Xcb(window_handle)) }
.unwrap();
let display_handle =
unsafe { DisplayHandle::borrow_raw(RawDisplayHandle::Xcb(display_handle)) };
let window_handle =
unsafe { WindowHandle::borrow_raw(RawWindowHandle::Xcb(window_handle)) };
let context = softbuffer::Context::new(display_handle).unwrap();
let mut surface = softbuffer::Surface::new(&context, window_handle).unwrap();
// Register an atom for closing the window.
let wm_protocols_atom = conn

View file

@ -1,6 +1,8 @@
use std::num::NonZeroU32;
use winit::event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent};
use std::rc::Rc;
use winit::event::{ElementState, Event, KeyEvent, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::keyboard::{KeyCode, PhysicalKey};
use winit::window::WindowBuilder;
fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
@ -20,12 +22,14 @@ fn redraw(buffer: &mut [u32], width: usize, height: usize, flag: bool) {
}
fn main() {
let event_loop = EventLoop::new();
let event_loop = EventLoop::new().unwrap();
let window = WindowBuilder::new()
.with_title("Press space to show/hide a rectangle")
.build(&event_loop)
.unwrap();
let window = Rc::new(
WindowBuilder::new()
.with_title("Press space to show/hide a rectangle")
.build(&event_loop)
.unwrap(),
);
#[cfg(target_arch = "wasm32")]
{
@ -37,66 +41,71 @@ 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 flag = false;
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() => {
// Grab the window's client area dimensions
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() => {
// Grab the window's client area dimensions
if let (Some(width), Some(height)) = {
let size = window.inner_size();
(NonZeroU32::new(size.width), NonZeroU32::new(size.height))
} {
// Resize surface if needed
surface.resize(width, height).unwrap();
// 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.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, width as usize, height as usize, flag);
buffer.present().unwrap();
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
event:
KeyEvent {
state: ElementState::Pressed,
physical_key: PhysicalKey::Code(KeyCode::Space),
..
},
..
},
window_id,
} if window_id == window.id() => {
// Flip the rectangle flag and request a redraw to show the changed image
flag = !flag;
window.request_redraw();
}
_ => {}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
Event::WindowEvent {
event:
WindowEvent::KeyboardInput {
input:
KeyboardInput {
state: ElementState::Pressed,
virtual_keycode: Some(VirtualKeyCode::Space),
..
},
..
},
window_id,
} if window_id == window.id() => {
// Flip the rectangle flag and request a redraw to show the changed image
flag = !flag;
window.request_redraw();
}
_ => {}
}
});
})
.unwrap();
}

View file

@ -1,11 +1,12 @@
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")]
{
@ -17,50 +18,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() => {
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))
} {
surface.resize(width, height).unwrap();
surface
.resize(
NonZeroU32::new(width).unwrap(),
NonZeroU32::new(height).unwrap(),
)
.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;
buffer[index] = blue | (green << 8) | (red << 16);
}
}
let mut buffer = surface.buffer_mut().unwrap();
for y in 0..height {
for x in 0..width {
let red = x % 255;
let green = y % 255;
let blue = (x * y) % 255;
let index = y as usize * width as usize + x as usize;
buffer[index] = blue | (green << 8) | (red << 16);
buffer.present().unwrap();
}
}
buffer.present().unwrap();
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
})
.unwrap();
}

View file

@ -1,4 +1,5 @@
use std::num::NonZeroU32;
use std::rc::Rc;
use winit::event::{Event, WindowEvent};
use winit::event_loop::{ControlFlow, EventLoop};
use winit::window::WindowBuilder;
@ -7,8 +8,8 @@ const BUFFER_WIDTH: usize = 256;
const BUFFER_HEIGHT: usize = 128;
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")]
{
@ -20,45 +21,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(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.unwrap();
match event {
Event::WindowEvent {
window_id,
event: WindowEvent::RedrawRequested,
} if window_id == window.id() => {
surface
.resize(
NonZeroU32::new(BUFFER_WIDTH as u32).unwrap(),
NonZeroU32::new(BUFFER_HEIGHT as u32).unwrap(),
)
.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 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 color = blue | (green << 8) | (red << 16);
buffer[y * BUFFER_WIDTH + x] = color;
let color = blue | (green << 8) | (red << 16);
buffer[y * BUFFER_WIDTH + x] = color;
}
}
buffer.present().unwrap();
}
buffer.present().unwrap();
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
elwt.exit();
}
_ => {}
}
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == window.id() => {
*control_flow = ControlFlow::Exit;
}
_ => {}
}
});
})
.unwrap();
}