7@implementation QContainerLayer {
8 CALayer *m_contentLayer;
10- (instancetype)initWithContentLayer:(CALayer *)contentLayer
12 if ((self = [super init])) {
13 m_contentLayer = contentLayer;
14 [self addSublayer:contentLayer];
19- (CALayer*)contentLayer
21 return m_contentLayer;
24- (
void)layoutSublayers
30 m_contentLayer.frame = self.bounds;
33- (
void)setNeedsDisplay
35 [self setNeedsDisplayInRect:CGRectInfinite];
38- (
void)setNeedsDisplayInRect:(CGRect)rect
40 [super setNeedsDisplayInRect:rect];
41 [self.contentLayer setNeedsDisplayInRect:rect];
45@implementation QNSView (Drawing)
50 const QSurfaceFormat surfaceFormat = m_platformWindow->format();
51 if (QColorSpace colorSpace = surfaceFormat.colorSpace(); colorSpace.isValid()) {
52 NSData *iccData = colorSpace.iccProfile().toNSData();
53 self.colorSpace = [[[NSColorSpace alloc] initWithICCProfileData:iccData] autorelease];
57 self.wantsLayer = YES;
62 if (!m_platformWindow)
64 return m_platformWindow->isOpaque();
72- (NSColorSpace*)colorSpace
75 return m_colorSpace ? m_colorSpace : self.window.colorSpace;
80- (BOOL)shouldUseMetalLayer
82 if (!m_platformWindow)
86 QSurface::SurfaceType surfaceType = m_platformWindow->window()->surfaceType();
87 return surfaceType == QWindow::MetalSurface || surfaceType == QWindow::VulkanSurface;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107- (CALayer *)makeBackingLayer
109 if ([self shouldUseMetalLayer]) {
113 if ([MTLCreateSystemDefaultDevice() autorelease]) {
114 static bool allowPresentsWithTransaction =
115 !qEnvironmentVariableIsSet(
"QT_MTL_NO_TRANSACTION");
116 return allowPresentsWithTransaction ?
117 [QMetalLayer layer] : [CAMetalLayer layer];
119 qCWarning(lcQpaDrawing) <<
"Failed to create QWindow::MetalSurface."
120 <<
"Metal is not supported by any of the GPUs in this system.";
129 return [CALayer layer];
133
134
135
136
137
138
139- (
void)setLayer:(CALayer *)layer
141 if (!m_platformWindow) {
142 [super setLayer:layer];
146 qCDebug(lcQpaDrawing) <<
"Making" << self
147 << (self.wantsLayer ?
"layer-backed" :
"layer-hosted")
150 if (layer.delegate && layer.delegate != self) {
151 qCWarning(lcQpaDrawing) <<
"Layer already has delegate" << layer.delegate
152 <<
"This delegate is responsible for all view updates for" << self;
154 layer.delegate = self;
157 layer.name = @
"Qt content layer";
159 static const bool containerLayerOptOut = qEnvironmentVariableIsSet(
"QT_MAC_NO_CONTAINER_LAYER");
160 if (m_platformWindow->window()->surfaceType() != QSurface::OpenGLSurface && !containerLayerOptOut) {
161 qCDebug(lcQpaDrawing) <<
"Wrapping content layer" << layer <<
"in container layer";
162 auto *containerLayer = [[[QContainerLayer alloc] initWithContentLayer:layer] autorelease];
163 containerLayer.name = @
"Qt container layer";
164 containerLayer.delegate = self;
165 layer = containerLayer;
168 [super setLayer:layer];
170 [self propagateBackingProperties];
172 if (self.opaque && lcQpaDrawing().isDebugEnabled()) {
176 layer.backgroundColor = NSColor.magentaColor.CGColor;
182- (NSViewLayerContentsRedrawPolicy)layerContentsRedrawPolicy
186 return NSViewLayerContentsRedrawDuringViewResize;
189- (NSViewLayerContentsPlacement)layerContentsPlacement
195 return NSViewLayerContentsPlacementTopLeft;
198- (
void)viewDidChangeBackingProperties
200 qCDebug(lcQpaDrawing) <<
"Backing properties changed for" << self;
202 if (!m_platformWindow)
205 [self propagateBackingProperties];
214 QWindowSystemInterface::handleWindowDevicePixelRatioChanged
215 <QWindowSystemInterface::SynchronousDelivery>(m_platformWindow->window());
219 [self setNeedsDisplay:YES];
222- (
void)propagateBackingProperties
234 auto devicePixelRatio = m_platformWindow->devicePixelRatio();
235 auto *contentLayer = m_platformWindow->contentLayer();
236 qCDebug(lcQpaDrawing) <<
"Updating" << contentLayer <<
"content scale to" << devicePixelRatio;
237 contentLayer.contentsScale = devicePixelRatio;
239 if ([contentLayer isKindOfClass:CAMetalLayer.
class]) {
240 CAMetalLayer *metalLayer =
static_cast<CAMetalLayer *>(contentLayer);
241 metalLayer.colorspace = self.colorSpace.CGColorSpace;
242 qCDebug(lcQpaDrawing) <<
"Set" << metalLayer <<
"color space to" << metalLayer.colorspace;
247
248
249
250
251
252- (BOOL)layer:(CALayer *)layer shouldInheritContentsScale:(CGFloat)scale fromWindow:(NSWindow *)window
263
264
265
266
267- (
void)displayLayer:(CALayer *)layer
269 if (
auto *containerLayer = qt_objc_cast<QContainerLayer*>(layer)) {
270 qCDebug(lcQpaDrawing) <<
"Skipping display of" << containerLayer
271 <<
"as display is handled by content layer" << containerLayer.contentLayer;
275 if (!m_platformWindow)
278 auto *qtMetalLayer = qt_objc_cast<QMetalLayer *>(layer);
287 QMetaObject::invokeMethod(m_platformWindow, [qtMetalLayer]{
288 qCDebug(lcMetalLayer) <<
"Unlocking" << qtMetalLayer <<
"after finishing display-cycle";
289 qtMetalLayer.displayLock.unlock();
290 }, Qt::QueuedConnection);
293 if (!NSThread.isMainThread) {
297 qCWarning(lcQpaDrawing) <<
"Display non non-main thread! Deferring to main thread";
298 dispatch_async(dispatch_get_main_queue(), ^{ self.needsDisplay = YES; });
302 if (m_platformWindow->m_deliveringUpdateRequest) {
307 qCWarning(lcQpaDrawing) <<
"Asked to display during update-request delivery. Deferring.";
308 dispatch_async(dispatch_get_main_queue(), ^{ self.needsDisplay = YES; });
312 auto *currentScreen =
static_cast<QCocoaScreen*>(m_platformWindow->screen());
313 if (!currentScreen || !currentScreen->isOnline()) {
314 qCWarning(lcQpaDrawing) <<
"Display requested for non-online display" << currentScreen
315 <<
"Deferring to next runloop pass";
316 dispatch_async(dispatch_get_main_queue(), ^{ self.needsDisplay = YES; });
320 const auto handleExposeEvent = [&]{
321 const auto bounds = QRectF::fromCGRect(self.bounds).toRect();
322 qCDebug(lcQpaDrawing) <<
"[QNSView displayLayer]" << m_platformWindow->window() << bounds;
323 m_platformWindow->handleExposeEvent(bounds);
327 const bool presentedWithTransaction = qtMetalLayer.presentsWithTransaction;
328 qtMetalLayer.presentsWithTransaction = YES;
340 QMacAutoReleasePool pool;
344 if (
auto mainThreadPresentation = qtMetalLayer.mainThreadPresentation) {
345 mainThreadPresentation();
346 qtMetalLayer.mainThreadPresentation = nil;
350 qtMetalLayer.presentsWithTransaction = presentedWithTransaction;