• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "modules/skottie/src/effects/Effects.h"
9 
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/SkottieValue.h"
16 #include "modules/sksg/include/SkSGRenderEffect.h"
17 #include "src/utils/SkJSON.h"
18 
19 namespace skottie::internal {
20 
21 namespace {
22 
23 class ShadowAdapter final : public DiscardableAdapterBase<ShadowAdapter,
24                                                           sksg::ExternalImageFilter> {
25 public:
26     enum Type {
27         kDropShadow,
28         kInnerShadow,
29     };
30 
ShadowAdapter(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,Type type)31     ShadowAdapter(const skjson::ObjectValue& jstyle,
32                   const AnimationBuilder& abuilder,
33                   Type type)
34         : fType(type) {
35         this->bind(abuilder, jstyle["c"], fColor);
36         this->bind(abuilder, jstyle["o"], fOpacity);
37         this->bind(abuilder, jstyle["a"], fAngle);
38         this->bind(abuilder, jstyle["s"], fSize);
39         this->bind(abuilder, jstyle["d"], fDistance);
40     }
41 
42 private:
onSync()43     void onSync() override {
44         const auto    rad = SkDegreesToRadians(180 + fAngle), // 0deg -> left (style)
45                     sigma = fSize * kBlurSizeToSigma,
46                   opacity = SkTPin(fOpacity / 100, 0.0f, 1.0f);
47         const auto  color = static_cast<SkColor4f>(fColor);
48         const auto offset = SkV2{ fDistance * SkScalarCos(rad),
49                                  -fDistance * SkScalarSin(rad)};
50 
51         // Shadow effects largely follow the feDropShadow spec [1]:
52         //
53         //   1) isolate source alpha
54         //   2) apply a gaussian blur
55         //   3) apply an offset
56         //   4) modulate with a flood/color generator
57         //   5) composite with the source
58         //
59         // Note: as an optimization, we can fold #1 and #4 into a single color matrix filter.
60         //
61         // Inner shadow differences:
62         //
63         //   a) operates on the inverse of source alpha
64         //   b) the result is masked against the source
65         //   c) composited on top of source
66         //
67         // [1] https://drafts.fxtf.org/filter-effects/#feDropShadowElement
68 
69         // Select and colorize the source alpha channel.
70         SkColorMatrix cm{0, 0, 0,                  0, color.fR,
71                          0, 0, 0,                  0, color.fG,
72                          0, 0, 0,                  0, color.fB,
73                          0, 0, 0, opacity * color.fA,        0};
74 
75         // Inner shadows use the alpha inverse.
76         if (fType == Type::kInnerShadow) {
77             cm.preConcat({1, 0, 0, 0, 0,
78                           0, 1, 0, 0, 0,
79                           0, 0, 1, 0, 0,
80                           0, 0, 0,-1, 1});
81         }
82         auto f = SkImageFilters::ColorFilter(SkColorFilters::Matrix(cm), nullptr);
83 
84         if (sigma > 0) {
85             f = SkImageFilters::Blur(sigma, sigma, std::move(f));
86         }
87 
88         if (!SkScalarNearlyZero(offset.x) || !SkScalarNearlyZero(offset.y)) {
89             f = SkImageFilters::Offset(offset.x, offset.y, std::move(f));
90         }
91 
92         sk_sp<SkImageFilter> source;
93 
94         if (fType == Type::kInnerShadow) {
95             // Inner shadows draw on top of, and are masked with, the source.
96             f = SkImageFilters::Blend(SkBlendMode::kDstIn, std::move(f));
97 
98             std::swap(source, f);
99         }
100 
101         this->node()->setImageFilter(SkImageFilters::Merge(std::move(f),
102                                                            std::move(source)));
103     }
104 
105     const Type fType;
106 
107     VectorValue fColor;
108     ScalarValue fOpacity  = 100, // percentage
109                 fAngle    =   0, // degrees
110                 fSize     =   0,
111                 fDistance =   0;
112 
113     using INHERITED = DiscardableAdapterBase<ShadowAdapter, sksg::ExternalImageFilter>;
114 };
115 
make_shadow_effect(const skjson::ObjectValue & jstyle,const AnimationBuilder & abuilder,sk_sp<sksg::RenderNode> layer,ShadowAdapter::Type type)116 static sk_sp<sksg::RenderNode> make_shadow_effect(const skjson::ObjectValue& jstyle,
117                                                   const AnimationBuilder& abuilder,
118                                                   sk_sp<sksg::RenderNode> layer,
119                                                   ShadowAdapter::Type type) {
120     auto filter_node = abuilder.attachDiscardableAdapter<ShadowAdapter>(jstyle, abuilder, type);
121 
122     return sksg::ImageFilterEffect::Make(std::move(layer), std::move(filter_node));
123 }
124 
125 } // namespace
126 
attachDropShadowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const127 sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowStyle(const skjson::ObjectValue& jstyle,
128                                                              sk_sp<sksg::RenderNode> layer) const {
129     return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
130                               ShadowAdapter::Type::kDropShadow);
131 }
132 
attachInnerShadowStyle(const skjson::ObjectValue & jstyle,sk_sp<sksg::RenderNode> layer) const133 sk_sp<sksg::RenderNode> EffectBuilder::attachInnerShadowStyle(const skjson::ObjectValue& jstyle,
134                                                               sk_sp<sksg::RenderNode> layer) const {
135     return make_shadow_effect(jstyle, *fBuilder, std::move(layer),
136                               ShadowAdapter::Type::kInnerShadow);
137 }
138 
139 }  // namespace skottie::internal
140