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
qsgabstractsoftwarerenderer.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
10
11#include <QtCore/QLoggingCategory>
12#include <QtGui/QWindow>
13#include <QtQuick/QSGSimpleRectNode>
14
15Q_STATIC_LOGGING_CATEGORY(lc2DRender, "qt.scenegraph.softwarecontext.abstractrenderer")
16
17QT_BEGIN_NAMESPACE
18
19QSGAbstractSoftwareRenderer::QSGAbstractSoftwareRenderer(QSGRenderContext *context)
20 : QSGRenderer(context)
21 , m_background(new QSGSimpleRectNode)
22 , m_nodeUpdater(new QSGSoftwareRenderableNodeUpdater(this))
23{
24 // Setup special background node
25 auto backgroundRenderable = new QSGSoftwareRenderableNode(QSGSoftwareRenderableNode::SimpleRect, m_background);
26 addNodeMapping(m_background, backgroundRenderable);
27}
28
29QSGAbstractSoftwareRenderer::~QSGAbstractSoftwareRenderer()
30{
31 // Cleanup RenderableNodes
32 delete m_background;
33
34 qDeleteAll(m_nodes);
35
36 delete m_nodeUpdater;
37}
38
39QSGSoftwareRenderableNode *QSGAbstractSoftwareRenderer::renderableNode(QSGNode *node) const
40{
41 return m_nodes.value(node, nullptr);
42}
43
44// Used by GammaRay
45const QVector<QSGSoftwareRenderableNode*> &QSGAbstractSoftwareRenderer::renderableNodes() const
46{
47 return m_renderableNodes;
48}
49
50void QSGAbstractSoftwareRenderer::addNodeMapping(QSGNode *node, QSGSoftwareRenderableNode *renderableNode)
51{
52 m_nodes.insert(node, renderableNode);
53}
54
55void QSGAbstractSoftwareRenderer::appendRenderableNode(QSGSoftwareRenderableNode *node)
56{
57 m_renderableNodes.append(node);
58}
59
60void QSGAbstractSoftwareRenderer::nodeChanged(QSGNode *node, QSGNode::DirtyState state)
61{
62 if (state & QSGNode::DirtyGeometry) {
63 nodeGeometryUpdated(node);
64 }
65 if (state & QSGNode::DirtyMaterial) {
66 nodeMaterialUpdated(node);
67 }
68 if (state & QSGNode::DirtyMatrix) {
69 nodeMatrixUpdated(node);
70 }
71 if (state & QSGNode::DirtyNodeAdded) {
72 nodeAdded(node);
73 }
74 if (state & QSGNode::DirtyNodeRemoved) {
75 nodeRemoved(node);
76 }
77 if (state & QSGNode::DirtyOpacity) {
78 nodeOpacityUpdated(node);
79 }
80 if (state & QSGNode::DirtySubtreeBlocked) {
81 m_nodeUpdater->updateNodes(node);
82 }
83 if (state & QSGNode::DirtyForceUpdate) {
84 m_nodeUpdater->updateNodes(node);
85 }
86 QSGRenderer::nodeChanged(node, state);
87}
88
89QRegion QSGAbstractSoftwareRenderer::renderNodes(QPainter *painter)
90{
91 QRegion dirtyRegion;
92 // If there are no nodes, do nothing
93 if (m_renderableNodes.isEmpty())
94 return dirtyRegion;
95
96 auto iterator = m_renderableNodes.begin();
97 // First node is the background and needs to painted without blending
98 if (m_clearColorEnabled) {
99 auto backgroundNode = *iterator;
100 dirtyRegion += backgroundNode->renderNode(painter, /*force opaque painting*/ true);
101 }
102 iterator++;
103
104 for (; iterator != m_renderableNodes.end(); ++iterator) {
105 auto node = *iterator;
106 dirtyRegion += node->renderNode(painter);
107 }
108
109 return dirtyRegion;
110}
111
112void QSGAbstractSoftwareRenderer::buildRenderList()
113{
114 // Clear the previous renderlist
115 m_renderableNodes.clear();
116 // Add the background renderable (always first)
117 m_renderableNodes.append(renderableNode(m_background));
118 // Build the renderlist
119 QSGSoftwareRenderListBuilder(this).visitChildren(rootNode());
120}
121
122QRegion QSGAbstractSoftwareRenderer::optimizeRenderList()
123{
124 // Iterate through the renderlist from front to back
125 // Objective is to update the dirty status and rects.
126 for (auto i = m_renderableNodes.rbegin(); i != m_renderableNodes.rend(); ++i) {
127 auto node = *i;
128 // Track the original version of isDirty() as it can change if
129 // when we subtract dirty regions but still need to mark the previously
130 // dirty region as dirty.
131 const bool wasDirty = node->isDirty();
132 if (!m_dirtyRegion.isEmpty()) {
133 // See if the current dirty regions apply to the current node
134 node->addDirtyRegion(m_dirtyRegion, true);
135 }
136
137 if (!m_obscuredRegion.isEmpty()) {
138 // Don't try to paint things that are covered by opaque objects
139 node->subtractDirtyRegion(m_obscuredRegion);
140 }
141
142 // Keep up with obscured regions
143 if (node->isOpaque()) {
144 m_obscuredRegion += node->boundingRectMin();
145 }
146
147 if (node->isDirty()) {
148 // Don't paint things outside of the rendering area
149 if (!m_background->rect().toRect().contains(node->boundingRectMax(), /*proper*/ true)) {
150 // Some part(s) of node is(are) outside of the rendering area
151 QRegion renderArea(m_background->rect().toRect());
152 QRegion outsideRegions = node->dirtyRegion().subtracted(renderArea);
153 if (!outsideRegions.isEmpty())
154 node->subtractDirtyRegion(outsideRegions);
155 }
156
157 // Get the dirty region's to pass to the next nodes
158 if (node->isOpaque()) {
159 // if isOpaque, subtract node's dirty rect from m_dirtyRegion
160 m_dirtyRegion -= node->boundingRectMin();
161 } else {
162 // if isAlpha, add node's dirty rect to m_dirtyRegion
163 m_dirtyRegion += node->dirtyRegion();
164 }
165 }
166
167 if (wasDirty) {
168 // If this node started out dirty, make sure its previous region is
169 // added to the dirty region so that it gets cleared properly.
170 QRegion prevDirty = node->previousDirtyRegion();
171 if (!prevDirty.isNull())
172 m_dirtyRegion += prevDirty;
173 }
174 }
175
176 if (m_obscuredRegion.contains(m_background->rect().toAlignedRect())) {
177 m_isOpaque = true;
178 } else {
179 m_isOpaque = false;
180 }
181
182 // Empty dirtyRegion (for second pass)
183 m_dirtyRegion = QRegion();
184 m_obscuredRegion = QRegion();
185
186 // Iterate through the renderlist from back to front
187 // Objective is to make sure all non-opaque items are painted when an item under them is dirty
188 for (auto j = m_renderableNodes.begin(); j != m_renderableNodes.end(); ++j) {
189 auto node = *j;
190
191 if ((!node->isOpaque() || node->boundingRectMax() != node->boundingRectMin()) && !m_dirtyRegion.isEmpty()) {
192 // Blended nodes need to be updated
193 // QTBUG-113745: Also nodes with floating point boundary rectangles need to
194 // be updated. The reason is that m_obscuredRegion contains only the rounded
195 // down bounding rectangle (node->boundingRectMin()) and thus not the whole
196 // node. As a result up to 1 pixel would be overpainted when it should not.
197 node->addDirtyRegion(m_dirtyRegion, true);
198 }
199
200 m_dirtyRegion += node->dirtyRegion();
201 }
202
203 QRegion updateRegion = m_dirtyRegion;
204
205 // Empty dirtyRegion
206 m_dirtyRegion = QRegion();
207 m_obscuredRegion = QRegion();
208
209 return updateRegion;
210}
211
212void QSGAbstractSoftwareRenderer::setBackgroundColor(const QColor &color)
213{
214 if (m_background->color() == color)
215 return;
216 m_background->setColor(color);
217 renderableNode(m_background)->markMaterialDirty();
218}
219
220void QSGAbstractSoftwareRenderer::setBackgroundRect(const QRect &rect, qreal devicePixelRatio)
221{
222 if (m_background->rect().toRect() == rect && m_devicePixelRatio == devicePixelRatio)
223 return;
224 m_background->setRect(rect);
225 m_devicePixelRatio = devicePixelRatio;
226 renderableNode(m_background)->markGeometryDirty();
227 // Invalidate the whole scene when the background is resized
228 markDirty();
229}
230
231QColor QSGAbstractSoftwareRenderer::backgroundColor()
232{
233 return m_background->color();
234}
235
236QRect QSGAbstractSoftwareRenderer::backgroundRect()
237{
238 return m_background->rect().toRect();
239}
240
241void QSGAbstractSoftwareRenderer::nodeAdded(QSGNode *node)
242{
243 qCDebug(lc2DRender, "nodeAdded %p", (void*)node);
244
245 m_nodeUpdater->updateNodes(node);
246}
247
248void QSGAbstractSoftwareRenderer::nodeRemoved(QSGNode *node)
249{
250 qCDebug(lc2DRender, "nodeRemoved %p", (void*)node);
251
252 auto renderable = renderableNode(node);
253 // remove mapping
254 if (renderable != nullptr) {
255 // Need to mark this region dirty in the other nodes
256 QRegion dirtyRegion = renderable->previousDirtyRegion(true);
257 if (dirtyRegion.isEmpty())
258 dirtyRegion = renderable->boundingRectMax();
259 m_dirtyRegion += dirtyRegion;
260 m_nodes.remove(node);
261 delete renderable;
262 }
263
264 // Remove all children nodes as well
265 for (QSGNode *child = node->firstChild(); child; child = child->nextSibling()) {
266 nodeRemoved(child);
267 }
268
269 m_nodeUpdater->updateNodes(node, true);
270}
271
272void QSGAbstractSoftwareRenderer::nodeGeometryUpdated(QSGNode *node)
273{
274 qCDebug(lc2DRender, "nodeGeometryUpdated");
275
276 // Mark node as dirty
277 auto renderable = renderableNode(node);
278 if (renderable != nullptr) {
279 renderable->markGeometryDirty();
280 } else {
281 m_nodeUpdater->updateNodes(node);
282 }
283}
284
285void QSGAbstractSoftwareRenderer::nodeMaterialUpdated(QSGNode *node)
286{
287 qCDebug(lc2DRender, "nodeMaterialUpdated");
288
289 // Mark node as dirty
290 auto renderable = renderableNode(node);
291 if (renderable != nullptr) {
292 renderable->markMaterialDirty();
293 } else {
294 m_nodeUpdater->updateNodes(node);
295 }
296}
297
298void QSGAbstractSoftwareRenderer::nodeMatrixUpdated(QSGNode *node)
299{
300 qCDebug(lc2DRender, "nodeMaterialUpdated");
301
302 // Update children nodes
303 m_nodeUpdater->updateNodes(node);
304}
305
306void QSGAbstractSoftwareRenderer::nodeOpacityUpdated(QSGNode *node)
307{
308 qCDebug(lc2DRender, "nodeOpacityUpdated");
309
310 // Update children nodes
311 m_nodeUpdater->updateNodes(node);
312}
313
314void QSGAbstractSoftwareRenderer::markDirty()
315{
316 m_dirtyRegion = QRegion(m_background->rect().toRect());
317}
318
319void QSGAbstractSoftwareRenderer::setClearColorEnabled(bool enable)
320{
321 m_clearColorEnabled = enable;
322}
323
324bool QSGAbstractSoftwareRenderer::clearColorEnabled() const
325{
326 return m_clearColorEnabled;
327}
328
329QT_END_NAMESPACE
Q_STATIC_LOGGING_CATEGORY(lcAccessibilityCore, "qt.accessibility.core")