Coverage Report

Created: 2024-09-14 07:19

/src/skia/modules/svg/src/SkSVGMask.cpp
Line
Count
Source (jump to first uncovered line)
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
1.56k
bool SkSVGMask::parseAndSetAttribute(const char* n, const char* v) {
23
1.56k
    return INHERITED::parseAndSetAttribute(n, v) ||
24
1.56k
           this->setX(SkSVGAttributeParser::parse<SkSVGLength>("x", n, v)) ||
25
1.56k
           this->setY(SkSVGAttributeParser::parse<SkSVGLength>("y", n, v)) ||
26
1.56k
           this->setWidth(SkSVGAttributeParser::parse<SkSVGLength>("width", n, v)) ||
27
1.56k
           this->setHeight(SkSVGAttributeParser::parse<SkSVGLength>("height", n, v)) ||
28
1.56k
           this->setMaskUnits(
29
1.29k
                SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>("maskUnits", n, v)) ||
30
1.56k
           this->setMaskContentUnits(
31
1.29k
                SkSVGAttributeParser::parse<SkSVGObjectBoundingBoxUnits>("maskContentUnits", n, v));
32
1.56k
}
33
34
0
SkRect SkSVGMask::bounds(const SkSVGRenderContext& ctx) const {
35
0
    return ctx.resolveOBBRect(fX, fY, fWidth, fHeight, fMaskUnits);
36
0
}
37
38
0
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
0
    SkSVGRenderContext lctx(ctx);
47
0
    this->onPrepareToRender(&lctx);
48
49
0
    const auto ci = *lctx.presentationContext().fInherited.fColorInterpolation;
50
0
    auto ci_filter = (ci == SkSVGColorspace::kLinearRGB)
51
0
            ? SkColorFilters::SRGBToLinearGamma()
52
0
            : nullptr;
53
54
0
    SkPaint mask_filter;
55
0
    mask_filter.setColorFilter(
56
0
                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
0
    lctx.canvas()->saveLayer(nullptr, &mask_filter);
65
66
0
    const auto obbt = ctx.transformForCurrentOBB(fMaskContentUnits);
67
0
    lctx.canvas()->translate(obbt.offset.x, obbt.offset.y);
68
0
    lctx.canvas()->scale(obbt.scale.x, obbt.scale.y);
69
70
0
    for (const auto& child : fChildren) {
71
0
        child->render(lctx);
72
0
    }
73
0
}