blob: d327955a2552d9cae66809b078502166fd0f77bc [file] [log] [blame]
Florin Malitaa6e30f72018-03-23 13:41:58 -04001/*
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 "SkottieValue.h"
9
10#include "SkColor.h"
Florin Malitac353ee22018-04-30 21:49:41 -040011#include "SkNx.h"
Florin Malitaa6e30f72018-03-23 13:41:58 -040012#include "SkPoint.h"
13#include "SkSize.h"
14
15namespace skottie {
16
17template <>
18size_t ValueTraits<ScalarValue>::Cardinality(const ScalarValue&) {
19 return 1;
20}
21
22template <>
Florin Malita30f4e962018-08-07 11:59:55 -040023void ValueTraits<ScalarValue>::Lerp(const ScalarValue& v0, const ScalarValue& v1, float t,
24 ScalarValue* result) {
Florin Malitac353ee22018-04-30 21:49:41 -040025 SkASSERT(t >= 0 && t <= 1);
Florin Malita30f4e962018-08-07 11:59:55 -040026 *result = v0 + (v1 - v0) * t;
Florin Malitac353ee22018-04-30 21:49:41 -040027}
28
29template <>
Florin Malitaa6e30f72018-03-23 13:41:58 -040030template <>
31SkScalar ValueTraits<ScalarValue>::As<SkScalar>(const ScalarValue& v) {
32 return v;
33}
34
35template <>
36size_t ValueTraits<VectorValue>::Cardinality(const VectorValue& vec) {
37 return vec.size();
38}
39
40template <>
Florin Malita30f4e962018-08-07 11:59:55 -040041void ValueTraits<VectorValue>::Lerp(const VectorValue& v0, const VectorValue& v1, float t,
42 VectorValue* result) {
Florin Malitac353ee22018-04-30 21:49:41 -040043 SkASSERT(v0.size() == v1.size());
44
Florin Malita30f4e962018-08-07 11:59:55 -040045 result->resize(v0.size());
Florin Malitac353ee22018-04-30 21:49:41 -040046
47 for (size_t i = 0; i < v0.size(); ++i) {
Florin Malita30f4e962018-08-07 11:59:55 -040048 ValueTraits<ScalarValue>::Lerp(v0[i], v1[i], t, &(*result)[i]);
Florin Malitac353ee22018-04-30 21:49:41 -040049 }
Florin Malitac353ee22018-04-30 21:49:41 -040050}
51
52template <>
Florin Malitaa6e30f72018-03-23 13:41:58 -040053template <>
54SkColor ValueTraits<VectorValue>::As<SkColor>(const VectorValue& v) {
55 // best effort to turn this into a color
56 const auto r = v.size() > 0 ? v[0] : 0,
57 g = v.size() > 1 ? v[1] : 0,
58 b = v.size() > 2 ? v[2] : 0,
59 a = v.size() > 3 ? v[3] : 1;
60
Florin Malita92206a42018-06-10 17:10:58 -040061 return SkColorSetARGB(SkScalarRoundToInt(SkTPin(a, 0.0f, 1.0f) * 255),
62 SkScalarRoundToInt(SkTPin(r, 0.0f, 1.0f) * 255),
63 SkScalarRoundToInt(SkTPin(g, 0.0f, 1.0f) * 255),
64 SkScalarRoundToInt(SkTPin(b, 0.0f, 1.0f) * 255));
Florin Malitaa6e30f72018-03-23 13:41:58 -040065}
66
67template <>
68template <>
69SkPoint ValueTraits<VectorValue>::As<SkPoint>(const VectorValue& vec) {
70 // best effort to turn this into a point
71 const auto x = vec.size() > 0 ? vec[0] : 0,
72 y = vec.size() > 1 ? vec[1] : 0;
73 return SkPoint::Make(x, y);
74}
75
76template <>
77template <>
78SkSize ValueTraits<VectorValue>::As<SkSize>(const VectorValue& vec) {
79 const auto pt = ValueTraits::As<SkPoint>(vec);
80 return SkSize::Make(pt.x(), pt.y());
81}
82
83template <>
Florin Malitac353ee22018-04-30 21:49:41 -040084size_t ValueTraits<ShapeValue>::Cardinality(const ShapeValue& shape) {
85 return shape.fVertices.size();
86}
87
88static SkPoint lerp_point(const SkPoint& v0, const SkPoint& v1, const Sk2f& t) {
89 const auto v2f0 = Sk2f::Load(&v0),
90 v2f1 = Sk2f::Load(&v1);
91
92 SkPoint v;
93 (v2f0 + (v2f1 - v2f0) * t).store(&v);
94
95 return v;
96}
97
98template <>
Florin Malita30f4e962018-08-07 11:59:55 -040099void ValueTraits<ShapeValue>::Lerp(const ShapeValue& v0, const ShapeValue& v1, float t,
100 ShapeValue* result) {
Florin Malitac353ee22018-04-30 21:49:41 -0400101 SkASSERT(t >= 0 && t <= 1);
102 SkASSERT(v0.fVertices.size() == v1.fVertices.size());
103 SkASSERT(v0.fClosed == v1.fClosed);
104
Florin Malita30f4e962018-08-07 11:59:55 -0400105 result->fClosed = v0.fClosed;
106 result->fVolatile = true; // interpolated values are volatile
Florin Malitac353ee22018-04-30 21:49:41 -0400107
108 const auto t2f = Sk2f(t);
Florin Malita30f4e962018-08-07 11:59:55 -0400109 result->fVertices.resize(v0.fVertices.size());
Florin Malitac353ee22018-04-30 21:49:41 -0400110
111 for (size_t i = 0; i < v0.fVertices.size(); ++i) {
Florin Malita30f4e962018-08-07 11:59:55 -0400112 result->fVertices[i] = BezierVertex({
Florin Malitac353ee22018-04-30 21:49:41 -0400113 lerp_point(v0.fVertices[i].fInPoint , v1.fVertices[i].fInPoint , t2f),
114 lerp_point(v0.fVertices[i].fOutPoint, v1.fVertices[i].fOutPoint, t2f),
115 lerp_point(v0.fVertices[i].fVertex , v1.fVertices[i].fVertex , t2f)
Florin Malita30f4e962018-08-07 11:59:55 -0400116 });
Florin Malitac353ee22018-04-30 21:49:41 -0400117 }
Florin Malitaa6e30f72018-03-23 13:41:58 -0400118}
119
120template <>
121template <>
Florin Malitac353ee22018-04-30 21:49:41 -0400122SkPath ValueTraits<ShapeValue>::As<SkPath>(const ShapeValue& shape) {
123 SkPath path;
124
125 if (!shape.fVertices.empty()) {
Florin Malita3c992962018-08-08 12:06:40 -0400126 // conservatively assume all cubics
127 path.incReserve(1 + SkToU32(shape.fVertices.size() * 3));
128
Florin Malitac353ee22018-04-30 21:49:41 -0400129 path.moveTo(shape.fVertices.front().fVertex);
130 }
131
132 const auto& addCubic = [&](size_t from, size_t to) {
133 const auto c0 = shape.fVertices[from].fVertex + shape.fVertices[from].fOutPoint,
134 c1 = shape.fVertices[to].fVertex + shape.fVertices[to].fInPoint;
135
136 if (c0 == shape.fVertices[from].fVertex &&
137 c1 == shape.fVertices[to].fVertex) {
138 // If the control points are coincident, we can power-reduce to a straight line.
139 // TODO: we could also do that when the controls are on the same line as the
140 // vertices, but it's unclear how common that case is.
141 path.lineTo(shape.fVertices[to].fVertex);
142 } else {
143 path.cubicTo(c0, c1, shape.fVertices[to].fVertex);
144 }
145 };
146
147 for (size_t i = 1; i < shape.fVertices.size(); ++i) {
148 addCubic(i - 1, i);
149 }
150
151 if (!shape.fVertices.empty() && shape.fClosed) {
152 addCubic(shape.fVertices.size() - 1, 0);
153 path.close();
154 }
155
156 path.setIsVolatile(shape.fVolatile);
157
Florin Malitaa6e30f72018-03-23 13:41:58 -0400158 return path;
159}
160
161} // namespace skottie