xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGFeMorphology.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "modules/svg/include/SkSVGFeMorphology.h"
9 
10 #include "include/core/SkImageFilter.h"
11 #include "include/core/SkM44.h"
12 #include "include/core/SkRect.h"
13 #include "include/effects/SkImageFilters.h"
14 #include "include/private/base/SkAssert.h"
15 #include "modules/svg/include/SkSVGAttributeParser.h"
16 #include "modules/svg/include/SkSVGFilterContext.h"
17 #include "modules/svg/include/SkSVGRenderContext.h"
18 
19 #include <tuple>
20 
parseAndSetAttribute(const char * name,const char * value)21 bool SkSVGFeMorphology::parseAndSetAttribute(const char* name, const char* value) {
22     return INHERITED::parseAndSetAttribute(name, value) ||
23            this->setOperator(SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(
24                    "operator", name, value)) ||
25            this->setRadius(SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(
26                    "radius", name, value));
27 }
28 
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const29 sk_sp<SkImageFilter> SkSVGFeMorphology::onMakeImageFilter(const SkSVGRenderContext& ctx,
30                                                           const SkSVGFilterContext& fctx) const {
31     const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
32     const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
33     sk_sp<SkImageFilter> input = fctx.resolveInput(ctx, this->getIn(), colorspace);
34 
35     const auto r = SkV2{fRadius.fX, fRadius.fY}
36                  * ctx.transformForCurrentOBB(fctx.primitiveUnits()).scale;
37     switch (fOperator) {
38         case Operator::kErode:
39             return SkImageFilters::Erode(r.x, r.y, input, cropRect);
40         case Operator::kDilate:
41             return SkImageFilters::Dilate(r.x, r.y, input, cropRect);
42     }
43 
44     SkUNREACHABLE;
45 }
46 
47 template <>
parse(SkSVGFeMorphology::Operator * op)48 bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Operator>(SkSVGFeMorphology::Operator* op) {
49     static constexpr std::tuple<const char*, SkSVGFeMorphology::Operator> gMap[] = {
50             { "dilate", SkSVGFeMorphology::Operator::kDilate },
51             { "erode" , SkSVGFeMorphology::Operator::kErode  },
52     };
53 
54     return this->parseEnumMap(gMap, op) && this->parseEOSToken();
55 }
56 
57 template <>
parse(SkSVGFeMorphology::Radius * radius)58 bool SkSVGAttributeParser::parse<SkSVGFeMorphology::Radius>(SkSVGFeMorphology::Radius* radius) {
59     std::vector<SkSVGNumberType> values;
60     if (!this->parse(&values)) {
61         return false;
62     }
63 
64     radius->fX = values[0];
65     radius->fY = values.size() > 1 ? values[1] : values[0];
66     return true;
67 }
68