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/SkSVGFilter.h"
9
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkRect.h"
13 #include "include/effects/SkImageFilters.h"
14 #include "include/private/base/SkTArray.h"
15 #include "modules/svg/include/SkSVGAttributeParser.h"
16 #include "modules/svg/include/SkSVGFe.h"
17 #include "modules/svg/include/SkSVGFilterContext.h"
18 #include "modules/svg/include/SkSVGRenderContext.h"
19
parseAndSetAttribute(const char * name,const char * value)20 bool SkSVGFilter::parseAndSetAttribute(const char* name, const char* value) {
21 return INHERITED::parseAndSetAttribute(name, value) ||
22 this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", name, value)) ||
23 this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", name, value)) ||
24 this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", name, value)) ||
25 this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", name, value)) ||
26 this->setFilterUnits(SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(
27 "filterUnits", name, value)) ||
28 this->setPrimitiveUnits(SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>(
29 "primitiveUnits", name, value));
30 }
31
applyProperties(SkSVGRenderContext * ctx) const32 void SkSVGFilter::applyProperties(SkSVGRenderContext* ctx) const { this->onPrepareToRender(ctx); }
33
buildFilterDAG(const SkSVGRenderContext & ctx) const34 sk_sp<SkImageFilter> SkSVGFilter::buildFilterDAG(const SkSVGRenderContext& ctx) const {
35 sk_sp<SkImageFilter> filter;
36 SkSVGFilterContext fctx(ctx.resolveOBBRect(fX, fY, fWidth, fHeight, fFilterUnits),
37 fPrimitiveUnits);
38 SkSVGRenderContext localCtx(ctx);
39 this->applyProperties(&localCtx);
40 SkSVGColorspace cs = SkSVGColorspace::kSRGB;
41 for (const auto& child : fChildren) {
42 if (!SkSVGFe::IsFilterEffect(child)) {
43 continue;
44 }
45
46 const auto& feNode = static_cast<const SkSVGFe&>(*child);
47 const auto& feResultType = feNode.getResult();
48
49 // Propagate any inherited properties that may impact filter effect behavior (e.g.
50 // color-interpolation-filters). We call this explicitly here because the SkSVGFe
51 // nodes do not participate in the normal onRender path, which is when property
52 // propagation currently occurs.
53 SkSVGRenderContext localChildCtx(localCtx);
54 feNode.applyProperties(&localChildCtx);
55
56 const SkRect filterSubregion = feNode.resolveFilterSubregion(localChildCtx, fctx);
57 cs = feNode.resolveColorspace(localChildCtx, fctx);
58 filter = feNode.makeImageFilter(localChildCtx, fctx);
59
60 if (!feResultType.isEmpty()) {
61 fctx.registerResult(feResultType, filter, filterSubregion, cs);
62 }
63
64 // Unspecified 'in' and 'in2' inputs implicitly resolve to the previous filter's result.
65 fctx.setPreviousResult(filter, filterSubregion, cs);
66 }
67
68 // Convert to final destination colorspace
69 if (cs != SkSVGColorspace::kSRGB) {
70 filter = SkImageFilters::ColorFilter(SkColorFilters::LinearToSRGBGamma(), filter);
71 }
72
73 return filter;
74 }
75