| 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 | |
| 25 | RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {} |
| 26 | |
| 27 | bool RenderNode::isVisible() const { |
| 28 | return !(fNodeFlags & kInvisible_Flag); |
| 29 | } |
| 30 | |
| 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 | |
| 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); |
| 45 | } |
| 46 | SkASSERT(!this->hasInval()); |
| 47 | } |
| 48 | |
| 49 | const RenderNode* RenderNode::nodeAt(const SkPoint& p) const { |
| 50 | return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr; |
| 51 | } |
| 52 | |
| 53 | static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) { |
| 54 | return SkToU8(sk_float_round2int(alpha * opacity)); |
| 55 | } |
| 56 | |
| 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 | |
| 86 | 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 | || fBlendMode != SkBlendMode::kSrcOver; |
| 92 | } |
| 93 | |
| 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())); |
| 98 | if (fShader) { |
| 99 | paint->setShader(LocalShader(fShader, fShaderCTM, ctm)); |
| 100 | } |
| 101 | if (fBlendMode != SkBlendMode::kSrcOver) { |
| 102 | paint->setBlendMode(fBlendMode); |
| 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 | |
| 114 | RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx) |
| 115 | : fCanvas(canvas) |
| 116 | , fCtx(ctx ? *ctx : RenderContext()) |
| 117 | , fRestoreCount(canvas->getSaveCount()) {} |
| 118 | |
| 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&& |
| 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&& |
| 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&& |
| 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&& |
| 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&& |
| 180 | RenderNode::ScopedRenderContext::modulateBlendMode(SkBlendMode mode) { |
| 181 | fCtx.fBlendMode = mode; |
| 182 | return std::move(*this); |
| 183 | } |
| 184 | |
| 185 | RenderNode::ScopedRenderContext&& |
| 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.fOpacity = 1; |
| 202 | fCtx.fBlendMode = SkBlendMode::kSrcOver; |
| 203 | } |
| 204 | |
| 205 | return std::move(*this); |
| 206 | } |
| 207 | |
| 208 | RenderNode::ScopedRenderContext&& |
| 209 | RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm, |
| 210 | sk_sp<SkImageFilter> filter) { |
| 211 | SkPaint layer_paint; |
| 212 | fCtx.modulatePaint(ctm, &layer_paint); |
| 213 | |
| 214 | SkASSERT(!layer_paint.getImageFilter()); |
| 215 | layer_paint.setImageFilter(std::move(filter)); |
| 216 | fCanvas->saveLayer(bounds, &layer_paint); |
| 217 | fCtx = RenderContext(); |
| 218 | |
| 219 | return std::move(*this); |
| 220 | } |
| 221 | |
| 222 | CustomRenderNode::CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children) |
| 223 | : INHERITED(kOverrideDamage_Trait) // We cannot make any assumptions - override conservatively. |
| 224 | , fChildren(std::move(children)) { |
| 225 | for (const auto& child : fChildren) { |
| 226 | this->observeInval(child); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | CustomRenderNode::~CustomRenderNode() { |
| 231 | for (const auto& child : fChildren) { |
| 232 | this->unobserveInval(child); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | bool CustomRenderNode::hasChildrenInval() const { |
| 237 | for (const auto& child : fChildren) { |
| 238 | if (NodePriv::HasInval(child)) { |
| 239 | return true; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | } // namespace sksg |
| 247 | |