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
8 #include "modules/sksg/include/SkSGRenderNode.h"
9
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkPaint.h"
13 #include "modules/sksg/src/SkSGNodePriv.h"
14
15 namespace sksg {
16
17 namespace {
18
19 enum Flags : uint8_t {
20 kInvisible_Flag = 1 << 0,
21 };
22
23 } // namespace
24
RenderNode(uint32_t inval_traits)25 RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {}
26
isVisible() const27 bool RenderNode::isVisible() const {
28 return !(fNodeFlags & kInvisible_Flag);
29 }
30
setVisible(bool v)31 void RenderNode::setVisible(bool v) {
32 if (v == this->isVisible()) {
33 return;
34 }
35
36 this->invalidate();
37 fNodeFlags = v ? (fNodeFlags & ~kInvisible_Flag)
38 : (fNodeFlags | kInvisible_Flag);
39 }
40
render(SkCanvas * canvas,const RenderContext * ctx) const41 void RenderNode::render(SkCanvas* canvas, const RenderContext* ctx) const {
42 SkASSERT(!this->hasInval());
43 if (this->isVisible() && !this->bounds().isEmpty()) {
44 this->onRender(canvas, ctx);
45 }
46 SkASSERT(!this->hasInval());
47 }
48
nodeAt(const SkPoint & p) const49 const RenderNode* RenderNode::nodeAt(const SkPoint& p) const {
50 return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr;
51 }
52
ScaleAlpha(SkAlpha alpha,float opacity)53 static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) {
54 return SkToU8(sk_float_round2int(alpha * opacity));
55 }
56
LocalShader(const sk_sp<SkShader> shader,const SkMatrix & base,const SkMatrix & ctm)57 static sk_sp<SkShader> LocalShader(const sk_sp<SkShader> shader,
58 const SkMatrix& base,
59 const SkMatrix& ctm) {
60 // Mask filters / shaders are declared to operate under a specific transform, but due to the
61 // deferral mechanism, other transformations might have been pushed to the state.
62 // We want to undo these transforms (T):
63 //
64 // baseCTM x T = ctm
65 //
66 // => T = Inv(baseCTM) x ctm
67 //
68 // => Inv(T) = Inv(Inv(baseCTM) x ctm)
69 //
70 // => Inv(T) = Inv(ctm) x baseCTM
71
72 SkMatrix lm;
73 if (base != ctm && ctm.invert(&lm)) {
74 lm.preConcat(base);
75 } else {
76 lm = SkMatrix::I();
77 }
78
79 // Note: this doesn't play ball with existing shader local matrices (what we really want is
80 // SkShader::makeWithPostLocalMatrix). Probably a good signal that the whole mechanism is
81 // contrived and should be redesigned (use SkCanvas::clipShader when available, drop shader
82 // "effects" completely, etc).
83 return shader->makeWithLocalMatrix(lm);
84 }
85
requiresIsolation() const86 bool RenderNode::RenderContext::requiresIsolation() const {
87 // Note: fShader is never applied on isolation layers.
88 return ScaleAlpha(SK_AlphaOPAQUE, fOpacity) != SK_AlphaOPAQUE
89 || fColorFilter
90 || fMaskShader
91 || fBlender;
92 }
93
modulatePaint(const SkMatrix & ctm,SkPaint * paint,bool is_layer_paint) const94 void RenderNode::RenderContext::modulatePaint(const SkMatrix& ctm, SkPaint* paint,
95 bool is_layer_paint) const {
96 paint->setAlpha(ScaleAlpha(paint->getAlpha(), fOpacity));
97 paint->setColorFilter(SkColorFilters::Compose(fColorFilter, paint->refColorFilter()));
98 if (fShader) {
99 paint->setShader(LocalShader(fShader, fShaderCTM, ctm));
100 }
101 if (fBlender) {
102 paint->setBlender(fBlender);
103 }
104
105 // Only apply the shader mask for regular paints. Isolation layers require
106 // special handling on restore.
107 if (!is_layer_paint && fMaskShader) {
108 paint->setShader(SkShaders::Blend(SkBlendMode::kSrcIn,
109 LocalShader(fMaskShader, fMaskCTM, ctm),
110 paint->refShader()));
111 }
112 }
113
ScopedRenderContext(SkCanvas * canvas,const RenderContext * ctx)114 RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx)
115 : fCanvas(canvas)
116 , fCtx(ctx ? *ctx : RenderContext())
117 , fRestoreCount(canvas->getSaveCount()) {}
118
~ScopedRenderContext()119 RenderNode::ScopedRenderContext::~ScopedRenderContext() {
120 if (fRestoreCount >= 0) {
121 if (fMaskShader) {
122 SkPaint mask_paint;
123 mask_paint.setBlendMode(SkBlendMode::kDstIn);
124 mask_paint.setShader(std::move(fMaskShader));
125 fCanvas->drawPaint(mask_paint);
126 }
127 fCanvas->restoreToCount(fRestoreCount);
128 }
129 }
130
131 RenderNode::ScopedRenderContext&&
modulateOpacity(float opacity)132 RenderNode::ScopedRenderContext::modulateOpacity(float opacity) {
133 SkASSERT(opacity >= 0 && opacity <= 1);
134 fCtx.fOpacity *= opacity;
135 return std::move(*this);
136 }
137
138 RenderNode::ScopedRenderContext&&
modulateColorFilter(sk_sp<SkColorFilter> cf)139 RenderNode::ScopedRenderContext::modulateColorFilter(sk_sp<SkColorFilter> cf) {
140 fCtx.fColorFilter = SkColorFilters::Compose(std::move(fCtx.fColorFilter), std::move(cf));
141 return std::move(*this);
142 }
143
144 RenderNode::ScopedRenderContext&&
modulateShader(sk_sp<SkShader> sh,const SkMatrix & shader_ctm)145 RenderNode::ScopedRenderContext::modulateShader(sk_sp<SkShader> sh, const SkMatrix& shader_ctm) {
146 // Topmost shader takes precedence.
147 if (!fCtx.fShader) {
148 fCtx.fShader = std::move(sh);
149 fCtx.fShaderCTM = shader_ctm;
150 }
151
152 return std::move(*this);
153 }
154
155 RenderNode::ScopedRenderContext&&
modulateMaskShader(sk_sp<SkShader> ms,const SkMatrix & ctm)156 RenderNode::ScopedRenderContext::modulateMaskShader(sk_sp<SkShader> ms, const SkMatrix& ctm) {
157 if (fCtx.fMaskShader) {
158 // As we compose mask filters, use the relative transform T for the inner mask:
159 //
160 // maskCTM x T = ctm
161 //
162 // => T = Inv(maskCTM) x ctm
163 //
164 SkMatrix invMaskCTM;
165 if (ms && fCtx.fMaskCTM.invert(&invMaskCTM)) {
166 const auto relative_transform = SkMatrix::Concat(invMaskCTM, ctm);
167 fCtx.fMaskShader = SkShaders::Blend(SkBlendMode::kSrcIn,
168 std::move(fCtx.fMaskShader),
169 ms->makeWithLocalMatrix(relative_transform));
170 }
171 } else {
172 fCtx.fMaskShader = std::move(ms);
173 fCtx.fMaskCTM = ctm;
174 }
175
176 return std::move(*this);
177 }
178
179 RenderNode::ScopedRenderContext&&
modulateBlender(sk_sp<SkBlender> blender)180 RenderNode::ScopedRenderContext::modulateBlender(sk_sp<SkBlender> blender) {
181 fCtx.fBlender = std::move(blender);
182 return std::move(*this);
183 }
184
185 RenderNode::ScopedRenderContext&&
setIsolation(const SkRect & bounds,const SkMatrix & ctm,bool isolation)186 RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, const SkMatrix& ctm,
187 bool isolation) {
188 if (isolation && fCtx.requiresIsolation()) {
189 SkPaint layer_paint;
190 fCtx.modulatePaint(ctm, &layer_paint, /*is_layer_paint = */true);
191 fCanvas->saveLayer(bounds, &layer_paint);
192
193 // Fetch the mask shader for restore.
194 if (fCtx.fMaskShader) {
195 fMaskShader = LocalShader(fCtx.fMaskShader, fCtx.fMaskCTM, ctm);
196 }
197
198 // Reset only the props applied via isolation layers.
199 fCtx.fColorFilter = nullptr;
200 fCtx.fMaskShader = nullptr;
201 fCtx.fBlender = nullptr;
202 fCtx.fOpacity = 1;
203 }
204
205 return std::move(*this);
206 }
207
208 RenderNode::ScopedRenderContext&&
setFilterIsolation(const SkRect & bounds,const SkMatrix & ctm,sk_sp<SkImageFilter> filter)209 RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm,
210 sk_sp<SkImageFilter> filter) {
211 if (filter) {
212 SkPaint layer_paint;
213 fCtx.modulatePaint(ctm, &layer_paint);
214
215 SkASSERT(!layer_paint.getImageFilter());
216 layer_paint.setImageFilter(std::move(filter));
217 fCanvas->saveLayer(bounds, &layer_paint);
218 fCtx = RenderContext();
219 }
220
221 return std::move(*this);
222 }
223
CustomRenderNode(std::vector<sk_sp<RenderNode>> && children)224 CustomRenderNode::CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children)
225 : INHERITED(kOverrideDamage_Trait) // We cannot make any assumptions - override conservatively.
226 , fChildren(std::move(children)) {
227 for (const auto& child : fChildren) {
228 this->observeInval(child);
229 }
230 }
231
~CustomRenderNode()232 CustomRenderNode::~CustomRenderNode() {
233 for (const auto& child : fChildren) {
234 this->unobserveInval(child);
235 }
236 }
237
hasChildrenInval() const238 bool CustomRenderNode::hasChildrenInval() const {
239 for (const auto& child : fChildren) {
240 if (NodePriv::HasInval(child)) {
241 return true;
242 }
243 }
244
245 return false;
246 }
247
248 } // namespace sksg
249