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