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