Add an SVG DM source

R=mtklein@google.com,robertphillips@google.com,stephana@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2209593004

Review-Url: https://codereview.chromium.org/2209593004
diff --git a/BUILD.gn b/BUILD.gn
index 0d15f31..1286a06 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -501,6 +501,27 @@
   ]
 }
 
+test_lib("experimental_svg_model") {
+  public_include_dirs = [ "experimental/svg/model" ]
+  sources = [
+    "experimental/svg/model/SkSVGAttribute.cpp",
+    "experimental/svg/model/SkSVGAttributeParser.cpp",
+    "experimental/svg/model/SkSVGContainer.cpp",
+    "experimental/svg/model/SkSVGDOM.cpp",
+    "experimental/svg/model/SkSVGNode.cpp",
+    "experimental/svg/model/SkSVGPath.cpp",
+    "experimental/svg/model/SkSVGRect.cpp",
+    "experimental/svg/model/SkSVGRenderContext.cpp",
+    "experimental/svg/model/SkSVGSVG.cpp",
+    "experimental/svg/model/SkSVGShape.cpp",
+    "experimental/svg/model/SkSVGTransformableNode.cpp",
+    "experimental/svg/model/SkSVGValue.cpp",
+  ]
+  deps = [
+    ":skia",
+  ]
+}
+
 if (!is_component_build) {  # Our test tools use many non-SK_API APIs...
   executable("dm") {
     sources = [
@@ -510,6 +531,7 @@
     ]
     include_dirs = [ "tests" ]
     deps = [
+      ":experimental_svg_model",
       ":flags",
       ":gm",
       ":gpu_tool_utils",
diff --git a/dm/DM.cpp b/dm/DM.cpp
index 4387fde..fcc3c55 100644
--- a/dm/DM.cpp
+++ b/dm/DM.cpp
@@ -73,6 +73,8 @@
 
 DEFINE_string(mskps, "", "Directory to read mskps from, or a single mskp file.");
 
+DEFINE_string(svgs, "", "Directory to read SVGs from, or a single SVG file.");
+
 using namespace DM;
 using sk_gpu_test::GrContextFactory;
 using sk_gpu_test::GLTestContext;
@@ -710,34 +712,31 @@
     return false;
 }
 
+template <typename T>
+void gather_file_srcs(const SkCommandLineFlags::StringArray& flags, const char* ext) {
+    for (int i = 0; i < flags.count(); i++) {
+        const char* path = flags[i];
+        if (sk_isdir(path)) {
+            SkOSFile::Iter it(path, ext);
+            for (SkString file; it.next(&file); ) {
+                push_src(ext, "", new T(SkOSPath::Join(path, file.c_str())));
+            }
+        } else {
+            push_src(ext, "", new T(path));
+        }
+    }
+}
+
 static bool gather_srcs() {
     for (const skiagm::GMRegistry* r = skiagm::GMRegistry::Head(); r; r = r->next()) {
         push_src("gm", "", new GMSrc(r->factory()));
     }
-    for (int i = 0; i < FLAGS_skps.count(); i++) {
-        const char* path = FLAGS_skps[i];
-        if (sk_isdir(path)) {
-            SkOSFile::Iter it(path, "skp");
-            for (SkString file; it.next(&file); ) {
-                push_src("skp", "", new SKPSrc(SkOSPath::Join(path, file.c_str())));
-            }
-        } else {
-            push_src("skp", "", new SKPSrc(path));
-        }
-    }
 
-    for (int i = 0; i < FLAGS_mskps.count(); i++) {
-        const char* path = FLAGS_mskps[i];
-        if (sk_isdir(path)) {
-            SkOSFile::Iter it(path, "mskp");
-            for (SkString file; it.next(&file);) {
-                push_src("mskp", "",
-                         new MSKPSrc(SkOSPath::Join(path, file.c_str())));
-            }
-        } else {
-            push_src("mskp", "", new MSKPSrc(path));
-        }
-    }
+    gather_file_srcs<SKPSrc>(FLAGS_skps, "skp");
+    gather_file_srcs<MSKPSrc>(FLAGS_mskps, "mskp");
+#if defined(SK_XML)
+    gather_file_srcs<SVGSrc>(FLAGS_svgs, "svg");
+#endif
 
     SkTArray<SkString> images;
     if (!CollectImages(FLAGS_images, &images)) {
diff --git a/dm/DMSrcSink.cpp b/dm/DMSrcSink.cpp
index a00f468..f821616 100644
--- a/dm/DMSrcSink.cpp
+++ b/dm/DMSrcSink.cpp
@@ -34,6 +34,7 @@
 #include "SkSVGCanvas.h"
 #include "SkStream.h"
 #include "SkTLogic.h"
+#include "SkSVGDOM.h"
 #include "SkSwizzler.h"
 #include <functional>
 
@@ -1025,6 +1026,36 @@
 Name SKPSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
 
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+#if defined(SK_XML)
+// Should we try to use the SVG intrinsic size instead?
+static const SkSize kSVGSize = SkSize::Make(1000, 1000);
+
+SVGSrc::SVGSrc(Path path) : fPath(path) {}
+
+Error SVGSrc::draw(SkCanvas* canvas) const {
+    SkFILEStream stream(fPath.c_str());
+    if (!stream.isValid()) {
+        return SkStringPrintf("Unable to open file: %s", fPath.c_str());
+    }
+
+    sk_sp<SkSVGDOM> dom = SkSVGDOM::MakeFromStream(stream, kSVGSize);
+    if (!dom) {
+        return SkStringPrintf("Unable to parse file: %s", fPath.c_str());
+    }
+
+    dom->render(canvas);
+
+    return "";
+}
+
+SkISize SVGSrc::size() const {
+    return kSVGSize.toRound();
+}
+
+Name SVGSrc::name() const { return SkOSPath::Basename(fPath.c_str()); }
+
+#endif // defined(SK_XML)
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 MSKPSrc::MSKPSrc(Path path) : fPath(path) {
     std::unique_ptr<SkStreamAsset> stream(SkStream::NewFromFile(fPath.c_str()));
diff --git a/dm/DMSrcSink.h b/dm/DMSrcSink.h
index b05fdb0..a54ddb8 100644
--- a/dm/DMSrcSink.h
+++ b/dm/DMSrcSink.h
@@ -248,6 +248,21 @@
     Path fPath;
 };
 
+#if defined(SK_XML)
+class SVGSrc : public Src {
+public:
+    explicit SVGSrc(Path path);
+
+    Error draw(SkCanvas*) const override;
+    SkISize size() const override;
+    Name name() const override;
+
+private:
+    Path fPath;
+
+    typedef Src INHERITED;
+};
+#endif // SK_XML
 /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
 
 class MSKPSrc : public Src {
diff --git a/gyp/dm.gypi b/gyp/dm.gypi
index 1e766e0..fab2f91 100644
--- a/gyp/dm.gypi
+++ b/gyp/dm.gypi
@@ -24,6 +24,7 @@
     'skia_lib.gyp:skia_lib',
     'sksl.gyp:sksl',
     'svg.gyp:svg',
+    'svg.gyp:svgdom',
     'tools.gyp:crash_handler',
     'tools.gyp:picture_utils',
     'tools.gyp:proc_stats',