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
qwidgetrepaintmanager.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
5#include "qplatformdefs.h"
6
8
9#include <QtCore/qglobal.h>
10#include <QtCore/qdebug.h>
11#include <QtCore/qvarlengtharray.h>
12#include <QtGui/qevent.h>
13#include <QtWidgets/qapplication.h>
14#include <QtGui/qpaintengine.h>
15#if QT_CONFIG(graphicsview)
16#include <QtWidgets/qgraphicsproxywidget.h>
17#endif
18
19#include <private/qwidget_p.h>
20#include <private/qapplication_p.h>
21#include <private/qpaintengine_raster_p.h>
22#if QT_CONFIG(graphicseffect)
23#include <private/qgraphicseffect_p.h>
24#endif
25#include <QtGui/private/qwindow_p.h>
26
27#include <qpa/qplatformbackingstore.h>
28
30
31Q_GLOBAL_STATIC(QPlatformTextureList, qt_dummy_platformTextureList)
32
33// Watches one or more QPlatformTextureLists for changes in the lock state and
34// triggers a backingstore sync when all the registered lists turn into
35// unlocked state. This is essential when a custom rhiFlush()
36// implementation in a platform plugin is not synchronous and keeps
37// holding on to the textures for some time even after returning from there.
38class QPlatformTextureListWatcher : public QObject
39{
40 Q_OBJECT
41public:
42 QPlatformTextureListWatcher(QWidgetRepaintManager *repaintManager)
43 : m_repaintManager(repaintManager) {}
44
45 void watch(QPlatformTextureList *textureList) {
46 connect(textureList, SIGNAL(locked(bool)), SLOT(onLockStatusChanged(bool)));
47 m_locked[textureList] = textureList->isLocked();
48 }
49
50 bool isLocked() const {
51 for (const auto &[_, v] : m_locked.asKeyValueRange()) {
52 if (v)
53 return true;
54 }
55 return false;
56 }
57
58private slots:
59 void onLockStatusChanged(bool locked) {
60 QPlatformTextureList *tl = static_cast<QPlatformTextureList *>(sender());
61 m_locked[tl] = locked;
62 if (!isLocked())
63 m_repaintManager->sync();
64 }
65
66private:
67 QHash<QPlatformTextureList *, bool> m_locked;
68 QWidgetRepaintManager *m_repaintManager;
69};
70
71// ---------------------------------------------------------------------------
72
73QWidgetRepaintManager::QWidgetRepaintManager(QWidget *topLevel)
74 : tlw(topLevel), store(tlw->backingStore())
75{
76 Q_ASSERT(store);
77
78 // Ensure all existing subsurfaces and static widgets are added to their respective lists.
79 updateLists(topLevel);
80}
81
82void QWidgetRepaintManager::updateLists(QWidget *cur)
83{
84 if (!cur)
85 return;
86
87 QList<QObject*> children = cur->children();
88 for (int i = 0; i < children.size(); ++i) {
89 QWidget *child = qobject_cast<QWidget*>(children.at(i));
90 if (!child || child->isWindow())
91 continue;
92
93 updateLists(child);
94 }
95
96 if (cur->testAttribute(Qt::WA_StaticContents))
97 addStaticWidget(cur);
98}
99
100QWidgetRepaintManager::~QWidgetRepaintManager()
101{
102 for (int c = 0; c < dirtyWidgets.size(); ++c)
103 resetWidget(dirtyWidgets.at(c));
104 for (int c = 0; c < dirtyRenderToTextureWidgets.size(); ++c)
105 resetWidget(dirtyRenderToTextureWidgets.at(c));
106}
107
108/*!
109 \internal
110 Invalidates the \a r (in widget's coordinates) of the backing store, i.e.
111 all widgets intersecting with the region will be repainted when the backing
112 store is synced.
113*/
114template <class T>
115void QWidgetPrivate::invalidateBackingStore(const T &r)
116{
117 if (r.isEmpty())
118 return;
119
120 if (QCoreApplication::closingDown())
121 return;
122
123 Q_Q(QWidget);
124 if (!q->isVisible() || !q->updatesEnabled())
125 return;
126
127 QTLWExtra *tlwExtra = q->window()->d_func()->maybeTopData();
128 if (!tlwExtra || !tlwExtra->backingStore || !tlwExtra->repaintManager)
129 return;
130
131 T clipped(r);
132 clipped &= clipRect();
133 if (clipped.isEmpty())
134 return;
135
136 if (!graphicsEffect && extra && extra->hasMask) {
137 QRegion masked(extra->mask);
138 masked &= clipped;
139 if (masked.isEmpty())
140 return;
141
142 tlwExtra->repaintManager->markDirty(masked, q,
143 QWidgetRepaintManager::UpdateLater, QWidgetRepaintManager::BufferInvalid);
144 } else {
145 tlwExtra->repaintManager->markDirty(clipped, q,
146 QWidgetRepaintManager::UpdateLater, QWidgetRepaintManager::BufferInvalid);
147 }
148}
149// Needed by tst_QWidget
150template Q_AUTOTEST_EXPORT void QWidgetPrivate::invalidateBackingStore<QRect>(const QRect &r);
151
152static inline QRect widgetRectFor(QWidget *, const QRect &r) { return r; }
153static inline QRect widgetRectFor(QWidget *widget, const QRegion &) { return widget->rect(); }
154
155/*!
156 \internal
157 Marks the region of the widget as dirty (if not already marked as dirty) and
158 posts an UpdateRequest event to the top-level widget (if not already posted).
159
160 If updateTime is UpdateNow, the event is sent immediately instead of posted.
161
162 If bufferState is BufferInvalid, all widgets intersecting with the region will be dirty.
163
164 If the widget paints directly on screen, the event is sent to the widget
165 instead of the top-level widget, and bufferState is completely ignored.
166*/
167template <class T>
168void QWidgetRepaintManager::markDirty(const T &r, QWidget *widget, UpdateTime updateTime, BufferState bufferState)
169{
170 qCInfo(lcWidgetPainting) << "Marking" << r << "of" << widget << "dirty"
171 << "with" << updateTime;
172
173 Q_ASSERT(tlw->d_func()->extra);
174 Q_ASSERT(tlw->d_func()->extra->topextra);
175 Q_ASSERT(widget->isVisible() && widget->updatesEnabled());
176 Q_ASSERT(widget->window() == tlw);
177 Q_ASSERT(!r.isEmpty());
178
179#if QT_CONFIG(graphicseffect)
180 widget->d_func()->invalidateGraphicsEffectsRecursively();
181#endif
182
183 QRect widgetRect = widgetRectFor(widget, r);
184
185 // ---------------------------------------------------------------------------
186
187 if (widget->d_func()->shouldPaintOnScreen()) {
188 if (widget->d_func()->dirty.isEmpty()) {
189 widget->d_func()->dirty = r;
190 sendUpdateRequest(widget, updateTime);
191 return;
192 } else if (qt_region_strictContains(widget->d_func()->dirty, widgetRect)) {
193 if (updateTime == UpdateNow)
194 sendUpdateRequest(widget, updateTime);
195 return; // Already dirty
196 }
197
198 const bool eventAlreadyPosted = !widget->d_func()->dirty.isEmpty();
199 widget->d_func()->dirty += r;
200 if (!eventAlreadyPosted || updateTime == UpdateNow)
201 sendUpdateRequest(widget, updateTime);
202 return;
203 }
204
205 // ---------------------------------------------------------------------------
206
207 if (QWidgetPrivate::get(widget)->renderToTexture) {
208 if (!widget->d_func()->inDirtyList)
209 addDirtyRenderToTextureWidget(widget);
210 if (!updateRequestSent || updateTime == UpdateNow)
211 sendUpdateRequest(tlw, updateTime);
212 return;
213 }
214
215 // ---------------------------------------------------------------------------
216
217 QRect effectiveWidgetRect = widget->d_func()->effectiveRectFor(widgetRect);
218 const QPoint offset = widget->mapTo(tlw, QPoint());
219 QRect translatedRect = effectiveWidgetRect.translated(offset);
220#if QT_CONFIG(graphicseffect)
221 // Graphics effects may exceed window size, clamp
222 translatedRect = translatedRect.intersected(QRect(QPoint(), tlw->size()));
223#endif
224 if (qt_region_strictContains(dirty, translatedRect)) {
225 if (updateTime == UpdateNow)
226 sendUpdateRequest(tlw, updateTime);
227 return; // Already dirty
228 }
229
230 // ---------------------------------------------------------------------------
231
232 if (bufferState == BufferInvalid) {
233 const bool eventAlreadyPosted = !dirty.isEmpty() || updateRequestSent;
234#if QT_CONFIG(graphicseffect)
235 if (widget->d_func()->graphicsEffect)
236 dirty += widget->d_func()->effectiveRectFor(r).translated(offset);
237 else
238#endif
239 dirty += r.translated(offset);
240
241 if (!eventAlreadyPosted || updateTime == UpdateNow)
242 sendUpdateRequest(tlw, updateTime);
243 return;
244 }
245
246 // ---------------------------------------------------------------------------
247
248 if (dirtyWidgets.isEmpty()) {
249 addDirtyWidget(widget, r);
250 sendUpdateRequest(tlw, updateTime);
251 return;
252 }
253
254 // ---------------------------------------------------------------------------
255
256 if (widget->d_func()->inDirtyList) {
257 if (!qt_region_strictContains(widget->d_func()->dirty, effectiveWidgetRect)) {
258#if QT_CONFIG(graphicseffect)
259 if (widget->d_func()->graphicsEffect)
260 widget->d_func()->dirty += widget->d_func()->effectiveRectFor(r);
261 else
262#endif
263 widget->d_func()->dirty += r;
264 }
265 } else {
266 addDirtyWidget(widget, r);
267 }
268
269 // ---------------------------------------------------------------------------
270
271 if (updateTime == UpdateNow)
272 sendUpdateRequest(tlw, updateTime);
273}
274template void QWidgetRepaintManager::markDirty<QRect>(const QRect &, QWidget *, UpdateTime, BufferState);
275template void QWidgetRepaintManager::markDirty<QRegion>(const QRegion &, QWidget *, UpdateTime, BufferState);
276
277void QWidgetRepaintManager::addDirtyWidget(QWidget *widget, const QRegion &rgn)
278{
279 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
280 QWidgetPrivate *widgetPrivate = widget->d_func();
281#if QT_CONFIG(graphicseffect)
282 if (widgetPrivate->graphicsEffect)
283 widgetPrivate->dirty = widgetPrivate->effectiveRectFor(rgn.boundingRect());
284 else
285#endif // QT_CONFIG(graphicseffect)
286 widgetPrivate->dirty = rgn;
287 dirtyWidgets.append(widget);
288 widgetPrivate->inDirtyList = true;
289 }
290}
291
292void QWidgetRepaintManager::removeDirtyWidget(QWidget *w)
293{
294 if (!w)
295 return;
296
297 dirtyWidgets.removeAll(w);
298 dirtyRenderToTextureWidgets.removeAll(w);
299 resetWidget(w);
300
301 needsFlushWidgets.removeAll(w);
302
303 QWidgetPrivate *wd = w->d_func();
304 const int n = wd->children.size();
305 for (int i = 0; i < n; ++i) {
306 if (QWidget *child = qobject_cast<QWidget*>(wd->children.at(i)))
307 removeDirtyWidget(child);
308 }
309}
310
311void QWidgetRepaintManager::resetWidget(QWidget *widget)
312{
313 if (widget) {
314 widget->d_func()->inDirtyList = false;
315 widget->d_func()->isScrolled = false;
316 widget->d_func()->isMoved = false;
317 widget->d_func()->dirty = QRegion();
318 }
319}
320
321void QWidgetRepaintManager::addDirtyRenderToTextureWidget(QWidget *widget)
322{
323 if (widget && !widget->d_func()->inDirtyList && !widget->data->in_destructor) {
324 QWidgetPrivate *widgetPrivate = widget->d_func();
325 Q_ASSERT(widgetPrivate->renderToTexture);
326 dirtyRenderToTextureWidgets.append(widget);
327 widgetPrivate->inDirtyList = true;
328 }
329}
330
331void QWidgetRepaintManager::sendUpdateRequest(QWidget *widget, UpdateTime updateTime)
332{
333 if (!widget)
334 return;
335
336 qCInfo(lcWidgetPainting) << "Sending update request to" << widget << "with" << updateTime;
337
338 // Having every repaint() leading to a sync/flush is bad as it causes
339 // compositing and waiting for vsync each and every time. Change to
340 // UpdateLater, except for approx. once per frame to prevent starvation in
341 // case the control does not get back to the event loop.
342 if (updateTime == UpdateNow && QWidgetPrivate::get(widget)->textureChildSeen) {
343 int refresh = 60;
344 QWidget *w = widget->window();
345 QScreen *ws = w->windowHandle()->screen();
346 if (ws)
347 refresh = ws->refreshRate();
348 QWindowPrivate *wd = QWindowPrivate::get(w->windowHandle());
349 if (wd->lastComposeTime.isValid()) {
350 const qint64 elapsed = wd->lastComposeTime.elapsed();
351 if (elapsed <= qint64(1000.0f / refresh))
352 updateTime = UpdateLater;
353 }
354 }
355
356 switch (updateTime) {
357 case UpdateLater:
358 // Prevent redundant update request events, unless it's a
359 // paint on screen widget, as these don't go through the
360 // normal backingstore sync machinery.
361 if (!widget->d_func()->shouldPaintOnScreen())
362 updateRequestSent = true;
363 QCoreApplication::postEvent(widget, new QEvent(QEvent::UpdateRequest), Qt::LowEventPriority);
364 break;
365 case UpdateNow: {
366 QEvent event(QEvent::UpdateRequest);
367 QCoreApplication::sendEvent(widget, &event);
368 break;
369 }
370 }
371}
372
373// ---------------------------------------------------------------------------
374
375static bool hasPlatformWindow(QWidget *widget)
376{
377 return widget && widget->windowHandle() && widget->windowHandle()->handle();
378}
379
380//parent's coordinates; move whole rect; update parent and widget
381//assume the screen blt has already been done, so we don't need to refresh that part
382void QWidgetPrivate::moveRect(const QRect &rect, int dx, int dy)
383{
384 Q_Q(QWidget);
385 if (!q->isVisible() || (dx == 0 && dy == 0))
386 return;
387
388 QWidget *tlw = q->window();
389
390 static const bool accelEnv = qEnvironmentVariableIntValue("QT_NO_FAST_MOVE") == 0;
391
392 QWidget *parentWidget = q->parentWidget();
393 QPoint toplevelOffset = parentWidget->mapTo(tlw, QPoint());
394 QWidgetPrivate *parentPrivate = parentWidget->d_func();
395 const QRect clipR(parentPrivate->clipRect());
396 const QRect newRect(rect.translated(dx, dy));
397 QRect destRect = rect.intersected(clipR);
398 if (destRect.isValid())
399 destRect = destRect.translated(dx, dy).intersected(clipR);
400 const QRect sourceRect(destRect.translated(-dx, -dy));
401 const QRect parentRect(rect & clipR);
402 const bool nativeWithTextureChild = textureChildSeen && hasPlatformWindow(q);
403
404 bool accelerateMove = accelEnv && isOpaque && !nativeWithTextureChild && sourceRect.isValid()
405#if QT_CONFIG(graphicsview)
406 // No accelerate move for proxy widgets.
407 && !tlw->d_func()->extra->proxyWidget
408#endif
409 && !isOverlapped(sourceRect) && !isOverlapped(destRect);
410
411 if (!accelerateMove) {
412 QRegion parentRegion(effectiveRectFor(parentRect));
413 if (!extra || !extra->hasMask) {
414 parentRegion -= newRect;
415 } else {
416 // invalidateBackingStore() excludes anything outside the mask
417 parentRegion += newRect & clipR;
418 }
419 parentPrivate->invalidateBackingStore(parentRegion);
420 invalidateBackingStore((newRect & clipR).translated(-data.crect.topLeft()));
421 } else {
422 QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(tlw)->maybeRepaintManager();
423 Q_ASSERT(repaintManager);
424 QRegion childExpose(newRect & clipR);
425
426 if (repaintManager->bltRect(sourceRect, dx, dy, parentWidget))
427 childExpose -= destRect;
428
429 if (!parentWidget->updatesEnabled())
430 return;
431
432 const bool childUpdatesEnabled = q->updatesEnabled();
433
434 if (childUpdatesEnabled && !childExpose.isEmpty()) {
435 childExpose.translate(-data.crect.topLeft());
436 repaintManager->markDirty(childExpose, q);
437 isMoved = true;
438 }
439
440 QRegion parentExpose(parentRect);
441 parentExpose -= newRect;
442 if (extra && extra->hasMask)
443 parentExpose += QRegion(newRect) - extra->mask.translated(data.crect.topLeft());
444
445 if (!parentExpose.isEmpty()) {
446 repaintManager->markDirty(parentExpose, parentWidget);
447 parentPrivate->isMoved = true;
448 }
449
450 if (childUpdatesEnabled) {
451 QRegion needsFlush(sourceRect);
452 needsFlush += destRect;
453 repaintManager->markNeedsFlush(parentWidget, needsFlush, toplevelOffset);
454 }
455 }
456}
457
458//widget's coordinates; scroll within rect; only update widget
459void QWidgetPrivate::scrollRect(const QRect &rect, int dx, int dy)
460{
461 Q_Q(QWidget);
462 QWidget *tlw = q->window();
463
464 QWidgetRepaintManager *repaintManager = QWidgetPrivate::get(tlw)->maybeRepaintManager();
465 if (!repaintManager)
466 return;
467
468 static const bool accelEnv = qEnvironmentVariableIntValue("QT_NO_FAST_SCROLL") == 0;
469
470 const QRect scrollRect = rect & clipRect();
471 bool overlapped = false;
472 bool accelerateScroll = accelEnv && isOpaque && !q_func()->testAttribute(Qt::WA_WState_InPaintEvent)
473 && !(overlapped = isOverlapped(scrollRect.translated(data.crect.topLeft())));
474
475 if (!accelerateScroll) {
476 if (overlapped) {
477 QRegion region(scrollRect);
478 subtractOpaqueSiblings(region);
479 invalidateBackingStore(region);
480 }else {
481 invalidateBackingStore(scrollRect);
482 }
483 } else {
484 const QPoint toplevelOffset = q->mapTo(tlw, QPoint());
485 const QRect destRect = scrollRect.translated(dx, dy) & scrollRect;
486 const QRect sourceRect = destRect.translated(-dx, -dy);
487
488 QRegion childExpose(scrollRect);
489 if (sourceRect.isValid()) {
490 if (repaintManager->bltRect(sourceRect, dx, dy, q))
491 childExpose -= destRect;
492 }
493
494 if (inDirtyList) {
495 if (rect == q->rect()) {
496 dirty.translate(dx, dy);
497 } else {
498 QRegion dirtyScrollRegion = dirty.intersected(scrollRect);
499 if (!dirtyScrollRegion.isEmpty()) {
500 dirty -= dirtyScrollRegion;
501 dirtyScrollRegion.translate(dx, dy);
502 dirty += dirtyScrollRegion;
503 }
504 }
505 }
506
507 if (!q->updatesEnabled())
508 return;
509
510 if (!childExpose.isEmpty()) {
511 repaintManager->markDirty(childExpose, q);
512 isScrolled = true;
513 }
514
515 // Instead of using native scroll-on-screen, we copy from
516 // backingstore, giving only one screen update for each
517 // scroll, and a solid appearance
518 repaintManager->markNeedsFlush(q, destRect, toplevelOffset);
519 }
520}
521
522/*
523 Moves the whole rect by (dx, dy) in widget's coordinate system.
524 Doesn't generate any updates.
525*/
526bool QWidgetRepaintManager::bltRect(const QRect &rect, int dx, int dy, QWidget *widget)
527{
528 const QPoint pos(widget->mapTo(tlw, rect.topLeft()));
529 const QRect tlwRect(QRect(pos, rect.size()));
530 if (dirty.intersects(tlwRect))
531 return false; // We don't want to scroll junk.
532 return store->scroll(tlwRect, dx, dy);
533}
534
535// ---------------------------------------------------------------------------
536
537static void findTextureWidgetsRecursively(QWidget *tlw, QWidget *widget,
538 QPlatformTextureList *widgetTextures,
539 QList<QWidget *> *nativeChildren)
540{
541 QWidgetPrivate *wd = QWidgetPrivate::get(widget);
542 if (wd->renderToTexture) {
543 QPlatformTextureList::Flags flags = wd->textureListFlags();
544 const QRect rect(widget->mapTo(tlw, QPoint()), widget->size());
545 QWidgetPrivate::TextureData data = wd->texture();
546 widgetTextures->appendTexture(widget, data.textureLeft, data.textureRight, rect, wd->clipRect(), flags);
547 }
548
549 for (int i = 0; i < wd->children.size(); ++i) {
550 QWidget *w = qobject_cast<QWidget *>(wd->children.at(i));
551 // Stop at native widgets but store them. Stop at hidden widgets too.
552 if (w && !w->isWindow() && hasPlatformWindow(w))
553 nativeChildren->append(w);
554 if (w && !w->isWindow() && !hasPlatformWindow(w) && !w->isHidden() && QWidgetPrivate::get(w)->textureChildSeen)
555 findTextureWidgetsRecursively(tlw, w, widgetTextures, nativeChildren);
556 }
557}
558
559static void findAllTextureWidgetsRecursively(QWidget *tlw, QWidget *widget)
560{
561 // textureChildSeen does not take native child widgets into account and that's good.
562 if (QWidgetPrivate::get(widget)->textureChildSeen) {
563 QList<QWidget *> nativeChildren;
564 auto tl = std::make_unique<QPlatformTextureList>();
565 // Look for texture widgets (incl. widget itself) from 'widget' down,
566 // but skip subtrees with a parent of a native child widget.
567 findTextureWidgetsRecursively(tlw, widget, tl.get(), &nativeChildren);
568 // tl may be empty regardless of textureChildSeen if we have native or hidden children.
569 if (!tl->isEmpty())
570 QWidgetPrivate::get(tlw)->topData()->widgetTextures.push_back(std::move(tl));
571 // Native child widgets, if there was any, get their own separate QPlatformTextureList.
572 for (QWidget *ncw : std::as_const(nativeChildren)) {
573 if (QWidgetPrivate::get(ncw)->textureChildSeen)
574 findAllTextureWidgetsRecursively(tlw, ncw);
575 }
576 }
577}
578
579static QPlatformTextureList *widgetTexturesFor(QWidget *tlw, QWidget *widget)
580{
581 for (const auto &tl : QWidgetPrivate::get(tlw)->topData()->widgetTextures) {
582 Q_ASSERT(!tl->isEmpty());
583 for (int i = 0; i < tl->count(); ++i) {
584 QWidget *w = static_cast<QWidget *>(tl->source(i));
585 if ((hasPlatformWindow(w) && w == widget) || (!hasPlatformWindow(w) && w->nativeParentWidget() == widget))
586 return tl.get();
587 }
588 }
589
590 if (QWidgetPrivate::get(widget)->textureChildSeen)
591 return qt_dummy_platformTextureList();
592
593 return nullptr;
594}
595
596// ---------------------------------------------------------------------------
597
598/*!
599 \class QWidgetRepaintManager
600 \inmodule QtWidgets
601 \internal
602*/
603
604/*!
605 Synchronizes the \a exposedRegion of the \a exposedWidget with the backing store.
606
607 If there are dirty widgets, including but not limited to the \a exposedWidget,
608 these will be repainted first. The backingstore is then flushed to the screen,
609 regardless of whether or not there were any repaints.
610*/
611void QWidgetRepaintManager::sync(QWidget *exposedWidget, const QRegion &exposedRegion)
612{
613 qCInfo(lcWidgetPainting) << "Syncing" << exposedRegion << "of" << exposedWidget;
614
615 if (!tlw->isVisible())
616 return;
617
618 if (!exposedWidget || !hasPlatformWindow(exposedWidget)
619 || !exposedWidget->isVisible() || !exposedWidget->testAttribute(Qt::WA_Mapped)
620 || !exposedWidget->updatesEnabled() || exposedRegion.isEmpty()) {
621 return;
622 }
623
624 // Nothing to repaint.
625 if (!isDirty() && store->size() == tlw->size()) {
626 QPlatformTextureList *widgetTextures = widgetTexturesFor(tlw, exposedWidget);
627 flush(exposedWidget, widgetTextures ? QRegion() : exposedRegion, widgetTextures);
628 return;
629 }
630
631 // As requests to sync a specific widget typically comes from an expose event
632 // we can't rely solely on our own dirty tracking to decide what to flush, and
633 // need to respect the platform's request to at least flush the entire widget,
634 QPoint offset = exposedWidget != tlw ? exposedWidget->mapTo(tlw, QPoint()) : QPoint();
635 markNeedsFlush(exposedWidget, exposedRegion, offset);
636
637 if (syncAllowed())
638 paintAndFlush();
639}
640
641/*!
642 Synchronizes the backing store, i.e. dirty areas are repainted and flushed.
643*/
644void QWidgetRepaintManager::sync()
645{
646 qCInfo(lcWidgetPainting) << "Syncing dirty widgets";
647
648 updateRequestSent = false;
649 if (qt_widget_private(tlw)->shouldDiscardSyncRequest()) {
650 // If the top-level is minimized, it's not visible on the screen so we can delay the
651 // update until it's shown again. In order to do that we must keep the dirty states.
652 // These will be cleared when we receive the first expose after showNormal().
653 // However, if the widget is not visible (isVisible() returns false), everything will
654 // be invalidated once the widget is shown again, so clear all dirty states.
655 if (!tlw->isVisible()) {
656 dirty = QRegion();
657 for (int i = 0; i < dirtyWidgets.size(); ++i)
658 resetWidget(dirtyWidgets.at(i));
659 dirtyWidgets.clear();
660 }
661 return;
662 }
663
664 if (syncAllowed())
665 paintAndFlush();
666}
667
668bool QWidgetPrivate::shouldDiscardSyncRequest() const
669{
670 Q_Q(const QWidget);
671 return !maybeTopData() || !q->testAttribute(Qt::WA_Mapped) || !q->isVisible();
672}
673
674bool QWidgetRepaintManager::syncAllowed()
675{
676 QTLWExtra *tlwExtra = tlw->d_func()->maybeTopData();
677 if (textureListWatcher && !textureListWatcher->isLocked()) {
678 textureListWatcher->deleteLater();
679 textureListWatcher = nullptr;
680 } else if (!tlwExtra->widgetTextures.empty()) {
681 bool skipSync = false;
682 for (const auto &tl : tlwExtra->widgetTextures) {
683 if (tl->isLocked()) {
684 if (!textureListWatcher)
685 textureListWatcher = new QPlatformTextureListWatcher(this);
686 if (!textureListWatcher->isLocked())
687 textureListWatcher->watch(tl.get());
688 skipSync = true;
689 }
690 }
691 if (skipSync) // cannot compose due to widget textures being in use
692 return false;
693 }
694 return true;
695}
696
697static bool isDrawnInEffect(const QWidget *w)
698{
699#if QT_CONFIG(graphicseffect)
700 do {
701 if (w->graphicsEffect())
702 return true;
703 w = w->parentWidget();
704 } while (w);
705#endif
706 return false;
707}
708
709void QWidgetRepaintManager::paintAndFlush()
710{
711 qCInfo(lcWidgetPainting) << "Painting and flushing dirty"
712 << "top level" << dirty << "and dirty widgets" << dirtyWidgets;
713
714 const bool updatesDisabled = !tlw->updatesEnabled();
715 bool repaintAllWidgets = false;
716
717 const QRect tlwRect = tlw->data->crect;
718 if (!updatesDisabled && store->size() != tlwRect.size()) {
719 QPlatformIntegration *integration = QGuiApplicationPrivate::platformIntegration();
720 if (hasStaticContents() && !store->size().isEmpty()
721 && integration->hasCapability(QPlatformIntegration::BackingStoreStaticContents)) {
722 // Repaint existing dirty area and newly visible area.
723 const QRect clipRect(QPoint(0, 0), store->size());
724 const QRegion staticRegion(staticContents(nullptr, clipRect));
725 QRegion newVisible(0, 0, tlwRect.width(), tlwRect.height());
726 newVisible -= staticRegion;
727 dirty += newVisible;
728 store->setStaticContents(staticRegion);
729 } else {
730 // Repaint everything.
731 dirty = QRegion(0, 0, tlwRect.width(), tlwRect.height());
732 for (int i = 0; i < dirtyWidgets.size(); ++i)
733 resetWidget(dirtyWidgets.at(i));
734 dirtyWidgets.clear();
735 repaintAllWidgets = true;
736 }
737 }
738
739 if (store->size() != tlwRect.size())
740 store->resize(tlwRect.size());
741
742 if (updatesDisabled)
743 return;
744
745 // Contains everything that needs repaint.
746 QRegion toClean(dirty);
747
748 // Loop through all update() widgets and remove them from the list before they are
749 // painted (in case someone calls update() in paintEvent). If the widget is opaque
750 // and does not have transparent overlapping siblings, append it to the
751 // opaqueNonOverlappedWidgets list and paint it directly without composition.
752 QVarLengthArray<QWidget *, 32> opaqueNonOverlappedWidgets;
753 for (int i = 0; i < dirtyWidgets.size(); ++i) {
754 QWidget *w = dirtyWidgets.at(i);
755 QWidgetPrivate *wd = w->d_func();
756 if (wd->data.in_destructor)
757 continue;
758
759 // Clip with mask() and clipRect().
760 wd->dirty &= wd->clipRect();
761 wd->clipToEffectiveMask(wd->dirty);
762
763 // Subtract opaque siblings and children.
764 bool hasDirtySiblingsAbove = false;
765 // We know for sure that the widget isn't overlapped if 'isMoved' is true.
766 if (!wd->isMoved)
767 wd->subtractOpaqueSiblings(wd->dirty, &hasDirtySiblingsAbove);
768
769 // Make a copy of the widget's dirty region, to restore it in case there is an opaque
770 // render-to-texture child that completely covers the widget, because otherwise the
771 // render-to-texture child won't be visible, due to its parent widget not being redrawn
772 // with a proper blending mask.
773 const QRegion dirtyBeforeSubtractedOpaqueChildren = wd->dirty;
774
775 // Scrolled and moved widgets must draw all children.
776 if (!wd->isScrolled && !wd->isMoved)
777 wd->subtractOpaqueChildren(wd->dirty, w->rect());
778
779 if (wd->dirty.isEmpty() && wd->textureChildSeen)
780 wd->dirty = dirtyBeforeSubtractedOpaqueChildren;
781
782 if (wd->dirty.isEmpty()) {
783 resetWidget(w);
784 continue;
785 }
786
787 const QRegion widgetDirty(w != tlw ? wd->dirty.translated(w->mapTo(tlw, QPoint()))
788 : wd->dirty);
789 toClean += widgetDirty;
790
791#if QT_CONFIG(graphicsview)
792 if (tlw->d_func()->extra->proxyWidget) {
793 resetWidget(w);
794 continue;
795 }
796#endif
797
798 if (!isDrawnInEffect(w) && !hasDirtySiblingsAbove && wd->isOpaque
799 && !dirty.intersects(widgetDirty.boundingRect())) {
800 opaqueNonOverlappedWidgets.append(w);
801 } else {
802 resetWidget(w);
803 dirty += widgetDirty;
804 }
805 }
806 dirtyWidgets.clear();
807
808 if (toClean.isEmpty()) {
809 // Nothing to repaint. However renderToTexture widgets are handled
810 // specially, they are not in the regular dirty list, in order to
811 // prevent triggering unnecessary backingstore painting when only the
812 // texture content changes. Check if we have such widgets in the special
813 // dirty list.
814 QVarLengthArray<QWidget *, 16> paintPending;
815 const int numPaintPending = dirtyRenderToTextureWidgets.size();
816 paintPending.reserve(numPaintPending);
817 for (int i = 0; i < numPaintPending; ++i) {
818 QWidget *w = dirtyRenderToTextureWidgets.at(i);
819 paintPending << w;
820 resetWidget(w);
821 }
822 dirtyRenderToTextureWidgets.clear();
823 for (int i = 0; i < numPaintPending; ++i) {
824 QWidget *w = paintPending[i];
825 w->d_func()->sendPaintEvent(w->rect());
826 if (w != tlw) {
827 QWidget *npw = w->nativeParentWidget();
828 if (hasPlatformWindow(w) || (npw && npw != tlw)) {
829 if (!hasPlatformWindow(w))
830 w = npw;
831 markNeedsFlush(w);
832 }
833 }
834 }
835
836 // We might have newly exposed areas on the screen if this function was
837 // called from sync(QWidget *, QRegion)), so we have to make sure those
838 // are flushed. We also need to composite the renderToTexture widgets.
839 flush();
840
841 return;
842 }
843
844 // Find all render-to-texture child widgets (including self).
845 // The search is cut at native widget boundaries, meaning that each native child widget
846 // has its own list for the subtree below it.
847 QTLWExtra *tlwExtra = tlw->d_func()->topData();
848 tlwExtra->widgetTextures.clear();
849 findAllTextureWidgetsRecursively(tlw, tlw);
850 for (const auto &tl : tlwExtra->widgetTextures) {
851 for (int i = 0; i < tl->count(); ++i) {
852 QWidget *w = static_cast<QWidget *>(tl->source(i));
853 if (dirtyRenderToTextureWidgets.contains(w)) {
854 const QRect rect = tl->geometry(i); // mapped to the tlw already
855 // Set a flag to indicate that the paint event for this
856 // render-to-texture widget must not to be optimized away.
857 w->d_func()->renderToTextureReallyDirty = 1;
858 dirty += rect;
859 toClean += rect;
860 }
861 }
862 }
863 for (int i = 0; i < dirtyRenderToTextureWidgets.size(); ++i)
864 resetWidget(dirtyRenderToTextureWidgets.at(i));
865 dirtyRenderToTextureWidgets.clear();
866
867#if QT_CONFIG(graphicsview)
868 if (tlw->d_func()->extra->proxyWidget) {
869 updateStaticContentsSize();
870 dirty = QRegion();
871 updateRequestSent = false;
872 for (const QRect &rect : toClean)
873 tlw->d_func()->extra->proxyWidget->update(rect);
874 return;
875 }
876#endif
877
878 store->beginPaint(toClean);
879
880 // Must do this before sending any paint events because
881 // the size may change in the paint event.
882 updateStaticContentsSize();
883 const QRegion dirtyCopy(dirty);
884 dirty = QRegion();
885 updateRequestSent = false;
886
887 // Paint opaque non overlapped widgets.
888 for (int i = 0; i < opaqueNonOverlappedWidgets.size(); ++i) {
889 QWidget *w = opaqueNonOverlappedWidgets[i];
890 QWidgetPrivate *wd = w->d_func();
891
892 QWidgetPrivate::DrawWidgetFlags flags = QWidgetPrivate::DrawRecursive;
893 // Scrolled and moved widgets must draw all children.
894 if (!wd->isScrolled && !wd->isMoved)
895 flags |= QWidgetPrivate::DontDrawOpaqueChildren;
896 if (w == tlw)
897 flags |= QWidgetPrivate::DrawAsRoot;
898
899 QRegion toBePainted(wd->dirty);
900 resetWidget(w);
901
902 QPoint offset;
903 if (w != tlw)
904 offset += w->mapTo(tlw, QPoint());
905 wd->drawWidget(store->paintDevice(), toBePainted, offset, flags, nullptr, this);
906 }
907
908 // Paint the rest with composition.
909 if (repaintAllWidgets || !dirtyCopy.isEmpty()) {
910 QWidgetPrivate::DrawWidgetFlags flags = QWidgetPrivate::DrawAsRoot | QWidgetPrivate::DrawRecursive
911 | QWidgetPrivate::UseEffectRegionBounds;
912 tlw->d_func()->drawWidget(store->paintDevice(), dirtyCopy, QPoint(), flags, nullptr, this);
913 }
914
915 store->endPaint();
916
917 flush();
918}
919
920/*!
921 Marks the \a region of the \a widget as needing a flush. The \a region will be copied from
922 the backing store to the \a widget's native parent next time flush() is called.
923
924 Paint on screen widgets are ignored.
925*/
926void QWidgetRepaintManager::markNeedsFlush(QWidget *widget, const QRegion &region, const QPoint &topLevelOffset)
927{
928 if (!widget || widget->d_func()->shouldPaintOnScreen() || region.isEmpty())
929 return;
930
931 if (widget == tlw) {
932 // Top-level (native)
933 qCInfo(lcWidgetPainting) << "Marking" << region << "of top level"
934 << widget << "as needing flush";
935 topLevelNeedsFlush += region;
936 } else if (!hasPlatformWindow(widget) && !widget->isWindow()) {
937 QWidget *nativeParent = widget->nativeParentWidget();
938 qCInfo(lcWidgetPainting) << "Marking" << region << "of"
939 << widget << "as needing flush in" << nativeParent
940 << "at offset" << topLevelOffset;
941 if (nativeParent == tlw) {
942 // Alien widgets with the top-level as the native parent (common case)
943 topLevelNeedsFlush += region.translated(topLevelOffset);
944 } else {
945 // Alien widgets with native parent != tlw
946 const QPoint nativeParentOffset = widget->mapTo(nativeParent, QPoint());
947 markNeedsFlush(nativeParent, region.translated(nativeParentOffset));
948 }
949 } else {
950 // Native child widgets
951 qCInfo(lcWidgetPainting) << "Marking" << region
952 << "of native child" << widget << "as needing flush";
953 markNeedsFlush(widget, region);
954 }
955}
956
957void QWidgetRepaintManager::markNeedsFlush(QWidget *widget, const QRegion &region)
958{
959 if (!widget)
960 return;
961
962 auto *widgetPrivate = qt_widget_private(widget);
963 if (!widgetPrivate->needsFlush)
964 widgetPrivate->needsFlush = new QRegion;
965
966 *widgetPrivate->needsFlush += region;
967
968 if (!needsFlushWidgets.contains(widget))
969 needsFlushWidgets.append(widget);
970}
971
972/*!
973 Flushes the contents of the backing store into the top-level widget.
974*/
975void QWidgetRepaintManager::flush()
976{
977 qCInfo(lcWidgetPainting) << "Flushing top level"
978 << topLevelNeedsFlush << "and children" << needsFlushWidgets;
979
980 // Paint events may have changed the render-to-texture widgets. For example,
981 // an item view may resize an index widget, replacing its texture. Collect
982 // the current textures and geometry before flushing either painting path.
983 tlw->d_func()->topData()->widgetTextures.clear();
984 findAllTextureWidgetsRecursively(tlw, tlw);
985
986 const bool hasNeedsFlushWidgets = !needsFlushWidgets.isEmpty();
987 bool flushed = false;
988
989 // Flush the top level widget
990 if (!topLevelNeedsFlush.isEmpty()) {
991 flush(tlw, topLevelNeedsFlush, widgetTexturesFor(tlw, tlw));
992 topLevelNeedsFlush = QRegion();
993 flushed = true;
994 }
995
996 // Render-to-texture widgets are not in topLevelNeedsFlush so flush if we have not done it above.
997 if (!flushed && !hasNeedsFlushWidgets) {
998 if (!tlw->d_func()->topData()->widgetTextures.empty()) {
999 if (QPlatformTextureList *widgetTextures = widgetTexturesFor(tlw, tlw))
1000 flush(tlw, QRegion(), widgetTextures);
1001 }
1002 }
1003
1004 if (!hasNeedsFlushWidgets)
1005 return;
1006
1007 for (QWidget *w : std::exchange(needsFlushWidgets, {})) {
1008 QWidgetPrivate *wd = w->d_func();
1009 Q_ASSERT(wd->needsFlush);
1010 QPlatformTextureList *widgetTexturesForNative = wd->textureChildSeen ? widgetTexturesFor(tlw, w) : nullptr;
1011 flush(w, *wd->needsFlush, widgetTexturesForNative);
1012 *wd->needsFlush = QRegion();
1013 }
1014}
1015
1016/*
1017 Flushes the contents of the backingstore into the screen area of \a widget.
1018
1019 \a region is the region to be updated in \a widget coordinates.
1020 */
1021void QWidgetRepaintManager::flush(QWidget *widget, const QRegion &region, QPlatformTextureList *widgetTextures)
1022{
1023 Q_ASSERT(!region.isEmpty() || widgetTextures);
1024 Q_ASSERT(widget);
1025 Q_ASSERT(tlw);
1026
1027 if (tlw->testAttribute(Qt::WA_DontShowOnScreen) || widget->testAttribute(Qt::WA_DontShowOnScreen))
1028 return;
1029
1030 QWindow *window = widget->windowHandle();
1031 // We should only be flushing to native widgets
1032 Q_ASSERT(window);
1033
1034 // Foreign Windows do not have backing store content and must not be flushed
1035 if (window->type() == Qt::ForeignWindow)
1036 return;
1037
1038 static bool fpsDebug = qEnvironmentVariableIntValue("QT_DEBUG_FPS");
1039 if (fpsDebug) {
1040 if (!perfFrames++)
1041 perfTime.start();
1042 if (perfTime.elapsed() > 5000) {
1043 double fps = double(perfFrames * 1000) / perfTime.restart();
1044 qDebug("FPS: %.1f\n", fps);
1045 perfFrames = 0;
1046 }
1047 }
1048
1049 QPoint offset;
1050 if (widget != tlw)
1051 offset += widget->mapTo(tlw, QPoint());
1052
1053 // A widget uses RHI flush if itself, or one of its non-native children
1054 // uses RHI for its drawing. If so, we composite the backing store raster
1055 // data along with textures produced by the RHI widgets.
1056 const bool flushWithRhi = widget->d_func()->usesRhiFlush;
1057
1058 qCDebug(lcWidgetPainting) << "Flushing" << region << "of" << widget
1059 << "to" << window << (flushWithRhi ? "using RHI" : "");
1060
1061 // A widget uses RHI flush if itself, or one of its non-native children
1062 // uses RHI for its drawing. If so, we composite the backing store raster
1063 // data along with textures produced by the RHI widgets.
1064 if (flushWithRhi) {
1065 if (!widgetTextures)
1066 widgetTextures = qt_dummy_platformTextureList;
1067
1068 // We only need to let the widget sub-hierarchy that
1069 // we are flushing know that we're compositing.
1070 auto *widgetPrivate = QWidgetPrivate::get(widget);
1071 widgetPrivate->sendComposeStatus(widget, false);
1072
1073 // A window may have alpha even when the app did not request
1074 // WA_TranslucentBackground. Therefore the compositor needs to know whether the app intends
1075 // to rely on translucency, in order to decide if it should clear to transparent or opaque.
1076 const bool translucentBackground = widget->testAttribute(Qt::WA_TranslucentBackground);
1077
1078 QPlatformBackingStore::FlushResult flushResult;
1079 flushResult = store->handle()->rhiFlush(window,
1080 widget->devicePixelRatio(),
1081 region,
1082 offset,
1083 widgetTextures,
1084 translucentBackground);
1085
1086 widgetPrivate->sendComposeStatus(widget, true);
1087
1088 if (flushResult == QPlatformBackingStore::FlushFailedDueToLostDevice) {
1089 qSendWindowChangeToTextureChildrenRecursively(widget->window(),
1090 QEvent::WindowAboutToChangeInternal);
1091 store->handle()->graphicsDeviceReportedLost(window);
1092 qSendWindowChangeToTextureChildrenRecursively(widget->window(),
1093 QEvent::WindowChangeInternal);
1094 widget->update();
1095 }
1096 } else {
1097 store->flush(region, window, offset);
1098 }
1099}
1100
1101// ---------------------------------------------------------------------------
1102
1103void QWidgetRepaintManager::addStaticWidget(QWidget *widget)
1104{
1105 if (!widget)
1106 return;
1107
1108 Q_ASSERT(widget->testAttribute(Qt::WA_StaticContents));
1109 if (!staticWidgets.contains(widget))
1110 staticWidgets.append(widget);
1111}
1112
1113// Move the reparented widget and all its static children from this backing store
1114// to the new backing store if reparented into another top-level / backing store.
1115void QWidgetRepaintManager::moveStaticWidgets(QWidget *reparented)
1116{
1117 Q_ASSERT(reparented);
1118 QWidgetRepaintManager *newPaintManager = reparented->d_func()->maybeRepaintManager();
1119 if (newPaintManager == this)
1120 return;
1121
1122 int i = 0;
1123 while (i < staticWidgets.size()) {
1124 QWidget *w = staticWidgets.at(i);
1125 if (reparented == w || reparented->isAncestorOf(w)) {
1126 staticWidgets.removeAt(i);
1127 if (newPaintManager)
1128 newPaintManager->addStaticWidget(w);
1129 } else {
1130 ++i;
1131 }
1132 }
1133}
1134
1135void QWidgetRepaintManager::removeStaticWidget(QWidget *widget)
1136{
1137 staticWidgets.removeAll(widget);
1138}
1139
1140bool QWidgetRepaintManager::hasStaticContents() const
1141{
1142 return !staticWidgets.isEmpty();
1143}
1144
1145/*!
1146 Returns the static content inside the \a parent if non-zero; otherwise the static content
1147 for the entire backing store is returned. The content will be clipped to \a withinClipRect
1148 if non-empty.
1149*/
1150QRegion QWidgetRepaintManager::staticContents(QWidget *parent, const QRect &withinClipRect) const
1151{
1152 if (!parent && tlw->testAttribute(Qt::WA_StaticContents)) {
1153 QRect backingstoreRect(QPoint(0, 0), store->size());
1154 if (!withinClipRect.isEmpty())
1155 backingstoreRect &= withinClipRect;
1156 return QRegion(backingstoreRect);
1157 }
1158
1159 QRegion region;
1160 if (parent && parent->d_func()->children.isEmpty())
1161 return region;
1162
1163 const bool clipToRect = !withinClipRect.isEmpty();
1164 const int count = staticWidgets.size();
1165 for (int i = 0; i < count; ++i) {
1166 QWidget *w = staticWidgets.at(i);
1167 QWidgetPrivate *wd = w->d_func();
1168 if (!wd->isOpaque || !wd->extra || wd->extra->staticContentsSize.isEmpty()
1169 || !w->isVisible() || (parent && !parent->isAncestorOf(w))) {
1170 continue;
1171 }
1172
1173 QRect rect(0, 0, wd->extra->staticContentsSize.width(), wd->extra->staticContentsSize.height());
1174 const QPoint offset = w->mapTo(parent ? parent : tlw, QPoint());
1175 if (clipToRect)
1176 rect &= withinClipRect.translated(-offset);
1177 if (rect.isEmpty())
1178 continue;
1179
1180 rect &= wd->clipRect();
1181 if (rect.isEmpty())
1182 continue;
1183
1184 QRegion visible(rect);
1185 wd->clipToEffectiveMask(visible);
1186 if (visible.isEmpty())
1187 continue;
1188 wd->subtractOpaqueSiblings(visible, nullptr, /*alsoNonOpaque=*/true);
1189
1190 visible.translate(offset);
1191 region += visible;
1192 }
1193
1194 return region;
1195}
1196
1197void QWidgetRepaintManager::updateStaticContentsSize()
1198{
1199 for (int i = 0; i < staticWidgets.size(); ++i) {
1200 QWidgetPrivate *wd = staticWidgets.at(i)->d_func();
1201 if (!wd->extra)
1202 wd->createExtra();
1203 wd->extra->staticContentsSize = wd->data.crect.size();
1204 }
1205}
1206
1207// ---------------------------------------------------------------------------
1208
1209bool QWidgetRepaintManager::isDirty() const
1210{
1211 return !(dirtyWidgets.isEmpty() && dirty.isEmpty() && dirtyRenderToTextureWidgets.isEmpty());
1212}
1213
1214/*!
1215 \class QWidgetPrivate
1216 \inmodule QtWidgets
1217 \internal
1218*/
1219
1220/*!
1221 Invalidates the backing store when the widget is resized.
1222 Static areas are never invalidated unless absolutely needed.
1223*/
1224void QWidgetPrivate::invalidateBackingStore_resizeHelper(const QPoint &oldPos, const QSize &oldSize)
1225{
1226 Q_Q(QWidget);
1227 Q_ASSERT(!q->isWindow());
1228 Q_ASSERT(q->parentWidget());
1229
1230 const bool staticContents = q->testAttribute(Qt::WA_StaticContents);
1231 const bool sizeDecreased = (data.crect.width() < oldSize.width())
1232 || (data.crect.height() < oldSize.height());
1233
1234 const QPoint offset(data.crect.x() - oldPos.x(), data.crect.y() - oldPos.y());
1235 const bool parentAreaExposed = !offset.isNull() || sizeDecreased;
1236 const QRect newWidgetRect(q->rect());
1237 const QRect oldWidgetRect(0, 0, oldSize.width(), oldSize.height());
1238
1239 if (!staticContents || graphicsEffect) {
1240 QRegion staticChildren;
1241 QWidgetRepaintManager *bs = nullptr;
1242 if (offset.isNull() && (bs = maybeRepaintManager()))
1243 staticChildren = bs->staticContents(q, oldWidgetRect);
1244 const bool hasStaticChildren = !staticChildren.isEmpty();
1245
1246 if (hasStaticChildren) {
1247 QRegion dirty(newWidgetRect);
1248 dirty -= staticChildren;
1249 invalidateBackingStore(dirty);
1250 } else {
1251 // Entire widget needs repaint.
1252 invalidateBackingStore(newWidgetRect);
1253 }
1254
1255 if (!parentAreaExposed)
1256 return;
1257
1258 // Invalidate newly exposed area of the parent.
1259 if (!graphicsEffect && extra && extra->hasMask) {
1260 QRegion parentExpose(extra->mask.translated(oldPos));
1261 parentExpose &= QRect(oldPos, oldSize);
1262 if (hasStaticChildren)
1263 parentExpose -= data.crect; // Offset is unchanged, safe to do this.
1264 q->parentWidget()->d_func()->invalidateBackingStore(parentExpose);
1265 } else {
1266 if (hasStaticChildren && !graphicsEffect) {
1267 QRegion parentExpose(QRect(oldPos, oldSize));
1268 parentExpose -= data.crect; // Offset is unchanged, safe to do this.
1269 q->parentWidget()->d_func()->invalidateBackingStore(parentExpose);
1270 } else {
1271 q->parentWidget()->d_func()->invalidateBackingStore(effectiveRectFor(QRect(oldPos, oldSize)));
1272 }
1273 }
1274 return;
1275 }
1276
1277 // Move static content to its new position.
1278 if (!offset.isNull()) {
1279 if (sizeDecreased) {
1280 const QSize minSize(qMin(oldSize.width(), data.crect.width()),
1281 qMin(oldSize.height(), data.crect.height()));
1282 moveRect(QRect(oldPos, minSize), offset.x(), offset.y());
1283 } else {
1284 moveRect(QRect(oldPos, oldSize), offset.x(), offset.y());
1285 }
1286 }
1287
1288 // Invalidate newly visible area of the widget.
1289 if (!sizeDecreased || !oldWidgetRect.contains(newWidgetRect)) {
1290 QRegion newVisible(newWidgetRect);
1291 newVisible -= oldWidgetRect;
1292 invalidateBackingStore(newVisible);
1293 }
1294
1295 if (!parentAreaExposed)
1296 return;
1297
1298 // Invalidate newly exposed area of the parent.
1299 const QRect oldRect(oldPos, oldSize);
1300 if (extra && extra->hasMask) {
1301 QRegion parentExpose(oldRect);
1302 parentExpose &= extra->mask.translated(oldPos);
1303 parentExpose -= (extra->mask.translated(data.crect.topLeft()) & data.crect);
1304 q->parentWidget()->d_func()->invalidateBackingStore(parentExpose);
1305 } else {
1306 QRegion parentExpose(oldRect);
1307 parentExpose -= data.crect;
1308 q->parentWidget()->d_func()->invalidateBackingStore(parentExpose);
1309 }
1310}
1311
1312QT_END_NAMESPACE
1313
1314#include "qwidgetrepaintmanager.moc"
1315#include "moc_qwidgetrepaintmanager_p.cpp"
Combined button and popup list for selecting options.
static void findAllTextureWidgetsRecursively(QWidget *tlw, QWidget *widget)
static QPlatformTextureList * widgetTexturesFor(QWidget *tlw, QWidget *widget)
static bool isDrawnInEffect(const QWidget *w)
static void findTextureWidgetsRecursively(QWidget *tlw, QWidget *widget, QPlatformTextureList *widgetTextures, QList< QWidget * > *nativeChildren)
static QRect widgetRectFor(QWidget *, const QRect &r)
static bool hasPlatformWindow(QWidget *widget)