1 /*
2 * Copyright 2020 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/core/SkColorFilter.h"
11 #include "include/effects/SkRuntimeEffect.h"
12 #include "include/private/base/SkTPin.h"
13 #include "modules/skottie/src/Adapter.h"
14 #include "modules/skottie/src/SkottieJson.h"
15 #include "modules/skottie/src/SkottieValue.h"
16 #include "modules/sksg/include/SkSGColorFilter.h"
17
18 #include <cmath>
19
20 namespace skottie::internal {
21
22 #ifdef SK_ENABLE_SKSL
23
24 namespace {
25
26 // The contrast effect transfer function can be approximated with the following
27 // 3rd degree polynomial:
28 //
29 // f(x) = -2πC/3 * x³ + πC * x² + (1 - πC/3) * x
30 //
31 // where C is the normalized contrast value [-1..1].
32 //
33 // Derivation:
34 //
35 // - start off with sampling the AE contrast effect for various contrast/input values [1]
36 //
37 // - apply cubic polynomial curve fitting to determine best-fit coefficients for given
38 // contrast values [2]
39 //
40 // - observations:
41 // * negative contrast appears clamped at -0.5 (-50)
42 // * a,b coefficients vary linearly vs. contrast
43 // * the b coefficient for max contrast (1.0) looks kinda familiar: 3.14757 - coincidence?
44 // probably not. let's run with it: b == πC
45 //
46 // - additionally, we expect the following to hold:
47 // * f(0 ) = 0 \ | d = 0
48 // * f(1 ) = 1 | => | a = -2b/3
49 // * f(0.5) = 0.5 / | c = 1 - b/3
50 //
51 // - this yields a pretty decent approximation: [3]
52 //
53 //
54 // Note (courtesy of mtklein, reed): [4] seems to yield a closer approximation, but requires
55 // a more expensive sin
56 //
57 // f(x) = x + a * sin(2πx)/2π
58 //
59 // [1] https://www.desmos.com/calculator/oksptqpo8z
60 // [2] https://www.desmos.com/calculator/oukrf6yahn
61 // [3] https://www.desmos.com/calculator/ehem0vy3ft
62 // [4] https://www.desmos.com/calculator/5t4xi10q4v
63 //
64
65 #ifndef SKOTTIE_ACCURATE_CONTRAST_APPROXIMATION
make_contrast_coeffs(float contrast)66 static sk_sp<SkData> make_contrast_coeffs(float contrast) {
67 struct { float a, b, c; } coeffs;
68
69 coeffs.b = SK_ScalarPI * contrast;
70 coeffs.a = -2 * coeffs.b / 3;
71 coeffs.c = 1 - coeffs.b / 3;
72
73 return SkData::MakeWithCopy(&coeffs, sizeof(coeffs));
74 }
75
76 static constexpr char CONTRAST_EFFECT[] =
77 "uniform half a;"
78 "uniform half b;"
79 "uniform half c;"
80
81 "half4 main(half4 color) {"
82 // C' = a*C^3 + b*C^2 + c*C
83 "color.rgb = ((a*color.rgb + b)*color.rgb + c)*color.rgb;"
84 "return color;"
85 "}"
86 ;
87 #else
88 // More accurate (but slower) approximation:
89 //
90 // f(x) = x + a * sin(2πx)
91 //
92 // a = -contrast/3π
93 //
94 static sk_sp<SkData> make_contrast_coeffs(float contrast) {
95 const auto coeff_a = -contrast / (3 * SK_ScalarPI);
96
97 return SkData::MakeWithCopy(&coeff_a, sizeof(coeff_a));
98 }
99
100 static constexpr char CONTRAST_EFFECT[] =
101 "uniform half a;"
102
103 "half4 main(half4 color) {"
104 "color.rgb += a * sin(color.rgb * 6.283185);"
105 "return color;"
106 "}"
107 ;
108
109 #endif
110
111 // Brightness transfer function approximation:
112 //
113 // f(x) = 1 - (1 - x)^(2^(1.8*B))
114 //
115 // where B is the normalized [-1..1] brightness value
116 //
117 // Visualization: https://www.desmos.com/calculator/wuyqa2wtol
118 //
make_brightness_coeffs(float brightness)119 static sk_sp<SkData> make_brightness_coeffs(float brightness) {
120 const float coeff_a = std::pow(2.0f, brightness * 1.8f);
121
122 return SkData::MakeWithCopy(&coeff_a, sizeof(coeff_a));
123 }
124
125 static constexpr char BRIGHTNESS_EFFECT[] =
126 "uniform half a;"
127
128 "half4 main(half4 color) {"
129 "color.rgb = 1 - pow(1 - color.rgb, half3(a));"
130 "return color;"
131 "}"
132 ;
133
134 class BrightnessContrastAdapter final : public DiscardableAdapterBase<BrightnessContrastAdapter,
135 sksg::ExternalColorFilter> {
136 public:
BrightnessContrastAdapter(const skjson::ArrayValue & jprops,const AnimationBuilder & abuilder,sk_sp<sksg::RenderNode> layer)137 BrightnessContrastAdapter(const skjson::ArrayValue& jprops,
138 const AnimationBuilder& abuilder,
139 sk_sp<sksg::RenderNode> layer)
140 : INHERITED(sksg::ExternalColorFilter::Make(std::move(layer)))
141 , fBrightnessEffect(SkRuntimeEffect::MakeForColorFilter(SkString(BRIGHTNESS_EFFECT)).effect)
142 , fContrastEffect(SkRuntimeEffect::MakeForColorFilter(SkString(CONTRAST_EFFECT)).effect) {
143 SkASSERT(fBrightnessEffect);
144 SkASSERT(fContrastEffect);
145
146 enum : size_t {
147 kBrightness_Index = 0,
148 kContrast_Index = 1,
149 kUseLegacy_Index = 2,
150 };
151
152 EffectBinder(jprops, abuilder, this)
153 .bind(kBrightness_Index, fBrightness)
154 .bind( kContrast_Index, fContrast )
155 .bind( kUseLegacy_Index, fUseLegacy );
156 }
157
158 private:
onSync()159 void onSync() override {
160 this->node()->setColorFilter(SkScalarRoundToInt(fUseLegacy)
161 ? this->makeLegacyCF()
162 : this->makeCF());
163 }
164
makeLegacyCF() const165 sk_sp<SkColorFilter> makeLegacyCF() const {
166 // In 'legacy' mode, brightness is
167 //
168 // - in the [-100..100] range
169 // - applied component-wise as a direct offset (255-based)
170 // - (neutral value: 0)
171 // - transfer function: https://www.desmos.com/calculator/zne0oqwwzb
172 //
173 // while contrast is
174 //
175 // - in the [-100..100] range
176 // - applied as a component-wise linear transformation (scale+offset), such that
177 //
178 // -100 always yields mid-gray: contrast(x, -100) == 0.5
179 // 0 is the neutral value: contrast(x, 0) == x
180 // 100 always yields white: contrast(x, 100) == 1
181 //
182 // - transfer function: https://www.desmos.com/calculator/x5rxzhowhs
183 //
184
185 // Normalize to [-1..1]
186 const auto brightness = SkTPin(fBrightness, -100.0f, 100.0f) / 255, // [-100/255 .. 100/255]
187 contrast = SkTPin(fContrast , -100.0f, 100.0f) / 100; // [ -1 .. 1]
188
189 // The component scale is derived from contrast:
190 //
191 // Contrast[-1 .. 0] -> Scale[0 .. 1]
192 // Contrast( 0 .. 1] -> Scale(1 .. +inf)
193 const auto S = contrast > 0
194 ? 1 / std::max(1 - contrast, SK_ScalarNearlyZero)
195 : 1 + contrast;
196
197 // The component offset is derived from both brightness and contrast:
198 //
199 // Brightness[-100/255 .. 100/255] -> Offset[-100/255 .. 100/255]
200 // Contrast [ -1 .. 0] -> Offset[ 0.5 .. 0]
201 // Contrast ( 0 .. 1] -> Offset( 0 .. -inf)
202 //
203 // Why do these pre/post compose depending on contrast scale, you ask?
204 // Because AE - that's why!
205 const auto B = 0.5f * (1 - S) + brightness * std::max(S, 1.0f);
206
207 const float cm[] = {
208 S, 0, 0, 0, B,
209 0, S, 0, 0, B,
210 0, 0, S, 0, B,
211 0, 0, 0, 1, 0,
212 };
213
214 return SkColorFilters::Matrix(cm);
215 }
216
makeCF() const217 sk_sp<SkColorFilter> makeCF() const {
218 const auto brightness = SkTPin(fBrightness, -150.0f, 150.0f) / 150, // [-1.0 .. 1]
219 contrast = SkTPin(fContrast , -50.0f, 100.0f) / 100; // [-0.5 .. 1]
220
221 auto b_eff = SkScalarNearlyZero(brightness)
222 ? nullptr
223 : fBrightnessEffect->makeColorFilter(make_brightness_coeffs(brightness)),
224 c_eff = SkScalarNearlyZero(fContrast)
225 ? nullptr
226 : fContrastEffect->makeColorFilter(make_contrast_coeffs(contrast));
227
228 return SkColorFilters::Compose(std::move(c_eff), std::move(b_eff));
229 }
230
231 const sk_sp<SkRuntimeEffect> fBrightnessEffect,
232 fContrastEffect;
233
234 ScalarValue fBrightness = 0,
235 fContrast = 0,
236 fUseLegacy = 0;
237
238 using INHERITED = DiscardableAdapterBase<BrightnessContrastAdapter, sksg::ExternalColorFilter>;
239 };
240
241 } // namespace
242
243 #endif // SK_ENABLE_SKSL
244
attachBrightnessContrastEffect(const skjson::ArrayValue & jprops,sk_sp<sksg::RenderNode> layer) const245 sk_sp<sksg::RenderNode> EffectBuilder::attachBrightnessContrastEffect(
246 const skjson::ArrayValue& jprops, sk_sp<sksg::RenderNode> layer) const {
247 #ifdef SK_ENABLE_SKSL
248 return fBuilder->attachDiscardableAdapter<BrightnessContrastAdapter>(jprops,
249 *fBuilder,
250 std::move(layer));
251 #else
252 // TODO(skia:12197)
253 return layer;
254 #endif
255 }
256
257 } // namespace skottie::internal
258