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/SkSVGFeComposite.h"
11 #include "modules/svg/include/SkSVGFilterContext.h"
12 #include "modules/svg/include/SkSVGRenderContext.h"
13
parseAndSetAttribute(const char * name,const char * value)14 bool SkSVGFeComposite::parseAndSetAttribute(const char* name, const char* value) {
15 return INHERITED::parseAndSetAttribute(name, value) ||
16 // SkSVGFeInputType parsing defined in SkSVGFe.cpp:
17 this->setIn2(SkSVGAttributeParser::parse<SkSVGFeInputType>("in2", name, value)) ||
18 this->setK1(SkSVGAttributeParser::parse<SkSVGNumberType>("k1", name, value)) ||
19 this->setK2(SkSVGAttributeParser::parse<SkSVGNumberType>("k2", name, value)) ||
20 this->setK3(SkSVGAttributeParser::parse<SkSVGNumberType>("k3", name, value)) ||
21 this->setK4(SkSVGAttributeParser::parse<SkSVGNumberType>("k4", name, value)) ||
22 this->setOperator(
23 SkSVGAttributeParser::parse<SkSVGFeCompositeOperator>("operator", name, value));
24 }
25
BlendModeForOperator(SkSVGFeCompositeOperator op)26 SkBlendMode SkSVGFeComposite::BlendModeForOperator(SkSVGFeCompositeOperator op) {
27 switch (op) {
28 case SkSVGFeCompositeOperator::kOver:
29 return SkBlendMode::kSrcOver;
30 case SkSVGFeCompositeOperator::kIn:
31 return SkBlendMode::kSrcIn;
32 case SkSVGFeCompositeOperator::kOut:
33 return SkBlendMode::kSrcOut;
34 case SkSVGFeCompositeOperator::kAtop:
35 return SkBlendMode::kSrcATop;
36 case SkSVGFeCompositeOperator::kXor:
37 return SkBlendMode::kXor;
38 case SkSVGFeCompositeOperator::kArithmetic:
39 // Arithmetic is not handled with a blend
40 SkASSERT(false);
41 return SkBlendMode::kSrcOver;
42 }
43
44 SkUNREACHABLE;
45 }
46
onMakeImageFilter(const SkSVGRenderContext & ctx,const SkSVGFilterContext & fctx) const47 sk_sp<SkImageFilter> SkSVGFeComposite::onMakeImageFilter(const SkSVGRenderContext& ctx,
48 const SkSVGFilterContext& fctx) const {
49 const SkRect cropRect = this->resolveFilterSubregion(ctx, fctx);
50 const SkSVGColorspace colorspace = this->resolveColorspace(ctx, fctx);
51 const sk_sp<SkImageFilter> background = fctx.resolveInput(ctx, fIn2, colorspace);
52 const sk_sp<SkImageFilter> foreground = fctx.resolveInput(ctx, this->getIn(), colorspace);
53 if (fOperator == SkSVGFeCompositeOperator::kArithmetic) {
54 constexpr bool enforcePMColor = true;
55 return SkImageFilters::Arithmetic(
56 fK1, fK2, fK3, fK4, enforcePMColor, background, foreground, cropRect);
57 } else {
58 return SkImageFilters::Blend(
59 BlendModeForOperator(fOperator), background, foreground, cropRect);
60 }
61 }
62
parse(SkSVGFeCompositeOperator * op)63 template <> bool SkSVGAttributeParser::parse(SkSVGFeCompositeOperator* op) {
64 static constexpr std::tuple<const char*, SkSVGFeCompositeOperator> gOpMap[] = {
65 {"over", SkSVGFeCompositeOperator::kOver},
66 {"in", SkSVGFeCompositeOperator::kIn},
67 {"out", SkSVGFeCompositeOperator::kOut},
68 {"atop", SkSVGFeCompositeOperator::kAtop},
69 {"xor", SkSVGFeCompositeOperator::kXor},
70 {"arithmetic", SkSVGFeCompositeOperator::kArithmetic},
71 };
72
73 return this->parseEnumMap(gOpMap, op) && this->parseEOSToken();
74 }
75