Viewer SVG support

Change-Id: I93ee61271ebe960063bec16ba472b3fd243ee149
Reviewed-on: https://skia-review.googlesource.com/118885
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 8b30c41..69ed2d4 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -1693,7 +1693,7 @@
     deps = [
       ":flags",
       ":skia",
-      ":tool_utils"
+      ":tool_utils",
     ]
   }
 
@@ -2014,6 +2014,7 @@
         "tools/viewer/SkottieSlide.cpp",
         "tools/viewer/SlideDir.cpp",
         "tools/viewer/StatsLayer.cpp",
+        "tools/viewer/SvgSlide.cpp",
         "tools/viewer/Viewer.cpp",
       ]
       libs = []
@@ -2022,6 +2023,7 @@
       deps = [
         ":experimental_skottie",
         ":experimental_sksg",
+        ":experimental_svg_model",
         ":flags",
         ":gm",
         ":gpu_tool_utils",
diff --git a/tools/viewer/SvgSlide.cpp b/tools/viewer/SvgSlide.cpp
new file mode 100644
index 0000000..41dc318
--- /dev/null
+++ b/tools/viewer/SvgSlide.cpp
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SvgSlide.h"
+
+#include "SkCanvas.h"
+#include "SkStream.h"
+#include "SkSVGDOM.h"
+
+SvgSlide::SvgSlide(const SkString& name, const SkString& path)
+    : fPath(path) {
+    fName = name;
+}
+
+void SvgSlide::load(SkScalar w, SkScalar h) {
+    fWinSize   = SkSize::Make(w, h);
+
+    if (const auto svgStream =  SkStream::MakeFromFile(fPath.c_str())) {
+        fDom = SkSVGDOM::MakeFromStream(*svgStream);
+        if (fDom) {
+            fDom->setContainerSize(fWinSize);
+        }
+    }
+}
+
+void SvgSlide::unload() {
+    fDom.reset();
+}
+
+SkISize SvgSlide::getDimensions() const {
+    // We always scale to fill the window.
+    return fWinSize.toCeil();
+}
+
+void SvgSlide::draw(SkCanvas* canvas) {
+    if (fDom) {
+        fDom->render(canvas);
+    }
+}
diff --git a/tools/viewer/SvgSlide.h b/tools/viewer/SvgSlide.h
new file mode 100644
index 0000000..9fc3a5b
--- /dev/null
+++ b/tools/viewer/SvgSlide.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SvgSlide_DEFINED
+#define SvgSlide_DEFINED
+
+#include "Slide.h"
+
+class SkSVGDOM;
+
+class SvgSlide final : public Slide {
+public:
+    SvgSlide(const SkString& name, const SkString& path);
+
+    void load(SkScalar winWidth, SkScalar winHeight) override;
+    void unload() override;
+
+    SkISize getDimensions() const override;
+
+    void draw(SkCanvas*) override;
+private:
+    const SkString  fPath;
+
+    SkSize          fWinSize = SkSize::MakeEmpty();
+    sk_sp<SkSVGDOM> fDom;
+
+    typedef Slide INHERITED;
+};
+
+#endif // SvgSlide_DEFINED
diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp
index ad02a65..fb65955 100644
--- a/tools/viewer/Viewer.cpp
+++ b/tools/viewer/Viewer.cpp
@@ -15,6 +15,7 @@
 #include "SkottieSlide.h"
 #include "SKPSlide.h"
 #include "SlideDir.h"
+#include "SvgSlide.h"
 
 #include "GrContext.h"
 #include "SkCanvas.h"
@@ -577,6 +578,21 @@
                                                    std::move(dirSlides)));
         }
     }
+
+    // SVGs
+    for (const auto& svg : FLAGS_svgs) {
+        SkOSFile::Iter it(svg.c_str(), ".svg");
+
+        SkString svgName;
+        while (it.next(&svgName)) {
+            if (SkCommandLineFlags::ShouldSkip(FLAGS_match, svgName.c_str())) {
+                continue;
+            }
+            auto slide = sk_make_sp<SvgSlide>(svgName, SkOSPath::Join(svg.c_str(),
+                                                                      svgName.c_str()));
+            fSlides.push_back(std::move(slide));
+        }
+    }
 }