Bug 1657892 - Cache variation axes in the UnscaledFontMac to accelerate font instantiations. r=lsalzman

Differential Revision: https://phabricator.services.mozilla.com/D86475
This commit is contained in:
Jonathan Kew 2020-08-09 07:23:42 +00:00
Родитель 24ad033dab
Коммит b12c5b9ea0
3 изменённых файлов: 27 добавлений и 15 удалений

Просмотреть файл

@ -493,7 +493,7 @@ ScaledFontMac::InstanceData::InstanceData(
} }
static CFDictionaryRef CreateVariationDictionaryOrNull( static CFDictionaryRef CreateVariationDictionaryOrNull(
CGFontRef aCGFont, uint32_t aVariationCount, CGFontRef aCGFont, CFArrayRef& aAxesCache, uint32_t aVariationCount,
const FontVariation* aVariations) { const FontVariation* aVariations) {
// Avoid calling potentially buggy variation APIs on pre-Sierra macOS // Avoid calling potentially buggy variation APIs on pre-Sierra macOS
// versions (see bug 1331683) // versions (see bug 1331683)
@ -501,14 +501,16 @@ static CFDictionaryRef CreateVariationDictionaryOrNull(
return nullptr; return nullptr;
} }
if (!aAxesCache) {
AutoRelease<CTFontRef> ctFont( AutoRelease<CTFontRef> ctFont(
CTFontCreateWithGraphicsFont(aCGFont, 0, nullptr, nullptr)); CTFontCreateWithGraphicsFont(aCGFont, 0, nullptr, nullptr));
AutoRelease<CFArrayRef> axes(CTFontCopyVariationAxes(ctFont)); aAxesCache = CTFontCopyVariationAxes(ctFont);
if (!axes) { if (!aAxesCache) {
return nullptr; return nullptr;
} }
}
CFIndex axisCount = CFArrayGetCount(axes); CFIndex axisCount = CFArrayGetCount(aAxesCache);
AutoRelease<CFMutableDictionaryRef> dict(CFDictionaryCreateMutable( AutoRelease<CFMutableDictionaryRef> dict(CFDictionaryCreateMutable(
kCFAllocatorDefault, axisCount, &kCFTypeDictionaryKeyCallBacks, kCFAllocatorDefault, axisCount, &kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks)); &kCFTypeDictionaryValueCallBacks));
@ -520,7 +522,7 @@ static CFDictionaryRef CreateVariationDictionaryOrNull(
for (CFIndex i = 0; i < axisCount; ++i) { for (CFIndex i = 0; i < axisCount; ++i) {
// We sanity-check the axis info found in the CTFont, and bail out // We sanity-check the axis info found in the CTFont, and bail out
// (returning null) if it doesn't have the expected types. // (returning null) if it doesn't have the expected types.
CFTypeRef axisInfo = CFArrayGetValueAtIndex(axes, i); CFTypeRef axisInfo = CFArrayGetValueAtIndex(aAxesCache, i);
if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) { if (CFDictionaryGetTypeID() != CFGetTypeID(axisInfo)) {
return nullptr; return nullptr;
} }
@ -592,14 +594,15 @@ static CFDictionaryRef CreateVariationDictionaryOrNull(
return dict.forget(); return dict.forget();
} }
/* static */
CGFontRef UnscaledFontMac::CreateCGFontWithVariations( CGFontRef UnscaledFontMac::CreateCGFontWithVariations(
CGFontRef aFont, uint32_t aVariationCount, CGFontRef aFont, CFArrayRef& aAxesCache, uint32_t aVariationCount,
const FontVariation* aVariations) { const FontVariation* aVariations) {
MOZ_ASSERT(aVariationCount > 0); MOZ_ASSERT(aVariationCount > 0);
MOZ_ASSERT(aVariations); MOZ_ASSERT(aVariations);
AutoRelease<CFDictionaryRef> varDict( AutoRelease<CFDictionaryRef> varDict(CreateVariationDictionaryOrNull(
CreateVariationDictionaryOrNull(aFont, aVariationCount, aVariations)); aFont, aAxesCache, aVariationCount, aVariations));
if (!varDict) { if (!varDict) {
return nullptr; return nullptr;
} }
@ -622,8 +625,8 @@ already_AddRefed<ScaledFont> UnscaledFontMac::CreateScaledFont(
CGFontRef fontRef = mFont; CGFontRef fontRef = mFont;
if (aNumVariations > 0) { if (aNumVariations > 0) {
CGFontRef varFont = CGFontRef varFont = CreateCGFontWithVariations(mFont, mAxesCache,
CreateCGFontWithVariations(mFont, aNumVariations, aVariations); aNumVariations, aVariations);
if (varFont) { if (varFont) {
fontRef = varFont; fontRef = varFont;
} }

Просмотреть файл

@ -26,7 +26,12 @@ class UnscaledFontMac final : public UnscaledFont {
: mFont(aFont), mIsDataFont(aIsDataFont) { : mFont(aFont), mIsDataFont(aIsDataFont) {
CFRetain(mFont); CFRetain(mFont);
} }
virtual ~UnscaledFontMac() { CFRelease(mFont); } virtual ~UnscaledFontMac() {
if (mAxesCache) {
CFRelease(mAxesCache);
}
CFRelease(mFont);
}
FontType GetType() const override { return FontType::MAC; } FontType GetType() const override { return FontType::MAC; }
@ -47,16 +52,20 @@ class UnscaledFontMac final : public UnscaledFont {
const FontVariation* aVariations, uint32_t aNumVariations) override; const FontVariation* aVariations, uint32_t aNumVariations) override;
static CGFontRef CreateCGFontWithVariations(CGFontRef aFont, static CGFontRef CreateCGFontWithVariations(CGFontRef aFont,
CFArrayRef& aAxesCache,
uint32_t aVariationCount, uint32_t aVariationCount,
const FontVariation* aVariations); const FontVariation* aVariations);
bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override; bool GetFontDescriptor(FontDescriptorOutput aCb, void* aBaton) override;
CFArrayRef& AxesCache() { return mAxesCache; }
static already_AddRefed<UnscaledFont> CreateFromFontDescriptor( static already_AddRefed<UnscaledFont> CreateFromFontDescriptor(
const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex); const uint8_t* aData, uint32_t aDataLength, uint32_t aIndex);
private: private:
CGFontRef mFont; CGFontRef mFont;
CFArrayRef mAxesCache = nullptr; // Cached array of variation axis details
bool mIsDataFont; bool mIsDataFont;
}; };

Просмотреть файл

@ -102,7 +102,7 @@ gfxMacFont::gfxMacFont(const RefPtr<UnscaledFontMac>& aUnscaledFont,
} }
mCGFont = UnscaledFontMac::CreateCGFontWithVariations( mCGFont = UnscaledFontMac::CreateCGFontWithVariations(
baseFont, vars.Length(), vars.Elements()); baseFont, aUnscaledFont->AxesCache(), vars.Length(), vars.Elements());
if (!mCGFont) { if (!mCGFont) {
::CFRetain(baseFont); ::CFRetain(baseFont);
mCGFont = baseFont; mCGFont = baseFont;