blob: aea7ce72aeb60a184fb657a812b06894a11e2f5e [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);
Mike Reed8be952a2017-02-13 20:44:33 -050022static const SkScalar kCloseSqd = kClose * kClose;
robertphillips84b00882015-05-06 05:15:57 -070023
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
ethannicholasfab4a9b2016-08-26 11:03:32 -070032// dot product above which we consider two adjacent curves to be part of the "same" curve
ethannicholasfc6cb732016-08-30 07:26:58 -070033static const SkScalar kCurveConnectionThreshold = 0.8f;
ethannicholasfab4a9b2016-08-26 11:03:32 -070034
robertphillips8c170972016-09-22 12:42:30 -070035static bool intersect(const SkPoint& p0, const SkPoint& n0,
36 const SkPoint& p1, const SkPoint& n1,
37 SkScalar* t) {
robertphillips84b00882015-05-06 05:15:57 -070038 const SkPoint v = p1 - p0;
robertphillips84b00882015-05-06 05:15:57 -070039 SkScalar perpDot = n0.fX * n1.fY - n0.fY * n1.fX;
robertphillips8c170972016-09-22 12:42:30 -070040 if (SkScalarNearlyZero(perpDot)) {
41 return false;
42 }
43 *t = (v.fX * n1.fY - v.fY * n1.fX) / perpDot;
44 SkASSERT(SkScalarIsFinite(*t));
45 return true;
robertphillips84b00882015-05-06 05:15:57 -070046}
47
halcanary9d524f22016-03-29 09:03:52 -070048// This is a special case version of intersect where we have the vector
robertphillips84b00882015-05-06 05:15:57 -070049// perpendicular to the second line rather than the vector parallel to it.
50static SkScalar perp_intersect(const SkPoint& p0, const SkPoint& n0,
51 const SkPoint& p1, const SkPoint& perp) {
52 const SkPoint v = p1 - p0;
53 SkScalar perpDot = n0.dot(perp);
54 return v.dot(perp) / perpDot;
55}
56
57static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) {
58 SkScalar distSq = p0.distanceToSqd(p1);
59 return distSq < kCloseSqd;
60}
61
62static SkScalar abs_dist_from_line(const SkPoint& p0, const SkVector& v, const SkPoint& test) {
63 SkPoint testV = test - p0;
64 SkScalar dist = testV.fX * v.fY - testV.fY * v.fX;
65 return SkScalarAbs(dist);
66}
67
68int GrAAConvexTessellator::addPt(const SkPoint& pt,
69 SkScalar depth,
fmalitabd5d7e72015-06-26 07:18:24 -070070 SkScalar coverage,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070071 bool movable,
ethannicholasfab4a9b2016-08-26 11:03:32 -070072 CurveState curve) {
robertphillips84b00882015-05-06 05:15:57 -070073 this->validate();
74
75 int index = fPts.count();
76 *fPts.push() = pt;
fmalitabd5d7e72015-06-26 07:18:24 -070077 *fCoverages.push() = coverage;
robertphillips84b00882015-05-06 05:15:57 -070078 *fMovable.push() = movable;
ethannicholasfab4a9b2016-08-26 11:03:32 -070079 *fCurveState.push() = curve;
robertphillips84b00882015-05-06 05:15:57 -070080
81 this->validate();
82 return index;
83}
84
85void GrAAConvexTessellator::popLastPt() {
86 this->validate();
87
88 fPts.pop();
fmalitabd5d7e72015-06-26 07:18:24 -070089 fCoverages.pop();
robertphillips84b00882015-05-06 05:15:57 -070090 fMovable.pop();
Robert Phillips1d089982016-09-26 14:07:48 -040091 fCurveState.pop();
robertphillips84b00882015-05-06 05:15:57 -070092
93 this->validate();
94}
95
96void GrAAConvexTessellator::popFirstPtShuffle() {
97 this->validate();
98
99 fPts.removeShuffle(0);
fmalitabd5d7e72015-06-26 07:18:24 -0700100 fCoverages.removeShuffle(0);
robertphillips84b00882015-05-06 05:15:57 -0700101 fMovable.removeShuffle(0);
Robert Phillips1d089982016-09-26 14:07:48 -0400102 fCurveState.removeShuffle(0);
robertphillips84b00882015-05-06 05:15:57 -0700103
104 this->validate();
105}
106
107void GrAAConvexTessellator::updatePt(int index,
108 const SkPoint& pt,
fmalitabd5d7e72015-06-26 07:18:24 -0700109 SkScalar depth,
110 SkScalar coverage) {
robertphillips84b00882015-05-06 05:15:57 -0700111 this->validate();
112 SkASSERT(fMovable[index]);
113
114 fPts[index] = pt;
fmalitabd5d7e72015-06-26 07:18:24 -0700115 fCoverages[index] = coverage;
robertphillips84b00882015-05-06 05:15:57 -0700116}
117
118void GrAAConvexTessellator::addTri(int i0, int i1, int i2) {
119 if (i0 == i1 || i1 == i2 || i2 == i0) {
120 return;
121 }
122
123 *fIndices.push() = i0;
124 *fIndices.push() = i1;
125 *fIndices.push() = i2;
126}
127
128void GrAAConvexTessellator::rewind() {
129 fPts.rewind();
fmalitabd5d7e72015-06-26 07:18:24 -0700130 fCoverages.rewind();
robertphillips84b00882015-05-06 05:15:57 -0700131 fMovable.rewind();
132 fIndices.rewind();
133 fNorms.rewind();
Robert Phillips1d089982016-09-26 14:07:48 -0400134 fCurveState.rewind();
robertphillips84b00882015-05-06 05:15:57 -0700135 fInitialRing.rewind();
136 fCandidateVerts.rewind();
137#if GR_AA_CONVEX_TESSELLATOR_VIZ
138 fRings.rewind(); // TODO: leak in this case!
139#else
140 fRings[0].rewind();
141 fRings[1].rewind();
142#endif
143}
144
145void GrAAConvexTessellator::computeBisectors() {
146 fBisectors.setCount(fNorms.count());
147
148 int prev = fBisectors.count() - 1;
149 for (int cur = 0; cur < fBisectors.count(); prev = cur, ++cur) {
150 fBisectors[cur] = fNorms[cur] + fNorms[prev];
robertphillips364ad002015-05-20 11:49:55 -0700151 if (!fBisectors[cur].normalize()) {
152 SkASSERT(SkPoint::kLeft_Side == fSide || SkPoint::kRight_Side == fSide);
153 fBisectors[cur].setOrthog(fNorms[cur], (SkPoint::Side)-fSide);
154 SkVector other;
155 other.setOrthog(fNorms[prev], fSide);
156 fBisectors[cur] += other;
halcanary9d524f22016-03-29 09:03:52 -0700157 SkAssertResult(fBisectors[cur].normalize());
robertphillips364ad002015-05-20 11:49:55 -0700158 } else {
159 fBisectors[cur].negate(); // make the bisector face in
160 }
ethannicholasfab4a9b2016-08-26 11:03:32 -0700161 if (fCurveState[prev] == kIndeterminate_CurveState) {
162 if (fCurveState[cur] == kSharp_CurveState) {
163 fCurveState[prev] = kSharp_CurveState;
164 } else {
165 if (SkScalarAbs(fNorms[cur].dot(fNorms[prev])) > kCurveConnectionThreshold) {
166 fCurveState[prev] = kCurve_CurveState;
167 fCurveState[cur] = kCurve_CurveState;
168 } else {
169 fCurveState[prev] = kSharp_CurveState;
170 fCurveState[cur] = kSharp_CurveState;
171 }
172 }
173 }
robertphillips84b00882015-05-06 05:15:57 -0700174
175 SkASSERT(SkScalarNearlyEqual(1.0f, fBisectors[cur].length()));
176 }
177}
178
fmalitabd5d7e72015-06-26 07:18:24 -0700179// Create as many rings as we need to (up to a predefined limit) to reach the specified target
180// depth. If we are in fill mode, the final ring will automatically be fanned.
181bool GrAAConvexTessellator::createInsetRings(Ring& previousRing, SkScalar initialDepth,
halcanary9d524f22016-03-29 09:03:52 -0700182 SkScalar initialCoverage, SkScalar targetDepth,
fmalitabd5d7e72015-06-26 07:18:24 -0700183 SkScalar targetCoverage, Ring** finalRing) {
184 static const int kMaxNumRings = 8;
185
186 if (previousRing.numPts() < 3) {
187 return false;
188 }
189 Ring* currentRing = &previousRing;
190 int i;
191 for (i = 0; i < kMaxNumRings; ++i) {
192 Ring* nextRing = this->getNextRing(currentRing);
193 SkASSERT(nextRing != currentRing);
194
halcanary9d524f22016-03-29 09:03:52 -0700195 bool done = this->createInsetRing(*currentRing, nextRing, initialDepth, initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700196 targetDepth, targetCoverage, i == 0);
197 currentRing = nextRing;
198 if (done) {
199 break;
200 }
201 currentRing->init(*this);
202 }
203
204 if (kMaxNumRings == i) {
205 // Bail if we've exceeded the amount of time we want to throw at this.
206 this->terminate(*currentRing);
207 return false;
208 }
209 bool done = currentRing->numPts() >= 3;
210 if (done) {
211 currentRing->init(*this);
212 }
213 *finalRing = currentRing;
214 return done;
215}
216
robertphillips84b00882015-05-06 05:15:57 -0700217// The general idea here is to, conceptually, start with the original polygon and slide
218// the vertices along the bisectors until the first intersection. At that
219// point two of the edges collapse and the process repeats on the new polygon.
220// The polygon state is captured in the Ring class while the GrAAConvexTessellator
221// controls the iteration. The CandidateVerts holds the formative points for the
222// next ring.
223bool GrAAConvexTessellator::tessellate(const SkMatrix& m, const SkPath& path) {
robertphillips84b00882015-05-06 05:15:57 -0700224 if (!this->extractFromPath(m, path)) {
225 return false;
226 }
227
fmalitabd5d7e72015-06-26 07:18:24 -0700228 SkScalar coverage = 1.0f;
ethannicholasfea77632015-08-19 12:09:12 -0700229 SkScalar scaleFactor = 0.0f;
robertphillips8c170972016-09-22 12:42:30 -0700230
231 if (SkStrokeRec::kStrokeAndFill_Style == fStyle) {
232 SkASSERT(m.isSimilarity());
233 scaleFactor = m.getMaxScale(); // x and y scale are the same
234 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
235 Ring outerStrokeAndAARing;
236 this->createOuterRing(fInitialRing,
237 effectiveStrokeWidth / 2 + kAntialiasingRadius, 0.0,
238 &outerStrokeAndAARing);
239
240 // discard all the triangles added between the originating ring and the new outer ring
241 fIndices.rewind();
242
243 outerStrokeAndAARing.init(*this);
244
245 outerStrokeAndAARing.makeOriginalRing();
246
247 // Add the outer stroke ring's normals to the originating ring's normals
248 // so it can also act as an originating ring
249 fNorms.setCount(fNorms.count() + outerStrokeAndAARing.numPts());
250 for (int i = 0; i < outerStrokeAndAARing.numPts(); ++i) {
251 SkASSERT(outerStrokeAndAARing.index(i) < fNorms.count());
252 fNorms[outerStrokeAndAARing.index(i)] = outerStrokeAndAARing.norm(i);
253 }
254
255 // the bisectors are only needed for the computation of the outer ring
256 fBisectors.rewind();
257
258 Ring* insetAARing;
259 this->createInsetRings(outerStrokeAndAARing,
260 0.0f, 0.0f, 2*kAntialiasingRadius, 1.0f,
261 &insetAARing);
262
263 SkDEBUGCODE(this->validate();)
264 return true;
265 }
266
267 if (SkStrokeRec::kStroke_Style == fStyle) {
268 SkASSERT(fStrokeWidth >= 0.0f);
halcanary9d524f22016-03-29 09:03:52 -0700269 SkASSERT(m.isSimilarity());
ethannicholasfea77632015-08-19 12:09:12 -0700270 scaleFactor = m.getMaxScale(); // x and y scale are the same
271 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
fmalitabd5d7e72015-06-26 07:18:24 -0700272 Ring outerStrokeRing;
halcanary9d524f22016-03-29 09:03:52 -0700273 this->createOuterRing(fInitialRing, effectiveStrokeWidth / 2 - kAntialiasingRadius,
ethannicholasfea77632015-08-19 12:09:12 -0700274 coverage, &outerStrokeRing);
fmalitabd5d7e72015-06-26 07:18:24 -0700275 outerStrokeRing.init(*this);
276 Ring outerAARing;
277 this->createOuterRing(outerStrokeRing, kAntialiasingRadius * 2, 0.0f, &outerAARing);
278 } else {
279 Ring outerAARing;
280 this->createOuterRing(fInitialRing, kAntialiasingRadius, 0.0f, &outerAARing);
281 }
robertphillips84b00882015-05-06 05:15:57 -0700282
283 // the bisectors are only needed for the computation of the outer ring
284 fBisectors.rewind();
robertphillips8c170972016-09-22 12:42:30 -0700285 if (SkStrokeRec::kStroke_Style == fStyle && fInitialRing.numPts() > 2) {
286 SkASSERT(fStrokeWidth >= 0.0f);
ethannicholasfea77632015-08-19 12:09:12 -0700287 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
fmalitabd5d7e72015-06-26 07:18:24 -0700288 Ring* insetStrokeRing;
ethannicholasfea77632015-08-19 12:09:12 -0700289 SkScalar strokeDepth = effectiveStrokeWidth / 2 - kAntialiasingRadius;
halcanary9d524f22016-03-29 09:03:52 -0700290 if (this->createInsetRings(fInitialRing, 0.0f, coverage, strokeDepth, coverage,
robertphillips8c170972016-09-22 12:42:30 -0700291 &insetStrokeRing)) {
fmalitabd5d7e72015-06-26 07:18:24 -0700292 Ring* insetAARing;
halcanary9d524f22016-03-29 09:03:52 -0700293 this->createInsetRings(*insetStrokeRing, strokeDepth, coverage, strokeDepth +
robertphillips8c170972016-09-22 12:42:30 -0700294 kAntialiasingRadius * 2, 0.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700295 }
fmalitabd5d7e72015-06-26 07:18:24 -0700296 } else {
297 Ring* insetAARing;
298 this->createInsetRings(fInitialRing, 0.0f, 0.5f, kAntialiasingRadius, 1.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700299 }
300
fmalitabd5d7e72015-06-26 07:18:24 -0700301 SkDEBUGCODE(this->validate();)
robertphillips84b00882015-05-06 05:15:57 -0700302 return true;
303}
304
305SkScalar GrAAConvexTessellator::computeDepthFromEdge(int edgeIdx, const SkPoint& p) const {
306 SkASSERT(edgeIdx < fNorms.count());
307
308 SkPoint v = p - fPts[edgeIdx];
309 SkScalar depth = -fNorms[edgeIdx].dot(v);
robertphillips84b00882015-05-06 05:15:57 -0700310 return depth;
311}
312
313// Find a point that is 'desiredDepth' away from the 'edgeIdx'-th edge and lies
314// along the 'bisector' from the 'startIdx'-th point.
315bool GrAAConvexTessellator::computePtAlongBisector(int startIdx,
316 const SkVector& bisector,
317 int edgeIdx,
318 SkScalar desiredDepth,
319 SkPoint* result) const {
320 const SkPoint& norm = fNorms[edgeIdx];
321
322 // First find the point where the edge and the bisector intersect
323 SkPoint newP;
fmalitabd5d7e72015-06-26 07:18:24 -0700324
robertphillips84b00882015-05-06 05:15:57 -0700325 SkScalar t = perp_intersect(fPts[startIdx], bisector, fPts[edgeIdx], norm);
326 if (SkScalarNearlyEqual(t, 0.0f)) {
327 // the start point was one of the original ring points
fmalitabd5d7e72015-06-26 07:18:24 -0700328 SkASSERT(startIdx < fPts.count());
robertphillips84b00882015-05-06 05:15:57 -0700329 newP = fPts[startIdx];
fmalitabd5d7e72015-06-26 07:18:24 -0700330 } else if (t < 0.0f) {
robertphillips84b00882015-05-06 05:15:57 -0700331 newP = bisector;
332 newP.scale(t);
333 newP += fPts[startIdx];
334 } else {
335 return false;
336 }
337
338 // Then offset along the bisector from that point the correct distance
fmalitabd5d7e72015-06-26 07:18:24 -0700339 SkScalar dot = bisector.dot(norm);
340 t = -desiredDepth / dot;
robertphillips84b00882015-05-06 05:15:57 -0700341 *result = bisector;
342 result->scale(t);
343 *result += newP;
344
robertphillips84b00882015-05-06 05:15:57 -0700345 return true;
346}
347
348bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& path) {
robertphillips84b00882015-05-06 05:15:57 -0700349 SkASSERT(SkPath::kConvex_Convexity == path.getConvexity());
350
351 // Outer ring: 3*numPts
352 // Middle ring: numPts
353 // Presumptive inner ring: numPts
354 this->reservePts(5*path.countPoints());
355 // Outer ring: 12*numPts
356 // Middle ring: 0
357 // Presumptive inner ring: 6*numPts + 6
358 fIndices.setReserve(18*path.countPoints() + 6);
359
360 fNorms.setReserve(path.countPoints());
361
robertphillips84b00882015-05-06 05:15:57 -0700362 // TODO: is there a faster way to extract the points from the path? Perhaps
363 // get all the points via a new entry point, transform them all in bulk
364 // and then walk them to find duplicates?
365 SkPath::Iter iter(path, true);
366 SkPoint pts[4];
367 SkPath::Verb verb;
Brian Salomon97042bf2017-02-28 11:21:28 -0500368 while ((verb = iter.next(pts, true, true)) != SkPath::kDone_Verb) {
robertphillips84b00882015-05-06 05:15:57 -0700369 switch (verb) {
370 case SkPath::kLine_Verb:
ethannicholasfab4a9b2016-08-26 11:03:32 -0700371 this->lineTo(m, pts[1], kSharp_CurveState);
robertphillips84b00882015-05-06 05:15:57 -0700372 break;
373 case SkPath::kQuad_Verb:
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700374 this->quadTo(m, pts);
375 break;
robertphillips84b00882015-05-06 05:15:57 -0700376 case SkPath::kCubic_Verb:
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700377 this->cubicTo(m, pts);
378 break;
379 case SkPath::kConic_Verb:
380 this->conicTo(m, pts, iter.conicWeight());
robertphillips84b00882015-05-06 05:15:57 -0700381 break;
382 case SkPath::kMove_Verb:
383 case SkPath::kClose_Verb:
384 case SkPath::kDone_Verb:
385 break;
386 }
387 }
388
fmalitabd5d7e72015-06-26 07:18:24 -0700389 if (this->numPts() < 2) {
robertphillips84b00882015-05-06 05:15:57 -0700390 return false;
391 }
392
393 // check if last point is a duplicate of the first point. If so, remove it.
394 if (duplicate_pt(fPts[this->numPts()-1], fPts[0])) {
395 this->popLastPt();
396 fNorms.pop();
397 }
398
399 SkASSERT(fPts.count() == fNorms.count()+1);
fmalitabd5d7e72015-06-26 07:18:24 -0700400 if (this->numPts() >= 3) {
401 if (abs_dist_from_line(fPts.top(), fNorms.top(), fPts[0]) < kClose) {
402 // The last point is on the line from the second to last to the first point.
403 this->popLastPt();
404 fNorms.pop();
405 }
406
407 *fNorms.push() = fPts[0] - fPts.top();
408 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top());
409 SkASSERT(len > 0.0f);
410 SkASSERT(fPts.count() == fNorms.count());
robertphillips84b00882015-05-06 05:15:57 -0700411 }
412
fmalitabd5d7e72015-06-26 07:18:24 -0700413 if (this->numPts() >= 3 && abs_dist_from_line(fPts[0], fNorms.top(), fPts[1]) < kClose) {
robertphillips84b00882015-05-06 05:15:57 -0700414 // The first point is on the line from the last to the second.
415 this->popFirstPtShuffle();
416 fNorms.removeShuffle(0);
417 fNorms[0] = fPts[1] - fPts[0];
418 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms[0]);
419 SkASSERT(len > 0.0f);
420 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[0].length()));
421 }
422
fmalitabd5d7e72015-06-26 07:18:24 -0700423 if (this->numPts() >= 3) {
424 // Check the cross product of the final trio
425 SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.top());
426 if (cross > 0.0f) {
427 fSide = SkPoint::kRight_Side;
428 } else {
429 fSide = SkPoint::kLeft_Side;
430 }
431
432 // Make all the normals face outwards rather than along the edge
433 for (int cur = 0; cur < fNorms.count(); ++cur) {
434 fNorms[cur].setOrthog(fNorms[cur], fSide);
435 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length()));
436 }
437
438 this->computeBisectors();
439 } else if (this->numPts() == 2) {
halcanary9d524f22016-03-29 09:03:52 -0700440 // We've got two points, so we're degenerate.
robertphillips8c170972016-09-22 12:42:30 -0700441 if (fStyle == SkStrokeRec::kFill_Style) {
fmalitabd5d7e72015-06-26 07:18:24 -0700442 // it's a fill, so we don't need to worry about degenerate paths
443 return false;
444 }
445 // For stroking, we still need to process the degenerate path, so fix it up
446 fSide = SkPoint::kLeft_Side;
447
448 // Make all the normals face outwards rather than along the edge
449 for (int cur = 0; cur < fNorms.count(); ++cur) {
450 fNorms[cur].setOrthog(fNorms[cur], fSide);
451 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length()));
452 }
453
454 fNorms.push(SkPoint::Make(-fNorms[0].fX, -fNorms[0].fY));
455 // we won't actually use the bisectors, so just push zeroes
456 fBisectors.push(SkPoint::Make(0.0, 0.0));
457 fBisectors.push(SkPoint::Make(0.0, 0.0));
458 } else {
robertphillips84b00882015-05-06 05:15:57 -0700459 return false;
460 }
461
robertphillips84b00882015-05-06 05:15:57 -0700462 fCandidateVerts.setReserve(this->numPts());
463 fInitialRing.setReserve(this->numPts());
464 for (int i = 0; i < this->numPts(); ++i) {
465 fInitialRing.addIdx(i, i);
466 }
467 fInitialRing.init(fNorms, fBisectors);
468
469 this->validate();
470 return true;
471}
472
473GrAAConvexTessellator::Ring* GrAAConvexTessellator::getNextRing(Ring* lastRing) {
474#if GR_AA_CONVEX_TESSELLATOR_VIZ
halcanary385fe4d2015-08-26 13:07:48 -0700475 Ring* ring = *fRings.push() = new Ring;
robertphillips84b00882015-05-06 05:15:57 -0700476 ring->setReserve(fInitialRing.numPts());
477 ring->rewind();
478 return ring;
479#else
480 // Flip flop back and forth between fRings[0] & fRings[1]
481 int nextRing = (lastRing == &fRings[0]) ? 1 : 0;
482 fRings[nextRing].setReserve(fInitialRing.numPts());
483 fRings[nextRing].rewind();
484 return &fRings[nextRing];
485#endif
486}
487
488void GrAAConvexTessellator::fanRing(const Ring& ring) {
489 // fan out from point 0
fmalitabd5d7e72015-06-26 07:18:24 -0700490 int startIdx = ring.index(0);
491 for (int cur = ring.numPts() - 2; cur >= 0; --cur) {
492 this->addTri(startIdx, ring.index(cur), ring.index(cur + 1));
robertphillips84b00882015-05-06 05:15:57 -0700493 }
494}
495
halcanary9d524f22016-03-29 09:03:52 -0700496void GrAAConvexTessellator::createOuterRing(const Ring& previousRing, SkScalar outset,
fmalitabd5d7e72015-06-26 07:18:24 -0700497 SkScalar coverage, Ring* nextRing) {
498 const int numPts = previousRing.numPts();
499 if (numPts == 0) {
500 return;
501 }
robertphillips84b00882015-05-06 05:15:57 -0700502
robertphillips84b00882015-05-06 05:15:57 -0700503 int prev = numPts - 1;
fmalitabd5d7e72015-06-26 07:18:24 -0700504 int lastPerpIdx = -1, firstPerpIdx = -1;
505
Mike Reed8be952a2017-02-13 20:44:33 -0500506 const SkScalar outsetSq = outset * outset;
507 SkScalar miterLimitSq = outset * fMiterLimit;
508 miterLimitSq = miterLimitSq * miterLimitSq;
robertphillips84b00882015-05-06 05:15:57 -0700509 for (int cur = 0; cur < numPts; ++cur) {
fmalitabd5d7e72015-06-26 07:18:24 -0700510 int originalIdx = previousRing.index(cur);
halcanary9d524f22016-03-29 09:03:52 -0700511 // For each vertex of the original polygon we add at least two points to the
fmalitabd5d7e72015-06-26 07:18:24 -0700512 // outset polygon - one extending perpendicular to each impinging edge. Connecting these
halcanary9d524f22016-03-29 09:03:52 -0700513 // two points yields a bevel join. We need one additional point for a mitered join, and
fmalitabd5d7e72015-06-26 07:18:24 -0700514 // a round join requires one or more points depending upon curvature.
robertphillips84b00882015-05-06 05:15:57 -0700515
fmalitabd5d7e72015-06-26 07:18:24 -0700516 // The perpendicular point for the last edge
517 SkPoint normal1 = previousRing.norm(prev);
518 SkPoint perp1 = normal1;
519 perp1.scale(outset);
520 perp1 += this->point(originalIdx);
robertphillips84b00882015-05-06 05:15:57 -0700521
fmalitabd5d7e72015-06-26 07:18:24 -0700522 // The perpendicular point for the next edge.
523 SkPoint normal2 = previousRing.norm(cur);
524 SkPoint perp2 = normal2;
525 perp2.scale(outset);
526 perp2 += fPts[originalIdx];
robertphillips84b00882015-05-06 05:15:57 -0700527
ethannicholasfab4a9b2016-08-26 11:03:32 -0700528 CurveState curve = fCurveState[originalIdx];
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700529
fmalitabd5d7e72015-06-26 07:18:24 -0700530 // We know it isn't a duplicate of the prior point (since it and this
531 // one are just perpendicular offsets from the non-merged polygon points)
ethannicholasfab4a9b2016-08-26 11:03:32 -0700532 int perp1Idx = this->addPt(perp1, -outset, coverage, false, curve);
fmalitabd5d7e72015-06-26 07:18:24 -0700533 nextRing->addIdx(perp1Idx, originalIdx);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700534
fmalitabd5d7e72015-06-26 07:18:24 -0700535 int perp2Idx;
536 // For very shallow angles all the corner points could fuse.
537 if (duplicate_pt(perp2, this->point(perp1Idx))) {
538 perp2Idx = perp1Idx;
539 } else {
ethannicholasfab4a9b2016-08-26 11:03:32 -0700540 perp2Idx = this->addPt(perp2, -outset, coverage, false, curve);
robertphillips84b00882015-05-06 05:15:57 -0700541 }
ethannicholas2436f192015-06-25 14:42:34 -0700542
fmalitabd5d7e72015-06-26 07:18:24 -0700543 if (perp2Idx != perp1Idx) {
ethannicholasfab4a9b2016-08-26 11:03:32 -0700544 if (curve == kCurve_CurveState) {
fmalitabd5d7e72015-06-26 07:18:24 -0700545 // bevel or round depending upon curvature
546 SkScalar dotProd = normal1.dot(normal2);
547 if (dotProd < kRoundCapThreshold) {
548 // Currently we "round" by creating a single extra point, which produces
549 // good results for common cases. For thick strokes with high curvature, we will
550 // need to add more points; for the time being we simply fall back to software
551 // rendering for thick strokes.
552 SkPoint miter = previousRing.bisector(cur);
553 miter.setLength(-outset);
554 miter += fPts[originalIdx];
reed9730f4a2015-06-26 05:06:43 -0700555
fmalitabd5d7e72015-06-26 07:18:24 -0700556 // For very shallow angles all the corner points could fuse
557 if (!duplicate_pt(miter, this->point(perp1Idx))) {
558 int miterIdx;
ethannicholasfab4a9b2016-08-26 11:03:32 -0700559 miterIdx = this->addPt(miter, -outset, coverage, false, kSharp_CurveState);
fmalitabd5d7e72015-06-26 07:18:24 -0700560 nextRing->addIdx(miterIdx, originalIdx);
561 // The two triangles for the corner
562 this->addTri(originalIdx, perp1Idx, miterIdx);
563 this->addTri(originalIdx, miterIdx, perp2Idx);
564 }
565 } else {
566 this->addTri(originalIdx, perp1Idx, perp2Idx);
567 }
reed9730f4a2015-06-26 05:06:43 -0700568 } else {
fmalitabd5d7e72015-06-26 07:18:24 -0700569 switch (fJoin) {
570 case SkPaint::Join::kMiter_Join: {
571 // The bisector outset point
572 SkPoint miter = previousRing.bisector(cur);
573 SkScalar dotProd = normal1.dot(normal2);
574 SkScalar sinHalfAngleSq = SkScalarHalf(SK_Scalar1 + dotProd);
575 SkScalar lengthSq = outsetSq / sinHalfAngleSq;
576 if (lengthSq > miterLimitSq) {
577 // just bevel it
578 this->addTri(originalIdx, perp1Idx, perp2Idx);
579 break;
580 }
581 miter.setLength(-SkScalarSqrt(lengthSq));
582 miter += fPts[originalIdx];
583
584 // For very shallow angles all the corner points could fuse
585 if (!duplicate_pt(miter, this->point(perp1Idx))) {
586 int miterIdx;
ethannicholasfab4a9b2016-08-26 11:03:32 -0700587 miterIdx = this->addPt(miter, -outset, coverage, false,
588 kSharp_CurveState);
fmalitabd5d7e72015-06-26 07:18:24 -0700589 nextRing->addIdx(miterIdx, originalIdx);
590 // The two triangles for the corner
591 this->addTri(originalIdx, perp1Idx, miterIdx);
592 this->addTri(originalIdx, miterIdx, perp2Idx);
593 }
594 break;
595 }
596 case SkPaint::Join::kBevel_Join:
597 this->addTri(originalIdx, perp1Idx, perp2Idx);
598 break;
599 default:
halcanary9d524f22016-03-29 09:03:52 -0700600 // kRound_Join is unsupported for now. GrAALinearizingConvexPathRenderer is
fmalitabd5d7e72015-06-26 07:18:24 -0700601 // only willing to draw mitered or beveled, so we should never get here.
602 SkASSERT(false);
603 }
reed9730f4a2015-06-26 05:06:43 -0700604 }
605
fmalitabd5d7e72015-06-26 07:18:24 -0700606 nextRing->addIdx(perp2Idx, originalIdx);
ethannicholas2436f192015-06-25 14:42:34 -0700607 }
fmalitabd5d7e72015-06-26 07:18:24 -0700608
609 if (0 == cur) {
610 // Store the index of the first perpendicular point to finish up
611 firstPerpIdx = perp1Idx;
612 SkASSERT(-1 == lastPerpIdx);
613 } else {
614 // The triangles for the previous edge
615 int prevIdx = previousRing.index(prev);
616 this->addTri(prevIdx, perp1Idx, originalIdx);
617 this->addTri(prevIdx, lastPerpIdx, perp1Idx);
618 }
619
620 // Track the last perpendicular outset point so we can construct the
621 // trailing edge triangles.
622 lastPerpIdx = perp2Idx;
623 prev = cur;
robertphillips84b00882015-05-06 05:15:57 -0700624 }
625
626 // pick up the final edge rect
fmalitabd5d7e72015-06-26 07:18:24 -0700627 int lastIdx = previousRing.index(numPts - 1);
628 this->addTri(lastIdx, firstPerpIdx, previousRing.index(0));
629 this->addTri(lastIdx, lastPerpIdx, firstPerpIdx);
robertphillips84b00882015-05-06 05:15:57 -0700630
631 this->validate();
632}
633
fmalitabd5d7e72015-06-26 07:18:24 -0700634// Something went wrong in the creation of the next ring. If we're filling the shape, just go ahead
635// and fan it.
robertphillips84b00882015-05-06 05:15:57 -0700636void GrAAConvexTessellator::terminate(const Ring& ring) {
robertphillips8c170972016-09-22 12:42:30 -0700637 if (fStyle != SkStrokeRec::kStroke_Style) {
fmalitabd5d7e72015-06-26 07:18:24 -0700638 this->fanRing(ring);
robertphillips84b00882015-05-06 05:15:57 -0700639 }
fmalitabd5d7e72015-06-26 07:18:24 -0700640}
robertphillips84b00882015-05-06 05:15:57 -0700641
halcanary9d524f22016-03-29 09:03:52 -0700642static SkScalar compute_coverage(SkScalar depth, SkScalar initialDepth, SkScalar initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700643 SkScalar targetDepth, SkScalar targetCoverage) {
644 if (SkScalarNearlyEqual(initialDepth, targetDepth)) {
645 return targetCoverage;
646 }
halcanary9d524f22016-03-29 09:03:52 -0700647 SkScalar result = (depth - initialDepth) / (targetDepth - initialDepth) *
fmalitabd5d7e72015-06-26 07:18:24 -0700648 (targetCoverage - initialCoverage) + initialCoverage;
649 return SkScalarClampMax(result, 1.0f);
robertphillips84b00882015-05-06 05:15:57 -0700650}
651
652// return true when processing is complete
halcanary9d524f22016-03-29 09:03:52 -0700653bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing,
654 SkScalar initialDepth, SkScalar initialCoverage,
655 SkScalar targetDepth, SkScalar targetCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700656 bool forceNew) {
robertphillips84b00882015-05-06 05:15:57 -0700657 bool done = false;
658
659 fCandidateVerts.rewind();
660
661 // Loop through all the points in the ring and find the intersection with the smallest depth
662 SkScalar minDist = SK_ScalarMax, minT = 0.0f;
663 int minEdgeIdx = -1;
664
665 for (int cur = 0; cur < lastRing.numPts(); ++cur) {
666 int next = (cur + 1) % lastRing.numPts();
robertphillips8c170972016-09-22 12:42:30 -0700667
668 SkScalar t;
669 bool result = intersect(this->point(lastRing.index(cur)), lastRing.bisector(cur),
670 this->point(lastRing.index(next)), lastRing.bisector(next),
671 &t);
672 if (!result) {
673 continue;
674 }
robertphillips84b00882015-05-06 05:15:57 -0700675 SkScalar dist = -t * lastRing.norm(cur).dot(lastRing.bisector(cur));
676
677 if (minDist > dist) {
678 minDist = dist;
679 minT = t;
680 minEdgeIdx = cur;
681 }
682 }
683
fmalitabd5d7e72015-06-26 07:18:24 -0700684 if (minEdgeIdx == -1) {
685 return false;
686 }
robertphillips84b00882015-05-06 05:15:57 -0700687 SkPoint newPt = lastRing.bisector(minEdgeIdx);
688 newPt.scale(minT);
689 newPt += this->point(lastRing.index(minEdgeIdx));
690
691 SkScalar depth = this->computeDepthFromEdge(lastRing.origEdgeID(minEdgeIdx), newPt);
fmalitabd5d7e72015-06-26 07:18:24 -0700692 if (depth >= targetDepth) {
robertphillips84b00882015-05-06 05:15:57 -0700693 // None of the bisectors intersect before reaching the desired depth.
694 // Just step them all to the desired depth
fmalitabd5d7e72015-06-26 07:18:24 -0700695 depth = targetDepth;
robertphillips84b00882015-05-06 05:15:57 -0700696 done = true;
697 }
698
699 // 'dst' stores where each point in the last ring maps to/transforms into
700 // in the next ring.
701 SkTDArray<int> dst;
702 dst.setCount(lastRing.numPts());
703
704 // Create the first point (who compares with no one)
705 if (!this->computePtAlongBisector(lastRing.index(0),
706 lastRing.bisector(0),
707 lastRing.origEdgeID(0),
708 depth, &newPt)) {
709 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700710 return true;
711 }
712 dst[0] = fCandidateVerts.addNewPt(newPt,
713 lastRing.index(0), lastRing.origEdgeID(0),
714 !this->movable(lastRing.index(0)));
715
716 // Handle the middle points (who only compare with the prior point)
717 for (int cur = 1; cur < lastRing.numPts()-1; ++cur) {
718 if (!this->computePtAlongBisector(lastRing.index(cur),
719 lastRing.bisector(cur),
720 lastRing.origEdgeID(cur),
721 depth, &newPt)) {
722 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700723 return true;
724 }
725 if (!duplicate_pt(newPt, fCandidateVerts.lastPoint())) {
726 dst[cur] = fCandidateVerts.addNewPt(newPt,
727 lastRing.index(cur), lastRing.origEdgeID(cur),
728 !this->movable(lastRing.index(cur)));
729 } else {
730 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
731 }
732 }
733
734 // Check on the last point (handling the wrap around)
735 int cur = lastRing.numPts()-1;
736 if (!this->computePtAlongBisector(lastRing.index(cur),
737 lastRing.bisector(cur),
738 lastRing.origEdgeID(cur),
739 depth, &newPt)) {
740 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700741 return true;
742 }
743 bool dupPrev = duplicate_pt(newPt, fCandidateVerts.lastPoint());
744 bool dupNext = duplicate_pt(newPt, fCandidateVerts.firstPoint());
745
746 if (!dupPrev && !dupNext) {
747 dst[cur] = fCandidateVerts.addNewPt(newPt,
748 lastRing.index(cur), lastRing.origEdgeID(cur),
749 !this->movable(lastRing.index(cur)));
750 } else if (dupPrev && !dupNext) {
751 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
752 } else if (!dupPrev && dupNext) {
753 dst[cur] = fCandidateVerts.fuseWithNext();
754 } else {
755 bool dupPrevVsNext = duplicate_pt(fCandidateVerts.firstPoint(), fCandidateVerts.lastPoint());
756
757 if (!dupPrevVsNext) {
758 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
759 } else {
ethannicholas0dacc672015-07-07 12:41:52 -0700760 const int fused = fCandidateVerts.fuseWithBoth();
761 dst[cur] = fused;
762 const int targetIdx = dst[cur - 1];
763 for (int i = cur - 1; i >= 0 && dst[i] == targetIdx; i--) {
764 dst[i] = fused;
765 }
robertphillips84b00882015-05-06 05:15:57 -0700766 }
767 }
768
769 // Fold the new ring's points into the global pool
770 for (int i = 0; i < fCandidateVerts.numPts(); ++i) {
771 int newIdx;
fmalitabd5d7e72015-06-26 07:18:24 -0700772 if (fCandidateVerts.needsToBeNew(i) || forceNew) {
halcanary9d524f22016-03-29 09:03:52 -0700773 // if the originating index is still valid then this point wasn't
robertphillips84b00882015-05-06 05:15:57 -0700774 // fused (and is thus movable)
halcanary9d524f22016-03-29 09:03:52 -0700775 SkScalar coverage = compute_coverage(depth, initialDepth, initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700776 targetDepth, targetCoverage);
777 newIdx = this->addPt(fCandidateVerts.point(i), depth, coverage,
ethannicholasfab4a9b2016-08-26 11:03:32 -0700778 fCandidateVerts.originatingIdx(i) != -1, kSharp_CurveState);
robertphillips84b00882015-05-06 05:15:57 -0700779 } else {
780 SkASSERT(fCandidateVerts.originatingIdx(i) != -1);
fmalitabd5d7e72015-06-26 07:18:24 -0700781 this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth,
782 targetCoverage);
robertphillips84b00882015-05-06 05:15:57 -0700783 newIdx = fCandidateVerts.originatingIdx(i);
784 }
785
786 nextRing->addIdx(newIdx, fCandidateVerts.origEdge(i));
787 }
788
789 // 'dst' currently has indices into the ring. Remap these to be indices
790 // into the global pool since the triangulation operates in that space.
791 for (int i = 0; i < dst.count(); ++i) {
792 dst[i] = nextRing->index(dst[i]);
793 }
794
robertphillips44c31282015-09-03 12:58:48 -0700795 for (int i = 0; i < lastRing.numPts(); ++i) {
796 int next = (i + 1) % lastRing.numPts();
robertphillips84b00882015-05-06 05:15:57 -0700797
robertphillips44c31282015-09-03 12:58:48 -0700798 this->addTri(lastRing.index(i), lastRing.index(next), dst[next]);
799 this->addTri(lastRing.index(i), dst[next], dst[i]);
robertphillips84b00882015-05-06 05:15:57 -0700800 }
801
robertphillips8c170972016-09-22 12:42:30 -0700802 if (done && fStyle != SkStrokeRec::kStroke_Style) {
803 // fill or stroke-and-fill
robertphillips84b00882015-05-06 05:15:57 -0700804 this->fanRing(*nextRing);
805 }
806
807 if (nextRing->numPts() < 3) {
808 done = true;
809 }
robertphillips84b00882015-05-06 05:15:57 -0700810 return done;
811}
812
813void GrAAConvexTessellator::validate() const {
robertphillips84b00882015-05-06 05:15:57 -0700814 SkASSERT(fPts.count() == fMovable.count());
Robert Phillips1d089982016-09-26 14:07:48 -0400815 SkASSERT(fPts.count() == fCoverages.count());
816 SkASSERT(fPts.count() == fCurveState.count());
robertphillips84b00882015-05-06 05:15:57 -0700817 SkASSERT(0 == (fIndices.count() % 3));
Robert Phillips1d089982016-09-26 14:07:48 -0400818 SkASSERT(!fBisectors.count() || fBisectors.count() == fNorms.count());
robertphillips84b00882015-05-06 05:15:57 -0700819}
820
821//////////////////////////////////////////////////////////////////////////////
822void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) {
823 this->computeNormals(tess);
robertphillips364ad002015-05-20 11:49:55 -0700824 this->computeBisectors(tess);
robertphillips84b00882015-05-06 05:15:57 -0700825}
826
827void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms,
828 const SkTDArray<SkVector>& bisectors) {
829 for (int i = 0; i < fPts.count(); ++i) {
830 fPts[i].fNorm = norms[i];
831 fPts[i].fBisector = bisectors[i];
832 }
833}
834
835// Compute the outward facing normal at each vertex.
836void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& tess) {
837 for (int cur = 0; cur < fPts.count(); ++cur) {
838 int next = (cur + 1) % fPts.count();
839
840 fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex);
fmalitabd5d7e72015-06-26 07:18:24 -0700841 SkPoint::Normalize(&fPts[cur].fNorm);
robertphillips84b00882015-05-06 05:15:57 -0700842 fPts[cur].fNorm.setOrthog(fPts[cur].fNorm, tess.side());
robertphillips84b00882015-05-06 05:15:57 -0700843 }
844}
845
robertphillips364ad002015-05-20 11:49:55 -0700846void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& tess) {
robertphillips84b00882015-05-06 05:15:57 -0700847 int prev = fPts.count() - 1;
848 for (int cur = 0; cur < fPts.count(); prev = cur, ++cur) {
849 fPts[cur].fBisector = fPts[cur].fNorm + fPts[prev].fNorm;
robertphillips364ad002015-05-20 11:49:55 -0700850 if (!fPts[cur].fBisector.normalize()) {
851 SkASSERT(SkPoint::kLeft_Side == tess.side() || SkPoint::kRight_Side == tess.side());
852 fPts[cur].fBisector.setOrthog(fPts[cur].fNorm, (SkPoint::Side)-tess.side());
853 SkVector other;
854 other.setOrthog(fPts[prev].fNorm, tess.side());
855 fPts[cur].fBisector += other;
856 SkAssertResult(fPts[cur].fBisector.normalize());
857 } else {
858 fPts[cur].fBisector.negate(); // make the bisector face in
859 }
fmalitabd5d7e72015-06-26 07:18:24 -0700860 }
robertphillips84b00882015-05-06 05:15:57 -0700861}
862
863//////////////////////////////////////////////////////////////////////////////
864#ifdef SK_DEBUG
865// Is this ring convex?
866bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const {
867 if (fPts.count() < 3) {
fmalitabd5d7e72015-06-26 07:18:24 -0700868 return true;
robertphillips84b00882015-05-06 05:15:57 -0700869 }
870
871 SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.top().fIndex);
872 SkPoint cur = tess.point(fPts[1].fIndex) - tess.point(fPts[0].fIndex);
873 SkScalar minDot = prev.fX * cur.fY - prev.fY * cur.fX;
874 SkScalar maxDot = minDot;
875
876 prev = cur;
877 for (int i = 1; i < fPts.count(); ++i) {
878 int next = (i + 1) % fPts.count();
879
880 cur = tess.point(fPts[next].fIndex) - tess.point(fPts[i].fIndex);
881 SkScalar dot = prev.fX * cur.fY - prev.fY * cur.fX;
882
883 minDot = SkMinScalar(minDot, dot);
884 maxDot = SkMaxScalar(maxDot, dot);
885
886 prev = cur;
887 }
888
fmalitabd5d7e72015-06-26 07:18:24 -0700889 if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) {
890 maxDot = 0;
891 }
892 if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) {
893 minDot = 0;
894 }
895 return (maxDot >= 0.0f) == (minDot >= 0.0f);
robertphillips84b00882015-05-06 05:15:57 -0700896}
897
robertphillips84b00882015-05-06 05:15:57 -0700898#endif
899
Robert Phillips1d089982016-09-26 14:07:48 -0400900void GrAAConvexTessellator::lineTo(const SkPoint& p, CurveState curve) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700901 if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) {
902 return;
903 }
904
905 SkASSERT(fPts.count() <= 1 || fPts.count() == fNorms.count()+1);
Robert Phillips1d089982016-09-26 14:07:48 -0400906 if (this->numPts() >= 2 && abs_dist_from_line(fPts.top(), fNorms.top(), p) < kClose) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700907 // The old last point is on the line from the second to last to the new point
908 this->popLastPt();
909 fNorms.pop();
ethannicholas1bcbc8f2016-01-08 14:09:18 -0800910 // double-check that the new last point is not a duplicate of the new point. In an ideal
911 // world this wouldn't be necessary (since it's only possible for non-convex paths), but
Robert Phillips1d089982016-09-26 14:07:48 -0400912 // floating point precision issues mean it can actually happen on paths that were
913 // determined to be convex.
ethannicholas1bcbc8f2016-01-08 14:09:18 -0800914 if (duplicate_pt(p, this->lastPoint())) {
915 return;
916 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700917 }
robertphillips8c170972016-09-22 12:42:30 -0700918 SkScalar initialRingCoverage = (SkStrokeRec::kFill_Style == fStyle) ? 0.5f : 1.0f;
ethannicholasfab4a9b2016-08-26 11:03:32 -0700919 this->addPt(p, 0.0f, initialRingCoverage, false, curve);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700920 if (this->numPts() > 1) {
921 *fNorms.push() = fPts.top() - fPts[fPts.count()-2];
922 SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top());
923 SkASSERT(len > 0.0f);
924 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms.top().length()));
925 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700926}
927
ethannicholasfab4a9b2016-08-26 11:03:32 -0700928void GrAAConvexTessellator::lineTo(const SkMatrix& m, SkPoint p, CurveState curve) {
fmalitabd5d7e72015-06-26 07:18:24 -0700929 m.mapPoints(&p, 1);
ethannicholasfab4a9b2016-08-26 11:03:32 -0700930 this->lineTo(p, curve);
fmalitabd5d7e72015-06-26 07:18:24 -0700931}
932
Robert Phillips1d089982016-09-26 14:07:48 -0400933void GrAAConvexTessellator::quadTo(const SkPoint pts[3]) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700934 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
935 fPointBuffer.setReserve(maxCount);
936 SkPoint* target = fPointBuffer.begin();
halcanary9d524f22016-03-29 09:03:52 -0700937 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
Robert Phillips1d089982016-09-26 14:07:48 -0400938 kQuadTolerance, &target, maxCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700939 fPointBuffer.setCount(count);
ethannicholasfab4a9b2016-08-26 11:03:32 -0700940 for (int i = 0; i < count - 1; i++) {
Robert Phillips1d089982016-09-26 14:07:48 -0400941 this->lineTo(fPointBuffer[i], kCurve_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700942 }
Robert Phillips1d089982016-09-26 14:07:48 -0400943 this->lineTo(fPointBuffer[count - 1], kIndeterminate_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700944}
945
fmalitabd5d7e72015-06-26 07:18:24 -0700946void GrAAConvexTessellator::quadTo(const SkMatrix& m, SkPoint pts[3]) {
Robert Phillips1d089982016-09-26 14:07:48 -0400947 m.mapPoints(pts, 3);
948 this->quadTo(pts);
fmalitabd5d7e72015-06-26 07:18:24 -0700949}
950
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700951void GrAAConvexTessellator::cubicTo(const SkMatrix& m, SkPoint pts[4]) {
fmalitabd5d7e72015-06-26 07:18:24 -0700952 m.mapPoints(pts, 4);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700953 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
954 fPointBuffer.setReserve(maxCount);
955 SkPoint* target = fPointBuffer.begin();
halcanary9d524f22016-03-29 09:03:52 -0700956 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700957 kCubicTolerance, &target, maxCount);
958 fPointBuffer.setCount(count);
ethannicholasfab4a9b2016-08-26 11:03:32 -0700959 for (int i = 0; i < count - 1; i++) {
Robert Phillips1d089982016-09-26 14:07:48 -0400960 this->lineTo(fPointBuffer[i], kCurve_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700961 }
Robert Phillips1d089982016-09-26 14:07:48 -0400962 this->lineTo(fPointBuffer[count - 1], kIndeterminate_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700963}
964
965// include down here to avoid compilation errors caused by "-" overload in SkGeometry.h
966#include "SkGeometry.h"
967
fmalitabd5d7e72015-06-26 07:18:24 -0700968void GrAAConvexTessellator::conicTo(const SkMatrix& m, SkPoint pts[3], SkScalar w) {
969 m.mapPoints(pts, 3);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700970 SkAutoConicToQuads quadder;
971 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
972 SkPoint lastPoint = *(quads++);
973 int count = quadder.countQuads();
974 for (int i = 0; i < count; ++i) {
975 SkPoint quadPts[3];
976 quadPts[0] = lastPoint;
977 quadPts[1] = quads[0];
978 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
Robert Phillips1d089982016-09-26 14:07:48 -0400979 this->quadTo(quadPts);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700980 lastPoint = quadPts[2];
981 quads += 2;
982 }
983}
984
robertphillips84b00882015-05-06 05:15:57 -0700985//////////////////////////////////////////////////////////////////////////////
986#if GR_AA_CONVEX_TESSELLATOR_VIZ
987static const SkScalar kPointRadius = 0.02f;
988static const SkScalar kArrowStrokeWidth = 0.0f;
989static const SkScalar kArrowLength = 0.2f;
990static const SkScalar kEdgeTextSize = 0.1f;
991static const SkScalar kPointTextSize = 0.02f;
992
993static void draw_point(SkCanvas* canvas, const SkPoint& p, SkScalar paramValue, bool stroke) {
994 SkPaint paint;
995 SkASSERT(paramValue <= 1.0f);
996 int gs = int(255*paramValue);
997 paint.setARGB(255, gs, gs, gs);
998
999 canvas->drawCircle(p.fX, p.fY, kPointRadius, paint);
1000
1001 if (stroke) {
1002 SkPaint stroke;
1003 stroke.setColor(SK_ColorYELLOW);
1004 stroke.setStyle(SkPaint::kStroke_Style);
1005 stroke.setStrokeWidth(kPointRadius/3.0f);
halcanary9d524f22016-03-29 09:03:52 -07001006 canvas->drawCircle(p.fX, p.fY, kPointRadius, stroke);
robertphillips84b00882015-05-06 05:15:57 -07001007 }
1008}
1009
1010static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkColor color) {
1011 SkPaint p;
1012 p.setColor(color);
1013
1014 canvas->drawLine(p0.fX, p0.fY, p1.fX, p1.fY, p);
1015}
1016
1017static void draw_arrow(SkCanvas*canvas, const SkPoint& p, const SkPoint &n,
1018 SkScalar len, SkColor color) {
1019 SkPaint paint;
1020 paint.setColor(color);
1021 paint.setStrokeWidth(kArrowStrokeWidth);
1022 paint.setStyle(SkPaint::kStroke_Style);
1023
1024 canvas->drawLine(p.fX, p.fY,
1025 p.fX + len * n.fX, p.fY + len * n.fY,
1026 paint);
1027}
1028
1029void GrAAConvexTessellator::Ring::draw(SkCanvas* canvas, const GrAAConvexTessellator& tess) const {
1030 SkPaint paint;
1031 paint.setTextSize(kEdgeTextSize);
1032
1033 for (int cur = 0; cur < fPts.count(); ++cur) {
1034 int next = (cur + 1) % fPts.count();
1035
1036 draw_line(canvas,
1037 tess.point(fPts[cur].fIndex),
1038 tess.point(fPts[next].fIndex),
1039 SK_ColorGREEN);
1040
1041 SkPoint mid = tess.point(fPts[cur].fIndex) + tess.point(fPts[next].fIndex);
1042 mid.scale(0.5f);
1043
1044 if (fPts.count()) {
1045 draw_arrow(canvas, mid, fPts[cur].fNorm, kArrowLength, SK_ColorRED);
1046 mid.fX += (kArrowLength/2) * fPts[cur].fNorm.fX;
1047 mid.fY += (kArrowLength/2) * fPts[cur].fNorm.fY;
1048 }
1049
1050 SkString num;
1051 num.printf("%d", this->origEdgeID(cur));
Cary Clark2a475ea2017-04-28 15:35:12 -04001052 canvas->drawString(num, mid.fX, mid.fY, paint);
robertphillips84b00882015-05-06 05:15:57 -07001053
1054 if (fPts.count()) {
1055 draw_arrow(canvas, tess.point(fPts[cur].fIndex), fPts[cur].fBisector,
1056 kArrowLength, SK_ColorBLUE);
1057 }
halcanary9d524f22016-03-29 09:03:52 -07001058 }
robertphillips84b00882015-05-06 05:15:57 -07001059}
1060
1061void GrAAConvexTessellator::draw(SkCanvas* canvas) const {
1062 for (int i = 0; i < fIndices.count(); i += 3) {
1063 SkASSERT(fIndices[i] < this->numPts()) ;
1064 SkASSERT(fIndices[i+1] < this->numPts()) ;
1065 SkASSERT(fIndices[i+2] < this->numPts()) ;
1066
1067 draw_line(canvas,
1068 this->point(this->fIndices[i]), this->point(this->fIndices[i+1]),
1069 SK_ColorBLACK);
1070 draw_line(canvas,
1071 this->point(this->fIndices[i+1]), this->point(this->fIndices[i+2]),
1072 SK_ColorBLACK);
1073 draw_line(canvas,
1074 this->point(this->fIndices[i+2]), this->point(this->fIndices[i]),
1075 SK_ColorBLACK);
1076 }
1077
1078 fInitialRing.draw(canvas, *this);
1079 for (int i = 0; i < fRings.count(); ++i) {
1080 fRings[i]->draw(canvas, *this);
1081 }
1082
1083 for (int i = 0; i < this->numPts(); ++i) {
1084 draw_point(canvas,
halcanary9d524f22016-03-29 09:03:52 -07001085 this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)),
robertphillips84b00882015-05-06 05:15:57 -07001086 !this->movable(i));
1087
1088 SkPaint paint;
1089 paint.setTextSize(kPointTextSize);
1090 paint.setTextAlign(SkPaint::kCenter_Align);
fmalitabd5d7e72015-06-26 07:18:24 -07001091 if (this->depth(i) <= -kAntialiasingRadius) {
robertphillips84b00882015-05-06 05:15:57 -07001092 paint.setColor(SK_ColorWHITE);
1093 }
1094
1095 SkString num;
1096 num.printf("%d", i);
Cary Clark2a475ea2017-04-28 15:35:12 -04001097 canvas->drawString(num,
halcanary9d524f22016-03-29 09:03:52 -07001098 this->point(i).fX, this->point(i).fY+(kPointRadius/2.0f),
robertphillips84b00882015-05-06 05:15:57 -07001099 paint);
1100 }
1101}
1102
1103#endif