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
qwasmbackingstore.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include "qwasmwindow.h"
8#include "qwasmdom.h"
9
10#include <QtGui/qpainter.h>
11#include <QtGui/qbackingstore.h>
12
13#include <emscripten.h>
14#include <emscripten/wire.h>
15
17
18QWasmBackingStore::QWasmBackingStore(QWasmCompositor *compositor, QWindow *window)
19 : QPlatformBackingStore(window), m_compositor(compositor)
20{
21 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
22 if (wasmWindow)
23 wasmWindow->setBackingStore(this);
24}
25
27{
28 auto window = this->window();
29 QWasmIntegration::get()->removeBackingStore(window);
30 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
31 if (wasmWindow)
32 wasmWindow->setBackingStore(nullptr);
33}
34
36{
37 return &m_image;
38}
39
40void QWasmBackingStore::flush(QWindow *window, const QRegion &region, const QPoint &offset)
41{
42 Q_UNUSED(window);
43 m_dirty |= region;
44
45 QRect updateRect = region.boundingRect();
46 updateRect.translate(offset);
47
48 m_compositor->handleBackingStoreFlush(this->window(), updateRect);
49}
50
52{
53 if (m_dirty.isNull())
54 return;
55
56 if (m_webImageDataArray.isUndefined()) {
57 m_webImageDataArray = window->context2d().call<emscripten::val>(
58 "createImageData", emscripten::val(m_image.width()),
59 emscripten::val(m_image.height()));
60 }
61
62 QRegion clippedDpiScaledRegion;
63 QRect imageRect = m_image.rect();
64
65 for (const QRect &rect : m_dirty) {
66 // Convert device-independent dirty region to device region
67 qreal dpr = m_image.devicePixelRatio();
68 QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);
69
70 // intersect with image rect to be sure
71 QRect r = imageRect & deviceRect;
72 // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
73 if (r.width() >= imageRect.width() / 2) {
74 r.setX(0);
75 r.setWidth(imageRect.width());
76 }
77
78 clippedDpiScaledRegion |= r;
79 }
80
81 for (const QRect &dirtyRect : clippedDpiScaledRegion)
82 dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect);
83
84 m_dirty = QRegion();
85}
86
87void QWasmBackingStore::beginPaint(const QRegion &region)
88{
89 m_dirty |= region;
90 // Keep backing store device pixel ratio in sync with window
91 if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
92 resize(backingStore()->size(), backingStore()->staticContents());
93
94 QPainter painter(&m_image);
95
96 if (m_image.hasAlphaChannel()) {
97 painter.setCompositionMode(QPainter::CompositionMode_Source);
98 const QColor blank = Qt::transparent;
99 for (const QRect &rect : region)
100 painter.fillRect(rect, blank);
101 }
102}
103
104void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
105{
106 Q_UNUSED(staticContents);
107
108 QImage::Format format = QImage::Format_RGBA8888;
109 const auto platformScreenDPR = window()->handle()->devicePixelRatio();
110 m_image = QImage(size * platformScreenDPR, format);
111 m_image.setDevicePixelRatio(platformScreenDPR);
112 m_webImageDataArray = emscripten::val::undefined();
113}
114
116{
117 // used by QPlatformBackingStore::rhiFlush
118 return m_image;
119}
120
122{
123 return m_image;
124}
125
127{
128 updateTexture(window);
129 return m_webImageDataArray;
130}
131
132QT_END_NAMESPACE
\inmodule QtCore\reentrant
Definition qpoint.h:30
emscripten::val getUpdatedWebImage(QWasmWindow *window)
void beginPaint(const QRegion &) override
This function is called before painting onto the surface begins, with the region in which the paintin...
void resize(const QSize &size, const QRegion &staticContents) override
void updateTexture(QWasmWindow *window)
QPaintDevice * paintDevice() override
Implement this function to return the appropriate paint device.
const QImage & getImageRef() const
QImage toImage() const override
Implemented in subclasses to return the content of the backingstore as a QImage.
static QWasmIntegration * get()
friend class QWasmCompositor
void setBackingStore(QWasmBackingStore *store)
Definition qwasmwindow.h:96
Combined button and popup list for selecting options.