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
applyProperties(SkSVGRenderContext * ctx) const28 void SkSVGFilter::applyProperties(SkSVGRenderContext* ctx) const { this->onPrepareToRender(ctx); }
29
buildFilterDAG(const SkSVGRenderContext & ctx) const30 sk_sp<SkImageFilter> SkSVGFilter::buildFilterDAG(const SkSVGRenderContext& ctx) const {
31 sk_sp<SkImageFilter> filter;
32 SkSVGFilterContext fctx(ctx.resolveOBBRect(fX, fY, fWidth, fHeight, fFilterUnits),
33 fPrimitiveUnits);
34 SkSVGRenderContext localCtx(ctx);
35 this->applyProperties(&localCtx);
36 SkSVGColorspace cs = SkSVGColorspace::kSRGB;
37 for (const auto& child : fChildren) {
38 if (!SkSVGFe::IsFilterEffect(child)) {
39 continue;
40 }
41
42 const auto& feNode = static_cast<const SkSVGFe&>(*child);
43 const auto& feResultType = feNode.getResult();
44
45 // Propagate any inherited properties that may impact filter effect behavior (e.g.
46 // color-interpolation-filters). We call this explicitly here because the SkSVGFe
47 // nodes do not participate in the normal onRender path, which is when property
48 // propagation currently occurs.
49 SkSVGRenderContext localChildCtx(localCtx);
50 feNode.applyProperties(&localChildCtx);
51
52 const SkRect filterSubregion = feNode.resolveFilterSubregion(localChildCtx, fctx);
53 cs = feNode.resolveColorspace(localChildCtx, fctx);
54 filter = feNode.makeImageFilter(localChildCtx, fctx);
55
56 if (!feResultType.isEmpty()) {
57 fctx.registerResult(feResultType, filter, filterSubregion, cs);
58 }
59
60 // Unspecified 'in' and 'in2' inputs implicitly resolve to the previous filter's result.
61 fctx.setPreviousResult(filter, filterSubregion, cs);
62 }
63
64 // Convert to final destination colorspace
65 if (cs != SkSVGColorspace::kSRGB) {
66 filter = SkImageFilters::ColorFilter(SkColorFilters::LinearToSRGBGamma(), filter);
67 }
68
69 return filter;
70 }
71