From c0da7869001a52668f4ffaa851e05dd0ca6e6d67 Mon Sep 17 00:00:00 2001
From: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@nokia.com>
Date: Thu, 1 Mar 2012 11:24:57 +0100
Subject: [PATCH] Avoid unnecessary updates when using in-process cache

Whenever an in-process cache is updated, it will emit
itemsAvailable() signals to all listening glyph caches. This will
in turn cause each of the glyph caches to update and each
of the glyph nodes to be preprocessed (the entire scene graph will
be updated.) This happens even if the changes to the in-process
cache are requested by an external client, due to a cross-process
cache sharing mechanism. However, itemsAvailable() signals are
only interesting if the items were requested by the in-process
cache.

We therefore add a mechanism now to check if the glyphs were actually
requested by the cache before updating anything.

Change-Id: I529f94b3928c2a5e06fec354014fa11d21671057
Reviewed-by: Jiang Jiang <jiang.jiang@nokia.com>
---
 .../qsgshareddistancefieldglyphcache.cpp      | 39 ++++++++++++++++---
 .../qsgshareddistancefieldglyphcache_p.h      |  2 +
 2 files changed, 36 insertions(+), 5 deletions(-)

diff --git a/src/quick/scenegraph/qsgshareddistancefieldglyphcache.cpp b/src/quick/scenegraph/qsgshareddistancefieldglyphcache.cpp
index e762898602..951ee210c5 100644
--- a/src/quick/scenegraph/qsgshareddistancefieldglyphcache.cpp
+++ b/src/quick/scenegraph/qsgshareddistancefieldglyphcache.cpp
@@ -90,7 +90,7 @@ QSGSharedDistanceFieldGlyphCache::QSGSharedDistanceFieldGlyphCache(const QByteAr
             this, SLOT(reportItemsAvailable(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
             Qt::DirectConnection);
     connect(sharedGraphicsCache, SIGNAL(itemsUpdated(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
-            this, SLOT(reportItemsAvailable(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
+            this, SLOT(reportItemsUpdated(QByteArray,void*,QSize,QVector<quint32>,QVector<QPoint>)),
             Qt::DirectConnection);
     connect(sharedGraphicsCache, SIGNAL(itemsInvalidated(QByteArray,QVector<quint32>)),
             this, SLOT(reportItemsInvalidated(QByteArray,QVector<quint32>)),
@@ -542,9 +542,38 @@ void QSGSharedDistanceFieldGlyphCache::processPendingGlyphs()
 }
 
 void QSGSharedDistanceFieldGlyphCache::reportItemsAvailable(const QByteArray &cacheId,
-                                                        void *bufferId, const QSize &bufferSize,
-                                                        const QVector<quint32> &itemIds,
-                                                        const QVector<QPoint> &positions)
+                                                            void *bufferId,
+                                                            const QSize &bufferSize,
+                                                            const QVector<quint32> &itemIds,
+                                                            const QVector<QPoint> &positions)
+{
+    bool requestedItemsInList = false;
+    {
+        QMutexLocker locker(&m_pendingGlyphsMutex);
+        if (m_cacheId != cacheId)
+            return;
+
+#if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
+            qDebug("QSGSharedDistanceFieldGlyphCache::reportItemsAvailable() called for %s (%d glyphs, bufferSize: %dx%d)",
+                   cacheId.constData(), itemIds.size(), bufferSize.width(), bufferSize.height());
+#endif
+
+        for (int i=0; i<itemIds.size(); ++i) {
+            if (m_requestedGlyphsThatHaveNotBeenReturned.contains(itemIds.at(i))) {
+                requestedItemsInList = true;
+                break;
+            }
+        }
+    }
+
+    if (requestedItemsInList)
+        reportItemsUpdated(cacheId, bufferId, bufferSize, itemIds, positions);
+}
+
+void QSGSharedDistanceFieldGlyphCache::reportItemsUpdated(const QByteArray &cacheId,
+                                                          void *bufferId, const QSize &bufferSize,
+                                                          const QVector<quint32> &itemIds,
+                                                          const QVector<QPoint> &positions)
 {
     {
         QMutexLocker locker(&m_pendingGlyphsMutex);
@@ -554,7 +583,7 @@ void QSGSharedDistanceFieldGlyphCache::reportItemsAvailable(const QByteArray &ca
         Q_ASSERT(itemIds.size() == positions.size());
 
 #if defined(QSGSHAREDDISTANCEFIELDGLYPHCACHE_DEBUG)
-        qDebug("QSGSharedDistanceFieldGlyphCache::reportItemsAvailable() called for %s (%d glyphs, bufferSize: %dx%d)",
+        qDebug("QSGSharedDistanceFieldGlyphCache::reportItemsUpdated() called for %s (%d glyphs, bufferSize: %dx%d)",
                cacheId.constData(), itemIds.size(), bufferSize.width(), bufferSize.height());
 #endif
 
diff --git a/src/quick/scenegraph/qsgshareddistancefieldglyphcache_p.h b/src/quick/scenegraph/qsgshareddistancefieldglyphcache_p.h
index cadf4bc55b..8250b9706e 100644
--- a/src/quick/scenegraph/qsgshareddistancefieldglyphcache_p.h
+++ b/src/quick/scenegraph/qsgshareddistancefieldglyphcache_p.h
@@ -78,6 +78,8 @@ private Q_SLOTS:
     void reportItemsAvailable(const QByteArray &cacheId,
                           void *bufferId, const QSize &bufferSize,
                           const QVector<quint32> &itemIds, const QVector<QPoint> &positions);
+    void reportItemsUpdated(const QByteArray &cacheId, void *bufferId, const QSize &bufferSize,
+                            const QVector<quint32> &itemIds, const QVector<QPoint> &positions);
     void reportItemsInvalidated(const QByteArray &cacheId, const QVector<quint32> &itemIds);
 
 private:
-- 
GitLab