Skip to content

Commit

Permalink
metal: check if in the main thread when calling create_surface
Browse files Browse the repository at this point in the history
  • Loading branch information
jinleili committed Jun 7, 2022
1 parent 717bc40 commit 702c4ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
8 changes: 7 additions & 1 deletion wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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];
Expand Down
1 change: 1 addition & 0 deletions wgpu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: raw_window_handle::HasRawWindowHandle>(
&self,
window: &W,
Expand Down

0 comments on commit 702c4ee

Please sign in to comment.