blob: af91626b5105ea525d69cae2dc0b7d3fa9011a9b [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
Cary Clarka4083c92017-09-15 11:59:23 -040010#include "SkColorData.h"
dandovb3c9d1c2014-08-12 08:34:29 -070011#include "SkGeometry.h"
Mike Reed435071e2017-05-23 11:22:56 -040012#include "SkPM4f.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "SkTo.h"
dandovb3c9d1c2014-08-12 08:34:29 -070014
Mike Reed4ebb43e2017-04-05 11:06:15 -040015namespace {
16 enum CubicCtrlPts {
17 kTopP0_CubicCtrlPts = 0,
18 kTopP1_CubicCtrlPts = 1,
19 kTopP2_CubicCtrlPts = 2,
20 kTopP3_CubicCtrlPts = 3,
21
22 kRightP0_CubicCtrlPts = 3,
23 kRightP1_CubicCtrlPts = 4,
24 kRightP2_CubicCtrlPts = 5,
25 kRightP3_CubicCtrlPts = 6,
26
27 kBottomP0_CubicCtrlPts = 9,
28 kBottomP1_CubicCtrlPts = 8,
29 kBottomP2_CubicCtrlPts = 7,
30 kBottomP3_CubicCtrlPts = 6,
31
32 kLeftP0_CubicCtrlPts = 0,
33 kLeftP1_CubicCtrlPts = 11,
34 kLeftP2_CubicCtrlPts = 10,
35 kLeftP3_CubicCtrlPts = 9,
36 };
37
38 // Enum for corner also clockwise.
39 enum Corner {
40 kTopLeft_Corner = 0,
41 kTopRight_Corner,
42 kBottomRight_Corner,
43 kBottomLeft_Corner
44 };
45}
46
dandovb3c9d1c2014-08-12 08:34:29 -070047/**
48 * Evaluator to sample the values of a cubic bezier using forward differences.
49 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
50 * adding precalculated values.
51 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
52 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
53 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
54 * obtaining this value (mh) we could just add this constant step to our first sampled point
55 * to compute the next one.
56 *
57 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
58 * apply again forward differences and get linear function to which we can apply again forward
59 * differences to get a constant difference. This is why we keep an array of size 4, the 0th
60 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
61 * difference values.
62 */
63
64class FwDCubicEvaluator {
halcanary9d524f22016-03-29 09:03:52 -070065
dandovb3c9d1c2014-08-12 08:34:29 -070066public:
halcanary9d524f22016-03-29 09:03:52 -070067
dandovb3c9d1c2014-08-12 08:34:29 -070068 /**
69 * Receives the 4 control points of the cubic bezier.
70 */
halcanary9d524f22016-03-29 09:03:52 -070071
caryclark5ba2b962016-01-26 17:02:30 -080072 explicit FwDCubicEvaluator(const SkPoint points[4])
73 : fCoefs(points) {
dandovb3c9d1c2014-08-12 08:34:29 -070074 memcpy(fPoints, points, 4 * sizeof(SkPoint));
halcanary9d524f22016-03-29 09:03:52 -070075
dandovb3c9d1c2014-08-12 08:34:29 -070076 this->restart(1);
77 }
halcanary9d524f22016-03-29 09:03:52 -070078
dandovb3c9d1c2014-08-12 08:34:29 -070079 /**
80 * Restarts the forward differences evaluator to the first value of t = 0.
81 */
82 void restart(int divisions) {
83 fDivisions = divisions;
dandovb3c9d1c2014-08-12 08:34:29 -070084 fCurrent = 0;
85 fMax = fDivisions + 1;
caryclark5ba2b962016-01-26 17:02:30 -080086 Sk2s h = Sk2s(1.f / fDivisions);
87 Sk2s h2 = h * h;
88 Sk2s h3 = h2 * h;
89 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3;
90 fFwDiff[3] = to_point(fwDiff3);
91 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
92 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
93 fFwDiff[0] = to_point(fCoefs.fD);
dandovb3c9d1c2014-08-12 08:34:29 -070094 }
halcanary9d524f22016-03-29 09:03:52 -070095
dandovb3c9d1c2014-08-12 08:34:29 -070096 /**
97 * Check if the evaluator is still within the range of 0<=t<=1
98 */
99 bool done() const {
100 return fCurrent > fMax;
101 }
halcanary9d524f22016-03-29 09:03:52 -0700102
dandovb3c9d1c2014-08-12 08:34:29 -0700103 /**
104 * Call next to obtain the SkPoint sampled and move to the next one.
105 */
106 SkPoint next() {
107 SkPoint point = fFwDiff[0];
108 fFwDiff[0] += fFwDiff[1];
109 fFwDiff[1] += fFwDiff[2];
110 fFwDiff[2] += fFwDiff[3];
111 fCurrent++;
112 return point;
113 }
halcanary9d524f22016-03-29 09:03:52 -0700114
dandovb3c9d1c2014-08-12 08:34:29 -0700115 const SkPoint* getCtrlPoints() const {
116 return fPoints;
117 }
halcanary9d524f22016-03-29 09:03:52 -0700118
dandovb3c9d1c2014-08-12 08:34:29 -0700119private:
caryclark5ba2b962016-01-26 17:02:30 -0800120 SkCubicCoeff fCoefs;
dandovb3c9d1c2014-08-12 08:34:29 -0700121 int fMax, fCurrent, fDivisions;
caryclark5ba2b962016-01-26 17:02:30 -0800122 SkPoint fFwDiff[4], fPoints[4];
dandovb3c9d1c2014-08-12 08:34:29 -0700123};
124
125////////////////////////////////////////////////////////////////////////////////
126
dandovecfff212014-08-04 10:02:00 -0700127// size in pixels of each partition per axis, adjust this knob
dandovb3c9d1c2014-08-12 08:34:29 -0700128static const int kPartitionSize = 10;
dandovecfff212014-08-04 10:02:00 -0700129
130/**
Mike Reedf4f06ad2018-05-02 15:11:42 -0400131 * Calculate the approximate arc length given a bezier curve's control points.
132 * Returns -1 if bad calc (i.e. non-finite)
dandovecfff212014-08-04 10:02:00 -0700133 */
Mike Reedf4f06ad2018-05-02 15:11:42 -0400134static SkScalar approx_arc_length(const SkPoint points[], int count) {
dandovecfff212014-08-04 10:02:00 -0700135 if (count < 2) {
136 return 0;
137 }
138 SkScalar arcLength = 0;
139 for (int i = 0; i < count - 1; i++) {
140 arcLength += SkPoint::Distance(points[i], points[i + 1]);
141 }
Mike Reedf4f06ad2018-05-02 15:11:42 -0400142 return SkScalarIsFinite(arcLength) ? arcLength : -1;
dandovecfff212014-08-04 10:02:00 -0700143}
144
dandovb3c9d1c2014-08-12 08:34:29 -0700145static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
Mike Reed435071e2017-05-23 11:22:56 -0400146 SkScalar c11) {
dandovb3c9d1c2014-08-12 08:34:29 -0700147 SkScalar a = c00 * (1.f - tx) + c10 * tx;
148 SkScalar b = c01 * (1.f - tx) + c11 * tx;
149 return a * (1.f - ty) + b * ty;
150}
151
Mike Reed435071e2017-05-23 11:22:56 -0400152static Sk4f bilerp(SkScalar tx, SkScalar ty,
153 const Sk4f& c00, const Sk4f& c10, const Sk4f& c01, const Sk4f& c11) {
154 Sk4f a = c00 * (1.f - tx) + c10 * tx;
155 Sk4f b = c01 * (1.f - tx) + c11 * tx;
156 return a * (1.f - ty) + b * ty;
157}
158
dandovb3c9d1c2014-08-12 08:34:29 -0700159SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
dandovecfff212014-08-04 10:02:00 -0700160 // Approximate length of each cubic.
dandovb3c9d1c2014-08-12 08:34:29 -0700161 SkPoint pts[kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -0400162 SkPatchUtils::GetTopCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700163 matrix->mapPoints(pts, kNumPtsCubic);
164 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700165
Mike Reed4ebb43e2017-04-05 11:06:15 -0400166 SkPatchUtils::GetBottomCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700167 matrix->mapPoints(pts, kNumPtsCubic);
168 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700169
Mike Reed4ebb43e2017-04-05 11:06:15 -0400170 SkPatchUtils::GetLeftCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700171 matrix->mapPoints(pts, kNumPtsCubic);
172 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700173
Mike Reed4ebb43e2017-04-05 11:06:15 -0400174 SkPatchUtils::GetRightCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700175 matrix->mapPoints(pts, kNumPtsCubic);
176 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700177
Mike Reedf4f06ad2018-05-02 15:11:42 -0400178 if (topLength < 0 || bottomLength < 0 || leftLength < 0 || rightLength < 0) {
179 return {0, 0}; // negative length is a sentinel for bad length (i.e. non-finite)
180 }
181
dandovecfff212014-08-04 10:02:00 -0700182 // Level of detail per axis, based on the larger side between top and bottom or left and right
183 int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitionSize);
184 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitionSize);
halcanary9d524f22016-03-29 09:03:52 -0700185
dandovb3c9d1c2014-08-12 08:34:29 -0700186 return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY));
187}
188
Mike Reed4ebb43e2017-04-05 11:06:15 -0400189void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700190 points[0] = cubics[kTopP0_CubicCtrlPts];
191 points[1] = cubics[kTopP1_CubicCtrlPts];
192 points[2] = cubics[kTopP2_CubicCtrlPts];
193 points[3] = cubics[kTopP3_CubicCtrlPts];
194}
195
Mike Reed4ebb43e2017-04-05 11:06:15 -0400196void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700197 points[0] = cubics[kBottomP0_CubicCtrlPts];
198 points[1] = cubics[kBottomP1_CubicCtrlPts];
199 points[2] = cubics[kBottomP2_CubicCtrlPts];
200 points[3] = cubics[kBottomP3_CubicCtrlPts];
201}
202
Mike Reed4ebb43e2017-04-05 11:06:15 -0400203void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700204 points[0] = cubics[kLeftP0_CubicCtrlPts];
205 points[1] = cubics[kLeftP1_CubicCtrlPts];
206 points[2] = cubics[kLeftP2_CubicCtrlPts];
207 points[3] = cubics[kLeftP3_CubicCtrlPts];
208}
209
Mike Reed4ebb43e2017-04-05 11:06:15 -0400210void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700211 points[0] = cubics[kRightP0_CubicCtrlPts];
212 points[1] = cubics[kRightP1_CubicCtrlPts];
213 points[2] = cubics[kRightP2_CubicCtrlPts];
214 points[3] = cubics[kRightP3_CubicCtrlPts];
215}
216
Mike Reed435071e2017-05-23 11:22:56 -0400217#include "SkPM4fPriv.h"
Mike Reed435071e2017-05-23 11:22:56 -0400218#include "SkColorSpaceXform.h"
219
220struct SkRGBAf {
221 float fVec[4];
222
223 static SkRGBAf From4f(const Sk4f& x) {
224 SkRGBAf c;
225 x.store(c.fVec);
226 return c;
227 }
228
229 static SkRGBAf FromBGRA32(SkColor c) {
230 return From4f(swizzle_rb(SkNx_cast<float>(Sk4b::Load(&c)) * (1/255.0f)));
231 }
232
233 Sk4f to4f() const {
234 return Sk4f::Load(fVec);
235 }
236
237 SkColor toBGRA32() const {
238 SkColor color;
239 SkNx_cast<uint8_t>(swizzle_rb(this->to4f()) * Sk4f(255) + Sk4f(0.5f)).store(&color);
240 return color;
241 }
242
243 SkRGBAf premul() const {
244 float a = fVec[3];
245 return From4f(this->to4f() * Sk4f(a, a, a, 1));
246 }
247
248 SkRGBAf unpremul() const {
249 float a = fVec[3];
250 float inv = a ? 1/a : 0;
251 return From4f(this->to4f() * Sk4f(inv, inv, inv, 1));
252 }
253};
254
Brian Osmane0a99622018-07-09 16:12:27 -0400255static void skcolor_to_float(SkRGBAf dst[], const SkColor src[], int count, SkColorSpace* dstCS,
256 bool doPremul) {
257 // Source is always sRGB SkColor (safe because sRGB is a global singleton)
258 auto srcCS = SkColorSpace::MakeSRGB().get();
259
260 auto op = doPremul ? SkColorSpaceXform::kPremul_AlphaOp : SkColorSpaceXform::kPreserve_AlphaOp;
261 SkAssertResult(SkColorSpaceXform::Apply(dstCS, SkColorSpaceXform::kRGBA_F32_ColorFormat, dst,
262 srcCS, SkColorSpaceXform::kBGRA_8888_ColorFormat, src,
263 count, op));
Mike Reed435071e2017-05-23 11:22:56 -0400264}
265
Brian Osmane0a99622018-07-09 16:12:27 -0400266static void float_to_skcolor(SkColor dst[], const SkRGBAf src[], int count, SkColorSpace* srcCS) {
267 // Destination is always sRGB SkColor (safe because sRGB is a global singleton)
268 auto dstCS = SkColorSpace::MakeSRGB().get();
269 SkAssertResult(SkColorSpaceXform::Apply(dstCS, SkColorSpaceXform::kBGRA_8888_ColorFormat, dst,
270 srcCS, SkColorSpaceXform::kRGBA_F32_ColorFormat, src,
271 count, SkColorSpaceXform::kPreserve_AlphaOp));
Mike Reed435071e2017-05-23 11:22:56 -0400272}
273
274static void unpremul(SkRGBAf array[], int count) {
275 for (int i = 0; i < count; ++i) {
276 array[i] = array[i].unpremul();
277 }
278}
279
Mike Reed795c5ea2017-03-17 14:29:05 -0400280sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
Mike Reed435071e2017-05-23 11:22:56 -0400281 const SkPoint srcTexCoords[4], int lodX, int lodY,
Brian Osmane0a99622018-07-09 16:12:27 -0400282 SkColorSpace* colorSpace) {
Mike Reed795c5ea2017-03-17 14:29:05 -0400283 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
284 return nullptr;
285 }
286
287 // check for overflow in multiplication
288 const int64_t lodX64 = (lodX + 1),
289 lodY64 = (lodY + 1),
290 mult64 = lodX64 * lodY64;
291 if (mult64 > SK_MaxS32) {
292 return nullptr;
293 }
294
Brian Osmane0a99622018-07-09 16:12:27 -0400295 // Treat null interpolation space as sRGB (safe because sRGB is a global singleton)
296 if (!colorSpace) {
297 colorSpace = SkColorSpace::MakeSRGB().get();
298 }
299
Mike Reed795c5ea2017-03-17 14:29:05 -0400300 int vertexCount = SkToS32(mult64);
301 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
302 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
303 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
304 float weightX = static_cast<float>(lodX) / (lodX + lodY);
305 float weightY = static_cast<float>(lodY) / (lodX + lodY);
306
307 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
308 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
Mike Reed70bebb02018-05-09 14:07:21 -0400309 // Need a min of 1 since we later divide by lod
310 lodX = std::max(1, sk_float_floor2int_no_saturate(weightX * 200));
311 lodY = std::max(1, sk_float_floor2int_no_saturate(weightY * 200));
Mike Reed795c5ea2017-03-17 14:29:05 -0400312 vertexCount = (lodX + 1) * (lodY + 1);
313 }
314 const int indexCount = lodX * lodY * 6;
315 uint32_t flags = 0;
316 if (srcTexCoords) {
317 flags |= SkVertices::kHasTexCoords_BuilderFlag;
318 }
319 if (srcColors) {
320 flags |= SkVertices::kHasColors_BuilderFlag;
321 }
322
Florin Malita14a64302017-05-24 14:53:44 -0400323 SkSTArenaAlloc<2048> alloc;
Mike Reed435071e2017-05-23 11:22:56 -0400324 SkRGBAf* cornerColors = srcColors ? alloc.makeArray<SkRGBAf>(4) : nullptr;
325 SkRGBAf* tmpColors = srcColors ? alloc.makeArray<SkRGBAf>(vertexCount) : nullptr;
Mike Reed435071e2017-05-23 11:22:56 -0400326
Mike Reed887cdf12017-04-03 11:11:09 -0400327 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
Mike Reed795c5ea2017-03-17 14:29:05 -0400328 SkPoint* pos = builder.positions();
329 SkPoint* texs = builder.texCoords();
Mike Reed795c5ea2017-03-17 14:29:05 -0400330 uint16_t* indices = builder.indices();
Mike Reed7346a1f2017-05-18 22:23:34 -0400331 bool is_opaque = false;
Mike Reed795c5ea2017-03-17 14:29:05 -0400332
Mike Reed435071e2017-05-23 11:22:56 -0400333 /*
334 * 1. Should we offer this as a runtime choice, as we do in gradients?
335 * 2. Since drawing the vertices wants premul, shoudl we extend SkVertices to store
336 * premul colors (as floats, w/ a colorspace)?
337 */
338 bool doPremul = true;
339 if (cornerColors) {
Mike Reed7346a1f2017-05-18 22:23:34 -0400340 SkColor c = ~0;
Mike Reed795c5ea2017-03-17 14:29:05 -0400341 for (int i = 0; i < kNumCorners; i++) {
Mike Reed7346a1f2017-05-18 22:23:34 -0400342 c &= srcColors[i];
Mike Reed795c5ea2017-03-17 14:29:05 -0400343 }
Mike Reed7346a1f2017-05-18 22:23:34 -0400344 is_opaque = (SkColorGetA(c) == 0xFF);
Mike Reed435071e2017-05-23 11:22:56 -0400345 if (is_opaque) {
346 doPremul = false; // no need
347 }
348
Brian Osmane0a99622018-07-09 16:12:27 -0400349 skcolor_to_float(cornerColors, srcColors, kNumCorners, colorSpace, doPremul);
Mike Reed795c5ea2017-03-17 14:29:05 -0400350 }
351
352 SkPoint pts[kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -0400353 SkPatchUtils::GetBottomCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400354 FwDCubicEvaluator fBottom(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400355 SkPatchUtils::GetTopCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400356 FwDCubicEvaluator fTop(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400357 SkPatchUtils::GetLeftCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400358 FwDCubicEvaluator fLeft(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400359 SkPatchUtils::GetRightCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400360 FwDCubicEvaluator fRight(pts);
361
362 fBottom.restart(lodX);
363 fTop.restart(lodX);
364
365 SkScalar u = 0.0f;
366 int stride = lodY + 1;
367 for (int x = 0; x <= lodX; x++) {
368 SkPoint bottom = fBottom.next(), top = fTop.next();
369 fLeft.restart(lodY);
370 fRight.restart(lodY);
371 SkScalar v = 0.f;
372 for (int y = 0; y <= lodY; y++) {
373 int dataIndex = x * (lodY + 1) + y;
374
375 SkPoint left = fLeft.next(), right = fRight.next();
376
377 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
378 (1.0f - v) * top.y() + v * bottom.y());
379 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
380 (1.0f - u) * left.y() + u * right.y());
381 SkPoint s2 = SkPoint::Make(
382 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
383 + u * fTop.getCtrlPoints()[3].x())
384 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
385 + u * fBottom.getCtrlPoints()[3].x()),
386 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
387 + u * fTop.getCtrlPoints()[3].y())
388 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
389 + u * fBottom.getCtrlPoints()[3].y()));
390 pos[dataIndex] = s0 + s1 - s2;
391
Mike Reed435071e2017-05-23 11:22:56 -0400392 if (cornerColors) {
393 bilerp(u, v, cornerColors[kTopLeft_Corner].to4f(),
394 cornerColors[kTopRight_Corner].to4f(),
395 cornerColors[kBottomLeft_Corner].to4f(),
396 cornerColors[kBottomRight_Corner].to4f()).store(tmpColors[dataIndex].fVec);
397 if (is_opaque) {
398 tmpColors[dataIndex].fVec[3] = 1;
Mike Reed7346a1f2017-05-18 22:23:34 -0400399 }
Mike Reed795c5ea2017-03-17 14:29:05 -0400400 }
401
402 if (texs) {
403 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
404 srcTexCoords[kTopRight_Corner].x(),
405 srcTexCoords[kBottomLeft_Corner].x(),
406 srcTexCoords[kBottomRight_Corner].x()),
407 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
408 srcTexCoords[kTopRight_Corner].y(),
409 srcTexCoords[kBottomLeft_Corner].y(),
410 srcTexCoords[kBottomRight_Corner].y()));
411
412 }
413
414 if(x < lodX && y < lodY) {
415 int i = 6 * (x * lodY + y);
416 indices[i] = x * stride + y;
417 indices[i + 1] = x * stride + 1 + y;
418 indices[i + 2] = (x + 1) * stride + 1 + y;
419 indices[i + 3] = indices[i];
420 indices[i + 4] = indices[i + 2];
421 indices[i + 5] = (x + 1) * stride + y;
422 }
423 v = SkScalarClampMax(v + 1.f / lodY, 1);
424 }
425 u = SkScalarClampMax(u + 1.f / lodX, 1);
426 }
Mike Reed435071e2017-05-23 11:22:56 -0400427
428 if (tmpColors) {
429 if (doPremul) {
430 unpremul(tmpColors, vertexCount);
431 }
Brian Osmane0a99622018-07-09 16:12:27 -0400432 float_to_skcolor(builder.colors(), tmpColors, vertexCount, colorSpace);
Mike Reed435071e2017-05-23 11:22:56 -0400433 }
Mike Reed795c5ea2017-03-17 14:29:05 -0400434 return builder.detach();
435}