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 = nullptr);
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 template <class T>
122 void markDirty(const T &r, QWidget *widget, UpdateTime updateTime = UpdateLater,
123 BufferState bufferState = BufferValid);
124
125private:
126 QWidget *tlw;
127 QRegion dirtyOnScreen; // needsFlush
128 QRegion dirty; // needsRepaint
129 QRegion dirtyFromPreviousSync;
130 QVector<QWidget *> dirtyWidgets;
131 QVector<QWidget *> dirtyRenderToTextureWidgets;
132 QVector<QWidget *> *dirtyOnScreenWidgets;
133 QList<QWidget *> staticWidgets;
134 QBackingStore *store;
135 uint updateRequestSent : 1;
136
137 QPlatformTextureListWatcher *textureListWatcher;
138 QElapsedTimer perfTime;
139 int perfFrames;
140
141 void sendUpdateRequest(QWidget *widget, UpdateTime updateTime);
142
143 static bool flushPaint(QWidget *widget, const QRegion &rgn);
144 static void unflushPaint(QWidget *widget, const QRegion &rgn);
145 static void qt_flush(QWidget *widget, const QRegion &region, QBackingStore *backingStore,
146 QWidget *tlw,
147 QPlatformTextureList *widgetTextures,
148 QWidgetBackingStore *widgetBackingStore);
149
150 void doSync();
151 bool bltRect(const QRect &rect, int dx, int dy, QWidget *widget);
152
153 void beginPaint(QRegion &toClean, QWidget *widget, QBackingStore *backingStore,
154 BeginPaintInfo *returnInfo, bool toCleanIsInTopLevelCoordinates = true);
155 void endPaint(const QRegion &cleaned, QBackingStore *backingStore, BeginPaintInfo *beginPaintInfo);
156
157 QRegion dirtyRegion(QWidget *widget = nullptr) const;
158 QRegion staticContents(QWidget *widget = nullptr, const QRect &withinClipRect = QRect()) const;
159
160 void markDirtyOnScreen(const QRegion &dirtyOnScreen, QWidget *widget, const QPoint &topLevelOffset);
161
162 void removeDirtyWidget(QWidget *w);
163
164 void updateLists(QWidget *widget);
165
166 bool syncAllowed();
167
168 inline void addDirtyWidget(QWidget *widget, const QRegion &rgn)
169 {
170 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
171 QWidgetPrivate *widgetPrivate = widget->d_func();
172#if QT_CONFIG(graphicseffect)
173 if (widgetPrivate->graphicsEffect)
174 widgetPrivate->dirty = widgetPrivate->effectiveRectFor(rgn.boundingRect());
175 else
176#endif // QT_CONFIG(graphicseffect)
177 widgetPrivate->dirty = rgn;
178 dirtyWidgets.append(widget);
179 widgetPrivate->inDirtyList = true;
180 }
181 }
182
183 inline void addDirtyRenderToTextureWidget(QWidget *widget)
184 {
185 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
186 QWidgetPrivate *widgetPrivate = widget->d_func();
187 Q_ASSERT(widgetPrivate->renderToTexture);
188 dirtyRenderToTextureWidgets.append(widget);
189 widgetPrivate->inDirtyList = true;
190 }
191 }
192
193 inline void dirtyWidgetsRemoveAll(QWidget *widget)
194 {
195 int i = 0;
196 while (i < dirtyWidgets.size()) {
197 if (dirtyWidgets.at(i) == widget)
198 dirtyWidgets.remove(i);
199 else
200 ++i;
201 }
202 }
203
204 inline void addStaticWidget(QWidget *widget)
205 {
206 if (!widget)
207 return;
208
209 Q_ASSERT(widget->testAttribute(Qt::WA_StaticContents));
210 if (!staticWidgets.contains(widget))
211 staticWidgets.append(widget);
212 }
213
214 inline void removeStaticWidget(QWidget *widget)
215 { staticWidgets.removeAll(widget); }
216
217 // Move the reparented widget and all its static children from this backing store
218 // to the new backing store if reparented into another top-level / backing store.
219 inline void moveStaticWidgets(QWidget *reparented)
220 {
221 Q_ASSERT(reparented);
222 QWidgetBackingStore *newBs = reparented->d_func()->maybeBackingStore();
223 if (newBs == this)
224 return;
225
226 int i = 0;
227 while (i < staticWidgets.size()) {
228 QWidget *w = staticWidgets.at(i);
229 if (reparented == w || reparented->isAncestorOf(w)) {
230 staticWidgets.removeAt(i);
231 if (newBs)
232 newBs->addStaticWidget(w);
233 } else {
234 ++i;
235 }
236 }
237 }
238
239 inline QRect topLevelRect() const
240 {
241 return tlw->data->crect;
242 }
243
244 inline void appendDirtyOnScreenWidget(QWidget *widget)
245 {
246 if (!widget)
247 return;
248
249 if (!dirtyOnScreenWidgets) {
250 dirtyOnScreenWidgets = new QVector<QWidget *>;
251 dirtyOnScreenWidgets->append(widget);
252 } else if (!dirtyOnScreenWidgets->contains(widget)) {
253 dirtyOnScreenWidgets->append(widget);
254 }
255 }
256
257 inline void dirtyOnScreenWidgetsRemoveAll(QWidget *widget)
258 {
259 if (!widget || !dirtyOnScreenWidgets)
260 return;
261
262 int i = 0;
263 while (i < dirtyOnScreenWidgets->size()) {
264 if (dirtyOnScreenWidgets->at(i) == widget)
265 dirtyOnScreenWidgets->remove(i);
266 else
267 ++i;
268 }
269 }
270
271 inline void resetWidget(QWidget *widget)
272 {
273 if (widget) {
274 widget->d_func()->inDirtyList = false;
275 widget->d_func()->isScrolled = false;
276 widget->d_func()->isMoved = false;
277 widget->d_func()->dirty = QRegion();
278 }
279 }
280
281 inline void updateStaticContentsSize()
282 {
283 for (int i = 0; i < staticWidgets.size(); ++i) {
284 QWidgetPrivate *wd = staticWidgets.at(i)->d_func();
285 if (!wd->extra)
286 wd->createExtra();
287 wd->extra->staticContentsSize = wd->data.crect.size();
288 }
289 }
290
291 inline bool hasStaticContents() const
292 {
293#if defined(Q_OS_WIN)
294 return !staticWidgets.isEmpty();
295#else
296 return !staticWidgets.isEmpty() && false;
297#endif
298 }
299
300 friend QRegion qt_dirtyRegion(QWidget *);
301 friend class QWidgetPrivate;
302 friend class QWidget;
303 friend class QBackingStore;
304
305 Q_DISABLE_COPY_MOVE(QWidgetBackingStore)
306};
307
308QT_END_NAMESPACE
309
310#endif // QBACKINGSTORE_P_H
311