From 92a5ab0cc907ab3e1095ec6a09356aff8d93ade8 Mon Sep 17 00:00:00 2001 From: Declan Tsien Date: Wed, 13 Mar 2024 09:25:19 +0800 Subject: [PATCH] Update `raw-window-handle` to v0.6 --- CHANGELOG.md | 1 + glutin-winit/Cargo.toml | 4 +-- glutin-winit/src/lib.rs | 13 ++++--- glutin-winit/src/window.rs | 7 ++-- glutin/Cargo.toml | 2 +- glutin/src/api/cgl/surface.rs | 36 +++++++++---------- glutin/src/api/egl/config.rs | 8 +++-- glutin/src/api/egl/display.rs | 33 ++++++++++------- glutin/src/api/egl/surface.rs | 32 ++++------------- glutin/src/api/glx/display.rs | 4 +-- glutin/src/api/wgl/config.rs | 4 +-- glutin/src/api/wgl/context.rs | 2 +- glutin/src/api/wgl/display.rs | 9 +++-- glutin/src/api/wgl/surface.rs | 6 +--- glutin_examples/Cargo.toml | 4 +-- .../examples/switch_render_thread.rs | 6 ++-- glutin_examples/src/lib.rs | 6 ++-- 17 files changed, 88 insertions(+), 89 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf4277e3b5..cc91771b3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ # Unreleased +- Updated raw-window-handle dependency to 0.6. See [raw-window-handle CHANGELOG](https://github.com/rust-windowing/raw-window-handle/blob/v0.6.0/CHANGELOG.md#060-2023-09-30) for more info. # Version 0.31.3 diff --git a/glutin-winit/Cargo.toml b/glutin-winit/Cargo.toml index a633df2426..4b87d40a0a 100644 --- a/glutin-winit/Cargo.toml +++ b/glutin-winit/Cargo.toml @@ -19,8 +19,8 @@ wayland = ["glutin/wayland", "winit/wayland"] [dependencies] glutin = { version = "0.31.0", path = "../glutin", default-features = false } -raw-window-handle = "0.5.2" -winit = { version = "0.29.2", default-features = false, features = ["rwh_05"] } +raw-window-handle = "0.6" +winit = { version = "0.29.2", default-features = false, features = ["rwh_06"] } [build-dependencies] cfg_aliases = "0.1.1" diff --git a/glutin-winit/src/lib.rs b/glutin-winit/src/lib.rs index cb3b2cd8f9..391be42f67 100644 --- a/glutin-winit/src/lib.rs +++ b/glutin-winit/src/lib.rs @@ -21,9 +21,9 @@ use glutin::platform::x11::X11GlConfigExt; use glutin::prelude::*; #[cfg(wgl_backend)] -use raw_window_handle::HasRawWindowHandle; +use raw_window_handle::HasWindowHandle; -use raw_window_handle::{HasRawDisplayHandle, RawWindowHandle}; +use raw_window_handle::{HasDisplayHandle, RawWindowHandle}; use winit::error::OsError; use winit::event_loop::EventLoopWindowTarget; use winit::window::{Window, WindowBuilder}; @@ -104,7 +104,9 @@ impl DisplayBuilder { }; #[cfg(wgl_backend)] - let raw_window_handle = window.as_ref().map(|window| window.raw_window_handle()); + let raw_window_handle = window + .as_ref() + .and_then(|window| window.window_handle().map(|handle| handle.as_raw()).ok()); #[cfg(not(wgl_backend))] let raw_window_handle = None; @@ -170,7 +172,10 @@ fn create_display( ApiPreference::FallbackEgl => DisplayApiPreference::WglThenEgl(_raw_window_handle), }; - unsafe { Ok(Display::new(window_target.raw_display_handle(), _preference)?) } + match window_target.display_handle() { + Ok(handle) => unsafe { Ok(Display::new(handle.as_raw(), _preference)?) }, + Err(err) => panic!("Error {err:?}"), + } } /// Finalize [`Window`] creation by applying the options from the [`Config`], be diff --git a/glutin-winit/src/window.rs b/glutin-winit/src/window.rs index 09d1717f01..f0fbbafef5 100644 --- a/glutin-winit/src/window.rs +++ b/glutin-winit/src/window.rs @@ -5,7 +5,7 @@ use glutin::surface::{ GlSurface, ResizeableSurface, Surface, SurfaceAttributes, SurfaceAttributesBuilder, SurfaceTypeTrait, WindowSurface, }; -use raw_window_handle::HasRawWindowHandle; +use raw_window_handle::HasWindowHandle; use winit::window::Window; /// [`Window`] extensions for working with [`glutin`] surfaces. @@ -53,7 +53,10 @@ impl GlWindow for Window { builder: SurfaceAttributesBuilder, ) -> SurfaceAttributes { let (w, h) = self.inner_size().non_zero().expect("invalid zero inner size"); - builder.build(self.raw_window_handle(), w, h) + match self.window_handle() { + Ok(handle) => builder.build(handle.as_raw(), w, h), + Err(err) => panic!("Error {err:?}"), + } } fn resize_surface( diff --git a/glutin/Cargo.toml b/glutin/Cargo.toml index 6be4f390ee..478fb5612e 100644 --- a/glutin/Cargo.toml +++ b/glutin/Cargo.toml @@ -23,7 +23,7 @@ wayland = ["wayland-sys", "egl"] bitflags = "2.2.1" libloading = { version = "0.8.0", optional = true } once_cell = "1.13" -raw-window-handle = "0.5.2" +raw-window-handle = "0.6" [target.'cfg(windows)'.dependencies] glutin_egl_sys = { version = "0.6.0", path = "../glutin_egl_sys", optional = true } diff --git a/glutin/src/api/cgl/surface.rs b/glutin/src/api/cgl/surface.rs index 7dcebb63f1..d7cc60eaf0 100644 --- a/glutin/src/api/cgl/surface.rs +++ b/glutin/src/api/cgl/surface.rs @@ -4,7 +4,7 @@ use std::fmt; use std::marker::PhantomData; use std::num::NonZeroU32; -use icrate::AppKit::{NSView, NSWindow}; +use icrate::AppKit::NSView; use icrate::Foundation::{MainThreadBound, MainThreadMarker}; use objc2::rc::Id; use raw_window_handle::RawWindowHandle; @@ -59,28 +59,19 @@ impl Display { // SAFETY: Validity of the view and window is ensured by caller // This function makes sure the window is non null. - let ns_view = if let Some(ns_view) = unsafe { Id::retain(native_window.ns_view.cast()) } { + let ns_view = if let Some(ns_view) = + unsafe { Id::retain(native_window.ns_view.as_ptr().cast()) } + { ns_view } else { return Err(ErrorKind::NotSupported("ns_view of provided native window is nil").into()); }; let ns_view = MainThreadBound::new(ns_view, mtm); - let ns_window = - if let Some(ns_window) = unsafe { Id::retain(native_window.ns_window.cast()) } { - ns_window - } else { - return Err( - ErrorKind::NotSupported("ns_window of provided native window is nil").into() - ); - }; - let ns_window = MainThreadBound::new(ns_window, mtm); - let surface = Surface { display: self.clone(), config: config.clone(), ns_view, - ns_window, _nosync: PhantomData, _ty: PhantomData, }; @@ -93,7 +84,6 @@ pub struct Surface { display: Display, config: Config, pub(crate) ns_view: MainThreadBound>, - ns_window: MainThreadBound>, _nosync: PhantomData<*const std::ffi::c_void>, _ty: PhantomData, } @@ -110,21 +100,27 @@ impl GlSurface for Surface { } fn width(&self) -> Option { - let window = &self.ns_window; let view = &self.ns_view; MainThreadMarker::run_on_main(|mtm| unsafe { - let scale_factor = window.get(mtm).backingScaleFactor(); - let frame = view.get(mtm).frame(); + let view = view.get(mtm); + let scale_factor = match view.window() { + Some(window) => window.backingScaleFactor(), + None => 1.0, + }; + let frame = view.frame(); Some((frame.size.width * scale_factor) as u32) }) } fn height(&self) -> Option { - let window = &self.ns_window; let view = &self.ns_view; MainThreadMarker::run_on_main(|mtm| unsafe { - let scale_factor = window.get(mtm).backingScaleFactor(); - let frame = view.get(mtm).frame(); + let view = view.get(mtm); + let scale_factor = match view.window() { + Some(window) => window.backingScaleFactor(), + None => 1.0, + }; + let frame = view.frame(); Some((frame.size.height * scale_factor) as u32) }) } diff --git a/glutin/src/api/egl/config.rs b/glutin/src/api/egl/config.rs index e400736cc8..fbb3a5e90d 100644 --- a/glutin/src/api/egl/config.rs +++ b/glutin/src/api/egl/config.rs @@ -196,8 +196,10 @@ impl Display { // XXX This can't be done by passing visual in the EGL attributes // when calling `eglChooseConfig` since the visual is ignored. match template.native_window { - Some(RawWindowHandle::Xcb(xcb)) if xcb.visual_id > 0 => { - xcb.visual_id as u32 == config.native_visual() + Some(RawWindowHandle::Xcb(xcb)) + if xcb.visual_id.is_some() && xcb.visual_id.unwrap().get() > 0 => + { + xcb.visual_id.unwrap().get() as u32 == config.native_visual() }, Some(RawWindowHandle::Xlib(xlib)) if xlib.visual_id > 0 => { xlib.visual_id as u32 == config.native_visual() @@ -386,7 +388,7 @@ impl X11GlConfigExt for Config { match *self.inner.display.inner._native_display? { raw_window_handle::RawDisplayHandle::Xlib(display_handle) => unsafe { let xid = self.native_visual(); - X11VisualInfo::from_xid(display_handle.display as *mut _, xid as _) + X11VisualInfo::from_xid(display_handle.display?.as_ptr() as *mut _, xid as _) }, _ => None, } diff --git a/glutin/src/api/egl/display.rs b/glutin/src/api/egl/display.rs index ea64ba66b2..2d2b9f3dcc 100644 --- a/glutin/src/api/egl/display.rs +++ b/glutin/src/api/egl/display.rs @@ -244,16 +244,18 @@ impl Display { RawDisplayHandle::Wayland(handle) if extensions.contains("EGL_KHR_platform_wayland") => { - (egl::PLATFORM_WAYLAND_KHR, handle.display) + (egl::PLATFORM_WAYLAND_KHR, handle.display.as_ptr()) }, #[cfg(x11_platform)] - RawDisplayHandle::Xlib(handle) if extensions.contains("EGL_KHR_platform_x11") => { + RawDisplayHandle::Xlib(handle) + if extensions.contains("EGL_KHR_platform_x11") && handle.display.is_some() => + { attrs.push(egl::PLATFORM_X11_SCREEN_KHR as EGLAttrib); attrs.push(handle.screen as EGLAttrib); - (egl::PLATFORM_X11_KHR, handle.display) + (egl::PLATFORM_X11_KHR, handle.display.unwrap().as_ptr()) }, RawDisplayHandle::Gbm(handle) if extensions.contains("EGL_KHR_platform_gbm") => { - (egl::PLATFORM_GBM_KHR, handle.gbm_device) + (egl::PLATFORM_GBM_KHR, handle.gbm_device.as_ptr()) }, RawDisplayHandle::Android(_) if extensions.contains("EGL_KHR_platform_android") => { (egl::PLATFORM_ANDROID_KHR, egl::DEFAULT_DISPLAY as *mut _) @@ -320,25 +322,28 @@ impl Display { RawDisplayHandle::Wayland(handle) if extensions.contains("EGL_EXT_platform_wayland") => { - (egl::PLATFORM_WAYLAND_EXT, handle.display) + (egl::PLATFORM_WAYLAND_EXT, handle.display.as_ptr()) }, #[cfg(x11_platform)] - RawDisplayHandle::Xlib(handle) if extensions.contains("EGL_EXT_platform_x11") => { + RawDisplayHandle::Xlib(handle) + if extensions.contains("EGL_EXT_platform_x11") && handle.display.is_some() => + { attrs.push(egl::PLATFORM_X11_SCREEN_EXT as EGLint); attrs.push(handle.screen as EGLint); - (egl::PLATFORM_X11_EXT, handle.display) + (egl::PLATFORM_X11_EXT, handle.display.unwrap().as_ptr()) }, #[cfg(x11_platform)] RawDisplayHandle::Xcb(handle) - if extensions.contains("EGL_MESA_platform_xcb") - || extensions.contains("EGL_EXT_platform_xcb") => + if (extensions.contains("EGL_MESA_platform_xcb") + || extensions.contains("EGL_EXT_platform_xcb")) + && handle.connection.is_some() => { attrs.push(egl::PLATFORM_XCB_SCREEN_EXT as EGLint); attrs.push(handle.screen as EGLint); - (egl::PLATFORM_XCB_EXT, handle.connection) + (egl::PLATFORM_XCB_EXT, handle.connection.unwrap().as_ptr()) }, RawDisplayHandle::Gbm(handle) if extensions.contains("EGL_MESA_platform_gbm") => { - (egl::PLATFORM_GBM_MESA, handle.gbm_device) + (egl::PLATFORM_GBM_MESA, handle.gbm_device.as_ptr()) }, RawDisplayHandle::Windows(..) if extensions.contains("EGL_ANGLE_platform_angle") => { // Only CreateWindowSurface appears to work with Angle. @@ -405,9 +410,11 @@ impl Display { fn get_display(egl: &Egl, display: RawDisplayHandle) -> Result { let mut display = match display { - RawDisplayHandle::Gbm(handle) => handle.gbm_device, + RawDisplayHandle::Gbm(handle) => handle.gbm_device.as_ptr(), #[cfg(x11_platform)] - RawDisplayHandle::Xlib(handle) => handle.display, + RawDisplayHandle::Xlib(handle) if handle.display.is_some() => { + handle.display.unwrap().as_ptr() + }, RawDisplayHandle::Android(_) => egl::DEFAULT_DISPLAY as *mut _, _ => { return Err( diff --git a/glutin/src/api/egl/surface.rs b/glutin/src/api/egl/surface.rs index 7a13d69c4c..791f91bf57 100644 --- a/glutin/src/api/egl/surface.rs +++ b/glutin/src/api/egl/surface.rs @@ -472,14 +472,10 @@ impl NativeWindow { let native_window = match raw_window_handle { #[cfg(wayland_platform)] RawWindowHandle::Wayland(window_handle) => unsafe { - if window_handle.surface.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - let ptr = ffi_dispatch!( wayland_egl_handle(), wl_egl_window_create, - window_handle.surface.cast(), + window_handle.surface.as_ptr().cast(), _width.get() as _, _height.get() as _ ); @@ -498,36 +494,20 @@ impl NativeWindow { }, #[cfg(x11_platform)] RawWindowHandle::Xcb(window_handle) => { - if window_handle.window == 0 { + if window_handle.window.get() == 0 { return Err(ErrorKind::BadNativeWindow.into()); } - Self::Xcb(window_handle.window as _) + Self::Xcb(window_handle.window.get() as _) }, #[cfg(android_platform)] RawWindowHandle::AndroidNdk(window_handle) => { - if window_handle.a_native_window.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Android(window_handle.a_native_window) + Self::Android(window_handle.a_native_window.as_ptr()) }, #[cfg(windows)] - RawWindowHandle::Win32(window_handle) => { - if window_handle.hwnd.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Win32(window_handle.hwnd as _) - }, + RawWindowHandle::Win32(window_handle) => Self::Win32(window_handle.hwnd.get() as _), #[cfg(free_unix)] - RawWindowHandle::Gbm(window_handle) => { - if window_handle.gbm_surface.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - - Self::Gbm(window_handle.gbm_surface) - }, + RawWindowHandle::Gbm(window_handle) => Self::Gbm(window_handle.gbm_surface.as_ptr()), _ => { return Err( ErrorKind::NotSupported("provided native window is not supported").into() diff --git a/glutin/src/api/glx/display.rs b/glutin/src/api/glx/display.rs index 1cdf96d191..11e8e999cc 100644 --- a/glutin/src/api/glx/display.rs +++ b/glutin/src/api/glx/display.rs @@ -45,11 +45,11 @@ impl Display { // Don't load GLX when unsupported platform was requested. let (display, screen) = match display { RawDisplayHandle::Xlib(handle) => { - if handle.display.is_null() { + if handle.display.is_none() { return Err(ErrorKind::BadDisplay.into()); } - (GlxDisplay(handle.display as *mut _), handle.screen as i32) + (GlxDisplay(handle.display.unwrap().as_ptr() as *mut _), handle.screen as i32) }, _ => { return Err( diff --git a/glutin/src/api/wgl/config.rs b/glutin/src/api/wgl/config.rs index ad5fabbe8d..4f6a368b29 100644 --- a/glutin/src/api/wgl/config.rs +++ b/glutin/src/api/wgl/config.rs @@ -33,7 +33,7 @@ impl Display { template: ConfigTemplate, ) -> Result + '_>> { let hwnd = match template.native_window { - Some(RawWindowHandle::Win32(window_handle)) => window_handle.hwnd as _, + Some(RawWindowHandle::Win32(window_handle)) => window_handle.hwnd.get() as _, _ => 0, }; let hdc = unsafe { gdi::GetDC(hwnd) }; @@ -289,7 +289,7 @@ impl Config { /// The `raw_window_handle` should point to a valid value. pub unsafe fn apply_on_native_window(&self, raw_window_handle: &RawWindowHandle) -> Result<()> { let hdc = match raw_window_handle { - RawWindowHandle::Win32(window) => unsafe { gdi::GetDC(window.hwnd as _) }, + RawWindowHandle::Win32(window) => unsafe { gdi::GetDC(window.hwnd.get() as _) }, _ => return Err(ErrorKind::BadNativeWindow.into()), }; diff --git a/glutin/src/api/wgl/context.rs b/glutin/src/api/wgl/context.rs index 4ba8fd42eb..fe5278456e 100644 --- a/glutin/src/api/wgl/context.rs +++ b/glutin/src/api/wgl/context.rs @@ -35,7 +35,7 @@ impl Display { let hdc = match context_attributes.raw_window_handle.as_ref() { handle @ Some(RawWindowHandle::Win32(window)) => unsafe { let _ = config.apply_on_native_window(handle.unwrap()); - gdi::GetDC(window.hwnd as _) + gdi::GetDC(window.hwnd.get() as _) }, _ => config.inner.hdc, }; diff --git a/glutin/src/api/wgl/display.rs b/glutin/src/api/wgl/display.rs index 62c1309bf5..5133ea8526 100644 --- a/glutin/src/api/wgl/display.rs +++ b/glutin/src/api/wgl/display.rs @@ -59,9 +59,14 @@ impl Display { // In case native window was provided init extra functions. let (wgl_extra, client_extensions) = if let Some(RawWindowHandle::Win32(window)) = native_window { + if window.hinstance.is_none() { + return Err(ErrorKind::BadDisplay.into()); + } unsafe { - let (wgl_extra, client_extensions) = - super::load_extra_functions(window.hinstance as _, window.hwnd as _)?; + let (wgl_extra, client_extensions) = super::load_extra_functions( + window.hinstance.unwrap().get() as _, + window.hwnd.get() as _, + )?; (Some(wgl_extra), client_extensions) } } else { diff --git a/glutin/src/api/wgl/surface.rs b/glutin/src/api/wgl/surface.rs index 77cdbc16c6..b47ccafa39 100644 --- a/glutin/src/api/wgl/surface.rs +++ b/glutin/src/api/wgl/surface.rs @@ -49,12 +49,8 @@ impl Display { ) -> Result> { let hwnd = match surface_attributes.raw_window_handle.as_ref().unwrap() { handle @ RawWindowHandle::Win32(window_handle) => { - if window_handle.hwnd.is_null() { - return Err(ErrorKind::BadNativeWindow.into()); - } - let _ = unsafe { config.apply_on_native_window(handle) }; - window_handle.hwnd as HWND + window_handle.hwnd.get() as HWND }, _ => { return Err( diff --git a/glutin_examples/Cargo.toml b/glutin_examples/Cargo.toml index 2da256a749..d67da46ab3 100644 --- a/glutin_examples/Cargo.toml +++ b/glutin_examples/Cargo.toml @@ -22,11 +22,11 @@ wayland = ["glutin-winit/wayland", "winit/wayland-dlopen", "winit/wayland-csd-ad glutin = { path = "../glutin", default-features = false } glutin-winit = { path = "../glutin-winit", default-features = false } png = { version = "0.17.6", optional = true } -raw-window-handle = "0.5" +raw-window-handle = "0.6" winit = { version = "0.29.2", default-features = false, features = ["rwh_05"] } [target.'cfg(target_os = "android")'.dependencies] -winit = { version = "0.29.2", default-features = false, features = ["android-native-activity", "rwh_05"] } +winit = { version = "0.29.2", default-features = false, features = ["android-native-activity", "rwh_06"] } [build-dependencies] gl_generator = "0.14" diff --git a/glutin_examples/examples/switch_render_thread.rs b/glutin_examples/examples/switch_render_thread.rs index 672f04f369..132d1dc046 100644 --- a/glutin_examples/examples/switch_render_thread.rs +++ b/glutin_examples/examples/switch_render_thread.rs @@ -13,7 +13,7 @@ use glutin::surface::{GlSurface, Surface, WindowSurface}; use glutin_examples::gl::types::GLfloat; use glutin_examples::{gl_config_picker, Renderer}; use glutin_winit::{self, DisplayBuilder, GlWindow}; -use raw_window_handle::HasRawWindowHandle; +use raw_window_handle::HasWindowHandle; use winit::dpi::PhysicalSize; use winit::event::{ElementState, Event, WindowEvent}; use winit::event_loop::{EventLoopBuilder, EventLoopProxy}; @@ -177,7 +177,9 @@ fn create_window_with_render_context( println!("Picked a config with {} samples", gl_config.num_samples()); - let raw_window_handle = window.as_ref().map(|window| window.raw_window_handle()); + let raw_window_handle = window + .as_ref() + .and_then(|window| window.window_handle().map(|handle| handle.as_raw()).ok()); let window = window.take().unwrap(); diff --git a/glutin_examples/src/lib.rs b/glutin_examples/src/lib.rs index 1c1c0bd7e0..767786c754 100644 --- a/glutin_examples/src/lib.rs +++ b/glutin_examples/src/lib.rs @@ -4,7 +4,7 @@ use std::num::NonZeroU32; use std::ops::Deref; use gl::types::GLfloat; -use raw_window_handle::HasRawWindowHandle; +use raw_window_handle::HasWindowHandle; use winit::event::{Event, KeyEvent, WindowEvent}; use winit::keyboard::{Key, NamedKey}; use winit::window::WindowBuilder; @@ -53,7 +53,9 @@ pub fn main(event_loop: winit::event_loop::EventLoop<()>) -> Result<(), Box