Bug 1485712 - set SkTypeface atomically in ScaledFonts. r=rhunt

This commit is contained in:
Lee Salzman 2018-08-23 14:58:21 -04:00
parent a5cee657d8
commit 5285c599ca
12 changed files with 62 additions and 63 deletions

View file

@ -54,7 +54,7 @@ ScaledFont::GetDefaultAAMode()
ScaledFontBase::~ScaledFontBase() ScaledFontBase::~ScaledFontBase()
{ {
#ifdef USE_SKIA #ifdef USE_SKIA
SkSafeUnref(mTypeface); SkSafeUnref<SkTypeface>(mTypeface);
#endif #endif
#ifdef USE_CAIRO_SCALED_FONT #ifdef USE_CAIRO_SCALED_FONT
cairo_scaled_font_destroy(mScaledFont); cairo_scaled_font_destroy(mScaledFont);
@ -64,16 +64,30 @@ ScaledFontBase::~ScaledFontBase()
ScaledFontBase::ScaledFontBase(const RefPtr<UnscaledFont>& aUnscaledFont, ScaledFontBase::ScaledFontBase(const RefPtr<UnscaledFont>& aUnscaledFont,
Float aSize) Float aSize)
: ScaledFont(aUnscaledFont) : ScaledFont(aUnscaledFont)
, mSize(aSize)
{
#ifdef USE_SKIA #ifdef USE_SKIA
mTypeface = nullptr; , mTypeface(nullptr)
#endif #endif
#ifdef USE_CAIRO_SCALED_FONT #ifdef USE_CAIRO_SCALED_FONT
mScaledFont = nullptr; , mScaledFont(nullptr)
#endif #endif
, mSize(aSize)
{
} }
#ifdef USE_SKIA
SkTypeface*
ScaledFontBase::GetSkTypeface()
{
if (!mTypeface) {
SkTypeface* typeface = CreateSkTypeface();
if (!mTypeface.compareExchange(nullptr, typeface)) {
SkSafeUnref(typeface);
}
}
return mTypeface;
}
#endif
#ifdef USE_CAIRO_SCALED_FONT #ifdef USE_CAIRO_SCALED_FONT
bool bool
ScaledFontBase::PopulateCairoScaledFont() ScaledFontBase::PopulateCairoScaledFont()

View file

@ -42,7 +42,7 @@ public:
virtual Float GetSize() const override { return mSize; } virtual Float GetSize() const override { return mSize; }
#ifdef USE_SKIA #ifdef USE_SKIA
virtual SkTypeface* GetSkTypeface() { return mTypeface; } SkTypeface* GetSkTypeface();
#endif #endif
#ifdef USE_CAIRO_SCALED_FONT #ifdef USE_CAIRO_SCALED_FONT
@ -54,7 +54,8 @@ public:
protected: protected:
friend class DrawTargetSkia; friend class DrawTargetSkia;
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* mTypeface; Atomic<SkTypeface*> mTypeface;
virtual SkTypeface* CreateSkTypeface() { return nullptr; }
SkPath GetSkiaPathForGlyphs(const GlyphBuffer &aBuffer); SkPath GetSkiaPathForGlyphs(const GlyphBuffer &aBuffer);
#endif #endif
#ifdef USE_CAIRO_SCALED_FONT #ifdef USE_CAIRO_SCALED_FONT

View file

@ -172,29 +172,26 @@ ScaledFontDWrite::GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* SkTypeface*
ScaledFontDWrite::GetSkTypeface() ScaledFontDWrite::CreateSkTypeface()
{ {
if (!mTypeface) { RefPtr<IDWriteFactory> factory = Factory::GetDWriteFactory();
RefPtr<IDWriteFactory> factory = Factory::GetDWriteFactory(); if (!factory) {
if (!factory) { return nullptr;
return nullptr;
}
Float gamma = mGamma;
// Skia doesn't support a gamma value outside of 0-4, so default to 2.2
if (gamma < 0.0f || gamma > 4.0f) {
gamma = 2.2f;
}
Float contrast = mContrast;
// Skia doesn't support a contrast value outside of 0-1, so default to 1.0
if (contrast < 0.0f || contrast > 1.0f) {
contrast = 1.0f;
}
mTypeface = SkCreateTypefaceFromDWriteFont(factory, mFontFace, mStyle, mForceGDIMode, gamma, contrast);
} }
return mTypeface;
Float gamma = mGamma;
// Skia doesn't support a gamma value outside of 0-4, so default to 2.2
if (gamma < 0.0f || gamma > 4.0f) {
gamma = 2.2f;
}
Float contrast = mContrast;
// Skia doesn't support a contrast value outside of 0-1, so default to 1.0
if (contrast < 0.0f || contrast > 1.0f) {
contrast = 1.0f;
}
return SkCreateTypefaceFromDWriteFont(factory, mFontFace, mStyle, mForceGDIMode, gamma, contrast);
} }
#endif #endif

View file

@ -67,7 +67,7 @@ public:
bool ForceGDIMode() { return mForceGDIMode; } bool ForceGDIMode() { return mForceGDIMode; }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* GetSkTypeface() override; SkTypeface* CreateSkTypeface() override;
SkFontStyle mStyle; SkFontStyle mStyle;
#endif #endif

View file

@ -43,13 +43,9 @@ ScaledFontFontconfig::~ScaledFontFontconfig()
} }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* ScaledFontFontconfig::GetSkTypeface() SkTypeface* ScaledFontFontconfig::CreateSkTypeface()
{ {
if (!mTypeface) { return SkCreateTypefaceFromCairoFTFontWithFontconfig(mScaledFont, mPattern);
mTypeface = SkCreateTypefaceFromCairoFTFontWithFontconfig(mScaledFont, mPattern);
}
return mTypeface;
} }
#endif #endif

View file

@ -28,7 +28,7 @@ public:
FontType GetType() const override { return FontType::FONTCONFIG; } FontType GetType() const override { return FontType::FONTCONFIG; }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* GetSkTypeface() override; SkTypeface* CreateSkTypeface() override;
#endif #endif
bool CanSerialize() override { return true; } bool CanSerialize() override { return true; }

View file

@ -35,13 +35,9 @@ ScaledFontFreeType::ScaledFontFreeType(cairo_scaled_font_t* aScaledFont,
} }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* ScaledFontFreeType::GetSkTypeface() SkTypeface* ScaledFontFreeType::CreateSkTypeface()
{ {
if (!mTypeface) { return SkCreateTypefaceFromCairoFTFont(mScaledFont);
mTypeface = SkCreateTypefaceFromCairoFTFont(mScaledFont);
}
return mTypeface;
} }
#endif #endif

View file

@ -27,7 +27,7 @@ public:
FontType GetType() const override { return FontType::FREETYPE; } FontType GetType() const override { return FontType::FREETYPE; }
#ifdef USE_SKIA #ifdef USE_SKIA
virtual SkTypeface* GetSkTypeface() override; virtual SkTypeface* CreateSkTypeface() override;
#endif #endif
bool CanSerialize() override { return true; } bool CanSerialize() override { return true; }

View file

@ -177,21 +177,19 @@ ScaledFontMac::~ScaledFontMac()
} }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* ScaledFontMac::GetSkTypeface() SkTypeface* ScaledFontMac::CreateSkTypeface()
{ {
if (!mTypeface) { if (mCTFont) {
if (mCTFont) { return SkCreateTypefaceFromCTFont(mCTFont);
mTypeface = SkCreateTypefaceFromCTFont(mCTFont); } else {
} else { auto unscaledMac = static_cast<UnscaledFontMac*>(GetUnscaledFont().get());
auto unscaledMac = static_cast<UnscaledFontMac*>(GetUnscaledFont().get()); bool dataFont = unscaledMac->IsDataFont();
bool dataFont = unscaledMac->IsDataFont(); CTFontRef fontFace =
CTFontRef fontFace = CreateCTFontFromCGFontWithVariations(mFont, mSize, !dataFont);
CreateCTFontFromCGFontWithVariations(mFont, mSize, !dataFont); SkTypeface* typeface = SkCreateTypefaceFromCTFont(fontFace);
mTypeface = SkCreateTypefaceFromCTFont(fontFace); CFRelease(fontFace);
CFRelease(fontFace); return typeface;
}
} }
return mTypeface;
} }
#endif #endif

View file

@ -34,7 +34,7 @@ public:
FontType GetType() const override { return FontType::MAC; } FontType GetType() const override { return FontType::MAC; }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* GetSkTypeface() override; SkTypeface* CreateSkTypeface() override;
#endif #endif
already_AddRefed<Path> GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget) override; already_AddRefed<Path> GetPathForGlyphs(const GlyphBuffer &aBuffer, const DrawTarget *aTarget) override;

View file

@ -122,12 +122,9 @@ ScaledFontWin::GetDefaultAAMode()
} }
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* ScaledFontWin::GetSkTypeface() SkTypeface* ScaledFontWin::CreateSkTypeface()
{ {
if (!mTypeface) { return SkCreateTypefaceFromLOGFONT(mLogFont);
mTypeface = SkCreateTypefaceFromLOGFONT(mLogFont);
}
return mTypeface;
} }
#endif #endif

View file

@ -28,7 +28,7 @@ public:
AntialiasMode GetDefaultAAMode() override; AntialiasMode GetDefaultAAMode() override;
#ifdef USE_SKIA #ifdef USE_SKIA
SkTypeface* GetSkTypeface() override; SkTypeface* CreateSkTypeface() override;
#endif #endif
protected: protected: