Qt 6.x
The Qt SDK
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
4#include "qwasmbackingstore.h"
5#include "qwasmwindow.h"
6#include "qwasmcompositor.h"
7
8#include <QtGui/qpainter.h>
9#include <QtGui/qbackingstore.h>
10
11#include <emscripten.h>
12#include <emscripten/wire.h>
13
15
18{
19 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
20 if (wasmWindow)
21 wasmWindow->setBackingStore(this);
22}
23
25{
26 auto window = this->window();
28 QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
29 if (wasmWindow)
30 wasmWindow->setBackingStore(nullptr);
31}
32
34{
35 return &m_image;
36}
37
39{
41 Q_UNUSED(region);
43
44 m_dirty |= region;
45 m_compositor->handleBackingStoreFlush(window);
46}
47
49{
50 if (m_dirty.isNull())
51 return;
52
53 if (m_webImageDataArray.isUndefined()) {
54 m_webImageDataArray = window->context2d().call<emscripten::val>(
55 "createImageData", emscripten::val(m_image.width()),
56 emscripten::val(m_image.height()));
57 }
58
59 QRegion clippedDpiScaledRegion;
60 QRect imageRect = m_image.rect();
61
62 for (const QRect &rect : m_dirty) {
63 // Convert device-independent dirty region to device region
64 qreal dpr = m_image.devicePixelRatio();
65 QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);
66
67 // intersect with image rect to be sure
68 QRect r = imageRect & deviceRect;
69 // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
70 if (r.width() >= imageRect.width() / 2) {
71 r.setX(0);
72 r.setWidth(imageRect.width());
73 }
74
75 clippedDpiScaledRegion |= r;
76 }
77
78 for (const QRect &dirtyRect : clippedDpiScaledRegion) {
79 constexpr int BytesPerColor = 4;
80 if (dirtyRect.width() == imageRect.width()) {
81 // Copy a contiguous chunk of memory
82 // ...............
83 // OOOOOOOOOOOOOOO
84 // OOOOOOOOOOOOOOO -> image data
85 // OOOOOOOOOOOOOOO
86 // ...............
87 auto imageMemory = emscripten::typed_memory_view(dirtyRect.width() * dirtyRect.height()
88 * BytesPerColor,
89 m_image.constScanLine(dirtyRect.y()));
90 m_webImageDataArray["data"].call<void>("set", imageMemory,
91 dirtyRect.y() * m_image.width() * BytesPerColor);
92 } else {
93 // Go through the scanlines manually to set the individual lines in bulk. This is
94 // marginally less performant than the above.
95 // ...............
96 // ...OOOOOOOOO... r = 0 -> image data
97 // ...OOOOOOOOO... r = 1 -> image data
98 // ...OOOOOOOOO... r = 2 -> image data
99 // ...............
100 for (int r = 0; r < dirtyRect.height(); ++r) {
101 auto scanlineMemory = emscripten::typed_memory_view(
102 dirtyRect.width() * BytesPerColor,
103 m_image.constScanLine(r + dirtyRect.y()) + BytesPerColor * dirtyRect.x());
104 m_webImageDataArray["data"].call<void>("set", scanlineMemory,
105 (dirtyRect.y() + r) * m_image.width()
106 * BytesPerColor
107 + dirtyRect.x() * BytesPerColor);
108 }
109 }
110 }
111
112 m_dirty = QRegion();
113}
114
116{
117 m_dirty |= region;
118 // Keep backing store device pixel ratio in sync with window
119 if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
120 resize(backingStore()->size(), backingStore()->staticContents());
121
122 QPainter painter(&m_image);
123
124 if (m_image.hasAlphaChannel()) {
126 const QColor blank = Qt::transparent;
127 for (const QRect &rect : region)
128 painter.fillRect(rect, blank);
129 }
130}
131
132void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
133{
134 Q_UNUSED(staticContents);
135
137 const auto platformScreenDPR = window()->handle()->devicePixelRatio();
138 m_image = QImage(size * platformScreenDPR, format);
139 m_image.setDevicePixelRatio(platformScreenDPR);
140 m_webImageDataArray = emscripten::val::undefined();
141}
142
144{
145 // used by QPlatformBackingStore::rhiFlush
146 return m_image;
147}
148
150{
151 return m_image;
152}
153
155{
157 return m_webImageDataArray;
158}
159
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
\inmodule QtGui
Definition qimage.h:37
bool hasAlphaChannel() const
Returns true if the image has a format that respects the alpha channel, otherwise returns false.
Definition qimage.cpp:4571
int width() const
Returns the width of the image.
int height() const
Returns the height of the image.
Format
The following image formats are available in Qt.
Definition qimage.h:41
@ Format_RGBA8888
Definition qimage.h:59
const uchar * constScanLine(int) const
Returns a pointer to the pixel data at the scanline with index i.
Definition qimage.cpp:1657
QRect rect() const
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
void setDevicePixelRatio(qreal scaleFactor)
Sets the device pixel ratio for the image.
Definition qimage.cpp:1488
qreal devicePixelRatio() const
Returns the device pixel ratio for the image.
Definition qimage.cpp:1460
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
@ CompositionMode_Source
Definition qpainter.h:101
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
The QPlatformBackingStore class provides the drawing area for top-level windows.
QBackingStore * backingStore() const
Returns a pointer to the backing store associated with this surface.
QWindow * window() const
Returns a pointer to the top-level window associated with this surface.
virtual qreal devicePixelRatio() const
Reimplement this function in subclass to return the device pixel ratio for the window.
\inmodule QtCore\reentrant
Definition qpoint.h:23
\inmodule QtCore\reentrant
Definition qrect.h:30
constexpr void setX(int x) noexcept
Sets the left edge of the rectangle to the given x coordinate.
Definition qrect.h:214
constexpr int width() const noexcept
Returns the width of the rectangle.
Definition qrect.h:235
The QRegion class specifies a clip region for a painter.
Definition qregion.h:27
bool isNull() const
\inmodule QtCore
Definition qsize.h:25
void flush(QWindow *window, const QRegion &region, const QPoint &offset) override
Flushes the given region from the specified window.
QWasmBackingStore(QWasmCompositor *compositor, QWindow *window)
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.
void handleBackingStoreFlush(QWindow *window)
static QWasmIntegration * get()
void removeBackingStore(QWindow *window)
void setBackingStore(QWasmBackingStore *store)
Definition qwasmwindow.h:92
\inmodule QtGui
Definition qwindow.h:63
rect
[4]
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:46
static QOpenGLCompositor * compositor
GLuint64 GLenum void * handle
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLenum GLuint GLintptr offset
GLint GLsizei GLsizei GLenum format
static QT_BEGIN_NAMESPACE qreal dpr(const QWindow *w)
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:92
QPainter painter(this)
[7]
aWidget window() -> setWindowTitle("New Window Title")
[2]