[skottie] Lazily-initialized default font manager

Only initialize a default font manager when needed.

Change-Id: I3473804d426f1f4981f3727d3539303fb670cdd1
Reviewed-on: https://skia-review.googlesource.com/150840
Reviewed-by: Ben Wagner <bungeman@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
diff --git a/modules/skottie/src/Skottie.cpp b/modules/skottie/src/Skottie.cpp
index e7f3dda..168aea7 100644
--- a/modules/skottie/src/Skottie.cpp
+++ b/modules/skottie/src/Skottie.cpp
@@ -136,7 +136,7 @@
                                    Animation::Builder::Stats* stats,
                                    float duration, float framerate)
     : fResourceProvider(std::move(rp))
-    , fFontMgr(std::move(fontmgr))
+    , fLazyFontMgr(std::move(fontmgr))
     , fStats(stats)
     , fDuration(duration)
     , fFrameRate(framerate) {}
@@ -200,8 +200,6 @@
     };
     auto resolvedProvider = fResourceProvider
             ? fResourceProvider : sk_make_sp<NullResourceProvider>();
-    auto resolvedFontMgr  = fFontMgr
-            ? fFontMgr : SkFontMgr::RefDefault();
 
     memset(&fStats, 0, sizeof(struct Stats));
 
@@ -236,8 +234,7 @@
     }
 
     SkASSERT(resolvedProvider);
-    SkASSERT(resolvedFontMgr);
-    internal::AnimationBuilder builder(std::move(resolvedProvider), std::move(resolvedFontMgr),
+    internal::AnimationBuilder builder(std::move(resolvedProvider), fFontMgr,
                                        &fStats, duration, fps);
     auto scene = builder.parse(json);
 
diff --git a/modules/skottie/src/SkottieLayer.cpp b/modules/skottie/src/SkottieLayer.cpp
index 83cfc9f..7b09ddc 100644
--- a/modules/skottie/src/SkottieLayer.cpp
+++ b/modules/skottie/src/SkottieLayer.cpp
@@ -214,7 +214,7 @@
 
     auto animation = Animation::Builder()
             .setResourceProvider(fResourceProvider)
-            .setFontManager(fFontMgr)
+            .setFontManager(fLazyFontMgr.getMaybeNull())
             .make(static_cast<const char*>(data->data()), data->size());
     if (!animation) {
         LOG("!! Could not parse nested animation: %s\n", name);
diff --git a/modules/skottie/src/SkottiePriv.h b/modules/skottie/src/SkottiePriv.h
index 90af97f..02ccbd9 100644
--- a/modules/skottie/src/SkottiePriv.h
+++ b/modules/skottie/src/SkottiePriv.h
@@ -101,8 +101,26 @@
     sk_sp<sksg::RenderNode> attachSolidLayer  (const skjson::ObjectValue&, AnimatorScope*) const;
     sk_sp<sksg::RenderNode> attachTextLayer   (const skjson::ObjectValue&, AnimatorScope*) const;
 
+    // Delay resolving the fontmgr until it is actually needed.
+    struct LazyResolveFontMgr {
+        LazyResolveFontMgr(sk_sp<SkFontMgr> fontMgr) : fFontMgr(std::move(fontMgr)) {}
+
+        const sk_sp<SkFontMgr>& get() {
+            if (!fFontMgr) {
+                fFontMgr = SkFontMgr::RefDefault();
+                SkASSERT(fFontMgr);
+            }
+            return fFontMgr;
+        }
+
+        const sk_sp<SkFontMgr>& getMaybeNull() const { return fFontMgr; }
+
+    private:
+        sk_sp<SkFontMgr> fFontMgr;
+    };
+
     sk_sp<ResourceProvider>    fResourceProvider;
-    sk_sp<SkFontMgr>           fFontMgr;
+    LazyResolveFontMgr         fLazyFontMgr;
     Animation::Builder::Stats* fStats;
     const float                fDuration,
                                fFrameRate;
diff --git a/modules/skottie/src/SkottieTextLayer.cpp b/modules/skottie/src/SkottieTextLayer.cpp
index fa8822a..49f834c 100644
--- a/modules/skottie/src/SkottieTextLayer.cpp
+++ b/modules/skottie/src/SkottieTextLayer.cpp
@@ -117,13 +117,14 @@
                     continue;
                 }
 
-                sk_sp<SkTypeface> tf(fFontMgr->matchFamilyStyle(jfamily->begin(),
-                                                                FontStyle(jstyle->begin())));
+                const auto& fmgr = fLazyFontMgr.get();
+                sk_sp<SkTypeface> tf(fmgr->matchFamilyStyle(jfamily->begin(),
+                                                            FontStyle(jstyle->begin())));
                 if (!tf) {
                     LOG("!! Could not create typeface for %s|%s\n",
                         jfamily->begin(), jstyle->begin());
                     // Last resort.
-                    tf = fFontMgr->legacyMakeTypeface(nullptr, FontStyle(jstyle->begin()));
+                    tf = fmgr->legacyMakeTypeface(nullptr, FontStyle(jstyle->begin()));
                     if (!tf) {
                         continue;
                     }