blob: 85ce7ba9ed6bc17681cbc791d7134210861e0539 [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 {
684 dst[cur] = dst[cur-1] = fCandidateVerts.fuseWithBoth();
685 }
686 }
687
688 // Fold the new ring's points into the global pool
689 for (int i = 0; i < fCandidateVerts.numPts(); ++i) {
690 int newIdx;
fmalitabd5d7e72015-06-26 07:18:24 -0700691 if (fCandidateVerts.needsToBeNew(i) || forceNew) {
robertphillips84b00882015-05-06 05:15:57 -0700692 // if the originating index is still valid then this point wasn't
693 // fused (and is thus movable)
fmalitabd5d7e72015-06-26 07:18:24 -0700694 SkScalar coverage = compute_coverage(depth, initialDepth, initialCoverage,
695 targetDepth, targetCoverage);
696 newIdx = this->addPt(fCandidateVerts.point(i), depth, coverage,
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700697 fCandidateVerts.originatingIdx(i) != -1, false);
robertphillips84b00882015-05-06 05:15:57 -0700698 } else {
699 SkASSERT(fCandidateVerts.originatingIdx(i) != -1);
fmalitabd5d7e72015-06-26 07:18:24 -0700700 this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth,
701 targetCoverage);
robertphillips84b00882015-05-06 05:15:57 -0700702 newIdx = fCandidateVerts.originatingIdx(i);
703 }
704
705 nextRing->addIdx(newIdx, fCandidateVerts.origEdge(i));
706 }
707
708 // 'dst' currently has indices into the ring. Remap these to be indices
709 // into the global pool since the triangulation operates in that space.
710 for (int i = 0; i < dst.count(); ++i) {
711 dst[i] = nextRing->index(dst[i]);
712 }
713
714 for (int cur = 0; cur < lastRing.numPts(); ++cur) {
715 int next = (cur + 1) % lastRing.numPts();
716
717 this->addTri(lastRing.index(cur), lastRing.index(next), dst[next]);
718 this->addTri(lastRing.index(cur), dst[next], dst[cur]);
719 }
720
fmalitabd5d7e72015-06-26 07:18:24 -0700721 if (done && fStrokeWidth < 0.0f) {
722 // fill
robertphillips84b00882015-05-06 05:15:57 -0700723 this->fanRing(*nextRing);
724 }
725
726 if (nextRing->numPts() < 3) {
727 done = true;
728 }
robertphillips84b00882015-05-06 05:15:57 -0700729 return done;
730}
731
732void GrAAConvexTessellator::validate() const {
robertphillips84b00882015-05-06 05:15:57 -0700733 SkASSERT(fPts.count() == fMovable.count());
734 SkASSERT(0 == (fIndices.count() % 3));
735}
736
737//////////////////////////////////////////////////////////////////////////////
738void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) {
739 this->computeNormals(tess);
robertphillips364ad002015-05-20 11:49:55 -0700740 this->computeBisectors(tess);
robertphillips84b00882015-05-06 05:15:57 -0700741}
742
743void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms,
744 const SkTDArray<SkVector>& bisectors) {
745 for (int i = 0; i < fPts.count(); ++i) {
746 fPts[i].fNorm = norms[i];
747 fPts[i].fBisector = bisectors[i];
748 }
749}
750
751// Compute the outward facing normal at each vertex.
752void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& tess) {
753 for (int cur = 0; cur < fPts.count(); ++cur) {
754 int next = (cur + 1) % fPts.count();
755
756 fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex);
fmalitabd5d7e72015-06-26 07:18:24 -0700757 SkPoint::Normalize(&fPts[cur].fNorm);
robertphillips84b00882015-05-06 05:15:57 -0700758 fPts[cur].fNorm.setOrthog(fPts[cur].fNorm, tess.side());
robertphillips84b00882015-05-06 05:15:57 -0700759 }
760}
761
robertphillips364ad002015-05-20 11:49:55 -0700762void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& tess) {
robertphillips84b00882015-05-06 05:15:57 -0700763 int prev = fPts.count() - 1;
764 for (int cur = 0; cur < fPts.count(); prev = cur, ++cur) {
765 fPts[cur].fBisector = fPts[cur].fNorm + fPts[prev].fNorm;
robertphillips364ad002015-05-20 11:49:55 -0700766 if (!fPts[cur].fBisector.normalize()) {
767 SkASSERT(SkPoint::kLeft_Side == tess.side() || SkPoint::kRight_Side == tess.side());
768 fPts[cur].fBisector.setOrthog(fPts[cur].fNorm, (SkPoint::Side)-tess.side());
769 SkVector other;
770 other.setOrthog(fPts[prev].fNorm, tess.side());
771 fPts[cur].fBisector += other;
772 SkAssertResult(fPts[cur].fBisector.normalize());
773 } else {
774 fPts[cur].fBisector.negate(); // make the bisector face in
775 }
fmalitabd5d7e72015-06-26 07:18:24 -0700776 }
robertphillips84b00882015-05-06 05:15:57 -0700777}
778
779//////////////////////////////////////////////////////////////////////////////
780#ifdef SK_DEBUG
781// Is this ring convex?
782bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const {
783 if (fPts.count() < 3) {
fmalitabd5d7e72015-06-26 07:18:24 -0700784 return true;
robertphillips84b00882015-05-06 05:15:57 -0700785 }
786
787 SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.top().fIndex);
788 SkPoint cur = tess.point(fPts[1].fIndex) - tess.point(fPts[0].fIndex);
789 SkScalar minDot = prev.fX * cur.fY - prev.fY * cur.fX;
790 SkScalar maxDot = minDot;
791
792 prev = cur;
793 for (int i = 1; i < fPts.count(); ++i) {
794 int next = (i + 1) % fPts.count();
795
796 cur = tess.point(fPts[next].fIndex) - tess.point(fPts[i].fIndex);
797 SkScalar dot = prev.fX * cur.fY - prev.fY * cur.fX;
798
799 minDot = SkMinScalar(minDot, dot);
800 maxDot = SkMaxScalar(maxDot, dot);
801
802 prev = cur;
803 }
804
fmalitabd5d7e72015-06-26 07:18:24 -0700805 if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) {
806 maxDot = 0;
807 }
808 if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) {
809 minDot = 0;
810 }
811 return (maxDot >= 0.0f) == (minDot >= 0.0f);
robertphillips84b00882015-05-06 05:15:57 -0700812}
813
robertphillips84b00882015-05-06 05:15:57 -0700814#endif
815
fmalitabd5d7e72015-06-26 07:18:24 -0700816void GrAAConvexTessellator::lineTo(SkPoint p, bool isCurve) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700817 if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) {
818 return;
819 }
820
821 SkASSERT(fPts.count() <= 1 || fPts.count() == fNorms.count()+1);
822 if (this->numPts() >= 2 &&
823 abs_dist_from_line(fPts.top(), fNorms.top(), p) < kClose) {
824 // The old last point is on the line from the second to last to the new point
825 this->popLastPt();
826 fNorms.pop();
827 fIsCurve.pop();
828 }
fmalitabd5d7e72015-06-26 07:18:24 -0700829 SkScalar initialRingCoverage = fStrokeWidth < 0.0f ? 0.5f : 1.0f;
830 this->addPt(p, 0.0f, initialRingCoverage, false, isCurve);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700831 if (this->numPts() > 1) {
832 *fNorms.push() = fPts.top() - fPts[fPts.count()-2];
833 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top());
834 SkASSERT(len > 0.0f);
835 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms.top().length()));
836 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700837}
838
fmalitabd5d7e72015-06-26 07:18:24 -0700839void GrAAConvexTessellator::lineTo(const SkMatrix& m, SkPoint p, bool isCurve) {
840 m.mapPoints(&p, 1);
841 this->lineTo(p, isCurve);
842}
843
844void GrAAConvexTessellator::quadTo(SkPoint pts[3]) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700845 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
846 fPointBuffer.setReserve(maxCount);
847 SkPoint* target = fPointBuffer.begin();
848 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
849 kQuadTolerance, &target, maxCount);
850 fPointBuffer.setCount(count);
851 for (int i = 0; i < count; i++) {
fmalitabd5d7e72015-06-26 07:18:24 -0700852 lineTo(fPointBuffer[i], true);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700853 }
854}
855
fmalitabd5d7e72015-06-26 07:18:24 -0700856void GrAAConvexTessellator::quadTo(const SkMatrix& m, SkPoint pts[3]) {
857 SkPoint transformed[3];
858 transformed[0] = pts[0];
859 transformed[1] = pts[1];
860 transformed[2] = pts[2];
861 m.mapPoints(transformed, 3);
862 quadTo(transformed);
863}
864
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700865void GrAAConvexTessellator::cubicTo(const SkMatrix& m, SkPoint pts[4]) {
fmalitabd5d7e72015-06-26 07:18:24 -0700866 m.mapPoints(pts, 4);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700867 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
868 fPointBuffer.setReserve(maxCount);
869 SkPoint* target = fPointBuffer.begin();
870 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
871 kCubicTolerance, &target, maxCount);
872 fPointBuffer.setCount(count);
873 for (int i = 0; i < count; i++) {
fmalitabd5d7e72015-06-26 07:18:24 -0700874 lineTo(fPointBuffer[i], true);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700875 }
876}
877
878// include down here to avoid compilation errors caused by "-" overload in SkGeometry.h
879#include "SkGeometry.h"
880
fmalitabd5d7e72015-06-26 07:18:24 -0700881void GrAAConvexTessellator::conicTo(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
882 m.mapPoints(pts, 3);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700883 SkAutoConicToQuads quadder;
884 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
885 SkPoint lastPoint = *(quads++);
886 int count = quadder.countQuads();
887 for (int i = 0; i < count; ++i) {
888 SkPoint quadPts[3];
889 quadPts[0] = lastPoint;
890 quadPts[1] = quads[0];
891 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
fmalitabd5d7e72015-06-26 07:18:24 -0700892 quadTo(quadPts);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700893 lastPoint = quadPts[2];
894 quads += 2;
895 }
896}
897
robertphillips84b00882015-05-06 05:15:57 -0700898//////////////////////////////////////////////////////////////////////////////
899#if GR_AA_CONVEX_TESSELLATOR_VIZ
900static const SkScalar kPointRadius = 0.02f;
901static const SkScalar kArrowStrokeWidth = 0.0f;
902static const SkScalar kArrowLength = 0.2f;
903static const SkScalar kEdgeTextSize = 0.1f;
904static const SkScalar kPointTextSize = 0.02f;
905
906static void draw_point(SkCanvas* canvas, const SkPoint& p, SkScalar paramValue, bool stroke) {
907 SkPaint paint;
908 SkASSERT(paramValue <= 1.0f);
909 int gs = int(255*paramValue);
910 paint.setARGB(255, gs, gs, gs);
911
912 canvas->drawCircle(p.fX, p.fY, kPointRadius, paint);
913
914 if (stroke) {
915 SkPaint stroke;
916 stroke.setColor(SK_ColorYELLOW);
917 stroke.setStyle(SkPaint::kStroke_Style);
918 stroke.setStrokeWidth(kPointRadius/3.0f);
919 canvas->drawCircle(p.fX, p.fY, kPointRadius, stroke);
920 }
921}
922
923static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkColor color) {
924 SkPaint p;
925 p.setColor(color);
926
927 canvas->drawLine(p0.fX, p0.fY, p1.fX, p1.fY, p);
928}
929
930static void draw_arrow(SkCanvas*canvas, const SkPoint& p, const SkPoint &n,
931 SkScalar len, SkColor color) {
932 SkPaint paint;
933 paint.setColor(color);
934 paint.setStrokeWidth(kArrowStrokeWidth);
935 paint.setStyle(SkPaint::kStroke_Style);
936
937 canvas->drawLine(p.fX, p.fY,
938 p.fX + len * n.fX, p.fY + len * n.fY,
939 paint);
940}
941
942void GrAAConvexTessellator::Ring::draw(SkCanvas* canvas, const GrAAConvexTessellator& tess) const {
943 SkPaint paint;
944 paint.setTextSize(kEdgeTextSize);
945
946 for (int cur = 0; cur < fPts.count(); ++cur) {
947 int next = (cur + 1) % fPts.count();
948
949 draw_line(canvas,
950 tess.point(fPts[cur].fIndex),
951 tess.point(fPts[next].fIndex),
952 SK_ColorGREEN);
953
954 SkPoint mid = tess.point(fPts[cur].fIndex) + tess.point(fPts[next].fIndex);
955 mid.scale(0.5f);
956
957 if (fPts.count()) {
958 draw_arrow(canvas, mid, fPts[cur].fNorm, kArrowLength, SK_ColorRED);
959 mid.fX += (kArrowLength/2) * fPts[cur].fNorm.fX;
960 mid.fY += (kArrowLength/2) * fPts[cur].fNorm.fY;
961 }
962
963 SkString num;
964 num.printf("%d", this->origEdgeID(cur));
965 canvas->drawText(num.c_str(), num.size(), mid.fX, mid.fY, paint);
966
967 if (fPts.count()) {
968 draw_arrow(canvas, tess.point(fPts[cur].fIndex), fPts[cur].fBisector,
969 kArrowLength, SK_ColorBLUE);
970 }
971 }
972}
973
974void GrAAConvexTessellator::draw(SkCanvas* canvas) const {
975 for (int i = 0; i < fIndices.count(); i += 3) {
976 SkASSERT(fIndices[i] < this->numPts()) ;
977 SkASSERT(fIndices[i+1] < this->numPts()) ;
978 SkASSERT(fIndices[i+2] < this->numPts()) ;
979
980 draw_line(canvas,
981 this->point(this->fIndices[i]), this->point(this->fIndices[i+1]),
982 SK_ColorBLACK);
983 draw_line(canvas,
984 this->point(this->fIndices[i+1]), this->point(this->fIndices[i+2]),
985 SK_ColorBLACK);
986 draw_line(canvas,
987 this->point(this->fIndices[i+2]), this->point(this->fIndices[i]),
988 SK_ColorBLACK);
989 }
990
991 fInitialRing.draw(canvas, *this);
992 for (int i = 0; i < fRings.count(); ++i) {
993 fRings[i]->draw(canvas, *this);
994 }
995
996 for (int i = 0; i < this->numPts(); ++i) {
997 draw_point(canvas,
fmalitabd5d7e72015-06-26 07:18:24 -0700998 this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)),
robertphillips84b00882015-05-06 05:15:57 -0700999 !this->movable(i));
1000
1001 SkPaint paint;
1002 paint.setTextSize(kPointTextSize);
1003 paint.setTextAlign(SkPaint::kCenter_Align);
fmalitabd5d7e72015-06-26 07:18:24 -07001004 if (this->depth(i) <= -kAntialiasingRadius) {
robertphillips84b00882015-05-06 05:15:57 -07001005 paint.setColor(SK_ColorWHITE);
1006 }
1007
1008 SkString num;
1009 num.printf("%d", i);
1010 canvas->drawText(num.c_str(), num.size(),
1011 this->point(i).fX, this->point(i).fY+(kPointRadius/2.0f),
1012 paint);
1013 }
1014}
1015
1016#endif
1017