[M108 Migration][Loading Performance] Add cache for mismatched SkTypeface 11/290311/3
authorGajendra N <gajendra.n@samsung.com>
Tue, 21 Mar 2023 08:58:22 +0000 (14:28 +0530)
committerBot Blink <blinkbot@samsung.com>
Thu, 23 Mar 2023 18:52:45 +0000 (18:52 +0000)
|matchFamilyName| is always called in |onLegacyCreateTypeface| to
match font. Because |SkTypeFaceCache| doesn't support for mismatched
typeface. |matchFamilyName| takes a long time (at least 1ms~).
This patch caches the mismatched SkTypeface and reduces the number of
|matchFamilyName| calls.
Hence, this patch improves loading performance.

Reference: https://review.tizen.org/gerrit/268117

Change-Id: Ie2697394c45affcb95d600f3faad84d39a06c6a3
Signed-off-by: Gajendra N <gajendra.n@samsung.com>
third_party/skia/src/core/SkTypefaceCache.cpp
third_party/skia/src/core/SkTypefaceCache.h
third_party/skia/src/ports/SkFontMgr_FontConfigInterface.cpp

index 3272d6a..f9874dd 100644 (file)
 
 #define TYPEFACE_CACHE_LIMIT    1024
 
+#if defined(USE_SK_MISMATCH_CACHE)
+#define MISMATCH_FONT_CACHE_LIMIT 64
+#endif
+
 SkTypefaceCache::SkTypefaceCache() {}
 
 void SkTypefaceCache::add(sk_sp<SkTypeface> face) {
@@ -50,6 +54,37 @@ void SkTypefaceCache::purgeAll() {
     this->purge(fTypefaces.count());
 }
 
+#if defined(USE_SK_MISMATCH_CACHE)
+void SkTypefaceCache::addMismatch(const char* requestedFamily) {
+    if (fMismatches.count() >= MISMATCH_FONT_CACHE_LIMIT) {
+        this->purgeMismatch(MISMATCH_FONT_CACHE_LIMIT >> 2);
+    }
+
+    fMismatches.emplace_back(SkString(requestedFamily));
+}
+
+bool SkTypefaceCache::findMismatch(const char* requestedFamily) {
+    for (const SkString& mismatch : fMismatches) {
+        if (mismatch.equals(requestedFamily)) {
+            return true;
+        }
+    }
+    return false;
+}
+
+void SkTypefaceCache::purgeMismatch(int numToPurge) {
+    int count = fMismatches.count();
+    int i = 0;
+    while (i < count) {
+        fMismatches.removeShuffle(i);
+        --count;
+        if (--numToPurge == 0) {
+            return;
+        }
+    }
+}
+#endif
+
 ///////////////////////////////////////////////////////////////////////////////
 
 SkTypefaceCache& SkTypefaceCache::Get() {
index 80bc341..69fabab 100644 (file)
 #include "include/core/SkTypeface.h"
 #include "include/private/SkTArray.h"
 
+#include "build/build_config.h"
+
+#if BUILDFLAG(IS_EFL)
+#define USE_SK_MISMATCH_CACHE
+#endif
+
 class SkTypefaceCache {
 public:
     SkTypefaceCache();
@@ -38,6 +44,12 @@ public:
      */
     sk_sp<SkTypeface> findByProcAndRef(FindProc proc, void* ctx) const;
 
+#if defined(USE_SK_MISMATCH_CACHE)
+    void addMismatch(const char*);
+    bool findMismatch(const char*);
+    void purgeMismatch(int numToPurge);
+#endif
+
     /**
      *  This will unref all of the typefaces in the cache for which the cache
      *  is the only owner. Normally this is handled automatically as needed.
@@ -69,6 +81,10 @@ private:
     void purge(int count);
 
     SkTArray<sk_sp<SkTypeface>> fTypefaces;
+
+#if defined(USE_SK_MISMATCH_CACHE)
+    SkTArray<SkString> fMismatches;
+#endif
 };
 
 #endif
index 33778e3..3dc5936 100644 (file)
@@ -302,12 +302,21 @@ protected:
             return sk_sp<SkTypeface>(face);
         }
 
+#if defined(USE_SK_MISMATCH_CACHE)
+        if (fTFCache.findMismatch(requestedFamilyName)) {
+            return nullptr;
+        }
+#endif
+
         SkFontConfigInterface::FontIdentity identity;
         SkString outFamilyName;
         SkFontStyle outStyle;
         if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
                                    &identity, &outFamilyName, &outStyle))
         {
+#if defined(USE_SK_MISMATCH_CACHE)
+            fTFCache.addMismatch(requestedFamilyName);
+#endif
             return nullptr;
         }