OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkPatch.h" | 8 #include "SkPatch.h" |
9 | 9 |
10 #include "SkGeometry.h" | 10 #include "SkGeometry.h" |
11 #include "SkColorPriv.h" | 11 #include "SkColorPriv.h" |
| 12 #include "SkBuffer.h" |
12 | 13 |
13 //////////////////////////////////////////////////////////////////////////////// | 14 //////////////////////////////////////////////////////////////////////////////// |
14 | 15 |
15 /** | 16 /** |
16 * Evaluator to sample the values of a cubic bezier using forward differences. | 17 * Evaluator to sample the values of a cubic bezier using forward differences. |
17 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only | 18 * Forward differences is a method for evaluating a nth degree polynomial at a u
niform step by only |
18 * adding precalculated values. | 19 * adding precalculated values. |
19 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h | 20 * For a linear example we have the function f(t) = m*t+b, then the value of tha
t function at t+h |
20 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must
add to the first | 21 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must
add to the first |
21 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After | 22 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t
+ b = mh. After |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 return fPoints; | 111 return fPoints; |
111 } | 112 } |
112 | 113 |
113 private: | 114 private: |
114 int fMax, fCurrent, fDivisions; | 115 int fMax, fCurrent, fDivisions; |
115 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; | 116 SkPoint fFwDiff[4], fCoefs[4], fPoints[4]; |
116 }; | 117 }; |
117 | 118 |
118 //////////////////////////////////////////////////////////////////////////////// | 119 //////////////////////////////////////////////////////////////////////////////// |
119 | 120 |
120 SkPatch::SkPatch(SkPoint points[12], SkColor colors[4]) { | 121 SkPatch::SkPatch(const SkPoint points[12], const SkColor colors[4]) { |
121 | 122 this->reset(points, colors); |
122 for (int i = 0; i < 12; i++) { | |
123 fCtrlPoints[i] = points[i]; | |
124 } | |
125 for (int i = 0; i < 4; i++) { | |
126 fCornerColors[i] = colors[i]; | |
127 } | |
128 | |
129 } | 123 } |
130 | 124 |
131 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { | 125 uint8_t bilinear(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar
c01, SkScalar c11) { |
132 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 126 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
133 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 127 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
134 return uint8_t(a * (1.f - ty) + b * ty); | 128 return uint8_t(a * (1.f - ty) + b * ty); |
135 } | 129 } |
136 | 130 |
137 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ | 131 bool SkPatch::getVertexData(SkPatch::VertexData* data, int lodX, int lodY) const
{ |
138 | 132 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 data->fIndices[i + 3] = data->fIndices[i]; | 223 data->fIndices[i + 3] = data->fIndices[i]; |
230 data->fIndices[i + 4] = data->fIndices[i + 2]; | 224 data->fIndices[i + 4] = data->fIndices[i + 2]; |
231 data->fIndices[i + 5] = (x + 1) * stride + y; | 225 data->fIndices[i + 5] = (x + 1) * stride + y; |
232 } | 226 } |
233 v = SkScalarClampMax(v + 1.f / lodY, 1); | 227 v = SkScalarClampMax(v + 1.f / lodY, 1); |
234 } | 228 } |
235 u = SkScalarClampMax(u + 1.f / lodX, 1); | 229 u = SkScalarClampMax(u + 1.f / lodX, 1); |
236 } | 230 } |
237 return true; | 231 return true; |
238 } | 232 } |
| 233 |
| 234 size_t SkPatch::writeToMemory(void* storage) const { |
| 235 int byteCount = kNumCtrlPts * sizeof(SkPoint) + kNumColors * sizeof(SkColor
); |
| 236 |
| 237 if (NULL == storage) { |
| 238 return SkAlign4(byteCount); |
| 239 } |
| 240 |
| 241 SkWBuffer buffer(storage); |
| 242 |
| 243 buffer.write(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint)); |
| 244 buffer.write(fCornerColors, kNumColors * sizeof(SkColor)); |
| 245 |
| 246 buffer.padToAlign4(); |
| 247 return buffer.pos(); |
| 248 } |
| 249 |
| 250 size_t SkPatch::readFromMemory(const void* storage, size_t length) { |
| 251 SkRBufferWithSizeCheck buffer(storage, length); |
| 252 |
| 253 int byteCount = 0; |
| 254 |
| 255 if (!buffer.read(fCtrlPoints, kNumCtrlPts * sizeof(SkPoint))) { |
| 256 return byteCount; |
| 257 } |
| 258 byteCount += kNumCtrlPts * sizeof(SkPoint); |
| 259 |
| 260 if (!buffer.read(fCornerColors, kNumColors * sizeof(SkColor))) { |
| 261 return byteCount; |
| 262 } |
| 263 byteCount += kNumColors * sizeof(SkColor); |
| 264 |
| 265 return byteCount; |
| 266 } |
OLD | NEW |