blob: 3fcafec1c27179fb4065a0ea40b819b3de944ee4 [file] [log] [blame]
Chris Dalton09a7bb22018-08-31 19:53:15 +08001/*
2 * Copyright 2018 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 "GrCCStrokeGeometry.h"
9
10#include "SkGeometry.h"
11#include "SkMathPriv.h"
12#include "SkNx.h"
13#include "SkStrokeRec.h"
14
15// This is the maximum distance in pixels that we can stray from the edge of a stroke when
16// converting it to flat line segments.
17static constexpr float kMaxErrorFromLinearization = 1/8.f;
18
19static inline float length(const Sk2f& n) {
20 Sk2f nn = n*n;
21 return SkScalarSqrt(nn[0] + nn[1]);
22}
23
24static inline Sk2f normalize(const Sk2f& v) {
25 Sk2f vv = v*v;
26 vv += SkNx_shuffle<1,0>(vv);
27 return v * vv.rsqrt();
28}
29
30static inline void transpose(const Sk2f& a, const Sk2f& b, Sk2f* X, Sk2f* Y) {
31 float transpose[4];
32 a.store(transpose);
33 b.store(transpose+2);
34 Sk2f::Load2(transpose, X, Y);
35}
36
37static inline void normalize2(const Sk2f& v0, const Sk2f& v1, SkPoint out[2]) {
38 Sk2f X, Y;
39 transpose(v0, v1, &X, &Y);
40 Sk2f invlength = (X*X + Y*Y).rsqrt();
41 Sk2f::Store2(out, Y * invlength, -X * invlength);
42}
43
44static inline float calc_curvature_costheta(const Sk2f& leftTan, const Sk2f& rightTan) {
45 Sk2f X, Y;
46 transpose(leftTan, rightTan, &X, &Y);
47 Sk2f invlength = (X*X + Y*Y).rsqrt();
48 Sk2f dotprod = leftTan * rightTan;
49 return (dotprod[0] + dotprod[1]) * invlength[0] * invlength[1];
50}
51
52static GrCCStrokeGeometry::Verb join_verb_from_join(SkPaint::Join join) {
53 using Verb = GrCCStrokeGeometry::Verb;
54 switch (join) {
55 case SkPaint::kBevel_Join:
56 return Verb::kBevelJoin;
57 case SkPaint::kMiter_Join:
58 return Verb::kMiterJoin;
59 case SkPaint::kRound_Join:
60 return Verb::kRoundJoin;
61 }
62 SK_ABORT("Invalid SkPaint::Join.");
63 return Verb::kBevelJoin;
64}
65
66void GrCCStrokeGeometry::beginPath(const SkStrokeRec& stroke, float strokeDevWidth,
67 InstanceTallies* tallies) {
68 SkASSERT(!fInsideContour);
69 // Client should have already converted the stroke to device space (i.e. width=1 for hairline).
70 SkASSERT(strokeDevWidth > 0);
71
72 fCurrStrokeRadius = strokeDevWidth/2;
73 fCurrStrokeJoinVerb = join_verb_from_join(stroke.getJoin());
74 fCurrStrokeCapType = stroke.getCap();
75 fCurrStrokeTallies = tallies;
76
77 if (Verb::kMiterJoin == fCurrStrokeJoinVerb) {
78 // We implement miters by placing a triangle-shaped cap on top of a bevel join. Convert the
79 // "miter limit" to how tall that triangle cap can be.
80 float m = stroke.getMiter();
81 fMiterMaxCapHeightOverWidth = .5f * SkScalarSqrt(m*m - 1);
82 }
83
84 // Find the angle of curvature where the arc height above a simple line from point A to point B
85 // is equal to kMaxErrorFromLinearization.
86 float r = SkTMax(1 - kMaxErrorFromLinearization / fCurrStrokeRadius, 0.f);
87 fMaxCurvatureCosTheta = 2*r*r - 1;
88
89 fCurrContourFirstPtIdx = -1;
90 fCurrContourFirstNormalIdx = -1;
91
92 fVerbs.push_back(Verb::kBeginPath);
93}
94
95void GrCCStrokeGeometry::moveTo(SkPoint pt) {
96 SkASSERT(!fInsideContour);
97 fCurrContourFirstPtIdx = fPoints.count();
98 fCurrContourFirstNormalIdx = fNormals.count();
99 fPoints.push_back(pt);
100 SkDEBUGCODE(fInsideContour = true);
101}
102
103void GrCCStrokeGeometry::lineTo(SkPoint pt) {
104 SkASSERT(fInsideContour);
105 this->lineTo(fCurrStrokeJoinVerb, pt);
106}
107
108void GrCCStrokeGeometry::lineTo(Verb leftJoinVerb, SkPoint pt) {
109 Sk2f tan = Sk2f::Load(&pt) - Sk2f::Load(&fPoints.back());
110 if ((tan == 0).allTrue()) {
111 return;
112 }
113
114 tan = normalize(tan);
115 SkVector n = SkVector::Make(tan[1], -tan[0]);
116
117 this->recordLeftJoinIfNotEmpty(leftJoinVerb, n);
118 fNormals.push_back(n);
119
120 this->recordStroke(Verb::kLinearStroke, 0);
121 fPoints.push_back(pt);
122}
123
124void GrCCStrokeGeometry::quadraticTo(const SkPoint P[3]) {
125 SkASSERT(fInsideContour);
126 this->quadraticTo(fCurrStrokeJoinVerb, P, SkFindQuadMaxCurvature(P));
127}
128
129// Wang's formula for quadratics (1985) gives us the number of evenly spaced (in the parametric
130// sense) line segments that are guaranteed to be within a distance of "kMaxErrorFromLinearization"
131// from the actual curve.
132static inline float wangs_formula_quadratic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2) {
133 static constexpr float k = 2 / (8 * kMaxErrorFromLinearization);
134 float f = SkScalarSqrt(k * length(p2 - p1*2 + p0));
135 return SkScalarCeilToInt(f);
136}
137
138void GrCCStrokeGeometry::quadraticTo(Verb leftJoinVerb, const SkPoint P[3], float maxCurvatureT) {
139 Sk2f p0 = Sk2f::Load(P);
140 Sk2f p1 = Sk2f::Load(P+1);
141 Sk2f p2 = Sk2f::Load(P+2);
142
143 Sk2f tan0 = p1 - p0;
144 Sk2f tan1 = p2 - p1;
145
146 // Snap to a "lineTo" if the control point is so close to an endpoint that FP error will become
147 // an issue.
148 if ((tan0.abs() < SK_ScalarNearlyZero).allTrue() || // p0 ~= p1
149 (tan1.abs() < SK_ScalarNearlyZero).allTrue()) { // p1 ~= p2
150 this->lineTo(leftJoinVerb, P[2]);
151 return;
152 }
153
154 SkPoint normals[2];
155 normalize2(tan0, tan1, normals);
156
157 // Decide how many flat line segments to chop the curve into.
158 int numSegments = wangs_formula_quadratic(p0, p1, p2);
159 if (numSegments <= 1) {
160 this->rotateTo(leftJoinVerb, normals[0]);
161 this->lineTo(Verb::kInternalRoundJoin, P[2]);
162 this->rotateTo(Verb::kInternalRoundJoin, normals[1]);
163 return;
164 }
165
166 // At + B gives a vector tangent to the quadratic.
167 Sk2f A = p0 - p1*2 + p2;
168 Sk2f B = p1 - p0;
169
170 // Find a line segment that crosses max curvature.
171 float segmentLength = SkScalarInvert(numSegments);
172 float leftT = maxCurvatureT - segmentLength/2;
173 float rightT = maxCurvatureT + segmentLength/2;
174 Sk2f leftTan, rightTan;
175 if (leftT <= 0) {
176 leftT = 0;
177 leftTan = tan0;
178 rightT = segmentLength;
179 rightTan = A*rightT + B;
180 } else if (rightT >= 1) {
181 leftT = 1 - segmentLength;
182 leftTan = A*leftT + B;
183 rightT = 1;
184 rightTan = tan1;
185 } else {
186 leftTan = A*leftT + B;
187 rightTan = A*rightT + B;
188 }
189
190 // Check if curvature is too strong for a triangle strip on the line segment that crosses max
191 // curvature. If it is, we will chop and convert the segment to a "lineTo" with round joins.
192 //
193 // FIXME: This is quite costly and the vast majority of curves only have moderate curvature. We
194 // would benefit significantly from a quick reject that detects curves that don't need special
195 // treatment for strong curvature.
196 bool isCurvatureTooStrong = calc_curvature_costheta(leftTan, rightTan) < fMaxCurvatureCosTheta;
197 if (isCurvatureTooStrong) {
198 SkPoint ptsBuffer[5];
199 const SkPoint* currQuadratic = P;
200
201 if (leftT > 0) {
202 SkChopQuadAt(currQuadratic, ptsBuffer, leftT);
203 this->quadraticTo(leftJoinVerb, ptsBuffer, /*maxCurvatureT=*/1);
204 if (rightT < 1) {
205 rightT = (rightT - leftT) / (1 - leftT);
206 }
207 currQuadratic = ptsBuffer + 2;
208 } else {
209 this->rotateTo(leftJoinVerb, normals[0]);
210 }
211
212 if (rightT < 1) {
213 SkChopQuadAt(currQuadratic, ptsBuffer, rightT);
214 this->lineTo(Verb::kInternalRoundJoin, ptsBuffer[2]);
215 this->quadraticTo(Verb::kInternalRoundJoin, ptsBuffer + 2, /*maxCurvatureT=*/0);
216 } else {
217 this->lineTo(Verb::kInternalRoundJoin, currQuadratic[2]);
218 this->rotateTo(Verb::kInternalRoundJoin, normals[1]);
219 }
220 return;
221 }
222
223 this->recordLeftJoinIfNotEmpty(leftJoinVerb, normals[0]);
224 fNormals.push_back_n(2, normals);
225
226 this->recordStroke(Verb::kQuadraticStroke, SkNextLog2(numSegments));
227 p1.store(&fPoints.push_back());
228 p2.store(&fPoints.push_back());
229}
230
231void GrCCStrokeGeometry::cubicTo(const SkPoint P[4]) {
232 SkASSERT(fInsideContour);
233 float roots[3];
234 int numRoots = SkFindCubicMaxCurvature(P, roots);
235 this->cubicTo(fCurrStrokeJoinVerb, P,
236 numRoots > 0 ? roots[numRoots/2] : 0,
237 numRoots > 1 ? roots[0] : kLeftMaxCurvatureNone,
238 numRoots > 2 ? roots[2] : kRightMaxCurvatureNone);
239}
240
241// Wang's formula for cubics (1985) gives us the number of evenly spaced (in the parametric sense)
242// line segments that are guaranteed to be within a distance of "kMaxErrorFromLinearization"
243// from the actual curve.
244static inline float wangs_formula_cubic(const Sk2f& p0, const Sk2f& p1, const Sk2f& p2,
245 const Sk2f& p3) {
246 static constexpr float k = (3 * 2) / (8 * kMaxErrorFromLinearization);
247 float f = SkScalarSqrt(k * length(Sk2f::Max((p2 - p1*2 + p0).abs(),
248 (p3 - p2*2 + p1).abs())));
249 return SkScalarCeilToInt(f);
250}
251
252void GrCCStrokeGeometry::cubicTo(Verb leftJoinVerb, const SkPoint P[4], float maxCurvatureT,
253 float leftMaxCurvatureT, float rightMaxCurvatureT) {
254 Sk2f p0 = Sk2f::Load(P);
255 Sk2f p1 = Sk2f::Load(P+1);
256 Sk2f p2 = Sk2f::Load(P+2);
257 Sk2f p3 = Sk2f::Load(P+3);
258
259 Sk2f tan0 = p1 - p0;
260 Sk2f tan1 = p3 - p2;
261
262 // Snap control points to endpoints if they are so close that FP error will become an issue.
263 if ((tan0.abs() < SK_ScalarNearlyZero).allTrue()) { // p0 ~= p1
264 p1 = p0;
265 tan0 = p2 - p0;
266 if ((tan0.abs() < SK_ScalarNearlyZero).allTrue()) { // p0 ~= p1 ~= p2
267 this->lineTo(leftJoinVerb, P[3]);
268 return;
269 }
270 }
271 if ((tan1.abs() < SK_ScalarNearlyZero).allTrue()) { // p2 ~= p3
272 p2 = p3;
273 tan1 = p3 - p1;
274 if ((tan1.abs() < SK_ScalarNearlyZero).allTrue() || // p1 ~= p2 ~= p3
275 (p0 == p1).allTrue()) { // p0 ~= p1 AND p2 ~= p3
276 this->lineTo(leftJoinVerb, P[3]);
277 return;
278 }
279 }
280
281 SkPoint normals[2];
282 normalize2(tan0, tan1, normals);
283
284 // Decide how many flat line segments to chop the curve into.
285 int numSegments = wangs_formula_cubic(p0, p1, p2, p3);
286 if (numSegments <= 1) {
287 this->rotateTo(leftJoinVerb, normals[0]);
288 this->lineTo(leftJoinVerb, P[3]);
289 this->rotateTo(Verb::kInternalRoundJoin, normals[1]);
290 return;
291 }
292
293 // At^2 + Bt + C gives a vector tangent to the cubic. (More specifically, it's the derivative
294 // minus an irrelevant scale by 3, since all we care about is the direction.)
295 Sk2f A = p3 + (p1 - p2)*3 - p0;
296 Sk2f B = (p0 - p1*2 + p2)*2;
297 Sk2f C = p1 - p0;
298
299 // Find a line segment that crosses max curvature.
300 float segmentLength = SkScalarInvert(numSegments);
301 float leftT = maxCurvatureT - segmentLength/2;
302 float rightT = maxCurvatureT + segmentLength/2;
303 Sk2f leftTan, rightTan;
304 if (leftT <= 0) {
305 leftT = 0;
306 leftTan = tan0;
307 rightT = segmentLength;
308 rightTan = A*rightT*rightT + B*rightT + C;
309 } else if (rightT >= 1) {
310 leftT = 1 - segmentLength;
311 leftTan = A*leftT*leftT + B*leftT + C;
312 rightT = 1;
313 rightTan = tan1;
314 } else {
315 leftTan = A*leftT*leftT + B*leftT + C;
316 rightTan = A*rightT*rightT + B*rightT + C;
317 }
318
319 // Check if curvature is too strong for a triangle strip on the line segment that crosses max
320 // curvature. If it is, we will chop and convert the segment to a "lineTo" with round joins.
321 //
322 // FIXME: This is quite costly and the vast majority of curves only have moderate curvature. We
323 // would benefit significantly from a quick reject that detects curves that don't need special
324 // treatment for strong curvature.
325 bool isCurvatureTooStrong = calc_curvature_costheta(leftTan, rightTan) < fMaxCurvatureCosTheta;
326 if (isCurvatureTooStrong) {
327 SkPoint ptsBuffer[7];
328 p0.store(ptsBuffer);
329 p1.store(ptsBuffer + 1);
330 p2.store(ptsBuffer + 2);
331 p3.store(ptsBuffer + 3);
332 const SkPoint* currCubic = ptsBuffer;
333
334 if (leftT > 0) {
335 SkChopCubicAt(currCubic, ptsBuffer, leftT);
336 this->cubicTo(leftJoinVerb, ptsBuffer, /*maxCurvatureT=*/1,
337 (kLeftMaxCurvatureNone != leftMaxCurvatureT)
338 ? leftMaxCurvatureT/leftT : kLeftMaxCurvatureNone,
339 kRightMaxCurvatureNone);
340 if (rightT < 1) {
341 rightT = (rightT - leftT) / (1 - leftT);
342 }
343 if (rightMaxCurvatureT < 1 && kRightMaxCurvatureNone != rightMaxCurvatureT) {
344 rightMaxCurvatureT = (rightMaxCurvatureT - leftT) / (1 - leftT);
345 }
346 currCubic = ptsBuffer + 3;
347 } else {
348 this->rotateTo(leftJoinVerb, normals[0]);
349 }
350
351 if (rightT < 1) {
352 SkChopCubicAt(currCubic, ptsBuffer, rightT);
353 this->lineTo(Verb::kInternalRoundJoin, ptsBuffer[3]);
354 currCubic = ptsBuffer + 3;
355 this->cubicTo(Verb::kInternalRoundJoin, currCubic, /*maxCurvatureT=*/0,
356 kLeftMaxCurvatureNone, kRightMaxCurvatureNone);
357 } else {
358 this->lineTo(Verb::kInternalRoundJoin, currCubic[3]);
359 this->rotateTo(Verb::kInternalRoundJoin, normals[1]);
360 }
361 return;
362 }
363
364 // Recurse and check the other two points of max curvature, if any.
365 if (kRightMaxCurvatureNone != rightMaxCurvatureT) {
366 this->cubicTo(leftJoinVerb, P, rightMaxCurvatureT, leftMaxCurvatureT,
367 kRightMaxCurvatureNone);
368 return;
369 }
370 if (kLeftMaxCurvatureNone != leftMaxCurvatureT) {
371 SkASSERT(kRightMaxCurvatureNone == rightMaxCurvatureT);
372 this->cubicTo(leftJoinVerb, P, leftMaxCurvatureT, kLeftMaxCurvatureNone,
373 kRightMaxCurvatureNone);
374 return;
375 }
376
377 this->recordLeftJoinIfNotEmpty(leftJoinVerb, normals[0]);
378 fNormals.push_back_n(2, normals);
379
380 this->recordStroke(Verb::kCubicStroke, SkNextLog2(numSegments));
381 p1.store(&fPoints.push_back());
382 p2.store(&fPoints.push_back());
383 p3.store(&fPoints.push_back());
384}
385
386void GrCCStrokeGeometry::recordStroke(Verb verb, int numSegmentsLog2) {
387 SkASSERT(Verb::kLinearStroke != verb || 0 == numSegmentsLog2);
388 SkASSERT(numSegmentsLog2 <= kMaxNumLinearSegmentsLog2);
389 fVerbs.push_back(verb);
390 if (Verb::kLinearStroke != verb) {
391 SkASSERT(numSegmentsLog2 > 0);
392 fParams.push_back().fNumLinearSegmentsLog2 = numSegmentsLog2;
393 }
394 ++fCurrStrokeTallies->fStrokes[numSegmentsLog2];
395}
396
397void GrCCStrokeGeometry::rotateTo(Verb leftJoinVerb, SkVector normal) {
398 this->recordLeftJoinIfNotEmpty(leftJoinVerb, normal);
399 fNormals.push_back(normal);
400}
401
402void GrCCStrokeGeometry::recordLeftJoinIfNotEmpty(Verb joinVerb, SkVector nextNormal) {
403 if (fNormals.count() <= fCurrContourFirstNormalIdx) {
404 // The contour is empty. Nothing to join with.
405 SkASSERT(fNormals.count() == fCurrContourFirstNormalIdx);
406 return;
407 }
408
409 if (Verb::kBevelJoin == joinVerb) {
410 this->recordBevelJoin(Verb::kBevelJoin);
411 return;
412 }
413
414 Sk2f n0 = Sk2f::Load(&fNormals.back());
415 Sk2f n1 = Sk2f::Load(&nextNormal);
416 Sk2f base = n1 - n0;
417 if ((base.abs() * fCurrStrokeRadius < kMaxErrorFromLinearization).allTrue()) {
418 // Treat any join as a bevel when the outside corners of the two adjoining strokes are
419 // close enough to each other. This is important because "miterCapHeightOverWidth" becomes
420 // unstable when n0 and n1 are nearly equal.
421 this->recordBevelJoin(joinVerb);
422 return;
423 }
424
425 // We implement miters and round joins by placing a triangle-shaped cap on top of a bevel join.
426 // (For round joins this triangle cap comprises the conic control points.) Find how tall to make
427 // this triangle cap, relative to its width.
428 //
429 // NOTE: This value would be infinite at 180 degrees, but we clamp miterCapHeightOverWidth at
430 // near-infinity. 180-degree round joins still look perfectly acceptable like this (though
431 // technically not pure arcs).
432 Sk2f cross = base * SkNx_shuffle<1,0>(n0);
433 Sk2f dot = base * n0;
434 float miterCapHeight = SkScalarAbs(dot[0] + dot[1]);
435 float miterCapWidth = SkScalarAbs(cross[0] - cross[1]) * 2;
436
437 if (Verb::kMiterJoin == joinVerb) {
438 if (miterCapHeight > fMiterMaxCapHeightOverWidth * miterCapWidth) {
439 // This join is tighter than the miter limit. Treat it as a bevel.
440 this->recordBevelJoin(Verb::kMiterJoin);
441 return;
442 }
443 this->recordMiterJoin(miterCapHeight / miterCapWidth);
444 return;
445 }
446
447 SkASSERT(Verb::kRoundJoin == joinVerb || Verb::kInternalRoundJoin == joinVerb);
448
449 // Conic arcs become unstable when they approach 180 degrees. When the conic control point
450 // begins shooting off to infinity (i.e., height/width > 32), split the conic into two.
451 static constexpr float kAlmost180Degrees = 32;
452 if (miterCapHeight > kAlmost180Degrees * miterCapWidth) {
453 Sk2f bisect = normalize(n0 - n1);
454 this->rotateTo(joinVerb, SkVector::Make(-bisect[1], bisect[0]));
455 this->recordLeftJoinIfNotEmpty(joinVerb, nextNormal);
456 return;
457 }
458
459 float miterCapHeightOverWidth = miterCapHeight / miterCapWidth;
460
461 // Find the heights of this round join's conic control point as well as the arc itself.
462 Sk2f X, Y;
463 transpose(base * base, n0 * n1, &X, &Y);
464 Sk2f r = Sk2f::Max(X + Y + Sk2f(0, 1), 0.f).sqrt();
465 Sk2f heights = SkNx_fma(r, Sk2f(miterCapHeightOverWidth, -SK_ScalarRoot2Over2), Sk2f(0, 1));
466 float controlPointHeight = SkScalarAbs(heights[0]);
467 float curveHeight = heights[1];
468 if (curveHeight * fCurrStrokeRadius < kMaxErrorFromLinearization) {
469 // Treat round joins as bevels when their curvature is nearly flat.
470 this->recordBevelJoin(joinVerb);
471 return;
472 }
473
474 float w = curveHeight / (controlPointHeight - curveHeight);
475 this->recordRoundJoin(joinVerb, miterCapHeightOverWidth, w);
476}
477
478void GrCCStrokeGeometry::recordBevelJoin(Verb originalJoinVerb) {
479 if (!IsInternalJoinVerb(originalJoinVerb)) {
480 fVerbs.push_back(Verb::kBevelJoin);
481 ++fCurrStrokeTallies->fTriangles;
482 } else {
483 fVerbs.push_back(Verb::kInternalBevelJoin);
484 fCurrStrokeTallies->fTriangles += 2;
485 }
486}
487
488void GrCCStrokeGeometry::recordMiterJoin(float miterCapHeightOverWidth) {
489 fVerbs.push_back(Verb::kMiterJoin);
490 fParams.push_back().fMiterCapHeightOverWidth = miterCapHeightOverWidth;
491 fCurrStrokeTallies->fTriangles += 2;
492}
493
494void GrCCStrokeGeometry::recordRoundJoin(Verb joinVerb, float miterCapHeightOverWidth,
495 float conicWeight) {
496 fVerbs.push_back(joinVerb);
497 fParams.push_back().fConicWeight = conicWeight;
498 fParams.push_back().fMiterCapHeightOverWidth = miterCapHeightOverWidth;
499 if (Verb::kRoundJoin == joinVerb) {
500 ++fCurrStrokeTallies->fTriangles;
501 ++fCurrStrokeTallies->fConics;
502 } else {
503 SkASSERT(Verb::kInternalRoundJoin == joinVerb);
504 fCurrStrokeTallies->fTriangles += 2;
505 fCurrStrokeTallies->fConics += 2;
506 }
507}
508
509void GrCCStrokeGeometry::closeContour() {
510 SkASSERT(fInsideContour);
511 SkASSERT(fPoints.count() > fCurrContourFirstPtIdx);
512 if (fPoints.back() != fPoints[fCurrContourFirstPtIdx]) {
513 // Draw a line back to the beginning.
514 this->lineTo(fCurrStrokeJoinVerb, fPoints[fCurrContourFirstPtIdx]);
515 }
516 if (fNormals.count() > fCurrContourFirstNormalIdx) {
517 // Join the first and last lines.
518 this->rotateTo(fCurrStrokeJoinVerb,fNormals[fCurrContourFirstNormalIdx]);
519 } else {
520 // This contour is empty. Add a bogus normal since the iterator always expects one.
521 SkASSERT(fNormals.count() == fCurrContourFirstNormalIdx);
522 fNormals.push_back({0, 0});
523 }
524 fVerbs.push_back(Verb::kEndContour);
525 SkDEBUGCODE(fInsideContour = false);
526}
527
528void GrCCStrokeGeometry::capContourAndExit() {
529 SkASSERT(fInsideContour);
530 if (fCurrContourFirstNormalIdx >= fNormals.count()) {
531 // This contour is empty. Add a normal in the direction that caps orient on empty geometry.
532 SkASSERT(fNormals.count() == fCurrContourFirstNormalIdx);
533 fNormals.push_back({1, 0});
534 }
535
536 this->recordCapsIfAny();
537 fVerbs.push_back(Verb::kEndContour);
538
539 SkDEBUGCODE(fInsideContour = false);
540}
541
542void GrCCStrokeGeometry::recordCapsIfAny() {
543 SkASSERT(fInsideContour);
544 SkASSERT(fCurrContourFirstNormalIdx < fNormals.count());
545
546 if (SkPaint::kButt_Cap == fCurrStrokeCapType) {
547 return;
548 }
549
550 Verb capVerb;
551 if (SkPaint::kSquare_Cap == fCurrStrokeCapType) {
552 if (fCurrStrokeRadius * SK_ScalarRoot2Over2 < kMaxErrorFromLinearization) {
553 return;
554 }
555 capVerb = Verb::kSquareCap;
556 fCurrStrokeTallies->fStrokes[0] += 2;
557 } else {
558 SkASSERT(SkPaint::kRound_Cap == fCurrStrokeCapType);
559 if (fCurrStrokeRadius < kMaxErrorFromLinearization) {
560 return;
561 }
562 capVerb = Verb::kRoundCap;
563 fCurrStrokeTallies->fTriangles += 2;
564 fCurrStrokeTallies->fConics += 4;
565 }
566
567 fVerbs.push_back(capVerb);
568 fVerbs.push_back(Verb::kEndContour);
569
570 fVerbs.push_back(capVerb);
571
572 // Reserve the space first, since push_back() takes the point by reference and might
573 // invalidate the reference if the array grows.
574 fPoints.reserve(fPoints.count() + 1);
575 fPoints.push_back(fPoints[fCurrContourFirstPtIdx]);
576
577 // Reserve the space first, since push_back() takes the normal by reference and might
578 // invalidate the reference if the array grows. (Although in this case we should be fine
579 // since there is a negate operator.)
580 fNormals.reserve(fNormals.count() + 1);
581 fNormals.push_back(-fNormals[fCurrContourFirstNormalIdx]);
582}