Skip to content

Commit

Permalink
Update raw-window-handle to 0.6, core-graphics to 0.23, and cocoa to …
Browse files Browse the repository at this point in the history
…0.25 (#275)

* Update raw-window-handle to 0.6

* Update core-graphics to 0.23, cocoa to 0.25

* Add raw-window-handle features for 0.5 and 0.6

* Fix rustdoc::bare_urls warning on Khronos link

* Fix compilation errors in threads example

This example segfaults on my machine (even before this PR); despite
this, I think having it compile is valuable.

* Update threads example to winit 0.29

* Add rwh 0.6 support to threads example

* Documentation fixes for consistency and correctness

* Update CI configuration for new features

* Fix Unix build

* Fix Android build

* Fix MacOS build

* Minor adjustments to documentation

---------

Co-authored-by: Martin Robinson <mrobinson@igalia.com>
  • Loading branch information
Nopey and mrobinson authored Mar 15, 2024
1 parent 5624f6b commit e42ca0d
Show file tree
Hide file tree
Showing 15 changed files with 460 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions surfman/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ 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"]
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"
Expand All @@ -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"
Expand Down Expand Up @@ -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" }
68 changes: 53 additions & 15 deletions surfman/examples/threads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<i32>) -> 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<i32>) -> 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);

Expand All @@ -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();

Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 20 additions & 6 deletions surfman/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ pub trait Connection: Sized {
native_device: Self::NativeDevice,
) -> Result<Self::Device, Error>;

/// 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<Self, Error>;

/// 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<Self, Error>;

/// Creates a native widget from a raw pointer
Expand All @@ -67,11 +73,19 @@ pub trait Connection: Sized {
size: Size2D<i32>,
) -> 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<i32>,
) -> Result<Self::NativeWidget, Error>;

/// 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<i32>,
) -> Result<Self::NativeWidget, Error>;
}
2 changes: 1 addition & 1 deletion surfman/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: <https://www.khronos.org/registry/webgl/specs/latest/1.0/#WEBGLCONTEXTATTRIBUTES>
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct ContextAttributes {
/// The OpenGL or OpenGL ES version that this context supports.
Expand Down
26 changes: 22 additions & 4 deletions surfman/src/implementation/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, Error> {
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, Error> {
Connection::from_display_handle(handle)
}

#[inline]
unsafe fn create_native_widget_from_ptr(
&self,
Expand All @@ -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<i32>,
) -> Result<NativeWidget, Error> {
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<i32>,
) -> Result<NativeWidget, Error> {
Connection::create_native_widget_from_window_handle(self, window, size)
}
}
38 changes: 32 additions & 6 deletions surfman/src/platform/android/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Connection, Error> {
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<Connection, Error> {
Ok(Connection)
}
Expand All @@ -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<i32>,
) -> Result<NativeWidget, Error> {
use raw_window_handle::RawWindowHandle::AndroidNdk;
use rwh_05::RawWindowHandle::AndroidNdk;

match raw_handle {
AndroidNdk(handle) => Ok(NativeWidget {
Expand All @@ -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<i32>,
) -> Result<NativeWidget, Error> {
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 {
Expand Down
Loading

0 comments on commit e42ca0d

Please sign in to comment.