From 0d019d9253d35ebce87c655fa8f7ceb191fbb6ed Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 14 Jul 2024 12:05:27 -0700 Subject: [PATCH] wgpu -> 22.1 This compiles, but when I run it on X11 I get a fully transparent window. I have not tried to debug this yet. --- Cargo.lock | 234 +++++++++++++++------------ wezterm-gui/Cargo.toml | 2 +- wezterm-gui/src/scripting/mod.rs | 1 + wezterm-gui/src/termwindow/webgpu.rs | 37 +++-- window/Cargo.toml | 3 +- window/src/os/macos/window.rs | 37 +++-- window/src/os/wayland/window.rs | 43 +++-- window/src/os/windows/window.rs | 44 +++-- window/src/os/x11/window.rs | 53 +++--- window/src/os/x_and_wayland.rs | 18 +-- 10 files changed, 270 insertions(+), 202 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6535a2f229b..faea7b60a28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -170,11 +170,11 @@ checksum = "175571dd1d178ced59193a6fc02dde1b972eb0bc56c892cde9beeceac5bf0f6b" [[package]] name = "ash" -version = "0.37.3+1.3.251" +version = "0.38.0+1.3.281" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e9c3835d686b0a6084ab4234fcd1b07dbf6e4767dce60874b12356a25ecd4a" +checksum = "0bb44936d800fea8f016d7f2311c6a4f97aebd5dc86f09906139ec848cf3a46f" dependencies = [ - "libloading 0.7.4", + "libloading", ] [[package]] @@ -469,7 +469,16 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0700ddab506f33b20a03b13996eccd309a48e5ff77d0d95926aa0210fb4e95f1" dependencies = [ - "bit-vec", + "bit-vec 0.6.3", +] + +[[package]] +name = "bit-set" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0481a0e032742109b1133a095184ee93d88f3dc9e0d28a5d033dc77a073f44f" +dependencies = [ + "bit-vec 0.7.0", ] [[package]] @@ -478,6 +487,12 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "349f9b6a179ed607305526ca489b34ad0a41aed5f7980fa90eb03160b69598fb" +[[package]] +name = "bit-vec" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c54ff287cfc0a34f38a6b832ea1bd8e448a330b3e40a50859e6488bee07f22" + [[package]] name = "bit_field" version = "0.10.2" @@ -905,10 +920,35 @@ dependencies = [ ] [[package]] -name = "com-rs" -version = "0.2.1" +name = "com" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e17887fd17353b65b1b2ef1c526c83e26cd72e74f598a8dc1bee13a48f3d9f6" +dependencies = [ + "com_macros", +] + +[[package]] +name = "com_macros" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf43edc576402991846b093a7ca18a3477e0ef9c588cde84964b5d3e43016642" +checksum = "d375883580a668c7481ea6631fc1a8863e33cc335bf56bfad8d7e6d4b04b13a5" +dependencies = [ + "com_macros_support", + "proc-macro2", + "syn 1.0.109", +] + +[[package]] +name = "com_macros_support" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad899a1087a9296d5644792d7cb72b8e34c1bec8e7d4fbc002230169a6e8710c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] [[package]] name = "concurrent-queue" @@ -1162,12 +1202,12 @@ checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991" [[package]] name = "d3d12" -version = "0.7.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e16e44ab292b1dddfdaf7be62cfd8877df52f2f3fde5858d95bab606be259f20" +checksum = "bdbd1f579714e3c809ebd822c81ef148b1ceaeb3d535352afc73fd0c4c6a0017" dependencies = [ "bitflags 2.6.0", - "libloading 0.8.5", + "libloading", "winapi", ] @@ -1299,7 +1339,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330c60081dcc4c72131f8eb70510f1ac07223e5d4163db481a04a0befcffa412" dependencies = [ - "libloading 0.8.5", + "libloading", ] [[package]] @@ -1320,6 +1360,15 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "document-features" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" +dependencies = [ + "litrs", +] + [[package]] name = "downcast-rs" version = "1.2.1" @@ -1564,7 +1613,7 @@ version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b95f7c0680e4142284cf8b22c14a476e87d61b004a3a0861872b32ef7ead40a2" dependencies = [ - "bit-set", + "bit-set 0.5.3", "regex", ] @@ -2023,9 +2072,9 @@ dependencies = [ [[package]] name = "glutin_wgl_sys" -version = "0.5.0" +version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c8098adac955faa2d31079b65dc48841251f69efd3ac25477903fc424362ead" +checksum = "0a4e1951bbd9434a81aa496fe59ccc2235af3820d27b85f9314e279609211e2c" dependencies = [ "gl_generator", ] @@ -2066,23 +2115,22 @@ dependencies = [ [[package]] name = "gpu-allocator" -version = "0.23.0" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "40fe17c8a05d60c38c0a4e5a3c802f2f1ceb66b76c67d96ffb34bef0475a7fad" +checksum = "fdd4240fc91d3433d5e5b0fc5b67672d771850dc19bbee03c1381e19322803d7" dependencies = [ - "backtrace", "log", "presser", "thiserror", "winapi", - "windows 0.51.1", + "windows 0.52.0", ] [[package]] name = "gpu-descriptor" -version = "0.2.4" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cc11df1ace8e7e564511f53af41f3e42ddc95b56fd07b3f4445d2a6048bc682c" +checksum = "9c08c1f623a8d0b722b8b99f821eb0ba672a1618f0d3b16ddbee1cedd2dd8557" dependencies = [ "bitflags 2.6.0", "gpu-descriptor-types", @@ -2091,9 +2139,9 @@ dependencies = [ [[package]] name = "gpu-descriptor-types" -version = "0.1.2" +version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bf0b36e6f090b7e1d8a4b49c0cb81c1f8376f72198c65dd3ad9ff3556b8b78c" +checksum = "fdf242682df893b86f33a73828fb09ca4b2d3bb6cc95249707fc684d27484b91" dependencies = [ "bitflags 2.6.0", ] @@ -2172,14 +2220,14 @@ dependencies = [ [[package]] name = "hassle-rs" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1397650ee315e8891a0df210707f0fc61771b0cc518c3023896064c5407cb3b0" +checksum = "af2a7e73e1f34c48da31fb668a907f250794837e08faa144fd24f0b8b741e890" dependencies = [ - "bitflags 1.3.2", - "com-rs", + "bitflags 2.6.0", + "com", "libc", - "libloading 0.7.4", + "libloading", "thiserror", "widestring", "winapi", @@ -2398,7 +2446,7 @@ dependencies = [ "iana-time-zone-haiku", "js-sys", "wasm-bindgen", - "windows-core 0.52.0", + "windows-core", ] [[package]] @@ -2618,6 +2666,12 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "jni-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eaf4bc02d17cbdd7ff4c7438cafcdf7fb9a4613313ad11b4f8fefe7d3fa0130" + [[package]] name = "jobserver" version = "0.1.32" @@ -2666,7 +2720,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6aae1df220ece3c0ada96b8153459b67eebe9ae9212258bb0134ae60416fdf76" dependencies = [ "libc", - "libloading 0.8.5", + "libloading", "pkg-config", ] @@ -2792,16 +2846,6 @@ dependencies = [ "pkg-config", ] -[[package]] -name = "libloading" -version = "0.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b67380fd3b2fbe7527a606e18729d21c6f3951633d0500574c4dc22d2d638b9f" -dependencies = [ - "cfg-if", - "winapi", -] - [[package]] name = "libloading" version = "0.8.5" @@ -2911,6 +2955,12 @@ version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" +[[package]] +name = "litrs" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" + [[package]] name = "lock_api" version = "0.4.12" @@ -3068,9 +3118,9 @@ dependencies = [ [[package]] name = "metal" -version = "0.27.0" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c43f73953f8cbe511f021b58f18c3ce1c3d1ae13fe953293e13345bf83217f25" +checksum = "7ecfd3296f8c56b7c1f6fbac3c71cefa9d78ce009850c45000015f206dc7fa21" dependencies = [ "bitflags 2.6.0", "block", @@ -3241,17 +3291,18 @@ dependencies = [ [[package]] name = "naga" -version = "0.14.2" +version = "22.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ae585df4b6514cf8842ac0f1ab4992edc975892704835b549cf818dc0191249e" +checksum = "8bd5a652b6faf21496f2cfd88fc49989c8db0825d1f6746b1a71a6ede24a63ad" dependencies = [ - "bit-set", + "arrayvec", + "bit-set 0.6.0", "bitflags 2.6.0", + "cfg_aliases 0.1.1", "codespan-reporting", "hexf-parse", "indexmap 2.2.6", "log", - "num-traits", "rustc-hash 1.1.0", "spirv", "termcolor", @@ -3294,6 +3345,15 @@ dependencies = [ "tempfile", ] +[[package]] +name = "ndk-sys" +version = "0.5.0+25.2.9519653" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c196769dd60fd4f363e11d948139556a344e79d451aeb2fa2fd040738ef7691" +dependencies = [ + "jni-sys", +] + [[package]] name = "new_debug_unreachable" version = "1.0.6" @@ -3494,16 +3554,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1" dependencies = [ "malloc_buf", - "objc_exception", -] - -[[package]] -name = "objc_exception" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4" -dependencies = [ - "cc", ] [[package]] @@ -4223,9 +4273,9 @@ dependencies = [ [[package]] name = "raw-window-handle" -version = "0.5.2" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" +checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" [[package]] name = "rayon" @@ -4994,12 +5044,11 @@ dependencies = [ [[package]] name = "spirv" -version = "0.2.0+1.5.4" +version = "0.3.0+sdk-1.3.268.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "246bfa38fe3db3f1dfc8ca5a2cdeb7348c78be2112740cc0ec8ef18b6d94f830" +checksum = "eda41003dc44290527a59b13432d4a0379379fa074b70174882adfbdfd917844" dependencies = [ - "bitflags 1.3.2", - "num-traits", + "bitflags 2.6.0", ] [[package]] @@ -5992,6 +6041,7 @@ checksum = "f90e11ce2ca99c97b940ee83edbae9da2d56a08f9ea8158550fd77fa31722993" dependencies = [ "cc", "downcast-rs", + "raw-window-handle", "rustix 0.38.34", "scoped-tls", "smallvec", @@ -6091,9 +6141,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.64" +version = "0.3.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b85cbef8c220a6abc02aefd892dfc0fc23afb1c6a426316ec33253a3877249b" +checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" dependencies = [ "js-sys", "wasm-bindgen", @@ -6557,13 +6607,13 @@ dependencies = [ [[package]] name = "wgpu" -version = "0.18.0" +version = "22.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30e7d227c9f961f2061c26f4cb0fbd4df0ef37e056edd0931783599d6c94ef24" +checksum = "e1d1c4ba43f80542cf63a0a6ed3134629ae73e8ab51e4b765a67f3aa062eb433" dependencies = [ "arrayvec", - "cfg-if", - "flume", + "cfg_aliases 0.1.1", + "document-features", "js-sys", "log", "naga", @@ -6582,39 +6632,42 @@ dependencies = [ [[package]] name = "wgpu-core" -version = "0.18.1" +version = "22.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef91c1d62d1e9e81c79e600131a258edf75c9531cbdbde09c44a011a47312726" +checksum = "0348c840d1051b8e86c3bcd31206080c5e71e5933dabd79be1ce732b0b2f089a" dependencies = [ "arrayvec", - "bit-vec", + "bit-vec 0.7.0", "bitflags 2.6.0", - "codespan-reporting", + "cfg_aliases 0.1.1", + "document-features", + "indexmap 2.2.6", "log", "naga", + "once_cell", "parking_lot 0.12.3", "profiling", "raw-window-handle", "rustc-hash 1.1.0", "smallvec", "thiserror", - "web-sys", "wgpu-hal", "wgpu-types", ] [[package]] name = "wgpu-hal" -version = "0.18.1" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84ecc802da3eb67b4cf3dd9ea6fe45bbb47ef13e6c49c5c3240868a9cc6cdd9" +checksum = "f6bbf4b4de8b2a83c0401d9e5ae0080a2792055f25859a02bf9be97952bbed4f" dependencies = [ "android_system_properties", "arrayvec", "ash", - "bit-set", + "bit-set 0.6.0", "bitflags 2.6.0", "block", + "cfg_aliases 0.1.1", "core-graphics-types", "d3d12", "glow", @@ -6626,10 +6679,11 @@ dependencies = [ "js-sys", "khronos-egl", "libc", - "libloading 0.8.5", + "libloading", "log", "metal", "naga", + "ndk-sys", "objc", "once_cell", "parking_lot 0.12.3", @@ -6648,9 +6702,9 @@ dependencies = [ [[package]] name = "wgpu-types" -version = "0.18.0" +version = "22.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d5ed5f0edf0de351fe311c53304986315ce866f394a2e6df0c4b3c70774bcdd" +checksum = "bc9d91f0e2c4b51434dfa6db77846f2793149d8e73f800fa2e41f52b8eac3c5d" dependencies = [ "bitflags 2.6.0", "js-sys", @@ -6746,7 +6800,7 @@ dependencies = [ "k9", "lazy_static", "libc", - "libloading 0.8.5", + "libloading", "line_drawing", "log", "metrics", @@ -6763,6 +6817,7 @@ dependencies = [ "thiserror", "tiny-skia", "url", + "wayland-backend", "wayland-client", "wayland-egl", "wayland-protocols", @@ -6805,35 +6860,16 @@ dependencies = [ "windows_x86_64_msvc 0.33.0", ] -[[package]] -name = "windows" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" -dependencies = [ - "windows-core 0.51.1", - "windows-targets 0.48.5", -] - [[package]] name = "windows" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" dependencies = [ - "windows-core 0.52.0", + "windows-core", "windows-targets 0.52.6", ] -[[package]] -name = "windows-core" -version = "0.51.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" -dependencies = [ - "windows-targets 0.48.5", -] - [[package]] name = "windows-core" version = "0.52.0" diff --git a/wezterm-gui/Cargo.toml b/wezterm-gui/Cargo.toml index e4be8916871..6fab17ed12b 100644 --- a/wezterm-gui/Cargo.toml +++ b/wezterm-gui/Cargo.toml @@ -105,7 +105,7 @@ wezterm-open-url = { path = "../wezterm-open-url" } wezterm-ssh = { path = "../wezterm-ssh" } wezterm-term = { path = "../term", features=["use_serde"] } wezterm-toast-notification = { path = "../wezterm-toast-notification" } -wgpu = "0.18" +wgpu = "22.1" window = { path = "../window" } window-funcs = { path = "../lua-api-crates/window-funcs" } diff --git a/wezterm-gui/src/scripting/mod.rs b/wezterm-gui/src/scripting/mod.rs index bcb447b22bc..8867be45d57 100644 --- a/wezterm-gui/src/scripting/mod.rs +++ b/wezterm-gui/src/scripting/mod.rs @@ -88,6 +88,7 @@ pub fn register(lua: &Lua) -> anyhow::Result<()> { }); let gpus: Vec = instance .enumerate_adapters(backends) + .into_iter() .map(|adapter| { let info = adapter.get_info(); crate::termwindow::webgpu::adapter_info_to_gpu_info(info) diff --git a/wezterm-gui/src/termwindow/webgpu.rs b/wezterm-gui/src/termwindow/webgpu.rs index 204259de715..92f9a08472d 100644 --- a/wezterm-gui/src/termwindow/webgpu.rs +++ b/wezterm-gui/src/termwindow/webgpu.rs @@ -6,7 +6,8 @@ use std::sync::Arc; use wgpu::util::DeviceExt; use window::bitmaps::Texture2d; use window::raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, WindowHandle, }; use window::{BitmapImage, Dimensions, Rect, Window}; @@ -23,7 +24,7 @@ pub struct ShaderUniform { pub struct WebGpuState { pub adapter_info: wgpu::AdapterInfo, pub downlevel_caps: wgpu::DownlevelCapabilities, - pub surface: wgpu::Surface, + pub surface: wgpu::Surface<'static>, pub device: wgpu::Device, pub queue: Arc, pub config: RefCell, @@ -44,21 +45,21 @@ pub struct RawHandlePair { impl RawHandlePair { fn new(window: &Window) -> Self { Self { - window: window.raw_window_handle(), - display: window.raw_display_handle(), + window: window.window_handle().expect("window handle").as_raw(), + display: window.display_handle().expect("display handle").as_raw(), } } } -unsafe impl HasRawWindowHandle for RawHandlePair { - fn raw_window_handle(&self) -> RawWindowHandle { - self.window +impl HasWindowHandle for RawHandlePair { + fn window_handle(&self) -> Result { + unsafe { Ok(WindowHandle::borrow_raw(self.window)) } } } -unsafe impl HasRawDisplayHandle for RawHandlePair { - fn raw_display_handle(&self) -> RawDisplayHandle { - self.display +impl HasDisplayHandle for RawHandlePair { + fn display_handle(&self) -> Result { + unsafe { Ok(DisplayHandle::borrow_raw(self.display)) } } } @@ -194,6 +195,7 @@ fn compute_compatibility_list( ) -> Vec { instance .enumerate_adapters(backends) + .into_iter() .map(|a| { let info = adapter_info_to_gpu_info(a.get_info()); let compatible = a.is_surface_supported(&surface); @@ -226,7 +228,9 @@ impl WebGpuState { backends, ..Default::default() }); - let surface = unsafe { instance.create_surface(&handle)? }; + let surface = unsafe { + instance.create_surface_unsafe(wgpu::SurfaceTargetUnsafe::from_window(&handle)?)? + }; let mut adapter: Option = None; @@ -316,16 +320,17 @@ impl WebGpuState { let (device, queue) = adapter .request_device( &wgpu::DeviceDescriptor { - features: wgpu::Features::empty(), + required_features: wgpu::Features::empty(), // WebGL doesn't support all of wgpu's features, so if // we're building for the web we'll have to disable some. - limits: if cfg!(target_arch = "wasm32") { + required_limits: if cfg!(target_arch = "wasm32") { wgpu::Limits::downlevel_webgl2_defaults() } else { wgpu::Limits::downlevel_defaults() } .using_resolution(adapter.limits()), label: None, + memory_hints: Default::default(), }, None, // Trace path ) @@ -374,6 +379,7 @@ impl WebGpuState { wgpu::CompositeAlphaMode::Auto }, view_formats, + desired_maximum_frame_latency: 2, }; surface.configure(&device, &config); @@ -454,6 +460,7 @@ impl WebGpuState { module: &shader, entry_point: "vs_main", buffers: &[Vertex::desc()], + compilation_options: wgpu::PipelineCompilationOptions::default(), }, fragment: Some(wgpu::FragmentState { module: &shader, @@ -463,6 +470,7 @@ impl WebGpuState { blend: Some(wgpu::BlendState::ALPHA_BLENDING), write_mask: wgpu::ColorWrites::ALL, })], + compilation_options: wgpu::PipelineCompilationOptions::default(), }), primitive: wgpu::PrimitiveState { @@ -481,6 +489,7 @@ impl WebGpuState { alpha_to_coverage_enabled: false, }, multiview: None, + cache: None, }); Ok(Self { @@ -528,7 +537,7 @@ impl WebGpuState { #[cfg(windows)] RawWindowHandle::Win32(h) => { let mut rect = unsafe { std::mem::zeroed() }; - unsafe { winapi::um::winuser::GetClientRect(h.hwnd as _, &mut rect) }; + unsafe { winapi::um::winuser::GetClientRect(h.hwnd.get() as _, &mut rect) }; dims.pixel_width = (rect.right - rect.left) as usize; dims.pixel_height = (rect.bottom - rect.top) as usize; } diff --git a/window/Cargo.toml b/window/Cargo.toml index 3cfa26dc9b6..8da9eb1d4b6 100644 --- a/window/Cargo.toml +++ b/window/Cargo.toml @@ -36,7 +36,7 @@ line_drawing = "0.8" log = "0.4" metrics = "0.23" promise = { path = "../promise" } -raw-window-handle = "0.5" +raw-window-handle = "0.6" resize = "0.5" serde = {version="1.0", features = ["rc", "derive"]} tiny-skia = "0.11" @@ -82,6 +82,7 @@ zbus = "4.2" zvariant = "4.0" smithay-client-toolkit = {version = "0.19", default-features=false, optional=true} +wayland-backend = {version="0.3.5", features=["client_system", "rwh_06"]} wayland-protocols = {version="0.32", optional=true} wayland-client = {version="0.31", optional=true} wayland-egl = {version="0.32", optional=true} diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 7576e72b637..b0657dfbd18 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -39,13 +39,14 @@ use objc::runtime::{Class, Object, Protocol, Sel}; use objc::*; use promise::Future; use raw_window_handle::{ - AppKitDisplayHandle, AppKitWindowHandle, HasRawDisplayHandle, HasRawWindowHandle, - RawDisplayHandle, RawWindowHandle, + AppKitDisplayHandle, AppKitWindowHandle, DisplayHandle, HandleError, HasDisplayHandle, + HasWindowHandle, RawDisplayHandle, RawWindowHandle, WindowHandle, }; use std::any::Any; use std::cell::RefCell; use std::ffi::c_void; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::Rc; use std::str::FromStr; use std::time::Instant; @@ -661,18 +662,21 @@ impl Window { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::AppKit(AppKitDisplayHandle::empty()) +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::AppKit( + AppKitDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = AppKitWindowHandle::empty(); - handle.ns_window = self.ns_window as *mut _; - handle.ns_view = self.ns_view as *mut _; - RawWindowHandle::AppKit(handle) +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result { + let mut handle = + AppKitWindowHandle::new(NonNull::new(self.ns_view as *mut _).expect("non-null")); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::AppKit(handle))) } } } @@ -865,18 +869,13 @@ impl WindowOps for Window { _config: &ConfigHandle, window_state: WindowState, ) -> anyhow::Result> { - let raw = self.raw_window_handle(); - // We implement this method primarily to provide Notch-avoidance for // systems with a notch. // We only need this for non-native full screen mode. - let native_full_screen = match raw { - RawWindowHandle::AppKit(raw) => { - let style_mask = unsafe { NSWindow::styleMask(raw.ns_window as *mut Object) }; - style_mask.contains(NSWindowStyleMask::NSFullScreenWindowMask) - } - _ => false, + let native_full_screen = { + let style_mask = unsafe { NSWindow::styleMask(self.ns_window) }; + style_mask.contains(NSWindowStyleMask::NSFullScreenWindowMask) }; let border_dimensions = diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index 254f42dea78..62149dc3cd5 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -6,6 +6,7 @@ use std::io::Read; use std::num::NonZeroU32; use std::os::fd::AsRawFd; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; @@ -16,8 +17,8 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::{Future, Promise}; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, - WaylandDisplayHandle, WaylandWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawWindowHandle, + WaylandWindowHandle, WindowHandle, }; use smithay_client_toolkit::compositor::{CompositorHandler, SurfaceData, SurfaceDataExt}; use smithay_client_toolkit::data_device_manager::ReadPipe; @@ -1377,26 +1378,35 @@ impl SurfaceDataExt for SurfaceUserData { } } -unsafe impl HasRawWindowHandle for WaylandWindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = WaylandWindowHandle::empty(); - let surface = self.surface(); - handle.surface = surface.id().as_ptr() as *mut _; - RawWindowHandle::Wayland(handle) +impl HasDisplayHandle for WaylandWindowInner { + fn display_handle(&self) -> Result { + let conn = WaylandConnection::get().unwrap().wayland(); + let backend = conn.connection.backend(); + let handle = backend.display_handle()?; + Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) }) + } +} + +impl HasWindowHandle for WaylandWindowInner { + fn window_handle(&self) -> Result { + let handle = WaylandWindowHandle::new( + NonNull::new(self.surface().id().as_ptr() as _).expect("non-null"), + ); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Wayland(handle))) } } } -unsafe impl HasRawDisplayHandle for WaylandWindow { - fn raw_display_handle(&self) -> RawDisplayHandle { - let mut handle = WaylandDisplayHandle::empty(); +impl HasDisplayHandle for WaylandWindow { + fn display_handle(&self) -> Result { let conn = WaylandConnection::get().unwrap().wayland(); - handle.display = conn.connection.backend().display_ptr() as *mut _; - RawDisplayHandle::Wayland(handle) + let backend = conn.connection.backend(); + let handle = backend.display_handle()?; + Ok(unsafe { DisplayHandle::borrow_raw(handle.as_raw()) }) } } -unsafe impl HasRawWindowHandle for WaylandWindow { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for WaylandWindow { + fn window_handle(&self) -> Result { let conn = Connection::get().expect("raw_window_handle only callable on main thread"); let handle = conn .wayland() @@ -1404,6 +1414,7 @@ unsafe impl HasRawWindowHandle for WaylandWindow { .expect("window handle invalid!?"); let inner = handle.borrow(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/windows/window.rs b/window/src/os/windows/window.rs index 58053344991..21dfa522d88 100644 --- a/window/src/os/windows/window.rs +++ b/window/src/os/windows/window.rs @@ -13,8 +13,8 @@ use config::{ConfigHandle, ImePreeditRendering, SystemBackdrop}; use lazy_static::lazy_static; use promise::Future; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, Win32WindowHandle, - WindowsDisplayHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, Win32WindowHandle, WindowHandle, WindowsDisplayHandle, }; use shared_library::shared_library; use std::any::Any; @@ -23,6 +23,7 @@ use std::collections::HashMap; use std::convert::TryInto; use std::ffi::OsString; use std::io::{self, Error as IoError}; +use std::num::NonZeroIsize; use std::os::windows::ffi::OsStringExt; use std::path::PathBuf; use std::ptr::{null, null_mut}; @@ -200,18 +201,22 @@ fn callback_behavior() -> glium::debug::DebugCallbackBehavior { } } -unsafe impl HasRawDisplayHandle for WindowInner { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) +impl HasDisplayHandle for WindowInner { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Windows( + WindowsDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for WindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = Win32WindowHandle::empty(); - handle.hwnd = self.hwnd.0 as *mut _; - handle.hinstance = unsafe { GetModuleHandleW(null()) } as _; - RawWindowHandle::Win32(handle) +impl HasWindowHandle for WindowInner { + fn window_handle(&self) -> Result { + let mut handle = + Win32WindowHandle::new(NonZeroIsize::new(self.hwnd.0 as _).expect("non-zero")); + handle.hinstance = NonZeroIsize::new(unsafe { GetModuleHandleW(null()) } as _); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Win32(handle))) } } } @@ -729,19 +734,24 @@ impl WindowInner { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { - RawDisplayHandle::Windows(WindowsDisplayHandle::empty()) +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result { + unsafe { + Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Windows( + WindowsDisplayHandle::new(), + ))) + } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result { let conn = Connection::get().expect("raw_window_handle only callable on main thread"); let handle = conn.get_window(self.0).expect("window handle invalid!?"); let inner = handle.borrow(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/x11/window.rs b/window/src/os/x11/window.rs index 58ec4f8936f..e6bd87c750f 100644 --- a/window/src/os/x11/window.rs +++ b/window/src/os/x11/window.rs @@ -13,12 +13,14 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::{Future, Promise}; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, XcbDisplayHandle, - XcbWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, RawDisplayHandle, + RawWindowHandle, WindowHandle, XcbDisplayHandle, XcbWindowHandle, }; use std::any::Any; use std::convert::TryInto; +use std::num::NonZeroU32; use std::path::PathBuf; +use std::ptr::NonNull; use std::rc::{Rc, Weak}; use std::sync::{Arc, Mutex}; use url::Url; @@ -134,24 +136,24 @@ impl Drop for XWindowInner { } } -unsafe impl HasRawDisplayHandle for XWindowInner { - fn raw_display_handle(&self) -> RawDisplayHandle { - let mut handle = XcbDisplayHandle::empty(); +impl HasDisplayHandle for XWindowInner { + fn display_handle(&self) -> Result, HandleError> { if let Some(conn) = self.conn.upgrade() { - handle.connection = conn.conn.get_raw_conn() as _; - handle.screen = conn.screen_num; + let handle = + XcbDisplayHandle::new(NonNull::new(conn.conn.get_raw_conn() as _), conn.screen_num); + unsafe { Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Xcb(handle))) } + } else { + Err(HandleError::Unavailable) } - - RawDisplayHandle::Xcb(handle) } } -unsafe impl HasRawWindowHandle for XWindowInner { - fn raw_window_handle(&self) -> RawWindowHandle { - let mut handle = XcbWindowHandle::empty(); - handle.window = self.child_id.resource_id(); - handle.visual_id = self.conn.upgrade().unwrap().visual.visual_id(); - RawWindowHandle::Xcb(handle) +impl HasWindowHandle for XWindowInner { + fn window_handle(&self) -> Result, HandleError> { + let mut handle = + XcbWindowHandle::new(NonZeroU32::new(self.window_id.resource_id()).expect("non-zero")); + handle.visual_id = NonZeroU32::new(self.conn.upgrade().unwrap().visual.visual_id()); + unsafe { Ok(WindowHandle::borrow_raw(RawWindowHandle::Xcb(handle))) } } } @@ -1909,29 +1911,28 @@ impl XWindowInner { } } -unsafe impl HasRawDisplayHandle for XWindow { - fn raw_display_handle(&self) -> RawDisplayHandle { +impl HasDisplayHandle for XWindow { + fn display_handle(&self) -> Result, HandleError> { let conn = Connection::get() - .expect("raw_window_handle only callable on main thread") + .expect("display_handle only callable on main thread") .x11(); - let mut handle = XcbDisplayHandle::empty(); - handle.connection = conn.get_raw_conn() as _; - handle.screen = conn.screen_num; + let handle = XcbDisplayHandle::new(NonNull::new(conn.get_raw_conn() as _), conn.screen_num); - RawDisplayHandle::Xcb(handle) + unsafe { Ok(DisplayHandle::borrow_raw(RawDisplayHandle::Xcb(handle))) } } } -unsafe impl HasRawWindowHandle for XWindow { - fn raw_window_handle(&self) -> RawWindowHandle { - let conn = Connection::get().expect("raw_window_handle only callable on main thread"); +impl HasWindowHandle for XWindow { + fn window_handle(&self) -> Result, HandleError> { + let conn = Connection::get().expect("window_handle only callable on main thread"); let handle = conn .x11() .window_by_id(self.0) .expect("window handle invalid!?"); let inner = handle.lock().unwrap(); - inner.raw_window_handle() + let handle = inner.window_handle()?; + unsafe { Ok(WindowHandle::borrow_raw(handle.as_raw())) } } } diff --git a/window/src/os/x_and_wayland.rs b/window/src/os/x_and_wayland.rs index d785feced80..b66d18d8201 100644 --- a/window/src/os/x_and_wayland.rs +++ b/window/src/os/x_and_wayland.rs @@ -16,7 +16,7 @@ use async_trait::async_trait; use config::ConfigHandle; use promise::*; use raw_window_handle::{ - HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle, + DisplayHandle, HandleError, HasDisplayHandle, HasWindowHandle, WindowHandle, }; use std::any::Any; use std::rc::Rc; @@ -202,22 +202,22 @@ impl Window { } } -unsafe impl HasRawDisplayHandle for Window { - fn raw_display_handle(&self) -> RawDisplayHandle { +impl HasDisplayHandle for Window { + fn display_handle(&self) -> Result, HandleError> { match self { - Self::X11(x) => x.raw_display_handle(), + Self::X11(x) => x.display_handle(), #[cfg(feature = "wayland")] - Self::Wayland(w) => w.raw_display_handle(), + Self::Wayland(w) => w.display_handle(), } } } -unsafe impl HasRawWindowHandle for Window { - fn raw_window_handle(&self) -> RawWindowHandle { +impl HasWindowHandle for Window { + fn window_handle(&self) -> Result, HandleError> { match self { - Self::X11(x) => x.raw_window_handle(), + Self::X11(x) => x.window_handle(), #[cfg(feature = "wayland")] - Self::Wayland(w) => w.raw_window_handle(), + Self::Wayland(w) => w.window_handle(), } } }