2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "modules/sksg/include/SkSGRenderNode.h"
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"
19 enum Flags : uint8_t {
20 kInvisible_Flag = 1 << 0,
25 RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {}
27 bool RenderNode::isVisible() const {
28 return !(fNodeFlags & kInvisible_Flag);
31 void RenderNode::setVisible(bool v) {
32 if (v == this->isVisible()) {
37 fNodeFlags = v ? (fNodeFlags & ~kInvisible_Flag)
38 : (fNodeFlags | kInvisible_Flag);
41 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);
46 SkASSERT(!this->hasInval());
49 const RenderNode* RenderNode::nodeAt(const SkPoint& p) const {
50 return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr;
53 static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) {
54 return SkToU8(sk_float_round2int(alpha * opacity));
57 static sk_sp<SkShader> LocalShader(const sk_sp<SkShader> shader,
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):
66 // => T = Inv(baseCTM) x ctm
68 // => Inv(T) = Inv(Inv(baseCTM) x ctm)
70 // => Inv(T) = Inv(ctm) x baseCTM
73 if (base != ctm && ctm.invert(&lm)) {
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);
86 bool RenderNode::RenderContext::requiresIsolation() const {
87 // Note: fShader is never applied on isolation layers.
88 return ScaleAlpha(SK_AlphaOPAQUE, fOpacity) != SK_AlphaOPAQUE
91 || fBlendMode != SkBlendMode::kSrcOver;
94 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()));
99 paint->setShader(LocalShader(fShader, fShaderCTM, ctm));
101 if (fBlendMode != SkBlendMode::kSrcOver) {
102 paint->setBlendMode(fBlendMode);
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()));
114 RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx)
116 , fCtx(ctx ? *ctx : RenderContext())
117 , fRestoreCount(canvas->getSaveCount()) {}
119 RenderNode::ScopedRenderContext::~ScopedRenderContext() {
120 if (fRestoreCount >= 0) {
123 mask_paint.setBlendMode(SkBlendMode::kDstIn);
124 mask_paint.setShader(std::move(fMaskShader));
125 fCanvas->drawPaint(mask_paint);
127 fCanvas->restoreToCount(fRestoreCount);
131 RenderNode::ScopedRenderContext&&
132 RenderNode::ScopedRenderContext::modulateOpacity(float opacity) {
133 SkASSERT(opacity >= 0 && opacity <= 1);
134 fCtx.fOpacity *= opacity;
135 return std::move(*this);
138 RenderNode::ScopedRenderContext&&
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);
144 RenderNode::ScopedRenderContext&&
145 RenderNode::ScopedRenderContext::modulateShader(sk_sp<SkShader> sh, const SkMatrix& shader_ctm) {
146 // Topmost shader takes precedence.
148 fCtx.fShader = std::move(sh);
149 fCtx.fShaderCTM = shader_ctm;
152 return std::move(*this);
155 RenderNode::ScopedRenderContext&&
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:
162 // => T = Inv(maskCTM) x ctm
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));
172 fCtx.fMaskShader = std::move(ms);
176 return std::move(*this);
179 RenderNode::ScopedRenderContext&&
180 RenderNode::ScopedRenderContext::modulateBlendMode(SkBlendMode mode) {
181 fCtx.fBlendMode = mode;
182 return std::move(*this);
185 RenderNode::ScopedRenderContext&&
186 RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, const SkMatrix& ctm,
188 if (isolation && fCtx.requiresIsolation()) {
190 fCtx.modulatePaint(ctm, &layer_paint, /*is_layer_paint = */true);
191 fCanvas->saveLayer(bounds, &layer_paint);
193 // Fetch the mask shader for restore.
194 if (fCtx.fMaskShader) {
195 fMaskShader = LocalShader(fCtx.fMaskShader, fCtx.fMaskCTM, ctm);
198 // Reset only the props applied via isolation layers.
199 fCtx.fColorFilter = nullptr;
200 fCtx.fMaskShader = nullptr;
202 fCtx.fBlendMode = SkBlendMode::kSrcOver;
205 return std::move(*this);
208 RenderNode::ScopedRenderContext&&
209 RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm,
210 sk_sp<SkImageFilter> filter) {
213 fCtx.modulatePaint(ctm, &layer_paint);
215 SkASSERT(!layer_paint.getImageFilter());
216 layer_paint.setImageFilter(std::move(filter));
217 fCanvas->saveLayer(bounds, &layer_paint);
218 fCtx = RenderContext();
221 return std::move(*this);
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);
232 CustomRenderNode::~CustomRenderNode() {
233 for (const auto& child : fChildren) {
234 this->unobserveInval(child);
238 bool CustomRenderNode::hasChildrenInval() const {
239 for (const auto& child : fChildren) {
240 if (NodePriv::HasInval(child)) {