Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qnsview_drawing.mm
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4// This file is included from qnsview.mm, and only used to organize the code
5
6@implementation QNSView (Drawing)
7
9{
10 if (qt_mac_resolveOption(-1, m_platformWindow->window(),
11 "_q_mac_wantsLayer", "QT_MAC_WANTS_LAYER") != -1) {
12 qCWarning(lcQpaDrawing) << "Layer-backing is always enabled."
13 << " QT_MAC_WANTS_LAYER/_q_mac_wantsLayer has no effect.";
14 }
15
16 self.wantsLayer = YES;
17}
18
19- (BOOL)isOpaque
20{
21 if (!m_platformWindow)
22 return true;
23 return m_platformWindow->isOpaque();
24}
25
26- (BOOL)isFlipped
27{
28 return YES;
29}
30
31// ----------------------- Layer setup -----------------------
32
33- (BOOL)shouldUseMetalLayer
34{
35 // MetalSurface needs a layer, and so does VulkanSurface (via MoltenVK)
36 QSurface::SurfaceType surfaceType = m_platformWindow->window()->surfaceType();
37 return surfaceType == QWindow::MetalSurface || surfaceType == QWindow::VulkanSurface;
38}
39
40/*
41 This method is called by AppKit when layer-backing is requested by
42 setting wantsLayer too YES (via -[NSView _updateLayerBackedness]),
43 or in cases where AppKit itself decides that a view should be
44 layer-backed.
45
46 Note however that some code paths in AppKit will not go via this
47 method for creating the backing layer, and will instead create the
48 layer manually, and just call setLayer. An example of this is when
49 an NSOpenGLContext is attached to a view, in which case AppKit will
50 create a new layer in NSOpenGLContextSetLayerOnViewIfNecessary.
51
52 For this reason we leave the implementation of this override as
53 minimal as possible, only focusing on creating the appropriate
54 layer type, and then leave it up to setLayer to do the work of
55 making sure the layer is set up correctly.
56*/
57- (CALayer *)makeBackingLayer
58{
59 if ([self shouldUseMetalLayer]) {
60 // Check if Metal is supported. If it isn't then it's most likely
61 // too late at this point and the QWindow will be non-functional,
62 // but we can at least print a warning.
63 if ([MTLCreateSystemDefaultDevice() autorelease]) {
64 return [CAMetalLayer layer];
65 } else {
66 qCWarning(lcQpaDrawing) << "Failed to create QWindow::MetalSurface."
67 << "Metal is not supported by any of the GPUs in this system.";
68 }
69 }
70
71 return [super makeBackingLayer];
72}
73
74/*
75 This method is called by AppKit whenever the view is asked to change
76 its layer, which can happen both as a result of enabling layer-backing,
77 or when a layer is set explicitly. The latter can happen both when a
78 view is layer-hosting, or when AppKit internals are switching out the
79 layer-backed view, as described above for makeBackingLayer.
80*/
81- (void)setLayer:(CALayer *)layer
82{
83 qCDebug(lcQpaDrawing) << "Making" << self
84 << (self.wantsLayer ? "layer-backed" : "layer-hosted")
85 << "with" << layer;
86
87 if (layer.delegate && layer.delegate != self) {
88 qCWarning(lcQpaDrawing) << "Layer already has delegate" << layer.delegate
89 << "This delegate is responsible for all view updates for" << self;
90 } else {
91 layer.delegate = self;
92 }
93
94 [super setLayer:layer];
95
96 // When adding a view to a view hierarchy the backing properties will change
97 // which results in updating the contents scale, but in case of switching the
98 // layer on a view that's already in a view hierarchy we need to manually ensure
99 // the scale is up to date.
100 if (self.superview)
101 [self updateLayerContentsScale];
102
103 if (self.opaque && lcQpaDrawing().isDebugEnabled()) {
104 // If the view claims to be opaque we expect it to fill the entire
105 // layer with content, in which case we want to detect any areas
106 // where it doesn't.
107 layer.backgroundColor = NSColor.magentaColor.CGColor;
108 }
109
110}
111
112// ----------------------- Layer updates -----------------------
113
114- (NSViewLayerContentsRedrawPolicy)layerContentsRedrawPolicy
115{
116 // We need to set this explicitly since the super implementation
117 // returns LayerContentsRedrawNever for custom layers like CAMetalLayer.
118 return NSViewLayerContentsRedrawDuringViewResize;
119}
120
121- (NSViewLayerContentsPlacement)layerContentsPlacement
122{
123 // Always place the layer at top left without any automatic scaling.
124 // This will highlight situations where we're missing content for the
125 // layer by not responding to the displayLayer: request synchronously.
126 // It also allows us to re-use larger layers when resizing a window down.
127 return NSViewLayerContentsPlacementTopLeft;
128}
129
130- (void)viewDidChangeBackingProperties
131{
132 qCDebug(lcQpaDrawing) << "Backing properties changed for" << self;
133
134 if (self.layer)
135 [self updateLayerContentsScale];
136
137 // Ideally we would plumb this situation through QPA in a way that lets
138 // clients invalidate their own caches, recreate QBackingStore, etc.
139 // For now we trigger an expose, and let QCocoaBackingStore deal with
140 // buffer invalidation internally.
141 [self setNeedsDisplay:YES];
142}
143
144- (void)updateLayerContentsScale
145{
146 // We expect clients to fill the layer with retina aware content,
147 // based on the devicePixelRatio of the QWindow, so we set the
148 // layer's content scale to match that. By going via devicePixelRatio
149 // instead of applying the NSWindow's backingScaleFactor, we also take
150 // into account OpenGL views with wantsBestResolutionOpenGLSurface set
151 // to NO. In this case the window will have a backingScaleFactor of 2,
152 // but the QWindow will have a devicePixelRatio of 1.
153 auto devicePixelRatio = m_platformWindow->devicePixelRatio();
154 qCDebug(lcQpaDrawing) << "Updating" << self.layer << "content scale to" << devicePixelRatio;
155 self.layer.contentsScale = devicePixelRatio;
156}
157
158/*
159 This method is called by AppKit to determine whether it should update
160 the contentScale of the layer to match the window backing scale.
161
162 We always return NO since we're updating the contents scale manually.
163*/
164- (BOOL)layer:(CALayer *)layer shouldInheritContentsScale:(CGFloat)scale fromWindow:(NSWindow *)window
165{
169 return NO;
170}
171
172// ----------------------- Draw callbacks -----------------------
173
174/*
175 This method is called by AppKit for the non-layer case, where we are
176 drawing into the NSWindow's surface.
177*/
178- (void)drawRect:(NSRect)dirtyBoundingRect
179{
180 Q_UNUSED(dirtyBoundingRect);
181 // As we are layer backed we shouldn't really end up here, but AppKit will
182 // in some cases call this method just because we implement it.
183 // FIXME: Remove drawRect and switch from displayLayer to updateLayer
184 qCWarning(lcQpaDrawing) << "[QNSView drawRect] called for layer backed view";
185}
186
187/*
188 This method is called by AppKit when we are layer-backed, where
189 we are drawing into the layer.
190*/
191- (void)displayLayer:(CALayer *)layer
192{
193 Q_ASSERT_X(self.layer && layer == self.layer, "QNSView",
194 "The displayLayer code path should only be hit for our own layer");
195
196 if (!m_platformWindow)
197 return;
198
199 if (!NSThread.isMainThread) {
200 // Qt is calling AppKit APIs such as -[NSOpenGLContext setView:] on secondary threads,
201 // which we shouldn't do. This may result in AppKit (wrongly) triggering a display on
202 // the thread where we made the call, so block it here and defer to the main thread.
203 qCWarning(lcQpaDrawing) << "Display non non-main thread! Deferring to main thread";
204 dispatch_async(dispatch_get_main_queue(), ^{ self.needsDisplay = YES; });
205 return;
206 }
207
208 qCDebug(lcQpaDrawing) << "[QNSView displayLayer]" << m_platformWindow->window();
209 m_platformWindow->handleExposeEvent(QRectF::fromCGRect(self.bounds).toRect());
210}
211
212@end
SurfaceType
The SurfaceType enum describes what type of surface this is.
Definition qsurface.h:30
@ MetalSurface
Definition qsurface.h:36
@ VulkanSurface
Definition qsurface.h:35
QString self
Definition language.cpp:57
float CGFloat
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
EGLOutputLayerEXT layer
#define qCWarning(category,...)
#define qCDebug(category,...)
GLenum GLenum GLenum GLenum GLenum scale
#define Q_ASSERT_X(cond, x, msg)
Definition qrandom.cpp:48
#define Q_UNUSED(x)
aWidget window() -> setWindowTitle("New Window Title")
[2]