From b894ce4dcd58e337a14b954a0d34997264401df2 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 4 Mar 2020 01:56:58 +0100 Subject: [PATCH] Make blurhash provider async --- CMakeLists.txt | 1 + resources/qml/delegates/ImageMessage.qml | 1 - src/BlurhashProvider.cpp | 35 ++++++++++------ src/BlurhashProvider.h | 52 +++++++++++++++++++++--- 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5561fc8d..8d0e4477 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -479,6 +479,7 @@ qt5_wrap_cpp(MOC_HEADERS src/notifications/Manager.h src/AvatarProvider.h + src/BlurhashProvider.h src/Cache_p.h src/ChatPage.h src/CommunitiesList.h diff --git a/resources/qml/delegates/ImageMessage.qml b/resources/qml/delegates/ImageMessage.qml index 62cae42c..65e1c454 100644 --- a/resources/qml/delegates/ImageMessage.qml +++ b/resources/qml/delegates/ImageMessage.qml @@ -20,7 +20,6 @@ Item { asynchronous: true fillMode: Image.PreserveAspectFit - sourceSize.width: parent.width sourceSize.height: parent.height } diff --git a/src/BlurhashProvider.cpp b/src/BlurhashProvider.cpp index 87d1f51b..08dc2d40 100644 --- a/src/BlurhashProvider.cpp +++ b/src/BlurhashProvider.cpp @@ -6,24 +6,33 @@ #include "blurhash.hpp" -QImage -BlurhashProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) +void +BlurhashResponse::run() { - QSize sz = requestedSize; - if (sz.width() < 1 || sz.height() < 1) - return QImage(); + if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) { + m_error = QStringLiteral("Blurhash needs size request"); + emit finished(); + return; + } + if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) { + m_image = QImage(m_requestedSize, QImage::Format_RGB32); + m_image.fill(QColor(0, 0, 0)); + emit finished(); + return; + } - if (size) - *size = sz; - - auto decoded = blurhash::decode( - QUrl::fromPercentEncoding(id.toUtf8()).toStdString(), sz.width(), sz.height(), 4); + auto decoded = blurhash::decode(QUrl::fromPercentEncoding(m_id.toUtf8()).toStdString(), + m_requestedSize.width(), + m_requestedSize.height(), + 4); if (decoded.image.empty()) { - *size = QSize(); - return QImage(); + m_error = QStringLiteral("Failed decode!"); + emit finished(); + return; } QImage image(decoded.image.data(), decoded.width, decoded.height, QImage::Format_RGB32); - return image.copy(); + m_image = image.copy(); + emit finished(); } diff --git a/src/BlurhashProvider.h b/src/BlurhashProvider.h index b05fff59..48c945de 100644 --- a/src/BlurhashProvider.h +++ b/src/BlurhashProvider.h @@ -1,11 +1,51 @@ -#include +#pragma once -class BlurhashProvider : public QQuickImageProvider +#include +#include + +#include +#include + +class BlurhashResponse + : public QQuickImageResponse + , public QRunnable { public: - BlurhashProvider() - : QQuickImageProvider(QQuickImageProvider::Image) - {} + BlurhashResponse(const QString &id, const QSize &requestedSize) - QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; + : m_id(id) + , m_requestedSize(requestedSize) + { + setAutoDelete(false); + } + + QQuickTextureFactory *textureFactory() const override + { + return QQuickTextureFactory::textureFactoryForImage(m_image); + } + QString errorString() const override { return m_error; } + + void run() override; + + QString m_id, m_error; + QSize m_requestedSize; + QImage m_image; +}; + +class BlurhashProvider + : public QObject + , public QQuickAsyncImageProvider +{ + Q_OBJECT +public slots: + QQuickImageResponse *requestImageResponse(const QString &id, + const QSize &requestedSize) override + { + BlurhashResponse *response = new BlurhashResponse(id, requestedSize); + pool.start(response); + return response; + } + +private: + QThreadPool pool; };