| 1 |
/* |
| 2 |
* Copyright 2019 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 "include/core/SkCanvas.h" |
| 9 |
#include "include/core/SkColor.h" |
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/core/SkShader.h" |
| 15 |
#include "include/core/SkTileMode.h" |
| 16 |
#include "include/effects/SkGradient.h" |
| 17 |
#include "include/private/base/SkAssert.h" |
| 18 |
#include "modules/skottie/src/Adapter.h" |
| 19 |
#include "modules/skottie/src/SkottiePriv.h" |
| 20 |
#include "modules/skottie/src/SkottieValue.h" |
| 21 |
#include "modules/skottie/src/effects/Effects.h" |
| 22 |
#include "modules/sksg/include/SkSGNode.h" |
| 23 |
#include "modules/sksg/include/SkSGRenderNode.h" |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <cmath> |
| 27 |
#include <cstddef> |
| 28 |
#include <utility> |
| 29 |
#include <vector> |
| 30 |
|
| 31 |
class SkMatrix; |
| 32 |
|
| 33 |
namespace skjson { |
| 34 |
class ArrayValue; |
| 35 |
} |
| 36 |
namespace sksg { |
| 37 |
class InvalidationController; |
| 38 |
} |
| 39 |
|
| 40 |
namespace skottie { |
| 41 |
namespace internal { |
| 42 |
|
| 43 |
namespace { |
| 44 |
|
| 45 |
class RWipeRenderNode final : public sksg::CustomRenderNode { |
| 46 |
public: |
| 47 |
explicit RWipeRenderNode(sk_sp<sksg::RenderNode> layer) |
| 48 |
: INHERITED({std::move(layer)}) {} |
| 49 |
|
| 50 |
SG_ATTRIBUTE(Completion, float , fCompletion) |
| 51 |
SG_ATTRIBUTE(StartAngle, float , fStartAngle) |
| 52 |
SG_ATTRIBUTE(WipeCenter, SkPoint, fWipeCenter) |
| 53 |
SG_ATTRIBUTE(Wipe , float , fWipe ) |
| 54 |
SG_ATTRIBUTE(Feather , float , fFeather ) |
| 55 |
|
| 56 |
protected: |
| 57 |
const RenderNode* onNodeAt(const SkPoint&) const override { return nullptr; } // no hit-testing |
| 58 |
|
| 59 |
SkRect onRevalidate(sksg::InvalidationController* ic, const SkMatrix& ctm) override { |
| 60 |
SkASSERT(this->children().size() == 1ul); |
| 61 |
const auto content_bounds = this->children()[0]->revalidate(ic, ctm); |
| 62 |
|
| 63 |
if (fCompletion >= 100) { |
| 64 |
return SkRect::MakeEmpty(); |
| 65 |
} |
| 66 |
|
| 67 |
if (fCompletion <= 0) { |
| 68 |
fMaskSigma = 0; |
| 69 |
fMaskShader = nullptr; |
| 70 |
} else { |
| 71 |
fMaskSigma = std::max(fFeather, 0.0f) * kBlurSizeToSigma; |
| 72 |
|
| 73 |
const auto t = fCompletion * 0.01f; |
| 74 |
|
| 75 |
// Note: this could be simplified as a one-hard-stop gradient + local matrix |
| 76 |
// (to apply rotation). Alas, local matrices are no longer supported in SkSG. |
| 77 |
SkColor4f c0 = {0, 0, 0, 0}, |
| 78 |
c1 = {1, 1, 1, 1}; |
| 79 |
auto sanitize_angle = [](float a) { |
| 80 |
a = std::fmod(a, 360); |
| 81 |
if (a < 0) { |
| 82 |
a += 360; |
| 83 |
} |
| 84 |
return a; |
| 85 |
}; |
| 86 |
|
| 87 |
auto a0 = sanitize_angle(fStartAngle - 90 + t * this->wipeAlignment()), |
| 88 |
a1 = sanitize_angle(a0 + t * 360); |
| 89 |
if (a0 > a1) { |
| 90 |
std::swap(a0, a1); |
| 91 |
std::swap(c0, c1); |
| 92 |
} |
| 93 |
|
| 94 |
const SkColor4f grad_colors[] = { c1, c0, c0, c1 }; |
| 95 |
const SkScalar grad_pos[] = { 0, 0, 1, 1 }; |
| 96 |
SkGradient grad = {{grad_colors, grad_pos, SkTileMode::kClamp}, {}}; |
| 97 |
|
| 98 |
fMaskShader = SkShaders::SweepGradient(fWipeCenter, a0, a1, grad); |
| 99 |
|
| 100 |
// Edge feather requires a real blur. |
| 101 |
if (fMaskSigma > 0) { |
| 102 |
// TODO: this feature is disabled ATM. |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
return content_bounds; |
| 107 |
} |
| 108 |
|
| 109 |
void onRender(SkCanvas* canvas, const RenderContext* ctx) const override { |
| 110 |
if (fCompletion >= 100) { |
| 111 |
// Fully masked out. |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
const auto local_ctx = ScopedRenderContext(canvas, ctx) |
| 116 |
.modulateMaskShader(fMaskShader, canvas->getTotalMatrix()); |
| 117 |
this->children()[0]->render(canvas, local_ctx); |
| 118 |
} |
| 119 |
|
| 120 |
private: |
| 121 |
float wipeAlignment() const { |
| 122 |
switch (SkScalarRoundToInt(fWipe)) { |
| 123 |
case 1: return 0.0f; // Clockwise |
| 124 |
case 2: return -360.0f; // Counterclockwise |
| 125 |
case 3: return -180.0f; // Both/center |
| 126 |
default: break; |
| 127 |
} |
| 128 |
return 0.0f; |
| 129 |
} |
| 130 |
|
| 131 |
SkPoint fWipeCenter = { 0, 0 }; |
| 132 |
float fCompletion = 0, |
| 133 |
fStartAngle = 0, |
| 134 |
fWipe = 0, |
| 135 |
fFeather = 0; |
| 136 |
|
| 137 |
// Cached during revalidation. |
| 138 |
sk_sp<SkShader> fMaskShader; |
| 139 |
float fMaskSigma; // edge feather/blur |
| 140 |
|
| 141 |
using INHERITED = sksg::CustomRenderNode; |
| 142 |
}; |
| 143 |
|
| 144 |
class RadialWipeAdapter final : public DiscardableAdapterBase<RadialWipeAdapter, RWipeRenderNode> { |
| 145 |
public: |
| 146 |
RadialWipeAdapter(const skjson::ArrayValue& jprops, |
| 147 |
sk_sp<sksg::RenderNode> layer, |
| 148 |
const AnimationBuilder& abuilder) |
| 149 |
: INHERITED(sk_make_sp<RWipeRenderNode>(std::move(layer))) { |
| 150 |
|
| 151 |
enum : size_t { |
| 152 |
kCompletion_Index = 0, |
| 153 |
kStartAngle_Index = 1, |
| 154 |
kWipeCenter_Index = 2, |
| 155 |
kWipe_Index = 3, |
| 156 |
kFeather_Index = 4, |
| 157 |
}; |
| 158 |
|
| 159 |
EffectBinder(jprops, abuilder, this) |
| 160 |
.bind(kCompletion_Index, fCompletion) |
| 161 |
.bind(kStartAngle_Index, fStartAngle) |
| 162 |
.bind(kWipeCenter_Index, fWipeCenter) |
| 163 |
.bind( kWipe_Index, fWipe ) |
| 164 |
.bind( kFeather_Index, fFeather ); |
| 165 |
} |
| 166 |
|
| 167 |
private: |
| 168 |
void onSync() override { |
| 169 |
const auto& wiper = this->node(); |
| 170 |
|
| 171 |
wiper->setCompletion(fCompletion); |
| 172 |
wiper->setStartAngle(fStartAngle); |
| 173 |
wiper->setWipeCenter({fWipeCenter.x, fWipeCenter.y}); |
| 174 |
wiper->setWipe(fWipe); |
| 175 |
wiper->setFeather(fFeather); |
| 176 |
} |
| 177 |
|
| 178 |
Vec2Value fWipeCenter = {0,0}; |
| 179 |
ScalarValue fCompletion = 0, |
| 180 |
fStartAngle = 0, |
| 181 |
fWipe = 0, |
| 182 |
fFeather = 0; |
| 183 |
|
| 184 |
using INHERITED = DiscardableAdapterBase<RadialWipeAdapter, RWipeRenderNode>; |
| 185 |
}; |
| 186 |
|
| 187 |
} // namespace |
| 188 |
|
| 189 |
sk_sp<sksg::RenderNode> EffectBuilder::attachRadialWipeEffect(const skjson::ArrayValue& jprops, |
| 190 |
sk_sp<sksg::RenderNode> layer) const { |
| 191 |
return fBuilder->attachDiscardableAdapter<RadialWipeAdapter>(jprops, |
| 192 |
std::move(layer), |
| 193 |
*fBuilder); |
| 194 |
} |
| 195 |
|
| 196 |
} // namespace internal |
| 197 |
} // namespace skottie |