1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtQuick module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qsgsoftwarerenderer_p.h"
41 
42 #include "qsgsoftwarerenderablenodeupdater_p.h"
43 #include "qsgsoftwarerenderlistbuilder_p.h"
44 #include "qsgsoftwarecontext_p.h"
45 #include "qsgsoftwarerenderablenode_p.h"
46 
47 #include <QtGui/QPaintDevice>
48 #include <QtGui/QBackingStore>
49 #include <QElapsedTimer>
50 
51 Q_LOGGING_CATEGORY(lcRenderer, "qt.scenegraph.softwarecontext.renderer")
52 
53 QT_BEGIN_NAMESPACE
54 
QSGSoftwareRenderer(QSGRenderContext * context)55 QSGSoftwareRenderer::QSGSoftwareRenderer(QSGRenderContext *context)
56     : QSGAbstractSoftwareRenderer(context)
57     , m_paintDevice(nullptr)
58     , m_backingStore(nullptr)
59 {
60 }
61 
~QSGSoftwareRenderer()62 QSGSoftwareRenderer::~QSGSoftwareRenderer()
63 {
64 }
65 
setCurrentPaintDevice(QPaintDevice * device)66 void QSGSoftwareRenderer::setCurrentPaintDevice(QPaintDevice *device)
67 {
68     m_paintDevice = device;
69     m_backingStore = nullptr;
70 }
71 
currentPaintDevice() const72 QPaintDevice *QSGSoftwareRenderer::currentPaintDevice() const
73 {
74     return m_paintDevice;
75 }
76 
setBackingStore(QBackingStore * backingStore)77 void QSGSoftwareRenderer::setBackingStore(QBackingStore *backingStore)
78 {
79     m_backingStore = backingStore;
80     m_paintDevice = nullptr;
81 }
82 
flushRegion() const83 QRegion QSGSoftwareRenderer::flushRegion() const
84 {
85     return m_flushRegion;
86 }
87 
renderScene(uint)88 void QSGSoftwareRenderer::renderScene(uint)
89 {
90     class B : public QSGBindable
91     {
92     public:
93         void bind() const override { }
94     } bindable;
95     QSGRenderer::renderScene(bindable);
96 }
97 
render()98 void QSGSoftwareRenderer::render()
99 {
100     if (!m_paintDevice && !m_backingStore)
101         return;
102 
103     // If there is a backingstore, set the current paint device
104     if (m_backingStore) {
105         // For HiDPI QBackingStores, the paint device is not valid
106         // until begin() has been called. See: QTBUG-55875
107         m_backingStore->beginPaint(QRegion());
108         m_paintDevice = m_backingStore->paintDevice();
109         m_backingStore->endPaint();
110     }
111 
112     QElapsedTimer renderTimer;
113 
114     setBackgroundColor(clearColor());
115     setBackgroundRect(QRect(0, 0,
116                             m_paintDevice->width() / m_paintDevice->devicePixelRatioF(),
117                             m_paintDevice->height() / m_paintDevice->devicePixelRatioF()),
118                      m_paintDevice->devicePixelRatioF());
119 
120     // Build Renderlist
121     // The renderlist is created by visiting each node in the tree and when a
122     // renderable node is reach, we find the coorosponding RenderableNode object
123     // and append it to the renderlist.  At this point the RenderableNode object
124     // should not need any further updating so it is just a matter of appending
125     // RenderableNodes
126     renderTimer.start();
127     buildRenderList();
128     qint64 buildRenderListTime = renderTimer.restart();
129 
130     // Optimize Renderlist
131     // This is a pass through the renderlist to determine what actually needs to
132     // be painted.  Without this pass the renderlist will simply render each item
133     // from back to front, with a high potential for overdraw.  It would also lead
134     // to the entire window being flushed every frame.  The objective of the
135     // optimization pass is to only paint dirty nodes that are not occuluded.  A
136     // side effect of this is that additional nodes may need to be marked dirty to
137     // force a repaint.  It is also important that any item that needs to be
138     // repainted only paints what is needed, via the use of clip regions.
139     const QRegion updateRegion = optimizeRenderList();
140     qint64 optimizeRenderListTime = renderTimer.restart();
141 
142     // If Rendering to a backingstore, prepare it to be updated
143     if (m_backingStore != nullptr) {
144         m_backingStore->beginPaint(updateRegion);
145         // It is possible that a QBackingStore's paintDevice() will change
146         // when begin() is called.
147         m_paintDevice = m_backingStore->paintDevice();
148     }
149 
150     QPainter painter(m_paintDevice);
151     painter.setRenderHint(QPainter::Antialiasing);
152     auto rc = static_cast<QSGSoftwareRenderContext *>(context());
153     QPainter *prevPainter = rc->m_activePainter;
154     rc->m_activePainter = &painter;
155 
156     // Render the contents Renderlist
157     m_flushRegion = renderNodes(&painter);
158     qint64 renderTime = renderTimer.elapsed();
159 
160     painter.end();
161     if (m_backingStore != nullptr)
162         m_backingStore->endPaint();
163 
164     rc->m_activePainter = prevPainter;
165     qCDebug(lcRenderer) << "render" << m_flushRegion << buildRenderListTime << optimizeRenderListTime << renderTime;
166 }
167 
168 QT_END_NAMESPACE
169