summaryrefslogtreecommitdiffstats
path: root/src/plugins/platforms/wasm/qwasmbackingstore.cpp
blob: a3c1ae8a502649795248b699dcaffdc28703ae99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (C) 2018 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qwasmbackingstore.h"
#include "qwasmwindow.h"
#include "qwasmcompositor.h"
#include "qwasmdom.h"

#include <QtGui/qpainter.h>
#include <QtGui/qbackingstore.h>

#include <emscripten.h>
#include <emscripten/wire.h>

QT_BEGIN_NAMESPACE

QWasmBackingStore::QWasmBackingStore(QWasmCompositor *compositor, QWindow *window)
    : QPlatformBackingStore(window), m_compositor(compositor)
{
    QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
    if (wasmWindow)
        wasmWindow->setBackingStore(this);
}

QWasmBackingStore::~QWasmBackingStore()
{
    auto window = this->window();
    QWasmIntegration::get()->removeBackingStore(window);
    QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle());
    if (wasmWindow)
        wasmWindow->setBackingStore(nullptr);
}

QPaintDevice *QWasmBackingStore::paintDevice()
{
    return &m_image;
}

void QWasmBackingStore::flush(QWindow *window, const QRegion &region, const QPoint &offset)
{
    Q_UNUSED(window);
    Q_UNUSED(region);
    Q_UNUSED(offset);

    m_dirty |= region;
    m_compositor->handleBackingStoreFlush(window);
}

void QWasmBackingStore::updateTexture(QWasmWindow *window)
{
    if (m_dirty.isNull())
        return;

    if (m_webImageDataArray.isUndefined()) {
        m_webImageDataArray = window->context2d().call<emscripten::val>(
                "createImageData", emscripten::val(m_image.width()),
                emscripten::val(m_image.height()));
    }

    QRegion clippedDpiScaledRegion;
    QRect imageRect = m_image.rect();

    for (const QRect &rect : m_dirty) {
        // Convert device-independent dirty region to device region
        qreal dpr = m_image.devicePixelRatio();
        QRect deviceRect = QRect(rect.topLeft() * dpr, rect.size() * dpr);

        // intersect with image rect to be sure
        QRect r = imageRect & deviceRect;
        // if the rect is wide enough it is cheaper to just extend it instead of doing an image copy
        if (r.width() >= imageRect.width() / 2) {
            r.setX(0);
            r.setWidth(imageRect.width());
        }

        clippedDpiScaledRegion |= r;
    }

    for (const QRect &dirtyRect : clippedDpiScaledRegion)
        dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect);

    m_dirty = QRegion();
}

void QWasmBackingStore::beginPaint(const QRegion &region)
{
    m_dirty |= region;
    // Keep backing store device pixel ratio in sync with window
    if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio())
        resize(backingStore()->size(), backingStore()->staticContents());

    QPainter painter(&m_image);

    if (m_image.hasAlphaChannel()) {
        painter.setCompositionMode(QPainter::CompositionMode_Source);
        const QColor blank = Qt::transparent;
        for (const QRect &rect : region)
            painter.fillRect(rect, blank);
    }
}

void QWasmBackingStore::resize(const QSize &size, const QRegion &staticContents)
{
    Q_UNUSED(staticContents);

    QImage::Format format = QImage::Format_RGBA8888;
    const auto platformScreenDPR = window()->handle()->devicePixelRatio();
    m_image = QImage(size * platformScreenDPR, format);
    m_image.setDevicePixelRatio(platformScreenDPR);
    m_webImageDataArray = emscripten::val::undefined();
}

QImage QWasmBackingStore::toImage() const
{
    // used by QPlatformBackingStore::rhiFlush
    return m_image;
}

const QImage &QWasmBackingStore::getImageRef() const
{
    return m_image;
}

emscripten::val QWasmBackingStore::getUpdatedWebImage(QWasmWindow *window)
{
    updateTexture(window);
    return m_webImageDataArray;
}

QT_END_NAMESPACE