Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#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
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
40{
42 Q_UNUSED(region);
44
45 m_dirty |= region;
46 m_compositor->handleBackingStoreFlush(window);
47}
48
50{
51 if (m_dirty.isNull())
52 return;
53
54 if (m_webImageDataArray.isUndefined()) {
55 m_webImageDataArray = window->context2d().call<emscripten::val>(
56 "createImageData", emscripten::val(m_image.width()),
57 emscripten::val(m_image.height()));
58 }
59
60 QRegion clippedDpiScaledRegion;
61 QRect imageRect = m_image.rect();
62
63 for (const QRect &rect : m_dirty) {
64 // Convert device-independent dirty region to device region
65 qreal dpr = m_image.devicePixelRatio();
66 QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);
67
68 // intersect with image rect to be sure
69 QRect r = imageRect & deviceRect;
70 // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
71 if (r.width() >= imageRect.width() / 2) {
72 r.setX(0);
73 r.setWidth(imageRect.width());
74 }
75
76 clippedDpiScaledRegion |= r;
77 }
78
79 for (const QRect &dirtyRect : clippedDpiScaledRegion)
80 dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect);
81
82 m_dirty = QRegion();
83}
84
86{
87 m_dirty |= region;
88 // Keep backing store device pixel ratio in sync with window
89 if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
90 resize(backingStore()->size(), backingStore()->staticContents());
91
92 QPainter painter(&m_image);
93
94 if (m_image.hasAlphaChannel()) {
96 const QColor blank = Qt::transparent;
97 for (const QRect &rect : region)
98 painter.fillRect(rect, blank);
99 }
100}
101
102void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
103{
104 Q_UNUSED(staticContents);
105
107 const auto platformScreenDPR = window()->handle()->devicePixelRatio();
108 m_image = QImage(size * platformScreenDPR, format);
109 m_image.setDevicePixelRatio(platformScreenDPR);
110 m_webImageDataArray = emscripten::val::undefined();
111}
112
114{
115 // used by QPlatformBackingStore::rhiFlush
116 return m_image;
117}
118
120{
121 return m_image;
122}
123
125{
127 return m_webImageDataArray;
128}
129
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:4589
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
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:1510
qreal devicePixelRatio() const
Returns the device pixel ratio for the image.
Definition qimage.cpp:1482
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:25
\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:215
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 setBackingStore(QWasmBackingStore *store)
Definition qwasmwindow.h:88
\inmodule QtGui
Definition qwindow.h:63
rect
[4]
Combined button and popup list for selecting options.
@ transparent
Definition qnamespace.h:47
void drawImageToWebImageDataArray(const QImage &sourceImage, emscripten::val destinationImageData, const QRect &sourceRect)
Definition qwasmdom.cpp:261
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:187
QPainter painter(this)
[7]
aWidget window() -> setWindowTitle("New Window Title")
[2]