Add GetTypefaceOrDefault to SkPaintPriv

Remove most uses of GetDefaultTypeface. SkTypeface has
fewer friends.

BUG=skia:7515

Change-Id: Iedec5b39b9ef8c638772be4971075491b59b740b
Reviewed-on: https://skia-review.googlesource.com/112300
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Herb Derby <herb@google.com>
diff --git a/include/core/SkTypeface.h b/include/core/SkTypeface.h
index fb2c64a..b55ac4e 100644
--- a/include/core/SkTypeface.h
+++ b/include/core/SkTypeface.h
@@ -309,6 +309,7 @@
     virtual SkScalerContext* onCreateScalerContext(const SkScalerContextEffects&,
                                                    const SkDescriptor*) const = 0;
     virtual void onFilterRec(SkScalerContextRec*) const = 0;
+    friend class SkScalerContext;  // onFilterRec
 
     //  Subclasses *must* override this method to work with the PDF backend.
     virtual std::unique_ptr<SkAdvancedTypefaceMetrics> onGetAdvancedMetrics() const;
@@ -364,12 +365,7 @@
     };
     static SkFontStyle FromOldStyle(Style oldStyle);
     static SkTypeface* GetDefaultTypeface(Style style = SkTypeface::kNormal);
-    static SkTypeface* NormalizeTypeface(SkTypeface* typeface) {
-        return typeface != nullptr ? typeface : SkTypeface::GetDefaultTypeface();
-    }
-    friend class SkGlyphCache;     // GetDefaultTypeface
-    friend class SkPaint;          // GetDefaultTypeface
-    friend class SkScalerContext;  // GetDefaultTypeface
+    friend class SkPaintPriv;      // GetDefaultTypeface
 
 private:
     SkFontID            fUniqueID;
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp
index 087fb52..9bb4360 100644
--- a/src/core/SkGlyphCache.cpp
+++ b/src/core/SkGlyphCache.cpp
@@ -9,6 +9,7 @@
 #include "SkGlyphCache.h"
 #include "SkGraphics.h"
 #include "SkOnce.h"
+#include "SkPaintPriv.h"
 #include "SkPath.h"
 #include "SkTemplates.h"
 #include "SkTraceMemoryDump.h"
@@ -788,9 +789,7 @@
 SkGlyphCache* SkGlyphCache::DetachCache(
     SkTypeface* typeface, const SkScalerContextEffects& effects, const SkDescriptor* desc)
 {
-
-    auto cache = FindOrCreateStrikeExclusive(
-        *desc, effects, *SkTypeface::NormalizeTypeface(typeface));
+    auto cache = FindOrCreateStrikeExclusive(*desc, effects, *typeface);
     return cache.release();
 }
 
@@ -804,5 +803,5 @@
     auto desc = SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
         paint, surfaceProps, scalerContextFlags, deviceMatrix, &ad, &effects);
 
-    return SkGlyphCache::DetachCache(paint.getTypeface(), effects, desc);
+    return SkGlyphCache::DetachCache(SkPaintPriv::GetTypefaceOrDefault(paint), effects, desc);
 }
diff --git a/src/core/SkPaint.cpp b/src/core/SkPaint.cpp
index 76f50ce..e368071 100644
--- a/src/core/SkPaint.cpp
+++ b/src/core/SkPaint.cpp
@@ -928,7 +928,7 @@
         paint, nullptr, SkScalerContextFlags::kNone, zoomPtr, &ad, &effects);
 
     {
-        auto typeface = SkTypeface::NormalizeTypeface(paint.getTypeface());
+        auto typeface = SkPaintPriv::GetTypefaceOrDefault(paint);
         auto cache = SkGlyphCache::FindOrCreateStrikeExclusive(*desc, effects, *typeface);
         *metrics = cache->getFontMetrics();
     }
@@ -1201,10 +1201,7 @@
     m.setScale(fTextSize * fTextScaleX, fTextSize);
     m.postSkew(fTextSkewX, 0);
 
-    SkTypeface* typeface = this->getTypeface();
-    if (nullptr == typeface) {
-        typeface = SkTypeface::GetDefaultTypeface();
-    }
+    SkTypeface* typeface = SkPaintPriv::GetTypefaceOrDefault(*this);
 
     SkRect bounds;
     m.mapRect(&bounds, typeface->getBounds());
@@ -1386,13 +1383,10 @@
     it if there are not tricky elements like shaders, etc.
  */
 void SkPaint::flatten(SkWriteBuffer& buffer) const {
-    SkTypeface* tf = this->getTypeface();
-    if (!tf) {
-        // We force recording our typeface, even if its "default" since the receiver process
-        // may have a different notion of default.
-        tf = SkTypeface::GetDefaultTypeface();
-        SkASSERT(tf);
-    }
+    // We force recording our typeface, even if its "default" since the receiver process
+    // may have a different notion of default.
+    SkTypeface* tf = SkPaintPriv::GetTypefaceOrDefault(*this);
+    SkASSERT(tf);
 
     uint8_t flatFlags = kHasTypeface_FlatFlag;
 
diff --git a/src/core/SkPaintPriv.h b/src/core/SkPaintPriv.h
index 08f80bf..c60c232 100644
--- a/src/core/SkPaintPriv.h
+++ b/src/core/SkPaintPriv.h
@@ -9,8 +9,9 @@
 #define SkPaintPriv_DEFINED
 
 #include "SkImageInfo.h"
-#include "SkPaint.h"
 #include "SkMatrix.h"
+#include "SkPaint.h"
+#include "SkTypeface.h"
 
 class SkBitmap;
 class SkImage;
@@ -67,6 +68,14 @@
 
     // returns 0 if buffer is invalid for specified encoding
     static int ValidCountText(const void* text, size_t length, SkPaint::TextEncoding);
+
+    static SkTypeface* GetTypefaceOrDefault(const SkPaint& paint) {
+        return paint.getTypeface() ? paint.getTypeface() : SkTypeface::GetDefaultTypeface();
+    }
+
+    static sk_sp<SkTypeface> RefTypefaceOrDefault(const SkPaint& paint) {
+        return paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault();
+    }
 };
 
 #endif
diff --git a/src/core/SkScalerContext.cpp b/src/core/SkScalerContext.cpp
index a821ec2..e0bded0 100644
--- a/src/core/SkScalerContext.cpp
+++ b/src/core/SkScalerContext.cpp
@@ -898,10 +898,8 @@
                                         SkScalerContextEffects* effects) {
     SkASSERT(deviceMatrix == nullptr || !deviceMatrix->hasPerspective());
 
-    SkTypeface* typeface = paint.getTypeface();
-    if (nullptr == typeface) {
-        typeface = SkTypeface::GetDefaultTypeface();
-    }
+    SkTypeface* typeface = SkPaintPriv::GetTypefaceOrDefault(paint);
+
     rec->fFontID = typeface->uniqueID();
     rec->fTextSize = paint.getTextSize();
     rec->fPreScaleX = paint.getTextScaleX();
diff --git a/src/core/SkTextBlob.cpp b/src/core/SkTextBlob.cpp
index 6d89e6f..f686f29 100644
--- a/src/core/SkTextBlob.cpp
+++ b/src/core/SkTextBlob.cpp
@@ -7,6 +7,7 @@
 
 #include "SkTextBlobRunIterator.h"
 
+#include "SkPaintPriv.h"
 #include "SkReadBuffer.h"
 #include "SkSafeMath.h"
 #include "SkTypeface.h"
@@ -26,7 +27,7 @@
     RunFont(const SkPaint& paint)
         : fSize(paint.getTextSize())
         , fScaleX(paint.getTextScaleX())
-        , fTypeface(SkSafeRef(paint.getTypeface()))
+        , fTypeface(SkPaintPriv::RefTypefaceOrDefault(paint))
         , fSkewX(paint.getTextSkewX())
         , fAlign(paint.getTextAlign())
         , fHinting(paint.getHinting())
diff --git a/src/gpu/text/GrAtlasTextBlob.cpp b/src/gpu/text/GrAtlasTextBlob.cpp
index bd3d768..b4404c3 100644
--- a/src/gpu/text/GrAtlasTextBlob.cpp
+++ b/src/gpu/text/GrAtlasTextBlob.cpp
@@ -14,6 +14,7 @@
 #include "SkDrawFilter.h"
 #include "SkGlyphCache.h"
 #include "SkMaskFilterBase.h"
+#include "SkPaintPriv.h"
 #include "SkTextBlobRunIterator.h"
 #include "SkTextToPathIter.h"
 #include "ops/GrAtlasTextOp.h"
@@ -67,7 +68,7 @@
     SkScalerContextEffects effects;
     SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
         skPaint, &props, scalerContextFlags, viewMatrix, desc, &effects);
-    run->fTypeface.reset(SkSafeRef(skPaint.getTypeface()));
+    run->fTypeface = SkPaintPriv::RefTypefaceOrDefault(skPaint);
     run->fPathEffect = sk_ref_sp(effects.fPathEffect);
     run->fMaskFilter = sk_ref_sp(effects.fMaskFilter);
     return SkGlyphCache::DetachCache(run->fTypeface.get(), effects, desc->getDesc());
diff --git a/src/gpu/text/GrAtlasTextContext.cpp b/src/gpu/text/GrAtlasTextContext.cpp
index 6ebd02c..ad708d7 100644
--- a/src/gpu/text/GrAtlasTextContext.cpp
+++ b/src/gpu/text/GrAtlasTextContext.cpp
@@ -17,6 +17,7 @@
 #include "SkGraphics.h"
 #include "SkMakeUnique.h"
 #include "SkMaskFilterBase.h"
+#include "SkPaintPriv.h"
 #include "SkTextMapStateProc.h"
 
 #include "ops/GrMeshDrawOp.h"
@@ -692,8 +693,9 @@
     // passed-in scaler context flags. (It's only used when we fall-back to bitmap text).
     SkScalerContext::CreateDescriptorAndEffectsUsingPaint(
         skPaint, &props, SkScalerContextFlags::kNone, nullptr, &desc, &effects);
+    auto typeface = SkPaintPriv::GetTypefaceOrDefault(skPaint);
     SkGlyphCache* origPaintCache =
-            SkGlyphCache::DetachCache(skPaint.getTypeface(), effects, desc.getDesc());
+            SkGlyphCache::DetachCache(typeface, effects, desc.getDesc());
 
     SkTArray<SkScalar> positions;
 
diff --git a/src/svg/SkSVGDevice.cpp b/src/svg/SkSVGDevice.cpp
index 6bb971d..7206709 100644
--- a/src/svg/SkSVGDevice.cpp
+++ b/src/svg/SkSVGDevice.cpp
@@ -17,6 +17,7 @@
 #include "SkDraw.h"
 #include "SkImageEncoder.h"
 #include "SkPaint.h"
+#include "SkPaintPriv.h"
 #include "SkParsePath.h"
 #include "SkShader.h"
 #include "SkStream.h"
@@ -544,7 +545,7 @@
 
     SkString familyName;
     SkTHashSet<SkString> familySet;
-    sk_sp<SkTypeface> tface(paint.getTypeface() ? paint.refTypeface() : SkTypeface::MakeDefault());
+    sk_sp<SkTypeface> tface = SkPaintPriv::RefTypefaceOrDefault(paint);
 
     SkASSERT(tface);
     SkFontStyle style = tface->fontStyle();
diff --git a/tests/FontHostStreamTest.cpp b/tests/FontHostStreamTest.cpp
index e97ea0d..3a5f528 100644
--- a/tests/FontHostStreamTest.cpp
+++ b/tests/FontHostStreamTest.cpp
@@ -11,6 +11,7 @@
 #include "SkFontDescriptor.h"
 #include "SkGraphics.h"
 #include "SkPaint.h"
+#include "SkPaintPriv.h"
 #include "SkPoint.h"
 #include "SkRect.h"
 #include "SkStream.h"
@@ -84,8 +85,7 @@
         drawBG(&origCanvas);
         origCanvas.drawString("A", point.fX, point.fY, paint);
 
-        sk_sp<SkTypeface> typeface(paint.getTypeface() ? paint.refTypeface()
-                                                       : SkTypeface::MakeDefault());
+        sk_sp<SkTypeface> typeface = SkPaintPriv::RefTypefaceOrDefault(paint);
         int ttcIndex;
         std::unique_ptr<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
         if (!fontData) {