| 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/SkSVGFilterContext.h" |
| 9 |
|
| 10 |
#include "include/core/SkBlendMode.h" |
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkColorFilter.h" |
| 13 |
#include "include/core/SkColorSpace.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkShader.h" |
| 16 |
#include "include/effects/SkColorMatrix.h" |
| 17 |
#include "include/effects/SkImageFilters.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "include/private/base/SkDebug.h" |
| 20 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 21 |
#include "modules/svg/include/SkSVGTypes.h" |
| 22 |
|
| 23 |
#include <utility> |
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
sk_sp<SkImageFilter> ConvertFilterColorspace(sk_sp<SkImageFilter>&& input, |
| 28 |
SkSVGColorspace src, |
| 29 |
SkSVGColorspace dst) { |
| 30 |
if (src == dst) { |
| 31 |
return std::move(input); |
| 32 |
} else if (src == SkSVGColorspace::kSRGB && dst == SkSVGColorspace::kLinearRGB) { |
| 33 |
return SkImageFilters::ColorFilter(SkColorFilters::SRGBToLinearGamma(), input); |
| 34 |
} else { |
| 35 |
SkASSERT(src == SkSVGColorspace::kLinearRGB && dst == SkSVGColorspace::kSRGB); |
| 36 |
return SkImageFilters::ColorFilter(SkColorFilters::LinearToSRGBGamma(), input); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
sk_sp<SkShader> paint_as_shader(const SkPaint& paint) { |
| 41 |
sk_sp<SkShader> shader = paint.refShader(); |
| 42 |
auto color = paint.getColor4f(); |
| 43 |
if (shader && color.fA < 1.f) { |
| 44 |
// Multiply by paint alpha |
| 45 |
shader = shader->makeWithColorFilter( |
| 46 |
SkColorFilters::Blend(color, /*colorSpace=*/nullptr, SkBlendMode::kDstIn)); |
| 47 |
} else if (!shader) { |
| 48 |
shader = SkShaders::Color(color, /*colorSpace=*/nullptr); |
| 49 |
} |
| 50 |
if (paint.getColorFilter()) { |
| 51 |
shader = shader->makeWithColorFilter(paint.refColorFilter()); |
| 52 |
} |
| 53 |
return shader; |
| 54 |
} |
| 55 |
|
| 56 |
} // namespace |
| 57 |
|
| 58 |
const SkSVGFilterContext::Result* SkSVGFilterContext::findResultById( |
| 59 |
const SkSVGStringType& id) const { |
| 60 |
return fResults.find(id); |
| 61 |
} |
| 62 |
|
| 63 |
const SkRect& SkSVGFilterContext::filterPrimitiveSubregion(const SkSVGFeInputType& input) const { |
| 64 |
const Result* res = nullptr; |
| 65 |
if (input.type() == SkSVGFeInputType::Type::kFilterPrimitiveReference) { |
| 66 |
res = fResults.find(input.id()); |
| 67 |
} else if (input.type() == SkSVGFeInputType::Type::kUnspecified) { |
| 68 |
res = &fPreviousResult; |
| 69 |
} |
| 70 |
return res ? res->fFilterSubregion : fFilterEffectsRegion; |
| 71 |
} |
| 72 |
|
| 73 |
void SkSVGFilterContext::registerResult(const SkSVGStringType& id, |
| 74 |
const sk_sp<SkImageFilter>& result, |
| 75 |
const SkRect& subregion, |
| 76 |
SkSVGColorspace resultColorspace) { |
| 77 |
SkASSERT(!id.isEmpty()); |
| 78 |
fResults[id] = {result, subregion, resultColorspace}; |
| 79 |
} |
| 80 |
|
| 81 |
void SkSVGFilterContext::setPreviousResult(const sk_sp<SkImageFilter>& result, |
| 82 |
const SkRect& subregion, |
| 83 |
SkSVGColorspace resultColorspace) { |
| 84 |
fPreviousResult = {result, subregion, resultColorspace}; |
| 85 |
} |
| 86 |
|
| 87 |
bool SkSVGFilterContext::previousResultIsSourceGraphic() const { |
| 88 |
return fPreviousResult.fImageFilter == nullptr; |
| 89 |
} |
| 90 |
|
| 91 |
// https://www.w3.org/TR/SVG11/filters.html#FilterPrimitiveInAttribute |
| 92 |
std::tuple<sk_sp<SkImageFilter>, SkSVGColorspace> SkSVGFilterContext::getInput( |
| 93 |
const SkSVGRenderContext& ctx, const SkSVGFeInputType& inputType) const { |
| 94 |
SkSVGColorspace inputCS = SkSVGColorspace::kSRGB; |
| 95 |
sk_sp<SkImageFilter> result; |
| 96 |
switch (inputType.type()) { |
| 97 |
case SkSVGFeInputType::Type::kSourceAlpha: { |
| 98 |
SkColorMatrix m; |
| 99 |
m.setScale(0, 0, 0, 1.0f); |
| 100 |
result = SkImageFilters::ColorFilter(SkColorFilters::Matrix(m), nullptr); |
| 101 |
break; |
| 102 |
} |
| 103 |
case SkSVGFeInputType::Type::kSourceGraphic: |
| 104 |
// Do nothing. |
| 105 |
break; |
| 106 |
case SkSVGFeInputType::Type::kFillPaint: { |
| 107 |
const auto& fillPaint = ctx.fillPaint(); |
| 108 |
if (fillPaint.has_value()) { |
| 109 |
auto dither = fillPaint->isDither() ? SkImageFilters::Dither::kYes |
| 110 |
: SkImageFilters::Dither::kNo; |
| 111 |
result = SkImageFilters::Shader(paint_as_shader(*fillPaint), dither); |
| 112 |
} |
| 113 |
break; |
| 114 |
} |
| 115 |
case SkSVGFeInputType::Type::kStrokePaint: { |
| 116 |
// The paint filter doesn't apply fill/stroke styling, but use the paint settings |
| 117 |
// defined for strokes. |
| 118 |
const auto& strokePaint = ctx.strokePaint(); |
| 119 |
if (strokePaint.has_value()) { |
| 120 |
auto dither = strokePaint->isDither() ? SkImageFilters::Dither::kYes |
| 121 |
: SkImageFilters::Dither::kNo; |
| 122 |
result = SkImageFilters::Shader(paint_as_shader(*strokePaint), dither); |
| 123 |
} |
| 124 |
break; |
| 125 |
} |
| 126 |
case SkSVGFeInputType::Type::kFilterPrimitiveReference: { |
| 127 |
const Result* res = findResultById(inputType.id()); |
| 128 |
if (res) { |
| 129 |
result = res->fImageFilter; |
| 130 |
inputCS = res->fColorspace; |
| 131 |
} |
| 132 |
break; |
| 133 |
} |
| 134 |
case SkSVGFeInputType::Type::kUnspecified: { |
| 135 |
result = fPreviousResult.fImageFilter; |
| 136 |
inputCS = fPreviousResult.fColorspace; |
| 137 |
break; |
| 138 |
} |
| 139 |
default: |
| 140 |
SkDEBUGF("unhandled filter input type %d\n", (int)inputType.type()); |
| 141 |
break; |
| 142 |
} |
| 143 |
|
| 144 |
return {result, inputCS}; |
| 145 |
} |
| 146 |
|
| 147 |
SkSVGColorspace SkSVGFilterContext::resolveInputColorspace( |
| 148 |
const SkSVGRenderContext& ctx, const SkSVGFeInputType& inputType) const { |
| 149 |
return std::get<1>(this->getInput(ctx, inputType)); |
| 150 |
} |
| 151 |
|
| 152 |
sk_sp<SkImageFilter> SkSVGFilterContext::resolveInput(const SkSVGRenderContext& ctx, |
| 153 |
const SkSVGFeInputType& inputType) const { |
| 154 |
return std::get<0>(this->getInput(ctx, inputType)); |
| 155 |
} |
| 156 |
|
| 157 |
sk_sp<SkImageFilter> SkSVGFilterContext::resolveInput(const SkSVGRenderContext& ctx, |
| 158 |
const SkSVGFeInputType& inputType, |
| 159 |
SkSVGColorspace colorspace) const { |
| 160 |
auto [result, inputCS] = this->getInput(ctx, inputType); |
| 161 |
return ConvertFilterColorspace(std::move(result), inputCS, colorspace); |
| 162 |
} |