From 84e95e539f65b2b3d4b7e00a5784d2099dc3429e Mon Sep 17 00:00:00 2001 From: TheArtist Date: Fri, 11 Dec 2020 22:24:42 +0200 Subject: [PATCH] Using a RawWindowHandle with hal::create_surface for multi-backend compatibility --- Cargo.lock | 1 + libportability-gfx/Cargo.toml | 1 + libportability-gfx/src/impls.rs | 22 +++++++++++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index befc3de..3fcd675 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -753,6 +753,7 @@ dependencies = [ "lazy_static 1.4.0", "log", "parking_lot", + "raw-window-handle", "renderdoc", "smallvec", "typed-arena", diff --git a/libportability-gfx/Cargo.toml b/libportability-gfx/Cargo.toml index 1b60b10..8fd7a64 100644 --- a/libportability-gfx/Cargo.toml +++ b/libportability-gfx/Cargo.toml @@ -27,6 +27,7 @@ parking_lot = "0.11" smallvec = "1" renderdoc = { version = "0.3", optional = true } typed-arena = "2" +raw-window-handle = "0.3" [dependencies.hal] package = "gfx-hal" diff --git a/libportability-gfx/src/impls.rs b/libportability-gfx/src/impls.rs index 91f6209..19b8ee9 100644 --- a/libportability-gfx/src/impls.rs +++ b/libportability-gfx/src/impls.rs @@ -4716,17 +4716,33 @@ pub unsafe extern "C" fn gfxCreateXcbSurfaceKHR( ) -> VkResult { assert!(pAllocator.is_null()); let info = &*pCreateInfo; - #[cfg(all(feature = "gfx-backend-vulkan", target_os = "linux"))] + + #[cfg(target_os = "linux")] { assert_eq!(info.flags, 0); + use raw_window_handle::{unix::XcbHandle, HasRawWindowHandle, RawWindowHandle}; + + struct HandleWrapper(XcbHandle); + unsafe impl HasRawWindowHandle for HandleWrapper { + fn raw_window_handle(&self) -> RawWindowHandle { + RawWindowHandle::Xcb(self.0) + } + } + + let xcb_handle = XcbHandle { + window: info.window, + connection: info.connection, + ..XcbHandle::empty() + }; *pSurface = Handle::new( instance .backend - .create_surface_from_xcb(info.connection, info.window), + .create_surface(&HandleWrapper(xcb_handle)) + .unwrap(), ); VkResult::VK_SUCCESS } - #[cfg(not(all(feature = "gfx-backend-vulkan", target_os = "linux")))] + #[cfg(not(target_os = "linux"))] { let _ = (instance, info, pSurface); unreachable!()