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 Jan 26, 2022
1 parent 78c3d4c commit 31a0924
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion sixtyfps_runtime/corelib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pin-project = "1"
atomic-polyfill = { version = "0.1.5" }
num-traits = { version = "0.2", default-features = false }
sixtyfps-common = { version = "=0.2.0", path = "../common" }

raw-window-handle = { version = "0.4.2" }
defmt = { version = "0.3.0", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
Expand Down
9 changes: 8 additions & 1 deletion sixtyfps_runtime/corelib/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use core::pin::Pin;
/// This trait represents the interface that the generated code and the run-time
/// require in order to implement functionality such as device-independent pixels,
/// window resizing and other typically windowing system related tasks.
pub trait PlatformWindow {
pub trait PlatformWindow: raw_window_handle::HasRawWindowHandle {
/// Registers the window with the windowing system.
fn show(self: Rc<Self>);
/// De-registers the window from the windowing system.
Expand Down Expand Up @@ -591,6 +591,13 @@ pub mod api {
self.0.hide();
}
}

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()
}
}
}

impl WindowHandleAccess for api::Window {
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_runtime/rendering_backends/gl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ ttf-parser = "0.14.0"
unicode-script = "0.5.3"
cfg-if = "1"
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
10 changes: 10 additions & 0 deletions sixtyfps_runtime/rendering_backends/gl/glwindow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,16 @@ impl PlatformWindow for GLWindow {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for GLWindow {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
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())
}
}
}

struct MappedWindow {
canvas: Option<CanvasRc>,
opengl_context: crate::OpenGLContext,
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_runtime/rendering_backends/qt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ euclid = "0.22.1"
pin-weak = "1"
once_cell = "1"
lyon_path = "0.17"
raw-window-handle = { version = "0.4.2" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
qttypes = { version = "0.2.5", default-features = false }
Expand Down
6 changes: 6 additions & 0 deletions sixtyfps_runtime/rendering_backends/qt/qt_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,12 @@ impl PlatformWindow for QtWindow {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for QtWindow {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
unimplemented!()
}
}

fn get_font(request: FontRequest) -> QFont {
let family: qttypes::QString = request.family.unwrap_or_default().as_str().into();
let pixel_size: f32 = request.pixel_size.unwrap_or(0.);
Expand Down
1 change: 1 addition & 0 deletions sixtyfps_runtime/rendering_backends/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ path = "lib.rs"
[dependencies]
sixtyfps-corelib = { version = "=0.2.0", path = "../../corelib" }
image = { version = "0.23.14", default-features = false, features = ["png"] }
raw-window-handle = { version = "0.4.2" }
6 changes: 6 additions & 0 deletions sixtyfps_runtime/rendering_backends/testing/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@ impl PlatformWindow for TestingWindow {
}
}

unsafe impl raw_window_handle::HasRawWindowHandle for TestingWindow {
fn raw_window_handle(&self) -> raw_window_handle::RawWindowHandle {
unimplemented!()
}
}

/// Initialize the testing backend.
/// Must be called before any call that would otherwise initialize the rendering backend.
/// Calling it when the rendering backend is already initialized will have no effects
Expand Down

0 comments on commit 31a0924

Please sign in to comment.