blob: 9698a55ce539b8ee3e660ffdceadfa5d2e50730c [file] [log] [blame]
Tyler Dennistonf005c8a2021-01-25 12:56:13 -05001/*
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
15bool 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
27sk_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) {
Florin Malita3010f3d2021-04-30 10:48:42 -040039 const auto obbt = ctx.transformForCurrentOBB(fctx.primitiveUnits());
40 scale = SkSVGLengthContext({obbt.scale.x, obbt.scale.y})
41 .resolve(SkSVGLength(scale, SkSVGLength::Unit::kPercentage),
Tyler Dennistonf005c8a2021-01-25 12:56:13 -050042 SkSVGLengthContext::LengthType::kOther);
43 }
44
45 return SkImageFilters::DisplacementMap(
46 fXChannelSelector, fYChannelSelector, scale, in2, in, cropRect);
47}
48
49SkSVGColorspace 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
57template <>
58bool 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}