Flutter Engine
The Flutter Engine
SkSGRenderNode.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2017 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
9
16#include "include/core/SkRect.h"
22
23namespace sksg {
24
25namespace {
26
27enum Flags : uint8_t {
28 kInvisible_Flag = 1 << 0,
29};
30
31} // namespace
32
33RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {}
34
36 return !(fNodeFlags & kInvisible_Flag);
37}
38
40 if (v == this->isVisible()) {
41 return;
42 }
43
44 this->invalidate();
45 fNodeFlags = v ? (fNodeFlags & ~kInvisible_Flag)
46 : (fNodeFlags | kInvisible_Flag);
47}
48
49void RenderNode::render(SkCanvas* canvas, const RenderContext* ctx) const {
50 SkASSERT(!this->hasInval());
51 if (this->isVisible() && !this->bounds().isEmpty()) {
52 this->onRender(canvas, ctx);
53 }
54 SkASSERT(!this->hasInval());
55}
56
57const RenderNode* RenderNode::nodeAt(const SkPoint& p) const {
58 return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr;
59}
60
61static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) {
62 return SkToU8(sk_float_round2int(alpha * opacity));
63}
64
66 const SkMatrix& base,
67 const SkMatrix& ctm) {
68 // Mask filters / shaders are declared to operate under a specific transform, but due to the
69 // deferral mechanism, other transformations might have been pushed to the state.
70 // We want to undo these transforms (T):
71 //
72 // baseCTM x T = ctm
73 //
74 // => T = Inv(baseCTM) x ctm
75 //
76 // => Inv(T) = Inv(Inv(baseCTM) x ctm)
77 //
78 // => Inv(T) = Inv(ctm) x baseCTM
79
80 SkMatrix lm;
81 if (base != ctm && ctm.invert(&lm)) {
82 lm.preConcat(base);
83 } else {
84 lm = SkMatrix::I();
85 }
86
87 // Note: this doesn't play ball with existing shader local matrices (what we really want is
88 // SkShader::makeWithPostLocalMatrix). Probably a good signal that the whole mechanism is
89 // contrived and should be redesigned (use SkCanvas::clipShader when available, drop shader
90 // "effects" completely, etc).
91 return shader->makeWithLocalMatrix(lm);
92}
93
95 // Note: fShader is never applied on isolation layers.
99 || fBlender;
100}
101
103 bool is_layer_paint) const {
104 paint->setAlpha(ScaleAlpha(paint->getAlpha(), fOpacity));
105 paint->setColorFilter(SkColorFilters::Compose(fColorFilter, paint->refColorFilter()));
106 if (fShader) {
107 paint->setShader(LocalShader(fShader, fShaderCTM, ctm));
108 }
109 if (fBlender) {
110 paint->setBlender(fBlender);
111 }
112
113 // Only apply the shader mask for regular paints. Isolation layers require
114 // special handling on restore.
115 if (!is_layer_paint && fMaskShader) {
117 LocalShader(fMaskShader, fMaskCTM, ctm),
118 paint->refShader()));
119 }
120}
121
123 : fCanvas(canvas)
124 , fCtx(ctx ? *ctx : RenderContext())
125 , fRestoreCount(canvas->getSaveCount()) {}
126
128 if (fRestoreCount >= 0) {
129 if (fMaskShader) {
130 SkPaint mask_paint;
132 mask_paint.setShader(std::move(fMaskShader));
133 fCanvas->drawPaint(mask_paint);
134 }
135 fCanvas->restoreToCount(fRestoreCount);
136 }
137}
138
141 SkASSERT(opacity >= 0 && opacity <= 1);
142 fCtx.fOpacity *= opacity;
143 return std::move(*this);
144}
145
148 fCtx.fColorFilter = SkColorFilters::Compose(std::move(fCtx.fColorFilter), std::move(cf));
149 return std::move(*this);
150}
151
154 // Topmost shader takes precedence.
155 if (!fCtx.fShader) {
156 fCtx.fShader = std::move(sh);
157 fCtx.fShaderCTM = shader_ctm;
158 }
159
160 return std::move(*this);
161}
162
165 if (fCtx.fMaskShader) {
166 // As we compose mask filters, use the relative transform T for the inner mask:
167 //
168 // maskCTM x T = ctm
169 //
170 // => T = Inv(maskCTM) x ctm
171 //
172 SkMatrix invMaskCTM;
173 if (ms && fCtx.fMaskCTM.invert(&invMaskCTM)) {
174 const auto relative_transform = SkMatrix::Concat(invMaskCTM, ctm);
175 fCtx.fMaskShader = SkShaders::Blend(SkBlendMode::kSrcIn,
176 std::move(fCtx.fMaskShader),
177 ms->makeWithLocalMatrix(relative_transform));
178 }
179 } else {
180 fCtx.fMaskShader = std::move(ms);
181 fCtx.fMaskCTM = ctm;
182 }
183
184 return std::move(*this);
185}
186
189 fCtx.fBlender = std::move(blender);
190 return std::move(*this);
191}
192
195 bool isolation) {
196 if (isolation && fCtx.requiresIsolation()) {
197 SkPaint layer_paint;
198 fCtx.modulatePaint(ctm, &layer_paint, /*is_layer_paint = */true);
199 fCanvas->saveLayer(bounds, &layer_paint);
200
201 // Fetch the mask shader for restore.
202 if (fCtx.fMaskShader) {
203 fMaskShader = LocalShader(fCtx.fMaskShader, fCtx.fMaskCTM, ctm);
204 }
205
206 // Reset only the props applied via isolation layers.
207 fCtx.fColorFilter = nullptr;
208 fCtx.fMaskShader = nullptr;
209 fCtx.fBlender = nullptr;
210 fCtx.fOpacity = 1;
211 }
212
213 return std::move(*this);
214}
215
218 sk_sp<SkImageFilter> filter) {
219 if (filter) {
220 SkPaint layer_paint;
221 fCtx.modulatePaint(ctm, &layer_paint);
222 // shaders and image filters are not composable, so we convert the shader to an
223 // image filter and blend them together
224 if (layer_paint.getShader()) {
225 filter = SkImageFilters::Blend(SkBlendMode::kSrcIn, std::move(filter),
226 SkImageFilters::Shader(layer_paint.refShader()));
227 }
228 SkASSERT(!layer_paint.getImageFilter());
229 layer_paint.setImageFilter(std::move(filter));
230 fCanvas->saveLayer(bounds, &layer_paint);
231 fCtx = RenderContext();
232 }
233
234 return std::move(*this);
235}
236
238 : INHERITED(kOverrideDamage_Trait) // We cannot make any assumptions - override conservatively.
239 , fChildren(std::move(children)) {
240 for (const auto& child : fChildren) {
241 this->observeInval(child);
242 }
243}
244
246 for (const auto& child : fChildren) {
247 this->unobserveInval(child);
248 }
249}
250
252 for (const auto& child : fChildren) {
253 if (NodePriv::HasInval(child)) {
254 return true;
255 }
256 }
257
258 return false;
259}
260
261} // namespace sksg
#define SkASSERT(cond)
Definition: SkAssert.h:116
@ kDstIn
r = d * sa
@ kSrcIn
r = s * da
uint8_t SkAlpha
Definition: SkColor.h:26
constexpr SkAlpha SK_AlphaOPAQUE
Definition: SkColor.h:94
#define sk_float_round2int(x)
constexpr uint8_t SkToU8(S x)
Definition: SkTo.h:22
static sk_sp< SkColorFilter > Compose(const sk_sp< SkColorFilter > &outer, sk_sp< SkColorFilter > inner)
Definition: SkColorFilter.h:92
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Shader(sk_sp< SkShader > shader, const CropRect &cropRect={})
static SkMatrix Concat(const SkMatrix &a, const SkMatrix &b)
Definition: SkMatrix.h:1775
bool invert(SkMatrix *inverse) const
Definition: SkMatrix.h:1206
static const SkMatrix & I()
Definition: SkMatrix.cpp:1544
SkMatrix & preConcat(const SkMatrix &other)
Definition: SkMatrix.cpp:674
sk_sp< SkShader > refShader() const
void setImageFilter(sk_sp< SkImageFilter > imageFilter)
void setShader(sk_sp< SkShader > shader)
void setBlendMode(SkBlendMode mode)
Definition: SkPaint.cpp:151
SkImageFilter * getImageFilter() const
Definition: SkPaint.h:564
SkShader * getShader() const
Definition: SkPaint.h:397
sk_sp< SkShader > makeWithLocalMatrix(const SkMatrix &) const
Definition: SkShader.cpp:26
CustomRenderNode(std::vector< sk_sp< RenderNode > > &&children)
static bool HasInval(const sk_sp< Node > &node)
Definition: SkSGNodePriv.h:19
friend class RenderNode
Definition: SkSGNode.h:94
void observeInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:61
void unobserveInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:84
const SkRect & bounds() const
Definition: SkSGNode.h:55
bool hasInval() const
Definition: SkSGNode.h:60
@ kOverrideDamage_Trait
Definition: SkSGNode.h:49
void invalidate(bool damage=true)
Definition: SkSGNode.cpp:113
ScopedRenderContext && setFilterIsolation(const SkRect &bounds, const SkMatrix &ctm, sk_sp< SkImageFilter >)
ScopedRenderContext && modulateOpacity(float opacity)
ScopedRenderContext && modulateMaskShader(sk_sp< SkShader >, const SkMatrix &ms_ctm)
ScopedRenderContext && setIsolation(const SkRect &bounds, const SkMatrix &ctm, bool do_isolate)
ScopedRenderContext(SkCanvas *, const RenderContext *)
ScopedRenderContext && modulateColorFilter(sk_sp< SkColorFilter >)
ScopedRenderContext && modulateBlender(sk_sp< SkBlender >)
ScopedRenderContext && modulateShader(sk_sp< SkShader >, const SkMatrix &shader_ctm)
void render(SkCanvas *, const RenderContext *=nullptr) const
virtual const RenderNode * onNodeAt(const SkPoint &p) const =0
bool isVisible() const
const RenderNode * nodeAt(const SkPoint &point) const
virtual void onRender(SkCanvas *, const RenderContext *) const =0
void setVisible(bool)
const Paint & paint
Definition: color_source.cc:38
SK_API sk_sp< SkShader > Blend(SkBlendMode mode, sk_sp< SkShader > dst, sk_sp< SkShader > src)
sh
Definition: run_sh.py:10
Definition: Skottie.h:32
static sk_sp< SkShader > LocalShader(const sk_sp< SkShader > &shader, const SkMatrix &base, const SkMatrix &ctm)
static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity)
Definition: ref_ptr.h:256
constexpr float x() const
Definition: SkPoint_impl.h:181
bool contains(SkScalar x, SkScalar y) const
Definition: extension.cpp:19
sk_sp< SkColorFilter > fColorFilter
void modulatePaint(const SkMatrix &ctm, SkPaint *, bool is_layer_paint=false) const