xref: /aosp_15_r20/external/skia/modules/skottie/src/effects/ShadowStyles.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
ShadowAdapter(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,Type type)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 
51 private:
onSync()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         }
91         auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(cm), nullptr);
92 
93         if (sigma > 0) {
94             f = SkImageFilters::Blur(sigma, sigma, std::move(f));
95         }
96 
97         if (!SkScalarNearlyZero(offset.x) || !SkScalarNearlyZero(offset.y)) {
98             f = SkImageFilters::Offset(offset.x, offset.y, std::move(f));
99         }
100 
101         sk_sp<SkImageFilter> source;
102 
103         if (fType == Type::kInnerShadow) {
104             // Inner shadows draw on top of, and are masked with, the source.
105             f = SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(f));
106 
107             std::swap(source, f);
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 
make_shadow_effect(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,sk_sp<sksg::RenderNode> layer,ShadowAdapter::Type type)125 static sk_sp<sksg::RenderNode> make_shadow_effect(const skjson::ObjectValue& jstyle,
126                                                   const AnimationBuilder& abuilder,
127                                                   sk_sp<sksg::RenderNode> layer,
128                                                   ShadowAdapter::Type type) {
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 
attachDropShadowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const136 sk_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 
attachInnerShadowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const142 sk_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
149