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
60 changes: 40 additions & 20 deletions src/thumbnails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <QCryptographicHash>
#include <QtEndian>
#include <zlib.h>
#include <QDeadlineTimer>
#include <QDir>
#include <QFont>
#include <QFontMetricsF>
Expand All @@ -27,6 +28,24 @@

namespace {

bool isCancelled(const std::atomic_bool *cancelled) {
return cancelled && *cancelled;
}

// Keep the existing timeout, but release a worker when its image is discarded.
bool waitForHelper(QProcess &process, const std::atomic_bool *cancelled) {
const QDeadlineTimer deadline(15000);
while (!isCancelled(cancelled) && !deadline.hasExpired()) {
if (process.waitForFinished(int(qMin<qint64>(100, deadline.remainingTime()))))
return !isCancelled(cancelled);
if (process.state() == QProcess::NotRunning)
return false;
}
process.kill();
process.waitForFinished(1000);
return false;
}

QString fileUri(const QString &path) {
return QUrl::fromLocalFile(path).toString(QUrl::FullyEncoded);
}
Expand Down Expand Up @@ -162,7 +181,7 @@ QString Thumbnails::sharpPath(const QString &path, int pixels, bool alpha) const
return QFileInfo(m_cache).absolutePath() + "/folio/sharp/" + QString::number(pixels) + "/" + key + (alpha ? ".png" : ".jpg");
}

QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *read) const {
QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *read, const std::atomic_bool *cancelled) const {
*fresh = false;
const QFileInfo info(path);
if (!info.isFile())
Expand All @@ -178,15 +197,15 @@ QImage Thumbnails::sharp(const QString &path, const QString &mime, int pixels, b
QImageReader reader(path);
reader.setAutoTransform(true);
if (!reader.canRead())
return load(path, mime, 1024);
return load(path, mime, 1024, cancelled);
const QSize full = reader.size();
// Small enough already: read as it is, quickly, and not kept.
const bool small = full.isValid() && full.width() <= pixels && full.height() <= pixels;
if (full.isValid() && !small)
reader.setScaledSize(full.scaled(pixels, pixels, Qt::KeepAspectRatio));
const QImage image = reader.read();
if (image.isNull())
return load(path, mime, 1024);
return load(path, mime, 1024, cancelled);
*fresh = !small;
return image;
}
Expand All @@ -211,7 +230,9 @@ void Thumbnails::keep(const QImage &image, const QString &path, int pixels, cons
}
}

QImage Thumbnails::load(const QString &path, const QString &mime, int size) const {
QImage Thumbnails::load(const QString &path, const QString &mime, int size, const std::atomic_bool *cancelled) const {
if (isCancelled(cancelled))
return {};
const QFileInfo info(path);
if (!info.isFile())
return {};
Expand All @@ -235,7 +256,10 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
const QString type = FolderModel::typeByContent(path, named);
const int pixels = bucketPixels(bucketFor(size));
bool attempted = false;
QImage image = generate(path, type, pixels, attempted);
QImage image = generate(path, type, pixels, attempted, cancelled);
// Discarding a picture says nothing about whether its file is broken.
if (isCancelled(cancelled))
return {};
if (image.isNull()) {
// Only a reader or thumbnailer that ran and failed is remembered, so
// one installed later still gets its turn.
Expand All @@ -249,7 +273,9 @@ QImage Thumbnails::load(const QString &path, const QString &mime, int size) cons
return image;
}

QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted) const {
QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels, bool &attempted, const std::atomic_bool *cancelled) const {
if (isCancelled(cancelled))
return {};
// Common photo formats decode fastest in-process; anything Qt can't read
// (HEIC, JPEG XL) falls through to the installed thumbnailers.
if (mime.startsWith("image/")) {
Expand All @@ -268,13 +294,13 @@ QImage Thumbnails::generate(const QString &path, const QString &mime, int pixels
const QString command = m_thumbnailers.value(mime);
if (!command.isEmpty()) {
attempted = true;
return runThumbnailer(command, path, pixels);
return runThumbnailer(command, path, pixels, cancelled);
}
// Without a PDF thumbnailer installed, Poppler's pdftoppm (there with
// CUPS) draws the first page.
if (mime == "application/pdf" && !QStandardPaths::findExecutable("pdftoppm").isEmpty()) {
attempted = true;
return pdfPage(path, pixels);
return pdfPage(path, pixels, cancelled);
}
return {};
}
Expand Down Expand Up @@ -326,24 +352,21 @@ QImage Thumbnails::textPage(const QString &path, int pixels, const QColor &paper
return page;
}

QImage Thumbnails::pdfPage(const QString &path, int pixels) {
QImage Thumbnails::pdfPage(const QString &path, int pixels, const std::atomic_bool *cancelled) {
QTemporaryDir scratch;
if (!scratch.isValid())
return {};
QProcess process;
process.start("pdftoppm", {"-f", "1", "-l", "1", "-singlefile", "-png", "-scale-to", QString::number(pixels),
path, scratch.filePath("page")});
if (!process.waitForFinished(15000)) {
process.kill();
process.waitForFinished(1000);
if (!waitForHelper(process, cancelled))
return {};
}
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0)
return {};
return QImage(scratch.filePath("page.png"));
}

QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels) const {
QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, int pixels, const std::atomic_bool *cancelled) const {
QTemporaryDir scratch;
if (!scratch.isValid())
return {};
Expand Down Expand Up @@ -372,11 +395,8 @@ QImage Thumbnails::runThumbnailer(const QString &command, const QString &path, i
process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
process.start(program, args);
// A broken file must never hang a tile forever.
if (!process.waitForFinished(15000)) {
process.kill();
process.waitForFinished(1000);
if (!waitForHelper(process, cancelled))
return {};
}
if (process.exitStatus() != QProcess::NormalExit || process.exitCode() != 0)
return {};
return QImage(output);
Expand Down Expand Up @@ -475,7 +495,7 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable {
const QString path = QUrl::fromPercentEncoding(parts.mid(4).join('/').toUtf8());
bool fresh = false;
QDateTime modified;
m_image = m_thumbnails->sharp(path, mime, pixels, &fresh, &modified);
m_image = m_thumbnails->sharp(path, mime, pixels, &fresh, &modified, &m_cancelled);
// Shown first, kept after: writing it shouldn't hold it up. Qt
// may delete the response once it's finished, so nothing of it
// is used after.
Expand All @@ -497,7 +517,7 @@ class ThumbnailResponse : public QQuickImageResponse, public QRunnable {
const int size = parts.at(0).toInt();
const QString mime = QUrl::fromPercentEncoding(parts.at(2).toUtf8());
const QString path = QUrl::fromPercentEncoding(parts.mid(3).join('/').toUtf8());
m_image = m_thumbnails->load(path, mime, qBound(64, size, 1024));
m_image = m_thumbnails->load(path, mime, qBound(64, size, 1024), &m_cancelled);
}
if (m_image.isNull())
m_error = QStringLiteral("No thumbnail");
Expand Down
11 changes: 6 additions & 5 deletions src/thumbnails.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <atomic>
#include <QHash>
#include <QDateTime>
#include <QImage>
Expand All @@ -24,7 +25,7 @@ class Thumbnails {

// Returns a thumbnail no larger than `size` on its longest side, from the
// cache when it is fresh, or a null image when none can be made.
QImage load(const QString &path, const QString &mime, int size) const;
QImage load(const QString &path, const QString &mime, int size, const std::atomic_bool *cancelled = nullptr) const;

// A picture at `pixels` on its longest side, as sharp as the details
// column draws it: read from the file when it's that small already,
Expand All @@ -34,7 +35,7 @@ class Thumbnails {
// now, for keep() to cache, dated `modified`: the picture's time as it
// was before its pixels were read, so one saved over meanwhile is made
// again rather than kept old under its new time.
QImage sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *modified) const;
QImage sharp(const QString &path, const QString &mime, int pixels, bool *fresh, QDateTime *modified, const std::atomic_bool *cancelled = nullptr) const;
void keep(const QImage &image, const QString &path, int pixels, const QDateTime &modified) const;

static QString cacheKey(const QString &path);
Expand All @@ -49,9 +50,9 @@ class Thumbnails {
QString failPath(const QString &path) const;
QString sharpPath(const QString &path, int pixels, bool alpha) const;
// Sets `attempted` when Qt's reader or a thumbnailer could take the file.
QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted) const;
QImage runThumbnailer(const QString &command, const QString &path, int pixels) const;
static QImage pdfPage(const QString &path, int pixels);
QImage generate(const QString &path, const QString &mime, int pixels, bool &attempted, const std::atomic_bool *cancelled) const;
QImage runThumbnailer(const QString &command, const QString &path, int pixels, const std::atomic_bool *cancelled) const;
static QImage pdfPage(const QString &path, int pixels, const std::atomic_bool *cancelled);
void remember(QImage image, const QString &path, int size, const QDateTime &modified) const;
void rememberFailure(const QString &path, const QDateTime &modified) const;
bool failedBefore(const QString &path, const QDateTime &modified) const;
Expand Down
53 changes: 53 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6549,6 +6549,59 @@ private slots:
QCOMPARE(shownWidth(second.addMSecs(600)), 128);
}

void thumbnailsStopWorkWhenItsImagesGoAway()
{
QTemporaryDir dir;
QImage frame(64, 32, QImage::Format_RGB32);
frame.fill(Qt::green);
QVERIFY(frame.save(dir.filePath("frame.png")));
// One slow thumbnail per worker, like leaving a folder whose visible tiles are still loading.
write(dir.filePath("render"), "#!/bin/sh\nprintf started > \"$1.started\"\n"
"case \"${1##*/}\" in slow*.mp4) exec /usr/bin/sleep 2.5;; esac\ncp \""
+ dir.filePath("frame.png").toUtf8() + "\" \"$2\"\n");
write(dir.filePath("test.thumbnailer"), "[Thumbnailer Entry]\nExec=/bin/sh \""
+ dir.filePath("render").toUtf8() + "\" %i %o\nMimeType=video/mp4;\n");
Thumbnails thumbnails(dir.filePath("cache"), {dir.path()});
QStringList files;
{
QQmlEngine engine;
engine.addImageProvider("thumb", new ThumbnailProvider(&thumbnails));
QQmlComponent component(&engine);
component.setData("import QtQuick\nImage { asynchronous: true; cache: false }", QUrl());
QVERIFY2(component.isReady(), qPrintable(component.errorString()));
auto source = [](const QString &path) {
return QUrl("image://thumb/128/0/video%2Fmp4/" + QUrl::toPercentEncoding(path));
};
QList<QObject *> images;
const auto clear = qScopeGuard([&] { qDeleteAll(images); });
// Fill every slot in ThumbnailProvider's bounded pool.
const int workers = qBound(2, QThread::idealThreadCount() / 2, 6);
for (int i = 0; i < workers; ++i) {
const QString path = dir.filePath("slow" + QString::number(i) + ".mp4");
files << path;
write(path, "clip");
images << component.createWithInitialProperties({{"source", source(path)}});
QVERIFY(images.last());
}
QTRY_VERIFY_WITH_TIMEOUT(std::all_of(files.begin(), files.end(), [](const QString &path) {
return QFile::exists(path + ".started");
}), 3000);

for (auto *image : images)
QVERIFY(image->setProperty("source", QUrl()));
const QString next = dir.filePath("next.mp4");
write(next, "clip");
QVERIFY(images.first()->setProperty("source", source(next)));
// Its image is ready without waiting for the discarded folder's 2.5-second jobs.
QTRY_COMPARE_WITH_TIMEOUT(images.first()->property("status").toInt(), 1, 1000);
QCOMPARE(images.first()->property("implicitWidth").toInt(), 64);
}
for (const auto &path : files) {
QVERIFY(!QFile::exists(dir.filePath("cache/normal/" + Thumbnails::cacheKey(path))));
QVERIFY(!QFile::exists(dir.filePath("cache/fail/folio/" + Thumbnails::cacheKey(path))));
}
}

void thumbnailsReadInstalledThumbnailers()
{
QTemporaryDir dir;
Expand Down
Loading