[svg] Implement feDisplacementMap

https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement

Bug: skia:10841
Change-Id: Icf1a560b2d83e7954207fb1740e77193362b9fd4
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/356312
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Tyler Denniston <tdenniston@google.com>
diff --git a/modules/svg/src/SkSVGDOM.cpp b/modules/svg/src/SkSVGDOM.cpp
index 5d8b0be..08fea1f 100644
--- a/modules/svg/src/SkSVGDOM.cpp
+++ b/modules/svg/src/SkSVGDOM.cpp
@@ -19,6 +19,7 @@
 #include "modules/svg/include/SkSVGFeBlend.h"
 #include "modules/svg/include/SkSVGFeColorMatrix.h"
 #include "modules/svg/include/SkSVGFeComposite.h"
+#include "modules/svg/include/SkSVGFeDisplacementMap.h"
 #include "modules/svg/include/SkSVGFeFlood.h"
 #include "modules/svg/include/SkSVGFeGaussianBlur.h"
 #include "modules/svg/include/SkSVGFeMorphology.h"
@@ -257,36 +258,37 @@
 };
 
 SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
-    { "a"             , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make();              }},
-    { "circle"        , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make();         }},
-    { "clipPath"      , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make();       }},
-    { "defs"          , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make();           }},
-    { "ellipse"       , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make();        }},
-    { "feBlend"       , []() -> sk_sp<SkSVGNode> { return SkSVGFeBlend::Make();        }},
-    { "feColorMatrix" , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make();  }},
-    { "feComposite"   , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make();    }},
-    { "feFlood"       , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make();        }},
-    { "feGaussianBlur", []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
-    { "feMorphology"  , []() -> sk_sp<SkSVGNode> { return SkSVGFeMorphology::Make();   }},
-    { "feOffset"      , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::Make();       }},
-    { "feTurbulence"  , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make();   }},
-    { "filter"        , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make();         }},
-    { "g"             , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make();              }},
-    { "line"          , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make();           }},
-    { "linearGradient", []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make(); }},
-    { "mask"          , []() -> sk_sp<SkSVGNode> { return SkSVGMask::Make();           }},
-    { "path"          , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make();           }},
-    { "pattern"       , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make();        }},
-    { "polygon"       , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon();    }},
-    { "polyline"      , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline();   }},
-    { "radialGradient", []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make(); }},
-    { "rect"          , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make();           }},
-    { "stop"          , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make();           }},
+    { "a"                , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make();                 }},
+    { "circle"           , []() -> sk_sp<SkSVGNode> { return SkSVGCircle::Make();            }},
+    { "clipPath"         , []() -> sk_sp<SkSVGNode> { return SkSVGClipPath::Make();          }},
+    { "defs"             , []() -> sk_sp<SkSVGNode> { return SkSVGDefs::Make();              }},
+    { "ellipse"          , []() -> sk_sp<SkSVGNode> { return SkSVGEllipse::Make();           }},
+    { "feBlend"          , []() -> sk_sp<SkSVGNode> { return SkSVGFeBlend::Make();           }},
+    { "feColorMatrix"    , []() -> sk_sp<SkSVGNode> { return SkSVGFeColorMatrix::Make();     }},
+    { "feComposite"      , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make();       }},
+    { "feDisplacementMap", []() -> sk_sp<SkSVGNode> { return SkSVGFeDisplacementMap::Make(); }},
+    { "feFlood"          , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make();           }},
+    { "feGaussianBlur"   , []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make();    }},
+    { "feMorphology"     , []() -> sk_sp<SkSVGNode> { return SkSVGFeMorphology::Make();      }},
+    { "feOffset"         , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::Make();          }},
+    { "feTurbulence"     , []() -> sk_sp<SkSVGNode> { return SkSVGFeTurbulence::Make();      }},
+    { "filter"           , []() -> sk_sp<SkSVGNode> { return SkSVGFilter::Make();            }},
+    { "g"                , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make();                 }},
+    { "line"             , []() -> sk_sp<SkSVGNode> { return SkSVGLine::Make();              }},
+    { "linearGradient"   , []() -> sk_sp<SkSVGNode> { return SkSVGLinearGradient::Make();    }},
+    { "mask"             , []() -> sk_sp<SkSVGNode> { return SkSVGMask::Make();              }},
+    { "path"             , []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make();              }},
+    { "pattern"          , []() -> sk_sp<SkSVGNode> { return SkSVGPattern::Make();           }},
+    { "polygon"          , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolygon();       }},
+    { "polyline"         , []() -> sk_sp<SkSVGNode> { return SkSVGPoly::MakePolyline();      }},
+    { "radialGradient"   , []() -> sk_sp<SkSVGNode> { return SkSVGRadialGradient::Make();    }},
+    { "rect"             , []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make();              }},
+    { "stop"             , []() -> sk_sp<SkSVGNode> { return SkSVGStop::Make();              }},
 //    "svg" handled explicitly
-    { "text"          , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make();           }},
-    { "textPath"      , []() -> sk_sp<SkSVGNode> { return SkSVGTextPath::Make();       }},
-    { "tspan"         , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make();          }},
-    { "use"           , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make();            }},
+    { "text"             , []() -> sk_sp<SkSVGNode> { return SkSVGText::Make();              }},
+    { "textPath"         , []() -> sk_sp<SkSVGNode> { return SkSVGTextPath::Make();          }},
+    { "tspan"            , []() -> sk_sp<SkSVGNode> { return SkSVGTSpan::Make();             }},
+    { "use"              , []() -> sk_sp<SkSVGNode> { return SkSVGUse::Make();               }},
 };
 
 struct ConstructionContext {
diff --git a/modules/svg/src/SkSVGFeDisplacementMap.cpp b/modules/svg/src/SkSVGFeDisplacementMap.cpp
new file mode 100644
index 0000000..966faa5
--- /dev/null
+++ b/modules/svg/src/SkSVGFeDisplacementMap.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright 2020 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "include/effects/SkImageFilters.h"
+#include "modules/svg/include/SkSVGAttributeParser.h"
+#include "modules/svg/include/SkSVGFeDisplacementMap.h"
+#include "modules/svg/include/SkSVGFilterContext.h"
+#include "modules/svg/include/SkSVGRenderContext.h"
+#include "modules/svg/include/SkSVGValue.h"
+
+bool SkSVGFeDisplacementMap::parseAndSetAttribute(const char* name, const char* value) {
+    return INHERITED::parseAndSetAttribute(name, value) ||
+           this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) ||
+           this->setXChannelSelector(
+                   SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
+                           "xChannelSelector", name, value)) ||
+           this->setYChannelSelector(
+                   SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
+                           "yChannelSelector", name, value)) ||
+           this->setScale(SkSVGAttributeParser::parse<SkSVGNumberType>("scale", name, value));
+}
+
+sk_sp<SkImageFilter> SkSVGFeDisplacementMap::onMakeImageFilter(
+        const SkSVGRenderContext& ctx, const SkSVGFilterContext& fctx) const {
+    const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
+    const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
+
+    // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
+    // the 'in' source image must remain in its current colorspace.
+    sk_sp<SkImageFilter> in = fctx.resolveInput(ctx, this->getIn());
+    sk_sp<SkImageFilter> in2 = fctx.resolveInput(ctx, this->getIn2(), colorspace);
+
+    SkScalar scale = fScale;
+    if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
+        SkASSERT(ctx.node());
+        const SkRect objBounds = ctx.node()->objectBoundingBox(ctx);
+        const SkSVGLengthContext lctx({objBounds.width(), objBounds.height()});
+        scale = lctx.resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage),
+                             SkSVGLengthContext::LengthType::kOther);
+    }
+
+    return SkImageFilters::DisplacementMap(
+            fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect);
+}
+
+SkSVGColorspace SkSVGFeDisplacementMap::resolveColorspace(const SkSVGRenderContext& ctx,
+                                                          const SkSVGFilterContext& fctx) const {
+    // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
+    // the 'in' source image must remain in its current colorspace, which means the colorspace of
+    // this FE node is the same as the input.
+    return fctx.resolveInputColorspace(ctx, this->getIn());
+}
+
+template <>
+bool SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
+        SkSVGFeDisplacementMap::ChannelSelector* channel) {
+    static constexpr std::tuple<const char*, SkSVGFeDisplacementMap::ChannelSelector> gMap[] = {
+            { "R", SkSVGFeDisplacementMap::ChannelSelector::kR },
+            { "G", SkSVGFeDisplacementMap::ChannelSelector::kG },
+            { "B", SkSVGFeDisplacementMap::ChannelSelector::kB },
+            { "A", SkSVGFeDisplacementMap::ChannelSelector::kA },
+    };
+
+    return this->parseEnumMap(gMap, channel) && this->parseEOSToken();
+}