mirror of
https://github.com/Nheko-Reborn/nheko.git
synced 2024-11-25 20:48:52 +03:00
Make blurhash provider async
This commit is contained in:
parent
5ac18f1f5f
commit
b894ce4dcd
4 changed files with 69 additions and 20 deletions
|
@ -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
|
||||
|
|
|
@ -20,7 +20,6 @@ Item {
|
|||
asynchronous: true
|
||||
fillMode: Image.PreserveAspectFit
|
||||
|
||||
|
||||
sourceSize.width: parent.width
|
||||
sourceSize.height: parent.height
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -1,11 +1,51 @@
|
|||
#include <QQuickImageProvider>
|
||||
#pragma once
|
||||
|
||||
class BlurhashProvider : public QQuickImageProvider
|
||||
#include <QQuickAsyncImageProvider>
|
||||
#include <QQuickImageResponse>
|
||||
|
||||
#include <QImage>
|
||||
#include <QThreadPool>
|
||||
|
||||
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;
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue