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/SkGradientShader.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:
RWipeRenderNode(sk_sp<sksg::RenderNode> layer)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:
onNodeAt(const SkPoint &) const57 const RenderNode* onNodeAt(const SkPoint&) const override { return nullptr; } // no hit-testing
58
onRevalidate(sksg::InvalidationController * ic,const SkMatrix & ctm)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 SkColor c0 = 0x00000000,
78 c1 = 0xffffffff;
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 SkColor grad_colors[] = { c1, c0, c0, c1 };
95 const SkScalar grad_pos[] = { 0, 0, 1, 1 };
96
97 fMaskShader = SkGradientShader::MakeSweep(fWipeCenter.x(), fWipeCenter.y(),
98 grad_colors, grad_pos,
99 std::size(grad_colors),
100 SkTileMode::kClamp,
101 a0, a1, 0, nullptr);
102
103 // Edge feather requires a real blur.
104 if (fMaskSigma > 0) {
105 // TODO: this feature is disabled ATM.
106 }
107 }
108
109 return content_bounds;
110 }
111
onRender(SkCanvas * canvas,const RenderContext * ctx) const112 void onRender(SkCanvas* canvas, const RenderContext* ctx) const override {
113 if (fCompletion >= 100) {
114 // Fully masked out.
115 return;
116 }
117
118 const auto local_ctx = ScopedRenderContext(canvas, ctx)
119 .modulateMaskShader(fMaskShader, canvas->getTotalMatrix());
120 this->children()[0]->render(canvas, local_ctx);
121 }
122
123 private:
wipeAlignment() const124 float wipeAlignment() const {
125 switch (SkScalarRoundToInt(fWipe)) {
126 case 1: return 0.0f; // Clockwise
127 case 2: return -360.0f; // Counterclockwise
128 case 3: return -180.0f; // Both/center
129 default: break;
130 }
131 return 0.0f;
132 }
133
134 SkPoint fWipeCenter = { 0, 0 };
135 float fCompletion = 0,
136 fStartAngle = 0,
137 fWipe = 0,
138 fFeather = 0;
139
140 // Cached during revalidation.
141 sk_sp<SkShader> fMaskShader;
142 float fMaskSigma; // edge feather/blur
143
144 using INHERITED = sksg::CustomRenderNode;
145 };
146
147 class RadialWipeAdapter final : public DiscardableAdapterBase<RadialWipeAdapter, RWipeRenderNode> {
148 public:
RadialWipeAdapter(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer,const AnimationBuilder & abuilder)149 RadialWipeAdapter(const skjson::ArrayValue& jprops,
150 sk_sp<sksg::RenderNode> layer,
151 const AnimationBuilder& abuilder)
152 : INHERITED(sk_make_sp<RWipeRenderNode>(std::move(layer))) {
153
154 enum : size_t {
155 kCompletion_Index = 0,
156 kStartAngle_Index = 1,
157 kWipeCenter_Index = 2,
158 kWipe_Index = 3,
159 kFeather_Index = 4,
160 };
161
162 EffectBinder(jprops, abuilder, this)
163 .bind(kCompletion_Index, fCompletion)
164 .bind(kStartAngle_Index, fStartAngle)
165 .bind(kWipeCenter_Index, fWipeCenter)
166 .bind( kWipe_Index, fWipe )
167 .bind( kFeather_Index, fFeather );
168 }
169
170 private:
onSync()171 void onSync() override {
172 const auto& wiper = this->node();
173
174 wiper->setCompletion(fCompletion);
175 wiper->setStartAngle(fStartAngle);
176 wiper->setWipeCenter({fWipeCenter.x, fWipeCenter.y});
177 wiper->setWipe(fWipe);
178 wiper->setFeather(fFeather);
179 }
180
181 Vec2Value fWipeCenter = {0,0};
182 ScalarValue fCompletion = 0,
183 fStartAngle = 0,
184 fWipe = 0,
185 fFeather = 0;
186
187 using INHERITED = DiscardableAdapterBase<RadialWipeAdapter, RWipeRenderNode>;
188 };
189
190 } // namespace
191
attachRadialWipeEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const192 sk_sp<sksg::RenderNode> EffectBuilder::attachRadialWipeEffect(const skjson::ArrayValue& jprops,
193 sk_sp<sksg::RenderNode> layer) const {
194 return fBuilder->attachDiscardableAdapter<RadialWipeAdapter>(jprops,
195 std::move(layer),
196 *fBuilder);
197 }
198
199 } // namespace internal
200 } // namespace skottie
201