Move SkTypeface to sk_sp.

Committed: https://skia.googlesource.com/skia/+/6296da736fbf40aae881650c239420f64e576c3f
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1933393002

Review-Url: https://codereview.chromium.org/1933393002
diff --git a/tests/FontHostStreamTest.cpp b/tests/FontHostStreamTest.cpp
index 0b43655..ec32d9f 100644
--- a/tests/FontHostStreamTest.cpp
+++ b/tests/FontHostStreamTest.cpp
@@ -69,9 +69,7 @@
         paint.setColor(SK_ColorGRAY);
         paint.setTextSize(SkIntToScalar(30));
 
-        SkTypeface* fTypeface = SkTypeface::CreateFromName("Georgia",
-                                                           SkTypeface::kNormal);
-        SkSafeUnref(paint.setTypeface(fTypeface));
+        paint.setTypeface(SkTypeface::MakeFromName("Georgia", SkTypeface::kNormal));
 
         SkIRect origRect = SkIRect::MakeWH(64, 64);
         SkBitmap origBitmap;
@@ -89,23 +87,18 @@
         drawBG(&origCanvas);
         origCanvas.drawText("A", 1, point.fX, point.fY, paint);
 
-        SkTypeface* origTypeface = paint.getTypeface();
-        SkAutoTUnref<SkTypeface> aur;
-        if (nullptr == origTypeface) {
-            aur.reset(SkTypeface::RefDefault());
-            origTypeface = aur.get();
-        }
-
+        sk_sp<SkTypeface> typeface(SkToBool(paint.getTypeface()) ? sk_ref_sp(paint.getTypeface())
+                                                                 : SkTypeface::MakeDefault());
         int ttcIndex;
-        SkAutoTDelete<SkStreamAsset> fontData(origTypeface->openStream(&ttcIndex));
-        SkTypeface* streamTypeface = SkTypeface::CreateFromStream(fontData.release());
+        SkAutoTDelete<SkStreamAsset> fontData(typeface->openStream(&ttcIndex));
+        sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(fontData.release()));
 
         SkFontDescriptor desc;
         bool isLocalStream = false;
         streamTypeface->getFontDescriptor(&desc, &isLocalStream);
         REPORTER_ASSERT(reporter, isLocalStream);
 
-        SkSafeUnref(paint.setTypeface(streamTypeface));
+        paint.setTypeface(streamTypeface);
         drawBG(&streamCanvas);
         streamCanvas.drawPosText("A", 1, &point, paint);
 
diff --git a/tests/FontHostTest.cpp b/tests/FontHostTest.cpp
index d9a3df4..ebcc4ab 100644
--- a/tests/FontHostTest.cpp
+++ b/tests/FontHostTest.cpp
@@ -31,7 +31,7 @@
 
 // Test that getUnitsPerEm() agrees with a direct lookup in the 'head' table
 // (if that table is available).
-static void test_unitsPerEm(skiatest::Reporter* reporter, SkTypeface* face) {
+static void test_unitsPerEm(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
     int nativeUPEM = face->getUnitsPerEm();
 
     int tableUPEM = -1;
@@ -50,7 +50,7 @@
 
 // Test that countGlyphs() agrees with a direct lookup in the 'maxp' table
 // (if that table is available).
-static void test_countGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
+static void test_countGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
     int nativeGlyphs = face->countGlyphs();
 
     int tableGlyphs = -1;
@@ -86,7 +86,7 @@
 };
 
 // Test that SkPaint::textToGlyphs agrees with SkTypeface::charsToGlyphs.
-static void test_charsToGlyphs(skiatest::Reporter* reporter, SkTypeface* face) {
+static void test_charsToGlyphs(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
     uint16_t paintGlyphIds[256];
     uint16_t faceGlyphIds[256];
 
@@ -154,22 +154,22 @@
 }
 
 static void test_symbolfont(skiatest::Reporter* reporter) {
-    SkAutoTUnref<SkTypeface> typeface(GetResourceAsTypeface("/fonts/SpiderSymbol.ttf"));
-    if (!typeface) {
+    SkUnichar c = 0xf021;
+    uint16_t g;
+    SkPaint paint;
+    paint.setTypeface(MakeResourceAsTypeface("/fonts/SpiderSymbol.ttf"));
+    paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
+    paint.textToGlyphs(&c, 4, &g);
+
+    if (!paint.getTypeface()) {
         SkDebugf("Skipping FontHostTest::test_symbolfont\n");
         return;
     }
 
-    SkUnichar c = 0xf021;
-    uint16_t g;
-    SkPaint paint;
-    paint.setTypeface(typeface);
-    paint.setTextEncoding(SkPaint::kUTF32_TextEncoding);
-    paint.textToGlyphs(&c, 4, &g);
     REPORTER_ASSERT(reporter, g == 3);
 }
 
-static void test_tables(skiatest::Reporter* reporter, SkTypeface* face) {
+static void test_tables(skiatest::Reporter* reporter, const sk_sp<SkTypeface>& face) {
     if (false) { // avoid bit rot, suppress warning
         SkFontID fontID = face->uniqueID();
         REPORTER_ASSERT(reporter, fontID);
@@ -223,7 +223,7 @@
     };
 
     for (size_t i = 0; i < SK_ARRAY_COUNT(gNames); ++i) {
-        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(gNames[i], SkTypeface::kNormal));
+        sk_sp<SkTypeface> face(SkTypeface::MakeFromName(gNames[i], SkTypeface::kNormal));
         if (face) {
 #ifdef DUMP_TABLES
             SkDebugf("%s\n", gNames[i]);
@@ -277,8 +277,7 @@
     char txt[] = "long.text.with.lots.of.dots.";
 
     for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
-        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
-        paint.setTypeface(face);
+        paint.setTypeface(SkTypeface::MakeFromName(faces[i], SkTypeface::kNormal));
 
         for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
             paint.setHinting(settings[j].hinting);
diff --git a/tests/FontMgrTest.cpp b/tests/FontMgrTest.cpp
index 92dc18b..414631c 100644
--- a/tests/FontMgrTest.cpp
+++ b/tests/FontMgrTest.cpp
@@ -19,7 +19,7 @@
 
 static void test_font(skiatest::Reporter* reporter) {
     uint32_t flags = 0;
-    SkAutoTUnref<SkFont> font(SkFont::Create(nullptr, 24, SkFont::kA8_MaskType, flags));
+    sk_sp<SkFont> font(SkFont::Make(nullptr, 24, SkFont::kA8_MaskType, flags));
 
     REPORTER_ASSERT(reporter, font->getTypeface());
     REPORTER_ASSERT(reporter, 24 == font->getSize());
@@ -39,7 +39,7 @@
     REPORTER_ASSERT(reporter, glyphs[0] != glyphs[1]); // 'h' != 'e'
     REPORTER_ASSERT(reporter, glyphs[2] == glyphs[3]); // 'l' == 'l'
 
-    SkAutoTUnref<SkFont> newFont(font->cloneWithSize(36));
+    sk_sp<SkFont> newFont(font->makeWithSize(36));
     REPORTER_ASSERT(reporter, newFont.get());
     REPORTER_ASSERT(reporter, font->getTypeface() == newFont->getTypeface());
     REPORTER_ASSERT(reporter, 36 == newFont->getSize());   // double check we haven't changed
@@ -47,7 +47,7 @@
 
     SkPaint paint;
     paint.setTextSize(18);
-    font.reset(SkFont::Testing_CreateFromPaint(paint));
+    font = SkFont::Testing_CreateFromPaint(paint);
     REPORTER_ASSERT(reporter, font.get());
     REPORTER_ASSERT(reporter, font->getSize() == paint.getTextSize());
     REPORTER_ASSERT(reporter, SkFont::kBW_MaskType == font->getMaskType());
@@ -64,14 +64,12 @@
     };
 
     for (size_t i = 0; i < SK_ARRAY_COUNT(inNames); ++i) {
-        SkAutoTUnref<SkTypeface> first(SkTypeface::CreateFromName(inNames[i],
-                                                          SkTypeface::kNormal));
+        sk_sp<SkTypeface> first(SkTypeface::MakeFromName(inNames[i], SkTypeface::kNormal));
         if (nullptr == first.get()) {
             continue;
         }
         for (int j = 0; j < 10; ++j) {
-            SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(inNames[i],
-                                                         SkTypeface::kNormal));
+            sk_sp<SkTypeface> face(SkTypeface::MakeFromName(inNames[i], SkTypeface::kNormal));
     #if 0
             SkString name;
             face->getFamilyName(&name);
diff --git a/tests/FontObjTest.cpp b/tests/FontObjTest.cpp
index 9d18ce6..66c8bd5 100644
--- a/tests/FontObjTest.cpp
+++ b/tests/FontObjTest.cpp
@@ -23,7 +23,7 @@
 }
 
 static void test_cachedfont(skiatest::Reporter* reporter, const SkPaint& paint) {
-    SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
+    sk_sp<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
 
     // Currently SkFont resolves null into the default, so only test if paint's is not null
     if (paint.getTypeface()) {
@@ -78,8 +78,7 @@
     char txt[] = "long.text.with.lots.of.dots.";
 
     for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
-        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
-        paint.setTypeface(face);
+        paint.setTypeface(SkTypeface::MakeFromName(faces[i], SkTypeface::kNormal));
 
         for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
             paint.setHinting(settings[j].hinting);
@@ -103,7 +102,7 @@
 
                 REPORTER_ASSERT(reporter, width1 == width2);
 
-                SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
+                sk_sp<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
                 SkScalar font_width1 = font->measureText(txt, strlen(txt), kUTF8_SkTextEncoding);
                 // measureText not yet implemented...
                 REPORTER_ASSERT(reporter, font_width1 == -1);
diff --git a/tests/PDFPrimitivesTest.cpp b/tests/PDFPrimitivesTest.cpp
index 58dd773..07ddabc 100644
--- a/tests/PDFPrimitivesTest.cpp
+++ b/tests/PDFPrimitivesTest.cpp
@@ -430,7 +430,7 @@
     SkPDFCanon canon;
 
     const char resource[] = "fonts/Roboto2-Regular_NoEmbed.ttf";
-    sk_sp<SkTypeface> noEmbedTypeface(GetResourceAsTypeface(resource));
+    sk_sp<SkTypeface> noEmbedTypeface(MakeResourceAsTypeface(resource));
     if (noEmbedTypeface) {
         REPORTER_ASSERT(reporter,
                         !SkPDFFont::CanEmbedTypeface(noEmbedTypeface.get(), &canon));
diff --git a/tests/PaintTest.cpp b/tests/PaintTest.cpp
index bd00adb..f507467 100644
--- a/tests/PaintTest.cpp
+++ b/tests/PaintTest.cpp
@@ -80,7 +80,7 @@
 
     SkRandom rand;
     SkPaint paint;
-    paint.setTypeface(SkTypeface::RefDefault())->unref();
+    paint.setTypeface(SkTypeface::MakeDefault());
     SkTypeface* face = paint.getTypeface();
 
     for (int i = 0; i < 1000; ++i) {
@@ -333,7 +333,7 @@
     REPORTER_ASSERT(r, paint.getHash() == defaultHash);
 
     // SkTypeface is the first field we hash, so test it specially.
-    paint.setTypeface(SkTypeface::RefDefault())->unref();
+    paint.setTypeface(SkTypeface::MakeDefault());
     REPORTER_ASSERT(r, paint.getHash() != defaultHash);
     paint.setTypeface(nullptr);
     REPORTER_ASSERT(r, paint.getHash() == defaultHash);
diff --git a/tests/PictureTest.cpp b/tests/PictureTest.cpp
index 9e36d84..832a80f 100644
--- a/tests/PictureTest.cpp
+++ b/tests/PictureTest.cpp
@@ -1177,7 +1177,7 @@
     SkPictureRecorder recorder;
     SkCanvas* canvas = recorder.beginRecording(10, 10);
     SkPaint paint;
-    paint.setTypeface(SkTypeface::CreateFromName("Arial", SkTypeface::kItalic));
+    paint.setTypeface(SkTypeface::MakeFromName("Arial", SkTypeface::kItalic));
     canvas->drawText("Q", 1, 0, 10, paint);
     sk_sp<SkPicture> picture(recorder.finishRecordingAsPicture());
     REPORTER_ASSERT(reporter, picture->hasText());
diff --git a/tests/SerializationTest.cpp b/tests/SerializationTest.cpp
index 4750bbe..9e9b221 100644
--- a/tests/SerializationTest.cpp
+++ b/tests/SerializationTest.cpp
@@ -322,14 +322,14 @@
     }
     REPORTER_ASSERT(reporter, 0 == pixelErrors);
 }
-static void serialize_and_compare_typeface(SkTypeface* typeface, const char* text,
+static void serialize_and_compare_typeface(sk_sp<SkTypeface> typeface, const char* text,
                                            skiatest::Reporter* reporter)
 {
     // Create a paint with the typeface.
     SkPaint paint;
     paint.setColor(SK_ColorGRAY);
     paint.setTextSize(SkIntToScalar(30));
-    paint.setTypeface(typeface);
+    paint.setTypeface(std::move(typeface));
 
     // Paint some text.
     SkPictureRecorder recorder;
@@ -357,11 +357,11 @@
     {
         // Load typeface from file to test CreateFromFile with index.
         SkString filename = GetResourcePath("/fonts/test.ttc");
-        SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFile(filename.c_str(), 1));
+        sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFile(filename.c_str(), 1));
         if (!typeface) {
             INFOF(reporter, "Could not run fontstream test because test.ttc not found.");
         } else {
-            serialize_and_compare_typeface(typeface, "A!", reporter);
+            serialize_and_compare_typeface(std::move(typeface), "A!", reporter);
         }
     }
 
@@ -372,12 +372,12 @@
             INFOF(reporter, "Could not run fontstream test because Distortable.ttf not found.");
         } else {
             SkFixed axis = SK_FixedSqrt2;
-            SkAutoTUnref<SkTypeface> typeface(SkTypeface::CreateFromFontData(
+            sk_sp<SkTypeface> typeface(SkTypeface::MakeFromFontData(
                 new SkFontData(distortable.release(), 0, &axis, 1)));
             if (!typeface) {
                 INFOF(reporter, "Could not run fontstream test because Distortable.ttf not created.");
             } else {
-                serialize_and_compare_typeface(typeface, "abc", reporter);
+                serialize_and_compare_typeface(std::move(typeface), "abc", reporter);
             }
         }
     }
diff --git a/tests/TextBlobCacheTest.cpp b/tests/TextBlobCacheTest.cpp
index 23f45a4..cbc6b99 100644
--- a/tests/TextBlobCacheTest.cpp
+++ b/tests/TextBlobCacheTest.cpp
@@ -99,12 +99,11 @@
             set->getStyle(j, &fs, nullptr);
 
             // We use a typeface which randomy returns unexpected mask formats to fuzz
-            SkAutoTUnref<SkTypeface> orig(set->createTypeface(j));
+            sk_sp<SkTypeface> orig(set->createTypeface(j));
             if (normal) {
                 paint.setTypeface(orig);
             } else {
-                SkAutoTUnref<SkTypeface> typeface(new SkRandomTypeface(orig, paint, true));
-                paint.setTypeface(typeface);
+                paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true));
             }
 
             SkTextBlobBuilder builder;
diff --git a/tests/TextBlobTest.cpp b/tests/TextBlobTest.cpp
index 923669e..6107024 100644
--- a/tests/TextBlobTest.cpp
+++ b/tests/TextBlobTest.cpp
@@ -178,12 +178,10 @@
         SkPaint font;
         font.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
 
-        SkAutoTUnref<SkTypeface> typeface(SkTypeface::RefDefault());
-
         // Kitchen sink font.
         font.setTextSize(42);
         font.setTextScaleX(4.2f);
-        font.setTypeface(typeface);
+        font.setTypeface(SkTypeface::MakeDefault());
         font.setTextSkewX(0.42f);
         font.setTextAlign(SkPaint::kCenter_Align);
         font.setHinting(SkPaint::kFull_Hinting);
diff --git a/tests/TypefaceTest.cpp b/tests/TypefaceTest.cpp
index 950449d..6a606d4 100644
--- a/tests/TypefaceTest.cpp
+++ b/tests/TypefaceTest.cpp
@@ -12,8 +12,8 @@
 
 DEF_TEST(Typeface, reporter) {
 
-    SkAutoTUnref<SkTypeface> t1(SkTypeface::CreateFromName(nullptr, SkTypeface::kNormal));
-    SkAutoTUnref<SkTypeface> t2(SkTypeface::RefDefault(SkTypeface::kNormal));
+    sk_sp<SkTypeface> t1(SkTypeface::MakeFromName(nullptr, SkTypeface::kNormal));
+    sk_sp<SkTypeface> t2(SkTypeface::MakeDefault(SkTypeface::kNormal));
 
     REPORTER_ASSERT(reporter, SkTypeface::Equal(t1.get(), t2.get()));
     REPORTER_ASSERT(reporter, SkTypeface::Equal(0, t1.get()));
@@ -22,8 +22,8 @@
     REPORTER_ASSERT(reporter, SkTypeface::Equal(t2.get(), 0));
 
 #ifdef SK_BUILD_FOR_ANDROID
-    SkAutoTUnref<SkTypeface> t3(SkTypeface::CreateFromName("non-existent-font", SkTypeface::kNormal));
-    REPORTER_ASSERT(reporter, nullptr == t3.get());
+    sk_sp<SkTypeface> t3(SkTypeface::MakeFromName("non-existent-font", SkTypeface::kNormal));
+    REPORTER_ASSERT(reporter, nullptr == t3);
 #endif
 }