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 "SkPatchUtils.h" | 8 #include "SkPatchUtils.h" |
9 | 9 |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 10 matching lines...) Expand all Loading... |
21 * to compute the next one. | 21 * to compute the next one. |
22 * | 22 * |
23 * For the cubic case the first difference gives as a result a quadratic polynom
ial to which we can | 23 * For the cubic case the first difference gives as a result a quadratic polynom
ial to which we can |
24 * apply again forward differences and get linear function to which we can apply
again forward | 24 * apply again forward differences and get linear function to which we can apply
again forward |
25 * differences to get a constant difference. This is why we keep an array of siz
e 4, the 0th | 25 * differences to get a constant difference. This is why we keep an array of siz
e 4, the 0th |
26 * position keeps the sampled value while the next ones keep the quadratic, line
ar and constant | 26 * position keeps the sampled value while the next ones keep the quadratic, line
ar and constant |
27 * difference values. | 27 * difference values. |
28 */ | 28 */ |
29 | 29 |
30 class FwDCubicEvaluator { | 30 class FwDCubicEvaluator { |
31 | 31 |
32 public: | 32 public: |
33 | 33 |
34 /** | 34 /** |
35 * Receives the 4 control points of the cubic bezier. | 35 * Receives the 4 control points of the cubic bezier. |
36 */ | 36 */ |
37 | 37 |
38 explicit FwDCubicEvaluator(const SkPoint points[4]) | 38 explicit FwDCubicEvaluator(const SkPoint points[4]) |
39 : fCoefs(points) { | 39 : fCoefs(points) { |
40 memcpy(fPoints, points, 4 * sizeof(SkPoint)); | 40 memcpy(fPoints, points, 4 * sizeof(SkPoint)); |
41 | 41 |
42 this->restart(1); | 42 this->restart(1); |
43 } | 43 } |
44 | 44 |
45 /** | 45 /** |
46 * Restarts the forward differences evaluator to the first value of t = 0. | 46 * Restarts the forward differences evaluator to the first value of t = 0. |
47 */ | 47 */ |
48 void restart(int divisions) { | 48 void restart(int divisions) { |
49 fDivisions = divisions; | 49 fDivisions = divisions; |
50 fCurrent = 0; | 50 fCurrent = 0; |
51 fMax = fDivisions + 1; | 51 fMax = fDivisions + 1; |
52 Sk2s h = Sk2s(1.f / fDivisions); | 52 Sk2s h = Sk2s(1.f / fDivisions); |
53 Sk2s h2 = h * h; | 53 Sk2s h2 = h * h; |
54 Sk2s h3 = h2 * h; | 54 Sk2s h3 = h2 * h; |
55 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3; | 55 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3; |
56 fFwDiff[3] = to_point(fwDiff3); | 56 fFwDiff[3] = to_point(fwDiff3); |
57 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2); | 57 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2); |
58 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h); | 58 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h); |
59 fFwDiff[0] = to_point(fCoefs.fD); | 59 fFwDiff[0] = to_point(fCoefs.fD); |
60 } | 60 } |
61 | 61 |
62 /** | 62 /** |
63 * Check if the evaluator is still within the range of 0<=t<=1 | 63 * Check if the evaluator is still within the range of 0<=t<=1 |
64 */ | 64 */ |
65 bool done() const { | 65 bool done() const { |
66 return fCurrent > fMax; | 66 return fCurrent > fMax; |
67 } | 67 } |
68 | 68 |
69 /** | 69 /** |
70 * Call next to obtain the SkPoint sampled and move to the next one. | 70 * Call next to obtain the SkPoint sampled and move to the next one. |
71 */ | 71 */ |
72 SkPoint next() { | 72 SkPoint next() { |
73 SkPoint point = fFwDiff[0]; | 73 SkPoint point = fFwDiff[0]; |
74 fFwDiff[0] += fFwDiff[1]; | 74 fFwDiff[0] += fFwDiff[1]; |
75 fFwDiff[1] += fFwDiff[2]; | 75 fFwDiff[1] += fFwDiff[2]; |
76 fFwDiff[2] += fFwDiff[3]; | 76 fFwDiff[2] += fFwDiff[3]; |
77 fCurrent++; | 77 fCurrent++; |
78 return point; | 78 return point; |
79 } | 79 } |
80 | 80 |
81 const SkPoint* getCtrlPoints() const { | 81 const SkPoint* getCtrlPoints() const { |
82 return fPoints; | 82 return fPoints; |
83 } | 83 } |
84 | 84 |
85 private: | 85 private: |
86 SkCubicCoeff fCoefs; | 86 SkCubicCoeff fCoefs; |
87 int fMax, fCurrent, fDivisions; | 87 int fMax, fCurrent, fDivisions; |
88 SkPoint fFwDiff[4], fPoints[4]; | 88 SkPoint fFwDiff[4], fPoints[4]; |
89 }; | 89 }; |
90 | 90 |
91 //////////////////////////////////////////////////////////////////////////////// | 91 //////////////////////////////////////////////////////////////////////////////// |
92 | 92 |
93 // size in pixels of each partition per axis, adjust this knob | 93 // size in pixels of each partition per axis, adjust this knob |
94 static const int kPartitionSize = 10; | 94 static const int kPartitionSize = 10; |
(...skipping 13 matching lines...) Expand all Loading... |
108 } | 108 } |
109 | 109 |
110 static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkS
calar c01, | 110 static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkS
calar c01, |
111 SkScalar c11) { | 111 SkScalar c11) { |
112 SkScalar a = c00 * (1.f - tx) + c10 * tx; | 112 SkScalar a = c00 * (1.f - tx) + c10 * tx; |
113 SkScalar b = c01 * (1.f - tx) + c11 * tx; | 113 SkScalar b = c01 * (1.f - tx) + c11 * tx; |
114 return a * (1.f - ty) + b * ty; | 114 return a * (1.f - ty) + b * ty; |
115 } | 115 } |
116 | 116 |
117 SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix*
matrix) { | 117 SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix*
matrix) { |
118 | 118 |
119 // Approximate length of each cubic. | 119 // Approximate length of each cubic. |
120 SkPoint pts[kNumPtsCubic]; | 120 SkPoint pts[kNumPtsCubic]; |
121 SkPatchUtils::getTopCubic(cubics, pts); | 121 SkPatchUtils::getTopCubic(cubics, pts); |
122 matrix->mapPoints(pts, kNumPtsCubic); | 122 matrix->mapPoints(pts, kNumPtsCubic); |
123 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic); | 123 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic); |
124 | 124 |
125 SkPatchUtils::getBottomCubic(cubics, pts); | 125 SkPatchUtils::getBottomCubic(cubics, pts); |
126 matrix->mapPoints(pts, kNumPtsCubic); | 126 matrix->mapPoints(pts, kNumPtsCubic); |
127 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic); | 127 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic); |
128 | 128 |
129 SkPatchUtils::getLeftCubic(cubics, pts); | 129 SkPatchUtils::getLeftCubic(cubics, pts); |
130 matrix->mapPoints(pts, kNumPtsCubic); | 130 matrix->mapPoints(pts, kNumPtsCubic); |
131 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic); | 131 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic); |
132 | 132 |
133 SkPatchUtils::getRightCubic(cubics, pts); | 133 SkPatchUtils::getRightCubic(cubics, pts); |
134 matrix->mapPoints(pts, kNumPtsCubic); | 134 matrix->mapPoints(pts, kNumPtsCubic); |
135 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic); | 135 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic); |
136 | 136 |
137 // Level of detail per axis, based on the larger side between top and bottom
or left and right | 137 // Level of detail per axis, based on the larger side between top and bottom
or left and right |
138 int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitio
nSize); | 138 int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitio
nSize); |
139 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitio
nSize); | 139 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitio
nSize); |
140 | 140 |
141 return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY)); | 141 return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY)); |
142 } | 142 } |
143 | 143 |
144 void SkPatchUtils::getTopCubic(const SkPoint cubics[12], SkPoint points[4]) { | 144 void SkPatchUtils::getTopCubic(const SkPoint cubics[12], SkPoint points[4]) { |
145 points[0] = cubics[kTopP0_CubicCtrlPts]; | 145 points[0] = cubics[kTopP0_CubicCtrlPts]; |
146 points[1] = cubics[kTopP1_CubicCtrlPts]; | 146 points[1] = cubics[kTopP1_CubicCtrlPts]; |
147 points[2] = cubics[kTopP2_CubicCtrlPts]; | 147 points[2] = cubics[kTopP2_CubicCtrlPts]; |
148 points[3] = cubics[kTopP3_CubicCtrlPts]; | 148 points[3] = cubics[kTopP3_CubicCtrlPts]; |
149 } | 149 } |
150 | 150 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 203 |
204 // if colors is not null then create array for colors | 204 // if colors is not null then create array for colors |
205 SkPMColor colorsPM[kNumCorners]; | 205 SkPMColor colorsPM[kNumCorners]; |
206 if (colors) { | 206 if (colors) { |
207 // premultiply colors to avoid color bleeding. | 207 // premultiply colors to avoid color bleeding. |
208 for (int i = 0; i < kNumCorners; i++) { | 208 for (int i = 0; i < kNumCorners; i++) { |
209 colorsPM[i] = SkPreMultiplyColor(colors[i]); | 209 colorsPM[i] = SkPreMultiplyColor(colors[i]); |
210 } | 210 } |
211 data->fColors = new uint32_t[data->fVertexCount]; | 211 data->fColors = new uint32_t[data->fVertexCount]; |
212 } | 212 } |
213 | 213 |
214 // if texture coordinates are not null then create array for them | 214 // if texture coordinates are not null then create array for them |
215 if (texCoords) { | 215 if (texCoords) { |
216 data->fTexCoords = new SkPoint[data->fVertexCount]; | 216 data->fTexCoords = new SkPoint[data->fVertexCount]; |
217 } | 217 } |
218 | 218 |
219 SkPoint pts[kNumPtsCubic]; | 219 SkPoint pts[kNumPtsCubic]; |
220 SkPatchUtils::getBottomCubic(cubics, pts); | 220 SkPatchUtils::getBottomCubic(cubics, pts); |
221 FwDCubicEvaluator fBottom(pts); | 221 FwDCubicEvaluator fBottom(pts); |
222 SkPatchUtils::getTopCubic(cubics, pts); | 222 SkPatchUtils::getTopCubic(cubics, pts); |
223 FwDCubicEvaluator fTop(pts); | 223 FwDCubicEvaluator fTop(pts); |
224 SkPatchUtils::getLeftCubic(cubics, pts); | 224 SkPatchUtils::getLeftCubic(cubics, pts); |
225 FwDCubicEvaluator fLeft(pts); | 225 FwDCubicEvaluator fLeft(pts); |
226 SkPatchUtils::getRightCubic(cubics, pts); | 226 SkPatchUtils::getRightCubic(cubics, pts); |
227 FwDCubicEvaluator fRight(pts); | 227 FwDCubicEvaluator fRight(pts); |
228 | 228 |
229 fBottom.restart(lodX); | 229 fBottom.restart(lodX); |
230 fTop.restart(lodX); | 230 fTop.restart(lodX); |
231 | 231 |
232 SkScalar u = 0.0f; | 232 SkScalar u = 0.0f; |
233 int stride = lodY + 1; | 233 int stride = lodY + 1; |
234 for (int x = 0; x <= lodX; x++) { | 234 for (int x = 0; x <= lodX; x++) { |
235 SkPoint bottom = fBottom.next(), top = fTop.next(); | 235 SkPoint bottom = fBottom.next(), top = fTop.next(); |
236 fLeft.restart(lodY); | 236 fLeft.restart(lodY); |
237 fRight.restart(lodY); | 237 fRight.restart(lodY); |
238 SkScalar v = 0.f; | 238 SkScalar v = 0.f; |
239 for (int y = 0; y <= lodY; y++) { | 239 for (int y = 0; y <= lodY; y++) { |
240 int dataIndex = x * (lodY + 1) + y; | 240 int dataIndex = x * (lodY + 1) + y; |
241 | 241 |
242 SkPoint left = fLeft.next(), right = fRight.next(); | 242 SkPoint left = fLeft.next(), right = fRight.next(); |
243 | 243 |
244 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), | 244 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(), |
245 (1.0f - v) * top.y() + v * bottom.y()); | 245 (1.0f - v) * top.y() + v * bottom.y()); |
246 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), | 246 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(), |
247 (1.0f - u) * left.y() + u * right.y()); | 247 (1.0f - u) * left.y() + u * right.y()); |
248 SkPoint s2 = SkPoint::Make( | 248 SkPoint s2 = SkPoint::Make( |
249 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() | 249 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].x() |
250 + u * fTop.getCtrlPoints()[
3].x()) | 250 + u * fTop.getCtrlPoints()[
3].x()) |
251 + v * ((1.0f - u) * fBottom.getCtrlPoints
()[0].x() | 251 + v * ((1.0f - u) * fBottom.getCtrlPoints
()[0].x() |
252 + u * fBottom.getCtrlPoints()[3].x
()), | 252 + u * fBottom.getCtrlPoints()[3].x
()), |
253 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() | 253 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPo
ints()[0].y() |
254 + u * fTop.getCtrlPoints()[
3].y()) | 254 + u * fTop.getCtrlPoints()[
3].y()) |
255 + v * ((1.0f - u) * fBottom.getCtrlPoints
()[0].y() | 255 + v * ((1.0f - u) * fBottom.getCtrlPoints
()[0].y() |
256 + u * fBottom.getCtrlPoints()[3].y
())); | 256 + u * fBottom.getCtrlPoints()[3].y
())); |
257 data->fPoints[dataIndex] = s0 + s1 - s2; | 257 data->fPoints[dataIndex] = s0 + s1 - s2; |
258 | 258 |
259 if (colors) { | 259 if (colors) { |
260 uint8_t a = uint8_t(bilerp(u, v, | 260 uint8_t a = uint8_t(bilerp(u, v, |
261 SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner
])), | 261 SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner
])), |
262 SkScalar(SkColorGetA(colorsPM[kTopRight_Corne
r])), | 262 SkScalar(SkColorGetA(colorsPM[kTopRight_Corne
r])), |
263 SkScalar(SkColorGetA(colorsPM[kBottomLeft_Cor
ner])), | 263 SkScalar(SkColorGetA(colorsPM[kBottomLeft_Cor
ner])), |
264 SkScalar(SkColorGetA(colorsPM[kBottomRight_Co
rner])))); | 264 SkScalar(SkColorGetA(colorsPM[kBottomRight_Co
rner])))); |
265 uint8_t r = uint8_t(bilerp(u, v, | 265 uint8_t r = uint8_t(bilerp(u, v, |
266 SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner
])), | 266 SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner
])), |
267 SkScalar(SkColorGetR(colorsPM[kTopRight_Corne
r])), | 267 SkScalar(SkColorGetR(colorsPM[kTopRight_Corne
r])), |
268 SkScalar(SkColorGetR(colorsPM[kBottomLeft_Cor
ner])), | 268 SkScalar(SkColorGetR(colorsPM[kBottomLeft_Cor
ner])), |
269 SkScalar(SkColorGetR(colorsPM[kBottomRight_Co
rner])))); | 269 SkScalar(SkColorGetR(colorsPM[kBottomRight_Co
rner])))); |
270 uint8_t g = uint8_t(bilerp(u, v, | 270 uint8_t g = uint8_t(bilerp(u, v, |
271 SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner
])), | 271 SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner
])), |
272 SkScalar(SkColorGetG(colorsPM[kTopRight_Corne
r])), | 272 SkScalar(SkColorGetG(colorsPM[kTopRight_Corne
r])), |
273 SkScalar(SkColorGetG(colorsPM[kBottomLeft_Cor
ner])), | 273 SkScalar(SkColorGetG(colorsPM[kBottomLeft_Cor
ner])), |
274 SkScalar(SkColorGetG(colorsPM[kBottomRight_Co
rner])))); | 274 SkScalar(SkColorGetG(colorsPM[kBottomRight_Co
rner])))); |
275 uint8_t b = uint8_t(bilerp(u, v, | 275 uint8_t b = uint8_t(bilerp(u, v, |
276 SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner
])), | 276 SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner
])), |
277 SkScalar(SkColorGetB(colorsPM[kTopRight_Corne
r])), | 277 SkScalar(SkColorGetB(colorsPM[kTopRight_Corne
r])), |
278 SkScalar(SkColorGetB(colorsPM[kBottomLeft_Cor
ner])), | 278 SkScalar(SkColorGetB(colorsPM[kBottomLeft_Cor
ner])), |
279 SkScalar(SkColorGetB(colorsPM[kBottomRight_Co
rner])))); | 279 SkScalar(SkColorGetB(colorsPM[kBottomRight_Co
rner])))); |
280 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); | 280 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b); |
281 } | 281 } |
282 | 282 |
283 if (texCoords) { | 283 if (texCoords) { |
284 data->fTexCoords[dataIndex] = SkPoint::Make( | 284 data->fTexCoords[dataIndex] = SkPoint::Make( |
285 bilerp(u, v, texCoords[kTopLeft_Corn
er].x(), | 285 bilerp(u, v, texCoords[kTopLeft_Corn
er].x(), |
286 texCoords[kTopRight_Corner].x
(), | 286 texCoords[kTopRight_Corner].x
(), |
287 texCoords[kBottomLeft_Corner]
.x(), | 287 texCoords[kBottomLeft_Corner]
.x(), |
288 texCoords[kBottomRight_Corner
].x()), | 288 texCoords[kBottomRight_Corner
].x()), |
289 bilerp(u, v, texCoords[kTopLeft_Corn
er].y(), | 289 bilerp(u, v, texCoords[kTopLeft_Corn
er].y(), |
290 texCoords[kTopRight_Corner].y
(), | 290 texCoords[kTopRight_Corner].y
(), |
291 texCoords[kBottomLeft_Corner]
.y(), | 291 texCoords[kBottomLeft_Corner]
.y(), |
292 texCoords[kBottomRight_Corner
].y())); | 292 texCoords[kBottomRight_Corner
].y())); |
293 | 293 |
294 } | 294 } |
295 | 295 |
296 if(x < lodX && y < lodY) { | 296 if(x < lodX && y < lodY) { |
297 int i = 6 * (x * lodY + y); | 297 int i = 6 * (x * lodY + y); |
298 data->fIndices[i] = x * stride + y; | 298 data->fIndices[i] = x * stride + y; |
299 data->fIndices[i + 1] = x * stride + 1 + y; | 299 data->fIndices[i + 1] = x * stride + 1 + y; |
300 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; | 300 data->fIndices[i + 2] = (x + 1) * stride + 1 + y; |
301 data->fIndices[i + 3] = data->fIndices[i]; | 301 data->fIndices[i + 3] = data->fIndices[i]; |
302 data->fIndices[i + 4] = data->fIndices[i + 2]; | 302 data->fIndices[i + 4] = data->fIndices[i + 2]; |
303 data->fIndices[i + 5] = (x + 1) * stride + y; | 303 data->fIndices[i + 5] = (x + 1) * stride + y; |
304 } | 304 } |
305 v = SkScalarClampMax(v + 1.f / lodY, 1); | 305 v = SkScalarClampMax(v + 1.f / lodY, 1); |
306 } | 306 } |
307 u = SkScalarClampMax(u + 1.f / lodX, 1); | 307 u = SkScalarClampMax(u + 1.f / lodX, 1); |
308 } | 308 } |
309 return true; | 309 return true; |
310 | 310 |
311 } | 311 } |
OLD | NEW |