| 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/SkBlendMode.h" |
| 11 |
#include "include/core/SkCanvas.h" |
| 12 |
#include "include/core/SkColor.h" |
| 13 |
#include "include/core/SkImageFilter.h" |
| 14 |
#include "include/core/SkPaint.h" |
| 15 |
#include "include/core/SkPoint.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/effects/SkImageFilters.h" |
| 18 |
#include "include/private/base/SkAssert.h" |
| 19 |
#include "include/private/base/SkFloatingPoint.h" |
| 20 |
#include "include/private/base/SkTo.h" |
| 21 |
#include "modules/sksg/src/SkSGNodePriv.h" |
| 22 |
|
| 23 |
namespace sksg { |
| 24 |
|
| 25 |
namespace { |
| 26 |
|
| 27 |
enum Flags : uint8_t { |
| 28 |
kInvisible_Flag = 1 << 0, |
| 29 |
}; |
| 30 |
|
| 31 |
} // namespace |
| 32 |
|
| 33 |
RenderNode::RenderNode(uint32_t inval_traits) : INHERITED(inval_traits) {} |
| 34 |
|
| 35 |
bool RenderNode::isVisible() const { |
| 36 |
return !(fNodeFlags & kInvisible_Flag); |
| 37 |
} |
| 38 |
|
| 39 |
void RenderNode::setVisible(bool v) { |
| 40 |
if (v == this->isVisible()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
this->invalidate(); |
| 45 |
fNodeFlags = v ? (fNodeFlags & ~kInvisible_Flag) |
| 46 |
: (fNodeFlags | kInvisible_Flag); |
| 47 |
} |
| 48 |
|
| 49 |
void 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 |
|
| 57 |
const RenderNode* RenderNode::nodeAt(const SkPoint& p) const { |
| 58 |
return this->bounds().contains(p.x(), p.y()) ? this->onNodeAt(p) : nullptr; |
| 59 |
} |
| 60 |
|
| 61 |
static SkAlpha ScaleAlpha(SkAlpha alpha, float opacity) { |
| 62 |
return SkToU8(sk_float_round2int(alpha * opacity)); |
| 63 |
} |
| 64 |
|
| 65 |
static sk_sp<SkShader> LocalShader(const sk_sp<SkShader>& shader, |
| 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 |
|
| 94 |
bool RenderNode::RenderContext::requiresIsolation() const { |
| 95 |
// Note: fShader is never applied on isolation layers. |
| 96 |
return ScaleAlpha(SK_AlphaOPAQUE, fOpacity) != SK_AlphaOPAQUE |
| 97 |
|| fColorFilter |
| 98 |
|| fMaskShader |
| 99 |
|| fBlender; |
| 100 |
} |
| 101 |
|
| 102 |
void RenderNode::RenderContext::modulatePaint(const SkMatrix& ctm, SkPaint* paint, |
| 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) { |
| 116 |
paint->setShader(SkShaders::Blend(SkBlendMode::kSrcIn, |
| 117 |
LocalShader(fMaskShader, fMaskCTM, ctm), |
| 118 |
paint->refShader())); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
RenderNode::ScopedRenderContext::ScopedRenderContext(SkCanvas* canvas, const RenderContext* ctx) |
| 123 |
: fCanvas(canvas) |
| 124 |
, fCtx(ctx ? *ctx : RenderContext()) |
| 125 |
, fRestoreCount(canvas->getSaveCount()) {} |
| 126 |
|
| 127 |
RenderNode::ScopedRenderContext::~ScopedRenderContext() { |
| 128 |
if (fRestoreCount >= 0) { |
| 129 |
if (fMaskShader) { |
| 130 |
SkPaint mask_paint; |
| 131 |
mask_paint.setBlendMode(SkBlendMode::kDstIn); |
| 132 |
mask_paint.setShader(std::move(fMaskShader)); |
| 133 |
fCanvas->drawPaint(mask_paint); |
| 134 |
} |
| 135 |
fCanvas->restoreToCount(fRestoreCount); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
RenderNode::ScopedRenderContext&& |
| 140 |
RenderNode::ScopedRenderContext::modulateOpacity(float opacity) { |
| 141 |
SkASSERT(opacity >= 0 && opacity <= 1); |
| 142 |
fCtx.fOpacity *= opacity; |
| 143 |
return std::move(*this); |
| 144 |
} |
| 145 |
|
| 146 |
RenderNode::ScopedRenderContext&& |
| 147 |
RenderNode::ScopedRenderContext::modulateColorFilter(sk_sp<SkColorFilter> cf) { |
| 148 |
fCtx.fColorFilter = SkColorFilters::Compose(std::move(fCtx.fColorFilter), std::move(cf)); |
| 149 |
return std::move(*this); |
| 150 |
} |
| 151 |
|
| 152 |
RenderNode::ScopedRenderContext&& |
| 153 |
RenderNode::ScopedRenderContext::modulateShader(sk_sp<SkShader> sh, const SkMatrix& shader_ctm) { |
| 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 |
|
| 163 |
RenderNode::ScopedRenderContext&& |
| 164 |
RenderNode::ScopedRenderContext::modulateMaskShader(sk_sp<SkShader> ms, const SkMatrix& ctm) { |
| 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 |
|
| 187 |
RenderNode::ScopedRenderContext&& |
| 188 |
RenderNode::ScopedRenderContext::modulateBlender(sk_sp<SkBlender> blender) { |
| 189 |
fCtx.fBlender = std::move(blender); |
| 190 |
return std::move(*this); |
| 191 |
} |
| 192 |
|
| 193 |
RenderNode::ScopedRenderContext&& |
| 194 |
RenderNode::ScopedRenderContext::setIsolation(const SkRect& bounds, const SkMatrix& ctm, |
| 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 |
|
| 216 |
RenderNode::ScopedRenderContext&& |
| 217 |
RenderNode::ScopedRenderContext::setFilterIsolation(const SkRect& bounds, const SkMatrix& ctm, |
| 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 |
|
| 237 |
CustomRenderNode::CustomRenderNode(std::vector<sk_sp<RenderNode>>&& children) |
| 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 |
|
| 245 |
CustomRenderNode::~CustomRenderNode() { |
| 246 |
for (const auto& child : fChildren) { |
| 247 |
this->unobserveInval(child); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
bool CustomRenderNode::hasChildrenInval() const { |
| 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 |