Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GlowStyles.cpp
Go to the documentation of this file.
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
24#include "src/utils/SkJSON.h"
25
26#include <algorithm>
27#include <cmath>
28#include <utility>
29
30namespace skottie::internal {
31
32namespace {
33
34class GlowAdapter final : public DiscardableAdapterBase<GlowAdapter, sksg::ExternalImageFilter> {
35public:
36 enum Type {
37 kOuterGlow,
38 kInnerGlow,
39 };
40
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
50private:
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
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
123
124 if (fType == Type::kInnerGlow) {
125 // Inner glows draw on top of, and are masked with, the source.
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
151static sk_sp<sksg::RenderNode> make_glow_effect(const skjson::ObjectValue& jstyle,
152 const AnimationBuilder& abuilder,
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
162sk_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
167sk_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
SkColor4f color
@ kDstIn
r = d * sa
static constexpr float sk_ieee_float_divide(float numer, float denom)
#define INHERITED(method,...)
#define SkScalarRoundToInt(x)
Definition SkScalar.h:37
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition SkTPin.h:19
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
void preConcat(const SkColorMatrix &mat)
static sk_sp< SkImageFilter > ColorFilter(sk_sp< SkColorFilter > cf, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Merge(sk_sp< SkImageFilter > *const filters, int count, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blur(SkScalar sigmaX, SkScalar sigmaY, SkTileMode tileMode, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< SkImageFilter > Blend(SkBlendMode mode, sk_sp< SkImageFilter > background, sk_sp< SkImageFilter > foreground=nullptr, const CropRect &cropRect={})
static sk_sp< RenderNode > Make(sk_sp< RenderNode > child, sk_sp< ImageFilter > filter)
SkBitmap source
Definition examples.cpp:28
static constexpr float kBlurSizeToSigma
Definition SkottiePriv.h:47
SkScalar ScalarValue