From b12c5b9ea072d1f3158b6e9979a0a1ae96eb448e Mon Sep 17 00:00:00 2001 From: Jonathan Kew Date: Sun, 9 Aug 2020 07:23:42 +0000 Subject: [PATCH] Bug 1657892 - Cache variation axes in the UnscaledFontMac to accelerate font instantiations. r=lsalzman Differential Revision: https://phabricator.services.mozilla.com/D86475 --- gfx/2d/ScaledFontMac.cpp | 29 ++++++++++++++++------------- gfx/2d/UnscaledFontMac.h | 11 ++++++++++- gfx/thebes/gfxMacFont.cpp | 2 +- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/gfx/2d/ScaledFontMac.cpp b/gfx/2d/ScaledFontMac.cpp index 5948a9e31ca6..920275baf4cb 100644 --- a/gfx/2d/ScaledFontMac.cpp +++ b/gfx/2d/ScaledFontMac.cpp @@ -493,7 +493,7 @@ ScaledFontMac::InstanceData::InstanceData( } static CFDictionaryRef CreateVariationDictionaryOrNull( - CGFontRef aCGFont, uint32_t aVariationCount, + CGFontRef aCGFont, CFArrayRef& aAxesCache, uint32_t aVariationCount, const FontVariation* aVariations) { // Avoid calling potentially buggy variation APIs on pre-Sierra macOS // versions (see bug 1331683) @@ -501,14 +501,16 @@ static CFDictionaryRef CreateVariationDictionaryOrNull( return nullptr; } - AutoRelease ctFont( - CTFontCreateWithGraphicsFont(aCGFont, 0, nullptr, nullptr)); - AutoRelease axes(CTFontCopyVariationAxes(ctFont)); - if (!axes) { - return nullptr; + if (!aAxesCache) { + AutoRelease ctFont( + CTFontCreateWithGraphicsFont(aCGFont, 0, nullptr, nullptr)); + aAxesCache = CTFontCopyVariationAxes(ctFont); + if (!aAxesCache) { + return nullptr; + } } - CFIndex axisCount = CFArrayGetCount(axes); + CFIndex axisCount = CFArrayGetCount(aAxesCache); AutoRelease dict(CFDictionaryCreateMutable( kCFAllocatorDefault, axisCount, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); @@ -520,7 +522,7 @@ static CFDictionaryRef CreateVariationDictionaryOrNull( for (CFIndex i = 0; i < axisCount; ++i) { // We sanity-check the axis info found in the CTFont, and bail out // (returning null) if it doesn't have the expected types. - CFTypeRef axisInfo = CFArrayGetValueAtIndex(axes, i); + CFTypeRef axisInfo = CFArrayGetValueAtIndex(aAxesCache, i); if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { return nullptr; } @@ -592,14 +594,15 @@ static CFDictionaryRef CreateVariationDictionaryOrNull( return dict.forget(); } +/* static */ CGFontRef UnscaledFontMac::CreateCGFontWithVariations( - CGFontRef aFont, uint32_t aVariationCount, + CGFontRef aFont, CFArrayRef& aAxesCache, uint32_t aVariationCount, const FontVariation* aVariations) { MOZ_ASSERT(aVariationCount > 0); MOZ_ASSERT(aVariations); - AutoRelease varDict( - CreateVariationDictionaryOrNull(aFont, aVariationCount, aVariations)); + AutoRelease varDict(CreateVariationDictionaryOrNull( + aFont, aAxesCache, aVariationCount, aVariations)); if (!varDict) { return nullptr; } @@ -622,8 +625,8 @@ already_AddRefed UnscaledFontMac::CreateScaledFont( CGFontRef fontRef = mFont; if (aNumVariations > 0) { - CGFontRef varFont = - CreateCGFontWithVariations(mFont, aNumVariations, aVariations); + CGFontRef varFont = CreateCGFontWithVariations(mFont, mAxesCache, + aNumVariations, aVariations); if (varFont) { fontRef = varFont; } diff --git a/gfx/2d/UnscaledFontMac.h b/gfx/2d/UnscaledFontMac.h index d4884a24fcad..86b62f14b15e 100644 --- a/gfx/2d/UnscaledFontMac.h +++ b/gfx/2d/UnscaledFontMac.h @@ -26,7 +26,12 @@ class UnscaledFontMac final : public UnscaledFont { : mFont(aFont), mIsDataFont(aIsDataFont) { CFRetain(mFont); } - virtual ~UnscaledFontMac() { CFRelease(mFont); } + virtual ~UnscaledFontMac() { + if (mAxesCache) { + CFRelease(mAxesCache); + } + CFRelease(mFont); + } FontType GetType() const override { return FontType::MAC; } @@ -47,16 +52,20 @@ class UnscaledFontMac final : public UnscaledFont { const FontVariation* aVariations, uint32_t aNumVariations) override; static CGFontRef CreateCGFontWithVariations(CGFontRef aFont, + CFArrayRef& aAxesCache, uint32_t aVariationCount, const FontVariation* aVariations); bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override; + CFArrayRef& AxesCache() { return mAxesCache; } + static already_AddRefed CreateFromFontDescriptor( const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex); private: CGFontRef mFont; + CFArrayRef mAxesCache = nullptr; // Cached array of variation axis details bool mIsDataFont; }; diff --git a/gfx/thebes/gfxMacFont.cpp b/gfx/thebes/gfxMacFont.cpp index 329a75da323b..0e8ff1cf6d5a 100644 --- a/gfx/thebes/gfxMacFont.cpp +++ b/gfx/thebes/gfxMacFont.cpp @@ -102,7 +102,7 @@ gfxMacFont::gfxMacFont(const RefPtr& aUnscaledFont, } mCGFont = UnscaledFontMac::CreateCGFontWithVariations( - baseFont, vars.Length(), vars.Elements()); + baseFont, aUnscaledFont->AxesCache(), vars.Length(), vars.Elements()); if (!mCGFont) { ::CFRetain(baseFont); mCGFont = baseFont;