xref: /aosp_15_r20/external/skia/modules/skottie/src/effects/GlowStyles.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/SkBlendMode.h"
9 #include "include/core/SkColor.h"
10 #include "include/core/SkColorFilter.h"
11 #include "include/core/SkImageFilter.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/effects/SkColorMatrix.h"
15 #include "include/effects/SkImageFilters.h"
16 #include "include/private/base/SkFloatingPoint.h"
17 #include "include/private/base/SkTPin.h"
18 #include "modules/skottie/src/Adapter.h"
19 #include "modules/skottie/src/SkottiePriv.h"
20 #include "modules/skottie/src/SkottieValue.h"
21 #include "modules/skottie/src/effects/Effects.h"
22 #include "modules/sksg/include/SkSGRenderEffect.h"
23 #include "modules/sksg/include/SkSGRenderNode.h"
24 #include "src/utils/SkJSON.h"
25 
26 #include <algorithm>
27 #include <cmath>
28 #include <utility>
29 
30 namespace skottie::internal {
31 
32 namespace  {
33 
34 class GlowAdapter final : public DiscardableAdapterBase<GlowAdapter, sksg::ExternalImageFilter> {
35 public:
36     enum Type {
37         kOuterGlow,
38         kInnerGlow,
39     };
40 
GlowAdapter(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,Type type)41     GlowAdapter(const skjson::ObjectValue& jstyle, const AnimationBuilder& abuilder, Type type)
42         : fType(type) {
43         this->bind(abuilder, jstyle["c" ], fColor);
44         this->bind(abuilder, jstyle["o" ], fOpacity);
45         this->bind(abuilder, jstyle["s" ], fSize);
46         this->bind(abuilder, jstyle["sr"], fInnerSource);
47         this->bind(abuilder, jstyle["ch"], fChoke);
48     }
49 
50 private:
onSync()51     void onSync() override {
52         const auto sigma = fSize * kBlurSizeToSigma,
53                  opacity = SkTPin(fOpacity / 100, 0.0f, 1.0f),
54                    choke = SkTPin(fChoke   / 100, 0.0f, 1.0f);
55         const auto color = static_cast<SkColor4f>(fColor);
56 
57         // Select the source alpha channel.
58         SkColorMatrix mask_cm{
59             0, 0, 0, 0, 0,
60             0, 0, 0, 0, 0,
61             0, 0, 0, 0, 0,
62             0, 0, 0, 1, 0
63         };
64 
65         // Inner glows with an edge source use the alpha inverse.
66         if (fType == Type::kInnerGlow && SkScalarRoundToInt(fInnerSource) == kEdge) {
67             mask_cm.preConcat({
68                 1, 0, 0, 0, 0,
69                 0, 1, 0, 0, 0,
70                 0, 0, 1, 0, 0,
71                 0, 0, 0,-1, 1
72             });
73         }
74 
75         // Add glow color and opacity.
76         const SkColorMatrix color_cm {
77             0, 0, 0,                  0, color.fR,
78             0, 0, 0,                  0, color.fG,
79             0, 0, 0,                  0, color.fB,
80             0, 0, 0, opacity * color.fA,        0
81         };
82 
83         // Alpha choke only applies when a blur is in use.
84         const auto requires_alpha_choke = (sigma > 0 && choke > 0);
85 
86         if (!requires_alpha_choke) {
87             // We can fold the colorization step with the initial source alpha.
88             mask_cm.postConcat(color_cm);
89         }
90 
91         auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(mask_cm), nullptr);
92 
93         if (sigma > 0) {
94             f = SkImageFilters::Blur(sigma, sigma, std::move(f));
95         }
96 
97         if (requires_alpha_choke) {
98             // Choke/spread semantics (applied to the blur result):
99             //
100             //     0  -> no effect
101             //     1  -> all non-transparent values turn opaque (the blur "spreads" all the way)
102             // (0..1) -> some form of gradual/nonlinear transition between the two.
103             //
104             // One way to emulate this effect is by upscaling the blur alpha by 1 / (1 - choke):
105             static constexpr float kMaxAlphaScale = 1e6f,
106                                    kChokeGamma    = 0.2f;
107             const auto alpha_scale =
108                 std::min(sk_ieee_float_divide(1, 1 - std::pow(choke, kChokeGamma)), kMaxAlphaScale);
109 
110             SkColorMatrix choke_cm(1, 0, 0,           0, 0,
111                                    0, 1, 0,           0, 0,
112                                    0, 0, 1,           0, 0,
113                                    0, 0, 0, alpha_scale, 0);
114 
115             f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(choke_cm), std::move(f));
116 
117             // Colorization is deferred until after alpha choke.  It also must be applied as a
118             // separate color filter to ensure the choke scale above is clamped.
119             f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(color_cm), std::move(f));
120         }
121 
122         sk_sp<SkImageFilter> source;
123 
124         if (fType == Type::kInnerGlow) {
125             // Inner glows draw on top of, and are masked with, the source.
126             f = SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(f));
127 
128             std::swap(source, f);
129         }
130 
131         this->node()->setImageFilter(SkImageFilters::Merge(std::move(f),
132                                                            std::move(source)));
133     }
134 
135     enum InnerSource {
136         kEdge   = 1,
137         kCenter = 2,
138     };
139 
140     const Type fType;
141 
142     ColorValue  fColor;
143     ScalarValue fOpacity  = 100, // percentage
144                 fSize     =   0,
145                 fChoke    =   0,
146              fInnerSource = kEdge;
147 
148     using INHERITED = DiscardableAdapterBase<GlowAdapter, sksg::ExternalImageFilter>;
149 };
150 
make_glow_effect(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,sk_sp<sksg::RenderNode> layer,GlowAdapter::Type type)151 static sk_sp<sksg::RenderNode> make_glow_effect(const skjson::ObjectValue& jstyle,
152                                                 const AnimationBuilder& abuilder,
153                                                 sk_sp<sksg::RenderNode> layer,
154                                                 GlowAdapter::Type type) {
155     auto filter_node = abuilder.attachDiscardableAdapter<GlowAdapter>(jstyle, abuilder, type);
156 
157     return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node));
158 }
159 
160 } // namespace
161 
attachOuterGlowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const162 sk_sp<sksg::RenderNode> EffectBuilder::attachOuterGlowStyle(const skjson::ObjectValue& jstyle,
163                                                             sk_sp<sksg::RenderNode> layer) const {
164     return make_glow_effect(jstyle, *fBuilder, std::move(layer), GlowAdapter::Type::kOuterGlow);
165 }
166 
attachInnerGlowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const167 sk_sp<sksg::RenderNode> EffectBuilder::attachInnerGlowStyle(const skjson::ObjectValue& jstyle,
168                                                             sk_sp<sksg::RenderNode> layer) const {
169     return make_glow_effect(jstyle, *fBuilder, std::move(layer), GlowAdapter::Type::kInnerGlow);
170 }
171 
172 } // namespace skottie::internal
173