Skip to content

Commit

Permalink
[Skia] Create the font manager on demand when first needed
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=271396

Reviewed by Adrian Perez de Castro.

We are creating the font manager in FontCache::platformInit(), but
that's only called once for all the instances. This makes all offscreen
canvas tests rendering text to crash, because the FontCache in the
worker thread doesn't have a font manager.

* Source/WebCore/platform/graphics/FontCache.h:
(WebCore::FontCache::fontManager const): Deleted.
* Source/WebCore/platform/graphics/skia/FontCacheSkia.cpp:
(WebCore::FontCache::platformInit):
(WebCore::FontCache::fontManager const):
(WebCore::FontCache::systemFallbackForCharacterCluster):
(WebCore::FontCache::systemFontFamilies):
(WebCore::FontCache::createFontPlatformData):
* Source/WebCore/platform/graphics/skia/FontCustomPlatformDataSkia.cpp:
(WebCore::FontCustomPlatformData::create):

Canonical link: https://commits.webkit.org/276483@main
  • Loading branch information
carlosgcampos committed Mar 21, 2024
1 parent 84ef0be commit 82bbfe7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Source/WebCore/platform/graphics/FontCache.h
Expand Up @@ -199,7 +199,7 @@ class FontCache {

#if USE(SKIA)
static Vector<hb_feature_t> computeFeatures(const FontDescription&, const FontCreationContext&);
SkFontMgr* fontManager() const { return m_fontManager.get(); }
SkFontMgr& fontManager() const;
SkiaHarfBuzzFontCache& harfBuzzFontCache() { return m_harfBuzzFontCache; }
#endif

Expand Down Expand Up @@ -268,7 +268,7 @@ class FontCache {
#endif

#if USE(SKIA)
sk_sp<SkFontMgr> m_fontManager;
mutable sk_sp<SkFontMgr> m_fontManager;
SkiaHarfBuzzFontCache m_harfBuzzFontCache;
#endif

Expand Down
18 changes: 13 additions & 5 deletions Source/WebCore/platform/graphics/skia/FontCacheSkia.cpp
Expand Up @@ -39,7 +39,14 @@ namespace WebCore {

void FontCache::platformInit()
{
m_fontManager = SkFontMgr_New_FontConfig(FcConfigReference(nullptr));
}

SkFontMgr& FontCache::fontManager() const
{
if (!m_fontManager)
m_fontManager = SkFontMgr_New_FontConfig(FcConfigReference(nullptr));
RELEASE_ASSERT(m_fontManager);
return *m_fontManager.get();
}

RefPtr<Font> FontCache::systemFallbackForCharacterCluster(const FontDescription& description, const Font&, IsForPlatformFont, PreferColoredFont, StringView stringView)
Expand All @@ -61,19 +68,20 @@ RefPtr<Font> FontCache::systemFallbackForCharacterCluster(const FontDescription&

// FIXME: handle synthetic properties.
auto features = computeFeatures(description, { });
auto typeface = m_fontManager->matchFamilyStyleCharacter(nullptr, { }, bcp47.data(), bcp47.size(), baseCharacter);
auto typeface = fontManager().matchFamilyStyleCharacter(nullptr, { }, bcp47.data(), bcp47.size(), baseCharacter);
FontPlatformData alternateFontData(WTFMove(typeface), description.computedSize(), false /* syntheticBold */, false /* syntheticOblique */, description.orientation(), description.widthVariant(), description.textRenderingMode(), WTFMove(features));
return fontForPlatformData(alternateFontData);
}

Vector<String> FontCache::systemFontFamilies()
{
int count = m_fontManager->countFamilies();
auto& manager = fontManager();
int count = manager.countFamilies();
Vector<String> fontFamilies;
fontFamilies.reserveInitialCapacity(count);
for (int i = 0; i < count; ++i) {
SkString familyName;
m_fontManager->getFamilyName(i, &familyName);
manager.getFamilyName(i, &familyName);
fontFamilies.append(String::fromUTF8(familyName.data()));
}
return fontFamilies;
Expand Down Expand Up @@ -319,7 +327,7 @@ static SkFontStyle skiaFontStyle(const FontDescription& fontDescription)
std::unique_ptr<FontPlatformData> FontCache::createFontPlatformData(const FontDescription& fontDescription, const AtomString& family, const FontCreationContext& fontCreationContext)
{
auto familyName = getFamilyNameStringFromFamily(family);
auto typeface = m_fontManager->matchFamilyStyle(familyName.utf8().data(), skiaFontStyle(fontDescription));
auto typeface = fontManager().matchFamilyStyle(familyName.utf8().data(), skiaFontStyle(fontDescription));
if (!typeface)
return nullptr;

Expand Down
Expand Up @@ -101,7 +101,7 @@ FontPlatformData FontCustomPlatformData::fontPlatformData(const FontDescription&

RefPtr<FontCustomPlatformData> FontCustomPlatformData::create(SharedBuffer& buffer, const String& itemInCollection)
{
sk_sp<SkTypeface> typeface = FontCache::forCurrentThread().fontManager()->makeFromData(buffer.createSkData());
sk_sp<SkTypeface> typeface = FontCache::forCurrentThread().fontManager().makeFromData(buffer.createSkData());
if (!typeface)
return nullptr;

Expand Down

0 comments on commit 82bbfe7

Please sign in to comment.