blob: 119f734a24315c9e48a3c64d146fafd3c5da06fd [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
Mike Reed4ebb43e2017-04-05 11:06:15 -040013namespace {
14 enum CubicCtrlPts {
15 kTopP0_CubicCtrlPts = 0,
16 kTopP1_CubicCtrlPts = 1,
17 kTopP2_CubicCtrlPts = 2,
18 kTopP3_CubicCtrlPts = 3,
19
20 kRightP0_CubicCtrlPts = 3,
21 kRightP1_CubicCtrlPts = 4,
22 kRightP2_CubicCtrlPts = 5,
23 kRightP3_CubicCtrlPts = 6,
24
25 kBottomP0_CubicCtrlPts = 9,
26 kBottomP1_CubicCtrlPts = 8,
27 kBottomP2_CubicCtrlPts = 7,
28 kBottomP3_CubicCtrlPts = 6,
29
30 kLeftP0_CubicCtrlPts = 0,
31 kLeftP1_CubicCtrlPts = 11,
32 kLeftP2_CubicCtrlPts = 10,
33 kLeftP3_CubicCtrlPts = 9,
34 };
35
36 // Enum for corner also clockwise.
37 enum Corner {
38 kTopLeft_Corner = 0,
39 kTopRight_Corner,
40 kBottomRight_Corner,
41 kBottomLeft_Corner
42 };
43}
44
dandovb3c9d1c2014-08-12 08:34:29 -070045/**
46 * Evaluator to sample the values of a cubic bezier using forward differences.
47 * Forward differences is a method for evaluating a nth degree polynomial at a uniform step by only
48 * adding precalculated values.
49 * For a linear example we have the function f(t) = m*t+b, then the value of that function at t+h
50 * would be f(t+h) = m*(t+h)+b. If we want to know the uniform step that we must add to the first
51 * evaluation f(t) then we need to substract f(t+h) - f(t) = m*t + m*h + b - m*t + b = mh. After
52 * obtaining this value (mh) we could just add this constant step to our first sampled point
53 * to compute the next one.
54 *
55 * For the cubic case the first difference gives as a result a quadratic polynomial to which we can
56 * apply again forward differences and get linear function to which we can apply again forward
57 * differences to get a constant difference. This is why we keep an array of size 4, the 0th
58 * position keeps the sampled value while the next ones keep the quadratic, linear and constant
59 * difference values.
60 */
61
62class FwDCubicEvaluator {
halcanary9d524f22016-03-29 09:03:52 -070063
dandovb3c9d1c2014-08-12 08:34:29 -070064public:
halcanary9d524f22016-03-29 09:03:52 -070065
dandovb3c9d1c2014-08-12 08:34:29 -070066 /**
67 * Receives the 4 control points of the cubic bezier.
68 */
halcanary9d524f22016-03-29 09:03:52 -070069
caryclark5ba2b962016-01-26 17:02:30 -080070 explicit FwDCubicEvaluator(const SkPoint points[4])
71 : fCoefs(points) {
dandovb3c9d1c2014-08-12 08:34:29 -070072 memcpy(fPoints, points, 4 * sizeof(SkPoint));
halcanary9d524f22016-03-29 09:03:52 -070073
dandovb3c9d1c2014-08-12 08:34:29 -070074 this->restart(1);
75 }
halcanary9d524f22016-03-29 09:03:52 -070076
dandovb3c9d1c2014-08-12 08:34:29 -070077 /**
78 * Restarts the forward differences evaluator to the first value of t = 0.
79 */
80 void restart(int divisions) {
81 fDivisions = divisions;
dandovb3c9d1c2014-08-12 08:34:29 -070082 fCurrent = 0;
83 fMax = fDivisions + 1;
caryclark5ba2b962016-01-26 17:02:30 -080084 Sk2s h = Sk2s(1.f / fDivisions);
85 Sk2s h2 = h * h;
86 Sk2s h3 = h2 * h;
87 Sk2s fwDiff3 = Sk2s(6) * fCoefs.fA * h3;
88 fFwDiff[3] = to_point(fwDiff3);
89 fFwDiff[2] = to_point(fwDiff3 + times_2(fCoefs.fB) * h2);
90 fFwDiff[1] = to_point(fCoefs.fA * h3 + fCoefs.fB * h2 + fCoefs.fC * h);
91 fFwDiff[0] = to_point(fCoefs.fD);
dandovb3c9d1c2014-08-12 08:34:29 -070092 }
halcanary9d524f22016-03-29 09:03:52 -070093
dandovb3c9d1c2014-08-12 08:34:29 -070094 /**
95 * Check if the evaluator is still within the range of 0<=t<=1
96 */
97 bool done() const {
98 return fCurrent > fMax;
99 }
halcanary9d524f22016-03-29 09:03:52 -0700100
dandovb3c9d1c2014-08-12 08:34:29 -0700101 /**
102 * Call next to obtain the SkPoint sampled and move to the next one.
103 */
104 SkPoint next() {
105 SkPoint point = fFwDiff[0];
106 fFwDiff[0] += fFwDiff[1];
107 fFwDiff[1] += fFwDiff[2];
108 fFwDiff[2] += fFwDiff[3];
109 fCurrent++;
110 return point;
111 }
halcanary9d524f22016-03-29 09:03:52 -0700112
dandovb3c9d1c2014-08-12 08:34:29 -0700113 const SkPoint* getCtrlPoints() const {
114 return fPoints;
115 }
halcanary9d524f22016-03-29 09:03:52 -0700116
dandovb3c9d1c2014-08-12 08:34:29 -0700117private:
caryclark5ba2b962016-01-26 17:02:30 -0800118 SkCubicCoeff fCoefs;
dandovb3c9d1c2014-08-12 08:34:29 -0700119 int fMax, fCurrent, fDivisions;
caryclark5ba2b962016-01-26 17:02:30 -0800120 SkPoint fFwDiff[4], fPoints[4];
dandovb3c9d1c2014-08-12 08:34:29 -0700121};
122
123////////////////////////////////////////////////////////////////////////////////
124
dandovecfff212014-08-04 10:02:00 -0700125// size in pixels of each partition per axis, adjust this knob
dandovb3c9d1c2014-08-12 08:34:29 -0700126static const int kPartitionSize = 10;
dandovecfff212014-08-04 10:02:00 -0700127
128/**
129 * Calculate the approximate arc length given a bezier curve's control points.
130 */
131static SkScalar approx_arc_length(SkPoint* points, int count) {
132 if (count < 2) {
133 return 0;
134 }
135 SkScalar arcLength = 0;
136 for (int i = 0; i < count - 1; i++) {
137 arcLength += SkPoint::Distance(points[i], points[i + 1]);
138 }
139 return arcLength;
140}
141
dandovb3c9d1c2014-08-12 08:34:29 -0700142static SkScalar bilerp(SkScalar tx, SkScalar ty, SkScalar c00, SkScalar c10, SkScalar c01,
143 SkScalar c11) {
144 SkScalar a = c00 * (1.f - tx) + c10 * tx;
145 SkScalar b = c01 * (1.f - tx) + c11 * tx;
146 return a * (1.f - ty) + b * ty;
147}
148
149SkISize SkPatchUtils::GetLevelOfDetail(const SkPoint cubics[12], const SkMatrix* matrix) {
halcanary9d524f22016-03-29 09:03:52 -0700150
dandovecfff212014-08-04 10:02:00 -0700151 // Approximate length of each cubic.
dandovb3c9d1c2014-08-12 08:34:29 -0700152 SkPoint pts[kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -0400153 SkPatchUtils::GetTopCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700154 matrix->mapPoints(pts, kNumPtsCubic);
155 SkScalar topLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700156
Mike Reed4ebb43e2017-04-05 11:06:15 -0400157 SkPatchUtils::GetBottomCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700158 matrix->mapPoints(pts, kNumPtsCubic);
159 SkScalar bottomLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700160
Mike Reed4ebb43e2017-04-05 11:06:15 -0400161 SkPatchUtils::GetLeftCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700162 matrix->mapPoints(pts, kNumPtsCubic);
163 SkScalar leftLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700164
Mike Reed4ebb43e2017-04-05 11:06:15 -0400165 SkPatchUtils::GetRightCubic(cubics, pts);
dandovb3c9d1c2014-08-12 08:34:29 -0700166 matrix->mapPoints(pts, kNumPtsCubic);
167 SkScalar rightLength = approx_arc_length(pts, kNumPtsCubic);
halcanary9d524f22016-03-29 09:03:52 -0700168
dandovecfff212014-08-04 10:02:00 -0700169 // Level of detail per axis, based on the larger side between top and bottom or left and right
170 int lodX = static_cast<int>(SkMaxScalar(topLength, bottomLength) / kPartitionSize);
171 int lodY = static_cast<int>(SkMaxScalar(leftLength, rightLength) / kPartitionSize);
halcanary9d524f22016-03-29 09:03:52 -0700172
dandovb3c9d1c2014-08-12 08:34:29 -0700173 return SkISize::Make(SkMax32(8, lodX), SkMax32(8, lodY));
174}
175
Mike Reed4ebb43e2017-04-05 11:06:15 -0400176void SkPatchUtils::GetTopCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700177 points[0] = cubics[kTopP0_CubicCtrlPts];
178 points[1] = cubics[kTopP1_CubicCtrlPts];
179 points[2] = cubics[kTopP2_CubicCtrlPts];
180 points[3] = cubics[kTopP3_CubicCtrlPts];
181}
182
Mike Reed4ebb43e2017-04-05 11:06:15 -0400183void SkPatchUtils::GetBottomCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700184 points[0] = cubics[kBottomP0_CubicCtrlPts];
185 points[1] = cubics[kBottomP1_CubicCtrlPts];
186 points[2] = cubics[kBottomP2_CubicCtrlPts];
187 points[3] = cubics[kBottomP3_CubicCtrlPts];
188}
189
Mike Reed4ebb43e2017-04-05 11:06:15 -0400190void SkPatchUtils::GetLeftCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700191 points[0] = cubics[kLeftP0_CubicCtrlPts];
192 points[1] = cubics[kLeftP1_CubicCtrlPts];
193 points[2] = cubics[kLeftP2_CubicCtrlPts];
194 points[3] = cubics[kLeftP3_CubicCtrlPts];
195}
196
Mike Reed4ebb43e2017-04-05 11:06:15 -0400197void SkPatchUtils::GetRightCubic(const SkPoint cubics[12], SkPoint points[4]) {
dandovb3c9d1c2014-08-12 08:34:29 -0700198 points[0] = cubics[kRightP0_CubicCtrlPts];
199 points[1] = cubics[kRightP1_CubicCtrlPts];
200 points[2] = cubics[kRightP2_CubicCtrlPts];
201 points[3] = cubics[kRightP3_CubicCtrlPts];
202}
203
Mike Reed795c5ea2017-03-17 14:29:05 -0400204sk_sp<SkVertices> SkPatchUtils::MakeVertices(const SkPoint cubics[12], const SkColor srcColors[4],
205 const SkPoint srcTexCoords[4], int lodX, int lodY) {
206 if (lodX < 1 || lodY < 1 || nullptr == cubics) {
207 return nullptr;
208 }
209
210 // check for overflow in multiplication
211 const int64_t lodX64 = (lodX + 1),
212 lodY64 = (lodY + 1),
213 mult64 = lodX64 * lodY64;
214 if (mult64 > SK_MaxS32) {
215 return nullptr;
216 }
217
218 int vertexCount = SkToS32(mult64);
219 // it is recommended to generate draw calls of no more than 65536 indices, so we never generate
220 // more than 60000 indices. To accomplish that we resize the LOD and vertex count
221 if (vertexCount > 10000 || lodX > 200 || lodY > 200) {
222 float weightX = static_cast<float>(lodX) / (lodX + lodY);
223 float weightY = static_cast<float>(lodY) / (lodX + lodY);
224
225 // 200 comes from the 100 * 2 which is the max value of vertices because of the limit of
226 // 60000 indices ( sqrt(60000 / 6) that comes from data->fIndexCount = lodX * lodY * 6)
227 lodX = static_cast<int>(weightX * 200);
228 lodY = static_cast<int>(weightY * 200);
229 vertexCount = (lodX + 1) * (lodY + 1);
230 }
231 const int indexCount = lodX * lodY * 6;
232 uint32_t flags = 0;
233 if (srcTexCoords) {
234 flags |= SkVertices::kHasTexCoords_BuilderFlag;
235 }
236 if (srcColors) {
237 flags |= SkVertices::kHasColors_BuilderFlag;
238 }
239
Mike Reed887cdf12017-04-03 11:11:09 -0400240 SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vertexCount, indexCount, flags);
Mike Reed795c5ea2017-03-17 14:29:05 -0400241 SkPoint* pos = builder.positions();
242 SkPoint* texs = builder.texCoords();
243 SkColor* colors = builder.colors();
244 uint16_t* indices = builder.indices();
Mike Reed7346a1f2017-05-18 22:23:34 -0400245 bool is_opaque = false;
Mike Reed795c5ea2017-03-17 14:29:05 -0400246
247 // if colors is not null then create array for colors
248 SkPMColor colorsPM[kNumCorners];
249 if (srcColors) {
Mike Reed7346a1f2017-05-18 22:23:34 -0400250 SkColor c = ~0;
Mike Reed795c5ea2017-03-17 14:29:05 -0400251 // premultiply colors to avoid color bleeding.
252 for (int i = 0; i < kNumCorners; i++) {
253 colorsPM[i] = SkPreMultiplyColor(srcColors[i]);
Mike Reed7346a1f2017-05-18 22:23:34 -0400254 c &= srcColors[i];
Mike Reed795c5ea2017-03-17 14:29:05 -0400255 }
256 srcColors = colorsPM;
Mike Reed7346a1f2017-05-18 22:23:34 -0400257 is_opaque = (SkColorGetA(c) == 0xFF);
Mike Reed795c5ea2017-03-17 14:29:05 -0400258 }
259
260 SkPoint pts[kNumPtsCubic];
Mike Reed4ebb43e2017-04-05 11:06:15 -0400261 SkPatchUtils::GetBottomCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400262 FwDCubicEvaluator fBottom(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400263 SkPatchUtils::GetTopCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400264 FwDCubicEvaluator fTop(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400265 SkPatchUtils::GetLeftCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400266 FwDCubicEvaluator fLeft(pts);
Mike Reed4ebb43e2017-04-05 11:06:15 -0400267 SkPatchUtils::GetRightCubic(cubics, pts);
Mike Reed795c5ea2017-03-17 14:29:05 -0400268 FwDCubicEvaluator fRight(pts);
269
270 fBottom.restart(lodX);
271 fTop.restart(lodX);
272
273 SkScalar u = 0.0f;
274 int stride = lodY + 1;
275 for (int x = 0; x <= lodX; x++) {
276 SkPoint bottom = fBottom.next(), top = fTop.next();
277 fLeft.restart(lodY);
278 fRight.restart(lodY);
279 SkScalar v = 0.f;
280 for (int y = 0; y <= lodY; y++) {
281 int dataIndex = x * (lodY + 1) + y;
282
283 SkPoint left = fLeft.next(), right = fRight.next();
284
285 SkPoint s0 = SkPoint::Make((1.0f - v) * top.x() + v * bottom.x(),
286 (1.0f - v) * top.y() + v * bottom.y());
287 SkPoint s1 = SkPoint::Make((1.0f - u) * left.x() + u * right.x(),
288 (1.0f - u) * left.y() + u * right.y());
289 SkPoint s2 = SkPoint::Make(
290 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].x()
291 + u * fTop.getCtrlPoints()[3].x())
292 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].x()
293 + u * fBottom.getCtrlPoints()[3].x()),
294 (1.0f - v) * ((1.0f - u) * fTop.getCtrlPoints()[0].y()
295 + u * fTop.getCtrlPoints()[3].y())
296 + v * ((1.0f - u) * fBottom.getCtrlPoints()[0].y()
297 + u * fBottom.getCtrlPoints()[3].y()));
298 pos[dataIndex] = s0 + s1 - s2;
299
300 if (colors) {
Mike Reed7346a1f2017-05-18 22:23:34 -0400301 uint8_t a = 0xFF;
302 // We do the opaque check for speed, and to ensure that opaque stays opaque,
303 // in case we lose precision in the bilerp.
304 if (!is_opaque) {
305 a = uint8_t(bilerp(u, v,
306 SkScalar(SkColorGetA(colorsPM[kTopLeft_Corner])),
307 SkScalar(SkColorGetA(colorsPM[kTopRight_Corner])),
308 SkScalar(SkColorGetA(colorsPM[kBottomLeft_Corner])),
309 SkScalar(SkColorGetA(colorsPM[kBottomRight_Corner]))));
310 }
Mike Reed795c5ea2017-03-17 14:29:05 -0400311 uint8_t r = uint8_t(bilerp(u, v,
312 SkScalar(SkColorGetR(colorsPM[kTopLeft_Corner])),
313 SkScalar(SkColorGetR(colorsPM[kTopRight_Corner])),
314 SkScalar(SkColorGetR(colorsPM[kBottomLeft_Corner])),
315 SkScalar(SkColorGetR(colorsPM[kBottomRight_Corner]))));
316 uint8_t g = uint8_t(bilerp(u, v,
317 SkScalar(SkColorGetG(colorsPM[kTopLeft_Corner])),
318 SkScalar(SkColorGetG(colorsPM[kTopRight_Corner])),
319 SkScalar(SkColorGetG(colorsPM[kBottomLeft_Corner])),
320 SkScalar(SkColorGetG(colorsPM[kBottomRight_Corner]))));
321 uint8_t b = uint8_t(bilerp(u, v,
322 SkScalar(SkColorGetB(colorsPM[kTopLeft_Corner])),
323 SkScalar(SkColorGetB(colorsPM[kTopRight_Corner])),
324 SkScalar(SkColorGetB(colorsPM[kBottomLeft_Corner])),
325 SkScalar(SkColorGetB(colorsPM[kBottomRight_Corner]))));
326 colors[dataIndex] = SkPackARGB32(a,r,g,b);
327 }
328
329 if (texs) {
330 texs[dataIndex] = SkPoint::Make(bilerp(u, v, srcTexCoords[kTopLeft_Corner].x(),
331 srcTexCoords[kTopRight_Corner].x(),
332 srcTexCoords[kBottomLeft_Corner].x(),
333 srcTexCoords[kBottomRight_Corner].x()),
334 bilerp(u, v, srcTexCoords[kTopLeft_Corner].y(),
335 srcTexCoords[kTopRight_Corner].y(),
336 srcTexCoords[kBottomLeft_Corner].y(),
337 srcTexCoords[kBottomRight_Corner].y()));
338
339 }
340
341 if(x < lodX && y < lodY) {
342 int i = 6 * (x * lodY + y);
343 indices[i] = x * stride + y;
344 indices[i + 1] = x * stride + 1 + y;
345 indices[i + 2] = (x + 1) * stride + 1 + y;
346 indices[i + 3] = indices[i];
347 indices[i + 4] = indices[i + 2];
348 indices[i + 5] = (x + 1) * stride + y;
349 }
350 v = SkScalarClampMax(v + 1.f / lodY, 1);
351 }
352 u = SkScalarClampMax(u + 1.f / lodX, 1);
353 }
354 return builder.detach();
355}