From 97f7819f1d9a2f97e61e50defc93c466bcc7cfe6 Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 13 May 2025 14:38:13 +0200 Subject: [PATCH] offscreen: Enable backingstore images with alpha MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Grabbing a QQuickWindow (software backend) cleared to something semi-transparent is not going to be correct otherwise. Pick-to: 6.9 6.8 Task-number: QTBUG-136755 Change-Id: I59f378b177154af25b1d7bff878435c715e3d114 Reviewed-by: Tor Arne Vestbø --- .../platforms/offscreen/qoffscreencommon.cpp | 15 ++++++++++++++- .../platforms/offscreen/qoffscreencommon.h | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/plugins/platforms/offscreen/qoffscreencommon.cpp b/src/plugins/platforms/offscreen/qoffscreencommon.cpp index 923fffb29c6..3fca2302068 100644 --- a/src/plugins/platforms/offscreen/qoffscreencommon.cpp +++ b/src/plugins/platforms/offscreen/qoffscreencommon.cpp @@ -148,7 +148,9 @@ void QOffscreenBackingStore::flush(QWindow *window, const QRegion ®ion, const void QOffscreenBackingStore::resize(const QSize &size, const QRegion &) { - QImage::Format format = QGuiApplication::primaryScreen()->handle()->format(); + QImage::Format format = window()->format().hasAlpha() + ? QImage::Format_ARGB32_Premultiplied + : QGuiApplication::primaryScreen()->handle()->format(); if (m_image.size() != size) m_image = QImage(size, format); clearHash(); @@ -167,6 +169,17 @@ bool QOffscreenBackingStore::scroll(const QRegion &area, int dx, int dy) return true; } +void QOffscreenBackingStore::beginPaint(const QRegion ®ion) +{ + if (QImage::toPixelFormat(m_image.format()).alphaUsage() == QPixelFormat::UsesAlpha) { + QPainter p(&m_image); + p.setCompositionMode(QPainter::CompositionMode_Source); + const QColor blank = Qt::transparent; + for (const QRect &r : region) + p.fillRect(r, blank); + } +} + QPixmap QOffscreenBackingStore::grabWindow(WId window, const QRect &rect) const { QRect area = m_windowAreaHash.value(window, QRect()); diff --git a/src/plugins/platforms/offscreen/qoffscreencommon.h b/src/plugins/platforms/offscreen/qoffscreencommon.h index a3d62271688..787af87cfff 100644 --- a/src/plugins/platforms/offscreen/qoffscreencommon.h +++ b/src/plugins/platforms/offscreen/qoffscreencommon.h @@ -68,6 +68,7 @@ public: void flush(QWindow *window, const QRegion ®ion, const QPoint &offset) override; void resize(const QSize &size, const QRegion &staticContents) override; bool scroll(const QRegion &area, int dx, int dy) override; + void beginPaint(const QRegion &) override; QPixmap grabWindow(WId window, const QRect &rect) const; QImage toImage() const override { return m_image; }