diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8c1fae8e..e8b5fafd 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -19,7 +19,7 @@ jobs: matrix: os: [macos-latest, ubuntu-22.04, windows-latest] rust: [stable] - features: ["", "--features 'chains sm-raw-window-handle'"] + features: ["", "--features 'chains sm-raw-window-handle-06'", "--features 'chains sm-raw-window-handle-05'"] target: ["default"] include: # rust stable diff --git a/surfman/Cargo.toml b/surfman/Cargo.toml index c45e9427..ff76ae1b 100644 --- a/surfman/Cargo.toml +++ b/surfman/Cargo.toml @@ -18,7 +18,7 @@ cfg_aliases = "0.1.0" [features] chains = ["fnv", "sparkle"] -default = ["sm-raw-window-handle"] +default = ["sm-raw-window-handle-06"] sm-angle = [] sm-angle-builtin = ["mozangle"] sm-angle-default = ["sm-angle"] @@ -26,7 +26,9 @@ sm-no-wgl = ["sm-angle-default"] sm-test = [] sm-wayland-default = [] sm-x11 = ["x11"] -sm-raw-window-handle = ["raw-window-handle"] +sm-raw-window-handle-generic = [] +sm-raw-window-handle-05 = ["dep:rwh_05"] +sm-raw-window-handle-06 = ["dep:rwh_06"] [dependencies] bitflags = "1.1" @@ -37,19 +39,21 @@ libc = "0.2" log = "0.4" sparkle = { version = "0.1", optional = true } osmesa-sys = { version = "0.1", optional = true } -raw-window-handle = { version = "0.5", optional = true } +rwh_05 = { package = "raw-window-handle", version = "0.5.2", features = ["std"], optional = true } +rwh_06 = { package = "raw-window-handle", version = "0.6", features = ["std"], optional = true } [dev-dependencies] clap = "2" gl = "0.14" png = "0.17" rand = "0.8" +winit = "0.29" [target.'cfg(target_os = "macos")'.dependencies] cgl = "0.3.2" -cocoa = "0.24" +cocoa = "0.25" core-foundation = "0.9" -core-graphics = "0.22" +core-graphics = "0.23" servo-display-link = "0.2" io-surface = "0.15" mach2 = "0.4" @@ -83,4 +87,4 @@ winapi = { version = "0.3", features = [ ] } [target.'cfg(target_os = "android")'.dependencies] -raw-window-handle = "0.5" +rwh_06 = { package = "raw-window-handle", version = "0.6" } diff --git a/surfman/examples/threads.rs b/surfman/examples/threads.rs index 7482f937..e56816a8 100644 --- a/surfman/examples/threads.rs +++ b/surfman/examples/threads.rs @@ -19,11 +19,15 @@ use surfman::{ContextAttributeFlags, ContextAttributes, GLVersion}; #[cfg(not(target_os = "android"))] use winit::{ dpi::PhysicalSize, - event::{DeviceEvent, Event, KeyboardInput, VirtualKeyCode, WindowEvent}, + event::{DeviceEvent, Event, WindowEvent}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder }; -use winit::raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle}; + +#[cfg(feature = "sm-raw-window-handle-05")] +use rwh_05::{HasRawDisplayHandle, HasRawWindowHandle}; +#[cfg(not(feature = "sm-raw-window-handle-05"))] +use rwh_06::{HasDisplayHandle, HasWindowHandle}; pub mod common; @@ -86,9 +90,47 @@ static BACKGROUND_COLOR: [f32; 4] = [ 1.0, ]; +#[cfg(feature = "sm-raw-window-handle-05")] +fn make_connection(window: &winit::window::Window) -> surfman::Connection +{ + let raw_display_handle = window.raw_display_handle(); + let connection = Connection::from_raw_display_handle(raw_display_handle).unwrap(); + connection +} + +#[cfg(not(feature = "sm-raw-window-handle-05"))] +fn make_connection(window: &winit::window::Window) -> surfman::Connection +{ + let display_handle = window.display_handle().expect("failed to get display handle from window"); + let connection = Connection::from_display_handle(display_handle).unwrap(); + connection +} + +#[cfg(feature = "sm-raw-window-handle-05")] +fn make_native_widget(window: &winit::window::Window, connection: &surfman::Connection, window_size: Size2D) -> surfman::NativeWidget +{ + let raw_window_handle = window.raw_window_handle(); + let native_widget = connection + .create_native_widget_from_raw_window_handle(raw_window_handle, window_size) + .unwrap(); + native_widget +} + +#[cfg(not(feature = "sm-raw-window-handle-05"))] +fn make_native_widget(window: &winit::window::Window, connection: &surfman::Connection, window_size: Size2D) -> surfman::NativeWidget +{ + let raw_window_handle = window.window_handle().expect("couldn't get window handle from window"); + let native_widget = connection + .create_native_widget_from_window_handle(raw_window_handle, window_size) + .unwrap(); + native_widget +} + #[cfg(not(target_os = "android"))] fn main() { - let event_loop = EventLoop::new(); + use winit::{event::RawKeyEvent, keyboard::{KeyCode, PhysicalKey}}; + + let event_loop = EventLoop::new().expect("couldn't create eventloop"); let window_size = Size2D::new(WINDOW_WIDTH, WINDOW_HEIGHT); let physical_size = PhysicalSize::new(WINDOW_WIDTH, WINDOW_HEIGHT); @@ -100,15 +142,11 @@ fn main() { window.set_visible(true); - let raw_display_handle = window.raw_display_handle(); - let connection = Connection::from_raw_display_handle(raw_display_handle).unwrap(); + let connection = make_connection(&window); let window_size = window.inner_size(); let window_size = Size2D::new(window_size.width as i32, window_size.height as i32); - let raw_window_handle = window.raw_window_handle(); - let native_widget = connection - .create_native_widget_from_raw_window_handle(raw_window_handle, window_size) - .unwrap(); + let native_widget = make_native_widget(&window, &connection, window_size); let adapter = connection.create_low_power_adapter().unwrap(); let mut device = connection.create_device(&adapter).unwrap(); @@ -139,21 +177,21 @@ fn main() { window_size, ); - event_loop.run(move |event, _, control_flow| match event { + event_loop.run(move |event, target| match event { Event::WindowEvent { event: WindowEvent::CloseRequested, .. } | Event::DeviceEvent { event: - DeviceEvent::Key(KeyboardInput { - virtual_keycode: Some(VirtualKeyCode::Escape), + DeviceEvent::Key(RawKeyEvent { + physical_key: PhysicalKey::Code(KeyCode::Escape), .. }), .. - } => *control_flow = ControlFlow::Exit, - _ => { app.tick(true); *control_flow = ControlFlow::Poll; } - }); + } => target.exit(), + _ => { app.tick(true); target.set_control_flow(ControlFlow::Poll) } + }).expect("failed to run event loop"); } pub struct App { diff --git a/surfman/src/connection.rs b/surfman/src/connection.rs index d9bdfc23..b93e1968 100644 --- a/surfman/src/connection.rs +++ b/surfman/src/connection.rs @@ -54,10 +54,16 @@ pub trait Connection: Sized { native_device: Self::NativeDevice, ) -> Result; - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, + ) -> Result; + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + fn from_display_handle( + handle: rwh_06::DisplayHandle, ) -> Result; /// Creates a native widget from a raw pointer @@ -67,11 +73,19 @@ pub trait Connection: Sized { size: Size2D, ) -> Self::NativeWidget; - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] fn create_native_widget_from_raw_window_handle( &self, - window: raw_window_handle::RawWindowHandle, + window: rwh_05::RawWindowHandle, + size: Size2D, + ) -> Result; + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + fn create_native_widget_from_window_handle( + &self, + window: rwh_06::WindowHandle, size: Size2D, ) -> Result; } diff --git a/surfman/src/context.rs b/surfman/src/context.rs index 01e0b024..db87a6e8 100644 --- a/surfman/src/context.rs +++ b/surfman/src/context.rs @@ -50,7 +50,7 @@ bitflags! { /// Attributes that control aspects of a context and/or surfaces created from that context. /// -/// Similar to: https://www.khronos.org/registry/webgl/specs/latest/1.0/#WEBGLCONTEXTATTRIBUTES +/// Similar to: #[derive(Clone, Copy, PartialEq, Debug)] pub struct ContextAttributes { /// The OpenGL or OpenGL ES version that this context supports. diff --git a/surfman/src/implementation/connection.rs b/surfman/src/implementation/connection.rs index f0d380ce..e6d99851 100644 --- a/surfman/src/implementation/connection.rs +++ b/surfman/src/implementation/connection.rs @@ -71,13 +71,21 @@ impl ConnectionInterface for Connection { } #[inline] - #[cfg(feature = "sm-raw-window-handle")] + #[cfg(feature = "sm-raw-window-handle-05")] fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result { Connection::from_raw_display_handle(raw_handle) } + #[inline] + #[cfg(feature = "sm-raw-window-handle-06")] + fn from_display_handle( + handle: rwh_06::DisplayHandle, + ) -> Result { + Connection::from_display_handle(handle) + } + #[inline] unsafe fn create_native_widget_from_ptr( &self, @@ -88,12 +96,22 @@ impl ConnectionInterface for Connection { } #[inline] - #[cfg(feature = "sm-raw-window-handle")] + #[cfg(feature = "sm-raw-window-handle-05")] fn create_native_widget_from_raw_window_handle( &self, - window: raw_window_handle::RawWindowHandle, + window: rwh_05::RawWindowHandle, size: Size2D, ) -> Result { Connection::create_native_widget_from_raw_window_handle(self, window, size) } + + #[inline] + #[cfg(feature = "sm-raw-window-handle-06")] + fn create_native_widget_from_window_handle( + &self, + window: rwh_06::WindowHandle, + size: Size2D, + ) -> Result { + Connection::create_native_widget_from_window_handle(self, window, size) + } } diff --git a/surfman/src/platform/android/connection.rs b/surfman/src/platform/android/connection.rs index 70acbd4c..d574b1ef 100644 --- a/surfman/src/platform/android/connection.rs +++ b/surfman/src/platform/android/connection.rs @@ -98,9 +98,17 @@ impl Connection { } /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - _: raw_window_handle::RawDisplayHandle, + _: rwh_05::RawDisplayHandle, + ) -> Result { + Ok(Connection) + } + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + _: rwh_06::DisplayHandle, ) -> Result { Ok(Connection) } @@ -116,15 +124,15 @@ impl Connection { } } - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] #[inline] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::AndroidNdk; + use rwh_05::RawWindowHandle::AndroidNdk; match raw_handle { AndroidNdk(handle) => Ok(NativeWidget { @@ -133,6 +141,24 @@ impl Connection { _ => Err(Error::IncompatibleNativeWidget), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + #[inline] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + use rwh_06::RawWindowHandle::AndroidNdk; + + match handle.as_raw() { + AndroidNdk(handle) => Ok(NativeWidget { + native_window: handle.a_native_window.as_ptr() as *mut _, + }), + _ => Err(Error::IncompatibleNativeWidget), + } + } } impl NativeConnection { diff --git a/surfman/src/platform/generic/multi/connection.rs b/surfman/src/platform/generic/multi/connection.rs index bc756cb4..d128dfaf 100644 --- a/surfman/src/platform/generic/multi/connection.rs +++ b/surfman/src/platform/generic/multi/connection.rs @@ -179,10 +179,10 @@ where } } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result, Error> { match ::from_raw_display_handle(raw_handle) { Ok(connection) => Ok(Connection::Default(connection)), @@ -192,6 +192,19 @@ where } } + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + handle: rwh_06::DisplayHandle, + ) -> Result, Error> { + match ::from_display_handle(handle) { + Ok(connection) => Ok(Connection::Default(connection)), + Err(_) => { + ::from_display_handle(handle).map(Connection::Alternate) + } + } + } + /// Create a native widget from a raw pointer pub unsafe fn create_native_widget_from_ptr( &self, @@ -208,11 +221,11 @@ where } } - /// Create a native widget type from the given `raw_window_handle::HasRawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, size: Size2D, ) -> Result, Error> { match *self { @@ -224,6 +237,23 @@ where .map(NativeWidget::Alternate), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + size: Size2D, + ) -> Result, Error> { + match *self { + Connection::Default(ref connection) => connection + .create_native_widget_from_window_handle(handle, size) + .map(NativeWidget::Default), + Connection::Alternate(ref connection) => connection + .create_native_widget_from_window_handle(handle, size) + .map(NativeWidget::Alternate), + } + } } impl ConnectionInterface for Connection @@ -287,13 +317,20 @@ where Connection::create_device_from_native_device(self, native_device) } - #[cfg(feature = "sm-raw-window-handle")] + #[cfg(feature = "sm-raw-window-handle-05")] fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result, Error> { Connection::from_raw_display_handle(raw_handle) } + #[cfg(feature = "sm-raw-window-handle-06")] + fn from_display_handle( + handle: rwh_06::DisplayHandle, + ) -> Result, Error> { + Connection::from_display_handle(handle) + } + #[inline] unsafe fn create_native_widget_from_ptr( &self, @@ -303,12 +340,21 @@ where Connection::create_native_widget_from_ptr(self, raw, size) } - #[cfg(feature = "sm-raw-window-handle")] + #[cfg(feature = "sm-raw-window-handle-05")] fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, size: Size2D, ) -> Result { Connection::create_native_widget_from_raw_window_handle(self, raw_handle, size) } + + #[cfg(feature = "sm-raw-window-handle-06")] + fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + size: Size2D, + ) -> Result { + Connection::create_native_widget_from_window_handle(self, handle, size) + } } diff --git a/surfman/src/platform/macos/cgl/connection.rs b/surfman/src/platform/macos/cgl/connection.rs index 2e2e600b..35b7f236 100644 --- a/surfman/src/platform/macos/cgl/connection.rs +++ b/surfman/src/platform/macos/cgl/connection.rs @@ -16,11 +16,6 @@ use euclid::default::Size2D; use std::os::raw::c_void; -#[cfg(feature = "sm-raw-window-handle")] -use crate::platform::macos::system::surface::NSView; -#[cfg(feature = "sm-raw-window-handle")] -use cocoa::base::id; - pub use crate::platform::macos::system::connection::NativeConnection; /// A connection to the display server. @@ -99,14 +94,20 @@ impl Connection { .map(Device) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result { SystemConnection::from_raw_display_handle(raw_handle).map(Connection) } + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle(handle: rwh_06::DisplayHandle) -> Result { + SystemConnection::from_display_handle(handle).map(Connection) + } + /// Creates a native widget from a raw pointer pub unsafe fn create_native_widget_from_ptr( &self, @@ -116,15 +117,17 @@ impl Connection { self.0.create_native_widget_from_ptr(raw, size) } - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] #[inline] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::AppKit; + use crate::platform::macos::system::surface::NSView; + use cocoa::base::id; + use rwh_05::RawWindowHandle::AppKit; match raw_handle { AppKit(handle) => Ok(NativeWidget { @@ -134,4 +137,33 @@ impl Connection { _ => Err(Error::IncompatibleNativeWidget), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + #[inline] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + use crate::platform::macos::system::surface::NSView; + use cocoa::base::id; + use rwh_06::RawWindowHandle::AppKit; + + match handle.as_raw() { + AppKit(handle) => { + let ns_view = handle.ns_view.as_ptr() as id; + // https://developer.apple.com/documentation/appkit/nsview/1483301-window + let ns_window: id = unsafe{ msg_send![ns_view, window] }; + Ok(NativeWidget { + // Increment the nsview's reference count with retain. See: + // https://developer.apple.com/documentation/objectivec/1418956-nsobject/1571946-retain + view: NSView(unsafe { msg_send![ns_view, retain] }), + // https://developer.apple.com/documentation/appkit/nswindow/1419086-isopaque + opaque: unsafe { msg_send![ns_window, isOpaque] }, + }) + } + _ => Err(Error::IncompatibleNativeWidget), + } + } } diff --git a/surfman/src/platform/macos/system/connection.rs b/surfman/src/platform/macos/system/connection.rs index 00b2bcb2..77ea6ded 100644 --- a/surfman/src/platform/macos/system/connection.rs +++ b/surfman/src/platform/macos/system/connection.rs @@ -118,10 +118,18 @@ impl Connection { self.create_device(&self.create_adapter()?) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - _: raw_window_handle::RawDisplayHandle, + _: rwh_05::RawDisplayHandle, + ) -> Result { + Connection::new() + } + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + _: rwh_06::DisplayHandle, ) -> Result { Connection::new() } @@ -138,15 +146,15 @@ impl Connection { } } - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] #[inline] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::AppKit; + use rwh_05::RawWindowHandle::AppKit; match raw_handle { AppKit(handle) => Ok(NativeWidget { @@ -156,6 +164,33 @@ impl Connection { _ => Err(Error::IncompatibleNativeWidget), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + #[inline] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + use rwh_06::RawWindowHandle::AppKit; + + match handle.as_raw() { + AppKit(handle) => { + let ns_view = handle.ns_view.as_ptr() as id; + // https://developer.apple.com/documentation/appkit/nsview/1483301-window + let ns_window: id = unsafe{ msg_send![ns_view, window] }; + Ok(NativeWidget { + // Increment the nsview's reference count with retain. See: + // https://developer.apple.com/documentation/objectivec/1418956-nsobject/1571946-retain + view: NSView(unsafe { msg_send![ns_view, retain] }), + // https://developer.apple.com/documentation/appkit/nswindow/1419086-isopaque + opaque: unsafe { msg_send![ns_window, isOpaque] }, + }) + } + _ => Err(Error::IncompatibleNativeWidget), + } + } } impl NativeConnection { diff --git a/surfman/src/platform/unix/generic/connection.rs b/surfman/src/platform/unix/generic/connection.rs index 4f822ae9..d5b4521d 100644 --- a/surfman/src/platform/unix/generic/connection.rs +++ b/surfman/src/platform/unix/generic/connection.rs @@ -134,10 +134,18 @@ impl Connection { Device::new(self, &self.create_adapter()?) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - _: raw_window_handle::RawDisplayHandle, + _: rwh_05::RawDisplayHandle, + ) -> Result { + Err(Error::IncompatibleNativeWidget) + } + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + _: rwh_06::DisplayHandle, ) -> Result { Err(Error::IncompatibleNativeWidget) } @@ -151,12 +159,23 @@ impl Connection { NativeWidget } - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] #[inline] pub fn create_native_widget_from_raw_window_handle( &self, - _: raw_window_handle::RawWindowHandle, + _: rwh_05::RawWindowHandle, + _size: Size2D, + ) -> Result { + Err(Error::IncompatibleNativeWidget) + } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + #[inline] + pub fn create_native_widget_from_window_handle( + &self, + _: rwh_06::WindowHandle, _size: Size2D, ) -> Result { Err(Error::IncompatibleNativeWidget) diff --git a/surfman/src/platform/unix/wayland/connection.rs b/surfman/src/platform/unix/wayland/connection.rs index 5fdcbbf8..ca59dda9 100644 --- a/surfman/src/platform/unix/wayland/connection.rs +++ b/surfman/src/platform/unix/wayland/connection.rs @@ -156,13 +156,13 @@ impl Connection { }) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result { - use raw_window_handle::RawDisplayHandle::Wayland; - use raw_window_handle::WaylandDisplayHandle; + use rwh_05::RawDisplayHandle::Wayland; + use rwh_05::WaylandDisplayHandle; unsafe { let wayland_display = match raw_handle { Wayland(WaylandDisplayHandle { display, .. }) => display as *mut wl_display, @@ -173,6 +173,23 @@ impl Connection { } } + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + handle: rwh_06::DisplayHandle, + ) -> Result { + use rwh_06::RawDisplayHandle::Wayland; + use rwh_06::WaylandDisplayHandle; + unsafe { + let wayland_display = match handle.as_raw() { + Wayland(WaylandDisplayHandle { display, .. }) => display.as_ptr() as *mut wl_display, + _ => return Err(Error::IncompatibleRawDisplayHandle), + }; + + Connection::from_wayland_display(wayland_display, false) + } + } + /// Create a native widget from a raw pointer pub unsafe fn create_native_widget_from_ptr( &self, @@ -185,14 +202,14 @@ impl Connection { } } - /// Creates a native widget type from the given `raw_window_handle::HasRawWindowHandle` - #[cfg(feature = "sm-raw-window-handle")] + /// Creates a native widget type from the given `RawWindowHandle` + #[cfg(feature = "sm-raw-window-handle-05")] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, window_size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::Wayland; + use rwh_05::RawWindowHandle::Wayland; let wayland_surface = match raw_handle { Wayland(handle) => handle.surface as *mut wl_proxy, @@ -204,6 +221,26 @@ impl Connection { size: window_size, }) } + + /// Creates a native widget type from the given `WindowHandle` + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + window_size: Size2D, + ) -> Result { + use rwh_06::RawWindowHandle::Wayland; + + let wayland_surface = match handle.as_raw() { + Wayland(handle) => handle.surface.as_ptr() as *mut wl_proxy, + _ => return Err(Error::IncompatibleNativeWidget), + }; + + Ok(NativeWidget { + wayland_surface, + size: window_size, + }) + } } impl Drop for NativeConnectionWrapper { diff --git a/surfman/src/platform/unix/x11/connection.rs b/surfman/src/platform/unix/x11/connection.rs index 09e2b8af..0b684c13 100644 --- a/surfman/src/platform/unix/x11/connection.rs +++ b/surfman/src/platform/unix/x11/connection.rs @@ -187,14 +187,14 @@ impl Connection { Device::new(self, &native_device.adapter) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - raw_handle: raw_window_handle::RawDisplayHandle, + raw_handle: rwh_05::RawDisplayHandle, ) -> Result { - use raw_window_handle::RawDisplayHandle::Xcb; - use raw_window_handle::RawDisplayHandle::Xlib; - use raw_window_handle::XlibDisplayHandle; + use rwh_05::RawDisplayHandle::Xcb; + use rwh_05::RawDisplayHandle::Xlib; + use rwh_05::XlibDisplayHandle; let display = match raw_handle { Xlib(XlibDisplayHandle { display, .. }) => display as *mut Display, Xcb(_) => return Err(Error::Unimplemented), @@ -204,6 +204,23 @@ impl Connection { Connection::from_x11_display(display, false) } + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + handle: rwh_06::DisplayHandle, + ) -> Result { + use rwh_06::RawDisplayHandle::Xcb; + use rwh_06::RawDisplayHandle::Xlib; + use rwh_06::XlibDisplayHandle; + let display = match handle.as_raw() { + Xlib(XlibDisplayHandle { display, .. }) => display as *mut Display, + Xcb(_) => return Err(Error::Unimplemented), + _ => return Err(Error::IncompatibleRawDisplayHandle), + }; + + Connection::from_x11_display(display, false) + } + /// Create a native widget from a raw pointer pub unsafe fn create_native_widget_from_ptr( &self, @@ -215,14 +232,14 @@ impl Connection { } } - /// Create a native widget type from the given `raw_window_handle::HasRawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::Xlib; + use rwh_05::RawWindowHandle::Xlib; match raw_handle { Xlib(handle) => Ok(NativeWidget { @@ -231,6 +248,23 @@ impl Connection { _ => Err(Error::IncompatibleNativeWidget), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + use rwh_06::RawWindowHandle::Xlib; + + match handle.as_raw() { + Xlib(handle) => Ok(NativeWidget { + window: handle.window, + }), + _ => Err(Error::IncompatibleNativeWidget), + } + } } impl NativeConnectionWrapper { diff --git a/surfman/src/platform/windows/angle/connection.rs b/surfman/src/platform/windows/angle/connection.rs index 4dcc820c..be0fa18f 100644 --- a/surfman/src/platform/windows/angle/connection.rs +++ b/surfman/src/platform/windows/angle/connection.rs @@ -128,10 +128,18 @@ impl Connection { Device::from_egl_display(egl_display) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - _: raw_window_handle::RawDisplayHandle, + _: rwh_05::RawDisplayHandle, + ) -> Result { + Connection::new() + } + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + _: rwh_06::DisplayHandle, ) -> Result { Connection::new() } @@ -147,15 +155,15 @@ impl Connection { } } - /// Create a native widget type from the given `raw_window_handle::RawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] #[inline] pub fn create_native_widget_from_raw_window_handle( &self, - handle: raw_window_handle::RawWindowHandle, + handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - if let raw_window_handle::RawWindowHandle::Win32(handle) = handle { + if let rwh_05::RawWindowHandle::Win32(handle) = handle { Ok(NativeWidget { egl_native_window: handle.hwnd as EGLNativeWindowType, }) @@ -163,6 +171,23 @@ impl Connection { Err(Error::IncompatibleNativeWidget) } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + #[inline] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + if let rwh_06::RawWindowHandle::Win32(handle) = handle.as_raw() { + Ok(NativeWidget { + egl_native_window: handle.hwnd.get() as EGLNativeWindowType, + }) + } else { + Err(Error::IncompatibleNativeWidget) + } + } } impl NativeConnection { diff --git a/surfman/src/platform/windows/wgl/connection.rs b/surfman/src/platform/windows/wgl/connection.rs index cfed2de2..eae675dc 100644 --- a/surfman/src/platform/windows/wgl/connection.rs +++ b/surfman/src/platform/windows/wgl/connection.rs @@ -99,10 +99,18 @@ impl Connection { Device::from_native_device(native_device) } - /// Opens the display connection corresponding to the given raw display handle. - #[cfg(feature = "sm-raw-window-handle")] + /// Opens the display connection corresponding to the given `RawDisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn from_raw_display_handle( - _: raw_window_handle::RawDisplayHandle, + _: rwh_05::RawDisplayHandle, + ) -> Result { + Connection::new() + } + + /// Opens the display connection corresponding to the given `DisplayHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn from_display_handle( + _: rwh_06::DisplayHandle, ) -> Result { Connection::new() } @@ -118,14 +126,14 @@ impl Connection { } } - /// Create a native widget type from the given `raw_window_handle::HasRawWindowHandle`. - #[cfg(feature = "sm-raw-window-handle")] + /// Create a native widget type from the given `RawWindowHandle`. + #[cfg(feature = "sm-raw-window-handle-05")] pub fn create_native_widget_from_raw_window_handle( &self, - raw_handle: raw_window_handle::RawWindowHandle, + raw_handle: rwh_05::RawWindowHandle, _size: Size2D, ) -> Result { - use raw_window_handle::RawWindowHandle::Win32; + use rwh_05::RawWindowHandle::Win32; match raw_handle { Win32(handle) => Ok(NativeWidget { @@ -134,6 +142,23 @@ impl Connection { _ => Err(Error::IncompatibleNativeWidget), } } + + /// Create a native widget type from the given `WindowHandle`. + #[cfg(feature = "sm-raw-window-handle-06")] + pub fn create_native_widget_from_window_handle( + &self, + handle: rwh_06::WindowHandle, + _size: Size2D, + ) -> Result { + use rwh_06::RawWindowHandle::Win32; + + match handle.as_raw() { + Win32(handle) => Ok(NativeWidget { + window_handle: handle.hwnd.get() as HWND, + }), + _ => Err(Error::IncompatibleNativeWidget), + } + } } impl NativeConnection {