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
14namespace skottie {
15namespace internal {
16
17namespace {
18
19class DropShadowAdapter final : public AnimatablePropertyContainer {
20public:
21 static sk_sp<DropShadowAdapter> MakeEffect(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), Type::fEffect));
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 static sk_sp<DropShadowAdapter> MakeStyle(const skjson::ObjectValue& jstyle,
47 sk_sp<sksg::RenderNode> layer,
48 const AnimationBuilder& abuilder) {
49 sk_sp<DropShadowAdapter> adapter(new DropShadowAdapter(std::move(layer), Type::fStyle));
50
51 adapter->bind(abuilder, jstyle["a"], adapter->fDirection);
52 adapter->bind(abuilder, jstyle["c"], adapter->fColor );
53 adapter->bind(abuilder, jstyle["d"], adapter->fDistance );
54 adapter->bind(abuilder, jstyle["o"], adapter->fOpacity );
55 adapter->bind(abuilder, jstyle["s"], adapter->fSoftness );
56
57 return adapter;
58 }
59
60 const sk_sp<sksg::RenderNode>& node() const { return fImageFilterEffect; }
61
62private:
63 enum class Type { fEffect, fStyle };
64 DropShadowAdapter(sk_sp<sksg::RenderNode> layer, Type ty)
65 : fDropShadow(sksg::DropShadowImageFilter::Make())
66 , fImageFilterEffect(sksg::ImageFilterEffect::Make(std::move(layer), fDropShadow))
67 , fType(ty) {
68 fOpacity = this->maxOpacity();
69 }
70
71 void onSync() override {
72 // fColor -> RGB, fOpacity -> A
73 const SkColor color = fColor;
74 fDropShadow->setColor(SkColorSetA(color,
75 SkScalarRoundToInt(SkTPin(fOpacity / this->maxOpacity(),
76 0.0f, 1.0f) * 255)));
77
78 // The offset is specified in terms of an angle + distance.
79 const auto rad = SkDegreesToRadians(fType == Type::fEffect
80 ? 90 - fDirection // bearing (effect)
81 : 180 + fDirection); // 0deg -> left (style)
82 fDropShadow->setOffset(SkVector::Make( fDistance * SkScalarCos(rad),
83 -fDistance * SkScalarSin(rad)));
84
85 const auto sigma = fSoftness * kBlurSizeToSigma;
86 fDropShadow->setSigma(SkVector::Make(sigma, sigma));
87
88 fDropShadow->setMode(SkToBool(fShdwOnly)
89 ? sksg::DropShadowImageFilter::Mode::kShadowOnly
90 : sksg::DropShadowImageFilter::Mode::kShadowAndForeground);
91 }
92
93 float maxOpacity() const {
94 return fType == Type::fEffect
95 ? 255.0f // effect: 0 - 255
96 : 100.0f; // style : 0 - 100
97 }
98
99 const sk_sp<sksg::DropShadowImageFilter> fDropShadow;
100 const sk_sp<sksg::RenderNode> fImageFilterEffect;
101 const Type fType;
102
103 VectorValue fColor = { 0, 0, 0, 1 };
104 ScalarValue fOpacity, // initialized explicitly depending on type
105 fDirection = 0,
106 fDistance = 0,
107 fSoftness = 0,
108 fShdwOnly = 0;
109};
110
111} // anonymous ns
112
113sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowEffect(const skjson::ArrayValue& jprops,
114 sk_sp<sksg::RenderNode> layer) const {
115 auto adapter = DropShadowAdapter::MakeEffect(jprops, std::move(layer), *fBuilder);
116 auto effect_node = adapter->node();
117
118 fBuilder->attachDiscardableAdapter(std::move(adapter));
119
120 return effect_node;
121}
122
123sk_sp<sksg::RenderNode> EffectBuilder::attachDropShadowStyle(const skjson::ObjectValue& jstyle,
124 sk_sp<sksg::RenderNode> layer) const {
125 auto adapter = DropShadowAdapter::MakeStyle(jstyle, std::move(layer), *fBuilder);
126 auto effect_node = adapter->node();
127
128 fBuilder->attachDiscardableAdapter(std::move(adapter));
129
130 return effect_node;
131}
132
133} // namespace internal
134} // namespace skottie
135