Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 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 "SkottieValue.h" |
| 9 | |
| 10 | #include "SkColor.h" |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 11 | #include "SkNx.h" |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 12 | #include "SkPoint.h" |
| 13 | #include "SkSize.h" |
| 14 | |
| 15 | namespace skottie { |
| 16 | |
| 17 | template <> |
| 18 | size_t ValueTraits<ScalarValue>::Cardinality(const ScalarValue&) { |
| 19 | return 1; |
| 20 | } |
| 21 | |
| 22 | template <> |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 23 | void ValueTraits<ScalarValue>::Lerp(const ScalarValue& v0, const ScalarValue& v1, float t, |
| 24 | ScalarValue* result) { |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 25 | SkASSERT(t >= 0 && t <= 1); |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 26 | *result = v0 + (v1 - v0) * t; |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | template <> |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 30 | template <> |
| 31 | SkScalar ValueTraits<ScalarValue>::As<SkScalar>(const ScalarValue& v) { |
| 32 | return v; |
| 33 | } |
| 34 | |
| 35 | template <> |
| 36 | size_t ValueTraits<VectorValue>::Cardinality(const VectorValue& vec) { |
| 37 | return vec.size(); |
| 38 | } |
| 39 | |
| 40 | template <> |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 41 | void ValueTraits<VectorValue>::Lerp(const VectorValue& v0, const VectorValue& v1, float t, |
| 42 | VectorValue* result) { |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 43 | SkASSERT(v0.size() == v1.size()); |
| 44 | |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 45 | result->resize(v0.size()); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 46 | |
| 47 | for (size_t i = 0; i < v0.size(); ++i) { |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 48 | ValueTraits<ScalarValue>::Lerp(v0[i], v1[i], t, &(*result)[i]); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 49 | } |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | template <> |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 53 | template <> |
| 54 | SkColor 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 Malita | 92206a4 | 2018-06-10 17:10:58 -0400 | [diff] [blame] | 61 | 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 Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | template <> |
| 68 | template <> |
| 69 | SkPoint 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 | |
| 76 | template <> |
| 77 | template <> |
| 78 | SkSize 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 | |
| 83 | template <> |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 84 | size_t ValueTraits<ShapeValue>::Cardinality(const ShapeValue& shape) { |
| 85 | return shape.fVertices.size(); |
| 86 | } |
| 87 | |
| 88 | static 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 | |
| 98 | template <> |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 99 | void ValueTraits<ShapeValue>::Lerp(const ShapeValue& v0, const ShapeValue& v1, float t, |
| 100 | ShapeValue* result) { |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 101 | SkASSERT(t >= 0 && t <= 1); |
| 102 | SkASSERT(v0.fVertices.size() == v1.fVertices.size()); |
| 103 | SkASSERT(v0.fClosed == v1.fClosed); |
| 104 | |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 105 | result->fClosed = v0.fClosed; |
| 106 | result->fVolatile = true; // interpolated values are volatile |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 107 | |
| 108 | const auto t2f = Sk2f(t); |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 109 | result->fVertices.resize(v0.fVertices.size()); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 110 | |
| 111 | for (size_t i = 0; i < v0.fVertices.size(); ++i) { |
Florin Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 112 | result->fVertices[i] = BezierVertex({ |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 113 | 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 Malita | 30f4e96 | 2018-08-07 11:59:55 -0400 | [diff] [blame] | 116 | }); |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 117 | } |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | template <> |
| 121 | template <> |
Florin Malita | c353ee2 | 2018-04-30 21:49:41 -0400 | [diff] [blame] | 122 | SkPath ValueTraits<ShapeValue>::As<SkPath>(const ShapeValue& shape) { |
| 123 | SkPath path; |
| 124 | |
| 125 | if (!shape.fVertices.empty()) { |
| 126 | path.moveTo(shape.fVertices.front().fVertex); |
| 127 | } |
| 128 | |
| 129 | const auto& addCubic = [&](size_t from, size_t to) { |
| 130 | const auto c0 = shape.fVertices[from].fVertex + shape.fVertices[from].fOutPoint, |
| 131 | c1 = shape.fVertices[to].fVertex + shape.fVertices[to].fInPoint; |
| 132 | |
| 133 | if (c0 == shape.fVertices[from].fVertex && |
| 134 | c1 == shape.fVertices[to].fVertex) { |
| 135 | // If the control points are coincident, we can power-reduce to a straight line. |
| 136 | // TODO: we could also do that when the controls are on the same line as the |
| 137 | // vertices, but it's unclear how common that case is. |
| 138 | path.lineTo(shape.fVertices[to].fVertex); |
| 139 | } else { |
| 140 | path.cubicTo(c0, c1, shape.fVertices[to].fVertex); |
| 141 | } |
| 142 | }; |
| 143 | |
| 144 | for (size_t i = 1; i < shape.fVertices.size(); ++i) { |
| 145 | addCubic(i - 1, i); |
| 146 | } |
| 147 | |
| 148 | if (!shape.fVertices.empty() && shape.fClosed) { |
| 149 | addCubic(shape.fVertices.size() - 1, 0); |
| 150 | path.close(); |
| 151 | } |
| 152 | |
| 153 | path.setIsVolatile(shape.fVolatile); |
| 154 | |
Florin Malita | a6e30f7 | 2018-03-23 13:41:58 -0400 | [diff] [blame] | 155 | return path; |
| 156 | } |
| 157 | |
| 158 | } // namespace skottie |