Skip to content

Commit fb76260

Browse files
bors[bot]jamsch0
andauthored
Merge #3038
3038: Replace `winit` with `raw-window-handle` in backend crates r=kvark a=antonok-edm Fixes #2956, closes #2967 PR checklist: - [x] `make` succeeds (on *nix) - [x] `make reftests` succeeds - [x] tested examples with the following backends: `vulkan` - [x] `rustfmt` run on changed code I've rebased @jchapman127's work off of the latest `master`. Unfortunately, `winit` has not yet updated past `raw-window-handle` v0.1.2, so #3037 will have to wait. Anything else that needs to be done? Co-authored-by: James Chapman <[email protected]>
2 parents da078cd + fd50602 commit fb76260

File tree

18 files changed

+114
-105
lines changed

18 files changed

+114
-105
lines changed

examples/Cargo.toml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ edition = "2018"
99
default = []
1010
metal = ["gfx-backend-metal"]
1111
gl = ["gfx-backend-gl", "gfx-backend-gl/glutin"]
12-
wgl = ["gfx-backend-gl", "gfx-backend-gl/winit", "gfx-backend-gl/wgl"]
12+
wgl = ["gfx-backend-gl", "gfx-backend-gl/wgl"]
1313
dx11 = ["gfx-backend-dx11"]
1414
dx12 = ["gfx-backend-dx12"]
1515
vulkan = ["gfx-backend-vulkan"]
@@ -57,23 +57,20 @@ features = [ "console", "Document", "Element", "HtmlElement", "Node", "Window" ]
5757
[dependencies.gfx-backend-vulkan]
5858
path = "../src/backend/vulkan"
5959
version = "0.3"
60-
features = ["winit", "x11"]
60+
features = ["x11"]
6161
optional = true
6262

6363
[target.'cfg(any(target_os = "macos", all(target_os = "ios", target_arch = "aarch64")))'.dependencies.gfx-backend-metal]
6464
path = "../src/backend/metal"
6565
version = "0.3"
66-
features = ["winit"]
6766
optional = true
6867

6968
[target.'cfg(windows)'.dependencies.gfx-backend-dx11]
7069
path = "../src/backend/dx11"
7170
version = "0.3"
72-
features = ["winit"]
7371
optional = true
7472

7573
[target.'cfg(windows)'.dependencies.gfx-backend-dx12]
7674
path = "../src/backend/dx12"
7775
version = "0.3"
78-
features = ["winit"]
7976
optional = true

examples/colour-uniform/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ fn create_backend(
568568
let window = wb.build(event_loop).unwrap();
569569
let instance = back::Instance::create("gfx-rs colour-uniform", 1)
570570
.expect("Failed to create an instance!");
571-
let surface = instance.create_surface(&window);
571+
let surface = instance.create_surface(&window).expect("Failed to create a surface!");
572572
let mut adapters = instance.enumerate_adapters();
573573
(
574574
BackendState {

examples/quad/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ fn main() {
119119
let window = wb.build(&event_loop).unwrap();
120120
let instance = back::Instance::create("gfx-rs quad", 1)
121121
.expect("Failed to create an instance!");
122-
let surface = instance.create_surface(&window);
122+
let surface = instance.create_surface(&window).expect("Failed to create a surface!");
123123
let adapters = instance.enumerate_adapters();
124124
(window, instance, adapters, surface)
125125
};

src/backend/dx11/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ smallvec = "0.6"
2727
spirv_cross = { version = "0.16", features = ["hlsl"] }
2828
parking_lot = "0.9"
2929
winapi = { version = "0.3", features = ["basetsd","d3d11", "d3d11sdklayers", "d3dcommon","d3dcompiler","dxgi1_2","dxgi1_3","dxgi1_4", "dxgi1_5", "dxgiformat","dxgitype","handleapi","minwindef","synchapi","unknwnbase","winbase","windef","winerror","winnt","winuser"] }
30-
winit = { version = "0.20.0-alpha3", optional = true }
3130
wio = "0.2"
31+
raw-window-handle = "0.1"
3232

3333
# This forces docs.rs to build the crate on windows, otherwise the build fails
3434
# and we get no docs at all.

src/backend/dx11/src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ extern crate smallvec;
1212
extern crate spirv_cross;
1313
#[macro_use]
1414
extern crate winapi;
15-
#[cfg(feature = "winit")]
16-
extern crate winit;
1715
extern crate wio;
1816

1917
use hal::{
@@ -150,10 +148,16 @@ impl Instance {
150148
}
151149
}
152150

153-
#[cfg(feature = "winit")]
154-
pub fn create_surface(&self, window: &winit::window::Window) -> Surface {
155-
use winit::platform::windows::WindowExtWindows;
156-
self.create_surface_from_hwnd(window.hwnd() as *mut _)
151+
pub fn create_surface(
152+
&self,
153+
has_handle: &impl raw_window_handle::HasRawWindowHandle,
154+
) -> Result<Surface, hal::window::InitError> {
155+
match has_handle.raw_window_handle() {
156+
raw_window_handle::RawWindowHandle::Windows(handle) => {
157+
Ok(self.create_surface_from_hwnd(handle.hwnd))
158+
}
159+
_ => Err(hal::window::InitError::UnsupportedWindowHandle),
160+
}
157161
}
158162
}
159163

src/backend/dx12/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ log = { version = "0.4" }
2727
smallvec = "0.6"
2828
spirv_cross = { version = "0.16", features = ["hlsl"] }
2929
winapi = { version = "0.3", features = ["basetsd","d3d12","d3d12sdklayers","d3d12shader","d3dcommon","d3dcompiler","dxgi1_2","dxgi1_3","dxgi1_4","dxgi1_6","dxgidebug","dxgiformat","dxgitype","handleapi","minwindef","synchapi","unknwnbase","winbase","windef","winerror","winnt","winuser"] }
30-
winit = { version = "0.20.0-alpha3", optional = true }
30+
raw-window-handle = "0.1"
3131

3232
# This forces docs.rs to build the crate on windows, otherwise the build fails
3333
# and we get no docs at all.

src/backend/dx12/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ extern crate log;
99
extern crate smallvec;
1010
extern crate spirv_cross;
1111
extern crate winapi;
12-
#[cfg(feature = "winit")]
13-
extern crate winit;
1412

1513
mod command;
1614
mod conv;

src/backend/dx12/src/window.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
use std::collections::VecDeque;
22
use std::{fmt, mem};
33

4-
#[cfg(feature = "winit")]
5-
use winit;
6-
74
use winapi::shared::{
85
dxgi1_4,
96
windef::{HWND, RECT},
107
winerror,
118
};
129
use winapi::um::winuser::GetClientRect;
1310

14-
use hal::{device::Device as _, format as f, image as i, window as w};
11+
use hal::{self, device::Device as _, format as f, image as i, window as w};
1512
use {conv, native, resource as r, Backend, Device, Instance, PhysicalDevice, QueueFamily};
1613

1714
use std::os::raw::c_void;
@@ -25,10 +22,16 @@ impl Instance {
2522
}
2623
}
2724

28-
#[cfg(feature = "winit")]
29-
pub fn create_surface(&self, window: &winit::window::Window) -> Surface {
30-
use winit::platform::windows::WindowExtWindows;
31-
self.create_surface_from_hwnd(window.hwnd() as *mut _)
25+
pub fn create_surface(
26+
&self,
27+
has_handle: &impl raw_window_handle::HasRawWindowHandle,
28+
) -> Result<Surface, hal::window::InitError> {
29+
match has_handle.raw_window_handle() {
30+
raw_window_handle::RawWindowHandle::Windows(handle) => {
31+
Ok(self.create_surface_from_hwnd(handle.hwnd))
32+
}
33+
_ => Err(hal::window::InitError::UnsupportedWindowHandle),
34+
}
3235
}
3336
}
3437

src/backend/empty/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ name = "gfx_backend_empty"
1313

1414
[dependencies]
1515
gfx-hal = { path = "../../hal", version = "0.3" }
16-
winit = { version = "0.20.0-alpha3", optional = true }
16+
raw-window-handle = "0.1"

src/backend/empty/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//! outside of the graphics development environment.
33
44
extern crate gfx_hal as hal;
5-
#[cfg(feature = "winit")]
6-
extern crate winit;
75

86
use hal::range::RangeArg;
97
use hal::{
@@ -1004,8 +1002,10 @@ impl Instance {
10041002
Ok(Instance)
10051003
}
10061004

1007-
#[cfg(feature = "winit")]
1008-
pub fn create_surface(&self, _: &winit::window::Window) -> Surface {
1005+
pub fn create_surface(
1006+
&self,
1007+
_: &impl raw_window_handle::HasRawWindowHandle,
1008+
) -> Result<Surface, hal::window::InitError> {
10091009
unimplemented!()
10101010
}
10111011
}

src/backend/gl/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "gfx_backend_gl"
1818

1919
[features]
2020
default = []
21-
wgl = []
21+
wgl = ["raw-window-handle"]
2222

2323
[dependencies]
2424
arrayvec = "0.4"
@@ -31,10 +31,10 @@ glow = "0.2.3"
3131
parking_lot = "0.9"
3232
spirv_cross = { version = "0.16", features = ["glsl"] }
3333
lazy_static = "1"
34+
raw-window-handle = { version = "0.1", optional = true }
3435

3536
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
3637
glutin = { version = "0.22.0-alpha3", optional = true }
37-
winit = { version = "0.20.0-alpha3", optional = true }
3838

3939
[target.'cfg(target_arch = "wasm32")'.dependencies]
4040
js-sys = "0.3.6"

src/backend/gl/src/window/wgl.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ pub mod wgl_ext_sys {
3030
#[link(name = "opengl32")]
3131
extern "C" {}
3232

33-
#[cfg(feature = "winit")]
34-
use winit;
35-
3633
pub(crate) struct Entry {
3734
hwnd: HWND,
3835
pub(crate) hdc: HDC,
@@ -186,12 +183,17 @@ impl Instance {
186183
}
187184
}
188185

189-
#[cfg(feature = "winit")]
190-
pub fn create_surface(&self, window: &winit::window::Window) -> Surface {
191-
use winit::platform::windows::WindowExtWindows;
192-
193-
let hwnd = window.hwnd();
194-
self.create_surface_from_hwnd(hwnd as *mut _)
186+
pub fn create_surface(
187+
&self,
188+
has_handle: &impl raw_window_handle::HasRawWindowHandle,
189+
) -> Result<Surface, hal::window::InitError> {
190+
match has_handle.raw_window_handle() {
191+
#[cfg(windows)]
192+
raw_window_handle::RawWindowHandle::Windows(handle) => {
193+
Ok(self.create_surface_from_hwnd(handle.hwnd))
194+
}
195+
_ => Err(hal::window::InitError::UnsupportedWindowHandle),
196+
}
195197
}
196198
}
197199

src/backend/metal/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ arrayvec = "0.4"
2828
bitflags = "1.0"
2929
copyless = "0.1.4"
3030
log = { version = "0.4" }
31-
winit = { version = "0.20.0-alpha3", optional = true }
3231
dispatch = { version = "0.1", optional = true }
3332
metal = { version = "0.17", features = ["private"] }
3433
foreign-types = "0.3"
@@ -41,6 +40,7 @@ spirv_cross = { version = "0.16", features = ["msl"] }
4140
parking_lot = "0.9"
4241
storage-map = "0.2"
4342
lazy_static = "1"
43+
raw-window-handle = "0.1"
4444

4545
# This forces docs.rs to build the crate on mac, otherwise the build fails
4646
# and we get no docs at all.

src/backend/metal/src/lib.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ use objc::{
7777
runtime::{Object, BOOL, YES, Sel, Class}
7878
};
7979
use parking_lot::{Condvar, Mutex};
80-
#[cfg(feature = "winit")]
81-
use winit;
8280
use lazy_static::lazy_static;
8381

8482
use std::mem;
@@ -305,17 +303,20 @@ impl Instance {
305303
})
306304
}
307305

308-
#[cfg(feature = "winit")]
309-
pub fn create_surface(&self, window: &winit::window::Window) -> Surface {
310-
#[cfg(target_os = "ios")]
311-
{
312-
use winit::platform::ios::WindowExtIOS;
313-
self.create_surface_from_uiview(window.ui_view(), false)
314-
}
315-
#[cfg(target_os = "macos")]
316-
{
317-
use winit::platform::macos::WindowExtMacOS;
318-
self.create_surface_from_nsview(window.ns_view(), false)
306+
pub fn create_surface(
307+
&self,
308+
has_handle: &impl raw_window_handle::HasRawWindowHandle,
309+
) -> Result<Surface, hal::window::InitError> {
310+
match has_handle.raw_window_handle() {
311+
#[cfg(target_os = "ios")]
312+
raw_window_handle::RawWindowHandle::IOS(handle) => {
313+
Ok(self.create_surface_from_uiview(handle.ui_view, false))
314+
}
315+
#[cfg(target_os = "macos")]
316+
raw_window_handle::RawWindowHandle::MacOS(handle) => {
317+
Ok(self.create_surface_from_nsview(handle.ns_view, false))
318+
}
319+
_ => Err(hal::window::InitError::UnsupportedWindowHandle),
319320
}
320321
}
321322

src/backend/vulkan/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ shared_library = { version = "0.1.9", optional = true }
2828
ash = "0.29.0"
2929
hal = { path = "../../hal", version = "0.3", package = "gfx-hal" }
3030
smallvec = "0.6"
31-
winit = { version = "0.20.0-alpha3", optional = true }
31+
raw-window-handle = "0.1"
3232

3333
[target.'cfg(windows)'.dependencies]
3434
winapi = { version = "0.3", features = ["libloaderapi", "windef", "winuser"] }

src/backend/vulkan/src/window.rs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -313,53 +313,52 @@ impl Instance {
313313
self.create_surface_from_vk_surface_khr(surface)
314314
}
315315

316-
#[cfg(feature = "winit")]
317-
#[allow(unreachable_code)]
318-
pub fn create_surface(&self, window: &winit::window::Window) -> Surface {
319-
#[cfg(all(unix, not(target_os = "android"), not(target_os = "macos")))]
320-
{
321-
use winit::platform::unix::WindowExtUnix;
322-
323-
if self.extensions.contains(&khr::WaylandSurface::name()) {
324-
if let Some(display) = window.wayland_display() {
325-
let display: *mut c_void = display as *mut _;
326-
let surface: *mut c_void = window.wayland_surface().unwrap() as *mut _;
327-
return self.create_surface_from_wayland(display, surface);
328-
}
316+
pub fn create_surface(
317+
&self,
318+
has_handle: &impl raw_window_handle::HasRawWindowHandle,
319+
) -> Result<Surface, hal::window::InitError> {
320+
use raw_window_handle::RawWindowHandle;
321+
322+
match has_handle.raw_window_handle() {
323+
#[cfg(all(
324+
unix,
325+
not(target_os = "android"),
326+
not(target_os = "macos")
327+
))]
328+
RawWindowHandle::Wayland(handle)
329+
if self.extensions.contains(&khr::WaylandSurface::name()) =>
330+
{
331+
Ok(self.create_surface_from_wayland(handle.display, handle.surface))
329332
}
330-
#[cfg(feature = "x11")]
331-
{
332-
if self.extensions.contains(&khr::XlibSurface::name()) {
333-
if let Some(display) = window.xlib_display() {
334-
let window = window.xlib_window().unwrap();
335-
return self.create_surface_from_xlib(display as _, window);
336-
}
337-
}
338-
}
339-
panic!("The Vulkan driver does not support surface creation!");
340-
}
341-
#[cfg(target_os = "android")]
342-
{
343-
use winit::platform::android::WindowExtAndroid;
344-
return self.create_surface_android(window.get_native_window());
345-
}
346-
#[cfg(windows)]
347-
{
348-
use winapi::um::libloaderapi::GetModuleHandleW;
349-
use winit::platform::windows::WindowExtWindows;
350-
351-
let hinstance = unsafe { GetModuleHandleW(ptr::null()) };
352-
let hwnd = window.hwnd();
353-
return self.create_surface_from_hwnd(hinstance as *mut _, hwnd as *mut _);
354-
}
355-
#[cfg(target_os = "macos")]
356-
{
357-
use winit::platform::macos::WindowExtMacOS;
358-
359-
return self.create_surface_from_ns_view(window.ns_view());
333+
#[cfg(all(
334+
feature = "x11",
335+
unix,
336+
not(target_os = "android"),
337+
not(target_os = "macos")
338+
))]
339+
RawWindowHandle::X11(handle)
340+
if self.extensions.contains(&khr::XlibSurface::name()) =>
341+
{
342+
Ok(self.create_surface_from_xlib(handle.display as *mut _, handle.window))
343+
}
344+
// #[cfg(target_os = "android")]
345+
// RawWindowHandle::ANativeWindowHandle(handle) => {
346+
// let native_window = unimplemented!();
347+
// self.create_surface_android(native_window)
348+
//}
349+
#[cfg(windows)]
350+
RawWindowHandle::Windows(handle) => {
351+
use winapi::um::libloaderapi::GetModuleHandleW;
352+
353+
let hinstance = unsafe { GetModuleHandleW(ptr::null()) };
354+
Ok(self.create_surface_from_hwnd(hinstance as *mut _, handle.hwnd))
355+
}
356+
#[cfg(target_os = "macos")]
357+
RawWindowHandle::MacOS(handle) => {
358+
Ok(self.create_surface_from_ns_view(handle.ns_view))
359+
}
360+
_ => Err(hal::window::InitError::UnsupportedWindowHandle),
360361
}
361-
let _ = window;
362-
panic!("No suitable WSI enabled!");
363362
}
364363

365364
pub fn create_surface_from_vk_surface_khr(&self, surface: vk::SurfaceKHR) -> Surface {

0 commit comments

Comments
 (0)