add DM::FontMgr to ok

The bots will still use DM, but the multiprocess architecture of ok
makes it easier to isolate and debug crashes, assertions, and unit
test failures.

Usage:
  $ ninja -C out ok
  $ out/ok test portable_fonts
...
935 ok, 4 failed, 5 crashed
...

Change-Id: I6bbd0ffc02d19bb5907c71eaebe30ac3646a80a6
Reviewed-on: https://skia-review.googlesource.com/68201
Reviewed-by: Mike Klein <mtklein@chromium.org>
Commit-Queue: Mike Klein <mtklein@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index e7744c9..e0c45fe 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1363,6 +1363,7 @@
 
   test_app("ok") {
     sources = [
+      "dm/DMFontMgr.cpp",
       "tools/ok.cpp",
       "tools/ok_dsts.cpp",
       "tools/ok_engines.cpp",
diff --git a/tools/ok_vias.cpp b/tools/ok_vias.cpp
index 815b159..1625223 100644
--- a/tools/ok_vias.cpp
+++ b/tools/ok_vias.cpp
@@ -5,6 +5,7 @@
  * found in the LICENSE file.
  */
 
+#include "../dm/DMFontMgr.h"
 #include "ProcStats.h"
 #include "SkColorFilter.h"
 #include "SkEventTracingPriv.h"
@@ -266,7 +267,6 @@
 };
 static Register memory{"memory", "print process maximum memory usage", Memory::Create};
 
-static SkOnce init_tracing_once;
 struct Trace : Dst {
     std::unique_ptr<Dst> target;
     std::string trace_mode;
@@ -279,7 +279,8 @@
     }
 
     Status draw(Src* src) override {
-        init_tracing_once([&] { initializeEventTracingForTools(trace_mode.c_str()); });
+        static SkOnce once;
+        once([&] { initializeEventTracingForTools(trace_mode.c_str()); });
         return target->draw(src);
     }
 
@@ -287,6 +288,35 @@
         return target->image();
     }
 };
-static Register trace {"trace",
-                       "enable tracing in mode=atrace, mode=debugf, or mode=trace.json",
-                       Trace::Create};
+static Register trace{"trace",
+                      "enable tracing in mode=atrace, mode=debugf, or mode=trace.json",
+                      Trace::Create};
+
+extern sk_sp<SkFontMgr> (*gSkFontMgr_DefaultFactory)();
+
+struct PortableFonts : Dst {
+    std::unique_ptr<Dst> target;
+
+    static std::unique_ptr<Dst> Create(Options options, std::unique_ptr<Dst> dst) {
+        PortableFonts via;
+        via.target = std::move(dst);
+        return move_unique(via);
+    }
+
+    Status draw(Src* src) override {
+        static SkOnce once;
+        once([]{
+            gSkFontMgr_DefaultFactory = []() -> sk_sp<SkFontMgr> {
+                return sk_make_sp<DM::FontMgr>();
+            };
+        });
+        return target->draw(src);
+    }
+
+    sk_sp<SkImage> image() override {
+        return target->image();
+    }
+};
+static Register portable_fonts{"portable_fonts",
+                               "use DM::FontMgr to make fonts more portable",
+                               PortableFonts::Create};