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/effects/SkImageFilters.h"
9 #include "modules/svg/include/SkSVGAttributeParser.h"
10 #include "modules/svg/include/SkSVGFeDisplacementMap.h"
11 #include "modules/svg/include/SkSVGFilterContext.h"
12 #include "modules/svg/include/SkSVGRenderContext.h"
13 #include "modules/svg/include/SkSVGValue.h"
14
parseAndSetAttribute(const char * name,const char * value)15 bool SkSVGFeDisplacementMap::parseAndSetAttribute(const char* name, const char* value) {
16 return INHERITED::parseAndSetAttribute(name, value) ||
17 this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) ||
18 this->setXChannelSelector(
19 SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
20 "xChannelSelector", name, value)) ||
21 this->setYChannelSelector(
22 SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
23 "yChannelSelector", name, value)) ||
24 this->setScale(SkSVGAttributeParser::parse<SkSVGNumberType>("scale", name, value));
25 }
26
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const27 sk_sp<SkImageFilter> SkSVGFeDisplacementMap::onMakeImageFilter(
28 const SkSVGRenderContext& ctx, const SkSVGFilterContext& fctx) const {
29 const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
30 const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
31
32 // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
33 // the 'in' source image must remain in its current colorspace.
34 sk_sp<SkImageFilter> in = fctx.resolveInput(ctx, this->getIn());
35 sk_sp<SkImageFilter> in2 = fctx.resolveInput(ctx, this->getIn2(), colorspace);
36
37 SkScalar scale = fScale;
38 if (fctx.primitiveUnits().type() == SkSVGObjectBoundingBoxUnits::Type::kObjectBoundingBox) {
39 const auto obbt = ctx.transformForCurrentOBB(fctx.primitiveUnits());
40 scale = SkSVGLengthContext({obbt.scale.x, obbt.scale.y})
41 .resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage),
42 SkSVGLengthContext::LengthType::kOther);
43 }
44
45 return SkImageFilters::DisplacementMap(
46 fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect);
47 }
48
resolveColorspace(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const49 SkSVGColorspace SkSVGFeDisplacementMap::resolveColorspace(const SkSVGRenderContext& ctx,
50 const SkSVGFilterContext& fctx) const {
51 // According to spec https://www.w3.org/TR/SVG11/filters.html#feDisplacementMapElement,
52 // the 'in' source image must remain in its current colorspace, which means the colorspace of
53 // this FE node is the same as the input.
54 return fctx.resolveInputColorspace(ctx, this->getIn());
55 }
56
57 template <>
parse(SkSVGFeDisplacementMap::ChannelSelector * channel)58 bool SkSVGAttributeParser::parse<SkSVGFeDisplacementMap::ChannelSelector>(
59 SkSVGFeDisplacementMap::ChannelSelector* channel) {
60 static constexpr std::tuple<const char*, SkSVGFeDisplacementMap::ChannelSelector> gMap[] = {
61 { "R", SkSVGFeDisplacementMap::ChannelSelector::kR },
62 { "G", SkSVGFeDisplacementMap::ChannelSelector::kG },
63 { "B", SkSVGFeDisplacementMap::ChannelSelector::kB },
64 { "A", SkSVGFeDisplacementMap::ChannelSelector::kA },
65 };
66
67 return this->parseEnumMap(gMap, channel) && this->parseEOSToken();
68 }
69