Skip to content

Commit

Permalink
[metal] Do not keep the view around that the surface was created from
Browse files Browse the repository at this point in the history
We do not use it, and the layer itself is already retained, so it won't
be de-allocated from under our feet.
  • Loading branch information
madsmtm committed Aug 25, 2024
1 parent 3a410a3 commit e337fdb
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
1 change: 0 additions & 1 deletion wgpu-hal/src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ pub struct Device {
}

pub struct Surface {
view: Option<NonNull<objc::runtime::Object>>,
render_layer: Mutex<metal::MetalLayer>,
swapchain_format: RwLock<Option<wgt::TextureFormat>>,
extent: RwLock<wgt::Extent3d>,
Expand Down
19 changes: 3 additions & 16 deletions wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ impl HalManagedMetalLayerDelegate {
}

impl super::Surface {
fn new(view: Option<NonNull<Object>>, layer: metal::MetalLayer) -> Self {
fn new(layer: metal::MetalLayer) -> Self {
Self {
view,
render_layer: Mutex::new(layer),
swapchain_format: RwLock::new(None),
extent: RwLock::new(wgt::Extent3d::default()),
Expand All @@ -86,16 +85,14 @@ impl super::Surface {
// SAFETY: The layer is an initialized instance of `CAMetalLayer`, and
// we transfer the retain count to `MetalLayer` using `ManuallyDrop`.
let layer = unsafe { metal::MetalLayer::from_ptr(layer.cast()) };
let view: *mut Object = msg_send![view.as_ptr(), retain];
let view = NonNull::new(view).expect("retain should return the same object");
Self::new(Some(view), layer)
Self::new(layer)
}

pub unsafe fn from_layer(layer: &metal::MetalLayerRef) -> Self {
let class = class!(CAMetalLayer);
let proper_kind: BOOL = msg_send![layer, isKindOfClass: class];
assert_eq!(proper_kind, YES);
Self::new(None, layer.to_owned())
Self::new(layer.to_owned())
}

/// Get or create a new `CAMetalLayer` associated with the given `NSView`
Expand Down Expand Up @@ -279,16 +276,6 @@ impl super::Surface {
}
}

impl Drop for super::Surface {
fn drop(&mut self) {
if let Some(view) = self.view {
unsafe {
let () = msg_send![view.as_ptr(), release];
}
}
}
}

impl crate::Surface for super::Surface {
type A = super::Api;

Expand Down

0 comments on commit e337fdb

Please sign in to comment.