diff --git a/src/common/Channel.cpp b/src/common/Channel.cpp index 6511d6a2e..3adcccd2f 100644 --- a/src/common/Channel.cpp +++ b/src/common/Channel.cpp @@ -98,8 +98,6 @@ void Channel::addOrReplaceTimeout(MessagePtr message) for (int i = snapshotLength - 1; i >= end; --i) { auto &s = snapshot[i]; - qDebug() << s->parseTime << minimumTime; - if (s->parseTime < minimumTime) { break; } diff --git a/src/debug/Benchmark.hpp b/src/debug/Benchmark.hpp index 5eeb6cb36..5faa1a537 100644 --- a/src/debug/Benchmark.hpp +++ b/src/debug/Benchmark.hpp @@ -1,6 +1,7 @@ #pragma once -#include +#include "debug/Log.hpp" + #include #include #include @@ -29,8 +30,7 @@ public: ~BenchmarkGuard() { - qDebug() << this->name << float(timer.nsecsElapsed()) / 1000000.0f - << "ms"; + Log("{} {} ms", this->name, float(timer.nsecsElapsed()) / 1000000.0f); } qreal getElapsedMs() diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index e6991fe1e..b91ea1a44 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -3,6 +3,7 @@ #include "Application.hpp" #include "common/NetworkRequest.hpp" #include "debug/AssertInGuiThread.hpp" +#include "debug/Benchmark.hpp" #include "debug/Log.hpp" #include "singletons/Emotes.hpp" #include "singletons/WindowManager.hpp" @@ -15,7 +16,6 @@ #include #include #include - #include #include @@ -81,11 +81,11 @@ const QPixmap *Frames::first() const } // functions -std::vector readFrames(QImageReader &reader, const Url &url) +std::vector readFrames(QImageReader &reader, const Url &url) { - std::vector frames; + std::vector frames; - if (reader.imageCount() <= 0) { + if (reader.imageCount() == 0) { Log("Error while reading image {}: '{}'", url.string, reader.errorString()); return frames; @@ -94,14 +94,14 @@ std::vector readFrames(QImageReader &reader, const Url &url) QImage image; for (int index = 0; index < reader.imageCount(); ++index) { if (reader.read(&image)) { - auto pixmap = std::make_unique(QPixmap::fromImage(image)); + QPixmap::fromImage(image); int duration = std::max(20, reader.nextImageDelay()); - frames.push_back(Frame{std::move(pixmap), duration}); + frames.push_back(ParseFrame{image, duration}); } } - if (frames.size() != 0) { + if (frames.size() == 0) { Log("Error while reading image {}: '{}'", url.string, reader.errorString()); } @@ -123,11 +123,65 @@ void queueLoadedEvent() }); } } + +// parsed +template +void asd(std::queue>> &queued, + std::mutex &mutex, std::atomic_bool &loadedEventQueued) +{ + std::lock_guard lock(mutex); + int i = 0; + + while (!queued.empty()) { + queued.front().first(std::move(queued.front().second)); + queued.pop(); + + if (++i > 50) { + QTimer::singleShot(3, + [&] { asd(queued, mutex, loadedEventQueued); }); + return; + } + } + + getApp()->windows->forceLayoutChannelViews(); + loadedEventQueued = false; +} + +template +auto makeConvertCallback(std::vector parsed, Assign assign) +{ + return [parsed = std::move(parsed), assign] { + // BenchmarkGuard guard("convert image"); + + // convert to pixmap + auto frames = std::vector(); + std::transform(parsed.begin(), parsed.end(), std::back_inserter(frames), + [](auto &frame) { + return Frame{std::make_unique( + QPixmap::fromImage(frame.image)), + frame.duration}; + }); + + // put into stack + static std::queue>> queued; + static std::mutex mutex; + + std::lock_guard lock(mutex); + queued.emplace(assign, std::move(frames)); + + static std::atomic_bool loadedEventQueued{false}; + + if (!loadedEventQueued) { + loadedEventQueued = true; + + QTimer::singleShot(100, + [=] { asd(queued, mutex, loadedEventQueued); }); + } + }; +} } // namespace // IMAGE2 -std::atomic Image::loadedEventQueued{false}; - ImagePtr Image::fromUrl(const Url &url, qreal scale) { static std::unordered_map> cache; @@ -249,25 +303,36 @@ void Image::load() NetworkRequest req(this->url().string); req.setCaller(&this->object_); req.setUseQuickLoadCache(true); - req.onSuccess([this, weak = weakOf(this)](auto result) -> Outcome { + req.onSuccess([that = this, weak = weakOf(this)](auto result) -> Outcome { assertInGuiThread(); auto shared = weak.lock(); if (!shared) return Failure; - // const cast since we are only reading from it - QBuffer buffer(const_cast(&result.getData())); - buffer.open(QIODevice::ReadOnly); - QImageReader reader(&buffer); + static auto parseThread = [] { + auto thread = std::make_unique(); + thread->start(); + return thread; + }(); - this->frames_ = readFrames(reader, this->url()); + postToThread( + [data = result.getData(), weak, that] { + // BenchmarkGuard guard("parse image"); + + // const cast since we are only reading from it + QBuffer buffer(const_cast(&data)); + buffer.open(QIODevice::ReadOnly); + QImageReader reader(&buffer); + auto parsed = readFrames(reader, that->url()); + + postToThread( + makeConvertCallback(std::move(parsed), [weak](auto frames) { + if (auto shared = weak.lock()) + shared->frames_ = std::move(frames); + })); + }, + parseThread.get()); - if (!loadedEventQueued) { - QTimer::singleShot(150, [] { - getApp()->windows->forceLayoutChannelViews(); - loadedEventQueued = false; - }); - } return Success; }); diff --git a/src/messages/Image.hpp b/src/messages/Image.hpp index 50af47f4e..b5c4d1f58 100644 --- a/src/messages/Image.hpp +++ b/src/messages/Image.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -19,6 +20,10 @@ struct Frame { Pixmap pixmap; int duration; }; +struct ParseFrame { + QImage image; + int duration; +}; class Frames { public: @@ -77,7 +82,5 @@ private: bool shouldLoad_{false}; Frames frames_{}; QObject object_{}; - - static std::atomic loadedEventQueued; }; } // namespace chatterino diff --git a/src/widgets/helper/ChannelView.cpp b/src/widgets/helper/ChannelView.cpp index 1615d15ba..e1dca8cfb 100644 --- a/src/widgets/helper/ChannelView.cpp +++ b/src/widgets/helper/ChannelView.cpp @@ -205,7 +205,7 @@ void ChannelView::layoutMessages() void ChannelView::actuallyLayoutMessages(bool causedByScrollbar) { - // BenchmarkGuard benchmark("layout messages"); + // BenchmarkGuard benchmark("layout"); auto app = getApp(); @@ -633,7 +633,7 @@ void ChannelView::updatePauseStatus() void ChannelView::paintEvent(QPaintEvent * /*event*/) { - // BenchmarkGuard benchmark("paint event"); + // BenchmarkGuard benchmark("paint"); QPainter painter(this);