Flutter Engine
The Flutter Engine
ShadowStyles.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
12#include "include/core/SkM44.h"
24#include "src/utils/SkJSON.h"
25
26#include <utility>
27
28namespace skottie::internal {
29
30namespace {
31
32class ShadowAdapter final : public DiscardableAdapterBase<ShadowAdapter,
33 sksg::ExternalImageFilter> {
34public:
35 enum Type {
36 kDropShadow,
37 kInnerShadow,
38 };
39
40 ShadowAdapter(const skjson::ObjectValue& jstyle,
41 const AnimationBuilder& abuilder,
42 Type type)
43 : fType(type) {
44 this->bind(abuilder, jstyle["c"], fColor);
45 this->bind(abuilder, jstyle["o"], fOpacity);
46 this->bind(abuilder, jstyle["a"], fAngle);
47 this->bind(abuilder, jstyle["s"], fSize);
48 this->bind(abuilder, jstyle["d"], fDistance);
49 }
50
51private:
52 void onSync() override {
53 const auto rad = SkDegreesToRadians(180 + fAngle), // 0deg -> left (style)
54 sigma = fSize * kBlurSizeToSigma,
55 opacity = SkTPin(fOpacity / 100, 0.0f, 1.0f);
56 const auto color = static_cast<SkColor4f>(fColor);
57 const auto offset = SkV2{ fDistance * SkScalarCos(rad),
58 -fDistance * SkScalarSin(rad)};
59
60 // Shadow effects largely follow the feDropShadow spec [1]:
61 //
62 // 1) isolate source alpha
63 // 2) apply a gaussian blur
64 // 3) apply an offset
65 // 4) modulate with a flood/color generator
66 // 5) composite with the source
67 //
68 // Note: as an optimization, we can fold #1 and #4 into a single color matrix filter.
69 //
70 // Inner shadow differences:
71 //
72 // a) operates on the inverse of source alpha
73 // b) the result is masked against the source
74 // c) composited on top of source
75 //
76 // [1] https://drafts.fxtf.org/filter-effects/#feDropShadowElement
77
78 // Select and colorize the source alpha channel.
79 SkColorMatrix cm{0, 0, 0, 0, color.fR,
80 0, 0, 0, 0, color.fG,
81 0, 0, 0, 0, color.fB,
82 0, 0, 0, opacity * color.fA, 0};
83
84 // Inner shadows use the alpha inverse.
85 if (fType == Type::kInnerShadow) {
86 cm.preConcat({1, 0, 0, 0, 0,
87 0, 1, 0, 0, 0,
88 0, 0, 1, 0, 0,
89 0, 0, 0,-1, 1});
90 }
92
93 if (sigma > 0) {
94 f = SkImageFilters::Blur(sigma, sigma, std::move(f));
95 }
96
98 f = SkImageFilters::Offset(offset.x, offset.y, std::move(f));
99 }
100
102
103 if (fType == Type::kInnerShadow) {
104 // Inner shadows draw on top of, and are masked with, the source.
106
108 }
109
110 this->node()->setImageFilter(SkImageFilters::Merge(std::move(f),
111 std::move(source)));
112 }
113
114 const Type fType;
115
116 ColorValue fColor;
117 ScalarValue fOpacity = 100, // percentage
118 fAngle = 0, // degrees
119 fSize = 0,
120 fDistance = 0;
121
122 using INHERITED = DiscardableAdapterBase<ShadowAdapter, sksg::ExternalImageFilter>;
123};
124
125static sk_sp<sksg::RenderNode> make_shadow_effect(const skjson::ObjectValue& jstyle,
126 const AnimationBuilder& abuilder,
129 auto filter_node = abuilder.attachDiscardableAdapter<ShadowAdapter>(jstyle, abuilder, type);
130
131 return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node));
132}
133
134} // namespace
135
136sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowStyle(const skjson::ObjectValue& jstyle,
137 sk_sp<sksg::RenderNode> layer) const {
138 return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
139 ShadowAdapter::Type::kDropShadow);
140}
141
142sk_sp<sksg::RenderNode> EffectBuilder::attachInnerShadowStyle(const skjson::ObjectValue& jstyle,
143 sk_sp<sksg::RenderNode> layer) const {
144 return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
145 ShadowAdapter::Type::kInnerShadow);
146}
147
148} // namespace skottie::internal
@ kDstIn
r = d * sa
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
void swap(sk_sp< T > &a, sk_sp< T > &b)
Definition: SkRefCnt.h:341
#define SkDegreesToRadians(degrees)
Definition: SkScalar.h:77
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
#define SkScalarSin(radians)
Definition: SkScalar.h:45
#define SkScalarCos(radians)
Definition: SkScalar.h:46
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
Type
Definition: SortBench.cpp:56
GLenum type
static sk_sp< SkColorFilter > Matrix(const SkColorMatrix &)
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< SkImageFilter > Offset(SkScalar dx, SkScalar dy, sk_sp< SkImageFilter > input, const CropRect &cropRect={})
static sk_sp< RenderNode > Make(sk_sp< RenderNode > child, sk_sp< ImageFilter > filter)
DlColor color
SkBitmap source
Definition: examples.cpp:28
static constexpr float kBlurSizeToSigma
Definition: SkottiePriv.h:47
SkScalar ScalarValue
Definition: SkottieValue.h:22
SeparatedVector2 offset
Definition: SkM44.h:19