Skip to content

Commit

Permalink
Always set delegate on layers created by Wgpu
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 4, 2024
1 parent 9192c90 commit d6e3e30
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
20 changes: 6 additions & 14 deletions wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand All @@ -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"
))),
Expand Down
18 changes: 6 additions & 12 deletions wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object>,
delegate: Option<&HalManagedMetalLayerDelegate>,
) -> Self {
let layer = unsafe { Self::get_metal_layer(view, delegate) };
pub unsafe fn from_view(view: NonNull<Object>) -> 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`.
Expand All @@ -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<Object>,
delegate: Option<&HalManagedMetalLayerDelegate>,
) -> StrongPtr {
pub(crate) unsafe fn get_metal_layer(view: NonNull<Object>) -> 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.");
Expand Down Expand Up @@ -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) }
}
}
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/vulkan/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit d6e3e30

Please sign in to comment.