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/SkottieJson.h"
11#include "modules/sksg/include/SkSGRenderEffect.h"
12#include "src/utils/SkJSON.h"
13
14#include <algorithm>
15#include <iterator>
16
17namespace skottie {
18namespace internal {
19
20EffectBuilder::EffectBuilder(const AnimationBuilder* abuilder, const SkSize& layer_size)
21 : fBuilder(abuilder)
22 , fLayerSize(layer_size) {}
23
24EffectBuilder::EffectBuilderT EffectBuilder::findBuilder(const skjson::ObjectValue& jeffect) const {
25 static constexpr struct BuilderInfo {
26 const char* fName;
27 EffectBuilderT fBuilder;
28 } gBuilderInfo[] = {
29 { "ADBE Corner Pin" , &EffectBuilder::attachCornerPinEffect },
30 { "ADBE Drop Shadow" , &EffectBuilder::attachDropShadowEffect },
31 { "ADBE Easy Levels2" , &EffectBuilder::attachEasyLevelsEffect },
32 { "ADBE Fill" , &EffectBuilder::attachFillEffect },
33 { "ADBE Gaussian Blur 2", &EffectBuilder::attachGaussianBlurEffect },
34 { "ADBE Geometry2" , &EffectBuilder::attachTransformEffect },
35 { "ADBE HUE SATURATION" , &EffectBuilder::attachHueSaturationEffect },
36 { "ADBE Invert" , &EffectBuilder::attachInvertEffect },
37 { "ADBE Linear Wipe" , &EffectBuilder::attachLinearWipeEffect },
38 { "ADBE Pro Levels2" , &EffectBuilder::attachProLevelsEffect },
39 { "ADBE Radial Wipe" , &EffectBuilder::attachRadialWipeEffect },
40 { "ADBE Ramp" , &EffectBuilder::attachGradientEffect },
41 { "ADBE Shift Channels" , &EffectBuilder::attachShiftChannelsEffect },
42 { "ADBE Tile" , &EffectBuilder::attachMotionTileEffect },
43 { "ADBE Tint" , &EffectBuilder::attachTintEffect },
44 { "ADBE Tritone" , &EffectBuilder::attachTritoneEffect },
45 { "ADBE Venetian Blinds", &EffectBuilder::attachVenetianBlindsEffect },
46 };
47
48 const skjson::StringValue* mn = jeffect["mn"];
49 if (mn) {
50 const BuilderInfo key { mn->begin(), nullptr };
51 const auto* binfo = std::lower_bound(std::begin(gBuilderInfo),
52 std::end (gBuilderInfo),
53 key,
54 [](const BuilderInfo& a, const BuilderInfo& b) {
55 return strcmp(a.fName, b.fName) < 0;
56 });
57
58 if (binfo != std::end(gBuilderInfo) && !strcmp(binfo->fName, key.fName)) {
59 return binfo->fBuilder;
60 }
61 }
62
63 // Some legacy clients rely solely on the 'ty' field and generate (non-BM) JSON
64 // without a valid 'mn' string. TODO: we should update them and remove this fallback.
65 enum : int32_t {
66 kTint_Effect = 20,
67 kFill_Effect = 21,
68 kTritone_Effect = 23,
69 kDropShadow_Effect = 25,
70 kRadialWipe_Effect = 26,
71 kGaussianBlur_Effect = 29,
72 };
73
74 switch (ParseDefault<int>(jeffect["ty"], -1)) {
75 case kTint_Effect: return &EffectBuilder::attachTintEffect;
76 case kFill_Effect: return &EffectBuilder::attachFillEffect;
77 case kTritone_Effect: return &EffectBuilder::attachTritoneEffect;
78 case kDropShadow_Effect: return &EffectBuilder::attachDropShadowEffect;
79 case kRadialWipe_Effect: return &EffectBuilder::attachRadialWipeEffect;
80 case kGaussianBlur_Effect: return &EffectBuilder::attachGaussianBlurEffect;
81 default: break;
82 }
83
84 fBuilder->log(Logger::Level::kWarning, &jeffect,
85 "Unsupported layer effect: %s", mn ? mn->begin() : "(unknown)");
86
87 return nullptr;
88}
89
90sk_sp<sksg::RenderNode> EffectBuilder::attachEffects(const skjson::ArrayValue& jeffects,
91 sk_sp<sksg::RenderNode> layer) const {
92 if (!layer) {
93 return nullptr;
94 }
95
96 for (const skjson::ObjectValue* jeffect : jeffects) {
97 if (!jeffect) {
98 continue;
99 }
100
101 const auto builder = this->findBuilder(*jeffect);
102 const skjson::ArrayValue* jprops = (*jeffect)["ef"];
103 if (!builder || !jprops) {
104 continue;
105 }
106
107 const AnimationBuilder::AutoPropertyTracker apt(fBuilder, *jeffect);
108 layer = (this->*builder)(*jprops, std::move(layer));
109
110 if (!layer) {
111 fBuilder->log(Logger::Level::kError, jeffect, "Invalid layer effect.");
112 return nullptr;
113 }
114 }
115
116 return layer;
117}
118
119sk_sp<sksg::RenderNode> EffectBuilder::attachStyles(const skjson::ArrayValue& jstyles,
120 sk_sp<sksg::RenderNode> layer) const {
121#if !defined(SKOTTIE_DISABLE_STYLES)
122 if (!layer) {
123 return nullptr;
124 }
125
126 using StyleBuilder =
127 sk_sp<sksg::RenderNode> (EffectBuilder::*)(const skjson::ObjectValue&,
128 sk_sp<sksg::RenderNode>) const;
129 static constexpr StyleBuilder gStyleBuilders[] = {
130 nullptr, // 'ty': 0 -> stroke
131 &EffectBuilder::attachDropShadowStyle, // 'ty': 1 -> drop shadow
132 };
133
134 for (const skjson::ObjectValue* jstyle : jstyles) {
135 if (!jstyle) {
136 continue;
137 }
138
139 const auto style_type =
140 ParseDefault<size_t>((*jstyle)["ty"], std::numeric_limits<size_t>::max());
141 auto builder = style_type < SK_ARRAY_COUNT(gStyleBuilders) ? gStyleBuilders[style_type]
142 : nullptr;
143
144 if (!builder) {
145 fBuilder->log(Logger::Level::kWarning, jstyle, "Unsupported layer style.");
146 continue;
147 }
148
149 layer = (this->*builder)(*jstyle, std::move(layer));
150 }
151#endif // !defined(SKOTTIE_DISABLE_STYLES)
152
153 return layer;
154}
155
156const skjson::Value& EffectBuilder::GetPropValue(const skjson::ArrayValue& jprops,
157 size_t prop_index) {
158 static skjson::NullValue kNull;
159
160 if (prop_index >= jprops.size()) {
161 return kNull;
162 }
163
164 const skjson::ObjectValue* jprop = jprops[prop_index];
165
166 return jprop ? (*jprop)["v"] : kNull;
167}
168
169MaskShaderEffectBase::MaskShaderEffectBase(sk_sp<sksg::RenderNode> child, const SkSize& ls)
170 : fMaskEffectNode(sksg::MaskShaderEffect::Make(std::move(child)))
171 , fLayerSize(ls) {}
172
173void MaskShaderEffectBase::onSync() {
174 const auto minfo = this->onMakeMask();
175
176 fMaskEffectNode->setVisible(minfo.fVisible);
177 fMaskEffectNode->setShader(std::move(minfo.fMaskShader));
178}
179
180} // namespace internal
181} // namespace skottie
182