Fix image loading slowness

Images are now loaded in another thread
This commit is contained in:
hemirt 2017-10-08 15:18:47 +02:00 committed by pajlada
parent 369b7c052b
commit e7e4ab768b
5 changed files with 176 additions and 61 deletions

View file

@ -99,7 +99,8 @@ SOURCES += \
src/twitch/twitchchannel.cpp \ src/twitch/twitchchannel.cpp \
src/widgets/rippleeffectlabel.cpp \ src/widgets/rippleeffectlabel.cpp \
src/widgets/rippleeffectbutton.cpp \ src/widgets/rippleeffectbutton.cpp \
src/messages/messagecolor.cpp src/messages/messagecolor.cpp \
src/messages/imageloadermanager.cpp
HEADERS += \ HEADERS += \
src/asyncexec.hpp \ src/asyncexec.hpp \
@ -165,7 +166,8 @@ HEADERS += \
src/widgets/emotepopup.hpp \ src/widgets/emotepopup.hpp \
src/messages/messagecolor.hpp \ src/messages/messagecolor.hpp \
src/util/nativeeventhelper.hpp \ src/util/nativeeventhelper.hpp \
src/debug/log.hpp src/debug/log.hpp \
src/messages/imageloadermanager.hpp
PRECOMPILED_HEADER = PRECOMPILED_HEADER =

View file

@ -0,0 +1,98 @@
#include "messages/imageloadermanager.hpp"
#include "emotemanager.hpp"
#include "messages/lazyloadedimage.hpp"
#include "windowmanager.hpp"
#include <QBuffer>
#include <QDebug>
#include <QEventLoop>
#include <QImageReader>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <sstream>
namespace chatterino {
namespace messages {
ImageLoaderManager::ImageLoaderManager()
: NaM(new QNetworkAccessManager)
{
qDebug() << "imageloaderthread" << QThread::currentThread();
qDebug() << "workerthread: " << &this->workerThread;
this->NaM->moveToThread(&this->workerThread);
this->workerThread.start();
}
ImageLoaderManager::~ImageLoaderManager()
{
this->workerThread.quit();
this->workerThread.wait();
}
void ImageLoaderWorker::handleRequest(LazyLoadedImage *lli, QNetworkAccessManager *nam)
{
QNetworkRequest request;
request.setUrl(QUrl(lli->getUrl()));
qDebug() << "handleRequest: " << lli->getUrl();
QNetworkReply *reply = nam->get(request);
QObject::connect(reply, &QNetworkReply::finished,
[lli, reply, this]() { this->handleLoad(lli, reply); });
}
void ImageLoaderManager::queue(chatterino::messages::LazyLoadedImage *lli)
{
ImageLoaderRequester requester;
ImageLoaderWorker *workerer = new ImageLoaderWorker;
workerer->moveToThread(&this->workerThread);
QObject::connect(&requester, &ImageLoaderRequester::request, workerer,
&ImageLoaderWorker::handleRequest);
emit requester.request(lli, this->NaM);
qDebug() << lli->getUrl();
}
void ImageLoaderWorker::handleLoad(chatterino::messages::LazyLoadedImage *lli, QNetworkReply *reply)
{
qDebug() << "Received emote " << lli->url;
qDebug() << QThread::currentThread();
QByteArray array = reply->readAll();
QBuffer buffer(&array);
buffer.open(QIODevice::ReadOnly);
QImage image;
QImageReader reader(&buffer);
bool first = true;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) {
first = false;
lli->currentPixmap = pixmap;
}
LazyLoadedImage::FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
lli->allFrames.push_back(data);
}
}
if (lli->allFrames.size() > 1) {
lli->animated = true;
}
lli->emoteManager.incGeneration();
lli->windowManager.layoutVisibleChatWidgets();
delete reply;
delete this;
}
} // namespace messages
} // namespace chatterino

View file

@ -0,0 +1,48 @@
#pragma once
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QThread>
#include <QTimerEvent>
namespace chatterino {
namespace messages {
class LazyLoadedImage;
class ImageLoaderWorker : public QObject
{
Q_OBJECT
public:
public slots:
void handleRequest(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam);
void handleLoad(LazyLoadedImage *lli, QNetworkReply *reply);
};
class ImageLoaderRequester : public QObject
{
Q_OBJECT
signals:
void request(chatterino::messages::LazyLoadedImage *lli, QNetworkAccessManager *nam);
};
class ImageLoaderManager : public QObject
{
Q_OBJECT
QThread workerThread;
QNetworkAccessManager *NaM;
public:
ImageLoaderManager();
~ImageLoaderManager();
void queue(chatterino::messages::LazyLoadedImage *lli);
};
} // namespace messages
} // namespace chatterino

View file

@ -2,16 +2,17 @@
#include "asyncexec.hpp" #include "asyncexec.hpp"
#include "emotemanager.hpp" #include "emotemanager.hpp"
#include "ircmanager.hpp" #include "ircmanager.hpp"
#include "messages/imageloadermanager.hpp"
#include "util/urlfetch.hpp" #include "util/urlfetch.hpp"
#include "windowmanager.hpp" #include "windowmanager.hpp"
#include <thread>
#include <QBuffer> #include <QBuffer>
#include <QImageReader> #include <QImageReader>
#include <QNetworkAccessManager> #include <QNetworkAccessManager>
#include <QNetworkReply> #include <QNetworkReply>
#include <QNetworkRequest> #include <QNetworkRequest>
#include <QTimer> #include <QTimer>
#include <thread>
#include <functional> #include <functional>
@ -51,52 +52,13 @@ LazyLoadedImage::LazyLoadedImage(EmoteManager &_emoteManager, WindowManager &_wi
void LazyLoadedImage::loadImage() void LazyLoadedImage::loadImage()
{ {
std::thread([=] () { static ImageLoaderManager imageLoader;
QNetworkRequest request; imageLoader.queue(this);
request.setUrl(QUrl(this->url));
QNetworkAccessManager NaM;
QEventLoop eventLoop;
QNetworkReply *reply = NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
eventLoop.exec(); // Wait until response is read.
qDebug() << "Received emote " << this->url; this->emoteManager.getGifUpdateSignal().connect([=]() {
QByteArray array = reply->readAll(); this->gifUpdateTimout();
QBuffer buffer(&array); }); // For some reason when Boost signal is in thread scope and thread deletes the signal
buffer.open(QIODevice::ReadOnly); // doesn't work, so this is the fix.
QImage image;
QImageReader reader(&buffer);
bool first = true;
for (int index = 0; index < reader.imageCount(); ++index) {
if (reader.read(&image)) {
auto pixmap = new QPixmap(QPixmap::fromImage(image));
if (first) {
first = false;
this->currentPixmap = pixmap;
}
FrameData data;
data.duration = std::max(20, reader.nextImageDelay());
data.image = pixmap;
this->allFrames.push_back(data);
}
}
if (this->allFrames.size() > 1) {
this->animated = true;
}
this->emoteManager.incGeneration();
this->windowManager.layoutVisibleChatWidgets();
delete reply;
}).detach();
this->emoteManager.getGifUpdateSignal().connect([=] () { this->gifUpdateTimout(); }); // For some reason when Boost signal is in thread scope and thread deletes the signal doesn't work, so this is the fix.
} }
void LazyLoadedImage::gifUpdateTimout() void LazyLoadedImage::gifUpdateTimout()

View file

@ -10,7 +10,9 @@ class WindowManager;
namespace messages { namespace messages {
class LazyLoadedImage : QObject class ImageLoaderManager;
class LazyLoadedImage : public QObject
{ {
public: public:
LazyLoadedImage() = delete; LazyLoadedImage() = delete;
@ -64,6 +66,9 @@ private:
void loadImage(); void loadImage();
void gifUpdateTimout(); void gifUpdateTimout();
//static ImageLoaderManager imageLoader;
friend class ImageLoaderWorker;
}; };
} // namespace messages } // namespace messages