diff --git a/wgpu-hal/src/metal/mod.rs b/wgpu-hal/src/metal/mod.rs index cd9b824f7f..1935e843ec 100644 --- a/wgpu-hal/src/metal/mod.rs +++ b/wgpu-hal/src/metal/mod.rs @@ -96,9 +96,7 @@ crate::impl_dyn_resource!( TextureView ); -pub struct Instance { - managed_metal_layer_delegate: surface::HalManagedMetalLayerDelegate, -} +pub struct Instance {} impl Instance { pub fn create_surface_from_layer(&self, layer: &metal::MetalLayerRef) -> Surface { @@ -113,9 +111,7 @@ impl crate::Instance for Instance { profiling::scope!("Init Metal Backend"); // We do not enable metal validation based on the validation flags as it affects the entire // process. Instead, we enable the validation inside the test harness itself in tests/src/native.rs. - Ok(Instance { - managed_metal_layer_delegate: surface::HalManagedMetalLayerDelegate::new(), - }) + Ok(Instance {}) } unsafe fn create_surface( @@ -126,16 +122,12 @@ impl crate::Instance for Instance { match window_handle { #[cfg(target_os = "ios")] raw_window_handle::RawWindowHandle::UiKit(handle) => { - let _ = &self.managed_metal_layer_delegate; - Ok(unsafe { Surface::from_view(handle.ui_view.cast(), None) }) + Ok(unsafe { Surface::from_view(handle.ui_view.cast()) }) } #[cfg(target_os = "macos")] - raw_window_handle::RawWindowHandle::AppKit(handle) => Ok(unsafe { - Surface::from_view( - handle.ns_view.cast(), - Some(&self.managed_metal_layer_delegate), - ) - }), + raw_window_handle::RawWindowHandle::AppKit(handle) => { + Ok(unsafe { Surface::from_view(handle.ns_view.cast()) }) + } _ => Err(crate::InstanceError::new(format!( "window handle {window_handle:?} is not a Metal-compatible handle" ))), diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index 5e61084b52..ec117f17ce 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -75,11 +75,8 @@ 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: NonNull, - delegate: Option<&HalManagedMetalLayerDelegate>, - ) -> Self { - let layer = unsafe { Self::get_metal_layer(view, delegate) }; + pub unsafe fn from_view(view: NonNull) -> Self { + let layer = unsafe { Self::get_metal_layer(view) }; let layer = ManuallyDrop::new(layer); // SAFETY: The layer is an initialized instance of `CAMetalLayer`, and // we transfer the retain count to `MetalLayer` using `ManuallyDrop`. @@ -104,10 +101,7 @@ impl super::Surface { /// # Safety /// /// The `view` must be a valid instance of `NSView` or `UIView`. - pub(crate) unsafe fn get_metal_layer( - view: NonNull, - delegate: Option<&HalManagedMetalLayerDelegate>, - ) -> StrongPtr { + pub(crate) unsafe fn get_metal_layer(view: NonNull) -> StrongPtr { let is_main_thread: BOOL = msg_send![class!(NSThread), isMainThread]; if is_main_thread == NO { panic!("get_metal_layer cannot be called in non-ui thread."); @@ -251,9 +245,9 @@ impl super::Surface { let scale_factor: CGFloat = msg_send![root_layer, contentsScale]; let () = msg_send![new_layer, setContentsScale: scale_factor]; - if let Some(delegate) = delegate { - let () = msg_send![new_layer, setDelegate: delegate.0]; - } + let delegate = HalManagedMetalLayerDelegate::new(); + let () = msg_send![new_layer, setDelegate: delegate.0]; + unsafe { StrongPtr::new(new_layer) } } } diff --git a/wgpu-hal/src/vulkan/instance.rs b/wgpu-hal/src/vulkan/instance.rs index 7759d814d1..f52a055284 100644 --- a/wgpu-hal/src/vulkan/instance.rs +++ b/wgpu-hal/src/vulkan/instance.rs @@ -518,7 +518,7 @@ impl super::Instance { ))); } - let layer = unsafe { crate::metal::Surface::get_metal_layer(view.cast(), None) }; + let layer = unsafe { crate::metal::Surface::get_metal_layer(view.cast()) }; // NOTE: The layer is retained by Vulkan's `vkCreateMetalSurfaceEXT`, // so no need to retain it beyond the scope of this function. let layer_ptr = (*layer).cast();