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