blob: 0a7759d8a13295152ee76cbd5905c3b514e06711 [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"
bsalomon@google.com181e9bd2011-09-07 18:42:30 +000011
senorblanco@chromium.org9d18b782011-03-28 20:47:09 +000012#include "GrPoint.h"
13
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}