blob: 2951e1f72cd1e6dea346220e5ab31cc4bdaf5ff1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000010#include "GrPathUtils.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000011#include "GrPoint.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000012#include "SkGeometry.h"
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000013
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000014GrScalar GrPathUtils::scaleToleranceToSrc(GrScalar devTol,
bsalomon@google.com38396322011-09-09 19:32:04 +000015 const GrMatrix& viewM,
16 const GrRect& pathBounds) {
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000017 // In order to tesselate the path we get a bound on how much the matrix can
18 // stretch when mapping to screen coordinates.
19 GrScalar stretch = viewM.getMaxStretch();
20 GrScalar srcTol = devTol;
21
22 if (stretch < 0) {
bsalomon@google.com38396322011-09-09 19:32:04 +000023 // take worst case mapRadius amoung four corners.
24 // (less than perfect)
25 for (int i = 0; i < 4; ++i) {
26 GrMatrix mat;
27 mat.setTranslate((i % 2) ? pathBounds.fLeft : pathBounds.fRight,
28 (i < 2) ? pathBounds.fTop : pathBounds.fBottom);
29 mat.postConcat(viewM);
30 stretch = SkMaxScalar(stretch, mat.mapRadius(SK_Scalar1));
31 }
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000032 }
33 srcTol = GrScalarDiv(srcTol, stretch);
34 return srcTol;
35}
36
bsalomon@google.comb5b31682011-06-16 18:05:35 +000037static const int MAX_POINTS_PER_CURVE = 1 << 10;
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000038static const GrScalar gMinCurveTol = GrFloatToScalar(0.0001f);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000039
40uint32_t GrPathUtils::quadraticPointCount(const GrPoint points[],
tomhudson@google.comc10a8882011-06-28 15:19:32 +000041 GrScalar tol) {
42 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +000043 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +000044 }
45 GrAssert(tol > 0);
46
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000047 GrScalar d = points[1].distanceToLineSegmentBetween(points[0], points[2]);
tomhudson@google.comc10a8882011-06-28 15:19:32 +000048 if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000049 return 1;
50 } else {
51 // Each time we subdivide, d should be cut in 4. So we need to
52 // subdivide x = log4(d/tol) times. x subdivisions creates 2^(x)
53 // points.
54 // 2^(log4(x)) = sqrt(x);
epoger@google.com2047f002011-05-17 17:36:59 +000055 int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol)));
bsalomon@google.com61f3bde2011-06-17 20:06:49 +000056 int pow2 = GrNextPow2(temp);
57 // Because of NaNs & INFs we can wind up with a degenerate temp
58 // such that pow2 comes out negative. Also, our point generator
59 // will always output at least one pt.
60 if (pow2 < 1) {
61 pow2 = 1;
62 }
63 return GrMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000064 }
65}
66
67uint32_t GrPathUtils::generateQuadraticPoints(const GrPoint& p0,
tomhudson@google.comc10a8882011-06-28 15:19:32 +000068 const GrPoint& p1,
69 const GrPoint& p2,
70 GrScalar tolSqd,
71 GrPoint** points,
72 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000073 if (pointsLeft < 2 ||
74 (p1.distanceToLineSegmentBetweenSqd(p0, p2)) < tolSqd) {
75 (*points)[0] = p2;
76 *points += 1;
77 return 1;
78 }
79
80 GrPoint q[] = {
reed@google.com7744c202011-05-06 19:26:26 +000081 { GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY) },
82 { GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY) },
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000083 };
reed@google.com7744c202011-05-06 19:26:26 +000084 GrPoint r = { GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000085
86 pointsLeft >>= 1;
87 uint32_t a = generateQuadraticPoints(p0, q[0], r, tolSqd, points, pointsLeft);
88 uint32_t b = generateQuadraticPoints(r, q[1], p2, tolSqd, points, pointsLeft);
89 return a + b;
90}
91
92uint32_t GrPathUtils::cubicPointCount(const GrPoint points[],
93 GrScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +000094 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +000095 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +000096 }
97 GrAssert(tol > 0);
98
99 GrScalar d = GrMax(
100 points[1].distanceToLineSegmentBetweenSqd(points[0], points[3]),
101 points[2].distanceToLineSegmentBetweenSqd(points[0], points[3]));
epoger@google.com2047f002011-05-17 17:36:59 +0000102 d = SkScalarSqrt(d);
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000103 if (d <= tol) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000104 return 1;
105 } else {
epoger@google.com2047f002011-05-17 17:36:59 +0000106 int temp = SkScalarCeil(SkScalarSqrt(SkScalarDiv(d, tol)));
bsalomon@google.com61f3bde2011-06-17 20:06:49 +0000107 int pow2 = GrNextPow2(temp);
108 // Because of NaNs & INFs we can wind up with a degenerate temp
109 // such that pow2 comes out negative. Also, our point generator
110 // will always output at least one pt.
111 if (pow2 < 1) {
112 pow2 = 1;
113 }
114 return GrMin(pow2, MAX_POINTS_PER_CURVE);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000115 }
116}
117
118uint32_t GrPathUtils::generateCubicPoints(const GrPoint& p0,
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000119 const GrPoint& p1,
120 const GrPoint& p2,
121 const GrPoint& p3,
122 GrScalar tolSqd,
123 GrPoint** points,
124 uint32_t pointsLeft) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000125 if (pointsLeft < 2 ||
126 (p1.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd &&
127 p2.distanceToLineSegmentBetweenSqd(p0, p3) < tolSqd)) {
128 (*points)[0] = p3;
129 *points += 1;
130 return 1;
131 }
132 GrPoint q[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000133 { GrScalarAve(p0.fX, p1.fX), GrScalarAve(p0.fY, p1.fY) },
134 { GrScalarAve(p1.fX, p2.fX), GrScalarAve(p1.fY, p2.fY) },
135 { GrScalarAve(p2.fX, p3.fX), GrScalarAve(p2.fY, p3.fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000136 };
137 GrPoint r[] = {
reed@google.com7744c202011-05-06 19:26:26 +0000138 { GrScalarAve(q[0].fX, q[1].fX), GrScalarAve(q[0].fY, q[1].fY) },
139 { GrScalarAve(q[1].fX, q[2].fX), GrScalarAve(q[1].fY, q[2].fY) }
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000140 };
reed@google.com7744c202011-05-06 19:26:26 +0000141 GrPoint s = { GrScalarAve(r[0].fX, r[1].fX), GrScalarAve(r[0].fY, r[1].fY) };
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000142 pointsLeft >>= 1;
143 uint32_t a = generateCubicPoints(p0, q[0], r[0], s, tolSqd, points, pointsLeft);
144 uint32_t b = generateCubicPoints(s, r[1], q[2], p3, tolSqd, points, pointsLeft);
145 return a + b;
146}
147
reed@google.com07f3ee12011-05-16 17:21:57 +0000148int GrPathUtils::worstCasePointCount(const GrPath& path, int* subpaths,
149 GrScalar tol) {
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000150 if (tol < gMinCurveTol) {
tomhudson@google.comafec7ba2011-06-30 14:47:55 +0000151 tol = gMinCurveTol;
tomhudson@google.comc10a8882011-06-28 15:19:32 +0000152 }
153 GrAssert(tol > 0);
154
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000155 int pointCount = 0;
156 *subpaths = 1;
157
158 bool first = true;
159
senorblanco@chromium.org129b8e32011-06-15 17:52:09 +0000160 SkPath::Iter iter(path, false);
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000161 GrPathCmd cmd;
162
163 GrPoint pts[4];
reed@google.com07f3ee12011-05-16 17:21:57 +0000164 while ((cmd = (GrPathCmd)iter.next(pts)) != kEnd_PathCmd) {
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +0000165
166 switch (cmd) {
167 case kLine_PathCmd:
168 pointCount += 1;
169 break;
170 case kQuadratic_PathCmd:
171 pointCount += quadraticPointCount(pts, tol);
172 break;
173 case kCubic_PathCmd:
174 pointCount += cubicPointCount(pts, tol);
175 break;
176 case kMove_PathCmd:
177 pointCount += 1;
178 if (!first) {
179 ++(*subpaths);
180 }
181 break;
182 default:
183 break;
184 }
185 first = false;
186 }
187 return pointCount;
188}
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000189
190namespace {
191// The matrix computed for quadDesignSpaceToUVCoordsMatrix should never really
192// have perspective and we really want to avoid perspective matrix muls.
193// However, the first two entries of the perspective row may be really close to
194// 0 and the third may not be 1 due to a scale on the entire matrix.
195inline void fixup_matrix(GrMatrix* mat) {
bsalomon@google.com5e9bf822012-01-17 14:39:21 +0000196#ifndef SK_SCALAR_IS_FLOAT
197 GrCrash("Expected scalar is float.");
198#endif
199 static const GrScalar gTOL = 1.f / 100.f;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000200 GrAssert(GrScalarAbs(mat->get(SkMatrix::kMPersp0)) < gTOL);
201 GrAssert(GrScalarAbs(mat->get(SkMatrix::kMPersp1)) < gTOL);
202 float m33 = mat->get(SkMatrix::kMPersp2);
203 if (1.f != m33) {
204 m33 = 1.f / m33;
205 mat->setAll(m33 * mat->get(SkMatrix::kMScaleX),
206 m33 * mat->get(SkMatrix::kMSkewX),
207 m33 * mat->get(SkMatrix::kMTransX),
208 m33 * mat->get(SkMatrix::kMSkewY),
209 m33 * mat->get(SkMatrix::kMScaleY),
210 m33 * mat->get(SkMatrix::kMTransY),
211 0.f, 0.f, 1.f);
212 } else {
213 mat->setPerspX(0);
214 mat->setPerspY(0);
215 }
216}
217}
218
219// Compute a matrix that goes from the 2d space coordinates to UV space where
220// u^2-v = 0 specifies the quad.
221void GrPathUtils::quadDesignSpaceToUVCoordsMatrix(const SkPoint qPts[3],
222 GrMatrix* matrix) {
223 // can't make this static, no cons :(
224 SkMatrix UVpts;
bsalomon@google.com5e9bf822012-01-17 14:39:21 +0000225#ifndef SK_SCALAR_IS_FLOAT
226 GrCrash("Expected scalar is float.");
227#endif
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000228 // We want M such that M * xy_pt = uv_pt
229 // We know M * control_pts = [0 1/2 1]
230 // [0 0 1]
231 // [1 1 1]
232 // We invert the control pt matrix and post concat to both sides to get M.
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000233 UVpts.setAll(0, 0.5f, 1.f,
234 0, 0, 1.f,
235 1.f, 1.f, 1.f);
236 matrix->setAll(qPts[0].fX, qPts[1].fX, qPts[2].fX,
237 qPts[0].fY, qPts[1].fY, qPts[2].fY,
238 1.f, 1.f, 1.f);
bsalomon@google.comdc3c7802012-01-31 20:46:32 +0000239 if (!matrix->invert(matrix)) {
240 // The quad is degenerate. Hopefully this is rare. Find the pts that are
241 // farthest apart to compute a line (unless it is really a pt).
242 SkScalar maxD = qPts[0].distanceToSqd(qPts[1]);
243 int maxEdge = 0;
244 SkScalar d = qPts[1].distanceToSqd(qPts[2]);
245 if (d > maxD) {
246 maxD = d;
247 maxEdge = 1;
248 }
249 d = qPts[2].distanceToSqd(qPts[0]);
250 if (d > maxD) {
251 maxD = d;
252 maxEdge = 2;
253 }
254 // We could have a tolerance here, not sure if it would improve anything
255 if (maxD > 0) {
256 // Set the matrix to give (u = 0, v = distance_to_line)
257 GrVec lineVec = qPts[maxEdge] - qPts[(maxEdge + 1)%3];
258 lineVec.setOrthog(lineVec);
259 lineVec.dot(qPts[0]);
260 matrix->setAll(0, 0, 0,
261 lineVec.fX, lineVec.fY, -lineVec.dot(qPts[maxEdge]),
262 0, 0, 1.f);
263 } else {
264 // It's a point. It should cover zero area. Just set the matrix such
265 // that (u, v) will always be far away from the quad.
266 matrix->setAll(0, 0, 100 * SK_Scalar1,
267 0, 0, 100 * SK_Scalar1,
268 0, 0, 1.f);
269 }
270 } else {
271 matrix->postConcat(UVpts);
272 fixup_matrix(matrix);
273 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000274}
275
276namespace {
277void convert_noninflect_cubic_to_quads(const SkPoint p[4],
278 SkScalar tolScale,
279 SkTArray<SkPoint, true>* quads,
280 int sublevel = 0) {
281 SkVector ab = p[1];
282 ab -= p[0];
283 SkVector dc = p[2];
284 dc -= p[3];
285
286 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
287 // base tolerance is 2 pixels in dev coords.
288 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 1 * SK_Scalar1);
289 static const int kMaxSubdivs = 10;
290
291 ab.scale(gLengthScale);
292 dc.scale(gLengthScale);
293
294 SkVector c0 = p[0];
295 c0 += ab;
296 SkVector c1 = p[3];
297 c1 += dc;
298
299 SkScalar dSqd = c0.distanceToSqd(c1);
300 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
301 SkPoint cAvg = c0;
302 cAvg += c1;
303 cAvg.scale(SK_ScalarHalf);
304
305 SkPoint* pts = quads->push_back_n(3);
306 pts[0] = p[0];
307 pts[1] = cAvg;
308 pts[2] = p[3];
309
310 return;
311 } else {
312 SkPoint choppedPts[7];
313 SkChopCubicAtHalf(p, choppedPts);
314 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
315 quads, sublevel + 1);
316 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
317 quads, sublevel + 1);
318 }
319}
320}
321
322void GrPathUtils::convertCubicToQuads(const GrPoint p[4],
323 SkScalar tolScale,
324 SkTArray<SkPoint, true>* quads) {
325 SkPoint chopped[10];
326 int count = SkChopCubicAtInflections(p, chopped);
327
328 for (int i = 0; i < count; ++i) {
329 SkPoint* cubic = chopped + 3*i;
330 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
331 }
332
333}