2026-06-02 13:45:42 +10:00
|
|
|
/**
|
|
|
|
|
* Objective-C++ implementation of the Cocoa bridge declared in
|
2026-06-04 11:11:14 +10:00
|
|
|
* MetalSurface_mac.h. Compiled only on macOS.
|
2026-06-02 13:45:42 +10:00
|
|
|
*/
|
|
|
|
|
|
2026-06-04 11:11:14 +10:00
|
|
|
#include "MetalSurface_mac.h"
|
2026-06-02 13:45:42 +10:00
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
|
2026-06-02 14:25:06 +10:00
|
|
|
// The AppKit umbrella header pulls in NSWindow so `view.window`'s
|
|
|
|
|
// `backingScaleFactor` resolves — `<AppKit/NSView.h>` alone only
|
|
|
|
|
// forward-declares NSWindow.
|
|
|
|
|
#import <AppKit/AppKit.h>
|
2026-06-02 13:45:42 +10:00
|
|
|
#import <QuartzCore/CAMetalLayer.h>
|
|
|
|
|
|
|
|
|
|
void* wgpu_macos_attach_metal_layer(void* nsview_ptr) {
|
|
|
|
|
if (!nsview_ptr) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
NSView* view = (__bridge NSView*)nsview_ptr;
|
|
|
|
|
|
|
|
|
|
// When QWindow::surfaceType is QSurface::MetalSurface, Qt already
|
|
|
|
|
// backs the NSView with a CAMetalLayer — just hand it back. Otherwise
|
|
|
|
|
// attach one ourselves (defensive: Qt's behaviour can change between
|
|
|
|
|
// major versions and 6.x has occasionally regressed this).
|
|
|
|
|
CAMetalLayer* layer = nil;
|
|
|
|
|
if ([view.layer isKindOfClass:[CAMetalLayer class]]) {
|
|
|
|
|
layer = (CAMetalLayer*)view.layer;
|
|
|
|
|
} else {
|
|
|
|
|
layer = [CAMetalLayer layer];
|
|
|
|
|
view.wantsLayer = YES;
|
|
|
|
|
view.layer = layer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Track the screen's backing scale so we get retina-resolution
|
|
|
|
|
// drawables. wgpu's surface configure picks the drawable size up
|
|
|
|
|
// from layer.drawableSize at present-time.
|
|
|
|
|
if (view.window) {
|
|
|
|
|
layer.contentsScale = view.window.backingScaleFactor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (__bridge void*)layer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // __APPLE__
|