1 | /* |
2 | * Copyright 2019 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 "modules/skottie/src/SkottieValue.h" |
11 | #include "modules/sksg/include/SkSGRenderEffect.h" |
12 | #include "src/utils/SkJSON.h" |
13 | |
14 | namespace skottie { |
15 | namespace internal { |
16 | |
17 | namespace { |
18 | |
19 | class DropShadowAdapter final : public AnimatablePropertyContainer { |
20 | public: |
21 | static sk_sp<DropShadowAdapter> Make(const skjson::ArrayValue& jprops, |
22 | sk_sp<sksg::RenderNode> layer, |
23 | const AnimationBuilder& abuilder) { |
24 | enum : size_t { |
25 | kShadowColor_Index = 0, |
26 | kOpacity_Index = 1, |
27 | kDirection_Index = 2, |
28 | kDistance_Index = 3, |
29 | kSoftness_Index = 4, |
30 | kShadowOnly_Index = 5, |
31 | }; |
32 | |
33 | sk_sp<DropShadowAdapter> adapter(new DropShadowAdapter(std::move(layer))); |
34 | |
35 | EffectBinder(jprops, abuilder, adapter.get()) |
36 | .bind(kShadowColor_Index, adapter->fColor ) |
37 | .bind( kOpacity_Index, adapter->fOpacity ) |
38 | .bind( kDirection_Index, adapter->fDirection) |
39 | .bind( kDistance_Index, adapter->fDistance ) |
40 | .bind( kSoftness_Index, adapter->fSoftness ) |
41 | .bind( kShadowOnly_Index, adapter->fShdwOnly ); |
42 | |
43 | return adapter; |
44 | } |
45 | |
46 | const sk_sp<sksg::RenderNode>& node() const { return fImageFilterEffect; } |
47 | |
48 | private: |
49 | explicit DropShadowAdapter(sk_sp<sksg::RenderNode> layer) |
50 | : fDropShadow(sksg::DropShadowImageFilter::Make()) |
51 | , fImageFilterEffect(sksg::ImageFilterEffect::Make(std::move(layer), fDropShadow)) {} |
52 | |
53 | void onSync() override { |
54 | // fColor -> RGB, fOpacity -> A |
55 | const SkColor color = fColor; |
56 | fDropShadow->setColor(SkColorSetA(color, SkTPin(SkScalarRoundToInt(fOpacity), 0, 255))); |
57 | |
58 | // The offset is specified in terms of a bearing + distance. |
59 | const auto rad = SkDegreesToRadians(90 - fDirection); |
60 | fDropShadow->setOffset(SkVector::Make( fDistance * SkScalarCos(rad), |
61 | -fDistance * SkScalarSin(rad))); |
62 | |
63 | const auto sigma = fSoftness * kBlurSizeToSigma; |
64 | fDropShadow->setSigma(SkVector::Make(sigma, sigma)); |
65 | |
66 | fDropShadow->setMode(SkToBool(fShdwOnly) |
67 | ? sksg::DropShadowImageFilter::Mode::kShadowOnly |
68 | : sksg::DropShadowImageFilter::Mode::kShadowAndForeground); |
69 | } |
70 | |
71 | const sk_sp<sksg::DropShadowImageFilter> fDropShadow; |
72 | const sk_sp<sksg::RenderNode> fImageFilterEffect; |
73 | |
74 | VectorValue fColor = { 0, 0, 0, 1 }; |
75 | ScalarValue fOpacity = 255, |
76 | fDirection = 0, |
77 | fDistance = 0, |
78 | fSoftness = 0, |
79 | fShdwOnly = 0; |
80 | }; |
81 | |
82 | } // namespace |
83 | |
84 | sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowEffect(const skjson::ArrayValue& jprops, |
85 | sk_sp<sksg::RenderNode> layer) const { |
86 | return fBuilder->attachDiscardableAdapter<DropShadowAdapter>(jprops, |
87 | std::move(layer), |
88 | *fBuilder); |
89 | } |
90 | |
91 | } // namespace internal |
92 | } // namespace skottie |
93 | |