2 * Copyright 2020 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/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"
21 namespace skottie::internal {
25 class GlowAdapter final : public DiscardableAdapterBase<GlowAdapter, sksg::ExternalImageFilter> {
32 GlowAdapter(const skjson::ObjectValue& jstyle, const AnimationBuilder& abuilder, Type 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);
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);
48 // Select the source alpha channel.
49 SkColorMatrix mask_cm{
56 // Inner glows with an edge source use the alpha inverse.
57 if (fType == Type::kInnerGlow && SkScalarRoundToInt(fInnerSource) == kEdge) {
66 // Add glow color and opacity.
67 const SkColorMatrix color_cm {
71 0, 0, 0, opacity * color.fA, 0
74 // Alpha choke only applies when a blur is in use.
75 const auto requires_alpha_choke = (sigma > 0 && choke > 0);
77 if (!requires_alpha_choke) {
78 // We can fold the colorization step with the initial source alpha.
79 mask_cm.postConcat(color_cm);
82 auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(mask_cm), nullptr);
85 f = SkImageFilters::Blur(sigma, sigma, std::move(f));
88 if (requires_alpha_choke) {
89 // Choke/spread semantics (applied to the blur result):
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.
95 // One way to emulate this effect is by upscaling the blur alpha by 1 / (1 - choke):
96 static constexpr float kMaxAlphaScale = 1e6f,
98 const auto alpha_scale =
99 std::min(sk_ieee_float_divide(1, 1 - std::pow(choke, kChokeGamma)), kMaxAlphaScale);
101 SkColorMatrix choke_cm(1, 0, 0, 0, 0,
104 0, 0, 0, alpha_scale, 0);
106 f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(choke_cm), std::move(f));
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));
113 sk_sp<SkImageFilter> source;
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));
119 std::swap(source, f);
122 this->node()->setImageFilter(SkImageFilters::Merge(std::move(f),
134 ScalarValue fOpacity = 100, // percentage
137 fInnerSource = kEdge;
139 using INHERITED = DiscardableAdapterBase<GlowAdapter, sksg::ExternalImageFilter>;
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);
148 return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node));
153 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);
158 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);
163 } // namespace skottie::internal