diff options
Diffstat (limited to 'src/plugins/platforms/wasm/qwasmbackingstore.cpp')
-rw-r--r-- | src/plugins/platforms/wasm/qwasmbackingstore.cpp | 131 |
1 files changed, 44 insertions, 87 deletions
diff --git a/src/plugins/platforms/wasm/qwasmbackingstore.cpp b/src/plugins/platforms/wasm/qwasmbackingstore.cpp index 7e8a382512..c14d1f59e4 100644 --- a/src/plugins/platforms/wasm/qwasmbackingstore.cpp +++ b/src/plugins/platforms/wasm/qwasmbackingstore.cpp @@ -1,50 +1,21 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the plugins of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 or (at your option) any later version -** approved by the KDE Free Qt Foundation. The licenses are as published by -** the Free Software Foundation and appearing in the file LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// 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/qopengltexture.h> -#include <QtGui/qmatrix4x4.h> #include <QtGui/qpainter.h> -#include <private/qguiapplication_p.h> -#include <qpa/qplatformscreen.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) - , m_texture(new QOpenGLTexture(QOpenGLTexture::Target2D)) + : QPlatformBackingStore(window), m_compositor(compositor) { QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()); if (wasmWindow) @@ -53,12 +24,11 @@ QWasmBackingStore::QWasmBackingStore(QWasmCompositor *compositor, QWindow *windo QWasmBackingStore::~QWasmBackingStore() { -} - -void QWasmBackingStore::destroy() -{ - if (m_texture->isCreated()) - m_texture->destroy(); + auto window = this->window(); + QWasmIntegration::get()->removeBackingStore(window); + QWasmWindow *wasmWindow = static_cast<QWasmWindow *>(window->handle()); + if (wasmWindow) + wasmWindow->setBackingStore(nullptr); } QPaintDevice *QWasmBackingStore::paintDevice() @@ -69,37 +39,29 @@ QPaintDevice *QWasmBackingStore::paintDevice() void QWasmBackingStore::flush(QWindow *window, const QRegion ®ion, const QPoint &offset) { Q_UNUSED(window); - Q_UNUSED(region); - Q_UNUSED(offset); - m_dirty |= region; - m_compositor->requestRedraw(); + + QRect updateRect = region.boundingRect(); + updateRect.translate(offset); + + m_compositor->handleBackingStoreFlush(this->window(), updateRect); } -void QWasmBackingStore::updateTexture() +void QWasmBackingStore::updateTexture(QWasmWindow *window) { if (m_dirty.isNull()) return; - if (m_recreateTexture && m_texture->isCreated()) { - m_recreateTexture = false; - m_texture->destroy(); - } - - if (!m_texture->isCreated()) { - m_texture->setMinificationFilter(QOpenGLTexture::Nearest); - m_texture->setMagnificationFilter(QOpenGLTexture::Nearest); - m_texture->setWrapMode(QOpenGLTexture::ClampToEdge); - m_texture->setData(m_image, QOpenGLTexture::DontGenerateMipMaps); - m_texture->create(); + if (m_webImageDataArray.isUndefined()) { + m_webImageDataArray = window->context2d().call<emscripten::val>( + "createImageData", emscripten::val(m_image.width()), + emscripten::val(m_image.height())); } - m_texture->bind(); - QRegion fixed; + 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); @@ -112,21 +74,11 @@ void QWasmBackingStore::updateTexture() r.setWidth(imageRect.width()); } - fixed |= r; + clippedDpiScaledRegion |= r; } - for (const QRect &rect : fixed) { - // if the sub-rect is full-width we can pass the image data directly to - // OpenGL instead of copying, since there is no gap between scanlines - if (rect.width() == imageRect.width()) { - glTexSubImage2D(GL_TEXTURE_2D, 0, 0, rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, - m_image.constScanLine(rect.y())); - } else { - glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x(), rect.y(), rect.width(), rect.height(), GL_RGBA, GL_UNSIGNED_BYTE, - m_image.copy(rect).constBits()); - } - } - /* End of code taken from QEGLPlatformBackingStore */ + for (const QRect &dirtyRect : clippedDpiScaledRegion) + dom::drawImageToWebImageDataArray(m_image, m_webImageDataArray, dirtyRect); m_dirty = QRegion(); } @@ -135,28 +87,33 @@ void QWasmBackingStore::beginPaint(const QRegion ®ion) { m_dirty |= region; // Keep backing store device pixel ratio in sync with window - if (m_image.devicePixelRatio() != window()->devicePixelRatio()) + if (m_image.devicePixelRatio() != window()->handle()->devicePixelRatio()) resize(backingStore()->size(), backingStore()->staticContents()); QPainter painter(&m_image); - painter.setCompositionMode(QPainter::CompositionMode_Source); - const QColor blank = Qt::transparent; - for (const QRect &rect : region) - painter.fillRect(rect, blank); + + 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) + Q_UNUSED(staticContents); - m_image = QImage(size * window()->devicePixelRatio(), QImage::Format_RGB32); - m_image.setDevicePixelRatio(window()->devicePixelRatio()); - m_recreateTexture = true; + 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::composeAndFlush + // used by QPlatformBackingStore::rhiFlush return m_image; } @@ -165,10 +122,10 @@ const QImage &QWasmBackingStore::getImageRef() const return m_image; } -const QOpenGLTexture *QWasmBackingStore::getUpdatedTexture() +emscripten::val QWasmBackingStore::getUpdatedWebImage(QWasmWindow *window) { - updateTexture(); - return m_texture.data(); + updateTexture(window); + return m_webImageDataArray; } QT_END_NAMESPACE |