Skip to content

Commit

Permalink
WIP: Provide an implementation for the HasRawWindowHandle trait in Ru…
Browse files Browse the repository at this point in the history
…st for sixtyfps::Window

RWH is the de-facto standard crate in Rust to express window handles
(and associated windowing system connections). This should be safe to
use in the public API.

This patch implements RWH for the GL backend.

cc #877
  • Loading branch information
tronical committed Feb 10, 2022
1 parent eb320b0 commit 3479e6c
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions internal/backends/gl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ ttf-parser = "0.14.0" # Use the same version was femtovg's rustybuzz, to avoid d
unicode-script = "0.5.3" # Use the same version was femtovg's rustybuzz, to avoid duplicate crates
usvg = { version= "0.20", optional = true, default-features = false, features = ["text"] }
winit = { version = "0.26", default-features = false }
raw-window-handle = { version = "0.4.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
web_sys = { version = "0.3", package = "web-sys", features=["console", "WebGlContextAttributes"] }
Expand Down
9 changes: 9 additions & 0 deletions internal/backends/gl/glwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,15 @@ impl PlatformWindow for GLWindow {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn raw_window_handle(&self) -> Option<raw_window_handle::RawWindowHandle> {
use raw_window_handle::HasRawWindowHandle;
Some(if let Some(mapped_window) = self.borrow_mapped_window() {
mapped_window.opengl_context.window().raw_window_handle()
} else {
raw_window_handle::RawWindowHandle::Xcb(raw_window_handle::XcbHandle::empty())
})
}
}

impl Drop for GLWindow {
Expand Down
1 change: 1 addition & 0 deletions internal/backends/qt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ euclid = "0.22.1"
lyon_path = "0.17"
once_cell = "1"
pin-weak = "1"
raw-window-handle = { version = "0.4.2" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
qttypes = { version = "0.2.5", default-features = false }
Expand Down
4 changes: 4 additions & 0 deletions internal/backends/qt/qt_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,10 @@ impl PlatformWindow for QtWindow {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn raw_window_handle(&self) -> Option<raw_window_handle::RawWindowHandle> {
None
}
}

fn get_font(request: FontRequest) -> QFont {
Expand Down
3 changes: 2 additions & 1 deletion internal/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ libm = ["num-traits/libm", "euclid/libm"]
# Allow the viewer to query at runtime information about item types
rtti = []
# Use the standard library
std = ["euclid/std", "once_cell/std", "scoped-tls-hkt", "lyon_path", "lyon_algorithms", "lyon_geom", "lyon_svg", "instant"]
std = ["euclid/std", "once_cell/std", "scoped-tls-hkt", "lyon_path", "lyon_algorithms", "lyon_geom", "lyon_svg", "instant", "raw-window-handle"]
# Unsafe feature meaning that there is only one core running and all thread_local are static.
# You can only enable this feature if you are sure that any API of this crate is only called
# from a single core, and not in a interrupt or signal handler.
Expand Down Expand Up @@ -52,6 +52,7 @@ num-traits = { version = "0.2", default-features = false }
once_cell = { version = "1.5", default-features = false }
pin-project = "1"
pin-weak = { version = "1.1", default-features = false }
raw-window-handle = { version = "0.4.2", optional = true }
# Note: the rgb version is extracted in ci.yaml for rustdoc builds
rgb = "0.8.27"
scoped-tls-hkt = { version = "0.1", optional = true }
Expand Down
10 changes: 10 additions & 0 deletions internal/core/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ impl Window {
}
}

#[cfg(feature = "std")]
unsafe impl raw_window_handle::HasRawWindowHandle for Window {
#![allow(unsafe_code)]
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
self.0
.raw_window_handle()
.expect("Current rendering backend does not support RawWindowHandle")
}
}

impl crate::window::WindowHandleAccess for Window {
fn window_handle(&self) -> &Rc<crate::window::Window> {
&self.0
Expand Down
6 changes: 6 additions & 0 deletions internal/core/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ pub trait PlatformWindow {

/// Return self as any so the backend can upcast
fn as_any(&self) -> &dyn core::any::Any;

/// Returns the raw window handle for the given window, if the backend implements that.
#[cfg(feature = "std")]
fn raw_window_handle(&self) -> Option<raw_window_handle::RawWindowHandle> {
None
}
}

struct WindowPropertiesTracker {
Expand Down

0 comments on commit 3479e6c

Please sign in to comment.