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/SkSVGFeBlend.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 SkSVGFeBlend::parseAndSetAttribute(const char* name, const char* value) {
16 return INHERITED::parseAndSetAttribute(name, value) ||
17 this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) ||
18 this->setMode(SkSVGAttributeParser::parse<SkSVGFeBlend::Mode>("mode", name, value));
19 }
20
GetBlendMode(SkSVGFeBlend::Mode mode)21 static SkBlendMode GetBlendMode(SkSVGFeBlend::Mode mode) {
22 switch (mode) {
23 case SkSVGFeBlend::Mode::kNormal:
24 return SkBlendMode::kSrcOver;
25 case SkSVGFeBlend::Mode::kMultiply:
26 return SkBlendMode::kMultiply;
27 case SkSVGFeBlend::Mode::kScreen:
28 return SkBlendMode::kScreen;
29 case SkSVGFeBlend::Mode::kDarken:
30 return SkBlendMode::kDarken;
31 case SkSVGFeBlend::Mode::kLighten:
32 return SkBlendMode::kLighten;
33 }
34
35 SkUNREACHABLE;
36 }
37
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const38 sk_sp<SkImageFilter> SkSVGFeBlend::onMakeImageFilter(const SkSVGRenderContext& ctx,
39 const SkSVGFilterContext& fctx) const {
40 const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
41 const SkBlendMode blendMode = GetBlendMode(this->getMode());
42 const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
43 const sk_sp<SkImageFilter> background = fctx.resolveInput(ctx, fIn2, colorspace);
44 const sk_sp<SkImageFilter> foreground = fctx.resolveInput(ctx, this->getIn(), colorspace);
45 return SkImageFilters::Blend(blendMode, background, foreground, cropRect);
46 }
47
48 template <>
parse(SkSVGFeBlend::Mode * mode)49 bool SkSVGAttributeParser::parse<SkSVGFeBlend::Mode>(
50 SkSVGFeBlend::Mode* mode) {
51 static constexpr std::tuple<const char*, SkSVGFeBlend::Mode> gMap[] = {
52 { "normal" , SkSVGFeBlend::Mode::kNormal },
53 { "multiply", SkSVGFeBlend::Mode::kMultiply },
54 { "screen" , SkSVGFeBlend::Mode::kScreen },
55 { "darken" , SkSVGFeBlend::Mode::kDarken },
56 { "lighten" , SkSVGFeBlend::Mode::kLighten },
57 };
58
59 return this->parseEnumMap(gMap, mode) && this->parseEOSToken();
60 }
61