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