Nicolas Werner 2021-12-21 12:17:12 +01:00
parent b9a4f369e1
commit d424145ee4
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
2 changed files with 45 additions and 20 deletions

View file

@ -11,17 +11,16 @@
#include "blurhash.hpp" #include "blurhash.hpp"
void void
BlurhashResponse::run() BlurhashRunnable::run()
{ {
if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) { if (m_requestedSize.width() < 0 || m_requestedSize.height() < 0) {
m_error = QStringLiteral("Blurhash needs size request"); emit error("Blurhash needs size request");
emit finished();
return; return;
} }
if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) { if (m_requestedSize.width() == 0 || m_requestedSize.height() == 0) {
m_image = QImage(m_requestedSize, QImage::Format_RGB32); auto image = QImage(m_requestedSize, QImage::Format_RGB32);
m_image.fill(QColor(0, 0, 0)); image.fill(QColor(0, 0, 0));
emit finished(); emit done(image);
return; return;
} }
@ -29,8 +28,7 @@ BlurhashResponse::run()
m_requestedSize.width(), m_requestedSize.width(),
m_requestedSize.height()); m_requestedSize.height());
if (decoded.image.empty()) { if (decoded.image.empty()) {
m_error = QStringLiteral("Failed decode!"); emit error(QStringLiteral("Failed decode!"));
emit finished();
return; return;
} }
@ -40,6 +38,5 @@ BlurhashResponse::run()
(int)decoded.width * 3, (int)decoded.width * 3,
QImage::Format_RGB888); QImage::Format_RGB888);
m_image = image.copy(); emit done(std::move(image));
emit finished();
} }

View file

@ -10,16 +10,38 @@
#include <QImage> #include <QImage>
#include <QThreadPool> #include <QThreadPool>
class BlurhashResponse class BlurhashRunnable
: public QQuickImageResponse : public QObject
, public QRunnable , public QRunnable
{ {
Q_OBJECT
public: public:
BlurhashResponse(const QString &id, const QSize &requestedSize) BlurhashRunnable(const QString &id, const QSize &requestedSize)
: m_id(id) : m_id(id)
, m_requestedSize(requestedSize) , m_requestedSize(requestedSize)
{ {
setAutoDelete(false); }
void run() override;
signals:
void done(QImage);
void error(QString);
private:
QString m_id;
QSize m_requestedSize;
};
class BlurhashResponse
: public QQuickImageResponse
{
public:
BlurhashResponse(const QString &id, const QSize &requestedSize, QThreadPool *pool)
{
auto runnable = new BlurhashRunnable(id, requestedSize);
connect(runnable, &BlurhashRunnable::done, this, &BlurhashResponse::handleDone);
connect(runnable, &BlurhashRunnable::error, this, &BlurhashResponse::handleError);
pool->start(runnable);
} }
QQuickTextureFactory *textureFactory() const override QQuickTextureFactory *textureFactory() const override
@ -28,10 +50,18 @@ public:
} }
QString errorString() const override { return m_error; } QString errorString() const override { return m_error; }
void run() override; void handleDone(QImage image)
{
m_image = image;
emit finished();
}
void handleError(QString error)
{
m_error = error;
emit finished();
}
QString m_id, m_error; QString m_error;
QSize m_requestedSize;
QImage m_image; QImage m_image;
}; };
@ -44,9 +74,7 @@ public slots:
QQuickImageResponse * QQuickImageResponse *
requestImageResponse(const QString &id, const QSize &requestedSize) override requestImageResponse(const QString &id, const QSize &requestedSize) override
{ {
BlurhashResponse *response = new BlurhashResponse(id, requestedSize); return new BlurhashResponse(id, requestedSize, &pool);
pool.start(response);
return response;
} }
private: private: