Title: [273237] trunk/Source/WebCore
Revision
273237
Author
[email protected]
Date
2021-02-22 03:39:10 -0800 (Mon, 22 Feb 2021)

Log Message

Move FontCascadeCache onto FontCache
https://bugs.webkit.org/show_bug.cgi?id=220858

Reviewed by Myles C. Maxfield.

Make FontCascadeCache and associated functions a child of FontCache,
accessible via FontCache::singleton.

No new tests, no behavior change.

* page/MemoryRelease.cpp:
(WebCore::releaseNoncriticalMemory):
* page/SettingsBase.cpp:
(WebCore::invalidateAfterGenericFamilyChange):
* platform/graphics/FontCache.cpp:
(WebCore::keysMatch):
(WebCore::FontCache::invalidateFontCascadeCache):
(WebCore::FontCache::clearWidthCaches):
(WebCore::makeFontCascadeCacheKey):
(WebCore::computeFontCascadeCacheHash):
(WebCore::FontCache::pruneUnreferencedEntriesFromFontCascadeCache):
(WebCore::FontCache::pruneSystemFallbackFonts):
(WebCore::FontCache::retrieveOrAddCachedFonts):
(WebCore::FontCache::updateFontCascade):
* platform/graphics/FontCache.h:
(WebCore::FontCascadeCacheEntry::FontCascadeCacheEntry):
* platform/graphics/FontCascade.cpp:
(WebCore::FontCascade::updateFonts const):
(WebCore::FontCascade::update const):
* platform/graphics/FontCascade.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (273236 => 273237)


--- trunk/Source/WebCore/ChangeLog	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/ChangeLog	2021-02-22 11:39:10 UTC (rev 273237)
@@ -1,3 +1,36 @@
+2021-02-22  Chris Lord  <[email protected]>
+
+        Move FontCascadeCache onto FontCache
+        https://bugs.webkit.org/show_bug.cgi?id=220858
+
+        Reviewed by Myles C. Maxfield.
+
+        Make FontCascadeCache and associated functions a child of FontCache,
+        accessible via FontCache::singleton.
+
+        No new tests, no behavior change.
+
+        * page/MemoryRelease.cpp:
+        (WebCore::releaseNoncriticalMemory):
+        * page/SettingsBase.cpp:
+        (WebCore::invalidateAfterGenericFamilyChange):
+        * platform/graphics/FontCache.cpp:
+        (WebCore::keysMatch):
+        (WebCore::FontCache::invalidateFontCascadeCache):
+        (WebCore::FontCache::clearWidthCaches):
+        (WebCore::makeFontCascadeCacheKey):
+        (WebCore::computeFontCascadeCacheHash):
+        (WebCore::FontCache::pruneUnreferencedEntriesFromFontCascadeCache):
+        (WebCore::FontCache::pruneSystemFallbackFonts):
+        (WebCore::FontCache::retrieveOrAddCachedFonts):
+        (WebCore::FontCache::updateFontCascade):
+        * platform/graphics/FontCache.h:
+        (WebCore::FontCascadeCacheEntry::FontCascadeCacheEntry):
+        * platform/graphics/FontCascade.cpp:
+        (WebCore::FontCascade::updateFonts const):
+        (WebCore::FontCascade::update const):
+        * platform/graphics/FontCascade.h:
+
 2021-02-22  Carlos Garcia Campos  <[email protected]>
 
         [SOUP] Add support for libsoup3

Modified: trunk/Source/WebCore/page/MemoryRelease.cpp (273236 => 273237)


--- trunk/Source/WebCore/page/MemoryRelease.cpp	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/page/MemoryRelease.cpp	2021-02-22 11:39:10 UTC (rev 273237)
@@ -67,8 +67,8 @@
     RenderTheme::singleton().purgeCaches();
 
     FontCache::singleton().purgeInactiveFontData();
+    FontCache::singleton().clearWidthCaches();
 
-    clearWidthCaches();
     TextPainter::clearGlyphDisplayLists();
 
     for (auto* document : Document::allDocuments()) {

Modified: trunk/Source/WebCore/page/SettingsBase.cpp (273236 => 273237)


--- trunk/Source/WebCore/page/SettingsBase.cpp	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/page/SettingsBase.cpp	2021-02-22 11:39:10 UTC (rev 273237)
@@ -34,7 +34,7 @@
 #include "DOMTimer.h"
 #include "Database.h"
 #include "Document.h"
-#include "FontCascade.h"
+#include "FontCache.h"
 #include "Frame.h"
 #include "FrameTree.h"
 #include "FrameView.h"
@@ -55,7 +55,7 @@
 
 static void invalidateAfterGenericFamilyChange(Page* page)
 {
-    invalidateFontCascadeCache();
+    FontCache::singleton().invalidateFontCascadeCache();
     if (page)
         page->setNeedsRecalcStyleInAllFrames();
 }

Modified: trunk/Source/WebCore/platform/graphics/FontCache.cpp (273236 => 273237)


--- trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/platform/graphics/FontCache.cpp	2021-02-22 11:39:10 UTC (rev 273237)
@@ -403,6 +403,103 @@
     platformPurgeInactiveFontData();
 }
 
+static bool keysMatch(const FontCascadeCacheKey& a, const FontCascadeCacheKey& b)
+{
+    if (a.fontDescriptionKey != b.fontDescriptionKey)
+        return false;
+    if (a.fontSelectorId != b.fontSelectorId || a.fontSelectorVersion != b.fontSelectorVersion)
+        return false;
+    unsigned size = a.families.size();
+    if (size != b.families.size())
+        return false;
+    for (unsigned i = 0; i < size; ++i) {
+        if (!FontCascadeDescription::familyNamesAreEqual(a.families[i], b.families[i]))
+            return false;
+    }
+    return true;
+}
+
+void FontCache::invalidateFontCascadeCache()
+{
+    m_fontCascadeCache.clear();
+}
+
+void FontCache::clearWidthCaches()
+{
+    for (auto& value : m_fontCascadeCache.values())
+        value->fonts.get().widthCache().clear();
+}
+
+static FontCascadeCacheKey makeFontCascadeCacheKey(const FontCascadeDescription& description, FontSelector* fontSelector)
+{
+    FontCascadeCacheKey key;
+    key.fontDescriptionKey = FontDescriptionKey(description);
+    unsigned familyCount = description.familyCount();
+    key.families.reserveInitialCapacity(familyCount);
+    for (unsigned i = 0; i < familyCount; ++i)
+        key.families.uncheckedAppend(description.familyAt(i));
+    key.fontSelectorId = fontSelector ? fontSelector->uniqueId() : 0;
+    key.fontSelectorVersion = fontSelector ? fontSelector->version() : 0;
+    return key;
+}
+
+static unsigned computeFontCascadeCacheHash(const FontCascadeCacheKey& key)
+{
+    // FIXME: Should hash the key and the family name characters rather than making a hash out of other hashes.
+    IntegerHasher hasher;
+    hasher.add(key.fontDescriptionKey.computeHash());
+    hasher.add(key.fontSelectorId);
+    hasher.add(key.fontSelectorVersion);
+    for (unsigned i = 0; i < key.families.size(); ++i) {
+        auto& family = key.families[i];
+        hasher.add(family.isNull() ? 0 : FontCascadeDescription::familyNameHash(family));
+    }
+    return hasher.hash();
+}
+
+void FontCache::pruneUnreferencedEntriesFromFontCascadeCache()
+{
+    m_fontCascadeCache.removeIf([](auto& entry) {
+        return entry.value->fonts.get().hasOneRef();
+    });
+}
+
+void FontCache::pruneSystemFallbackFonts()
+{
+    for (auto& entry : m_fontCascadeCache.values())
+        entry->fonts->pruneSystemFallbacks();
+}
+
+Ref<FontCascadeFonts> FontCache::retrieveOrAddCachedFonts(const FontCascadeDescription& fontDescription, RefPtr<FontSelector>&& fontSelector)
+{
+    auto key = makeFontCascadeCacheKey(fontDescription, fontSelector.get());
+
+    unsigned hash = computeFontCascadeCacheHash(key);
+    auto addResult = m_fontCascadeCache.add(hash, nullptr);
+    if (!addResult.isNewEntry && keysMatch(addResult.iterator->value->key, key))
+        return addResult.iterator->value->fonts.get();
+
+    auto& newEntry = addResult.iterator->value;
+    newEntry = makeUnique<FontCascadeCacheEntry>(WTFMove(key), FontCascadeFonts::create(WTFMove(fontSelector)));
+    Ref<FontCascadeFonts> glyphs = newEntry->fonts.get();
+
+    static const unsigned unreferencedPruneInterval = 50;
+    static const int maximumEntries = 400;
+    static unsigned pruneCounter;
+    // Referenced FontCascadeFonts would exist anyway so pruning them saves little memory.
+    if (!(++pruneCounter % unreferencedPruneInterval))
+        pruneUnreferencedEntriesFromFontCascadeCache();
+    // Prevent pathological growth.
+    if (m_fontCascadeCache.size() > maximumEntries)
+        m_fontCascadeCache.remove(m_fontCascadeCache.random());
+    return glyphs;
+}
+
+void FontCache::updateFontCascade(const FontCascade& fontCascade, RefPtr<FontSelector>&& fontSelector)
+{
+    fontCascade.updateFonts(retrieveOrAddCachedFonts(fontCascade.fontDescription(), WTFMove(fontSelector)));
+}
+
 size_t FontCache::fontCount()
 {
     return cachedFonts().size();

Modified: trunk/Source/WebCore/platform/graphics/FontCache.h (273236 => 273237)


--- trunk/Source/WebCore/platform/graphics/FontCache.h	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/platform/graphics/FontCache.h	2021-02-22 11:39:10 UTC (rev 273237)
@@ -29,6 +29,7 @@
 
 #pragma once
 
+#include "FontCascadeFonts.h"
 #include "FontDescription.h"
 #include "FontPlatformData.h"
 #include "FontTaggedSettings.h"
@@ -175,6 +176,27 @@
     static const bool safeToCompareToEmptyOrDeleted = true;
 };
 
+struct FontCascadeCacheKey {
+    FontDescriptionKey fontDescriptionKey; // Shared with the lower level FontCache (caching Font objects)
+    Vector<AtomString, 3> families;
+    unsigned fontSelectorId;
+    unsigned fontSelectorVersion;
+};
+
+struct FontCascadeCacheEntry {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    FontCascadeCacheEntry(FontCascadeCacheKey&& key, Ref<FontCascadeFonts>&& fonts)
+        : key(WTFMove(key))
+        , fonts(WTFMove(fonts))
+    { }
+    FontCascadeCacheKey key;
+    Ref<FontCascadeFonts> fonts;
+};
+
+// FIXME: Should make hash traits for FontCascadeCacheKey instead of using a hash as the key (so we hash a hash).
+typedef HashMap<unsigned, std::unique_ptr<FontCascadeCacheEntry>, AlreadyHashed> FontCascadeCache;
+
 class FontCache {
     friend class WTF::NeverDestroyed<FontCache>;
 
@@ -220,6 +242,10 @@
     WEBCORE_EXPORT void purgeInactiveFontData(unsigned count = UINT_MAX);
     void platformPurgeInactiveFontData();
 
+    void updateFontCascade(const FontCascade&, RefPtr<FontSelector>&&);
+    void invalidateFontCascadeCache();
+    void clearWidthCaches();
+
 #if PLATFORM(WIN)
     RefPtr<Font> fontFromDescriptionAndLogFont(const FontDescription&, const LOGFONT&, AtomString& outFontFamilyName);
 #endif
@@ -252,6 +278,9 @@
     ~FontCache() = delete;
 
     WEBCORE_EXPORT void purgeInactiveFontDataIfNeeded();
+    void pruneUnreferencedEntriesFromFontCascadeCache();
+    void pruneSystemFallbackFonts();
+    Ref<FontCascadeFonts> retrieveOrAddCachedFonts(const FontCascadeDescription&, RefPtr<FontSelector>&&);
 
     // FIXME: This method should eventually be removed.
     FontPlatformData* getCachedFontPlatformData(const FontDescription&, const AtomString& family, const FontFeatureSettings* fontFaceFeatures = nullptr, FontSelectionSpecifiedCapabilities fontFaceCapabilities = { }, bool checkingAlternateName = false);
@@ -266,6 +295,8 @@
     
     bool m_shouldMockBoldSystemFontForAccessibility { false };
 
+    FontCascadeCache m_fontCascadeCache;
+
 #if PLATFORM(COCOA)
     ListHashSet<String> m_seenFamiliesForPrewarming;
     ListHashSet<String> m_fontNamesRequiringSystemFallbackForPrewarming;

Modified: trunk/Source/WebCore/platform/graphics/FontCascade.cpp (273236 => 273237)


--- trunk/Source/WebCore/platform/graphics/FontCascade.cpp	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/platform/graphics/FontCascade.cpp	2021-02-22 11:39:10 UTC (rev 273237)
@@ -146,125 +146,6 @@
     return true;
 }
 
-struct FontCascadeCacheKey {
-    FontDescriptionKey fontDescriptionKey; // Shared with the lower level FontCache (caching Font objects)
-    Vector<AtomString, 3> families;
-    unsigned fontSelectorId;
-    unsigned fontSelectorVersion;
-};
-
-struct FontCascadeCacheEntry {
-    WTF_MAKE_FAST_ALLOCATED;
-public:
-    FontCascadeCacheEntry(FontCascadeCacheKey&& key, Ref<FontCascadeFonts>&& fonts)
-        : key(WTFMove(key))
-        , fonts(WTFMove(fonts))
-    { }
-    FontCascadeCacheKey key;
-    Ref<FontCascadeFonts> fonts;
-};
-
-// FIXME: Should make hash traits for FontCascadeCacheKey instead of using a hash as the key (so we hash a hash).
-typedef HashMap<unsigned, std::unique_ptr<FontCascadeCacheEntry>, AlreadyHashed> FontCascadeCache;
-
-static bool keysMatch(const FontCascadeCacheKey& a, const FontCascadeCacheKey& b)
-{
-    if (a.fontDescriptionKey != b.fontDescriptionKey)
-        return false;
-    if (a.fontSelectorId != b.fontSelectorId || a.fontSelectorVersion != b.fontSelectorVersion)
-        return false;
-    unsigned size = a.families.size();
-    if (size != b.families.size())
-        return false;
-    for (unsigned i = 0; i < size; ++i) {
-        if (!FontCascadeDescription::familyNamesAreEqual(a.families[i], b.families[i]))
-            return false;
-    }
-    return true;
-}
-
-static FontCascadeCache& fontCascadeCache()
-{
-    static NeverDestroyed<FontCascadeCache> cache;
-    return cache.get();
-}
-
-void invalidateFontCascadeCache()
-{
-    fontCascadeCache().clear();
-}
-
-void clearWidthCaches()
-{
-    for (auto& value : fontCascadeCache().values())
-        value->fonts.get().widthCache().clear();
-}
-
-static FontCascadeCacheKey makeFontCascadeCacheKey(const FontCascadeDescription& description, FontSelector* fontSelector)
-{
-    FontCascadeCacheKey key;
-    key.fontDescriptionKey = FontDescriptionKey(description);
-    unsigned familyCount = description.familyCount();
-    key.families.reserveInitialCapacity(familyCount);
-    for (unsigned i = 0; i < familyCount; ++i)
-        key.families.uncheckedAppend(description.familyAt(i));
-    key.fontSelectorId = fontSelector ? fontSelector->uniqueId() : 0;
-    key.fontSelectorVersion = fontSelector ? fontSelector->version() : 0;
-    return key;
-}
-
-static unsigned computeFontCascadeCacheHash(const FontCascadeCacheKey& key)
-{
-    // FIXME: Should hash the key and the family name characters rather than making a hash out of other hashes.
-    IntegerHasher hasher;
-    hasher.add(key.fontDescriptionKey.computeHash());
-    hasher.add(key.fontSelectorId);
-    hasher.add(key.fontSelectorVersion);
-    for (unsigned i = 0; i < key.families.size(); ++i) {
-        auto& family = key.families[i];
-        hasher.add(family.isNull() ? 0 : FontCascadeDescription::familyNameHash(family));
-    }
-    return hasher.hash();
-}
-
-void pruneUnreferencedEntriesFromFontCascadeCache()
-{
-    fontCascadeCache().removeIf([](auto& entry) {
-        return entry.value->fonts.get().hasOneRef();
-    });
-}
-
-void pruneSystemFallbackFonts()
-{
-    for (auto& entry : fontCascadeCache().values())
-        entry->fonts->pruneSystemFallbacks();
-}
-
-static Ref<FontCascadeFonts> retrieveOrAddCachedFonts(const FontCascadeDescription& fontDescription, RefPtr<FontSelector>&& fontSelector)
-{
-    auto key = makeFontCascadeCacheKey(fontDescription, fontSelector.get());
-
-    unsigned hash = computeFontCascadeCacheHash(key);
-    auto addResult = fontCascadeCache().add(hash, nullptr);
-    if (!addResult.isNewEntry && keysMatch(addResult.iterator->value->key, key))
-        return addResult.iterator->value->fonts.get();
-
-    auto& newEntry = addResult.iterator->value;
-    newEntry = makeUnique<FontCascadeCacheEntry>(WTFMove(key), FontCascadeFonts::create(WTFMove(fontSelector)));
-    Ref<FontCascadeFonts> glyphs = newEntry->fonts.get();
-
-    static const unsigned unreferencedPruneInterval = 50;
-    static const int maximumEntries = 400;
-    static unsigned pruneCounter;
-    // Referenced FontCascadeFonts would exist anyway so pruning them saves little memory.
-    if (!(++pruneCounter % unreferencedPruneInterval))
-        pruneUnreferencedEntriesFromFontCascadeCache();
-    // Prevent pathological growth.
-    if (fontCascadeCache().size() > maximumEntries)
-        fontCascadeCache().remove(fontCascadeCache().random());
-    return glyphs;
-}
-
 bool FontCascade::isCurrent(const FontSelector& fontSelector) const
 {
     if (!m_fonts)
@@ -277,14 +158,19 @@
     return true;
 }
 
-void FontCascade::update(RefPtr<FontSelector>&& fontSelector) const
+void FontCascade::updateFonts(Ref<FontCascadeFonts>&& fonts) const
 {
-    m_fonts = retrieveOrAddCachedFonts(m_fontDescription, WTFMove(fontSelector));
+    m_fonts = WTFMove(fonts);
     m_useBackslashAsYenSymbol = useBackslashAsYenSignForFamily(firstFamily());
     m_enableKerning = computeEnableKerning();
     m_requiresShaping = computeRequiresShaping();
 }
 
+void FontCascade::update(RefPtr<FontSelector>&& fontSelector) const
+{
+    FontCache::singleton().updateFontCascade(*this, WTFMove(fontSelector));
+}
+
 GlyphBuffer FontCascade::layoutText(CodePath codePathToUse, const TextRun& run, unsigned from, unsigned to, ForTextEmphasisOrNot forTextEmphasis) const
 {
     if (codePathToUse != CodePath::Complex)

Modified: trunk/Source/WebCore/platform/graphics/FontCascade.h (273236 => 273237)


--- trunk/Source/WebCore/platform/graphics/FontCascade.h	2021-02-22 11:07:31 UTC (rev 273236)
+++ trunk/Source/WebCore/platform/graphics/FontCascade.h	2021-02-22 11:39:10 UTC (rev 273237)
@@ -112,6 +112,7 @@
     float size() const { return fontDescription().computedSize(); }
 
     bool isCurrent(const FontSelector&) const;
+    void updateFonts(Ref<FontCascadeFonts>&&) const;
     WEBCORE_EXPORT void update(RefPtr<FontSelector>&& = nullptr) const;
 
     enum CustomFontNotReadyAction { DoNotPaintIfFontNotReady, UseFallbackIfFontNotReady };
@@ -329,11 +330,6 @@
     mutable bool m_requiresShaping { false }; // Computed from m_fontDescription.
 };
 
-void invalidateFontCascadeCache();
-void pruneUnreferencedEntriesFromFontCascadeCache();
-void pruneSystemFallbackFonts();
-void clearWidthCaches();
-
 inline const Font& FontCascade::primaryFont() const
 {
     ASSERT(m_fonts);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to