Title: [275308] trunk/Source/WebCore
Revision
275308
Author
cl...@igalia.com
Date
2021-03-31 15:03:07 -0700 (Wed, 31 Mar 2021)

Log Message

Make FontCache self-contained (remove static global variables)
https://bugs.webkit.org/show_bug.cgi?id=223995

Reviewed by Darin Adler.

This refactors FontCache so that the iOS font lock, the
FontPlatformDataCache, the FontDataCache, the FontVerticalDataCache,
the FontSelector clients list and the generation counter all live on
FontCache instead of in static global variables.

No new tests, no change in behavior.

* platform/graphics/FontCache.cpp:
(WebCore::FontCache::FontCache):
(WebCore::FontPlatformDataCacheKey::isHashTableDeletedValue const):
(WebCore::FontCache::getCachedFontPlatformData):
(WebCore::FontCache::verticalData):
(WebCore::FontCache::fontForPlatformData):
(WebCore::FontCache::purgeInactiveFontDataIfNeeded):
(WebCore::FontCache::purgeInactiveFontData):
(WebCore::FontCache::fontCount):
(WebCore::FontCache::inactiveFontCount):
(WebCore::FontCache::addClient):
(WebCore::FontCache::removeClient):
(WebCore::FontCache::invalidate):
* platform/graphics/FontCache.h:
(WebCore::FontCache::generation const):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (275307 => 275308)


--- trunk/Source/WebCore/ChangeLog	2021-03-31 21:48:22 UTC (rev 275307)
+++ trunk/Source/WebCore/ChangeLog	2021-03-31 22:03:07 UTC (rev 275308)
@@ -1,3 +1,33 @@
+2021-03-31  Chris Lord  <cl...@igalia.com>
+
+        Make FontCache self-contained (remove static global variables)
+        https://bugs.webkit.org/show_bug.cgi?id=223995
+
+        Reviewed by Darin Adler.
+
+        This refactors FontCache so that the iOS font lock, the
+        FontPlatformDataCache, the FontDataCache, the FontVerticalDataCache,
+        the FontSelector clients list and the generation counter all live on
+        FontCache instead of in static global variables.
+
+        No new tests, no change in behavior.
+
+        * platform/graphics/FontCache.cpp:
+        (WebCore::FontCache::FontCache):
+        (WebCore::FontPlatformDataCacheKey::isHashTableDeletedValue const):
+        (WebCore::FontCache::getCachedFontPlatformData):
+        (WebCore::FontCache::verticalData):
+        (WebCore::FontCache::fontForPlatformData):
+        (WebCore::FontCache::purgeInactiveFontDataIfNeeded):
+        (WebCore::FontCache::purgeInactiveFontData):
+        (WebCore::FontCache::fontCount):
+        (WebCore::FontCache::inactiveFontCount):
+        (WebCore::FontCache::addClient):
+        (WebCore::FontCache::removeClient):
+        (WebCore::FontCache::invalidate):
+        * platform/graphics/FontCache.h:
+        (WebCore::FontCache::generation const):
+
 2021-03-31  Sam Weinig  <wei...@apple.com>
 
         Merge DOMWindow+IndexedDatabase.idl and WorkerGlobalScope+IndexedDatabase.idl into a single WindowOrWorkerGlobalScope+IndexedDatabase.idl to match the IndexedDB spec

Modified: trunk/Source/WebCore/platform/graphics/FontCache.cpp (275307 => 275308)


--- trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-03-31 21:48:22 UTC (rev 275307)
+++ trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-03-31 22:03:07 UTC (rev 275308)
@@ -46,15 +46,6 @@
 #include "OpenTypeVerticalData.h"
 #endif
 
-#if PLATFORM(IOS_FAMILY)
-#include <wtf/Lock.h>
-#include <wtf/RecursiveLockAdapter.h>
-
-static RecursiveLock fontLock;
-
-#endif // PLATFORM(IOS_FAMILY)
-
-
 namespace WebCore {
 using namespace WTF;
 
@@ -66,6 +57,11 @@
 
 FontCache::FontCache()
     : m_purgeTimer(*this, &FontCache::purgeInactiveFontDataIfNeeded)
+    , m_fontPlatformDataCache(makeUniqueRef<FontPlatformDataCache>())
+    , m_fontDataCache(makeUniqueRef<FontDataCache>())
+#if ENABLE(OPENTYPE_VERTICAL)
+    , m_fontVerticalDataCache(makeUniqueRef<FontVerticalDataCache>())
+#endif
 {
 }
 
@@ -85,7 +81,6 @@
     { }
 
     bool isHashTableDeletedValue() const { return m_fontDescriptionKey.isHashTableDeletedValue(); }
-
     bool operator==(const FontPlatformDataCacheKey& other) const
     {
         if (m_fontDescriptionKey != other.m_fontDescriptionKey
@@ -126,7 +121,7 @@
             hasher.add(std::numeric_limits<unsigned>::max());
         return hasher.hash();
     }
-         
+
     static bool equal(const FontPlatformDataCacheKey& a, const FontPlatformDataCacheKey& b)
     {
         return a == b;
@@ -139,14 +134,6 @@
     static const bool emptyValueIsZero = false;
 };
 
-typedef HashMap<FontPlatformDataCacheKey, std::unique_ptr<FontPlatformData>, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyHashTraits> FontPlatformDataCache;
-
-static FontPlatformDataCache& fontPlatformDataCache()
-{
-    static NeverDestroyed<FontPlatformDataCache> cache;
-    return cache;
-}
-
 const AtomString& FontCache::alternateFamilyName(const AtomString& familyName)
 {
     static MainThreadNeverDestroyed<const AtomString> arial("Arial", AtomString::ConstructFromLiteral);
@@ -199,9 +186,9 @@
     const FontFeatureSettings* fontFaceFeatures, FontSelectionSpecifiedCapabilities fontFaceCapabilities, bool checkingAlternateName)
 {
 #if PLATFORM(IOS_FAMILY)
-    auto locker = holdLock(fontLock);
+    auto locker = holdLock(m_fontLock);
 #endif
-    
+
 #if OS(WINDOWS) && ENABLE(OPENTYPE_VERTICAL)
     // Leading "@" in the font name enables Windows vertical flow flag for the font.
     // Because we do vertical flow by ourselves, we don't want to use the Windows feature.
@@ -220,7 +207,7 @@
 
     FontPlatformDataCacheKey key(familyName, fontDescription, fontFaceFeatures, fontFaceCapabilities);
 
-    auto addResult = fontPlatformDataCache().add(key, nullptr);
+    auto addResult = m_fontPlatformDataCache->add(key, nullptr);
     FontPlatformDataCache::iterator it = addResult.iterator;
     if (addResult.isNewEntry) {
         it->value = createFontPlatformData(fontDescription, familyName, fontFaceFeatures, fontFaceCapabilities);
@@ -233,8 +220,8 @@
                 FontPlatformData* fontPlatformDataForAlternateName = getCachedFontPlatformData(fontDescription, alternateName, fontFaceFeatures, fontFaceCapabilities, true);
                 // Lookup the key in the hash table again as the previous iterator may have
                 // been invalidated by the recursive call to getCachedFontPlatformData().
-                it = fontPlatformDataCache().find(key);
-                ASSERT(it != fontPlatformDataCache().end());
+                it = m_fontPlatformDataCache->find(key);
+                ASSERT(it != m_fontPlatformDataCache->end());
                 if (fontPlatformDataForAlternateName)
                     it->value = makeUnique<FontPlatformData>(*fontPlatformDataForAlternateName);
             }
@@ -249,7 +236,7 @@
     {
         return platformData.hash();
     }
-         
+
     static bool equal(const FontPlatformData& a, const FontPlatformData& b)
     {
         return a == b;
@@ -275,26 +262,10 @@
     }
 };
 
-typedef HashMap<FontPlatformData, Ref<Font>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontDataCache;
-
-static FontDataCache& cachedFonts()
-{
-    static NeverDestroyed<FontDataCache> cache;
-    return cache;
-}
-
 #if ENABLE(OPENTYPE_VERTICAL)
-typedef HashMap<FontPlatformData, RefPtr<OpenTypeVerticalData>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontVerticalDataCache;
-
-FontVerticalDataCache& fontVerticalDataCache()
-{
-    static NeverDestroyed<FontVerticalDataCache> fontVerticalDataCache;
-    return fontVerticalDataCache;
-}
-
 RefPtr<OpenTypeVerticalData> FontCache::verticalData(const FontPlatformData& platformData)
 {
-    auto addResult = fontVerticalDataCache().ensure(platformData, [&platformData] {
+    auto addResult = m_fontVerticalDataCache->ensure(platformData, [&platformData] {
         return OpenTypeVerticalData::create(platformData);
     });
     return addResult.iterator->value;
@@ -326,10 +297,10 @@
 Ref<Font> FontCache::fontForPlatformData(const FontPlatformData& platformData)
 {
 #if PLATFORM(IOS_FAMILY)
-    auto locker = holdLock(fontLock);
+    auto locker = holdLock(m_fontLock);
 #endif
-    
-    auto addResult = cachedFonts().ensure(platformData, [&] {
+
+    auto addResult = m_fontDataCache->ensure(platformData, [&] {
         return Font::create(platformData);
     });
 
@@ -345,7 +316,7 @@
 
     LOG(Fonts, "FontCache::purgeInactiveFontDataIfNeeded() - underMemoryPressure %d, inactiveFontDataLimit %u", underMemoryPressure, inactiveFontDataLimit);
 
-    if (cachedFonts().size() < inactiveFontDataLimit)
+    if (m_fontDataCache->size() < inactiveFontDataLimit)
         return;
     unsigned inactiveCount = inactiveFontCount();
     if (inactiveCount <= inactiveFontDataLimit)
@@ -363,12 +334,12 @@
     pruneSystemFallbackFonts();
 
 #if PLATFORM(IOS_FAMILY)
-    auto locker = holdLock(fontLock);
+    auto locker = holdLock(m_fontLock);
 #endif
 
     while (purgeCount) {
         Vector<Ref<Font>, 20> fontsToDelete;
-        for (auto& font : cachedFonts().values()) {
+        for (auto& font : m_fontDataCache->values()) {
             LOG(Fonts, " trying to purge font %s (has one ref %d)", font->platformData().description().utf8().data(), font->hasOneRef());
             if (!font->hasOneRef())
                 continue;
@@ -380,18 +351,18 @@
         if (fontsToDelete.isEmpty())
             break;
         for (auto& font : fontsToDelete) {
-            bool success = cachedFonts().remove(font->platformData());
+            bool success = m_fontDataCache->remove(font->platformData());
             ASSERT_UNUSED(success, success);
 #if ENABLE(OPENTYPE_VERTICAL)
-            fontVerticalDataCache().remove(font->platformData());
+            m_fontVerticalDataCache->remove(font->platformData());
 #endif
         }
     };
 
     Vector<FontPlatformDataCacheKey> keysToRemove;
-    keysToRemove.reserveInitialCapacity(fontPlatformDataCache().size());
-    for (auto& entry : fontPlatformDataCache()) {
-        if (entry.value && !cachedFonts().contains(*entry.value))
+    keysToRemove.reserveInitialCapacity(m_fontPlatformDataCache->size());
+    for (auto& entry : m_fontPlatformDataCache.get()) {
+        if (entry.value && !m_fontDataCache->contains(*entry.value))
             keysToRemove.uncheckedAppend(entry.key);
     }
 
@@ -398,7 +369,7 @@
     LOG(Fonts, " removing %lu keys", keysToRemove.size());
 
     for (auto& key : keysToRemove)
-        fontPlatformDataCache().remove(key);
+        m_fontPlatformDataCache->remove(key);
 
     platformPurgeInactiveFontData();
 }
@@ -498,16 +469,16 @@
 
 size_t FontCache::fontCount()
 {
-    return cachedFonts().size();
+    return m_fontDataCache->size();
 }
 
 size_t FontCache::inactiveFontCount()
 {
 #if PLATFORM(IOS_FAMILY)
-    auto locker = holdLock(fontLock);
+    auto locker = holdLock(m_fontLock);
 #endif
     unsigned count = 0;
-    for (auto& font : cachedFonts().values()) {
+    for (auto& font : m_fontDataCache->values()) {
         if (font->hasOneRef())
             ++count;
     }
@@ -514,50 +485,32 @@
     return count;
 }
 
-static HashSet<FontSelector*>* gClients;
-
 void FontCache::addClient(FontSelector& client)
 {
-    if (!gClients)
-        gClients = new HashSet<FontSelector*>;
-
-    ASSERT(!gClients->contains(&client));
-    gClients->add(&client);
+    ASSERT(!m_clients.contains(&client));
+    m_clients.add(&client);
 }
 
 void FontCache::removeClient(FontSelector& client)
 {
-    ASSERT(gClients);
-    ASSERT(gClients->contains(&client));
+    ASSERT(m_clients.contains(&client));
 
-    gClients->remove(&client);
+    m_clients.remove(&client);
 }
 
-static unsigned short gGeneration = 0;
-
-unsigned short FontCache::generation()
-{
-    return gGeneration;
-}
-
 void FontCache::invalidate()
 {
-    if (!gClients) {
-        ASSERT(fontPlatformDataCache().isEmpty());
-        return;
-    }
-
-    fontPlatformDataCache().clear();
+    m_fontPlatformDataCache->clear();
 #if ENABLE(OPENTYPE_VERTICAL)
-    fontVerticalDataCache().clear();
+    m_fontVerticalDataCache->clear();
 #endif
     invalidateFontCascadeCache();
 
-    gGeneration++;
+    m_generation++;
 
     Vector<Ref<FontSelector>> clients;
-    clients.reserveInitialCapacity(gClients->size());
-    for (auto it = gClients->begin(), end = gClients->end(); it != end; ++it)
+    clients.reserveInitialCapacity(m_clients.size());
+    for (auto it = m_clients.begin(), end = m_clients.end(); it != end; ++it)
         clients.uncheckedAppend(**it);
 
     for (unsigned i = 0; i < clients.size(); ++i)

Modified: trunk/Source/WebCore/platform/graphics/FontCache.h (275307 => 275308)


--- trunk/Source/WebCore/platform/graphics/FontCache.h	2021-03-31 21:48:22 UTC (rev 275307)
+++ trunk/Source/WebCore/platform/graphics/FontCache.h	2021-03-31 22:03:07 UTC (rev 275308)
@@ -40,6 +40,7 @@
 #include <wtf/HashTraits.h>
 #include <wtf/ListHashSet.h>
 #include <wtf/RefPtr.h>
+#include <wtf/UniqueRef.h>
 #include <wtf/Vector.h>
 #include <wtf/WorkQueue.h>
 #include <wtf/text/AtomStringHash.h>
@@ -49,6 +50,11 @@
 #include "FontCacheCoreText.h"
 #endif
 
+#if PLATFORM(IOS_FAMILY)
+#include <wtf/Lock.h>
+#include <wtf/RecursiveLockAdapter.h>
+#endif
+
 #if OS(WINDOWS)
 #include <windows.h>
 #include <objidl.h>
@@ -58,11 +64,15 @@
 namespace WebCore {
 
 class FontCascade;
-class FontPlatformData;
 class FontSelector;
 class OpenTypeVerticalData;
 class Font;
 enum class IsForPlatformFont : uint8_t;
+struct FontDataCacheKeyHash;
+struct FontDataCacheKeyTraits;
+struct FontPlatformDataCacheKey;
+struct FontPlatformDataCacheKeyHash;
+struct FontPlatformDataCacheKeyHashTraits;
 
 #if PLATFORM(WIN)
 #if USE(IMLANG_FONT_LINK2)
@@ -208,6 +218,12 @@
 
 using FontCascadeCache = HashMap<FontCascadeCacheKey, std::unique_ptr<FontCascadeCacheEntry>, FontCascadeCacheKeyHash, FontCascadeCacheKeyHashTraits>;
 
+typedef HashMap<FontPlatformDataCacheKey, std::unique_ptr<FontPlatformData>, FontPlatformDataCacheKeyHash, FontPlatformDataCacheKeyHashTraits> FontPlatformDataCache;
+typedef HashMap<FontPlatformData, Ref<Font>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontDataCache;
+#if ENABLE(OPENTYPE_VERTICAL)
+typedef HashMap<FontPlatformData, RefPtr<OpenTypeVerticalData>, FontDataCacheKeyHash, FontDataCacheKeyTraits> FontVerticalDataCache;
+#endif
+
 class FontCache {
     friend class WTF::NeverDestroyed<FontCache>;
 
@@ -245,7 +261,7 @@
     void addClient(FontSelector&);
     void removeClient(FontSelector&);
 
-    unsigned short generation();
+    unsigned short generation() const { return m_generation; }
     WEBCORE_EXPORT void invalidate();
 
     WEBCORE_EXPORT size_t fontCount();
@@ -306,8 +322,20 @@
     
     bool m_shouldMockBoldSystemFontForAccessibility { false };
 
+    HashSet<FontSelector*> m_clients;
+    UniqueRef<FontPlatformDataCache> m_fontPlatformDataCache;
+    UniqueRef<FontDataCache> m_fontDataCache;
+#if ENABLE(OPENTYPE_VERTICAL)
+    UniqueRef<FontVerticalDataCache> m_fontVerticalDataCache;
+#endif
     FontCascadeCache m_fontCascadeCache;
 
+    unsigned short m_generation { 0 };
+
+#if PLATFORM(IOS_FAMILY)
+    RecursiveLock m_fontLock;
+#endif
+
 #if PLATFORM(COCOA)
     ListHashSet<String> m_seenFamiliesForPrewarming;
     ListHashSet<String> m_fontNamesRequiringSystemFallbackForPrewarming;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to