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