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
qsgsoftwarerenderer.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// Qt-Security score:significant reason:default
4
6
11
12#include <QtGui/QPaintDevice>
13#include <QtGui/QBackingStore>
14#include <QElapsedTimer>
15
16Q_STATIC_LOGGING_CATEGORY(lcRenderer, "qt.scenegraph.softwarecontext.renderer")
17
18QT_BEGIN_NAMESPACE
19
20QSGSoftwareRenderer::QSGSoftwareRenderer(QSGRenderContext *context)
21 : QSGAbstractSoftwareRenderer(context)
22 , m_paintDevice(nullptr)
23 , m_backingStore(nullptr)
24{
25 // Check if the environment variable is set to force/disable partial updates
26 static bool partialUpdateOverrideSet = false;
27 static const int forcePartialUpdates = qEnvironmentVariableIntValue("QSG_SOFTWARE_RENDERER_FORCE_PARTIAL_UPDATES", &partialUpdateOverrideSet);
28 if (partialUpdateOverrideSet) {
29 if (forcePartialUpdates == 0)
30 m_partialUpdateMode = DisablePartialUpdate;
31 else
32 m_partialUpdateMode = ForcePartialUpdate;
33 }
34}
35
36QSGSoftwareRenderer::~QSGSoftwareRenderer()
37{
38}
39
40void QSGSoftwareRenderer::setCurrentPaintDevice(QPaintDevice *device)
41{
42 m_paintDevice = device;
43 m_backingStore = nullptr;
44}
45
46QPaintDevice *QSGSoftwareRenderer::currentPaintDevice() const
47{
48 return m_paintDevice;
49}
50
51void QSGSoftwareRenderer::setBackingStore(QBackingStore *backingStore)
52{
53 if (backingStore != m_backingStore)
54 m_backingStoreSize = QSize();
55 m_backingStore = backingStore;
56 m_paintDevice = nullptr;
57}
58
59QRegion QSGSoftwareRenderer::flushRegion() const
60{
61 return m_flushRegion;
62}
63
64void QSGSoftwareRenderer::renderScene()
65{
66 QSGRenderer::renderScene();
67}
68
69void QSGSoftwareRenderer::render()
70{
71 if (!m_paintDevice && !m_backingStore && !m_rt.paintDevice)
72 return;
73
74 QPaintDevice *paintDevice = m_paintDevice ? m_paintDevice : m_rt.paintDevice;
75 QSize paintSize;
76 qreal paintDevicePixelRatio = 1.0;
77
78 if (paintDevice) {
79 paintSize = QSize(paintDevice->width(), paintDevice->height());
80 paintDevicePixelRatio = paintDevice->devicePixelRatio();
81 } else {
82 // For HiDPI QBackingStores, the paint device is valid between calls to
83 // beginPaint() and endPaint(). See: QTBUG-55875
84 const QSize size = m_backingStore->size();
85 const qreal windowDpr = m_backingStore->window()->devicePixelRatio();
86 if (size != m_backingStoreSize || windowDpr != m_windowDevicePixelRatio) {
87 m_backingStore->beginPaint(QRegion());
88
89 const QPaintDevice *backingStorePaintDevice = m_backingStore->paintDevice();
90 m_backingStorePaintSize = QSize(backingStorePaintDevice->width(),
91 backingStorePaintDevice->height());
92 m_backingStorePaintDevicePixelRatio = backingStorePaintDevice->devicePixelRatio();
93
94 m_backingStore->endPaint();
95 m_backingStoreSize = size;
96 m_windowDevicePixelRatio = windowDpr;
97 }
98 paintSize = m_backingStorePaintSize;
99 paintDevicePixelRatio = m_backingStorePaintDevicePixelRatio;
100 }
101
102 QElapsedTimer renderTimer;
103
104 setBackgroundColor(clearColor());
105 const QRect backgroundRect =
106 QRectF(QPointF(), QSizeF(paintSize) / paintDevicePixelRatio).toAlignedRect();
107 setBackgroundRect(backgroundRect, paintDevicePixelRatio);
108 const bool disablePartialUpdates = m_partialUpdateMode == DisablePartialUpdate;
109
110 // Force the whole render area to be marked as dirty, avoiding any partial update
111 if (disablePartialUpdates)
112 markDirty();
113
114 // Build Renderlist
115 // The renderlist is created by visiting each node in the tree and when a
116 // renderable node is reach, we find the coorosponding RenderableNode object
117 // and append it to the renderlist. At this point the RenderableNode object
118 // should not need any further updating so it is just a matter of appending
119 // RenderableNodes
120 renderTimer.start();
121 buildRenderList();
122 qint64 buildRenderListTime = renderTimer.restart();
123
124 // Optimize Renderlist
125 // This is a pass through the renderlist to determine what actually needs to
126 // be painted. Without this pass the renderlist will simply render each item
127 // from back to front, with a high potential for overdraw. It would also lead
128 // to the entire window being flushed every frame. The objective of the
129 // optimization pass is to only paint dirty nodes that are not occuluded. A
130 // side effect of this is that additional nodes may need to be marked dirty to
131 // force a repaint. It is also important that any item that needs to be
132 // repainted only paints what is needed, via the use of clip regions.
133 const QRegion updateRegion = optimizeRenderList();
134 qint64 optimizeRenderListTime = renderTimer.restart();
135
136 // Nothing to paint: render nodes are the only ones that paint without a dirty region
137 const auto &nodes = renderableNodes();
138 if (updateRegion.isEmpty() && std::none_of(nodes.cbegin(), nodes.cend(), [](auto *node) {
139 return node->type() == QSGSoftwareRenderableNode::RenderNode && node->isDirty();
140 })) {
141 for (auto *node : nodes)
142 node->clearDirty();
143 m_flushRegion = QRegion();
144 qCDebug(lcRenderer) << "render: nothing to paint" << buildRenderListTime << optimizeRenderListTime;
145 return;
146 }
147
148 // If Rendering to a backingstore, prepare it to be updated
149 bool usingBackingStore = false;
150 if (!paintDevice) {
151 m_backingStore->beginPaint(updateRegion);
152 paintDevice = m_backingStore->paintDevice();
153 usingBackingStore = true;
154 }
155
156 QPainter painter(paintDevice);
157 painter.setRenderHint(QPainter::Antialiasing);
158 auto rc = static_cast<QSGSoftwareRenderContext *>(context());
159 QPainter *prevPainter = rc->m_activePainter;
160 rc->m_activePainter = &painter;
161
162 // Render the contents Renderlist
163 m_flushRegion = renderNodes(&painter);
164 qint64 renderTime = renderTimer.elapsed();
165
166 painter.end();
167 if (usingBackingStore)
168 m_backingStore->endPaint();
169
170 rc->m_activePainter = prevPainter;
171 qCDebug(lcRenderer) << "render" << m_flushRegion << buildRenderListTime << optimizeRenderListTime << renderTime;
172}
173
174QT_END_NAMESPACE
QT_BEGIN_NAMESPACE Q_STATIC_LOGGING_CATEGORY(lcSynthesizedIterableAccess, "qt.iterable.synthesized", QtWarningMsg)