Skip to content

Commit 9c9996a

Browse files
committed
Add 'metal' feature for iOS/macOS, this will create a UIView/NSView with CAMetalLayer and sets gl_or_metal_backed (iOS) true
1 parent 911fad0 commit 9c9996a

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ mint = ["dpi/mint"]
7979
rwh_04 = ["dep:rwh_04", "ndk/rwh_04"]
8080
rwh_05 = ["dep:rwh_05", "ndk/rwh_05"]
8181
rwh_06 = ["dep:rwh_06", "ndk/rwh_06"]
82+
# Only works on iOS/macOS, will create a UIView/NSView with CAMetalLayer and sets gl_or_metal_backed (iOS) true
83+
metal = []
8284

8385
[build-dependencies]
8486
cfg_aliases = "0.2.1"

src/platform_impl/ios/view.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ declare_class!(
4848
}
4949

5050
unsafe impl WinitView {
51+
#[allow(non_snake_case)]
52+
#[cfg(feature="metal")]
53+
#[method(layerClass)]
54+
fn layerClass() -> &'static objc2::runtime::AnyClass{ objc2::class!(CAMetalLayer) }
55+
5156
#[method(drawRect:)]
5257
fn draw_rect(&self, rect: CGRect) {
5358
let mtm = MainThreadMarker::new().unwrap();

src/platform_impl/ios/window.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,9 @@ impl Window {
522522

523523
let view = WinitView::new(mtm, &window_attributes, frame);
524524

525-
let gl_or_metal_backed =
526-
view.isKindOfClass(class!(CAMetalLayer)) || view.isKindOfClass(class!(CAEAGLLayer));
525+
let gl_or_metal_backed = cfg!(feature = "metal")
526+
|| view.isKindOfClass(class!(CAMetalLayer))
527+
|| view.isKindOfClass(class!(CAEAGLLayer));
527528

528529
let view_controller = WinitViewController::new(mtm, &window_attributes, &view);
529530
let window = WinitUIWindow::new(mtm, &window_attributes, frame, &view_controller);

src/platform_impl/macos/view.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,16 @@ impl WinitView {
804804
});
805805
let this: Retained<Self> = unsafe { msg_send_id![super(this), init] };
806806

807+
if cfg!(feature = "metal") {
808+
unsafe {
809+
let _: () = objc2::msg_send![&this, setWantsLayer: objc2::ffi::YES];
810+
let metal_layer_cls = objc2::class!(CAMetalLayer);
811+
let metal_layer: *mut objc2::runtime::AnyObject =
812+
objc2::msg_send![metal_layer_cls, new];
813+
let _: () = objc2::msg_send![&this, setLayer: metal_layer];
814+
}
815+
}
816+
807817
this.setPostsFrameChangedNotifications(true);
808818
let notification_center = unsafe { NSNotificationCenter::defaultCenter() };
809819
unsafe {

0 commit comments

Comments
 (0)