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 bd427b7 commit 9192c90
Show file tree
Hide file tree
Showing 2 changed files with 9 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 @@ -367,7 +367,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
29 changes: 9 additions & 20 deletions wgpu-hal/src/metal/surface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,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 @@ -85,16 +84,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 @@ -278,16 +275,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 @@ -319,21 +306,23 @@ 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
// attached to a view; in those cases, we need to set the scale
// factor ourselves.
//
// For AppKit, we do so by adding a delegate on the layer with the
// `layer:shouldInheritContentsScale:fromWindow:` method returning
// `true` - this tells the system to automatically update the scale
// factor when it changes.
//
// For UIKit, we manually update the scale factor here.
// For UIKit, we manually update the scale factor from the super layer
// here, if there is one.
//
// 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 9192c90

Please sign in to comment.