diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index a0ca46ab35..3031e4c67c 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -9,7 +9,7 @@ use objc::{ declare::ClassDecl, msg_send, rc::autoreleasepool, - runtime::{Class, Object, Sel, BOOL, YES}, + runtime::{Class, Object, Sel, BOOL, NO, YES}, sel, sel_impl, }; use parking_lot::Mutex; @@ -74,6 +74,7 @@ impl super::Surface { } } + /// If not called on the main thread, this will panic. #[allow(clippy::transmute_ptr_to_ref)] pub unsafe fn from_view( view: *mut c_void, @@ -84,6 +85,11 @@ impl super::Surface { panic!("window does not have a valid contentView"); } + let is_main_thread: BOOL = msg_send![class!(NSThread), isMainThread]; + if is_main_thread == NO { + panic!("create_surface cannot be called in non-ui thread."); + } + let main_layer: *mut Object = msg_send![view, layer]; let class = class!(CAMetalLayer); let is_valid_layer: BOOL = msg_send![main_layer, isKindOfClass: class]; diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs index 38ae0fab0f..b39adb558b 100644 --- a/wgpu/src/lib.rs +++ b/wgpu/src/lib.rs @@ -1708,6 +1708,7 @@ impl Instance { /// /// - Raw Window Handle must be a valid object to create a surface upon and /// must remain valid for the lifetime of the returned surface. + /// - If not called on the main thread, metal backend will panic. pub unsafe fn create_surface( &self, window: &W,