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_image.size().isEmpty())
57 return;
58
59 if (m_webImageDataArray.isUndefined()) {
60 m_webImageDataArray = window->context2d().call<emscripten::val>(
61 "createImageData", emscripten::val(m_image.width()),
62 emscripten::val(m_image.height()));
63 }
64
65 QRegion clippedDpiScaledRegion;
66 QRect imageRect = m_image.rect();
67
68 for (const QRect &rect : m_dirty) {
69 // Convert device-independent dirty region to device region
70 qreal dpr = m_image.devicePixelRatio();
71 QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);
72
73 // intersect with image rect to be sure
74 QRect r = imageRect & deviceRect;
75 // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
76 if (r.width() >= imageRect.width() / 2) {
77 r.setX(0);
78 r.setWidth(imageRect.width());
79 }
80
81 clippedDpiScaledRegion |= r;
82 }
83
84 for (const QRect &dirtyRect : clippedDpiScaledRegion)
85 dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect);
86
87 m_dirty = QRegion();
88}
89
90void QWasmBackingStore::beginPaint(const QRegion &region)
91{
92 m_dirty |= region;
93 // Keep backing store device pixel ratio in sync with window
94 if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
95 resize(backingStore()->size(), backingStore()->staticContents());
96
97 QPainter painter(&m_image);
98
99 if (m_image.hasAlphaChannel()) {
100 painter.setCompositionMode(QPainter::CompositionMode_Source);
101 const QColor blank = Qt::transparent;
102 for (const QRect &rect : region)
103 painter.fillRect(rect, blank);
104 }
105}
106
107void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
108{
109 Q_UNUSED(staticContents);
110
111 QImage::Format format = QImage::Format_RGBA8888;
112 const auto platformScreenDPR = window()->handle()->devicePixelRatio();
113 m_image = QImage(size * platformScreenDPR, format);
114 m_image.setDevicePixelRatio(platformScreenDPR);
115 m_webImageDataArray = emscripten::val::undefined();
116}
117
119{
120 // used by QPlatformBackingStore::rhiFlush
121 return m_image;
122}
123
125{
126 return m_image;
127}
128
130{
131 updateTexture(window);
132 return m_webImageDataArray;
133}
134
135QT_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()
void setBackingStore(QWasmBackingStore *store)
Definition qwasmwindow.h:96
friend class QWasmScreen
Combined button and popup list for selecting options.