Skip to content

Commit ff9d88f

Browse files
committed
[metal] Do not keep the view around that the surface was created from
We do not need to use it, and the layer itself is already retained, so it won't be de-allocated from under our feet.
1 parent 3a410a3 commit ff9d88f

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

wgpu-hal/src/metal/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,6 @@ pub struct Device {
342342
}
343343

344344
pub struct Surface {
345-
view: Option<NonNull<objc::runtime::Object>>,
346345
render_layer: Mutex<metal::MetalLayer>,
347346
swapchain_format: RwLock<Option<wgt::TextureFormat>>,
348347
extent: RwLock<wgt::Extent3d>,

wgpu-hal/src/metal/surface.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ impl HalManagedMetalLayerDelegate {
6464
}
6565

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

9491
pub unsafe fn from_layer(layer: &metal::MetalLayerRef) -> Self {
9592
let class = class!(CAMetalLayer);
9693
let proper_kind: BOOL = msg_send![layer, isKindOfClass: class];
9794
assert_eq!(proper_kind, YES);
98-
Self::new(None, layer.to_owned())
95+
Self::new(layer.to_owned())
9996
}
10097

10198
/// Get or create a new `CAMetalLayer` associated with the given `NSView`
@@ -279,16 +276,6 @@ impl super::Surface {
279276
}
280277
}
281278

282-
impl Drop for super::Surface {
283-
fn drop(&mut self) {
284-
if let Some(view) = self.view {
285-
unsafe {
286-
let () = msg_send![view.as_ptr(), release];
287-
}
288-
}
289-
}
290-
}
291-
292279
impl crate::Surface for super::Surface {
293280
type A = super::Api;
294281

@@ -320,8 +307,8 @@ impl crate::Surface for super::Surface {
320307

321308
// AppKit / UIKit automatically sets the correct scale factor for
322309
// layers attached to a view. Our layer, however, may not be directly
323-
// attached to the view; in those cases, we need to set the scale
324-
// factor ourselves.
310+
// attached to a view; in those cases, we need to set the scale
311+
// factor ourselves from the super layer, if there is one.
325312
//
326313
// For AppKit, we do so by adding a delegate on the layer with the
327314
// `layer:shouldInheritContentsScale:fromWindow:` method returning
@@ -333,8 +320,9 @@ impl crate::Surface for super::Surface {
333320
// TODO: Is there a way that we could listen to such changes instead?
334321
#[cfg(not(target_os = "macos"))]
335322
{
336-
if let Some(view) = self.view {
337-
let scale_factor: CGFloat = msg_send![view.as_ptr(), contentScaleFactor];
323+
let superlayer: *mut Object = msg_send![render_layer.as_ptr(), superlayer];
324+
if !superlayer.is_null() {
325+
let scale_factor: CGFloat = msg_send![superlayer, contentsScale];
338326
let () = msg_send![render_layer.as_ptr(), setContentsScale: scale_factor];
339327
}
340328
}

0 commit comments

Comments
 (0)