• 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/SkTableColorFilter.h"
11 #include "modules/skottie/src/SkottieAdapter.h"
12 #include "modules/skottie/src/SkottieValue.h"
13 #include "modules/sksg/include/SkSGColorFilter.h"
14 #include "src/utils/SkJSON.h"
15 
16 #include <cmath>
17 
18 namespace skottie {
19 namespace internal {
20 
21 // Levels color correction effect.
22 //
23 // Maps the selected channels from [inBlack...inWhite] to [outBlack, outWhite],
24 // based on a gamma exponent.
25 //
26 // For [i0..i1] -> [o0..o1]:
27 //
28 //   c' = o0 + (o1 - o0) * ((c - i0) / (i1 - i0)) ^ G
29 //
30 // The output is optionally clipped to the output range.
31 //
32 // In/out intervals are clampped to [0..1].  Inversion is allowed.
33 
34 namespace  {
35 
36 class LevelsEffectAdapter final : public SkNVRefCnt<LevelsEffectAdapter> {
37 public:
LevelsEffectAdapter(sk_sp<sksg::RenderNode> child)38     explicit LevelsEffectAdapter(sk_sp<sksg::RenderNode> child)
39         : fEffect(sksg::ExternalColorFilter::Make(std::move(child))) {
40         SkASSERT(fEffect);
41     }
42 
43     // 1: RGB, 2: R, 3: G, 4: B, 5: A
44     ADAPTER_PROPERTY(  Channel, SkScalar, 1)
45     ADAPTER_PROPERTY(  InBlack, SkScalar, 0)
46     ADAPTER_PROPERTY(  InWhite, SkScalar, 1)
47     ADAPTER_PROPERTY( OutBlack, SkScalar, 0)
48     ADAPTER_PROPERTY( OutWhite, SkScalar, 1)
49     ADAPTER_PROPERTY(    Gamma, SkScalar, 1)
50     // 1: clip, 2,3: don't clip
51     ADAPTER_PROPERTY(ClipBlack, SkScalar, 1)
52     ADAPTER_PROPERTY(ClipWhite, SkScalar, 1)
53 
root() const54     const sk_sp<sksg::ExternalColorFilter>& root() const { return fEffect; }
55 
56 private:
apply()57     void apply() {
58         enum LottieChannel {
59             kRGB_Channel = 1,
60               kR_Channel = 2,
61               kG_Channel = 3,
62               kB_Channel = 4,
63               kA_Channel = 5,
64         };
65 
66         const auto channel = SkScalarTruncToInt(fChannel);
67         if (channel < kRGB_Channel || channel > kA_Channel) {
68             fEffect->setColorFilter(nullptr);
69             return;
70         }
71 
72         auto in_0 = SkTPin(fInBlack,  0.0f, 1.0f),
73              in_1 = SkTPin(fInWhite,  0.0f, 1.0f),
74             out_0 = SkTPin(fOutBlack, 0.0f, 1.0f),
75             out_1 = SkTPin(fOutWhite, 0.0f, 1.0f),
76                 g = sk_ieee_float_divide(1, std::max(fGamma, 0.0f));
77 
78         float clip[] = {0, 1};
79         const auto kLottieDoClip = 1;
80         if (SkScalarTruncToInt(fClipBlack) == kLottieDoClip) {
81             const auto idx = fOutBlack <= fOutWhite ? 0 : 1;
82             clip[idx] = out_0;
83         }
84         if (SkScalarTruncToInt(fClipWhite) == kLottieDoClip) {
85             const auto idx = fOutBlack <= fOutWhite ? 1 : 0;
86             clip[idx] = out_1;
87         }
88         SkASSERT(clip[0] <= clip[1]);
89 
90         auto dIn  =  in_1 -  in_0,
91              dOut = out_1 - out_0;
92 
93         if (SkScalarNearlyZero(dIn)) {
94             // Degenerate dIn == 0 makes the arithmetic below explode.
95             //
96             // We could specialize the builder to deal with that case, or we could just
97             // nudge by epsilon to make it all work.  The latter approach is simpler
98             // and doesn't have any noticeable downsides.
99             //
100             // Also nudge in_0 towards 0.5, in case it was sqashed against an extremity.
101             // This allows for some abrupt transition when the output interval is not
102             // collapsed, and produces results closer to AE.
103             static constexpr auto kEpsilon = 2 * SK_ScalarNearlyZero;
104             dIn  += std::copysign(kEpsilon, dIn);
105             in_0 += std::copysign(kEpsilon, .5f - in_0);
106             SkASSERT(!SkScalarNearlyZero(dIn));
107         }
108 
109         uint8_t lut[256];
110 
111         auto t =      -in_0 / dIn,
112             dT = 1 / 255.0f / dIn;
113 
114         // TODO: is linear gamma common-enough to warrant a fast path?
115         for (size_t i = 0; i < 256; ++i) {
116             const auto out = out_0 + dOut * std::pow(std::max(t, 0.0f), g);
117             SkASSERT(!SkScalarIsNaN(out));
118 
119             lut[i] = static_cast<uint8_t>(std::round(SkTPin(out, clip[0], clip[1]) * 255));
120 
121             t += dT;
122         }
123 
124         fEffect->setColorFilter(SkTableColorFilter::MakeARGB(
125             channel == kA_Channel                            ? lut : nullptr,
126             channel == kR_Channel || channel == kRGB_Channel ? lut : nullptr,
127             channel == kG_Channel || channel == kRGB_Channel ? lut : nullptr,
128             channel == kB_Channel || channel == kRGB_Channel ? lut : nullptr
129         ));
130     }
131 
132     sk_sp<sksg::ExternalColorFilter> fEffect;
133 };
134 
135 } // anonymous ns
136 
attachLevelsEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const137 sk_sp<sksg::RenderNode> EffectBuilder::attachLevelsEffect(const skjson::ArrayValue& jprops,
138                                                           sk_sp<sksg::RenderNode> layer) const {
139     enum : size_t {
140         kChannel_Index        = 0,
141         // ???                = 1,
142         kInputBlack_Index     = 2,
143         kInputWhite_Index     = 3,
144         kGamma_Index          = 4,
145         kOutputBlack_Index    = 5,
146         kOutputWhite_Index    = 6,
147         kClipToOutBlack_Index = 7,
148         kClipToOutWhite_Index = 8,
149     };
150 
151     auto adapter = sk_make_sp<LevelsEffectAdapter>(std::move(layer));
152 
153     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kChannel_Index),
154         [adapter](const ScalarValue& channel) {
155             adapter->setChannel(channel);
156         });
157     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kInputBlack_Index),
158         [adapter](const ScalarValue& ib) {
159             adapter->setInBlack(ib);
160         });
161     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kInputWhite_Index),
162         [adapter](const ScalarValue& iw) {
163             adapter->setInWhite(iw);
164         });
165     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kOutputBlack_Index),
166         [adapter](const ScalarValue& ob) {
167             adapter->setOutBlack(ob);
168         });
169     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kOutputWhite_Index),
170         [adapter](const ScalarValue& ow) {
171             adapter->setOutWhite(ow);
172         });
173     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kGamma_Index),
174         [adapter](const ScalarValue& g) {
175             adapter->setGamma(g);
176         });
177 
178     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kClipToOutBlack_Index),
179         [adapter](const ScalarValue& cb) {
180             adapter->setClipBlack(cb);
181         });
182     fBuilder->bindProperty<ScalarValue>(GetPropValue(jprops, kClipToOutWhite_Index),
183         [adapter](const ScalarValue& cw) {
184             adapter->setClipWhite(cw);
185         });
186 
187     return adapter->root();
188 }
189 
190 } // namespace internal
191 } // namespace skottie
192