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
5#include "qwasmwindow.h"
7#include "qwasmdom.h"
8
9#include <QtGui/qpainter.h>
10#include <QtGui/qbackingstore.h>
11
12#include <emscripten.h>
13#include <emscripten/wire.h>
14
16
17QWasmBackingStore::QWasmBackingStore(QWasmCompositor *compositor, QWindow *window)
18 : QPlatformBackingStore(window), m_compositor(compositor)
19{
20 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
21 if (wasmWindow)
22 wasmWindow->setBackingStore(this);
23}
24
26{
27 auto window = this->window();
28 QWasmIntegration::get()->removeBackingStore(window);
29 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
30 if (wasmWindow)
31 wasmWindow->setBackingStore(nullptr);
32}
33
35{
36 return &m_image;
37}
38
39void QWasmBackingStore::flush(QWindow *window, const QRegion &region, const QPoint &offset)
40{
41 Q_UNUSED(window);
42 m_dirty |= region;
43
44 QRect updateRect = region.boundingRect();
45 updateRect.translate(offset);
46
47 m_compositor->handleBackingStoreFlush(this->window(), updateRect);
48}
49
51{
52 if (m_dirty.isNull())
53 return;
54
55 if (m_webImageDataArray.isUndefined()) {
56 m_webImageDataArray = window->context2d().call<emscripten::val>(
57 "createImageData", emscripten::val(m_image.width()),
58 emscripten::val(m_image.height()));
59 }
60
61 QRegion clippedDpiScaledRegion;
62 QRect imageRect = m_image.rect();
63
64 for (const QRect &rect : m_dirty) {
65 // Convert device-independent dirty region to device region
66 qreal dpr = m_image.devicePixelRatio();
67 QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);
68
69 // intersect with image rect to be sure
70 QRect r = imageRect & deviceRect;
71 // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
72 if (r.width() >= imageRect.width() / 2) {
73 r.setX(0);
74 r.setWidth(imageRect.width());
75 }
76
77 clippedDpiScaledRegion |= r;
78 }
79
80 for (const QRect &dirtyRect : clippedDpiScaledRegion)
81 dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect);
82
83 m_dirty = QRegion();
84}
85
86void QWasmBackingStore::beginPaint(const QRegion &region)
87{
88 m_dirty |= region;
89 // Keep backing store device pixel ratio in sync with window
90 if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
91 resize(backingStore()->size(), backingStore()->staticContents());
92
93 QPainter painter(&m_image);
94
95 if (m_image.hasAlphaChannel()) {
96 painter.setCompositionMode(QPainter::CompositionMode_Source);
97 const QColor blank = Qt::transparent;
98 for (const QRect &rect : region)
99 painter.fillRect(rect, blank);
100 }
101}
102
103void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
104{
105 Q_UNUSED(staticContents);
106
107 QImage::Format format = QImage::Format_RGBA8888;
108 const auto platformScreenDPR = window()->handle()->devicePixelRatio();
109 m_image = QImage(size * platformScreenDPR, format);
110 m_image.setDevicePixelRatio(platformScreenDPR);
111 m_webImageDataArray = emscripten::val::undefined();
112}
113
115{
116 // used by QPlatformBackingStore::rhiFlush
117 return m_image;
118}
119
121{
122 return m_image;
123}
124
126{
127 updateTexture(window);
128 return m_webImageDataArray;
129}
130
131QT_END_NAMESPACE
\inmodule QtCore\reentrant
Definition qpoint.h:29
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