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