| 1 |
/* |
| 2 |
* Copyright 2021 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/SkSVGMask.h" |
| 9 |
|
| 10 |
#include "include/core/SkCanvas.h" |
| 11 |
#include "include/core/SkColorFilter.h" |
| 12 |
#include "include/core/SkM44.h" |
| 13 |
#include "include/core/SkPaint.h" |
| 14 |
#include "include/effects/SkLumaColorFilter.h" |
| 15 |
#include "include/private/base/SkTArray.h" |
| 16 |
#include "modules/svg/include/SkSVGAttribute.h" |
| 17 |
#include "modules/svg/include/SkSVGAttributeParser.h" |
| 18 |
#include "modules/svg/include/SkSVGRenderContext.h" |
| 19 |
|
| 20 |
#include <utility> |
| 21 |
|
| 22 |
bool SkSVGMask::parseAndSetAttribute(const char* n, const char* v) { |
| 23 |
return INHERITED::parseAndSetAttribute(n, v) || |
| 24 |
this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) || |
| 25 |
this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) || |
| 26 |
this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) || |
| 27 |
this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) || |
| 28 |
this->setMaskUnits( |
| 29 |
SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>("maskUnits", n, v)) || |
| 30 |
this->setMaskContentUnits( |
| 31 |
SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>("maskContentUnits", n, v)); |
| 32 |
} |
| 33 |
|
| 34 |
SkRect SkSVGMask::bounds(const SkSVGRenderContext& ctx) const { |
| 35 |
return ctx.resolveOBBRect(fX, fY, fWidth, fHeight, fMaskUnits); |
| 36 |
} |
| 37 |
|
| 38 |
void SkSVGMask::renderMask(const SkSVGRenderContext& ctx) const { |
| 39 |
// https://www.w3.org/TR/SVG11/masking.html#Masking |
| 40 |
|
| 41 |
// Propagate any inherited properties that may impact mask effect behavior (e.g. |
| 42 |
// color-interpolation). We call this explicitly here because the SkSVGMask |
| 43 |
// nodes do not participate in the normal onRender path, which is when property |
| 44 |
// propagation currently occurs. |
| 45 |
// The local context also restores the filter layer created below on scope exit. |
| 46 |
SkSVGRenderContext lctx(ctx); |
| 47 |
this->onPrepareToRender(&lctx); |
| 48 |
|
| 49 |
const auto ci = *lctx.presentationContext().fInherited.fColorInterpolation; |
| 50 |
auto ci_filter = (ci == SkSVGColorspace::kLinearRGB) |
| 51 |
? SkColorFilters::SRGBToLinearGamma() |
| 52 |
: nullptr; |
| 53 |
|
| 54 |
SkPaint mask_filter; |
| 55 |
mask_filter.setColorFilter( |
| 56 |
SkColorFilters::Compose(SkLumaColorFilter::Make(), std::move(ci_filter))); |
| 57 |
|
| 58 |
// Mask color filter layer. |
| 59 |
// Note: We could avoid this extra layer if we invert the stacking order |
| 60 |
// (mask/content -> content/mask, kSrcIn -> kDstIn) and apply the filter |
| 61 |
// via the top (mask) layer paint. That requires deferring mask rendering |
| 62 |
// until after node content, which introduces extra state/complexity. |
| 63 |
// Something to consider if masking performance ever becomes an issue. |
| 64 |
lctx.canvas()->saveLayer(nullptr, &mask_filter); |
| 65 |
|
| 66 |
const auto obbt = ctx.transformForCurrentOBB(fMaskContentUnits); |
| 67 |
lctx.canvas()->translate(obbt.offset.x, obbt.offset.y); |
| 68 |
lctx.canvas()->scale(obbt.scale.x, obbt.scale.y); |
| 69 |
|
| 70 |
for (const auto& child : fChildren) { |
| 71 |
child->render(lctx); |
| 72 |
} |
| 73 |
} |