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 "modules/skottie/src/effects/Effects.h"
9
10 #include "include/effects/SkGradientShader.h"
11 #include "include/effects/SkShaderMaskFilter.h"
12 #include "modules/skottie/src/SkottieAdapter.h"
13 #include "modules/skottie/src/SkottieValue.h"
14 #include "modules/sksg/include/SkSGRenderEffect.h"
15 #include "src/utils/SkJSON.h"
16
17 #include <cmath>
18
19 namespace skottie {
20 namespace internal {
21
22 namespace {
23
24 class VenetianBlindsAdapter final : public MaskFilterEffectBase {
25 public:
VenetianBlindsAdapter(sk_sp<sksg::RenderNode> layer,const SkSize & ls)26 VenetianBlindsAdapter(sk_sp<sksg::RenderNode> layer, const SkSize& ls)
27 : INHERITED(std::move(layer), ls) {}
28
29 ADAPTER_PROPERTY(Completion, float, 0)
30 ADAPTER_PROPERTY(Direction , float, 0)
31 ADAPTER_PROPERTY(Width , float, 0)
32 ADAPTER_PROPERTY(Feather , float, 0)
33
34 private:
onMakeMask() const35 MaskInfo onMakeMask() const override {
36 if (fCompletion >= 100) {
37 // The layer is fully disabled.
38 return { nullptr, false };
39 }
40
41 if (fCompletion <= 0) {
42 // The layer is fully visible (no mask).
43 return { nullptr, true };
44 }
45
46 static constexpr float kFeatherSigmaFactor = 3.0f,
47 kMinFeather = 0.5f; // for soft gradient edges
48
49 const auto t = fCompletion * 0.01f,
50 size = std::max(1.0f, fWidth),
51 angle = SkDegreesToRadians(-fDirection),
52 feather = std::max(fFeather * kFeatherSigmaFactor, kMinFeather),
53 df = feather / size, // feather distance in normalized stop space
54 df0 = 0.5f * std::min(df, t),
55 df1 = 0.5f * std::min(df, 1 - t);
56
57 // In its simplest form, the Venetian Blinds effect is a single-step gradient
58 // repeating along the direction vector.
59 //
60 // To avoid an expensive blur pass, we emulate the feather property by softening
61 // the gradient edges:
62 //
63 // 1.0 [ | ------- ]
64 // [ | / \ ]
65 // [ | / \ ]
66 // [ | / \ ]
67 // [ | / \ ]
68 // [ | / \ ]
69 // [ | / \ ]
70 // [ |/ \]
71 // 0.5 [ | ]
72 // [\ /| ]
73 // [ \ / | ]
74 // [ \ / | ]
75 // [ \ / | ]
76 // [ \ / | ]
77 // [ \ / | ]
78 // [ \ / | ]
79 // 0.0 [ ------------------- | ]
80 //
81 // ^ ^ ^ ^ ^ ^ ^
82 // 0 fp0 fp1 T fp2 fp3 1
83 //
84 // | | | | | | |
85 // |< df0 >| |< df0 >|< df1 >| |< df1 >|
86 //
87 // ... df >| |< df >| |< df ...
88 //
89 // Note 1: fp0-fp1 and/or fp2-fp3 can collapse when df is large enough.
90 //
91 // Note 2: G(fp0) == G(fp1) and G(fp2) == G(fp3), whether collapsed or not.
92 //
93 // Note 3: to minimize the number of gradient stops, we can shift the gradient by -df0
94 // (such that fp0 aligns with 0/pts[0]).
95
96 // Gradient value at fp0/fp1, fp2/fp3.
97 // Note: g01 > 0 iff fp0-fp1 is collapsed and g23 < 1 iff fp2-fp3 is collapsed
98 const auto g01 = std::max(0.0f, 0.5f * (1 + (0 - t) / df)),
99 g23 = std::min(1.0f, 0.5f * (1 + (1 - t) / df));
100
101 const SkColor c01 = SkColorSetA(SK_ColorWHITE, SkScalarRoundToInt(g01 * 0xff)),
102 c23 = SkColorSetA(SK_ColorWHITE, SkScalarRoundToInt(g23 * 0xff)),
103 colors[] = { c01, c23, c23, c01 };
104
105 const SkScalar pos[] = {
106 // 0, // fp0
107 t - df0 - df0, // fp1
108 t + df1 - df0, // fp2
109 1 - df1 - df0, // fp3
110 1,
111 };
112 static_assert(SK_ARRAY_COUNT(colors) == SK_ARRAY_COUNT(pos), "");
113
114 const auto center = SkPoint::Make(0.5f * this->layerSize().width(),
115 0.5f * this->layerSize().height()),
116 grad_vec = SkVector::Make( size * std::cos(angle),
117 -size * std::sin(angle));
118
119 const SkPoint pts[] = {
120 center + grad_vec * (df0 + 0),
121 center + grad_vec * (df0 + 1),
122 };
123
124 return {
125 SkShaderMaskFilter::Make(SkGradientShader::MakeLinear(pts, colors, pos,
126 SK_ARRAY_COUNT(colors),
127 SkTileMode::kRepeat,
128 0, nullptr)),
129 true
130 };
131 }
132
133 using INHERITED = MaskFilterEffectBase;
134 };
135
136 } // namespace
137
attachVenetianBlindsEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const138 sk_sp<sksg::RenderNode> EffectBuilder::attachVenetianBlindsEffect(
139 const skjson::ArrayValue& jprops, sk_sp<sksg::RenderNode> layer) const {
140 enum : size_t {
141 kCompletion_Index = 0,
142 kDirection_Index = 1,
143 kWidth_Index = 2,
144 kFeather_Index = 3,
145 };
146
147 auto adapter = sk_make_sp<VenetianBlindsAdapter>(std::move(layer), fLayerSize);
148
149 fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kCompletion_Index),
150 [adapter](const ScalarValue& c) {
151 adapter->setCompletion(c);
152 });
153 fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kDirection_Index),
154 [adapter](const ScalarValue& d) {
155 adapter->setDirection(d);
156 });
157 fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kWidth_Index),
158 [adapter](const ScalarValue& w) {
159 adapter->setWidth(w);
160 });
161 fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kFeather_Index),
162 [adapter](const ScalarValue& f) {
163 adapter->setFeather(f);
164 });
165
166 return adapter->root();
167 }
168
169 } // namespace internal
170 } // namespace skottie
171