← Back to team overview

ubuntu-sdk-team team mailing list archive

[Merge] lp:~aacid/ubuntu-ui-toolkit/save_stat_loading_already_loaded_image into lp:ubuntu-ui-toolkit/staging

 

Albert Astals Cid has proposed merging lp:~aacid/ubuntu-ui-toolkit/save_stat_loading_already_loaded_image into lp:ubuntu-ui-toolkit/staging.

Commit message:
Save a stat of the image we're loading is already in the cache

No need to call UCUnits::resolveResource to learn we just need to load it normally because the fact that we already loaded it normally means we need to load it normally.

Requested reviews:
  Ubuntu SDK team (ubuntu-sdk-team)
Related bugs:
  Bug #1558663 in ubuntu-ui-toolkit (Ubuntu): "Ubuntu Components is hard on the disk and CPU when loading images"
  https://bugs.launchpad.net/ubuntu/+source/ubuntu-ui-toolkit/+bug/1558663

For more details, see:
https://code.launchpad.net/~aacid/ubuntu-ui-toolkit/save_stat_loading_already_loaded_image/+merge/316235
-- 
Your team Ubuntu SDK team is requested to review the proposed merge of lp:~aacid/ubuntu-ui-toolkit/save_stat_loading_already_loaded_image into lp:ubuntu-ui-toolkit/staging.
=== modified file 'src/UbuntuToolkit/ucqquickimageextension.cpp'
--- src/UbuntuToolkit/ucqquickimageextension.cpp	2016-09-12 09:03:50 +0000
+++ src/UbuntuToolkit/ucqquickimageextension.cpp	2017-02-02 14:51:24 +0000
@@ -22,7 +22,9 @@
 #include <QtCore/QFile>
 #include <QtCore/QFileInfo>
 #include <QtGui/QGuiApplication>
+#include <QtQuick/private/qquickitem_p.h>
 #include <QtQuick/private/qquickimagebase_p.h>
+#include <QtQuick/private/qquickpixmapcache_p.h>
 
 #include "ucunits_p.h"
 
@@ -61,12 +63,33 @@
     return m_source;
 }
 
+
 void UCQQuickImageExtension::setSource(const QUrl& url)
 {
     if (url != m_source) {
         m_source = url;
+        reloadSourceOrPostEvent();
+    }
+}
+
+void UCQQuickImageExtension::reloadSourceOrPostEvent()
+{
+    // We need to wait until the component is complete
+    // so that m_image->sourceSize() is actually valid
+    if (QQuickItemPrivate::get(m_image)->componentComplete) {
         reloadSource();
-    }
+    } else {
+        qApp->postEvent(this, new QEvent(QEvent::User));
+    }
+}
+
+bool UCQQuickImageExtension::event(QEvent *e)
+{
+    if (e->type() == QEvent::User) {
+        reloadSourceOrPostEvent();
+        return true;
+    }
+    return QObject::event(e);
 }
 
 void UCQQuickImageExtension::reloadSource()
@@ -80,6 +103,22 @@
         return;
     }
 
+    // If the url we're trying to load is already in the cache and
+    // the devicePixelRatio is 1, we save calling UCUnits::resolveResource
+    // and just set that image directly.
+    // UCUnits::resolveResource is not cheap (does a stat on disk)
+    if (qFuzzyCompare(qGuiApp->devicePixelRatio(), (qreal)1.0)) {
+        QSize ss = m_image->sourceSize();
+        if (ss.isNull() && m_image->image().isNull()) {
+            ss = QSize(-1, -1);
+        }
+
+        if (QQuickPixmap::isCached(m_source, ss)) {
+            m_image->setSource(m_source);
+            return;
+        }
+    }
+
     QString resolved = UCUnits::instance()->resolveResource(m_source);
 
     if (resolved.isEmpty()) {
@@ -98,7 +137,9 @@
             || selectedFilePath.endsWith(QStringLiteral(".svgz"))) {
             // Take care to pass the original fragment
             QUrl selectedFileUrl(QUrl::fromLocalFile(selectedFilePath));
-            selectedFileUrl.setFragment(fragment);
+            if (m_source.hasFragment()) {
+                selectedFileUrl.setFragment(fragment);
+            }
             m_image->setSource(selectedFileUrl);
         } else {
             // Need to scale the pixel-based image to suit the devicePixelRatio setting ourselves.

=== modified file 'src/UbuntuToolkit/ucqquickimageextension_p.h'
--- src/UbuntuToolkit/ucqquickimageextension_p.h	2016-09-09 17:49:07 +0000
+++ src/UbuntuToolkit/ucqquickimageextension_p.h	2017-02-02 14:51:24 +0000
@@ -50,11 +50,14 @@
     void reloadSource();
 
 protected:
+    bool event(QEvent *e) override;
     bool rewriteSciFile(const QString &sciFilePath, const QString &scaleFactor, QTextStream& output);
     QString scaledBorder(const QString &border, const QString &scaleFactor);
     QString scaledSource(QString source, const QString &sciFilePath, const QString &scaleFactor);
 
 private:
+    void reloadSourceOrPostEvent();
+
     QQuickImageBase* m_image;
     QUrl m_source;
     static QHash<QUrl, QSharedPointer<QTemporaryFile> > s_rewrittenSciFiles;


Follow ups