[svg] Implement feMorphology filter

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

Bug: skia:10841
Change-Id: I0b0028fb815c490670f9f1e888770efb194a8f3b
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/356107
Reviewed-by: Florin Malita <fmalita@chromium.org>
Commit-Queue: Tyler Denniston <tdenniston@google.com>
This commit is contained in:
Tyler Denniston 2021-01-20 09:41:22 -05:00 committed by Skia Commit-Bot
parent 24c4d209ba
commit 4c89481be4
6 changed files with 122 additions and 2 deletions

View File

@ -21,7 +21,7 @@ public:
return node->tag() == SkSVGTag::kFeTurbulence || node->tag() == SkSVGTag::kFeColorMatrix ||
node->tag() == SkSVGTag::kFeComposite || node->tag() == SkSVGTag::kFeFlood ||
node->tag() == SkSVGTag::kFeGaussianBlur || node->tag() == SkSVGTag::kFeOffset ||
node->tag() == SkSVGTag::kFeBlend;
node->tag() == SkSVGTag::kFeBlend || node->tag() == SkSVGTag::kFeMorphology;
}
sk_sp<SkImageFilter> makeImageFilter(const SkSVGRenderContext& ctx,

View File

@ -0,0 +1,47 @@
/*
* Copyright 2020 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef SkSVGFeMorphology_DEFINED
#define SkSVGFeMorphology_DEFINED
#include "modules/svg/include/SkSVGFe.h"
#include "modules/svg/include/SkSVGTypes.h"
class SkSVGFeMorphology : public SkSVGFe {
public:
struct Radius {
SkSVGNumberType fX;
SkSVGNumberType fY;
};
enum class Operator {
kErode,
kDilate,
};
static sk_sp<SkSVGFeMorphology> Make() {
return sk_sp<SkSVGFeMorphology>(new SkSVGFeMorphology());
}
SVG_ATTR(Operator, Operator, Operator::kErode)
SVG_ATTR(Radius , Radius , Radius({0, 0}))
protected:
sk_sp<SkImageFilter> onMakeImageFilter(const SkSVGRenderContext&,
const SkSVGFilterContext&) const override;
std::vector<SkSVGFeInputType> getInputs() const override { return {this->getIn()}; }
bool parseAndSetAttribute(const char*, const char*) override;
private:
SkSVGFeMorphology() : INHERITED(SkSVGTag::kFeMorphology) {}
using INHERITED = SkSVGFe;
};
#endif // SkSVGFeMorphology_DEFINED

View File

@ -30,6 +30,7 @@ enum class SkSVGTag {
kFeComposite,
kFeFlood,
kFeGaussianBlur,
kFeMorphology,
kFeOffset,
kFeTurbulence,
kFilter,

View File

@ -21,6 +21,7 @@
#include "modules/svg/include/SkSVGFeComposite.h"
#include "modules/svg/include/SkSVGFeFlood.h"
#include "modules/svg/include/SkSVGFeGaussianBlur.h"
#include "modules/svg/include/SkSVGFeMorphology.h"
#include "modules/svg/include/SkSVGFeOffset.h"
#include "modules/svg/include/SkSVGFeTurbulence.h"
#include "modules/svg/include/SkSVGFilter.h"
@ -266,7 +267,8 @@ SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = {
{ "feComposite" , []() -> sk_sp<SkSVGNode> { return SkSVGFeComposite::Make(); }},
{ "feFlood" , []() -> sk_sp<SkSVGNode> { return SkSVGFeFlood::Make(); }},
{ "feGaussianBlur", []() -> sk_sp<SkSVGNode> { return SkSVGFeGaussianBlur::Make(); }},
{ "feOffset" , []() -> sk_sp<SkSVGNode> { return SkSVGFeOffset::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(); }},

View File

@ -0,0 +1,68 @@
/*
* 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/SkSVGFeMorphology.h"
#include "modules/svg/include/SkSVGFilterContext.h"
#include "modules/svg/include/SkSVGRenderContext.h"
#include "modules/svg/include/SkSVGValue.h"
bool SkSVGFeMorphology::parseAndSetAttribute(const char* name, const char* value) {
return INHERITED::parseAndSetAttribute(name, value) ||
this->setOperator(SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(
"operator", name, value)) ||
this->setRadius(SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(
"radius", name, value));
}
sk_sp<SkImageFilter> SkSVGFeMorphology::onMakeImageFilter(const SkSVGRenderContext& ctx,
const SkSVGFilterContext& fctx) const {
const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
const SkSVGColorspace colorspace = this->resolveColorspace(ctx);
sk_sp<SkImageFilter> input = fctx.resolveInput(ctx, this->getIn(), colorspace);
SkScalar rx = fRadius.fX;
SkScalar ry = fRadius.fY;
if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
SkASSERT(ctx.node());
const SkRect objBounds = ctx.node()->objectBoundingBox(ctx);
rx *= objBounds.width();
ry *= objBounds.height();
}
switch (fOperator) {
case Operator::kErode:
return SkImageFilters::Erode(rx, ry, input, cropRect);
case Operator::kDilate:
return SkImageFilters::Dilate(rx, ry, input, cropRect);
}
SkUNREACHABLE;
}
template <>
bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(SkSVGFeMorphology::Operator* op) {
static constexpr std::tuple<const char*, SkSVGFeMorphology::Operator> gMap[] = {
{ "dilate", SkSVGFeMorphology::Operator::kDilate },
{ "erode" , SkSVGFeMorphology::Operator::kErode },
};
return this->parseEnumMap(gMap, op) && this->parseEOSToken();
}
template <>
bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(SkSVGFeMorphology::Radius* radius) {
std::vector<SkSVGNumberType> values;
if (!this->parse(&values)) {
return false;
}
radius->fX = values[0];
radius->fY = values.size() > 1 ? values[1] : values[0];
return true;
}

View File

@ -22,6 +22,7 @@ skia_svg_public = [
"$_include/SkSVGFeComposite.h",
"$_include/SkSVGFeFlood.h",
"$_include/SkSVGFeGaussianBlur.h",
"$_include/SkSVGFeMorphology.h",
"$_include/SkSVGFeOffset.h",
"$_include/SkSVGFeTurbulence.h",
"$_include/SkSVGFilter.h",
@ -64,6 +65,7 @@ skia_svg_sources = [
"$_src/SkSVGFeComposite.cpp",
"$_src/SkSVGFeFlood.cpp",
"$_src/SkSVGFeGaussianBlur.cpp",
"$_src/SkSVGFeMorphology.cpp",
"$_src/SkSVGFeOffset.cpp",
"$_src/SkSVGFeTurbulence.cpp",
"$_src/SkSVGFilter.cpp",