metal: check if in the main thread when calling create_surface (#2736)

This commit is contained in:
Jinlei Li 2022-06-07 14:07:38 +08:00 committed by GitHub
parent 5bee63e605
commit 31c6b39c20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -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];

View File

@ -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,