@@ -34,7 +34,7 @@ use std::{
3434
3535use arrayvec:: ArrayVec ;
3636use bitflags:: bitflags;
37- use metal:: foreign_types:: ForeignTypeRef as _;
37+ use metal:: foreign_types:: { ForeignType as _ , ForeignTypeRef as _} ;
3838use parking_lot:: { Mutex , RwLock } ;
3939
4040#[ derive( Clone , Debug ) ]
@@ -100,7 +100,7 @@ pub struct Instance {}
100100
101101impl Instance {
102102 pub fn create_surface_from_layer ( & self , layer : & metal:: MetalLayerRef ) -> Surface {
103- unsafe { Surface :: from_layer ( layer) }
103+ Surface :: from_layer ( layer)
104104 }
105105}
106106
@@ -119,19 +119,25 @@ impl crate::Instance for Instance {
119119 _display_handle : raw_window_handle:: RawDisplayHandle ,
120120 window_handle : raw_window_handle:: RawWindowHandle ,
121121 ) -> Result < Surface , crate :: InstanceError > {
122- match window_handle {
123- #[ cfg( target_os = "ios" ) ]
124- raw_window_handle:: RawWindowHandle :: UiKit ( handle) => {
125- Ok ( unsafe { Surface :: from_view ( handle. ui_view . cast ( ) ) } )
122+ let layer = match window_handle {
123+ raw_window_handle:: RawWindowHandle :: AppKit ( handle) => unsafe {
124+ raw_window_metal:: Layer :: from_ns_view ( handle. ns_view )
125+ } ,
126+ raw_window_handle:: RawWindowHandle :: UiKit ( handle) => unsafe {
127+ raw_window_metal:: Layer :: from_ui_view ( handle. ui_view )
128+ } ,
129+ _ => {
130+ return Err ( crate :: InstanceError :: new ( format ! (
131+ "window handle {window_handle:?} is not a Metal-compatible handle"
132+ ) ) )
126133 }
127- #[ cfg( target_os = "macos" ) ]
128- raw_window_handle:: RawWindowHandle :: AppKit ( handle) => {
129- Ok ( unsafe { Surface :: from_view ( handle. ns_view . cast ( ) ) } )
130- }
131- _ => Err ( crate :: InstanceError :: new ( format ! (
132- "window handle {window_handle:?} is not a Metal-compatible handle"
133- ) ) ) ,
134- }
134+ } ;
135+
136+ // SAFETY: The layer is an initialized instance of `CAMetalLayer`, and
137+ // we transfer the retain count to `MetalLayer` using `into_raw`.
138+ let layer = unsafe { metal:: MetalLayer :: from_ptr ( layer. into_raw ( ) . cast ( ) . as_ptr ( ) ) } ;
139+
140+ Ok ( Surface :: new ( layer) )
135141 }
136142
137143 unsafe fn enumerate_adapters (
0 commit comments