Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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 // Pick up and persist requested color space from surface format
17 const QSurfaceFormat surfaceFormat = m_platformWindow->format();
18 if (QColorSpace colorSpace = surfaceFormat.colorSpace(); colorSpace.isValid()) {
19 NSData *iccData = colorSpace.iccProfile().toNSData();
20 self.colorSpace = [[[NSColorSpace alloc] initWithICCProfileData:iccData] autorelease];
21 }
22
23 // Trigger creation of the layer
24 self.wantsLayer = YES;
25}
26
27- (BOOL)isOpaque
28{
29 if (!m_platformWindow)
30 return true;
31 return m_platformWindow->isOpaque();
32}
33
34- (BOOL)isFlipped
35{
36 return YES;
37}
38
39- (NSColorSpace*)colorSpace
40{
41 // If no explicit color space was set, use the NSWindow's color space
42 return m_colorSpace ? m_colorSpace : self.window.colorSpace;
43}
44
45// ----------------------- Layer setup -----------------------
46
47- (BOOL)shouldUseMetalLayer
48{
49 // MetalSurface needs a layer, and so does VulkanSurface (via MoltenVK)
50 QSurface::SurfaceType surfaceType = m_platformWindow->window()->surfaceType();
51 return surfaceType == QWindow::MetalSurface || surfaceType == QWindow::VulkanSurface;
52}
53
54/*
55 This method is called by AppKit when layer-backing is requested by
56 setting wantsLayer too YES (via -[NSView _updateLayerBackedness]),
57 or in cases where AppKit itself decides that a view should be
58 layer-backed.
59
60 Note however that some code paths in AppKit will not go via this
61 method for creating the backing layer, and will instead create the
62 layer manually, and just call setLayer. An example of this is when
63 an NSOpenGLContext is attached to a view, in which case AppKit will
64 create a new layer in NSOpenGLContextSetLayerOnViewIfNecessary.
65
66 For this reason we leave the implementation of this override as
67 minimal as possible, only focusing on creating the appropriate
68 layer type, and then leave it up to setLayer to do the work of
69 making sure the layer is set up correctly.
70*/
71- (CALayer *)makeBackingLayer
72{
73 if ([self shouldUseMetalLayer]) {
74 // Check if Metal is supported. If it isn't then it's most likely
75 // too late at this point and the QWindow will be non-functional,
76 // but we can at least print a warning.
77 if ([MTLCreateSystemDefaultDevice() autorelease]) {
78 return [CAMetalLayer layer];
79 } else {
80 qCWarning(lcQpaDrawing) << "Failed to create QWindow::MetalSurface."
81 << "Metal is not supported by any of the GPUs in this system.";
82 }
83 }
84
85 return [super makeBackingLayer];
86}
87
88/*
89 This method is called by AppKit whenever the view is asked to change
90 its layer, which can happen both as a result of enabling layer-backing,
91 or when a layer is set explicitly. The latter can happen both when a
92 view is layer-hosting, or when AppKit internals are switching out the
93 layer-backed view, as described above for makeBackingLayer.
94*/
95- (void)setLayer:(CALayer *)layer
96{
97 qCDebug(lcQpaDrawing) << "Making" << self
98 << (self.wantsLayer ? "layer-backed" : "layer-hosted")
99 << "with" << layer;
100
101 if (layer.delegate && layer.delegate != self) {
102 qCWarning(lcQpaDrawing) << "Layer already has delegate" << layer.delegate
103 << "This delegate is responsible for all view updates for" << self;
104 } else {
105 layer.delegate = self;
106 }
107
108 [super setLayer:layer];
109
110 [self propagateBackingProperties];
111
112 if (self.opaque && lcQpaDrawing().isDebugEnabled()) {
113 // If the view claims to be opaque we expect it to fill the entire
114 // layer with content, in which case we want to detect any areas
115 // where it doesn't.
116 layer.backgroundColor = NSColor.magentaColor.CGColor;
117 }
118
119}
120
121// ----------------------- Layer updates -----------------------
122
123- (NSViewLayerContentsRedrawPolicy)layerContentsRedrawPolicy
124{
125 // We need to set this explicitly since the super implementation
126 // returns LayerContentsRedrawNever for custom layers like CAMetalLayer.
127 return NSViewLayerContentsRedrawDuringViewResize;
128}
129
130- (NSViewLayerContentsPlacement)layerContentsPlacement
131{
132 // Always place the layer at top left without any automatic scaling.
133 // This will highlight situations where we're missing content for the
134 // layer by not responding to the displayLayer: request synchronously.
135 // It also allows us to re-use larger layers when resizing a window down.
136 return NSViewLayerContentsPlacementTopLeft;
137}
138
139- (void)viewDidChangeBackingProperties
140{
141 qCDebug(lcQpaDrawing) << "Backing properties changed for" << self;
142
143 [self propagateBackingProperties];
144
145 // Ideally we would plumb this situation through QPA in a way that lets
146 // clients invalidate their own caches, recreate QBackingStore, etc.
147 // For now we trigger an expose, and let QCocoaBackingStore deal with
148 // buffer invalidation internally.
149 [self setNeedsDisplay:YES];
150}
151
152- (void)propagateBackingProperties
153{
154 if (!self.layer)
155 return;
156
157 // We expect clients to fill the layer with retina aware content,
158 // based on the devicePixelRatio of the QWindow, so we set the
159 // layer's content scale to match that. By going via devicePixelRatio
160 // instead of applying the NSWindow's backingScaleFactor, we also take
161 // into account OpenGL views with wantsBestResolutionOpenGLSurface set
162 // to NO. In this case the window will have a backingScaleFactor of 2,
163 // but the QWindow will have a devicePixelRatio of 1.
164 auto devicePixelRatio = m_platformWindow->devicePixelRatio();
165 qCDebug(lcQpaDrawing) << "Updating" << self.layer << "content scale to" << devicePixelRatio;
166 self.layer.contentsScale = devicePixelRatio;
167
168 if ([self.layer isKindOfClass:CAMetalLayer.class]) {
169 CAMetalLayer *metalLayer = static_cast<CAMetalLayer *>(self.layer);
170 metalLayer.colorspace = self.colorSpace.CGColorSpace;
171 qCDebug(lcQpaDrawing) << "Set" << metalLayer << "color space to" << metalLayer.colorspace;
172 }
173}
174
175/*
176 This method is called by AppKit to determine whether it should update
177 the contentScale of the layer to match the window backing scale.
178
179 We always return NO since we're updating the contents scale manually.
180*/
181- (BOOL)layer:(CALayer *)layer shouldInheritContentsScale:(CGFloat)scale fromWindow:(NSWindow *)window
182{
186 return NO;
187}
188
189// ----------------------- Draw callbacks -----------------------
190
191/*
192 This method is called by AppKit for the non-layer case, where we are
193 drawing into the NSWindow's surface.
194*/
195- (void)drawRect:(NSRect)dirtyBoundingRect
196{
197 Q_UNUSED(dirtyBoundingRect);
198 // As we are layer backed we shouldn't really end up here, but AppKit will
199 // in some cases call this method just because we implement it.
200 // FIXME: Remove drawRect and switch from displayLayer to updateLayer
201 qCWarning(lcQpaDrawing) << "[QNSView drawRect] called for layer backed view";
202}
203
204/*
205 This method is called by AppKit when we are layer-backed, where
206 we are drawing into the layer.
207*/
208- (void)displayLayer:(CALayer *)layer
209{
210 Q_ASSERT_X(self.layer && layer == self.layer, "QNSView",
211 "The displayLayer code path should only be hit for our own layer");
212
213 if (!m_platformWindow)
214 return;
215
216 if (!NSThread.isMainThread) {
217 // Qt is calling AppKit APIs such as -[NSOpenGLContext setView:] on secondary threads,
218 // which we shouldn't do. This may result in AppKit (wrongly) triggering a display on
219 // the thread where we made the call, so block it here and defer to the main thread.
220 qCWarning(lcQpaDrawing) << "Display non non-main thread! Deferring to main thread";
221 dispatch_async(dispatch_get_main_queue(), ^{ self.needsDisplay = YES; });
222 return;
223 }
224
225 qCDebug(lcQpaDrawing) << "[QNSView displayLayer]" << m_platformWindow->window();
226 m_platformWindow->handleExposeEvent(QRectF::fromCGRect(self.bounds).toRect());
227}
228
229@end
The QColorSpace class provides a color space abstraction.
Definition qcolorspace.h:21
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
const QColorSpace & colorSpace() const
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:58
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]