blob: e38ba3c3aed8c96a63ab83f70abb55c192e6ea79 [file] [log] [blame]
robertphillips84b00882015-05-06 05:15:57 -07001/*
2 * Copyright 2015 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 "GrAAConvexTessellator.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11#include "SkPoint.h"
12#include "SkString.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -070013#include "GrPathUtils.h"
robertphillips84b00882015-05-06 05:15:57 -070014
15// Next steps:
robertphillips84b00882015-05-06 05:15:57 -070016// add an interactive sample app slide
17// add debug check that all points are suitably far apart
18// test more degenerate cases
19
20// The tolerance for fusing vertices and eliminating colinear lines (It is in device space).
21static const SkScalar kClose = (SK_Scalar1 / 16);
22static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose);
23
fmalitabd5d7e72015-06-26 07:18:24 -070024// tesselation tolerance values, in device space pixels
25static const SkScalar kQuadTolerance = 0.2f;
26static const SkScalar kCubicTolerance = 0.2f;
27static const SkScalar kConicTolerance = 0.5f;
28
29// dot product below which we use a round cap between curve segments
30static const SkScalar kRoundCapThreshold = 0.8f;
31
robertphillips84b00882015-05-06 05:15:57 -070032static SkScalar intersect(const SkPoint& p0, const SkPoint& n0,
33 const SkPoint& p1, const SkPoint& n1) {
34 const SkPoint v = p1 - p0;
robertphillips84b00882015-05-06 05:15:57 -070035 SkScalar perpDot = n0.fX * n1.fY - n0.fY * n1.fX;
36 return (v.fX * n1.fY - v.fY * n1.fX) / perpDot;
37}
38
39// This is a special case version of intersect where we have the vector
40// perpendicular to the second line rather than the vector parallel to it.
41static SkScalar perp_intersect(const SkPoint& p0, const SkPoint& n0,
42 const SkPoint& p1, const SkPoint& perp) {
43 const SkPoint v = p1 - p0;
44 SkScalar perpDot = n0.dot(perp);
45 return v.dot(perp) / perpDot;
46}
47
48static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
49 SkScalar distSq = p0.distanceToSqd(p1);
50 return distSq < kCloseSqd;
51}
52
53static SkScalar abs_dist_from_line(const SkPoint& p0, const SkVector& v, const SkPoint& test) {
54 SkPoint testV = test - p0;
55 SkScalar dist = testV.fX * v.fY - testV.fY * v.fX;
56 return SkScalarAbs(dist);
57}
58
59int GrAAConvexTessellator::addPt(const SkPoint& pt,
60 SkScalar depth,
fmalitabd5d7e72015-06-26 07:18:24 -070061 SkScalar coverage,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070062 bool movable,
63 bool isCurve) {
robertphillips84b00882015-05-06 05:15:57 -070064 this->validate();
65
66 int index = fPts.count();
67 *fPts.push() = pt;
fmalitabd5d7e72015-06-26 07:18:24 -070068 *fCoverages.push() = coverage;
robertphillips84b00882015-05-06 05:15:57 -070069 *fMovable.push() = movable;
ethannicholas1a1b3ac2015-06-10 12:11:17 -070070 *fIsCurve.push() = isCurve;
robertphillips84b00882015-05-06 05:15:57 -070071
72 this->validate();
73 return index;
74}
75
76void GrAAConvexTessellator::popLastPt() {
77 this->validate();
78
79 fPts.pop();
fmalitabd5d7e72015-06-26 07:18:24 -070080 fCoverages.pop();
robertphillips84b00882015-05-06 05:15:57 -070081 fMovable.pop();
82
83 this->validate();
84}
85
86void GrAAConvexTessellator::popFirstPtShuffle() {
87 this->validate();
88
89 fPts.removeShuffle(0);
fmalitabd5d7e72015-06-26 07:18:24 -070090 fCoverages.removeShuffle(0);
robertphillips84b00882015-05-06 05:15:57 -070091 fMovable.removeShuffle(0);
92
93 this->validate();
94}
95
96void GrAAConvexTessellator::updatePt(int index,
97 const SkPoint& pt,
fmalitabd5d7e72015-06-26 07:18:24 -070098 SkScalar depth,
99 SkScalar coverage) {
robertphillips84b00882015-05-06 05:15:57 -0700100 this->validate();
101 SkASSERT(fMovable[index]);
102
103 fPts[index] = pt;
fmalitabd5d7e72015-06-26 07:18:24 -0700104 fCoverages[index] = coverage;
robertphillips84b00882015-05-06 05:15:57 -0700105}
106
107void GrAAConvexTessellator::addTri(int i0, int i1, int i2) {
108 if (i0 == i1 || i1 == i2 || i2 == i0) {
109 return;
110 }
111
112 *fIndices.push() = i0;
113 *fIndices.push() = i1;
114 *fIndices.push() = i2;
115}
116
117void GrAAConvexTessellator::rewind() {
118 fPts.rewind();
fmalitabd5d7e72015-06-26 07:18:24 -0700119 fCoverages.rewind();
robertphillips84b00882015-05-06 05:15:57 -0700120 fMovable.rewind();
121 fIndices.rewind();
122 fNorms.rewind();
123 fInitialRing.rewind();
124 fCandidateVerts.rewind();
125#if GR_AA_CONVEX_TESSELLATOR_VIZ
126 fRings.rewind(); // TODO: leak in this case!
127#else
128 fRings[0].rewind();
129 fRings[1].rewind();
130#endif
131}
132
133void GrAAConvexTessellator::computeBisectors() {
134 fBisectors.setCount(fNorms.count());
135
136 int prev = fBisectors.count() - 1;
137 for (int cur = 0; cur < fBisectors.count(); prev = cur, ++cur) {
138 fBisectors[cur] = fNorms[cur] + fNorms[prev];
robertphillips364ad002015-05-20 11:49:55 -0700139 if (!fBisectors[cur].normalize()) {
140 SkASSERT(SkPoint::kLeft_Side == fSide || SkPoint::kRight_Side == fSide);
141 fBisectors[cur].setOrthog(fNorms[cur], (SkPoint::Side)-fSide);
142 SkVector other;
143 other.setOrthog(fNorms[prev], fSide);
144 fBisectors[cur] += other;
145 SkAssertResult(fBisectors[cur].normalize());
146 } else {
147 fBisectors[cur].negate(); // make the bisector face in
148 }
robertphillips84b00882015-05-06 05:15:57 -0700149
150 SkASSERT(SkScalarNearlyEqual(1.0f, fBisectors[cur].length()));
151 }
152}
153
fmalitabd5d7e72015-06-26 07:18:24 -0700154// Create as many rings as we need to (up to a predefined limit) to reach the specified target
155// depth. If we are in fill mode, the final ring will automatically be fanned.
156bool GrAAConvexTessellator::createInsetRings(Ring& previousRing, SkScalar initialDepth,
157 SkScalar initialCoverage, SkScalar targetDepth,
158 SkScalar targetCoverage, Ring** finalRing) {
159 static const int kMaxNumRings = 8;
160
161 if (previousRing.numPts() < 3) {
162 return false;
163 }
164 Ring* currentRing = &previousRing;
165 int i;
166 for (i = 0; i < kMaxNumRings; ++i) {
167 Ring* nextRing = this->getNextRing(currentRing);
168 SkASSERT(nextRing != currentRing);
169
170 bool done = this->createInsetRing(*currentRing, nextRing, initialDepth, initialCoverage,
171 targetDepth, targetCoverage, i == 0);
172 currentRing = nextRing;
173 if (done) {
174 break;
175 }
176 currentRing->init(*this);
177 }
178
179 if (kMaxNumRings == i) {
180 // Bail if we've exceeded the amount of time we want to throw at this.
181 this->terminate(*currentRing);
182 return false;
183 }
184 bool done = currentRing->numPts() >= 3;
185 if (done) {
186 currentRing->init(*this);
187 }
188 *finalRing = currentRing;
189 return done;
190}
191
robertphillips84b00882015-05-06 05:15:57 -0700192// The general idea here is to, conceptually, start with the original polygon and slide
193// the vertices along the bisectors until the first intersection. At that
194// point two of the edges collapse and the process repeats on the new polygon.
195// The polygon state is captured in the Ring class while the GrAAConvexTessellator
196// controls the iteration. The CandidateVerts holds the formative points for the
197// next ring.
198bool GrAAConvexTessellator::tessellate(const SkMatrix& m, const SkPath& path) {
robertphillips84b00882015-05-06 05:15:57 -0700199 if (!this->extractFromPath(m, path)) {
200 return false;
201 }
202
fmalitabd5d7e72015-06-26 07:18:24 -0700203 SkScalar coverage = 1.0f;
204 if (fStrokeWidth >= 0.0f) {
205 Ring outerStrokeRing;
206 this->createOuterRing(fInitialRing, fStrokeWidth / 2 - kAntialiasingRadius, coverage,
207 &outerStrokeRing);
208 outerStrokeRing.init(*this);
209 Ring outerAARing;
210 this->createOuterRing(outerStrokeRing, kAntialiasingRadius * 2, 0.0f, &outerAARing);
211 } else {
212 Ring outerAARing;
213 this->createOuterRing(fInitialRing, kAntialiasingRadius, 0.0f, &outerAARing);
214 }
robertphillips84b00882015-05-06 05:15:57 -0700215
216 // the bisectors are only needed for the computation of the outer ring
217 fBisectors.rewind();
fmalitabd5d7e72015-06-26 07:18:24 -0700218 if (fStrokeWidth >= 0.0f && fInitialRing.numPts() > 2) {
219 Ring* insetStrokeRing;
220 SkScalar strokeDepth = fStrokeWidth / 2 - kAntialiasingRadius;
221 if (this->createInsetRings(fInitialRing, 0.0f, coverage, strokeDepth, coverage,
222 &insetStrokeRing)) {
223 Ring* insetAARing;
224 this->createInsetRings(*insetStrokeRing, strokeDepth, coverage, strokeDepth +
225 kAntialiasingRadius * 2, 0.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700226 }
fmalitabd5d7e72015-06-26 07:18:24 -0700227 } else {
228 Ring* insetAARing;
229 this->createInsetRings(fInitialRing, 0.0f, 0.5f, kAntialiasingRadius, 1.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700230 }
231
fmalitabd5d7e72015-06-26 07:18:24 -0700232 SkDEBUGCODE(this->validate();)
robertphillips84b00882015-05-06 05:15:57 -0700233 return true;
234}
235
236SkScalar GrAAConvexTessellator::computeDepthFromEdge(int edgeIdx, const SkPoint& p) const {
237 SkASSERT(edgeIdx < fNorms.count());
238
239 SkPoint v = p - fPts[edgeIdx];
240 SkScalar depth = -fNorms[edgeIdx].dot(v);
robertphillips84b00882015-05-06 05:15:57 -0700241 return depth;
242}
243
244// Find a point that is 'desiredDepth' away from the 'edgeIdx'-th edge and lies
245// along the 'bisector' from the 'startIdx'-th point.
246bool GrAAConvexTessellator::computePtAlongBisector(int startIdx,
247 const SkVector& bisector,
248 int edgeIdx,
249 SkScalar desiredDepth,
250 SkPoint* result) const {
251 const SkPoint& norm = fNorms[edgeIdx];
252
253 // First find the point where the edge and the bisector intersect
254 SkPoint newP;
fmalitabd5d7e72015-06-26 07:18:24 -0700255
robertphillips84b00882015-05-06 05:15:57 -0700256 SkScalar t = perp_intersect(fPts[startIdx], bisector, fPts[edgeIdx], norm);
257 if (SkScalarNearlyEqual(t, 0.0f)) {
258 // the start point was one of the original ring points
fmalitabd5d7e72015-06-26 07:18:24 -0700259 SkASSERT(startIdx < fPts.count());
robertphillips84b00882015-05-06 05:15:57 -0700260 newP = fPts[startIdx];
fmalitabd5d7e72015-06-26 07:18:24 -0700261 } else if (t < 0.0f) {
robertphillips84b00882015-05-06 05:15:57 -0700262 newP = bisector;
263 newP.scale(t);
264 newP += fPts[startIdx];
265 } else {
266 return false;
267 }
268
269 // Then offset along the bisector from that point the correct distance
fmalitabd5d7e72015-06-26 07:18:24 -0700270 SkScalar dot = bisector.dot(norm);
271 t = -desiredDepth / dot;
robertphillips84b00882015-05-06 05:15:57 -0700272 *result = bisector;
273 result->scale(t);
274 *result += newP;
275
robertphillips84b00882015-05-06 05:15:57 -0700276 return true;
277}
278
279bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& path) {
robertphillips84b00882015-05-06 05:15:57 -0700280 SkASSERT(SkPath::kConvex_Convexity == path.getConvexity());
281
282 // Outer ring: 3*numPts
283 // Middle ring: numPts
284 // Presumptive inner ring: numPts
285 this->reservePts(5*path.countPoints());
286 // Outer ring: 12*numPts
287 // Middle ring: 0
288 // Presumptive inner ring: 6*numPts + 6
289 fIndices.setReserve(18*path.countPoints() + 6);
290
291 fNorms.setReserve(path.countPoints());
292
robertphillips84b00882015-05-06 05:15:57 -0700293 // TODO: is there a faster way to extract the points from the path? Perhaps
294 // get all the points via a new entry point, transform them all in bulk
295 // and then walk them to find duplicates?
296 SkPath::Iter iter(path, true);
297 SkPoint pts[4];
298 SkPath::Verb verb;
299 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
300 switch (verb) {
301 case SkPath::kLine_Verb:
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700302 this->lineTo(m, pts[1], false);
robertphillips84b00882015-05-06 05:15:57 -0700303 break;
304 case SkPath::kQuad_Verb:
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700305 this->quadTo(m, pts);
306 break;
robertphillips84b00882015-05-06 05:15:57 -0700307 case SkPath::kCubic_Verb:
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700308 this->cubicTo(m, pts);
309 break;
310 case SkPath::kConic_Verb:
311 this->conicTo(m, pts, iter.conicWeight());
robertphillips84b00882015-05-06 05:15:57 -0700312 break;
313 case SkPath::kMove_Verb:
314 case SkPath::kClose_Verb:
315 case SkPath::kDone_Verb:
316 break;
317 }
318 }
319
fmalitabd5d7e72015-06-26 07:18:24 -0700320 if (this->numPts() < 2) {
robertphillips84b00882015-05-06 05:15:57 -0700321 return false;
322 }
323
324 // check if last point is a duplicate of the first point. If so, remove it.
325 if (duplicate_pt(fPts[this->numPts()-1], fPts[0])) {
326 this->popLastPt();
327 fNorms.pop();
328 }
329
330 SkASSERT(fPts.count() == fNorms.count()+1);
fmalitabd5d7e72015-06-26 07:18:24 -0700331 if (this->numPts() >= 3) {
332 if (abs_dist_from_line(fPts.top(), fNorms.top(), fPts[0]) < kClose) {
333 // The last point is on the line from the second to last to the first point.
334 this->popLastPt();
335 fNorms.pop();
336 }
337
338 *fNorms.push() = fPts[0] - fPts.top();
339 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top());
340 SkASSERT(len > 0.0f);
341 SkASSERT(fPts.count() == fNorms.count());
robertphillips84b00882015-05-06 05:15:57 -0700342 }
343
fmalitabd5d7e72015-06-26 07:18:24 -0700344 if (this->numPts() >= 3 && abs_dist_from_line(fPts[0], fNorms.top(), fPts[1]) < kClose) {
robertphillips84b00882015-05-06 05:15:57 -0700345 // The first point is on the line from the last to the second.
346 this->popFirstPtShuffle();
347 fNorms.removeShuffle(0);
348 fNorms[0] = fPts[1] - fPts[0];
349 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms[0]);
350 SkASSERT(len > 0.0f);
351 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[0].length()));
352 }
353
fmalitabd5d7e72015-06-26 07:18:24 -0700354 if (this->numPts() >= 3) {
355 // Check the cross product of the final trio
356 SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.top());
357 if (cross > 0.0f) {
358 fSide = SkPoint::kRight_Side;
359 } else {
360 fSide = SkPoint::kLeft_Side;
361 }
362
363 // Make all the normals face outwards rather than along the edge
364 for (int cur = 0; cur < fNorms.count(); ++cur) {
365 fNorms[cur].setOrthog(fNorms[cur], fSide);
366 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length()));
367 }
368
369 this->computeBisectors();
370 } else if (this->numPts() == 2) {
371 // We've got two points, so we're degenerate.
372 if (fStrokeWidth < 0.0f) {
373 // it's a fill, so we don't need to worry about degenerate paths
374 return false;
375 }
376 // For stroking, we still need to process the degenerate path, so fix it up
377 fSide = SkPoint::kLeft_Side;
378
379 // Make all the normals face outwards rather than along the edge
380 for (int cur = 0; cur < fNorms.count(); ++cur) {
381 fNorms[cur].setOrthog(fNorms[cur], fSide);
382 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length()));
383 }
384
385 fNorms.push(SkPoint::Make(-fNorms[0].fX, -fNorms[0].fY));
386 // we won't actually use the bisectors, so just push zeroes
387 fBisectors.push(SkPoint::Make(0.0, 0.0));
388 fBisectors.push(SkPoint::Make(0.0, 0.0));
389 } else {
robertphillips84b00882015-05-06 05:15:57 -0700390 return false;
391 }
392
robertphillips84b00882015-05-06 05:15:57 -0700393 fCandidateVerts.setReserve(this->numPts());
394 fInitialRing.setReserve(this->numPts());
395 for (int i = 0; i < this->numPts(); ++i) {
396 fInitialRing.addIdx(i, i);
397 }
398 fInitialRing.init(fNorms, fBisectors);
399
400 this->validate();
401 return true;
402}
403
404GrAAConvexTessellator::Ring* GrAAConvexTessellator::getNextRing(Ring* lastRing) {
405#if GR_AA_CONVEX_TESSELLATOR_VIZ
406 Ring* ring = *fRings.push() = SkNEW(Ring);
407 ring->setReserve(fInitialRing.numPts());
408 ring->rewind();
409 return ring;
410#else
411 // Flip flop back and forth between fRings[0] & fRings[1]
412 int nextRing = (lastRing == &fRings[0]) ? 1 : 0;
413 fRings[nextRing].setReserve(fInitialRing.numPts());
414 fRings[nextRing].rewind();
415 return &fRings[nextRing];
416#endif
417}
418
419void GrAAConvexTessellator::fanRing(const Ring& ring) {
420 // fan out from point 0
fmalitabd5d7e72015-06-26 07:18:24 -0700421 int startIdx = ring.index(0);
422 for (int cur = ring.numPts() - 2; cur >= 0; --cur) {
423 this->addTri(startIdx, ring.index(cur), ring.index(cur + 1));
robertphillips84b00882015-05-06 05:15:57 -0700424 }
425}
426
fmalitabd5d7e72015-06-26 07:18:24 -0700427void GrAAConvexTessellator::createOuterRing(const Ring& previousRing, SkScalar outset,
428 SkScalar coverage, Ring* nextRing) {
429 const int numPts = previousRing.numPts();
430 if (numPts == 0) {
431 return;
432 }
robertphillips84b00882015-05-06 05:15:57 -0700433
robertphillips84b00882015-05-06 05:15:57 -0700434 int prev = numPts - 1;
fmalitabd5d7e72015-06-26 07:18:24 -0700435 int lastPerpIdx = -1, firstPerpIdx = -1;
436
437 const SkScalar outsetSq = SkScalarMul(outset, outset);
438 SkScalar miterLimitSq = SkScalarMul(outset, fMiterLimit);
439 miterLimitSq = SkScalarMul(miterLimitSq, miterLimitSq);
robertphillips84b00882015-05-06 05:15:57 -0700440 for (int cur = 0; cur < numPts; ++cur) {
fmalitabd5d7e72015-06-26 07:18:24 -0700441 int originalIdx = previousRing.index(cur);
442 // For each vertex of the original polygon we add at least two points to the
443 // outset polygon - one extending perpendicular to each impinging edge. Connecting these
444 // two points yields a bevel join. We need one additional point for a mitered join, and
445 // a round join requires one or more points depending upon curvature.
robertphillips84b00882015-05-06 05:15:57 -0700446
fmalitabd5d7e72015-06-26 07:18:24 -0700447 // The perpendicular point for the last edge
448 SkPoint normal1 = previousRing.norm(prev);
449 SkPoint perp1 = normal1;
450 perp1.scale(outset);
451 perp1 += this->point(originalIdx);
robertphillips84b00882015-05-06 05:15:57 -0700452
fmalitabd5d7e72015-06-26 07:18:24 -0700453 // The perpendicular point for the next edge.
454 SkPoint normal2 = previousRing.norm(cur);
455 SkPoint perp2 = normal2;
456 perp2.scale(outset);
457 perp2 += fPts[originalIdx];
robertphillips84b00882015-05-06 05:15:57 -0700458
fmalitabd5d7e72015-06-26 07:18:24 -0700459 bool isCurve = fIsCurve[originalIdx];
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700460
fmalitabd5d7e72015-06-26 07:18:24 -0700461 // We know it isn't a duplicate of the prior point (since it and this
462 // one are just perpendicular offsets from the non-merged polygon points)
463 int perp1Idx = this->addPt(perp1, -outset, coverage, false, isCurve);
464 nextRing->addIdx(perp1Idx, originalIdx);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700465
fmalitabd5d7e72015-06-26 07:18:24 -0700466 int perp2Idx;
467 // For very shallow angles all the corner points could fuse.
468 if (duplicate_pt(perp2, this->point(perp1Idx))) {
469 perp2Idx = perp1Idx;
470 } else {
471 perp2Idx = this->addPt(perp2, -outset, coverage, false, isCurve);
robertphillips84b00882015-05-06 05:15:57 -0700472 }
ethannicholas2436f192015-06-25 14:42:34 -0700473
fmalitabd5d7e72015-06-26 07:18:24 -0700474 if (perp2Idx != perp1Idx) {
475 if (isCurve) {
476 // bevel or round depending upon curvature
477 SkScalar dotProd = normal1.dot(normal2);
478 if (dotProd < kRoundCapThreshold) {
479 // Currently we "round" by creating a single extra point, which produces
480 // good results for common cases. For thick strokes with high curvature, we will
481 // need to add more points; for the time being we simply fall back to software
482 // rendering for thick strokes.
483 SkPoint miter = previousRing.bisector(cur);
484 miter.setLength(-outset);
485 miter += fPts[originalIdx];
reed9730f4a2015-06-26 05:06:43 -0700486
fmalitabd5d7e72015-06-26 07:18:24 -0700487 // For very shallow angles all the corner points could fuse
488 if (!duplicate_pt(miter, this->point(perp1Idx))) {
489 int miterIdx;
490 miterIdx = this->addPt(miter, -outset, coverage, false, false);
491 nextRing->addIdx(miterIdx, originalIdx);
492 // The two triangles for the corner
493 this->addTri(originalIdx, perp1Idx, miterIdx);
494 this->addTri(originalIdx, miterIdx, perp2Idx);
495 }
496 } else {
497 this->addTri(originalIdx, perp1Idx, perp2Idx);
498 }
reed9730f4a2015-06-26 05:06:43 -0700499 } else {
fmalitabd5d7e72015-06-26 07:18:24 -0700500 switch (fJoin) {
501 case SkPaint::Join::kMiter_Join: {
502 // The bisector outset point
503 SkPoint miter = previousRing.bisector(cur);
504 SkScalar dotProd = normal1.dot(normal2);
505 SkScalar sinHalfAngleSq = SkScalarHalf(SK_Scalar1 + dotProd);
506 SkScalar lengthSq = outsetSq / sinHalfAngleSq;
507 if (lengthSq > miterLimitSq) {
508 // just bevel it
509 this->addTri(originalIdx, perp1Idx, perp2Idx);
510 break;
511 }
512 miter.setLength(-SkScalarSqrt(lengthSq));
513 miter += fPts[originalIdx];
514
515 // For very shallow angles all the corner points could fuse
516 if (!duplicate_pt(miter, this->point(perp1Idx))) {
517 int miterIdx;
518 miterIdx = this->addPt(miter, -outset, coverage, false, false);
519 nextRing->addIdx(miterIdx, originalIdx);
520 // The two triangles for the corner
521 this->addTri(originalIdx, perp1Idx, miterIdx);
522 this->addTri(originalIdx, miterIdx, perp2Idx);
523 }
524 break;
525 }
526 case SkPaint::Join::kBevel_Join:
527 this->addTri(originalIdx, perp1Idx, perp2Idx);
528 break;
529 default:
530 // kRound_Join is unsupported for now. GrAALinearizingConvexPathRenderer is
531 // only willing to draw mitered or beveled, so we should never get here.
532 SkASSERT(false);
533 }
reed9730f4a2015-06-26 05:06:43 -0700534 }
535
fmalitabd5d7e72015-06-26 07:18:24 -0700536 nextRing->addIdx(perp2Idx, originalIdx);
ethannicholas2436f192015-06-25 14:42:34 -0700537 }
fmalitabd5d7e72015-06-26 07:18:24 -0700538
539 if (0 == cur) {
540 // Store the index of the first perpendicular point to finish up
541 firstPerpIdx = perp1Idx;
542 SkASSERT(-1 == lastPerpIdx);
543 } else {
544 // The triangles for the previous edge
545 int prevIdx = previousRing.index(prev);
546 this->addTri(prevIdx, perp1Idx, originalIdx);
547 this->addTri(prevIdx, lastPerpIdx, perp1Idx);
548 }
549
550 // Track the last perpendicular outset point so we can construct the
551 // trailing edge triangles.
552 lastPerpIdx = perp2Idx;
553 prev = cur;
robertphillips84b00882015-05-06 05:15:57 -0700554 }
555
556 // pick up the final edge rect
fmalitabd5d7e72015-06-26 07:18:24 -0700557 int lastIdx = previousRing.index(numPts - 1);
558 this->addTri(lastIdx, firstPerpIdx, previousRing.index(0));
559 this->addTri(lastIdx, lastPerpIdx, firstPerpIdx);
robertphillips84b00882015-05-06 05:15:57 -0700560
561 this->validate();
562}
563
fmalitabd5d7e72015-06-26 07:18:24 -0700564// Something went wrong in the creation of the next ring. If we're filling the shape, just go ahead
565// and fan it.
robertphillips84b00882015-05-06 05:15:57 -0700566void GrAAConvexTessellator::terminate(const Ring& ring) {
fmalitabd5d7e72015-06-26 07:18:24 -0700567 if (fStrokeWidth < 0.0f) {
568 this->fanRing(ring);
robertphillips84b00882015-05-06 05:15:57 -0700569 }
fmalitabd5d7e72015-06-26 07:18:24 -0700570}
robertphillips84b00882015-05-06 05:15:57 -0700571
fmalitabd5d7e72015-06-26 07:18:24 -0700572static SkScalar compute_coverage(SkScalar depth, SkScalar initialDepth, SkScalar initialCoverage,
573 SkScalar targetDepth, SkScalar targetCoverage) {
574 if (SkScalarNearlyEqual(initialDepth, targetDepth)) {
575 return targetCoverage;
576 }
577 SkScalar result = (depth - initialDepth) / (targetDepth - initialDepth) *
578 (targetCoverage - initialCoverage) + initialCoverage;
579 return SkScalarClampMax(result, 1.0f);
robertphillips84b00882015-05-06 05:15:57 -0700580}
581
582// return true when processing is complete
fmalitabd5d7e72015-06-26 07:18:24 -0700583bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing,
584 SkScalar initialDepth, SkScalar initialCoverage,
585 SkScalar targetDepth, SkScalar targetCoverage,
586 bool forceNew) {
robertphillips84b00882015-05-06 05:15:57 -0700587 bool done = false;
588
589 fCandidateVerts.rewind();
590
591 // Loop through all the points in the ring and find the intersection with the smallest depth
592 SkScalar minDist = SK_ScalarMax, minT = 0.0f;
593 int minEdgeIdx = -1;
594
595 for (int cur = 0; cur < lastRing.numPts(); ++cur) {
596 int next = (cur + 1) % lastRing.numPts();
robertphillips84b00882015-05-06 05:15:57 -0700597 SkScalar t = intersect(this->point(lastRing.index(cur)), lastRing.bisector(cur),
598 this->point(lastRing.index(next)), lastRing.bisector(next));
599 SkScalar dist = -t * lastRing.norm(cur).dot(lastRing.bisector(cur));
600
601 if (minDist > dist) {
602 minDist = dist;
603 minT = t;
604 minEdgeIdx = cur;
605 }
606 }
607
fmalitabd5d7e72015-06-26 07:18:24 -0700608 if (minEdgeIdx == -1) {
609 return false;
610 }
robertphillips84b00882015-05-06 05:15:57 -0700611 SkPoint newPt = lastRing.bisector(minEdgeIdx);
612 newPt.scale(minT);
613 newPt += this->point(lastRing.index(minEdgeIdx));
614
615 SkScalar depth = this->computeDepthFromEdge(lastRing.origEdgeID(minEdgeIdx), newPt);
fmalitabd5d7e72015-06-26 07:18:24 -0700616 if (depth >= targetDepth) {
robertphillips84b00882015-05-06 05:15:57 -0700617 // None of the bisectors intersect before reaching the desired depth.
618 // Just step them all to the desired depth
fmalitabd5d7e72015-06-26 07:18:24 -0700619 depth = targetDepth;
robertphillips84b00882015-05-06 05:15:57 -0700620 done = true;
621 }
622
623 // 'dst' stores where each point in the last ring maps to/transforms into
624 // in the next ring.
625 SkTDArray<int> dst;
626 dst.setCount(lastRing.numPts());
627
628 // Create the first point (who compares with no one)
629 if (!this->computePtAlongBisector(lastRing.index(0),
630 lastRing.bisector(0),
631 lastRing.origEdgeID(0),
632 depth, &newPt)) {
633 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700634 return true;
635 }
636 dst[0] = fCandidateVerts.addNewPt(newPt,
637 lastRing.index(0), lastRing.origEdgeID(0),
638 !this->movable(lastRing.index(0)));
639
640 // Handle the middle points (who only compare with the prior point)
641 for (int cur = 1; cur < lastRing.numPts()-1; ++cur) {
642 if (!this->computePtAlongBisector(lastRing.index(cur),
643 lastRing.bisector(cur),
644 lastRing.origEdgeID(cur),
645 depth, &newPt)) {
646 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700647 return true;
648 }
649 if (!duplicate_pt(newPt, fCandidateVerts.lastPoint())) {
650 dst[cur] = fCandidateVerts.addNewPt(newPt,
651 lastRing.index(cur), lastRing.origEdgeID(cur),
652 !this->movable(lastRing.index(cur)));
653 } else {
654 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
655 }
656 }
657
658 // Check on the last point (handling the wrap around)
659 int cur = lastRing.numPts()-1;
660 if (!this->computePtAlongBisector(lastRing.index(cur),
661 lastRing.bisector(cur),
662 lastRing.origEdgeID(cur),
663 depth, &newPt)) {
664 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700665 return true;
666 }
667 bool dupPrev = duplicate_pt(newPt, fCandidateVerts.lastPoint());
668 bool dupNext = duplicate_pt(newPt, fCandidateVerts.firstPoint());
669
670 if (!dupPrev && !dupNext) {
671 dst[cur] = fCandidateVerts.addNewPt(newPt,
672 lastRing.index(cur), lastRing.origEdgeID(cur),
673 !this->movable(lastRing.index(cur)));
674 } else if (dupPrev && !dupNext) {
675 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
676 } else if (!dupPrev && dupNext) {
677 dst[cur] = fCandidateVerts.fuseWithNext();
678 } else {
679 bool dupPrevVsNext = duplicate_pt(fCandidateVerts.firstPoint(), fCandidateVerts.lastPoint());
680
681 if (!dupPrevVsNext) {
682 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
683 } else {
ethannicholas0dacc672015-07-07 12:41:52 -0700684 const int fused = fCandidateVerts.fuseWithBoth();
685 dst[cur] = fused;
686 const int targetIdx = dst[cur - 1];
687 for (int i = cur - 1; i >= 0 && dst[i] == targetIdx; i--) {
688 dst[i] = fused;
689 }
robertphillips84b00882015-05-06 05:15:57 -0700690 }
691 }
692
693 // Fold the new ring's points into the global pool
694 for (int i = 0; i < fCandidateVerts.numPts(); ++i) {
695 int newIdx;
fmalitabd5d7e72015-06-26 07:18:24 -0700696 if (fCandidateVerts.needsToBeNew(i) || forceNew) {
robertphillips84b00882015-05-06 05:15:57 -0700697 // if the originating index is still valid then this point wasn't
698 // fused (and is thus movable)
fmalitabd5d7e72015-06-26 07:18:24 -0700699 SkScalar coverage = compute_coverage(depth, initialDepth, initialCoverage,
700 targetDepth, targetCoverage);
701 newIdx = this->addPt(fCandidateVerts.point(i), depth, coverage,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700702 fCandidateVerts.originatingIdx(i) != -1, false);
robertphillips84b00882015-05-06 05:15:57 -0700703 } else {
704 SkASSERT(fCandidateVerts.originatingIdx(i) != -1);
fmalitabd5d7e72015-06-26 07:18:24 -0700705 this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth,
706 targetCoverage);
robertphillips84b00882015-05-06 05:15:57 -0700707 newIdx = fCandidateVerts.originatingIdx(i);
708 }
709
710 nextRing->addIdx(newIdx, fCandidateVerts.origEdge(i));
711 }
712
713 // 'dst' currently has indices into the ring. Remap these to be indices
714 // into the global pool since the triangulation operates in that space.
715 for (int i = 0; i < dst.count(); ++i) {
716 dst[i] = nextRing->index(dst[i]);
717 }
718
719 for (int cur = 0; cur < lastRing.numPts(); ++cur) {
720 int next = (cur + 1) % lastRing.numPts();
721
722 this->addTri(lastRing.index(cur), lastRing.index(next), dst[next]);
723 this->addTri(lastRing.index(cur), dst[next], dst[cur]);
724 }
725
fmalitabd5d7e72015-06-26 07:18:24 -0700726 if (done && fStrokeWidth < 0.0f) {
727 // fill
robertphillips84b00882015-05-06 05:15:57 -0700728 this->fanRing(*nextRing);
729 }
730
731 if (nextRing->numPts() < 3) {
732 done = true;
733 }
robertphillips84b00882015-05-06 05:15:57 -0700734 return done;
735}
736
737void GrAAConvexTessellator::validate() const {
robertphillips84b00882015-05-06 05:15:57 -0700738 SkASSERT(fPts.count() == fMovable.count());
739 SkASSERT(0 == (fIndices.count() % 3));
740}
741
742//////////////////////////////////////////////////////////////////////////////
743void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) {
744 this->computeNormals(tess);
robertphillips364ad002015-05-20 11:49:55 -0700745 this->computeBisectors(tess);
robertphillips84b00882015-05-06 05:15:57 -0700746}
747
748void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms,
749 const SkTDArray<SkVector>& bisectors) {
750 for (int i = 0; i < fPts.count(); ++i) {
751 fPts[i].fNorm = norms[i];
752 fPts[i].fBisector = bisectors[i];
753 }
754}
755
756// Compute the outward facing normal at each vertex.
757void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& tess) {
758 for (int cur = 0; cur < fPts.count(); ++cur) {
759 int next = (cur + 1) % fPts.count();
760
761 fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex);
fmalitabd5d7e72015-06-26 07:18:24 -0700762 SkPoint::Normalize(&fPts[cur].fNorm);
robertphillips84b00882015-05-06 05:15:57 -0700763 fPts[cur].fNorm.setOrthog(fPts[cur].fNorm, tess.side());
robertphillips84b00882015-05-06 05:15:57 -0700764 }
765}
766
robertphillips364ad002015-05-20 11:49:55 -0700767void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& tess) {
robertphillips84b00882015-05-06 05:15:57 -0700768 int prev = fPts.count() - 1;
769 for (int cur = 0; cur < fPts.count(); prev = cur, ++cur) {
770 fPts[cur].fBisector = fPts[cur].fNorm + fPts[prev].fNorm;
robertphillips364ad002015-05-20 11:49:55 -0700771 if (!fPts[cur].fBisector.normalize()) {
772 SkASSERT(SkPoint::kLeft_Side == tess.side() || SkPoint::kRight_Side == tess.side());
773 fPts[cur].fBisector.setOrthog(fPts[cur].fNorm, (SkPoint::Side)-tess.side());
774 SkVector other;
775 other.setOrthog(fPts[prev].fNorm, tess.side());
776 fPts[cur].fBisector += other;
777 SkAssertResult(fPts[cur].fBisector.normalize());
778 } else {
779 fPts[cur].fBisector.negate(); // make the bisector face in
780 }
fmalitabd5d7e72015-06-26 07:18:24 -0700781 }
robertphillips84b00882015-05-06 05:15:57 -0700782}
783
784//////////////////////////////////////////////////////////////////////////////
785#ifdef SK_DEBUG
786// Is this ring convex?
787bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const {
788 if (fPts.count() < 3) {
fmalitabd5d7e72015-06-26 07:18:24 -0700789 return true;
robertphillips84b00882015-05-06 05:15:57 -0700790 }
791
792 SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.top().fIndex);
793 SkPoint cur = tess.point(fPts[1].fIndex) - tess.point(fPts[0].fIndex);
794 SkScalar minDot = prev.fX * cur.fY - prev.fY * cur.fX;
795 SkScalar maxDot = minDot;
796
797 prev = cur;
798 for (int i = 1; i < fPts.count(); ++i) {
799 int next = (i + 1) % fPts.count();
800
801 cur = tess.point(fPts[next].fIndex) - tess.point(fPts[i].fIndex);
802 SkScalar dot = prev.fX * cur.fY - prev.fY * cur.fX;
803
804 minDot = SkMinScalar(minDot, dot);
805 maxDot = SkMaxScalar(maxDot, dot);
806
807 prev = cur;
808 }
809
fmalitabd5d7e72015-06-26 07:18:24 -0700810 if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) {
811 maxDot = 0;
812 }
813 if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) {
814 minDot = 0;
815 }
816 return (maxDot >= 0.0f) == (minDot >= 0.0f);
robertphillips84b00882015-05-06 05:15:57 -0700817}
818
robertphillips84b00882015-05-06 05:15:57 -0700819#endif
820
fmalitabd5d7e72015-06-26 07:18:24 -0700821void GrAAConvexTessellator::lineTo(SkPoint p, bool isCurve) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700822 if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) {
823 return;
824 }
825
826 SkASSERT(fPts.count() <= 1 || fPts.count() == fNorms.count()+1);
827 if (this->numPts() >= 2 &&
828 abs_dist_from_line(fPts.top(), fNorms.top(), p) < kClose) {
829 // The old last point is on the line from the second to last to the new point
830 this->popLastPt();
831 fNorms.pop();
832 fIsCurve.pop();
833 }
fmalitabd5d7e72015-06-26 07:18:24 -0700834 SkScalar initialRingCoverage = fStrokeWidth < 0.0f ? 0.5f : 1.0f;
835 this->addPt(p, 0.0f, initialRingCoverage, false, isCurve);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700836 if (this->numPts() > 1) {
837 *fNorms.push() = fPts.top() - fPts[fPts.count()-2];
838 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top());
839 SkASSERT(len > 0.0f);
840 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms.top().length()));
841 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700842}
843
fmalitabd5d7e72015-06-26 07:18:24 -0700844void GrAAConvexTessellator::lineTo(const SkMatrix& m, SkPoint p, bool isCurve) {
845 m.mapPoints(&p, 1);
846 this->lineTo(p, isCurve);
847}
848
849void GrAAConvexTessellator::quadTo(SkPoint pts[3]) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700850 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
851 fPointBuffer.setReserve(maxCount);
852 SkPoint* target = fPointBuffer.begin();
853 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
854 kQuadTolerance, &target, maxCount);
855 fPointBuffer.setCount(count);
856 for (int i = 0; i < count; i++) {
fmalitabd5d7e72015-06-26 07:18:24 -0700857 lineTo(fPointBuffer[i], true);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700858 }
859}
860
fmalitabd5d7e72015-06-26 07:18:24 -0700861void GrAAConvexTessellator::quadTo(const SkMatrix& m, SkPoint pts[3]) {
862 SkPoint transformed[3];
863 transformed[0] = pts[0];
864 transformed[1] = pts[1];
865 transformed[2] = pts[2];
866 m.mapPoints(transformed, 3);
867 quadTo(transformed);
868}
869
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700870void GrAAConvexTessellator::cubicTo(const SkMatrix& m, SkPoint pts[4]) {
fmalitabd5d7e72015-06-26 07:18:24 -0700871 m.mapPoints(pts, 4);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700872 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
873 fPointBuffer.setReserve(maxCount);
874 SkPoint* target = fPointBuffer.begin();
875 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
876 kCubicTolerance, &target, maxCount);
877 fPointBuffer.setCount(count);
878 for (int i = 0; i < count; i++) {
fmalitabd5d7e72015-06-26 07:18:24 -0700879 lineTo(fPointBuffer[i], true);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700880 }
881}
882
883// include down here to avoid compilation errors caused by "-" overload in SkGeometry.h
884#include "SkGeometry.h"
885
fmalitabd5d7e72015-06-26 07:18:24 -0700886void GrAAConvexTessellator::conicTo(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
887 m.mapPoints(pts, 3);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700888 SkAutoConicToQuads quadder;
889 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
890 SkPoint lastPoint = *(quads++);
891 int count = quadder.countQuads();
892 for (int i = 0; i < count; ++i) {
893 SkPoint quadPts[3];
894 quadPts[0] = lastPoint;
895 quadPts[1] = quads[0];
896 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
fmalitabd5d7e72015-06-26 07:18:24 -0700897 quadTo(quadPts);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700898 lastPoint = quadPts[2];
899 quads += 2;
900 }
901}
902
robertphillips84b00882015-05-06 05:15:57 -0700903//////////////////////////////////////////////////////////////////////////////
904#if GR_AA_CONVEX_TESSELLATOR_VIZ
905static const SkScalar kPointRadius = 0.02f;
906static const SkScalar kArrowStrokeWidth = 0.0f;
907static const SkScalar kArrowLength = 0.2f;
908static const SkScalar kEdgeTextSize = 0.1f;
909static const SkScalar kPointTextSize = 0.02f;
910
911static void draw_point(SkCanvas* canvas, const SkPoint& p, SkScalar paramValue, bool stroke) {
912 SkPaint paint;
913 SkASSERT(paramValue <= 1.0f);
914 int gs = int(255*paramValue);
915 paint.setARGB(255, gs, gs, gs);
916
917 canvas->drawCircle(p.fX, p.fY, kPointRadius, paint);
918
919 if (stroke) {
920 SkPaint stroke;
921 stroke.setColor(SK_ColorYELLOW);
922 stroke.setStyle(SkPaint::kStroke_Style);
923 stroke.setStrokeWidth(kPointRadius/3.0f);
924 canvas->drawCircle(p.fX, p.fY, kPointRadius, stroke);
925 }
926}
927
928static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkColor color) {
929 SkPaint p;
930 p.setColor(color);
931
932 canvas->drawLine(p0.fX, p0.fY, p1.fX, p1.fY, p);
933}
934
935static void draw_arrow(SkCanvas*canvas, const SkPoint& p, const SkPoint &n,
936 SkScalar len, SkColor color) {
937 SkPaint paint;
938 paint.setColor(color);
939 paint.setStrokeWidth(kArrowStrokeWidth);
940 paint.setStyle(SkPaint::kStroke_Style);
941
942 canvas->drawLine(p.fX, p.fY,
943 p.fX + len * n.fX, p.fY + len * n.fY,
944 paint);
945}
946
947void GrAAConvexTessellator::Ring::draw(SkCanvas* canvas, const GrAAConvexTessellator& tess) const {
948 SkPaint paint;
949 paint.setTextSize(kEdgeTextSize);
950
951 for (int cur = 0; cur < fPts.count(); ++cur) {
952 int next = (cur + 1) % fPts.count();
953
954 draw_line(canvas,
955 tess.point(fPts[cur].fIndex),
956 tess.point(fPts[next].fIndex),
957 SK_ColorGREEN);
958
959 SkPoint mid = tess.point(fPts[cur].fIndex) + tess.point(fPts[next].fIndex);
960 mid.scale(0.5f);
961
962 if (fPts.count()) {
963 draw_arrow(canvas, mid, fPts[cur].fNorm, kArrowLength, SK_ColorRED);
964 mid.fX += (kArrowLength/2) * fPts[cur].fNorm.fX;
965 mid.fY += (kArrowLength/2) * fPts[cur].fNorm.fY;
966 }
967
968 SkString num;
969 num.printf("%d", this->origEdgeID(cur));
970 canvas->drawText(num.c_str(), num.size(), mid.fX, mid.fY, paint);
971
972 if (fPts.count()) {
973 draw_arrow(canvas, tess.point(fPts[cur].fIndex), fPts[cur].fBisector,
974 kArrowLength, SK_ColorBLUE);
975 }
976 }
977}
978
979void GrAAConvexTessellator::draw(SkCanvas* canvas) const {
980 for (int i = 0; i < fIndices.count(); i += 3) {
981 SkASSERT(fIndices[i] < this->numPts()) ;
982 SkASSERT(fIndices[i+1] < this->numPts()) ;
983 SkASSERT(fIndices[i+2] < this->numPts()) ;
984
985 draw_line(canvas,
986 this->point(this->fIndices[i]), this->point(this->fIndices[i+1]),
987 SK_ColorBLACK);
988 draw_line(canvas,
989 this->point(this->fIndices[i+1]), this->point(this->fIndices[i+2]),
990 SK_ColorBLACK);
991 draw_line(canvas,
992 this->point(this->fIndices[i+2]), this->point(this->fIndices[i]),
993 SK_ColorBLACK);
994 }
995
996 fInitialRing.draw(canvas, *this);
997 for (int i = 0; i < fRings.count(); ++i) {
998 fRings[i]->draw(canvas, *this);
999 }
1000
1001 for (int i = 0; i < this->numPts(); ++i) {
1002 draw_point(canvas,
fmalitabd5d7e72015-06-26 07:18:24 -07001003 this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)),
robertphillips84b00882015-05-06 05:15:57 -07001004 !this->movable(i));
1005
1006 SkPaint paint;
1007 paint.setTextSize(kPointTextSize);
1008 paint.setTextAlign(SkPaint::kCenter_Align);
fmalitabd5d7e72015-06-26 07:18:24 -07001009 if (this->depth(i) <= -kAntialiasingRadius) {
robertphillips84b00882015-05-06 05:15:57 -07001010 paint.setColor(SK_ColorWHITE);
1011 }
1012
1013 SkString num;
1014 num.printf("%d", i);
1015 canvas->drawText(num.c_str(), num.size(),
1016 this->point(i).fX, this->point(i).fY+(kPointRadius/2.0f),
1017 paint);
1018 }
1019}
1020
1021#endif
1022