diff --git a/src/PreviewWindow.qml b/src/PreviewWindow.qml index 71ed54ea..b96e702f 100644 --- a/src/PreviewWindow.qml +++ b/src/PreviewWindow.qml @@ -228,6 +228,15 @@ ApplicationWindow { Image { id: still + // Share the in-flight decode across scale reloads. The hidden + // child uses the same window/DPR and releases its pixels at Ready. + property Image keeper: Image { + parent: still + visible: false + asynchronous: true + autoTransform: true + source: still.status === Image.Loading ? still.source : "" + } visible: !flick.animated width: flick.natural.width * flick.zoom height: flick.natural.height * flick.zoom diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index d4cfcbe5..17209b88 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include #include #include @@ -6649,6 +6650,63 @@ private slots: QVERIFY(Previewer::markdownOpening(longFence, 600).count('|') <= 600 + 4); } + void previewSharesAnInFlightDecodeAcrossReloads() + { + // A window learning its final display scale makes the picture ask + // for its pixels again while the first decode is still running. + // That reload must join the decode in flight, not start a second. + struct GatedImages : QQuickImageProvider { + GatedImages() : QQuickImageProvider(QQuickImageProvider::Image) {} + std::atomic reads{0}; + QSemaphore release; + QImage requestImage(const QString &, QSize *size, const QSize &) override + { + if (reads.fetch_add(1) == 0) + release.acquire(); + QImage image(1600, 900, QImage::Format_RGB32); + image.fill(Qt::darkCyan); + *size = image.size(); + return image; + } + }; + QTemporaryDir home; + Theme theme(home.filePath("theme")); + QQmlEngine engine; + auto *images = new GatedImages; + engine.addImageProvider("gated", images); + const auto unblock = qScopeGuard([&] { images->release.release(); }); + QQmlComponent stand(&engine); + stand.setData("import QtQml\n" + "QtObject {\n" + " signal changed\n" + " property var item: ({ name: 'Picture', path: '/picture.png', mime: 'image/png', size: 1 })\n" + " property string viewer: 'image'\n" + " property string url: 'image://gated/picture'\n" + " property int count: 1\n" + " property int index: 0\n" + " property string type: 'PNG'\n" + " property string text\n" + "}\n", + QUrl()); + std::unique_ptr preview(stand.create()); + QVERIFY2(preview, qPrintable(stand.errorString())); + engine.rootContext()->setContextProperty("theme", &theme); + engine.rootContext()->setContextProperty("previewer", preview.get()); + QQmlComponent component(&engine, QUrl("qrc:/PreviewWindow.qml")); + std::unique_ptr root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + auto *image = root->property("view").value()->property("source").value(); + QVERIFY(image); + QTRY_COMPARE(images->reads.load(), 1); + // Turning the cache off and on drops and re-requests the image as a + // scale change does, while leaving its source alone. + QVERIFY(image->setProperty("cache", false)); + QVERIFY(image->setProperty("cache", true)); + images->release.release(); + QTRY_COMPARE(image->property("status").toInt(), 1); + QCOMPARE(images->reads.load(), 1); + } + void previewerStepsThroughTheFolder() { QTemporaryDir dir;