xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGFe.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/SkSVGFe.h"
9 
10 #include "modules/svg/include/SkSVGAttribute.h"
11 #include "modules/svg/include/SkSVGAttributeParser.h"
12 #include "modules/svg/include/SkSVGFilterContext.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 
15 #include <cstddef>
16 #include <tuple>
17 
makeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const18 sk_sp<SkImageFilter> SkSVGFe::makeImageFilter(const SkSVGRenderContext& ctx,
19                                               const SkSVGFilterContext& fctx) const {
20     return this->onMakeImageFilter(ctx, fctx);
21 }
22 
resolveBoundaries(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const23 SkRect SkSVGFe::resolveBoundaries(const SkSVGRenderContext& ctx,
24                                   const SkSVGFilterContext& fctx) const {
25     const auto x = fX.isValid() ? *fX : SkSVGLength(0, SkSVGLength::Unit::kPercentage);
26     const auto y = fY.isValid() ? *fY : SkSVGLength(0, SkSVGLength::Unit::kPercentage);
27     const auto w = fWidth.isValid() ? *fWidth : SkSVGLength(100, SkSVGLength::Unit::kPercentage);
28     const auto h = fHeight.isValid() ? *fHeight : SkSVGLength(100, SkSVGLength::Unit::kPercentage);
29 
30     return ctx.resolveOBBRect(x, y, w, h, fctx.primitiveUnits());
31 }
32 
AnyIsStandardInput(const SkSVGFilterContext & fctx,const std::vector<SkSVGFeInputType> & inputs)33 static bool AnyIsStandardInput(const SkSVGFilterContext& fctx,
34                                const std::vector<SkSVGFeInputType>& inputs) {
35     for (const auto& in : inputs) {
36         switch (in.type()) {
37             case SkSVGFeInputType::Type::kFilterPrimitiveReference:
38                 break;
39             case SkSVGFeInputType::Type::kSourceGraphic:
40             case SkSVGFeInputType::Type::kSourceAlpha:
41             case SkSVGFeInputType::Type::kBackgroundImage:
42             case SkSVGFeInputType::Type::kBackgroundAlpha:
43             case SkSVGFeInputType::Type::kFillPaint:
44             case SkSVGFeInputType::Type::kStrokePaint:
45                 return true;
46             case SkSVGFeInputType::Type::kUnspecified:
47                 // Unspecified means previous result (which may be SourceGraphic).
48                 if (fctx.previousResultIsSourceGraphic()) {
49                     return true;
50                 }
51                 break;
52         }
53     }
54 
55     return false;
56 }
57 
resolveFilterSubregion(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const58 SkRect SkSVGFe::resolveFilterSubregion(const SkSVGRenderContext& ctx,
59                                        const SkSVGFilterContext& fctx) const {
60     // From https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveSubRegion,
61     // the default filter effect subregion is equal to the union of the subregions defined
62     // for all "referenced nodes" (filter effect inputs). If there are no inputs, the
63     // default subregion is equal to the filter effects region
64     // (https://www.w3.org/TR/SVG11/filters.html#FilterEffectsRegion).
65     const std::vector<SkSVGFeInputType> inputs = this->getInputs();
66     SkRect defaultSubregion;
67     if (inputs.empty() || AnyIsStandardInput(fctx, inputs)) {
68         defaultSubregion = fctx.filterEffectsRegion();
69     } else {
70         defaultSubregion = fctx.filterPrimitiveSubregion(inputs[0]);
71         for (size_t i = 1; i < inputs.size(); i++) {
72             defaultSubregion.join(fctx.filterPrimitiveSubregion(inputs[i]));
73         }
74     }
75 
76     // Next resolve the rect specified by the x, y, width, height attributes on this filter effect.
77     // If those attributes were given, they override the corresponding attribute of the default
78     // filter effect subregion calculated above.
79     const SkRect boundaries = this->resolveBoundaries(ctx, fctx);
80 
81     // Compute and return the fully resolved subregion.
82     return SkRect::MakeXYWH(fX.isValid() ? boundaries.fLeft : defaultSubregion.fLeft,
83                             fY.isValid() ? boundaries.fTop : defaultSubregion.fTop,
84                             fWidth.isValid() ? boundaries.width() : defaultSubregion.width(),
85                             fHeight.isValid() ? boundaries.height() : defaultSubregion.height());
86 }
87 
resolveColorspace(const SkSVGRenderContext & ctx,const SkSVGFilterContext &) const88 SkSVGColorspace SkSVGFe::resolveColorspace(const SkSVGRenderContext& ctx,
89                                            const SkSVGFilterContext&) const {
90     constexpr SkSVGColorspace kDefaultCS = SkSVGColorspace::kSRGB;
91     const SkSVGColorspace cs = *ctx.presentationContext().fInherited.fColorInterpolationFilters;
92     return cs == SkSVGColorspace::kAuto ? kDefaultCS : cs;
93 }
94 
applyProperties(SkSVGRenderContext * ctx) const95 void SkSVGFe::applyProperties(SkSVGRenderContext* ctx) const { this->onPrepareToRender(ctx); }
96 
parseAndSetAttribute(const char * name,const char * value)97 bool SkSVGFe::parseAndSetAttribute(const char* name, const char* value) {
98     return INHERITED::parseAndSetAttribute(name, value) ||
99            this->setIn(SkSVGAttributeParser::parse<SkSVGFeInputType>("in", name, value)) ||
100            this->setResult(SkSVGAttributeParser::parse<SkSVGStringType>("result", name, value)) ||
101            this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", name, value)) ||
102            this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", name, value)) ||
103            this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", name, value)) ||
104            this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", name, value));
105 }
106 
parse(SkSVGFeInputType * type)107 template <> bool SkSVGAttributeParser::parse(SkSVGFeInputType* type) {
108     static constexpr std::tuple<const char*, SkSVGFeInputType::Type> gTypeMap[] = {
109             {"SourceGraphic", SkSVGFeInputType::Type::kSourceGraphic},
110             {"SourceAlpha", SkSVGFeInputType::Type::kSourceAlpha},
111             {"BackgroundImage", SkSVGFeInputType::Type::kBackgroundImage},
112             {"BackgroundAlpha", SkSVGFeInputType::Type::kBackgroundAlpha},
113             {"FillPaint", SkSVGFeInputType::Type::kFillPaint},
114             {"StrokePaint", SkSVGFeInputType::Type::kStrokePaint},
115     };
116 
117     SkSVGStringType resultId;
118     SkSVGFeInputType::Type t;
119     bool parsedValue = false;
120     if (this->parseEnumMap(gTypeMap, &t)) {
121         *type = SkSVGFeInputType(t);
122         parsedValue = true;
123     } else if (parse(&resultId)) {
124         *type = SkSVGFeInputType(resultId);
125         parsedValue = true;
126     }
127 
128     return parsedValue && this->parseEOSToken();
129 }
130