Get rid of threadpool for images

This commit is contained in:
Nicolas Werner 2021-12-07 23:58:17 +01:00
parent 868342ffc9
commit 30791f7890
No known key found for this signature in database
GPG key ID: C8D75E610773F2D9
3 changed files with 48 additions and 21 deletions

View file

@ -16,7 +16,6 @@ class BlurhashResponse
{ {
public: public:
BlurhashResponse(const QString &id, const QSize &requestedSize) BlurhashResponse(const QString &id, const QSize &requestedSize)
: m_id(id) : m_id(id)
, m_requestedSize(requestedSize) , m_requestedSize(requestedSize)
{ {

View file

@ -43,9 +43,7 @@ MxcImageProvider::requestImageResponse(const QString &id, const QSize &requested
} }
} }
MxcImageResponse *response = new MxcImageResponse(id_, crop, radius, requestedSize); return new MxcImageResponse(id_, crop, radius, requestedSize);
pool.start(response);
return response;
} }
void void
@ -54,18 +52,18 @@ MxcImageProvider::addEncryptionInfo(mtx::crypto::EncryptedFile info)
infos.insert(QString::fromStdString(info.url), info); infos.insert(QString::fromStdString(info.url), info);
} }
void void
MxcImageResponse::run() MxcImageRunnable::run()
{ {
MxcImageProvider::download( MxcImageProvider::download(
m_id, m_id,
m_requestedSize, m_requestedSize,
[this](QString, QSize, QImage image, QString) { [this](QString, QSize, QImage image, QString) {
if (image.isNull()) { if (image.isNull()) {
m_error = "Failed to download image."; emit error("Failed to download image.");
} else { } else {
m_image = image; emit done(image);
} }
emit finished(); this->deleteLater();
}, },
m_crop, m_crop,
m_radius); m_radius);

View file

@ -8,24 +8,59 @@
#include <QQuickImageResponse> #include <QQuickImageResponse>
#include <QImage> #include <QImage>
#include <QThreadPool> //#include <QThreadPool>
#include <functional> #include <functional>
#include <mtx/common.hpp> #include <mtx/common.hpp>
class MxcImageResponse class MxcImageRunnable
: public QQuickImageResponse : public QObject
, public QRunnable
{ {
Q_OBJECT
signals:
void done(QImage image);
void error(QString error);
public: public:
MxcImageResponse(const QString &id, bool crop, double radius, const QSize &requestedSize) MxcImageRunnable(const QString &id, bool crop, double radius, const QSize &requestedSize)
: m_id(id) : m_id(id)
, m_requestedSize(requestedSize) , m_requestedSize(requestedSize)
, m_crop(crop) , m_crop(crop)
, m_radius(radius) , m_radius(radius)
{ {
setAutoDelete(false); }
void run();
QString m_id;
QSize m_requestedSize;
bool m_crop;
double m_radius;
};
class MxcImageResponse
: public QQuickImageResponse
{
public:
MxcImageResponse(const QString &id, bool crop, double radius, const QSize &requestedSize)
{
auto runnable = new MxcImageRunnable(id, crop, radius, requestedSize);
connect(runnable, &MxcImageRunnable::done, this, &MxcImageResponse::handleDone);
connect(runnable, &MxcImageRunnable::error, this, &MxcImageResponse::handleError);
runnable->run();
}
void handleDone(QImage image)
{
m_image = image;
emit finished();
}
void handleError(QString error)
{
m_error = error;
emit finished();
} }
QQuickTextureFactory *textureFactory() const override QQuickTextureFactory *textureFactory() const override
@ -34,13 +69,8 @@ public:
} }
QString errorString() const override { return m_error; } QString errorString() const override { return m_error; }
void run() override; QString m_error;
QString m_id, m_error;
QSize m_requestedSize;
QImage m_image; QImage m_image;
bool m_crop;
double m_radius;
}; };
class MxcImageProvider class MxcImageProvider
@ -60,5 +90,5 @@ public slots:
double radius = 0); double radius = 0);
private: private:
QThreadPool pool; // QThreadPool pool;
}; };