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 | |
4 | #include "qsgsoftwarerenderloop_p.h" |
5 | |
6 | #include "qsgsoftwarecontext_p.h" |
7 | |
8 | #include <QtCore/QCoreApplication> |
9 | |
10 | #include <private/qquickwindow_p.h> |
11 | #include <private/qquickitem_p.h> |
12 | #include <QElapsedTimer> |
13 | #include <private/qquickanimatorcontroller_p.h> |
14 | #include <private/qquickprofiler_p.h> |
15 | #include <private/qsgsoftwarerenderer_p.h> |
16 | #include <qpa/qplatformbackingstore.h> |
17 | |
18 | #include <QtGui/QBackingStore> |
19 | |
20 | #include <qtquick_tracepoints_p.h> |
21 | |
22 | QT_BEGIN_NAMESPACE |
23 | |
24 | QSGSoftwareRenderLoop::QSGSoftwareRenderLoop() |
25 | { |
26 | sg = new QSGSoftwareContext(); |
27 | rc = sg->createRenderContext(); |
28 | } |
29 | |
30 | QSGSoftwareRenderLoop::~QSGSoftwareRenderLoop() |
31 | { |
32 | delete rc; |
33 | delete sg; |
34 | } |
35 | |
36 | void QSGSoftwareRenderLoop::show(QQuickWindow *window) |
37 | { |
38 | WindowData data; |
39 | data.updatePending = false; |
40 | data.grabOnly = false; |
41 | m_windows[window] = data; |
42 | |
43 | if (m_backingStores[window] == nullptr) { |
44 | m_backingStores[window] = new QBackingStore(window); |
45 | } |
46 | |
47 | maybeUpdate(window); |
48 | } |
49 | |
50 | void QSGSoftwareRenderLoop::hide(QQuickWindow *window) |
51 | { |
52 | QQuickWindowPrivate *cd = QQuickWindowPrivate::get(c: window); |
53 | cd->fireAboutToStop(); |
54 | } |
55 | |
56 | void QSGSoftwareRenderLoop::windowDestroyed(QQuickWindow *window) |
57 | { |
58 | m_windows.remove(key: window); |
59 | delete m_backingStores[window]; |
60 | m_backingStores.remove(key: window); |
61 | hide(window); |
62 | |
63 | QQuickWindowPrivate *d = QQuickWindowPrivate::get(c: window); |
64 | d->cleanupNodesOnShutdown(); |
65 | |
66 | if (m_windows.size() == 0) { |
67 | rc->invalidate(); |
68 | } |
69 | |
70 | d->animationController.reset(); |
71 | } |
72 | |
73 | void QSGSoftwareRenderLoop::renderWindow(QQuickWindow *window, bool isNewExpose) |
74 | { |
75 | QQuickWindowPrivate *cd = QQuickWindowPrivate::get(c: window); |
76 | if (!m_windows.contains(key: window)) |
77 | return; |
78 | |
79 | WindowData &data = const_cast<WindowData &>(m_windows[window]); |
80 | |
81 | //If were not in grabOnly mode, dont render a non-renderable window |
82 | if (!data.grabOnly && !cd->isRenderable()) |
83 | return; |
84 | |
85 | //Resize the backing store if necessary |
86 | if (m_backingStores[window]->size() != window->size()) { |
87 | m_backingStores[window]->resize(size: window->size()); |
88 | } |
89 | |
90 | // ### create QPainter and set up pointer to current window/painter |
91 | QSGSoftwareRenderContext *ctx = static_cast<QSGSoftwareRenderContext*>(cd->context); |
92 | ctx->initializeIfNeeded(); |
93 | |
94 | bool alsoSwap = data.updatePending; |
95 | data.updatePending = false; |
96 | |
97 | if (!data.grabOnly) { |
98 | cd->deliveryAgentPrivate()->flushFrameSynchronousEvents(win: window); |
99 | // Event delivery/processing triggered the window to be deleted or stop rendering. |
100 | if (!m_windows.contains(key: window)) |
101 | return; |
102 | } |
103 | |
104 | Q_TRACE_SCOPE(QSG_renderWindow) |
105 | QElapsedTimer renderTimer; |
106 | qint64 renderTime = 0, syncTime = 0, polishTime = 0; |
107 | bool profileFrames = QSG_RASTER_LOG_TIME_RENDERLOOP().isDebugEnabled(); |
108 | if (profileFrames) |
109 | renderTimer.start(); |
110 | Q_QUICK_SG_PROFILE_START(QQuickProfiler::SceneGraphPolishFrame); |
111 | Q_TRACE(QSG_polishItems_entry); |
112 | |
113 | cd->polishItems(); |
114 | |
115 | if (profileFrames) |
116 | polishTime = renderTimer.nsecsElapsed(); |
117 | Q_TRACE(QSG_polishItems_exit); |
118 | Q_QUICK_SG_PROFILE_SWITCH(QQuickProfiler::SceneGraphPolishFrame, |
119 | QQuickProfiler::SceneGraphRenderLoopFrame, |
120 | QQuickProfiler::SceneGraphPolishPolish); |
121 | Q_TRACE(QSG_sync_entry); |
122 | |
123 | emit window->afterAnimating(); |
124 | |
125 | emit window->beforeFrameBegin(); |
126 | |
127 | cd->syncSceneGraph(); |
128 | rc->endSync(); |
129 | |
130 | if (profileFrames) |
131 | syncTime = renderTimer.nsecsElapsed(); |
132 | Q_TRACE(QSG_sync_exit); |
133 | Q_QUICK_SG_PROFILE_RECORD(QQuickProfiler::SceneGraphRenderLoopFrame, |
134 | QQuickProfiler::SceneGraphRenderLoopSync); |
135 | Q_TRACE(QSG_render_entry); |
136 | |
137 | //Tell the renderer about the windows backing store |
138 | auto softwareRenderer = static_cast<QSGSoftwareRenderer*>(cd->renderer); |
139 | if (softwareRenderer) |
140 | softwareRenderer->setBackingStore(m_backingStores[window]); |
141 | |
142 | cd->renderSceneGraph(); |
143 | |
144 | if (profileFrames) |
145 | renderTime = renderTimer.nsecsElapsed(); |
146 | Q_TRACE(QSG_render_exit); |
147 | Q_QUICK_SG_PROFILE_RECORD(QQuickProfiler::SceneGraphRenderLoopFrame, |
148 | QQuickProfiler::SceneGraphRenderLoopRender); |
149 | Q_TRACE(QSG_swap_entry); |
150 | |
151 | if (data.grabOnly) { |
152 | grabContent = m_backingStores[window]->handle()->toImage(); |
153 | data.grabOnly = false; |
154 | } |
155 | |
156 | if (alsoSwap && window->isVisible()) { |
157 | //Flush backingstore to window |
158 | if (!isNewExpose) |
159 | m_backingStores[window]->flush(region: softwareRenderer->flushRegion()); |
160 | else |
161 | m_backingStores[window]->flush(region: QRegion(QRect(QPoint(0,0), window->size()))); |
162 | cd->fireFrameSwapped(); |
163 | } |
164 | |
165 | emit window->afterFrameEnd(); |
166 | |
167 | qint64 swapTime = 0; |
168 | if (profileFrames) |
169 | swapTime = renderTimer.nsecsElapsed(); |
170 | Q_TRACE(QSG_swap_exit); |
171 | Q_QUICK_SG_PROFILE_END(QQuickProfiler::SceneGraphRenderLoopFrame, |
172 | QQuickProfiler::SceneGraphRenderLoopSwap); |
173 | |
174 | if (QSG_RASTER_LOG_TIME_RENDERLOOP().isDebugEnabled()) { |
175 | static QTime lastFrameTime = QTime::currentTime(); |
176 | qCDebug(QSG_RASTER_LOG_TIME_RENDERLOOP, |
177 | "Frame rendered with 'software' renderloop in %dms, polish=%d, sync=%d, render=%d, swap=%d, frameDelta=%d" , |
178 | int(swapTime / 1000000), |
179 | int(polishTime / 1000000), |
180 | int((syncTime - polishTime) / 1000000), |
181 | int((renderTime - syncTime) / 1000000), |
182 | int((swapTime - renderTime) / 1000000), |
183 | int(lastFrameTime.msecsTo(QTime::currentTime()))); |
184 | lastFrameTime = QTime::currentTime(); |
185 | } |
186 | |
187 | // Might have been set during syncSceneGraph() |
188 | if (data.updatePending) |
189 | maybeUpdate(window); |
190 | } |
191 | |
192 | void QSGSoftwareRenderLoop::exposureChanged(QQuickWindow *window) |
193 | { |
194 | if (window->isExposed()) { |
195 | m_windows[window].updatePending = true; |
196 | renderWindow(window, isNewExpose: true); |
197 | } |
198 | } |
199 | |
200 | QImage QSGSoftwareRenderLoop::grab(QQuickWindow *window) |
201 | { |
202 | //If the window was never shown, create a new backing store |
203 | if (!m_backingStores.contains(key: window)) { |
204 | m_backingStores[window] = new QBackingStore(window); |
205 | // Call create on window to make sure platform window is created |
206 | window->create(); |
207 | } |
208 | |
209 | //If there is no WindowData, add one |
210 | if (!m_windows.contains(key: window)) { |
211 | WindowData data; |
212 | data.updatePending = false; |
213 | m_windows[window] = data; |
214 | } |
215 | |
216 | m_windows[window].grabOnly = true; |
217 | |
218 | renderWindow(window); |
219 | |
220 | QImage grabbed = grabContent; |
221 | grabbed.detach(); |
222 | grabContent = QImage(); |
223 | return grabbed; |
224 | } |
225 | |
226 | |
227 | |
228 | void QSGSoftwareRenderLoop::maybeUpdate(QQuickWindow *window) |
229 | { |
230 | if (!m_windows.contains(key: window)) |
231 | return; |
232 | |
233 | m_windows[window].updatePending = true; |
234 | window->requestUpdate(); |
235 | } |
236 | |
237 | QSurface::SurfaceType QSGSoftwareRenderLoop::windowSurfaceType() const |
238 | { |
239 | return QSurface::RasterSurface; |
240 | } |
241 | |
242 | |
243 | |
244 | QSGContext *QSGSoftwareRenderLoop::sceneGraphContext() const |
245 | { |
246 | return sg; |
247 | } |
248 | |
249 | |
250 | void QSGSoftwareRenderLoop::handleUpdateRequest(QQuickWindow *window) |
251 | { |
252 | renderWindow(window); |
253 | } |
254 | |
255 | QT_END_NAMESPACE |
256 | |
257 | #include "moc_qsgsoftwarerenderloop_p.cpp" |
258 | |