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