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
15namespace sksg {
16
17namespace {
18
19enum Flags : uint8_t {
20 kInvisible_Flag = 1 << 0,
21};
22
23} // namespace
24
25RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {}
26
27bool RenderNode::isVisible() const {
28 return !(fNodeFlags & kInvisible_Flag);
29}
30
31void 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
41void 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
49const RenderNode* RenderNode::nodeAt(const SkPoint& p) const {
50 return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr;
51}
52
53static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) {
54 return SkToU8(sk_float_round2int(alpha * opacity));
55}
56
57static 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
86bool 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 || fBlendMode != SkBlendMode::kSrcOver;
92}
93
94void 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 paint->setBlendMode(fBlendMode);
102
103 // Only apply the shader mask for regular paints. Isolation layers require
104 // special handling on restore.
105 if (!is_layer_paint && fMaskShader) {
106 paint->setShader(SkShaders::Blend(SkBlendMode::kSrcIn,
107 LocalShader(fMaskShader, fMaskCTM, ctm),
108 paint->refShader()));
109 }
110}
111
112RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx)
113 : fCanvas(canvas)
114 , fCtx(ctx ? *ctx : RenderContext())
115 , fRestoreCount(canvas->getSaveCount()) {}
116
117RenderNode::ScopedRenderContext::~ScopedRenderContext() {
118 if (fRestoreCount >= 0) {
119 if (fMaskShader) {
120 SkPaint mask_paint;
121 mask_paint.setBlendMode(SkBlendMode::kDstIn);
122 mask_paint.setShader(std::move(fMaskShader));
123 fCanvas->drawPaint(mask_paint);
124 }
125 fCanvas->restoreToCount(fRestoreCount);
126 }
127}
128
129RenderNode::ScopedRenderContext&&
130RenderNode::ScopedRenderContext::modulateOpacity(float opacity) {
131 SkASSERT(opacity >= 0 && opacity <= 1);
132 fCtx.fOpacity *= opacity;
133 return std::move(*this);
134}
135
136RenderNode::ScopedRenderContext&&
137RenderNode::ScopedRenderContext::modulateColorFilter(sk_sp<SkColorFilter> cf) {
138 fCtx.fColorFilter = SkColorFilters::Compose(std::move(fCtx.fColorFilter), std::move(cf));
139 return std::move(*this);
140}
141
142RenderNode::ScopedRenderContext&&
143RenderNode::ScopedRenderContext::modulateShader(sk_sp<SkShader> sh, const SkMatrix& shader_ctm) {
144 // Topmost shader takes precedence.
145 if (!fCtx.fShader) {
146 fCtx.fShader = std::move(sh);
147 fCtx.fShaderCTM = shader_ctm;
148 }
149
150 return std::move(*this);
151}
152
153RenderNode::ScopedRenderContext&&
154RenderNode::ScopedRenderContext::modulateMaskShader(sk_sp<SkShader> ms, const SkMatrix& ctm) {
155 if (fCtx.fMaskShader) {
156 // As we compose mask filters, use the relative transform T for the inner mask:
157 //
158 // maskCTM x T = ctm
159 //
160 // => T = Inv(maskCTM) x ctm
161 //
162 SkMatrix invMaskCTM;
163 if (ms && fCtx.fMaskCTM.invert(&invMaskCTM)) {
164 const auto relative_transform = SkMatrix::Concat(invMaskCTM, ctm);
165 fCtx.fMaskShader = SkShaders::Blend(SkBlendMode::kSrcIn,
166 std::move(fCtx.fMaskShader),
167 ms->makeWithLocalMatrix(relative_transform));
168 }
169 } else {
170 fCtx.fMaskShader = std::move(ms);
171 fCtx.fMaskCTM = ctm;
172 }
173
174 return std::move(*this);
175}
176
177RenderNode::ScopedRenderContext&&
178RenderNode::ScopedRenderContext::modulateBlendMode(SkBlendMode mode) {
179 fCtx.fBlendMode = mode;
180 return std::move(*this);
181}
182
183RenderNode::ScopedRenderContext&&
184RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, const SkMatrix& ctm,
185 bool isolation) {
186 if (isolation && fCtx.requiresIsolation()) {
187 SkPaint layer_paint;
188 fCtx.modulatePaint(ctm, &layer_paint, /*is_layer_paint = */true);
189 fCanvas->saveLayer(bounds, &layer_paint);
190
191 // Fetch the mask shader for restore.
192 if (fCtx.fMaskShader) {
193 fMaskShader = LocalShader(fCtx.fMaskShader, fCtx.fMaskCTM, ctm);
194 }
195
196 // Reset only the props applied via isolation layers.
197 fCtx.fColorFilter = nullptr;
198 fCtx.fMaskShader = nullptr;
199 fCtx.fOpacity = 1;
200 fCtx.fBlendMode = SkBlendMode::kSrcOver;
201 }
202
203 return std::move(*this);
204}
205
206RenderNode::ScopedRenderContext&&
207RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm,
208 sk_sp<SkImageFilter> filter) {
209 SkPaint layer_paint;
210 fCtx.modulatePaint(ctm, &layer_paint);
211
212 SkASSERT(!layer_paint.getImageFilter());
213 layer_paint.setImageFilter(std::move(filter));
214 fCanvas->saveLayer(bounds, &layer_paint);
215 fCtx = RenderContext();
216
217 return std::move(*this);
218}
219
220CustomRenderNode::CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children)
221 : INHERITED(kOverrideDamage_Trait) // We cannot make any assumptions - override conservatively.
222 , fChildren(std::move(children)) {
223 for (const auto& child : fChildren) {
224 this->observeInval(child);
225 }
226}
227
228CustomRenderNode::~CustomRenderNode() {
229 for (const auto& child : fChildren) {
230 this->unobserveInval(child);
231 }
232}
233
234bool CustomRenderNode::hasChildrenInval() const {
235 for (const auto& child : fChildren) {
236 if (NodePriv::HasInval(child)) {
237 return true;
238 }
239 }
240
241 return false;
242}
243
244} // namespace sksg
245