/src/skia/modules/skottie/src/effects/ShadowStyles.cpp
Line | Count | Source (jump to first uncovered line) |
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/SkM44.h" |
13 | | #include "include/core/SkRefCnt.h" |
14 | | #include "include/core/SkScalar.h" |
15 | | #include "include/effects/SkColorMatrix.h" |
16 | | #include "include/effects/SkImageFilters.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 <utility> |
27 | | |
28 | | namespace skottie::internal { |
29 | | |
30 | | namespace { |
31 | | |
32 | | class ShadowAdapter final : public DiscardableAdapterBase<ShadowAdapter, |
33 | | sksg::ExternalImageFilter> { |
34 | | public: |
35 | | enum Type { |
36 | | kDropShadow, |
37 | | kInnerShadow, |
38 | | }; |
39 | | |
40 | | ShadowAdapter(const skjson::ObjectValue& jstyle, |
41 | | const AnimationBuilder& abuilder, |
42 | | Type type) |
43 | 24.9k | : fType(type) { |
44 | 24.9k | this->bind(abuilder, jstyle["c"], fColor); |
45 | 24.9k | this->bind(abuilder, jstyle["o"], fOpacity); |
46 | 24.9k | this->bind(abuilder, jstyle["a"], fAngle); |
47 | 24.9k | this->bind(abuilder, jstyle["s"], fSize); |
48 | 24.9k | this->bind(abuilder, jstyle["d"], fDistance); |
49 | 24.9k | } |
50 | | |
51 | | private: |
52 | 24.9k | void onSync() override { |
53 | 24.9k | const auto rad = SkDegreesToRadians(180 + fAngle), // 0deg -> left (style) |
54 | 24.9k | sigma = fSize * kBlurSizeToSigma, |
55 | 24.9k | opacity = SkTPin(fOpacity / 100, 0.0f, 1.0f); |
56 | 24.9k | const auto color = static_cast<SkColor4f>(fColor); |
57 | 24.9k | const auto offset = SkV2{ fDistance * SkScalarCos(rad), |
58 | 24.9k | -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 | 24.9k | SkColorMatrix cm{0, 0, 0, 0, color.fR, |
80 | 24.9k | 0, 0, 0, 0, color.fG, |
81 | 24.9k | 0, 0, 0, 0, color.fB, |
82 | 24.9k | 0, 0, 0, opacity * color.fA, 0}; |
83 | | |
84 | | // Inner shadows use the alpha inverse. |
85 | 24.9k | if (fType == Type::kInnerShadow) { |
86 | 6.55k | cm.preConcat({1, 0, 0, 0, 0, |
87 | 6.55k | 0, 1, 0, 0, 0, |
88 | 6.55k | 0, 0, 1, 0, 0, |
89 | 6.55k | 0, 0, 0,-1, 1}); |
90 | 6.55k | } |
91 | 24.9k | auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(cm), nullptr); |
92 | | |
93 | 24.9k | if (sigma > 0) { |
94 | 272 | f = SkImageFilters::Blur(sigma, sigma, std::move(f)); |
95 | 272 | } |
96 | | |
97 | 24.9k | if (!SkScalarNearlyZero(offset.x) || !SkScalarNearlyZero(offset.y)) { |
98 | 0 | f = SkImageFilters::Offset(offset.x, offset.y, std::move(f)); |
99 | 0 | } |
100 | | |
101 | 24.9k | sk_sp<SkImageFilter> source; |
102 | | |
103 | 24.9k | if (fType == Type::kInnerShadow) { |
104 | | // Inner shadows draw on top of, and are masked with, the source. |
105 | 6.55k | f = SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(f)); |
106 | | |
107 | 6.55k | std::swap(source, f); |
108 | 6.55k | } |
109 | | |
110 | 24.9k | this->node()->setImageFilter(SkImageFilters::Merge(std::move(f), |
111 | 24.9k | std::move(source))); |
112 | 24.9k | } |
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 | | |
125 | | static sk_sp<sksg::RenderNode> make_shadow_effect(const skjson::ObjectValue& jstyle, |
126 | | const AnimationBuilder& abuilder, |
127 | | sk_sp<sksg::RenderNode> layer, |
128 | 24.9k | ShadowAdapter::Type type) { |
129 | 24.9k | auto filter_node = abuilder.attachDiscardableAdapter<ShadowAdapter>(jstyle, abuilder, type); |
130 | | |
131 | 24.9k | return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node)); |
132 | 24.9k | } |
133 | | |
134 | | } // namespace |
135 | | |
136 | | sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowStyle(const skjson::ObjectValue& jstyle, |
137 | 18.3k | sk_sp<sksg::RenderNode> layer) const { |
138 | 18.3k | return make_shadow_effect(jstyle, *fBuilder, std::move(layer), |
139 | 18.3k | ShadowAdapter::Type::kDropShadow); |
140 | 18.3k | } |
141 | | |
142 | | sk_sp<sksg::RenderNode> EffectBuilder::attachInnerShadowStyle(const skjson::ObjectValue& jstyle, |
143 | 6.55k | sk_sp<sksg::RenderNode> layer) const { |
144 | 6.55k | return make_shadow_effect(jstyle, *fBuilder, std::move(layer), |
145 | 6.55k | ShadowAdapter::Type::kInnerShadow); |
146 | 6.55k | } |
147 | | |
148 | | } // namespace skottie::internal |