1 /*
2 * Copyright 2017 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/SkottieValue.h"
9
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkSize.h"
13 #include "include/private/SkNx.h"
14 #include "modules/skottie/src/SkottieJson.h"
15 #include "modules/skottie/src/SkottiePriv.h"
16
17 namespace skottie {
18
19 template <>
FromJSON(const skjson::Value & jv,const internal::AnimationBuilder *,ScalarValue * v)20 bool ValueTraits<ScalarValue>::FromJSON(const skjson::Value& jv, const internal::AnimationBuilder*,
21 ScalarValue* v) {
22 return Parse(jv, v);
23 }
24
25 template <>
CanLerp(const ScalarValue &,const ScalarValue &)26 bool ValueTraits<ScalarValue>::CanLerp(const ScalarValue&, const ScalarValue&) {
27 return true;
28 }
29
30 template <>
Lerp(const ScalarValue & v0,const ScalarValue & v1,float t,ScalarValue * result)31 void ValueTraits<ScalarValue>::Lerp(const ScalarValue& v0, const ScalarValue& v1, float t,
32 ScalarValue* result) {
33 *result = v0 + (v1 - v0) * t;
34 }
35
36 template <>
37 template <>
As(const ScalarValue & v)38 SkScalar ValueTraits<ScalarValue>::As<SkScalar>(const ScalarValue& v) {
39 return v;
40 }
41
42 template <>
FromJSON(const skjson::Value & jv,const internal::AnimationBuilder *,VectorValue * v)43 bool ValueTraits<VectorValue>::FromJSON(const skjson::Value& jv, const internal::AnimationBuilder*,
44 VectorValue* v) {
45 return Parse(jv, v);
46 }
47
48 template <>
CanLerp(const VectorValue & v1,const VectorValue & v2)49 bool ValueTraits<VectorValue>::CanLerp(const VectorValue& v1, const VectorValue& v2) {
50 return v1.size() == v2.size();
51 }
52
53 template <>
Lerp(const VectorValue & v0,const VectorValue & v1,float t,VectorValue * result)54 void ValueTraits<VectorValue>::Lerp(const VectorValue& v0, const VectorValue& v1, float t,
55 VectorValue* result) {
56 SkASSERT(v0.size() == v1.size());
57
58 result->resize(v0.size());
59
60 for (size_t i = 0; i < v0.size(); ++i) {
61 ValueTraits<ScalarValue>::Lerp(v0[i], v1[i], t, &(*result)[i]);
62 }
63 }
64
65 template <>
66 template <>
As(const VectorValue & v)67 SkColor ValueTraits<VectorValue>::As<SkColor>(const VectorValue& v) {
68 // best effort to turn this into a color
69 const auto r = v.size() > 0 ? v[0] : 0,
70 g = v.size() > 1 ? v[1] : 0,
71 b = v.size() > 2 ? v[2] : 0,
72 a = v.size() > 3 ? v[3] : 1;
73
74 return SkColorSetARGB(SkScalarRoundToInt(SkTPin(a, 0.0f, 1.0f) * 255),
75 SkScalarRoundToInt(SkTPin(r, 0.0f, 1.0f) * 255),
76 SkScalarRoundToInt(SkTPin(g, 0.0f, 1.0f) * 255),
77 SkScalarRoundToInt(SkTPin(b, 0.0f, 1.0f) * 255));
78 }
79
80 template <>
81 template <>
As(const VectorValue & vec)82 SkPoint ValueTraits<VectorValue>::As<SkPoint>(const VectorValue& vec) {
83 // best effort to turn this into a point
84 const auto x = vec.size() > 0 ? vec[0] : 0,
85 y = vec.size() > 1 ? vec[1] : 0;
86 return SkPoint::Make(x, y);
87 }
88
89 template <>
90 template <>
As(const VectorValue & vec)91 SkSize ValueTraits<VectorValue>::As<SkSize>(const VectorValue& vec) {
92 const auto pt = ValueTraits::As<SkPoint>(vec);
93 return SkSize::Make(pt.x(), pt.y());
94 }
95
96 namespace {
97
ParsePointVec(const skjson::Value & jv,std::vector<SkPoint> * pts)98 bool ParsePointVec(const skjson::Value& jv, std::vector<SkPoint>* pts) {
99 if (!jv.is<skjson::ArrayValue>())
100 return false;
101 const auto& av = jv.as<skjson::ArrayValue>();
102
103 pts->clear();
104 pts->reserve(av.size());
105
106 std::vector<float> vec;
107 for (size_t i = 0; i < av.size(); ++i) {
108 if (!Parse(av[i], &vec) || vec.size() != 2)
109 return false;
110 pts->push_back(SkPoint::Make(vec[0], vec[1]));
111 }
112
113 return true;
114 }
115
116 } // namespace
117
118 template <>
FromJSON(const skjson::Value & jv,const internal::AnimationBuilder * abuilder,ShapeValue * v)119 bool ValueTraits<ShapeValue>::FromJSON(const skjson::Value& jv,
120 const internal::AnimationBuilder* abuilder,
121 ShapeValue* v) {
122 SkASSERT(v->fVertices.empty());
123
124 // Some versions wrap values as single-element arrays.
125 if (const skjson::ArrayValue* av = jv) {
126 if (av->size() == 1) {
127 return FromJSON((*av)[0], abuilder, v);
128 }
129 }
130
131 if (!jv.is<skjson::ObjectValue>())
132 return false;
133 const auto& ov = jv.as<skjson::ObjectValue>();
134
135 std::vector<SkPoint> verts, // Cubic Bezier vertices.
136 inPts, // Cubic Bezier "in" control points, relative to vertices.
137 outPts; // Cubic Bezier "out" control points, relative to vertices.
138
139 if (!ParsePointVec(ov["v"], &verts)) {
140 // Vertices are required.
141 return false;
142 }
143
144 // In/out points are optional.
145 ParsePointVec(ov["i"], &inPts);
146 if (!inPts.empty() && inPts.size() != verts.size()) {
147 return false;
148 }
149 inPts.resize(verts.size(), { 0, 0 });
150
151 ParsePointVec(ov["o"], &outPts);
152 if (!outPts.empty() && outPts.size() != verts.size()) {
153 return false;
154 }
155 outPts.resize(verts.size(), { 0, 0 });
156
157 v->fVertices.reserve(inPts.size());
158 for (size_t i = 0; i < inPts.size(); ++i) {
159 v->fVertices.push_back(BezierVertex({inPts[i], outPts[i], verts[i]}));
160 }
161 v->fClosed = ParseDefault<bool>(ov["c"], false);
162
163 return true;
164 }
165
166 template <>
CanLerp(const ShapeValue & v1,const ShapeValue & v2)167 bool ValueTraits<ShapeValue>::CanLerp(const ShapeValue& v1, const ShapeValue& v2) {
168 return v1.fVertices.size() == v2.fVertices.size()
169 && v1.fClosed == v2.fClosed;
170 }
171
lerp_point(const SkPoint & v0,const SkPoint & v1,const Sk2f & t)172 static SkPoint lerp_point(const SkPoint& v0, const SkPoint& v1, const Sk2f& t) {
173 const auto v2f0 = Sk2f::Load(&v0),
174 v2f1 = Sk2f::Load(&v1);
175
176 SkPoint v;
177 (v2f0 + (v2f1 - v2f0) * t).store(&v);
178
179 return v;
180 }
181
182 template <>
Lerp(const ShapeValue & v0,const ShapeValue & v1,float t,ShapeValue * result)183 void ValueTraits<ShapeValue>::Lerp(const ShapeValue& v0, const ShapeValue& v1, float t,
184 ShapeValue* result) {
185 SkASSERT(v0.fVertices.size() == v1.fVertices.size());
186 SkASSERT(v0.fClosed == v1.fClosed);
187
188 result->fClosed = v0.fClosed;
189 result->fVolatile = true; // interpolated values are volatile
190
191 const auto t2f = Sk2f(t);
192 result->fVertices.resize(v0.fVertices.size());
193
194 for (size_t i = 0; i < v0.fVertices.size(); ++i) {
195 result->fVertices[i] = BezierVertex({
196 lerp_point(v0.fVertices[i].fInPoint , v1.fVertices[i].fInPoint , t2f),
197 lerp_point(v0.fVertices[i].fOutPoint, v1.fVertices[i].fOutPoint, t2f),
198 lerp_point(v0.fVertices[i].fVertex , v1.fVertices[i].fVertex , t2f)
199 });
200 }
201 }
202
203 template <>
204 template <>
As(const ShapeValue & shape)205 SkPath ValueTraits<ShapeValue>::As<SkPath>(const ShapeValue& shape) {
206 SkPath path;
207
208 if (!shape.fVertices.empty()) {
209 // conservatively assume all cubics
210 path.incReserve(1 + SkToU32(shape.fVertices.size() * 3));
211
212 path.moveTo(shape.fVertices.front().fVertex);
213 }
214
215 const auto& addCubic = [&](size_t from, size_t to) {
216 const auto c0 = shape.fVertices[from].fVertex + shape.fVertices[from].fOutPoint,
217 c1 = shape.fVertices[to].fVertex + shape.fVertices[to].fInPoint;
218
219 if (c0 == shape.fVertices[from].fVertex &&
220 c1 == shape.fVertices[to].fVertex) {
221 // If the control points are coincident, we can power-reduce to a straight line.
222 // TODO: we could also do that when the controls are on the same line as the
223 // vertices, but it's unclear how common that case is.
224 path.lineTo(shape.fVertices[to].fVertex);
225 } else {
226 path.cubicTo(c0, c1, shape.fVertices[to].fVertex);
227 }
228 };
229
230 for (size_t i = 1; i < shape.fVertices.size(); ++i) {
231 addCubic(i - 1, i);
232 }
233
234 if (!shape.fVertices.empty() && shape.fClosed) {
235 addCubic(shape.fVertices.size() - 1, 0);
236 path.close();
237 }
238
239 path.setIsVolatile(shape.fVolatile);
240 path.shrinkToFit();
241
242 return path;
243 }
244
245 } // namespace skottie
246