2 * Copyright 2019 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "modules/skottie/src/effects/Effects.h"
10 #include "include/effects/SkRuntimeEffect.h"
11 #include "include/private/SkTPin.h"
12 #include "modules/skottie/src/SkottieJson.h"
13 #include "modules/skottie/src/SkottieValue.h"
14 #include "modules/sksg/include/SkSGColorFilter.h"
15 #include "src/utils/SkJSON.h"
17 namespace skottie::internal {
23 // AE Saturation semantics:
25 // - saturation is applied as a component-wise scale (interpolation/extrapolation)
26 // relative to chroma mid point
27 // - the scale factor is clamped such that none of the components over/under saturates
28 // (e.g. below G/R and B are constrained to low_range and high_range, respectively)
29 // - the scale is also clammped to a maximum value of 126, empirically
30 // - the control is mapped linearly when desaturating, and non-linearly (1/1-S) when saturating
33 // |---------------+----+------------------+-----------------------------------|
36 // <------- chroma ------>
37 // <------- low_range -------> <---------------- high_range ----------------->
39 // With some care, we can stay in premul for these calculations.
40 static constexpr char gSaturateSkSL[] =
41 "uniform half u_scale;"
43 "half4 main(half4 c) {"
45 "half2 rg_srt = (c.r < c.g) ? c.rg : c.gr;"
46 "half c_min = min(rg_srt.x, c.b),"
47 "c_max = max(rg_srt.y, c.b),"
49 // chroma and mid-chroma (epsilon to avoid blowing up in the division below)
50 "ch = max(c_max - c_min, 0.0001),"
51 "ch_mid = (c_min + c_max)*0.5,"
53 // clamp scale to the maximum value which doesn't over/under saturate individual components
54 "scale_max = min(ch_mid, c.a - ch_mid)/ch*2,"
55 "scale = min(u_scale, scale_max);"
58 "c.rgb = ch_mid + (c.rgb - ch_mid)*scale;"
63 static sk_sp<SkColorFilter> make_saturate(float chroma_scale) {
64 static const auto* effect =
65 SkRuntimeEffect::MakeForColorFilter(SkString(gSaturateSkSL), {}).effect.release();
68 return effect->makeColorFilter(SkData::MakeWithCopy(&chroma_scale, sizeof(chroma_scale)));
72 static sk_sp<SkColorFilter> make_saturate(float) { return nullptr; }
73 #endif // SK_ENABLE_SKSL
75 class HueSaturationEffectAdapter final : public AnimatablePropertyContainer {
77 static sk_sp<HueSaturationEffectAdapter> Make(const skjson::ArrayValue& jprops,
78 sk_sp<sksg::RenderNode> layer,
79 const AnimationBuilder* abuilder) {
81 return sk_sp<HueSaturationEffectAdapter>(
82 new HueSaturationEffectAdapter(jprops, std::move(layer), abuilder));
85 const sk_sp<sksg::ExternalColorFilter>& node() const { return fColorFilter; }
88 HueSaturationEffectAdapter(const skjson::ArrayValue& jprops,
89 sk_sp<sksg::RenderNode> layer,
90 const AnimationBuilder* abuilder)
91 : fColorFilter(sksg::ExternalColorFilter::Make(std::move(layer))) {
93 kChannelControl_Index = 0,
94 kChannelRange_Index = 1,
97 kMasterLightness_Index = 4,
99 kColorizeHue_Index = 6,
100 kColorizeSat_Index = 7,
101 kColorizeLightness_Index = 8,
104 EffectBinder(jprops, *abuilder, this)
105 .bind( kChannelControl_Index, fChanCtrl )
106 .bind( kMasterHue_Index, fMasterHue )
107 .bind( kMasterSat_Index, fMasterSat )
108 .bind(kMasterLightness_Index, fMasterLight);
110 // TODO: colorize support?
113 void onSync() override {
114 fColorFilter->setColorFilter(this->makeColorFilter());
117 sk_sp<SkColorFilter> makeColorFilter() const {
121 kYellows_Chan = 0x03,
125 kMagentas_Chan = 0x07,
128 // We only support master channel controls at this point.
129 if (static_cast<int>(fChanCtrl) != kMaster_Chan) {
133 sk_sp<SkColorFilter> cf;
135 if (!SkScalarNearlyZero(fMasterHue)) {
136 // Linear control mapping hue(degrees) -> hue offset]
137 const auto h = fMasterHue/360;
139 const float cm[20] = {
146 cf = SkColorFilters::HSLAMatrix(cm);
149 if (!SkScalarNearlyZero(fMasterSat)) {
150 // AE clamps the max chroma scale to this value.
151 static constexpr auto kMaxScale = 126.0f;
154 // * sat [-100 .. 0) -> scale [0 .. 1) , linear
155 // * sat [0 .. 100] -> scale [1 .. max] , nonlinear: 100/(100 - sat)
156 const auto s = SkTPin(fMasterSat/100, -1.0f, 1.0f),
157 chroma_scale = s < 0 ? s + 1 : std::min(1/(1 - s), kMaxScale);
159 cf = SkColorFilters::Compose(std::move(cf), make_saturate(chroma_scale));
162 if (!SkScalarNearlyZero(fMasterLight)) {
163 // AE implements Lightness as a component-wise interpolation to 0 (for L < 0),
167 // * lightness [-100 .. 0) -> lerp[0 .. 1) from 0, linear
168 // * lightness [0 .. 100] -> lerp[1 .. 0] from 1, linear
169 const auto l = SkTPin(fMasterLight/100, -1.0f, 1.0f),
170 ls = 1 - std::abs(l), // scale
171 lo = l < 0 ? 0 : 1 - ls; // offset
173 const float cm[20] = {
180 cf = SkColorFilters::Compose(std::move(cf), SkColorFilters::Matrix(cm));
186 const sk_sp<sksg::ExternalColorFilter> fColorFilter;
188 float fChanCtrl = 0.0f,
196 sk_sp<sksg::RenderNode> EffectBuilder::attachHueSaturationEffect(
197 const skjson::ArrayValue& jprops, sk_sp<sksg::RenderNode> layer) const {
198 return fBuilder->attachDiscardableAdapter<HueSaturationEffectAdapter>(jprops,
203 } // namespace skottie::internal