Deliberately call typefaceproc only once per face in pictures

Before this CL, the new test would write out 2 copies of each typeface,
since each one is referenced twice in the picture.

Bug: skia:
Change-Id: I6ebfe6b5ea0bb0cca2869ef0cccad21a51e48411
Reviewed-on: https://skia-review.googlesource.com/151667
Auto-Submit: Mike Reed <reed@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Mike Reed <reed@google.com>
diff --git a/tests/SerialProcsTest.cpp b/tests/SerialProcsTest.cpp
index fd82079..01ae6b7 100644
--- a/tests/SerialProcsTest.cpp
+++ b/tests/SerialProcsTest.cpp
@@ -178,3 +178,46 @@
     test_pictures(reporter, p0, 1, true);
 }
 
+static sk_sp<SkPicture> make_picture(sk_sp<SkTypeface> tf0, sk_sp<SkTypeface> tf1) {
+    SkPictureRecorder rec;
+    SkCanvas* canvas = rec.beginRecording(100, 100);
+    SkPaint paint;
+    paint.setTypeface(tf0); canvas->drawText("hello", 5, 0, 0, paint);
+    paint.setTypeface(tf1); canvas->drawText("hello", 5, 0, 0, paint);
+    paint.setTypeface(tf0); canvas->drawText("hello", 5, 0, 0, paint);
+    paint.setTypeface(tf1); canvas->drawText("hello", 5, 0, 0, paint);
+    return rec.finishRecordingAsPicture();
+}
+
+DEF_TEST(serial_typeface, reporter) {
+    auto tf0 = MakeResourceAsTypeface("fonts/hintgasp.ttf");
+    auto tf1 = MakeResourceAsTypeface("fonts/Roboto2-Regular_NoEmbed.ttf");
+    if (!tf0 || !tf1 || tf0.get() == tf1.get()) {
+        return; // need two different typefaces for this test to make sense.
+    }
+
+#ifdef SK_DEBUG
+    REPORTER_ASSERT(reporter, tf0->getRefCnt() == 1);
+    REPORTER_ASSERT(reporter, tf1->getRefCnt() == 1);
+#endif
+    auto pic = make_picture(tf0, tf1);
+#ifdef SK_DEBUG
+    // picture should add 2 more references to each typeface
+    REPORTER_ASSERT(reporter, tf0->getRefCnt() == 3);
+    REPORTER_ASSERT(reporter, tf1->getRefCnt() == 3);
+#endif
+
+    int counter = 0;
+    SkSerialProcs procs;
+    procs.fTypefaceProc = [](SkTypeface* tf, void* ctx) -> sk_sp<SkData> {
+        *(int*)ctx += 1;
+        return nullptr;
+    };
+    procs.fTypefaceCtx = &counter;
+    auto data = pic->serialize(&procs);
+
+    // The picture has 2 references to each typeface, but we want the serialized picture to
+    // only have written the data 1 time per typeface.
+    REPORTER_ASSERT(reporter, counter == 2);
+}
+