From 131aa4d28f50ec531a7452c9bc5fab2824645112 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:21:36 +0300 Subject: [PATCH 1/2] Read cached thumbnails independently of generation --- src/thumbnails.cpp | 63 ++++++++++++++++++++++++++----- src/thumbnails.h | 5 +++ tests/folio_tests.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+), 9 deletions(-) diff --git a/src/thumbnails.cpp b/src/thumbnails.cpp index d191a272..33768ecd 100644 --- a/src/thumbnails.cpp +++ b/src/thumbnails.cpp @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -210,6 +209,18 @@ void Thumbnails::keep(const QImage &image, const QString &path, int pixels, cons } } +QImage Thumbnails::cached(const QString &path, int size) const { + const QFileInfo info(path); + if (info.absoluteFilePath().startsWith(m_cache + "/")) + return {}; + // Probe the local PNG before touching source metadata. A cold miss must + // not add a second round trip to the source file on a network mount. + const QImage image(cachePath(path, size)); + if (image.isNull() || !info.isFile() || !madeFor(image, info.lastModified())) + return {}; + return image; +} + QImage Thumbnails::load(const QString &path, const QString &mime, int size) const { const QFileInfo info(path); if (!info.isFile()) @@ -435,13 +446,30 @@ void Thumbnails::save(QImage image, const QString &target, const QString &path, namespace { -class ThumbnailResponse : public QQuickImageResponse, public QRunnable { +class ThumbnailResponse : public QQuickImageResponse { public: - ThumbnailResponse(const Thumbnails *thumbnails, const QString &id) : m_thumbnails(thumbnails), m_id(id) { - setAutoDelete(false); + ThumbnailResponse(const Thumbnails *thumbnails, const QString &id) : m_thumbnails(thumbnails), m_id(id) {} + + void readCache(QThreadPool *generators) { + if (m_cancelled) { + complete(); + return; + } + const QStringList parts = m_id.split('/'); + if (parts.size() >= 4) { + const QString path = QUrl::fromPercentEncoding(parts.mid(3).join('/').toUtf8()); + m_image = m_thumbnails->cached(path, qBound(64, parts.at(0).toInt(), 1024)); + } + if (m_cancelled || !m_image.isNull()) { + complete(); + return; + } + // This is the last access to the response from the cache task: the + // generation task may finish and Qt may delete it immediately. + generators->start([this] { run(); }); } - void run() override { + void run() { // A decoder that throws (LibRaw on a camera RAW it can't read, #164) // must not take Folio down: an exception out of a pool thread ends // the process. The file gets no thumbnail instead; once finished() @@ -496,10 +524,8 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable { const QString path = QUrl::fromPercentEncoding(parts.mid(3).join('/').toUtf8()); m_image = m_thumbnails->load(path, mime, qBound(64, size, 1024)); } - if (m_image.isNull()) - m_error = QStringLiteral("No thumbnail"); told = true; - emit finished(); + complete(); } void cancel() override { m_cancelled = true; } @@ -510,6 +536,14 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable { QString errorString() const override { return m_error; } private: + void complete() { + if (m_cancelled) + m_image = {}; + if (m_image.isNull()) + m_error = QStringLiteral("No thumbnail"); + emit finished(); + } + const Thumbnails *m_thumbnails; QString m_id; QImage m_image; @@ -523,11 +557,22 @@ ThumbnailProvider::ThumbnailProvider(const Thumbnails *thumbnails) : m_thumbnail // Thumbnailers are separate processes; a few at once keeps a big folder // filling in quickly without starving the machine. m_pool.setMaxThreadCount(qBound(2, QThread::idealThreadCount() / 2, 6)); + m_cachePool.setMaxThreadCount(2); +} + +ThumbnailProvider::~ThumbnailProvider() { + // Cache misses can still enqueue generation. Drain in that order, and + // keep every response alive until its one finished signal. + m_cachePool.waitForDone(); + m_pool.waitForDone(); } // The size comes in the id; no view sets a sourceSize to request one. QQuickImageResponse *ThumbnailProvider::requestImageResponse(const QString &id, const QSize &) { auto *response = new ThumbnailResponse(m_thumbnails, id); - m_pool.start(response); + if (id.startsWith("page/") || id.startsWith("sharp/")) + m_pool.start([response] { response->run(); }); + else + m_cachePool.start([response, generators = &m_pool] { response->readCache(generators); }); return response; } diff --git a/src/thumbnails.h b/src/thumbnails.h index 531e9c8d..084c39ae 100644 --- a/src/thumbnails.h +++ b/src/thumbnails.h @@ -37,6 +37,9 @@ class Thumbnails { QImage sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *modified) const; void keep(const QImage &image, const QString &path, int pixels, const QDateTime &modified) const; + // Read only an existing, fresh PNG; never start a decoder on a miss. + QImage cached(const QString &path, int size) const; + static QString cacheKey(const QString &path); static QString bucketFor(int size); // A text file's first lines set on a page in the given colors. Made @@ -68,9 +71,11 @@ class Thumbnails { class ThumbnailProvider : public QQuickAsyncImageProvider { public: explicit ThumbnailProvider(const Thumbnails *thumbnails); + ~ThumbnailProvider() override; QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override; private: const Thumbnails *m_thumbnails; QThreadPool m_pool; + QThreadPool m_cachePool; }; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index d4cfcbe5..b361e919 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6272,6 +6272,92 @@ private slots: QCOMPARE(free(120'000'000'000), QLocale("ar_EG").toString(120.0, 'f', 1) + " GB free"); } + void thumbnailsCachedImagesDoNotWaitForGenerators() + { + QTemporaryDir dir; + const QString release = dir.filePath("release"); + QImage frame(64, 32, QImage::Format_RGB32); + frame.fill(Qt::green); + const QString photo = dir.filePath("photo.png"); + QVERIFY(frame.save(photo)); + write(dir.filePath("render"), "#!/bin/sh\nprintf started > \"$1.started\"\n" + "while [ ! -e \"" + release.toUtf8() + "\" ]; do /bin/sleep 0.01; done\n" + "/bin/cp \"" + photo.toUtf8() + "\" \"$2\"\n"); + write(dir.filePath("slow.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/sh \"" + + dir.filePath("render").toUtf8() + "\" %i %o\nMimeType=video/mp4;\n"); + Thumbnails thumbnails(dir.filePath("cache"), {dir.path()}); + QVERIFY(!thumbnails.load(photo, "image/png", 256).isNull()); + auto provider = std::make_unique(&thumbnails); + QList responses; + const auto cleanup = qScopeGuard([&] { + write(release, "go"); + // A direct provider caller owns responses until all workers finish. + provider.reset(); + qDeleteAll(responses); + }); + auto request = [&](const QString &path, const QString &mime) { + auto *response = provider->requestImageResponse("256/0/" + QUrl::toPercentEncoding(mime) + + "/" + QUrl::toPercentEncoding(path), {}); + responses.append(response); + return response; + }; + const int workers = qBound(2, QThread::idealThreadCount() / 2, 6); + for (int i = 0; i < workers; ++i) { + const QString path = dir.filePath(QString::number(i) + ".mp4"); + write(path, "clip"); + request(path, "video/mp4"); + } + QVERIFY(QTest::qWaitFor([&] { + return QDir(dir.path()).entryList({"*.started"}, QDir::Files).size() == workers; + }, 3000)); + auto *warm = request(photo, "image/png"); + QSignalSpy ready(warm, &QQuickImageResponse::finished); + QVERIFY(ready.wait(500)); // Renderers remain held until cleanup releases them. + std::unique_ptr texture(warm->textureFactory()); + QVERIFY(texture); + // Queued misses cancelled before a generator is available must finish + // without starting a helper or marking the file as failed. + const QString queued = dir.filePath("queued.mp4"); + write(queued, "clip"); + for (int i = 0; i < 64; ++i) + request(queued, "video/mp4")->cancel(); + write(release, "go"); + provider.reset(); // Drains both stages, including cancelled responses. + QVERIFY(!QFile::exists(queued + ".started")); + QVERIFY(!QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(queued)))); + } + + void thumbnailsCacheLookupNeverGeneratesOrReturnsStalePictures() + { + QTemporaryDir dir; + const QString photo = dir.filePath("photo.png"), cache = dir.filePath("cache"); + QImage frame(64, 32, QImage::Format_RGB32); + frame.fill(Qt::blue); + QVERIFY(frame.save(photo)); + Thumbnails thumbnails(cache, {dir.path()}); + QVERIFY(thumbnails.cached(photo, 256).isNull()); + const QSize expected = thumbnails.load(photo, "image/png", 256).size(); + QVERIFY(!expected.isEmpty()); + QCOMPARE(thumbnails.cached(photo, 256).size(), expected); + + QFile changed(photo); + QVERIFY(changed.open(QIODevice::ReadWrite)); + QVERIFY(changed.setFileTime(QFileInfo(photo).lastModified().addSecs(2), QFileDevice::FileModificationTime)); + changed.close(); + QVERIFY(thumbnails.cached(photo, 256).isNull()); + QVERIFY(!thumbnails.load(photo, "image/png", 256).isNull()); + const QString png = cache + "/" + Thumbnails::bucketFor(256) + "/" + Thumbnails::cacheKey(photo); + write(png, "not a PNG"); + QVERIFY(thumbnails.cached(photo, 256).isNull()); + QCOMPARE(thumbnails.load(photo, "image/png", 256).size(), expected); + QVERIFY(QFile::remove(photo)); + QVERIFY(thumbnails.cached(photo, 256).isNull()); + + const QString recursive = cache + "/inside.png"; + QVERIFY(frame.save(recursive)); + QVERIFY(thumbnails.cached(recursive, 256).isNull()); + } + void thumbnailsFollowTheFreedesktopCache() { QTemporaryDir dir; From 265e92304d2e68f1acddecb671e9c2dc25f22476 Mon Sep 17 00:00:00 2001 From: Kirill Kostin <15322854+k-kostin@users.noreply.github.com> Date: Fri, 13 Nov 2026 15:27:41 +0300 Subject: [PATCH 2/2] Observe fast cache completion through QML image state --- tests/folio_tests.cpp | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index b361e919..628dc2cf 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -6310,11 +6310,28 @@ private slots: QVERIFY(QTest::qWaitFor([&] { return QDir(dir.path()).entryList({"*.started"}, QDir::Files).size() == workers; }, 3000)); - auto *warm = request(photo, "image/png"); - QSignalSpy ready(warm, &QQuickImageResponse::finished); - QVERIFY(ready.wait(500)); // Renderers remain held until cleanup releases them. - std::unique_ptr texture(warm->textureFactory()); - QVERIFY(texture); + // The engine observes completion even if a very fast cache hit + // finishes before requestImageResponse() returns to its caller. + class BorrowedProvider : public QQuickAsyncImageProvider { + public: + explicit BorrowedProvider(ThumbnailProvider *provider) : target(provider) {} + QQuickImageResponse *requestImageResponse(const QString &id, const QSize &size) override { + return target->requestImageResponse(id, size); + } + ThumbnailProvider *target; + }; + QQmlEngine engine; + engine.addImageProvider("thumb", new BorrowedProvider(provider.get())); + QQmlComponent image(&engine); + image.setData("import QtQuick\nImage { asynchronous: true; cache: false }", QUrl()); + QVERIFY2(image.isReady(), qPrintable(image.errorString())); + std::unique_ptr warm(image.createWithInitialProperties({ + {"source", QUrl("image://thumb/256/0/image%2Fpng/" + QUrl::toPercentEncoding(photo))}})); + QVERIFY(warm); + QTRY_COMPARE_WITH_TIMEOUT(warm->property("status").toInt(), 1, 500); + QVERIFY(warm->property("implicitWidth").toInt() > 0); + warm.reset(); // The forwarding provider must not outlive the real one. + engine.removeImageProvider("thumb"); // Queued misses cancelled before a generator is available must finish // without starting a helper or marking the file as failed. const QString queued = dir.filePath("queued.mp4");