Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgabstractrenderer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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
5
7
8/*!
9 \class QSGAbstractRenderer
10 \brief QSGAbstractRenderer gives access to the scene graph nodes and rendering.
11 \inmodule QtQuick
12 \since 5.4
13 \internal
14 */
15
16/*!
17 \enum QSGAbstractRenderer::MatrixTransformFlag
18
19 Used with setProjectionMatrixToRect() to indicate the expectations towards
20 the generated projection matrix.
21
22 \value MatrixTransformFlipY The traditional assumption in Qt Quick is that
23 Y points up in the normalized device coordinate system. There is at least
24 one modern graphics API where this is not the case (Vulkan). This flag can
25 then be used to get a projection that is appropriate for such an API.
26
27 \sa setProjectionMatrixToRect()
28
29 \since 5.14
30 */
31
32/*!
33 \fn void QSGAbstractRenderer::renderScene()
34
35 Renders the scene.
36 */
37
38/*!
39 \fn void QSGAbstractRenderer::sceneGraphChanged()
40
41 This signal is emitted on the first modification of a node in
42 the tree after the last scene render.
43 */
44
45/*!
46 \internal
47 */
48QSGAbstractRendererPrivate::QSGAbstractRendererPrivate()
49 : m_root_node(nullptr)
50 , m_clear_color(Qt::transparent)
51 , m_invertFrontFace(false)
52{
53 m_projection_matrix.resize(1);
54 m_projection_matrix_native_ndc.resize(1);
55}
56
57/*!
58 \internal
59 */
60QSGAbstractRenderer::QSGAbstractRenderer(QObject *parent)
61 : QObject(*new QSGAbstractRendererPrivate, parent)
62{
63}
64
65/*!
66 \internal
67 */
68QSGAbstractRenderer::~QSGAbstractRenderer()
69{
70}
71
72/*!
73 Sets the \a node as the root of the QSGNode scene
74 that you want to render. You need to provide a \a node
75 before trying to render the scene.
76
77 \note This doesn't take ownership of \a node.
78
79 \sa rootNode()
80*/
81void QSGAbstractRenderer::setRootNode(QSGRootNode *node)
82{
83 Q_D(QSGAbstractRenderer);
84 if (d->m_root_node == node)
85 return;
86 if (d->m_root_node) {
87 d->m_root_node->m_renderers.removeOne(this);
88 nodeChanged(d->m_root_node, QSGNode::DirtyNodeRemoved);
89 }
90 d->m_root_node = node;
91 if (d->m_root_node) {
92 Q_ASSERT(!d->m_root_node->m_renderers.contains(this));
93 d->m_root_node->m_renderers << this;
94 nodeChanged(d->m_root_node, QSGNode::DirtyNodeAdded);
95 }
96}
97
98/*!
99 Returns the root of the QSGNode scene.
100
101 \sa setRootNode()
102*/
103QSGRootNode *QSGAbstractRenderer::rootNode() const
104{
105 Q_D(const QSGAbstractRenderer);
106 return d->m_root_node;
107}
108
109
110/*!
111 \fn void QSGAbstractRenderer::setDeviceRect(const QSize &size)
112 \overload
113
114 Sets the \a size of the surface being rendered to.
115
116 \sa deviceRect()
117 */
118
119/*!
120 Sets \a rect as the geometry of the surface being rendered to.
121
122 \sa deviceRect()
123 */
124void QSGAbstractRenderer::setDeviceRect(const QRect &rect)
125{
126 Q_D(QSGAbstractRenderer);
127 d->m_device_rect = rect;
128}
129
130/*!
131 Returns the device rect of the surface being rendered to.
132
133 \sa setDeviceRect()
134 */
135QRect QSGAbstractRenderer::deviceRect() const
136{
137 Q_D(const QSGAbstractRenderer);
138 return d->m_device_rect;
139}
140
141/*!
142 \fn void QSGAbstractRenderer::setViewportRect(const QSize &size)
143 \overload
144
145 Sets the \a size of the viewport to render
146 on the surface.
147
148 \sa viewportRect()
149 */
150
151/*!
152 Sets \a rect as the geometry of the viewport to render
153 on the surface.
154
155 \sa viewportRect()
156 */
157void QSGAbstractRenderer::setViewportRect(const QRect &rect)
158{
159 Q_D(QSGAbstractRenderer);
160 d->m_viewport_rect = rect;
161}
162
163/*!
164 Returns the rect of the viewport to render.
165
166 \sa setViewportRect()
167 */
168QRect QSGAbstractRenderer::viewportRect() const
169{
170 Q_D(const QSGAbstractRenderer);
171 return d->m_viewport_rect;
172}
173
174/*!
175 Convenience method that calls setProjectionMatrix() with an
176 orthographic matrix generated from \a rect.
177
178 \note This function assumes that the graphics API uses Y up in its
179 normalized device coordinate system.
180
181 \sa setProjectionMatrix(), projectionMatrix()
182 */
183void QSGAbstractRenderer::setProjectionMatrixToRect(const QRectF &rect)
184{
185 setProjectionMatrixToRect(rect, {}, false);
186}
187
188/*!
189 Convenience method that calls setProjectionMatrix() with an
190 orthographic matrix generated from \a rect.
191
192 Set MatrixTransformFlipY in \a flags when the graphics API uses Y down in
193 its normalized device coordinate system (for example, Vulkan).
194
195 \sa setProjectionMatrix(), projectionMatrix()
196
197 \since 5.14
198 */
199void QSGAbstractRenderer::setProjectionMatrixToRect(const QRectF &rect, MatrixTransformFlags flags)
200{
201 setProjectionMatrixToRect(rect, flags, flags.testFlag(MatrixTransformFlipY));
202}
203
204/*!
205 Convenience method that calls setProjectionMatrix() with an
206 orthographic matrix generated from \a rect.
207
208 Set MatrixTransformFlipY in \a flags when the graphics API uses Y down in
209 its normalized device coordinate system (for example, Vulkan).
210
211 Convenience method that calls setProjectionMatrixWithNativeNDC() with an
212 orthographic matrix generated from \a rect.
213
214 Set true to \a nativeNDCFlipY to flip the Y axis relative to
215 projection matrix in its normalized device coordinate system.
216
217 \sa setProjectionMatrix(), projectionMatrix()
218 \sa setProjectionMatrixWithNativeNDC(), projectionMatrixWithNativeNDC()
219
220 \since 6.7
221 */
222void QSGAbstractRenderer::setProjectionMatrixToRect(const QRectF &rect, MatrixTransformFlags flags,
223 bool nativeNDCFlipY)
224{
225 const bool flipY = flags.testFlag(MatrixTransformFlipY);
226
227 const float left = rect.x();
228 const float right = rect.x() + rect.width();
229 float bottom = rect.y() + rect.height();
230 float top = rect.y();
231
232 if (flipY)
233 std::swap(top, bottom);
234
235 QMatrix4x4 matrix;
236 matrix.ortho(left, right, bottom, top, 1, -1);
237 setProjectionMatrix(matrix, 0);
238
239 if (nativeNDCFlipY) {
240 std::swap(top, bottom);
241
242 matrix.setToIdentity();
243 matrix.ortho(left, right, bottom, top, 1, -1);
244 }
245 setProjectionMatrixWithNativeNDC(matrix, 0);
246}
247
248/*!
249 Use \a matrix to project the QSGNode coordinates onto surface pixels.
250
251 \a index specifies the view index when multiview rendering is in use.
252
253 \sa projectionMatrix(), setProjectionMatrixToRect()
254 */
255void QSGAbstractRenderer::setProjectionMatrix(const QMatrix4x4 &matrix, int index)
256{
257 Q_D(QSGAbstractRenderer);
258 if (d->m_projection_matrix.count() <= index)
259 d->m_projection_matrix.resize(index + 1);
260 d->m_projection_matrix[index] = matrix;
261}
262
263/*!
264 \internal
265 */
266void QSGAbstractRenderer::setProjectionMatrixWithNativeNDC(const QMatrix4x4 &matrix, int index)
267{
268 Q_D(QSGAbstractRenderer);
269 if (d->m_projection_matrix_native_ndc.count() <= index)
270 d->m_projection_matrix_native_ndc.resize(index + 1);
271 d->m_projection_matrix_native_ndc[index] = matrix;
272}
273
274/*!
275 Returns the projection matrix
276
277 \sa setProjectionMatrix(), setProjectionMatrixToRect()
278 */
279QMatrix4x4 QSGAbstractRenderer::projectionMatrix(int index) const
280{
281 Q_D(const QSGAbstractRenderer);
282 return d->m_projection_matrix[index];
283}
284
285int QSGAbstractRenderer::projectionMatrixCount() const
286{
287 Q_D(const QSGAbstractRenderer);
288 return d->m_projection_matrix.count();
289}
290
291int QSGAbstractRenderer::projectionMatrixWithNativeNDCCount() const
292{
293 Q_D(const QSGAbstractRenderer);
294 return d->m_projection_matrix_native_ndc.count();
295}
296
297/*!
298 \internal
299 */
300QMatrix4x4 QSGAbstractRenderer::projectionMatrixWithNativeNDC(int index) const
301{
302 Q_D(const QSGAbstractRenderer);
303 return d->m_projection_matrix_native_ndc[index];
304}
305
306/*!
307 \internal
308 */
309void QSGAbstractRenderer::setInvertFrontFace(bool invert)
310{
311 Q_D(QSGAbstractRenderer);
312 d->m_invertFrontFace = invert;
313}
314
315/*!
316 \internal
317 */
318bool QSGAbstractRenderer::invertFrontFace() const
319{
320 Q_D(const QSGAbstractRenderer);
321 return d->m_invertFrontFace;
322}
323
324/*!
325 Sets the \a color to clear the framebuffer.
326
327 \sa clearColor()
328 */
329void QSGAbstractRenderer::setClearColor(const QColor &color)
330{
331 Q_D(QSGAbstractRenderer);
332 d->m_clear_color = color;
333}
334
335/*!
336 Returns the color that clears the framebuffer at the beginning
337 of the rendering.
338
339 \sa setClearColor()
340 */
341QColor QSGAbstractRenderer::clearColor() const
342{
343 Q_D(const QSGAbstractRenderer);
344 return d->m_clear_color;
345}
346
347/*!
348 \fn void QSGAbstractRenderer::nodeChanged(QSGNode *node, QSGNode::DirtyState state)
349 \internal
350 */
351
352void QSGAbstractRenderer::prepareSceneInline()
353{
354}
355
356void QSGAbstractRenderer::renderSceneInline()
357{
358}
359
360QT_END_NAMESPACE
361
362#include "moc_qsgabstractrenderer_p.cpp"