Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 54 additions & 9 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include <QMimeDatabase>
#include <QProcess>
#include <QQuickTextureFactory>
#include <QRunnable>
#include <QSaveFile>
#include <QFile>
#include <QStandardPaths>
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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; }
Expand All @@ -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;
Expand All @@ -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;
}
5 changes: 5 additions & 0 deletions src/thumbnails.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};
103 changes: 103 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6272,6 +6272,109 @@ 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<ThumbnailProvider>(&thumbnails);
QList<QQuickImageResponse *> 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));
// 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<QObject> 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");
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;
Expand Down
Loading