diff --git a/Cargo.toml b/Cargo.toml index 6920d059..549fa556 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -88,13 +88,13 @@ smol_str = "0.2.0" tracing = { version = "0.1.40", default_features = false } [dev-dependencies] -image = { version = "0.24.0", default-features = false, features = ["png"] } +image = { version = "0.25.0", default-features = false, features = ["png"] } tracing = { version = "0.1.40", default_features = false, features = ["log"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } winit = { path = ".", features = ["rwh_05"] } [target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dev-dependencies] -softbuffer = { version = "0.3.0", default-features = false, features = [ +softbuffer = { version = "0.4.0", default-features = false, features = [ "x11", "x11-dlopen", "wayland", diff --git a/examples/util/fill.rs b/examples/util/fill.rs index 4fcf67f4..5b5d9b46 100644 --- a/examples/util/fill.rs +++ b/examples/util/fill.rs @@ -15,6 +15,7 @@ pub use platform::fill_window; mod platform { use std::cell::RefCell; use std::collections::HashMap; + use std::mem; use std::mem::ManuallyDrop; use std::num::NonZeroU32; @@ -34,24 +35,32 @@ mod platform { /// The graphics context used to draw to a window. struct GraphicsContext { /// The global softbuffer context. - context: Context, + context: RefCell>, /// The hash map of window IDs to surfaces. - surfaces: HashMap, + surfaces: HashMap>, } impl GraphicsContext { fn new(w: &Window) -> Self { Self { - context: unsafe { Context::new(w) }.expect("Failed to create a softbuffer context"), + context: RefCell::new( + Context::new(unsafe { mem::transmute::<&'_ Window, &'static Window>(w) }) + .expect("Failed to create a softbuffer context"), + ), surfaces: HashMap::new(), } } - fn create_surface(&mut self, window: &Window) -> &mut Surface { + fn create_surface( + &mut self, + window: &Window, + ) -> &mut Surface<&'static Window, &'static Window> { self.surfaces.entry(window.id()).or_insert_with(|| { - unsafe { Surface::new(&self.context, window) } - .expect("Failed to create a softbuffer surface") + Surface::new(&self.context.borrow(), unsafe { + mem::transmute::<&'_ Window, &'static Window>(window) + }) + .expect("Failed to create a softbuffer surface") }) } diff --git a/examples/window.rs b/examples/window.rs index 82f480ee..437d90ec 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -7,11 +7,12 @@ use std::fmt::Debug; use std::mem; #[cfg(not(any(android_platform, ios_platform)))] use std::num::NonZeroU32; +use std::sync::Arc; use ::tracing::{error, info}; use cursor_icon::CursorIcon; #[cfg(not(any(android_platform, ios_platform)))] -use rwh_05::HasRawDisplayHandle; +use rwh_06::{DisplayHandle, HasDisplayHandle}; #[cfg(not(any(android_platform, ios_platform)))] use softbuffer::{Context, Surface}; @@ -83,14 +84,21 @@ struct Application { /// /// With OpenGL it could be EGLDisplay. #[cfg(not(any(android_platform, ios_platform)))] - context: Option, + context: Option>>, } impl Application { fn new(event_loop: &EventLoop) -> Self { // SAFETY: we drop the context right before the event loop is stopped, thus making it safe. #[cfg(not(any(android_platform, ios_platform)))] - let context = Some(unsafe { Context::from_raw(event_loop.raw_display_handle()).unwrap() }); + let context = Some( + Context::new(unsafe { + std::mem::transmute::, DisplayHandle<'static>>( + event_loop.display_handle().unwrap(), + ) + }) + .unwrap(), + ); // You'll have to choose an icon size at your own discretion. On X11, the desired size varies // by WM, and on Windows, you still have to account for screen scaling. Here we use 32px, @@ -494,9 +502,9 @@ struct WindowState { /// /// NOTE: This surface must be dropped before the `Window`. #[cfg(not(any(android_platform, ios_platform)))] - surface: Surface, + surface: Surface, Arc>, /// The actual winit Window. - window: Window, + window: Arc, /// The window theme we're drawing with. theme: Theme, /// Cursor position over the window. @@ -523,10 +531,12 @@ struct WindowState { impl WindowState { fn new(app: &Application, window: Window) -> Result> { + let window = Arc::new(window); + // SAFETY: the surface is dropped before the `window` which provided it with handle, thus // it doesn't outlive it. #[cfg(not(any(android_platform, ios_platform)))] - let surface = unsafe { Surface::new(app.context.as_ref().unwrap(), &window)? }; + let surface = Surface::new(app.context.as_ref().unwrap(), Arc::clone(&window))?; let theme = window.theme().unwrap_or(Theme::Dark); info!("Theme: {theme:?}");