blob: 7567304835f2d7a726de79ffaeedcd53b8f0d131 [file] [log] [blame]
dandovecfff212014-08-04 10:02:00 -07001/*
2 * Copyright 2014 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 "SkPatchUtils.h"
9
dandovb3c9d1c2014-08-12 08:34:29 -070010#include "SkColorPriv.h"
11#include "SkGeometry.h"
12
13/**
14 * Evaluator to sample the values of a cubic bezier using forward differences.
15 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
16 * adding precalculated values.
17 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
18 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
19 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
20 * obtaining this value (mh) we could just add this constant step to our first sampled point
21 * to compute the next one.
22 *
23 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
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 size 4, the 0th
26 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
27 * difference values.
28 */
29
30class FwDCubicEvaluator {
halcanary9d524f22016-03-29 09:03:52 -070031
dandovb3c9d1c2014-08-12 08:34:29 -070032public:
halcanary9d524f22016-03-29 09:03:52 -070033
dandovb3c9d1c2014-08-12 08:34:29 -070034 /**
35 * Receives the 4 control points of the cubic bezier.
36 */
halcanary9d524f22016-03-29 09:03:52 -070037
caryclark5ba2b962016-01-26 17:02:30 -080038 explicit FwDCubicEvaluator(const SkPoint points[4])
39 : fCoefs(points) {
dandovb3c9d1c2014-08-12 08:34:29 -070040 memcpy(fPoints, points, 4 * sizeof(SkPoint));
halcanary9d524f22016-03-29 09:03:52 -070041
dandovb3c9d1c2014-08-12 08:34:29 -070042 this->restart(1);
43 }
halcanary9d524f22016-03-29 09:03:52 -070044
dandovb3c9d1c2014-08-12 08:34:29 -070045 /**
46 * Restarts the forward differences evaluator to the first value of t = 0.
47 */
48 void restart(int divisions) {
49 fDivisions = divisions;
dandovb3c9d1c2014-08-12 08:34:29 -070050 fCurrent = 0;
51 fMax = fDivisions + 1;
caryclark5ba2b962016-01-26 17:02:30 -080052 Sk2s h = Sk2s(1.f / fDivisions);
53 Sk2s h2 = h * h;
54 Sk2s h3 = h2 * h;
55 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3;
56 fFwDiff[3] = to_point(fwDiff3);
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);
59 fFwDiff[0] = to_point(fCoefs.fD);
dandovb3c9d1c2014-08-12 08:34:29 -070060 }
halcanary9d524f22016-03-29 09:03:52 -070061
dandovb3c9d1c2014-08-12 08:34:29 -070062 /**
63 * Check if the evaluator is still within the range of 0<=t<=1
64 */
65 bool done() const {
66 return fCurrent > fMax;
67 }
halcanary9d524f22016-03-29 09:03:52 -070068
dandovb3c9d1c2014-08-12 08:34:29 -070069 /**
70 * Call next to obtain the SkPoint sampled and move to the next one.
71 */
72 SkPoint next() {
73 SkPoint point = fFwDiff[0];
74 fFwDiff[0] += fFwDiff[1];
75 fFwDiff[1] += fFwDiff[2];
76 fFwDiff[2] += fFwDiff[3];
77 fCurrent++;
78 return point;
79 }
halcanary9d524f22016-03-29 09:03:52 -070080
dandovb3c9d1c2014-08-12 08:34:29 -070081 const SkPoint* getCtrlPoints() const {
82 return fPoints;
83 }
halcanary9d524f22016-03-29 09:03:52 -070084
dandovb3c9d1c2014-08-12 08:34:29 -070085private:
caryclark5ba2b962016-01-26 17:02:30 -080086 SkCubicCoeff fCoefs;
dandovb3c9d1c2014-08-12 08:34:29 -070087 int fMax, fCurrent, fDivisions;
caryclark5ba2b962016-01-26 17:02:30 -080088 SkPoint fFwDiff[4], fPoints[4];
dandovb3c9d1c2014-08-12 08:34:29 -070089};
90
91////////////////////////////////////////////////////////////////////////////////
92
dandovecfff212014-08-04 10:02:00 -070093// size in pixels of each partition per axis, adjust this knob
dandovb3c9d1c2014-08-12 08:34:29 -070094static const int kPartitionSize = 10;
dandovecfff212014-08-04 10:02:00 -070095
96/**
97 * Calculate the approximate arc length given a bezier curve's control points.
98 */
99static SkScalar approx_arc_length(SkPoint* points, int count) {
100 if (count < 2) {
101 return 0;
102 }
103 SkScalar arcLength = 0;
104 for (int i = 0; i < count - 1; i++) {
105 arcLength += SkPoint::Distance(points[i], points[i + 1]);
106 }
107 return arcLength;
108}
109
dandovb3c9d1c2014-08-12 08:34:29 -0700110static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
111 SkScalar c11) {
112 SkScalar a = c00 * (1.f - tx) + c10 * tx;
113 SkScalar b = c01 * (1.f - tx) + c11 * tx;
114 return a * (1.f - ty) + b * ty;
115}
116
117SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
halcanary9d524f22016-03-29 09:03:52 -0700118
dandovecfff212014-08-04 10:02:00 -0700119 // Approximate length of each cubic.
dandovb3c9d1c2014-08-12 08:34:29 -0700120 SkPoint pts[kNumPtsCubic];
121 SkPatchUtils::getTopCubic(cubics, pts);
122 matrix->mapPoints(pts, kNumPtsCubic);
123 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700124
dandovb3c9d1c2014-08-12 08:34:29 -0700125 SkPatchUtils::getBottomCubic(cubics, pts);
126 matrix->mapPoints(pts, kNumPtsCubic);
127 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700128
dandovb3c9d1c2014-08-12 08:34:29 -0700129 SkPatchUtils::getLeftCubic(cubics, pts);
130 matrix->mapPoints(pts, kNumPtsCubic);
131 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700132
dandovb3c9d1c2014-08-12 08:34:29 -0700133 SkPatchUtils::getRightCubic(cubics, pts);
134 matrix->mapPoints(pts, kNumPtsCubic);
135 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700136
dandovecfff212014-08-04 10:02:00 -0700137 // 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) / kPartitionSize);
139 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitionSize);
halcanary9d524f22016-03-29 09:03:52 -0700140
dandovb3c9d1c2014-08-12 08:34:29 -0700141 return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY));
142}
143
144void SkPatchUtils::getTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
145 points[0] = cubics[kTopP0_CubicCtrlPts];
146 points[1] = cubics[kTopP1_CubicCtrlPts];
147 points[2] = cubics[kTopP2_CubicCtrlPts];
148 points[3] = cubics[kTopP3_CubicCtrlPts];
149}
150
151void SkPatchUtils::getBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
152 points[0] = cubics[kBottomP0_CubicCtrlPts];
153 points[1] = cubics[kBottomP1_CubicCtrlPts];
154 points[2] = cubics[kBottomP2_CubicCtrlPts];
155 points[3] = cubics[kBottomP3_CubicCtrlPts];
156}
157
158void SkPatchUtils::getLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
159 points[0] = cubics[kLeftP0_CubicCtrlPts];
160 points[1] = cubics[kLeftP1_CubicCtrlPts];
161 points[2] = cubics[kLeftP2_CubicCtrlPts];
162 points[3] = cubics[kLeftP3_CubicCtrlPts];
163}
164
165void SkPatchUtils::getRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
166 points[0] = cubics[kRightP0_CubicCtrlPts];
167 points[1] = cubics[kRightP1_CubicCtrlPts];
168 points[2] = cubics[kRightP2_CubicCtrlPts];
169 points[3] = cubics[kRightP3_CubicCtrlPts];
170}
171
172bool SkPatchUtils::getVertexData(SkPatchUtils::VertexData* data, const SkPoint cubics[12],
173 const SkColor colors[4], const SkPoint texCoords[4], int lodX, int lodY) {
halcanary96fcdcc2015-08-27 07:41:13 -0700174 if (lodX < 1 || lodY < 1 || nullptr == cubics || nullptr == data) {
dandovb3c9d1c2014-08-12 08:34:29 -0700175 return false;
176 }
dandov45f78422014-08-15 06:06:47 -0700177
178 // check for overflow in multiplication
179 const int64_t lodX64 = (lodX + 1),
180 lodY64 = (lodY + 1),
181 mult64 = lodX64 * lodY64;
182 if (mult64 > SK_MaxS32) {
183 return false;
184 }
185 data->fVertexCount = SkToS32(mult64);
186
187 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
188 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
189 if (data->fVertexCount > 10000 || lodX > 200 || lodY > 200) {
190 SkScalar weightX = static_cast<SkScalar>(lodX) / (lodX + lodY);
191 SkScalar weightY = static_cast<SkScalar>(lodY) / (lodX + lodY);
192
193 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
194 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
195 lodX = static_cast<int>(weightX * 200);
196 lodY = static_cast<int>(weightY * 200);
197 data->fVertexCount = (lodX + 1) * (lodY + 1);
198 }
dandovb3c9d1c2014-08-12 08:34:29 -0700199 data->fIndexCount = lodX * lodY * 6;
halcanary385fe4d2015-08-26 13:07:48 -0700200
201 data->fPoints = new SkPoint[data->fVertexCount];
202 data->fIndices = new uint16_t[data->fIndexCount];
203
dandovb3c9d1c2014-08-12 08:34:29 -0700204 // if colors is not null then create array for colors
205 SkPMColor colorsPM[kNumCorners];
bsalomon49f085d2014-09-05 13:34:00 -0700206 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -0700207 // premultiply colors to avoid color bleeding.
208 for (int i = 0; i < kNumCorners; i++) {
209 colorsPM[i] = SkPreMultiplyColor(colors[i]);
210 }
halcanary385fe4d2015-08-26 13:07:48 -0700211 data->fColors = new uint32_t[data->fVertexCount];
dandovb3c9d1c2014-08-12 08:34:29 -0700212 }
halcanary9d524f22016-03-29 09:03:52 -0700213
dandovb3c9d1c2014-08-12 08:34:29 -0700214 // if texture coordinates are not null then create array for them
bsalomon49f085d2014-09-05 13:34:00 -0700215 if (texCoords) {
halcanary385fe4d2015-08-26 13:07:48 -0700216 data->fTexCoords = new SkPoint[data->fVertexCount];
dandovb3c9d1c2014-08-12 08:34:29 -0700217 }
halcanary9d524f22016-03-29 09:03:52 -0700218
dandovb3c9d1c2014-08-12 08:34:29 -0700219 SkPoint pts[kNumPtsCubic];
220 SkPatchUtils::getBottomCubic(cubics, pts);
221 FwDCubicEvaluator fBottom(pts);
222 SkPatchUtils::getTopCubic(cubics, pts);
223 FwDCubicEvaluator fTop(pts);
224 SkPatchUtils::getLeftCubic(cubics, pts);
225 FwDCubicEvaluator fLeft(pts);
226 SkPatchUtils::getRightCubic(cubics, pts);
227 FwDCubicEvaluator fRight(pts);
halcanary9d524f22016-03-29 09:03:52 -0700228
dandovb3c9d1c2014-08-12 08:34:29 -0700229 fBottom.restart(lodX);
230 fTop.restart(lodX);
halcanary9d524f22016-03-29 09:03:52 -0700231
dandovb3c9d1c2014-08-12 08:34:29 -0700232 SkScalar u = 0.0f;
233 int stride = lodY + 1;
234 for (int x = 0; x <= lodX; x++) {
235 SkPoint bottom = fBottom.next(), top = fTop.next();
236 fLeft.restart(lodY);
237 fRight.restart(lodY);
238 SkScalar v = 0.f;
239 for (int y = 0; y <= lodY; y++) {
240 int dataIndex = x * (lodY + 1) + y;
halcanary9d524f22016-03-29 09:03:52 -0700241
dandovb3c9d1c2014-08-12 08:34:29 -0700242 SkPoint left = fLeft.next(), right = fRight.next();
halcanary9d524f22016-03-29 09:03:52 -0700243
dandovb3c9d1c2014-08-12 08:34:29 -0700244 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
245 (1.0f - v) * top.y() + v * bottom.y());
246 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
247 (1.0f - u) * left.y() + u * right.y());
248 SkPoint s2 = SkPoint::Make(
249 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
250 + u * fTop.getCtrlPoints()[3].x())
251 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
252 + u * fBottom.getCtrlPoints()[3].x()),
253 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
254 + u * fTop.getCtrlPoints()[3].y())
255 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
256 + u * fBottom.getCtrlPoints()[3].y()));
257 data->fPoints[dataIndex] = s0 + s1 - s2;
halcanary9d524f22016-03-29 09:03:52 -0700258
bsalomon49f085d2014-09-05 13:34:00 -0700259 if (colors) {
dandovb3c9d1c2014-08-12 08:34:29 -0700260 uint8_t a = uint8_t(bilerp(u, v,
261 SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner])),
262 SkScalar(SkColorGetA(colorsPM[kTopRight_Corner])),
263 SkScalar(SkColorGetA(colorsPM[kBottomLeft_Corner])),
264 SkScalar(SkColorGetA(colorsPM[kBottomRight_Corner]))));
265 uint8_t r = uint8_t(bilerp(u, v,
266 SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner])),
267 SkScalar(SkColorGetR(colorsPM[kTopRight_Corner])),
268 SkScalar(SkColorGetR(colorsPM[kBottomLeft_Corner])),
269 SkScalar(SkColorGetR(colorsPM[kBottomRight_Corner]))));
270 uint8_t g = uint8_t(bilerp(u, v,
271 SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner])),
272 SkScalar(SkColorGetG(colorsPM[kTopRight_Corner])),
273 SkScalar(SkColorGetG(colorsPM[kBottomLeft_Corner])),
274 SkScalar(SkColorGetG(colorsPM[kBottomRight_Corner]))));
275 uint8_t b = uint8_t(bilerp(u, v,
276 SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner])),
277 SkScalar(SkColorGetB(colorsPM[kTopRight_Corner])),
278 SkScalar(SkColorGetB(colorsPM[kBottomLeft_Corner])),
279 SkScalar(SkColorGetB(colorsPM[kBottomRight_Corner]))));
280 data->fColors[dataIndex] = SkPackARGB32(a,r,g,b);
281 }
halcanary9d524f22016-03-29 09:03:52 -0700282
bsalomon49f085d2014-09-05 13:34:00 -0700283 if (texCoords) {
dandovb3c9d1c2014-08-12 08:34:29 -0700284 data->fTexCoords[dataIndex] = SkPoint::Make(
285 bilerp(u, v, texCoords[kTopLeft_Corner].x(),
286 texCoords[kTopRight_Corner].x(),
287 texCoords[kBottomLeft_Corner].x(),
288 texCoords[kBottomRight_Corner].x()),
289 bilerp(u, v, texCoords[kTopLeft_Corner].y(),
290 texCoords[kTopRight_Corner].y(),
291 texCoords[kBottomLeft_Corner].y(),
292 texCoords[kBottomRight_Corner].y()));
halcanary9d524f22016-03-29 09:03:52 -0700293
dandovb3c9d1c2014-08-12 08:34:29 -0700294 }
halcanary9d524f22016-03-29 09:03:52 -0700295
dandovb3c9d1c2014-08-12 08:34:29 -0700296 if(x < lodX && y < lodY) {
297 int i = 6 * (x * lodY + y);
298 data->fIndices[i] = x * stride + y;
299 data->fIndices[i + 1] = x * stride + 1 + y;
300 data->fIndices[i + 2] = (x + 1) * stride + 1 + y;
301 data->fIndices[i + 3] = data->fIndices[i];
302 data->fIndices[i + 4] = data->fIndices[i + 2];
303 data->fIndices[i + 5] = (x + 1) * stride + y;
304 }
305 v = SkScalarClampMax(v + 1.f / lodY, 1);
306 }
307 u = SkScalarClampMax(u + 1.f / lodX, 1);
308 }
309 return true;
310
dandovecfff212014-08-04 10:02:00 -0700311}
Mike Reed795c5ea2017-03-17 14:29:05 -0400312
313///////////////////////////////////////////////////////////////////////////////////////////////////
314
315sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
316 const SkPoint srcTexCoords[4], int lodX, int lodY) {
317 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
318 return nullptr;
319 }
320
321 // check for overflow in multiplication
322 const int64_t lodX64 = (lodX + 1),
323 lodY64 = (lodY + 1),
324 mult64 = lodX64 * lodY64;
325 if (mult64 > SK_MaxS32) {
326 return nullptr;
327 }
328
329 int vertexCount = SkToS32(mult64);
330 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
331 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
332 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
333 float weightX = static_cast<float>(lodX) / (lodX + lodY);
334 float weightY = static_cast<float>(lodY) / (lodX + lodY);
335
336 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
337 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
338 lodX = static_cast<int>(weightX * 200);
339 lodY = static_cast<int>(weightY * 200);
340 vertexCount = (lodX + 1) * (lodY + 1);
341 }
342 const int indexCount = lodX * lodY * 6;
343 uint32_t flags = 0;
344 if (srcTexCoords) {
345 flags |= SkVertices::kHasTexCoords_BuilderFlag;
346 }
347 if (srcColors) {
348 flags |= SkVertices::kHasColors_BuilderFlag;
349 }
350
Mike Reed887cdf12017-04-03 11:11:09 -0400351 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
Mike Reed795c5ea2017-03-17 14:29:05 -0400352 SkPoint* pos = builder.positions();
353 SkPoint* texs = builder.texCoords();
354 SkColor* colors = builder.colors();
355 uint16_t* indices = builder.indices();
356
357 // if colors is not null then create array for colors
358 SkPMColor colorsPM[kNumCorners];
359 if (srcColors) {
360 // premultiply colors to avoid color bleeding.
361 for (int i = 0; i < kNumCorners; i++) {
362 colorsPM[i] = SkPreMultiplyColor(srcColors[i]);
363 }
364 srcColors = colorsPM;
365 }
366
367 SkPoint pts[kNumPtsCubic];
368 SkPatchUtils::getBottomCubic(cubics, pts);
369 FwDCubicEvaluator fBottom(pts);
370 SkPatchUtils::getTopCubic(cubics, pts);
371 FwDCubicEvaluator fTop(pts);
372 SkPatchUtils::getLeftCubic(cubics, pts);
373 FwDCubicEvaluator fLeft(pts);
374 SkPatchUtils::getRightCubic(cubics, pts);
375 FwDCubicEvaluator fRight(pts);
376
377 fBottom.restart(lodX);
378 fTop.restart(lodX);
379
380 SkScalar u = 0.0f;
381 int stride = lodY + 1;
382 for (int x = 0; x <= lodX; x++) {
383 SkPoint bottom = fBottom.next(), top = fTop.next();
384 fLeft.restart(lodY);
385 fRight.restart(lodY);
386 SkScalar v = 0.f;
387 for (int y = 0; y <= lodY; y++) {
388 int dataIndex = x * (lodY + 1) + y;
389
390 SkPoint left = fLeft.next(), right = fRight.next();
391
392 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
393 (1.0f - v) * top.y() + v * bottom.y());
394 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
395 (1.0f - u) * left.y() + u * right.y());
396 SkPoint s2 = SkPoint::Make(
397 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
398 + u * fTop.getCtrlPoints()[3].x())
399 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
400 + u * fBottom.getCtrlPoints()[3].x()),
401 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
402 + u * fTop.getCtrlPoints()[3].y())
403 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
404 + u * fBottom.getCtrlPoints()[3].y()));
405 pos[dataIndex] = s0 + s1 - s2;
406
407 if (colors) {
408 uint8_t a = uint8_t(bilerp(u, v,
409 SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner])),
410 SkScalar(SkColorGetA(colorsPM[kTopRight_Corner])),
411 SkScalar(SkColorGetA(colorsPM[kBottomLeft_Corner])),
412 SkScalar(SkColorGetA(colorsPM[kBottomRight_Corner]))));
413 uint8_t r = uint8_t(bilerp(u, v,
414 SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner])),
415 SkScalar(SkColorGetR(colorsPM[kTopRight_Corner])),
416 SkScalar(SkColorGetR(colorsPM[kBottomLeft_Corner])),
417 SkScalar(SkColorGetR(colorsPM[kBottomRight_Corner]))));
418 uint8_t g = uint8_t(bilerp(u, v,
419 SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner])),
420 SkScalar(SkColorGetG(colorsPM[kTopRight_Corner])),
421 SkScalar(SkColorGetG(colorsPM[kBottomLeft_Corner])),
422 SkScalar(SkColorGetG(colorsPM[kBottomRight_Corner]))));
423 uint8_t b = uint8_t(bilerp(u, v,
424 SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner])),
425 SkScalar(SkColorGetB(colorsPM[kTopRight_Corner])),
426 SkScalar(SkColorGetB(colorsPM[kBottomLeft_Corner])),
427 SkScalar(SkColorGetB(colorsPM[kBottomRight_Corner]))));
428 colors[dataIndex] = SkPackARGB32(a,r,g,b);
429 }
430
431 if (texs) {
432 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
433 srcTexCoords[kTopRight_Corner].x(),
434 srcTexCoords[kBottomLeft_Corner].x(),
435 srcTexCoords[kBottomRight_Corner].x()),
436 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
437 srcTexCoords[kTopRight_Corner].y(),
438 srcTexCoords[kBottomLeft_Corner].y(),
439 srcTexCoords[kBottomRight_Corner].y()));
440
441 }
442
443 if(x < lodX && y < lodY) {
444 int i = 6 * (x * lodY + y);
445 indices[i] = x * stride + y;
446 indices[i + 1] = x * stride + 1 + y;
447 indices[i + 2] = (x + 1) * stride + 1 + y;
448 indices[i + 3] = indices[i];
449 indices[i + 4] = indices[i + 2];
450 indices[i + 5] = (x + 1) * stride + y;
451 }
452 v = SkScalarClampMax(v + 1.f / lodY, 1);
453 }
454 u = SkScalarClampMax(u + 1.f / lodX, 1);
455 }
456 return builder.detach();
457}