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 QtWidgets 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#ifndef QWIDGETBACKINGSTORE_P_H
41#define QWIDGETBACKINGSTORE_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <QtWidgets/private/qtwidgetsglobal_p.h>
55#include <QDebug>
56#include <QtWidgets/qwidget.h>
57#include <private/qwidget_p.h>
58#include <QtGui/qbackingstore.h>
59
60QT_BEGIN_NAMESPACE
61
62class QPlatformTextureList;
63class QPlatformTextureListWatcher;
64class QWidgetBackingStore;
65
66struct BeginPaintInfo {
67 inline BeginPaintInfo() : wasFlushed(0), nothingToPaint(0), backingStoreRecreated(0) {}
68 uint wasFlushed : 1;
69 uint nothingToPaint : 1;
70 uint backingStoreRecreated : 1;
71};
72
73#ifndef QT_NO_OPENGL
74class QPlatformTextureListWatcher : public QObject
75{
76 Q_OBJECT
77
78public:
79 QPlatformTextureListWatcher(QWidgetBackingStore *backingStore);
80 void watch(QPlatformTextureList *textureList);
81 bool isLocked() const;
82
83private slots:
84 void onLockStatusChanged(bool locked);
85
86private:
87 QHash<QPlatformTextureList *, bool> m_locked;
88 QWidgetBackingStore *m_backingStore;
89};
90#endif
91
92class Q_AUTOTEST_EXPORT QWidgetBackingStore
93{
94public:
95 enum UpdateTime {
96 UpdateNow,
97 UpdateLater
98 };
99
100 enum BufferState{
101 BufferValid,
102 BufferInvalid
103 };
104
105 QWidgetBackingStore(QWidget *t);
106 ~QWidgetBackingStore();
107
108 static void showYellowThing(QWidget *widget, const QRegion &rgn, int msec, bool);
109
110 void sync(QWidget *exposedWidget, const QRegion &exposedRegion);
111 void sync();
112 void flush(QWidget *widget = 0);
113
114 QBackingStore *backingStore() const { return store; }
115
116 inline bool isDirty() const
117 {
118 return !(dirtyWidgets.isEmpty() && dirty.isEmpty() && dirtyRenderToTextureWidgets.isEmpty());
119 }
120
121 // ### Qt 4.6: Merge into a template function (after MSVC isn't supported anymore).
122 void markDirty(const QRegion &rgn, QWidget *widget, UpdateTime updateTime = UpdateLater,
123 BufferState bufferState = BufferValid);
124 void markDirty(const QRect &rect, QWidget *widget, UpdateTime updateTime = UpdateLater,
125 BufferState bufferState = BufferValid);
126
127private:
128 QWidget *tlw;
129 QRegion dirtyOnScreen; // needsFlush
130 QRegion dirty; // needsRepaint
131 QRegion dirtyFromPreviousSync;
132 QVector<QWidget *> dirtyWidgets;
133 QVector<QWidget *> dirtyRenderToTextureWidgets;
134 QVector<QWidget *> *dirtyOnScreenWidgets;
135 QList<QWidget *> staticWidgets;
136 QBackingStore *store;
137 uint updateRequestSent : 1;
138
139 QPlatformTextureListWatcher *textureListWatcher;
140 QElapsedTimer perfTime;
141 int perfFrames;
142
143 void sendUpdateRequest(QWidget *widget, UpdateTime updateTime);
144
145 static bool flushPaint(QWidget *widget, const QRegion &rgn);
146 static void unflushPaint(QWidget *widget, const QRegion &rgn);
147 static void qt_flush(QWidget *widget, const QRegion &region, QBackingStore *backingStore,
148 QWidget *tlw,
149 QPlatformTextureList *widgetTextures,
150 QWidgetBackingStore *widgetBackingStore);
151
152 void doSync();
153 bool bltRect(const QRect &rect, int dx, int dy, QWidget *widget);
154
155 void beginPaint(QRegion &toClean, QWidget *widget, QBackingStore *backingStore,
156 BeginPaintInfo *returnInfo, bool toCleanIsInTopLevelCoordinates = true);
157 void endPaint(const QRegion &cleaned, QBackingStore *backingStore, BeginPaintInfo *beginPaintInfo);
158
159 QRegion dirtyRegion(QWidget *widget = 0) const;
160 QRegion staticContents(QWidget *widget = 0, const QRect &withinClipRect = QRect()) const;
161
162 void markDirtyOnScreen(const QRegion &dirtyOnScreen, QWidget *widget, const QPoint &topLevelOffset);
163
164 void removeDirtyWidget(QWidget *w);
165
166 void updateLists(QWidget *widget);
167
168 bool syncAllowed();
169
170 inline void addDirtyWidget(QWidget *widget, const QRegion &rgn)
171 {
172 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
173 QWidgetPrivate *widgetPrivate = widget->d_func();
174#if QT_CONFIG(graphicseffect)
175 if (widgetPrivate->graphicsEffect)
176 widgetPrivate->dirty = widgetPrivate->effectiveRectFor(rgn.boundingRect());
177 else
178#endif // QT_CONFIG(graphicseffect)
179 widgetPrivate->dirty = rgn;
180 dirtyWidgets.append(widget);
181 widgetPrivate->inDirtyList = true;
182 }
183 }
184
185 inline void addDirtyRenderToTextureWidget(QWidget *widget)
186 {
187 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
188 QWidgetPrivate *widgetPrivate = widget->d_func();
189 Q_ASSERT(widgetPrivate->renderToTexture);
190 dirtyRenderToTextureWidgets.append(widget);
191 widgetPrivate->inDirtyList = true;
192 }
193 }
194
195 inline void dirtyWidgetsRemoveAll(QWidget *widget)
196 {
197 int i = 0;
198 while (i < dirtyWidgets.size()) {
199 if (dirtyWidgets.at(i) == widget)
200 dirtyWidgets.remove(i);
201 else
202 ++i;
203 }
204 }
205
206 inline void addStaticWidget(QWidget *widget)
207 {
208 if (!widget)
209 return;
210
211 Q_ASSERT(widget->testAttribute(Qt::WA_StaticContents));
212 if (!staticWidgets.contains(widget))
213 staticWidgets.append(widget);
214 }
215
216 inline void removeStaticWidget(QWidget *widget)
217 { staticWidgets.removeAll(widget); }
218
219 // Move the reparented widget and all its static children from this backing store
220 // to the new backing store if reparented into another top-level / backing store.
221 inline void moveStaticWidgets(QWidget *reparented)
222 {
223 Q_ASSERT(reparented);
224 QWidgetBackingStore *newBs = reparented->d_func()->maybeBackingStore();
225 if (newBs == this)
226 return;
227
228 int i = 0;
229 while (i < staticWidgets.size()) {
230 QWidget *w = staticWidgets.at(i);
231 if (reparented == w || reparented->isAncestorOf(w)) {
232 staticWidgets.removeAt(i);
233 if (newBs)
234 newBs->addStaticWidget(w);
235 } else {
236 ++i;
237 }
238 }
239 }
240
241 inline QRect topLevelRect() const
242 {
243 return tlw->data->crect;
244 }
245
246 inline void appendDirtyOnScreenWidget(QWidget *widget)
247 {
248 if (!widget)
249 return;
250
251 if (!dirtyOnScreenWidgets) {
252 dirtyOnScreenWidgets = new QVector<QWidget *>;
253 dirtyOnScreenWidgets->append(widget);
254 } else if (!dirtyOnScreenWidgets->contains(widget)) {
255 dirtyOnScreenWidgets->append(widget);
256 }
257 }
258
259 inline void dirtyOnScreenWidgetsRemoveAll(QWidget *widget)
260 {
261 if (!widget || !dirtyOnScreenWidgets)
262 return;
263
264 int i = 0;
265 while (i < dirtyOnScreenWidgets->size()) {
266 if (dirtyOnScreenWidgets->at(i) == widget)
267 dirtyOnScreenWidgets->remove(i);
268 else
269 ++i;
270 }
271 }
272
273 inline void resetWidget(QWidget *widget)
274 {
275 if (widget) {
276 widget->d_func()->inDirtyList = false;
277 widget->d_func()->isScrolled = false;
278 widget->d_func()->isMoved = false;
279 widget->d_func()->dirty = QRegion();
280 }
281 }
282
283 inline void updateStaticContentsSize()
284 {
285 for (int i = 0; i < staticWidgets.size(); ++i) {
286 QWidgetPrivate *wd = staticWidgets.at(i)->d_func();
287 if (!wd->extra)
288 wd->createExtra();
289 wd->extra->staticContentsSize = wd->data.crect.size();
290 }
291 }
292
293 inline bool hasStaticContents() const
294 {
295#if defined(Q_OS_WIN)
296 return !staticWidgets.isEmpty();
297#else
298 return !staticWidgets.isEmpty() && false;
299#endif
300 }
301
302 friend QRegion qt_dirtyRegion(QWidget *);
303 friend class QWidgetPrivate;
304 friend class QWidget;
305 friend class QBackingStore;
306
307 Q_DISABLE_COPY_MOVE(QWidgetBackingStore)
308};
309
310QT_END_NAMESPACE
311
312#endif // QBACKINGSTORE_P_H
313