Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
third_party
skia
modules
sksg
src
SkSGMaskEffect.cpp
Go to the documentation of this file.
1
/*
2
* Copyright 2018 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/sksg/include/SkSGMaskEffect.h
"
9
10
#include "
include/core/SkBlendMode.h
"
11
#include "
include/core/SkCanvas.h
"
12
#include "
include/core/SkColorFilter.h
"
13
#include "
include/core/SkPaint.h
"
14
#include "
include/effects/SkLumaColorFilter.h
"
15
#include "
include/private/base/SkAssert.h
"
16
#include "
include/private/base/SkTo.h
"
17
#include "
modules/sksg/include/SkSGNode.h
"
18
19
class
SkMatrix
;
20
struct
SkPoint
;
21
22
namespace
sksg
{
23
24
static
bool
is_inverted
(
sksg::MaskEffect::Mode
mode) {
25
return
static_cast<
uint32_t
>
(mode) & 1;
26
}
27
28
static
bool
is_luma
(
sksg::MaskEffect::Mode
mode) {
29
return
static_cast<
uint32_t
>
(mode) & 2;
30
}
31
32
MaskEffect::MaskEffect
(
sk_sp<RenderNode>
child,
sk_sp<RenderNode>
mask,
Mode
mode)
33
:
INHERITED
(
std
::move(child))
34
, fMaskNode(
std
::move(mask))
35
, fMaskMode(mode) {
36
this->
observeInval
(fMaskNode);
37
}
38
39
MaskEffect::~MaskEffect
() {
40
this->
unobserveInval
(fMaskNode);
41
}
42
43
void
MaskEffect::onRender
(
SkCanvas
* canvas,
const
RenderContext
* ctx)
const
{
44
SkAutoCanvasRestore
acr(canvas,
false
);
45
46
// The mask mode covers two independent bits.
47
//
48
// - mask source controls how the mask coverage is generated:
49
// * alpha => coverage = mask_alpha
50
// * luma => coverage = luma(mask_rgb)
51
//
52
// - mask type controls how the mask coverage is interpreted:
53
// * normal => coverage' = coverage
54
// * inverted => coverage' = 1 - coverage
55
56
{
57
// Outer layer: mask coverage stored in the alpha channel.
58
SkPaint
mask_layer_paint;
59
if
(ctx) {
60
// Apply all optional context overrides upfront.
61
ctx->
modulatePaint
(canvas->
getTotalMatrix
(), &mask_layer_paint);
62
}
63
64
RenderContext
mask_render_context;
65
if
(
is_luma
(fMaskMode)) {
66
mask_render_context.
fColorFilter
=
SkLumaColorFilter::Make
();
67
}
68
69
// TODO: could be an A8 layer?
70
canvas->
saveLayer
(this->
bounds
(), &mask_layer_paint);
71
fMaskNode->render(canvas, &mask_render_context);
72
73
{
74
// Inner layer: masked content.
75
SkPaint
content_layer_paint;
76
content_layer_paint.
setBlendMode
(
is_inverted
(fMaskMode) ?
SkBlendMode::kSrcOut
77
:
SkBlendMode::kSrcIn
);
78
canvas->
saveLayer
(this->
bounds
(), &content_layer_paint);
79
80
this->
INHERITED::onRender
(canvas,
nullptr
);
81
}
82
}
83
}
84
85
const
RenderNode
*
MaskEffect::onNodeAt
(
const
SkPoint
& p)
const
{
86
const
auto
mask_hit = (
SkToBool
(fMaskNode->nodeAt(p)) == !
is_inverted
(fMaskMode));
87
88
if
(!mask_hit) {
89
return
nullptr
;
90
}
91
92
return
this->
INHERITED::onNodeAt
(p);
93
}
94
95
SkRect
MaskEffect::onRevalidate
(
InvalidationController
* ic,
const
SkMatrix
& ctm) {
96
SkASSERT
(this->
hasInval
());
97
98
const
auto
maskBounds = fMaskNode->revalidate(ic, ctm);
99
auto
childBounds = this->
INHERITED::onRevalidate
(ic, ctm);
100
101
return
(
is_inverted
(fMaskMode) || childBounds.intersect(maskBounds))
102
? childBounds
103
:
SkRect::MakeEmpty
();
104
}
105
106
}
// namespace sksg
SkAssert.h
SkASSERT
#define SkASSERT(cond)
Definition
SkAssert.h:116
SkBlendMode.h
SkBlendMode::kSrcOut
@ kSrcOut
r = s * (1-da)
SkBlendMode::kSrcIn
@ kSrcIn
r = s * da
SkCanvas.h
SkColorFilter.h
SkLumaColorFilter.h
SkPaint.h
SkSGMaskEffect.h
SkSGNode.h
SkTo.h
SkToBool
static constexpr bool SkToBool(const T &x)
Definition
SkTo.h:35
SkAutoCanvasRestore
Definition
SkCanvas.h:2645
SkCanvas
Definition
SkCanvas.h:106
SkCanvas::saveLayer
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition
SkCanvas.cpp:500
SkCanvas::getTotalMatrix
SkMatrix getTotalMatrix() const
Definition
SkCanvas.cpp:1953
SkMatrix
Definition
SkMatrix.h:54
SkPaint
Definition
SkPaint.h:44
SkPaint::setBlendMode
void setBlendMode(SkBlendMode mode)
Definition
SkPaint.cpp:151
sk_sp
Definition
SkRefCnt.h:220
sksg::EffectNode
Definition
SkSGEffectNode.h:30
sksg::EffectNode::onNodeAt
const RenderNode * onNodeAt(const SkPoint &) const override
Definition
SkSGEffectNode.cpp:35
sksg::EffectNode::onRender
void onRender(SkCanvas *, const RenderContext *) const override
Definition
SkSGEffectNode.cpp:31
sksg::EffectNode::onRevalidate
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
Definition
SkSGEffectNode.cpp:39
sksg::InvalidationController
Definition
SkSGInvalidationController.h:24
sksg::MaskEffect::onNodeAt
const RenderNode * onNodeAt(const SkPoint &) const override
Definition
SkSGMaskEffect.cpp:85
sksg::MaskEffect::MaskEffect
MaskEffect(sk_sp< RenderNode >, sk_sp< RenderNode > mask, Mode)
Definition
SkSGMaskEffect.cpp:32
sksg::MaskEffect::onRevalidate
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
Definition
SkSGMaskEffect.cpp:95
sksg::MaskEffect::~MaskEffect
~MaskEffect() override
Definition
SkSGMaskEffect.cpp:39
sksg::MaskEffect::Mode
Mode
Definition
SkSGMaskEffect.h:32
sksg::MaskEffect::onRender
void onRender(SkCanvas *, const RenderContext *) const override
Definition
SkSGMaskEffect.cpp:43
sksg::Node::observeInval
void observeInval(const sk_sp< Node > &)
Definition
SkSGNode.cpp:61
sksg::Node::unobserveInval
void unobserveInval(const sk_sp< Node > &)
Definition
SkSGNode.cpp:84
sksg::Node::bounds
const SkRect & bounds() const
Definition
SkSGNode.h:55
sksg::Node::hasInval
bool hasInval() const
Definition
SkSGNode.h:60
sksg::RenderNode
Definition
SkSGRenderNode.h:34
sksg
Definition
Skottie.h:32
sksg::is_inverted
static bool is_inverted(sksg::MaskEffect::Mode mode)
Definition
SkSGMaskEffect.cpp:24
sksg::is_luma
static bool is_luma(sksg::MaskEffect::Mode mode)
Definition
SkSGMaskEffect.cpp:28
std
Definition
ref_ptr.h:256
SkLumaColorFilter::Make
static sk_sp< SkColorFilter > Make()
Definition
SkRuntimeColorFilter.cpp:145
SkPoint
Definition
SkPoint_impl.h:163
SkRect
Definition
extension.cpp:13
SkRect::MakeEmpty
static constexpr SkRect MakeEmpty()
Definition
SkRect.h:595
sksg::RenderNode::RenderContext
Definition
SkSGRenderNode.h:60
sksg::RenderNode::RenderContext::fColorFilter
sk_sp< SkColorFilter > fColorFilter
Definition
SkSGRenderNode.h:61
sksg::RenderNode::RenderContext::modulatePaint
void modulatePaint(const SkMatrix &ctm, SkPaint *, bool is_layer_paint=false) const
Definition
SkSGRenderNode.cpp:101
Generated on Fri Apr 26 2024 06:16:24 for Flutter Engine by
1.9.8