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 need to 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 ff9d88f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 21 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
28 changes: 8 additions & 20 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 Expand Up @@ -320,8 +307,8 @@ impl crate::Surface for super::Surface {

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

0 comments on commit ff9d88f

Please sign in to comment.