blob: 9952491b64b4139e067c2f4ad8db82bece246c28 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkPath.h"
10#include "include/core/SkPoint.h"
11#include "include/core/SkString.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040012#include "src/gpu/geometry/GrPathUtils.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/gpu/ops/GrAAConvexTessellator.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;
Brian Osmane3deee12018-11-20 11:10:15 -050027static const SkScalar kConicTolerance = 0.25f;
fmalitabd5d7e72015-06-26 07:18:24 -070028
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) {
Cary Clarkdf429f32017-11-08 11:44:31 -050058 SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1);
robertphillips84b00882015-05-06 05:15:57 -070059 return distSq < kCloseSqd;
60}
61
Brian Salomon0235c642018-08-31 12:04:18 -040062static bool points_are_colinear_and_b_is_middle(const SkPoint& a, const SkPoint& b,
63 const SkPoint& c) {
64 // 'area' is twice the area of the triangle with corners a, b, and c.
65 SkScalar area = a.fX * (b.fY - c.fY) + b.fX * (c.fY - a.fY) + c.fX * (a.fY - b.fY);
66 if (SkScalarAbs(area) >= 2 * kCloseSqd) {
67 return false;
68 }
69 return (a - b).dot(b - c) >= 0;
robertphillips84b00882015-05-06 05:15:57 -070070}
71
72int GrAAConvexTessellator::addPt(const SkPoint& pt,
73 SkScalar depth,
fmalitabd5d7e72015-06-26 07:18:24 -070074 SkScalar coverage,
ethannicholas1a1b3ac2015-06-10 12:11:17 -070075 bool movable,
ethannicholasfab4a9b2016-08-26 11:03:32 -070076 CurveState curve) {
Brian Salomonefa6bcb2018-09-11 13:19:19 -040077 SkASSERT(pt.isFinite());
robertphillips84b00882015-05-06 05:15:57 -070078 this->validate();
79
80 int index = fPts.count();
81 *fPts.push() = pt;
fmalitabd5d7e72015-06-26 07:18:24 -070082 *fCoverages.push() = coverage;
robertphillips84b00882015-05-06 05:15:57 -070083 *fMovable.push() = movable;
ethannicholasfab4a9b2016-08-26 11:03:32 -070084 *fCurveState.push() = curve;
robertphillips84b00882015-05-06 05:15:57 -070085
86 this->validate();
87 return index;
88}
89
90void GrAAConvexTessellator::popLastPt() {
91 this->validate();
92
93 fPts.pop();
fmalitabd5d7e72015-06-26 07:18:24 -070094 fCoverages.pop();
robertphillips84b00882015-05-06 05:15:57 -070095 fMovable.pop();
Robert Phillips1d089982016-09-26 14:07:48 -040096 fCurveState.pop();
robertphillips84b00882015-05-06 05:15:57 -070097
98 this->validate();
99}
100
101void GrAAConvexTessellator::popFirstPtShuffle() {
102 this->validate();
103
104 fPts.removeShuffle(0);
fmalitabd5d7e72015-06-26 07:18:24 -0700105 fCoverages.removeShuffle(0);
robertphillips84b00882015-05-06 05:15:57 -0700106 fMovable.removeShuffle(0);
Robert Phillips1d089982016-09-26 14:07:48 -0400107 fCurveState.removeShuffle(0);
robertphillips84b00882015-05-06 05:15:57 -0700108
109 this->validate();
110}
111
112void GrAAConvexTessellator::updatePt(int index,
113 const SkPoint& pt,
fmalitabd5d7e72015-06-26 07:18:24 -0700114 SkScalar depth,
115 SkScalar coverage) {
robertphillips84b00882015-05-06 05:15:57 -0700116 this->validate();
117 SkASSERT(fMovable[index]);
118
119 fPts[index] = pt;
fmalitabd5d7e72015-06-26 07:18:24 -0700120 fCoverages[index] = coverage;
robertphillips84b00882015-05-06 05:15:57 -0700121}
122
123void GrAAConvexTessellator::addTri(int i0, int i1, int i2) {
124 if (i0 == i1 || i1 == i2 || i2 == i0) {
125 return;
126 }
127
128 *fIndices.push() = i0;
129 *fIndices.push() = i1;
130 *fIndices.push() = i2;
131}
132
133void GrAAConvexTessellator::rewind() {
134 fPts.rewind();
fmalitabd5d7e72015-06-26 07:18:24 -0700135 fCoverages.rewind();
robertphillips84b00882015-05-06 05:15:57 -0700136 fMovable.rewind();
137 fIndices.rewind();
138 fNorms.rewind();
Robert Phillips1d089982016-09-26 14:07:48 -0400139 fCurveState.rewind();
robertphillips84b00882015-05-06 05:15:57 -0700140 fInitialRing.rewind();
141 fCandidateVerts.rewind();
142#if GR_AA_CONVEX_TESSELLATOR_VIZ
143 fRings.rewind(); // TODO: leak in this case!
144#else
145 fRings[0].rewind();
146 fRings[1].rewind();
147#endif
148}
149
Brian Salomon0235c642018-08-31 12:04:18 -0400150void GrAAConvexTessellator::computeNormals() {
151 auto normalToVector = [this](SkVector v) {
152 SkVector n = SkPointPriv::MakeOrthog(v, fSide);
153 SkAssertResult(n.normalize());
154 SkASSERT(SkScalarNearlyEqual(1.0f, n.length()));
155 return n;
156 };
157
158 // Check the cross product of the final trio
159 fNorms.append(fPts.count());
160 fNorms[0] = fPts[1] - fPts[0];
161 fNorms.top() = fPts[0] - fPts.top();
162 SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.top());
163 fSide = (cross > 0.0f) ? SkPointPriv::kRight_Side : SkPointPriv::kLeft_Side;
164 fNorms[0] = normalToVector(fNorms[0]);
165 for (int cur = 1; cur < fNorms.count() - 1; ++cur) {
166 fNorms[cur] = normalToVector(fPts[cur + 1] - fPts[cur]);
167 }
168 fNorms.top() = normalToVector(fNorms.top());
169}
170
robertphillips84b00882015-05-06 05:15:57 -0700171void GrAAConvexTessellator::computeBisectors() {
172 fBisectors.setCount(fNorms.count());
173
174 int prev = fBisectors.count() - 1;
175 for (int cur = 0; cur < fBisectors.count(); prev = cur, ++cur) {
176 fBisectors[cur] = fNorms[cur] + fNorms[prev];
robertphillips364ad002015-05-20 11:49:55 -0700177 if (!fBisectors[cur].normalize()) {
Brian Salomon0235c642018-08-31 12:04:18 -0400178 fBisectors[cur] = SkPointPriv::MakeOrthog(fNorms[cur], (SkPointPriv::Side)-fSide) +
179 SkPointPriv::MakeOrthog(fNorms[prev], fSide);
halcanary9d524f22016-03-29 09:03:52 -0700180 SkAssertResult(fBisectors[cur].normalize());
robertphillips364ad002015-05-20 11:49:55 -0700181 } else {
182 fBisectors[cur].negate(); // make the bisector face in
183 }
ethannicholasfab4a9b2016-08-26 11:03:32 -0700184 if (fCurveState[prev] == kIndeterminate_CurveState) {
185 if (fCurveState[cur] == kSharp_CurveState) {
186 fCurveState[prev] = kSharp_CurveState;
187 } else {
188 if (SkScalarAbs(fNorms[cur].dot(fNorms[prev])) > kCurveConnectionThreshold) {
189 fCurveState[prev] = kCurve_CurveState;
190 fCurveState[cur] = kCurve_CurveState;
191 } else {
192 fCurveState[prev] = kSharp_CurveState;
193 fCurveState[cur] = kSharp_CurveState;
194 }
195 }
196 }
robertphillips84b00882015-05-06 05:15:57 -0700197
198 SkASSERT(SkScalarNearlyEqual(1.0f, fBisectors[cur].length()));
199 }
200}
201
fmalitabd5d7e72015-06-26 07:18:24 -0700202// Create as many rings as we need to (up to a predefined limit) to reach the specified target
203// depth. If we are in fill mode, the final ring will automatically be fanned.
204bool GrAAConvexTessellator::createInsetRings(Ring& previousRing, SkScalar initialDepth,
halcanary9d524f22016-03-29 09:03:52 -0700205 SkScalar initialCoverage, SkScalar targetDepth,
fmalitabd5d7e72015-06-26 07:18:24 -0700206 SkScalar targetCoverage, Ring** finalRing) {
207 static const int kMaxNumRings = 8;
208
209 if (previousRing.numPts() < 3) {
210 return false;
211 }
212 Ring* currentRing = &previousRing;
213 int i;
214 for (i = 0; i < kMaxNumRings; ++i) {
215 Ring* nextRing = this->getNextRing(currentRing);
216 SkASSERT(nextRing != currentRing);
217
halcanary9d524f22016-03-29 09:03:52 -0700218 bool done = this->createInsetRing(*currentRing, nextRing, initialDepth, initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700219 targetDepth, targetCoverage, i == 0);
220 currentRing = nextRing;
221 if (done) {
222 break;
223 }
224 currentRing->init(*this);
225 }
226
227 if (kMaxNumRings == i) {
228 // Bail if we've exceeded the amount of time we want to throw at this.
229 this->terminate(*currentRing);
230 return false;
231 }
232 bool done = currentRing->numPts() >= 3;
233 if (done) {
234 currentRing->init(*this);
235 }
236 *finalRing = currentRing;
237 return done;
238}
239
robertphillips84b00882015-05-06 05:15:57 -0700240// The general idea here is to, conceptually, start with the original polygon and slide
241// the vertices along the bisectors until the first intersection. At that
242// point two of the edges collapse and the process repeats on the new polygon.
243// The polygon state is captured in the Ring class while the GrAAConvexTessellator
244// controls the iteration. The CandidateVerts holds the formative points for the
245// next ring.
246bool GrAAConvexTessellator::tessellate(const SkMatrix& m, const SkPath& path) {
robertphillips84b00882015-05-06 05:15:57 -0700247 if (!this->extractFromPath(m, path)) {
248 return false;
249 }
250
fmalitabd5d7e72015-06-26 07:18:24 -0700251 SkScalar coverage = 1.0f;
ethannicholasfea77632015-08-19 12:09:12 -0700252 SkScalar scaleFactor = 0.0f;
robertphillips8c170972016-09-22 12:42:30 -0700253
254 if (SkStrokeRec::kStrokeAndFill_Style == fStyle) {
255 SkASSERT(m.isSimilarity());
256 scaleFactor = m.getMaxScale(); // x and y scale are the same
257 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
258 Ring outerStrokeAndAARing;
259 this->createOuterRing(fInitialRing,
260 effectiveStrokeWidth / 2 + kAntialiasingRadius, 0.0,
261 &outerStrokeAndAARing);
262
263 // discard all the triangles added between the originating ring and the new outer ring
264 fIndices.rewind();
265
266 outerStrokeAndAARing.init(*this);
267
268 outerStrokeAndAARing.makeOriginalRing();
269
270 // Add the outer stroke ring's normals to the originating ring's normals
271 // so it can also act as an originating ring
272 fNorms.setCount(fNorms.count() + outerStrokeAndAARing.numPts());
273 for (int i = 0; i < outerStrokeAndAARing.numPts(); ++i) {
274 SkASSERT(outerStrokeAndAARing.index(i) < fNorms.count());
275 fNorms[outerStrokeAndAARing.index(i)] = outerStrokeAndAARing.norm(i);
276 }
277
278 // the bisectors are only needed for the computation of the outer ring
279 fBisectors.rewind();
280
281 Ring* insetAARing;
282 this->createInsetRings(outerStrokeAndAARing,
283 0.0f, 0.0f, 2*kAntialiasingRadius, 1.0f,
284 &insetAARing);
285
286 SkDEBUGCODE(this->validate();)
287 return true;
288 }
289
290 if (SkStrokeRec::kStroke_Style == fStyle) {
291 SkASSERT(fStrokeWidth >= 0.0f);
halcanary9d524f22016-03-29 09:03:52 -0700292 SkASSERT(m.isSimilarity());
ethannicholasfea77632015-08-19 12:09:12 -0700293 scaleFactor = m.getMaxScale(); // x and y scale are the same
294 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
fmalitabd5d7e72015-06-26 07:18:24 -0700295 Ring outerStrokeRing;
halcanary9d524f22016-03-29 09:03:52 -0700296 this->createOuterRing(fInitialRing, effectiveStrokeWidth / 2 - kAntialiasingRadius,
ethannicholasfea77632015-08-19 12:09:12 -0700297 coverage, &outerStrokeRing);
fmalitabd5d7e72015-06-26 07:18:24 -0700298 outerStrokeRing.init(*this);
299 Ring outerAARing;
300 this->createOuterRing(outerStrokeRing, kAntialiasingRadius * 2, 0.0f, &outerAARing);
301 } else {
302 Ring outerAARing;
303 this->createOuterRing(fInitialRing, kAntialiasingRadius, 0.0f, &outerAARing);
304 }
robertphillips84b00882015-05-06 05:15:57 -0700305
306 // the bisectors are only needed for the computation of the outer ring
307 fBisectors.rewind();
robertphillips8c170972016-09-22 12:42:30 -0700308 if (SkStrokeRec::kStroke_Style == fStyle && fInitialRing.numPts() > 2) {
309 SkASSERT(fStrokeWidth >= 0.0f);
ethannicholasfea77632015-08-19 12:09:12 -0700310 SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth;
fmalitabd5d7e72015-06-26 07:18:24 -0700311 Ring* insetStrokeRing;
ethannicholasfea77632015-08-19 12:09:12 -0700312 SkScalar strokeDepth = effectiveStrokeWidth / 2 - kAntialiasingRadius;
halcanary9d524f22016-03-29 09:03:52 -0700313 if (this->createInsetRings(fInitialRing, 0.0f, coverage, strokeDepth, coverage,
robertphillips8c170972016-09-22 12:42:30 -0700314 &insetStrokeRing)) {
fmalitabd5d7e72015-06-26 07:18:24 -0700315 Ring* insetAARing;
halcanary9d524f22016-03-29 09:03:52 -0700316 this->createInsetRings(*insetStrokeRing, strokeDepth, coverage, strokeDepth +
robertphillips8c170972016-09-22 12:42:30 -0700317 kAntialiasingRadius * 2, 0.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700318 }
fmalitabd5d7e72015-06-26 07:18:24 -0700319 } else {
320 Ring* insetAARing;
321 this->createInsetRings(fInitialRing, 0.0f, 0.5f, kAntialiasingRadius, 1.0f, &insetAARing);
robertphillips84b00882015-05-06 05:15:57 -0700322 }
323
fmalitabd5d7e72015-06-26 07:18:24 -0700324 SkDEBUGCODE(this->validate();)
robertphillips84b00882015-05-06 05:15:57 -0700325 return true;
326}
327
328SkScalar GrAAConvexTessellator::computeDepthFromEdge(int edgeIdx, const SkPoint& p) const {
329 SkASSERT(edgeIdx < fNorms.count());
330
331 SkPoint v = p - fPts[edgeIdx];
332 SkScalar depth = -fNorms[edgeIdx].dot(v);
robertphillips84b00882015-05-06 05:15:57 -0700333 return depth;
334}
335
336// Find a point that is 'desiredDepth' away from the 'edgeIdx'-th edge and lies
337// along the 'bisector' from the 'startIdx'-th point.
338bool GrAAConvexTessellator::computePtAlongBisector(int startIdx,
339 const SkVector& bisector,
340 int edgeIdx,
341 SkScalar desiredDepth,
342 SkPoint* result) const {
343 const SkPoint& norm = fNorms[edgeIdx];
344
345 // First find the point where the edge and the bisector intersect
346 SkPoint newP;
fmalitabd5d7e72015-06-26 07:18:24 -0700347
robertphillips84b00882015-05-06 05:15:57 -0700348 SkScalar t = perp_intersect(fPts[startIdx], bisector, fPts[edgeIdx], norm);
349 if (SkScalarNearlyEqual(t, 0.0f)) {
350 // the start point was one of the original ring points
fmalitabd5d7e72015-06-26 07:18:24 -0700351 SkASSERT(startIdx < fPts.count());
robertphillips84b00882015-05-06 05:15:57 -0700352 newP = fPts[startIdx];
fmalitabd5d7e72015-06-26 07:18:24 -0700353 } else if (t < 0.0f) {
robertphillips84b00882015-05-06 05:15:57 -0700354 newP = bisector;
355 newP.scale(t);
356 newP += fPts[startIdx];
357 } else {
358 return false;
359 }
360
361 // Then offset along the bisector from that point the correct distance
fmalitabd5d7e72015-06-26 07:18:24 -0700362 SkScalar dot = bisector.dot(norm);
363 t = -desiredDepth / dot;
robertphillips84b00882015-05-06 05:15:57 -0700364 *result = bisector;
365 result->scale(t);
366 *result += newP;
367
robertphillips84b00882015-05-06 05:15:57 -0700368 return true;
369}
370
371bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& path) {
Mike Reed4241f5e2019-09-14 19:13:23 +0000372 SkASSERT(SkPath::kConvex_Convexity == path.getConvexity());
robertphillips84b00882015-05-06 05:15:57 -0700373
Brian Salomonefa6bcb2018-09-11 13:19:19 -0400374 SkRect bounds = path.getBounds();
375 m.mapRect(&bounds);
376 if (!bounds.isFinite()) {
377 // We could do something smarter here like clip the path based on the bounds of the dst.
378 // We'd have to be careful about strokes to ensure we don't draw something wrong.
379 return false;
380 }
381
robertphillips84b00882015-05-06 05:15:57 -0700382 // Outer ring: 3*numPts
383 // Middle ring: numPts
384 // Presumptive inner ring: numPts
385 this->reservePts(5*path.countPoints());
386 // Outer ring: 12*numPts
387 // Middle ring: 0
388 // Presumptive inner ring: 6*numPts + 6
389 fIndices.setReserve(18*path.countPoints() + 6);
390
robertphillips84b00882015-05-06 05:15:57 -0700391 // TODO: is there a faster way to extract the points from the path? Perhaps
392 // get all the points via a new entry point, transform them all in bulk
393 // and then walk them to find duplicates?
Mike Reed5f152f02019-08-19 14:40:16 -0400394 SkPathEdgeIter iter(path);
395 while (auto e = iter.next()) {
396 switch (e.fEdge) {
397 case SkPathEdgeIter::Edge::kLine:
398 if (!SkPathPriv::AllPointsEq(e.fPts, 2)) {
399 this->lineTo(m, e.fPts[1], kSharp_CurveState);
Mike Reedba7e9a62019-08-16 13:30:34 -0400400 }
robertphillips84b00882015-05-06 05:15:57 -0700401 break;
Mike Reed5f152f02019-08-19 14:40:16 -0400402 case SkPathEdgeIter::Edge::kQuad:
403 if (!SkPathPriv::AllPointsEq(e.fPts, 3)) {
404 this->quadTo(m, e.fPts);
Mike Reedba7e9a62019-08-16 13:30:34 -0400405 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700406 break;
Mike Reed5f152f02019-08-19 14:40:16 -0400407 case SkPathEdgeIter::Edge::kCubic:
408 if (!SkPathPriv::AllPointsEq(e.fPts, 4)) {
409 this->cubicTo(m, e.fPts);
Mike Reedba7e9a62019-08-16 13:30:34 -0400410 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700411 break;
Mike Reed5f152f02019-08-19 14:40:16 -0400412 case SkPathEdgeIter::Edge::kConic:
413 if (!SkPathPriv::AllPointsEq(e.fPts, 3)) {
414 this->conicTo(m, e.fPts, iter.conicWeight());
Mike Reedba7e9a62019-08-16 13:30:34 -0400415 }
robertphillips84b00882015-05-06 05:15:57 -0700416 break;
robertphillips84b00882015-05-06 05:15:57 -0700417 }
418 }
419
fmalitabd5d7e72015-06-26 07:18:24 -0700420 if (this->numPts() < 2) {
robertphillips84b00882015-05-06 05:15:57 -0700421 return false;
422 }
423
424 // check if last point is a duplicate of the first point. If so, remove it.
425 if (duplicate_pt(fPts[this->numPts()-1], fPts[0])) {
426 this->popLastPt();
robertphillips84b00882015-05-06 05:15:57 -0700427 }
428
Brian Salomon0235c642018-08-31 12:04:18 -0400429 // Remove any lingering colinear points where the path wraps around
430 bool noRemovalsToDo = false;
431 while (!noRemovalsToDo && this->numPts() >= 3) {
432 if (points_are_colinear_and_b_is_middle(fPts[fPts.count() - 2], fPts.top(), fPts[0])) {
fmalitabd5d7e72015-06-26 07:18:24 -0700433 this->popLastPt();
Brian Salomon0235c642018-08-31 12:04:18 -0400434 } else if (points_are_colinear_and_b_is_middle(fPts.top(), fPts[0], fPts[1])) {
435 this->popFirstPtShuffle();
fmalitabd5d7e72015-06-26 07:18:24 -0700436 } else {
Brian Salomon0235c642018-08-31 12:04:18 -0400437 noRemovalsToDo = true;
fmalitabd5d7e72015-06-26 07:18:24 -0700438 }
Brian Salomon0235c642018-08-31 12:04:18 -0400439 }
fmalitabd5d7e72015-06-26 07:18:24 -0700440
Brian Salomon0235c642018-08-31 12:04:18 -0400441 // Compute the normals and bisectors.
442 SkASSERT(fNorms.empty());
443 if (this->numPts() >= 3) {
444 this->computeNormals();
fmalitabd5d7e72015-06-26 07:18:24 -0700445 this->computeBisectors();
446 } else if (this->numPts() == 2) {
halcanary9d524f22016-03-29 09:03:52 -0700447 // We've got two points, so we're degenerate.
robertphillips8c170972016-09-22 12:42:30 -0700448 if (fStyle == SkStrokeRec::kFill_Style) {
fmalitabd5d7e72015-06-26 07:18:24 -0700449 // it's a fill, so we don't need to worry about degenerate paths
450 return false;
451 }
452 // For stroking, we still need to process the degenerate path, so fix it up
Cary Clarkdf429f32017-11-08 11:44:31 -0500453 fSide = SkPointPriv::kLeft_Side;
fmalitabd5d7e72015-06-26 07:18:24 -0700454
Brian Salomon0235c642018-08-31 12:04:18 -0400455 fNorms.append(2);
456 fNorms[0] = SkPointPriv::MakeOrthog(fPts[1] - fPts[0], fSide);
457 fNorms[0].normalize();
458 fNorms[1] = -fNorms[0];
459 SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[0].length()));
fmalitabd5d7e72015-06-26 07:18:24 -0700460 // we won't actually use the bisectors, so just push zeroes
Mike Reed5edcd312018-08-08 11:23:41 -0400461 fBisectors.push_back(SkPoint::Make(0.0, 0.0));
462 fBisectors.push_back(SkPoint::Make(0.0, 0.0));
fmalitabd5d7e72015-06-26 07:18:24 -0700463 } else {
robertphillips84b00882015-05-06 05:15:57 -0700464 return false;
465 }
466
robertphillips84b00882015-05-06 05:15:57 -0700467 fCandidateVerts.setReserve(this->numPts());
468 fInitialRing.setReserve(this->numPts());
469 for (int i = 0; i < this->numPts(); ++i) {
470 fInitialRing.addIdx(i, i);
471 }
472 fInitialRing.init(fNorms, fBisectors);
473
474 this->validate();
475 return true;
476}
477
478GrAAConvexTessellator::Ring* GrAAConvexTessellator::getNextRing(Ring* lastRing) {
479#if GR_AA_CONVEX_TESSELLATOR_VIZ
halcanary385fe4d2015-08-26 13:07:48 -0700480 Ring* ring = *fRings.push() = new Ring;
robertphillips84b00882015-05-06 05:15:57 -0700481 ring->setReserve(fInitialRing.numPts());
482 ring->rewind();
483 return ring;
484#else
485 // Flip flop back and forth between fRings[0] & fRings[1]
486 int nextRing = (lastRing == &fRings[0]) ? 1 : 0;
487 fRings[nextRing].setReserve(fInitialRing.numPts());
488 fRings[nextRing].rewind();
489 return &fRings[nextRing];
490#endif
491}
492
493void GrAAConvexTessellator::fanRing(const Ring& ring) {
494 // fan out from point 0
fmalitabd5d7e72015-06-26 07:18:24 -0700495 int startIdx = ring.index(0);
496 for (int cur = ring.numPts() - 2; cur >= 0; --cur) {
497 this->addTri(startIdx, ring.index(cur), ring.index(cur + 1));
robertphillips84b00882015-05-06 05:15:57 -0700498 }
499}
500
halcanary9d524f22016-03-29 09:03:52 -0700501void GrAAConvexTessellator::createOuterRing(const Ring& previousRing, SkScalar outset,
fmalitabd5d7e72015-06-26 07:18:24 -0700502 SkScalar coverage, Ring* nextRing) {
503 const int numPts = previousRing.numPts();
504 if (numPts == 0) {
505 return;
506 }
robertphillips84b00882015-05-06 05:15:57 -0700507
robertphillips84b00882015-05-06 05:15:57 -0700508 int prev = numPts - 1;
fmalitabd5d7e72015-06-26 07:18:24 -0700509 int lastPerpIdx = -1, firstPerpIdx = -1;
510
Mike Reed8be952a2017-02-13 20:44:33 -0500511 const SkScalar outsetSq = outset * outset;
512 SkScalar miterLimitSq = outset * fMiterLimit;
513 miterLimitSq = miterLimitSq * miterLimitSq;
robertphillips84b00882015-05-06 05:15:57 -0700514 for (int cur = 0; cur < numPts; ++cur) {
fmalitabd5d7e72015-06-26 07:18:24 -0700515 int originalIdx = previousRing.index(cur);
halcanary9d524f22016-03-29 09:03:52 -0700516 // For each vertex of the original polygon we add at least two points to the
fmalitabd5d7e72015-06-26 07:18:24 -0700517 // outset polygon - one extending perpendicular to each impinging edge. Connecting these
halcanary9d524f22016-03-29 09:03:52 -0700518 // two points yields a bevel join. We need one additional point for a mitered join, and
fmalitabd5d7e72015-06-26 07:18:24 -0700519 // a round join requires one or more points depending upon curvature.
robertphillips84b00882015-05-06 05:15:57 -0700520
fmalitabd5d7e72015-06-26 07:18:24 -0700521 // The perpendicular point for the last edge
522 SkPoint normal1 = previousRing.norm(prev);
523 SkPoint perp1 = normal1;
524 perp1.scale(outset);
525 perp1 += this->point(originalIdx);
robertphillips84b00882015-05-06 05:15:57 -0700526
fmalitabd5d7e72015-06-26 07:18:24 -0700527 // The perpendicular point for the next edge.
528 SkPoint normal2 = previousRing.norm(cur);
529 SkPoint perp2 = normal2;
530 perp2.scale(outset);
531 perp2 += fPts[originalIdx];
robertphillips84b00882015-05-06 05:15:57 -0700532
ethannicholasfab4a9b2016-08-26 11:03:32 -0700533 CurveState curve = fCurveState[originalIdx];
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700534
fmalitabd5d7e72015-06-26 07:18:24 -0700535 // We know it isn't a duplicate of the prior point (since it and this
536 // one are just perpendicular offsets from the non-merged polygon points)
ethannicholasfab4a9b2016-08-26 11:03:32 -0700537 int perp1Idx = this->addPt(perp1, -outset, coverage, false, curve);
fmalitabd5d7e72015-06-26 07:18:24 -0700538 nextRing->addIdx(perp1Idx, originalIdx);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700539
fmalitabd5d7e72015-06-26 07:18:24 -0700540 int perp2Idx;
541 // For very shallow angles all the corner points could fuse.
542 if (duplicate_pt(perp2, this->point(perp1Idx))) {
543 perp2Idx = perp1Idx;
544 } else {
ethannicholasfab4a9b2016-08-26 11:03:32 -0700545 perp2Idx = this->addPt(perp2, -outset, coverage, false, curve);
robertphillips84b00882015-05-06 05:15:57 -0700546 }
ethannicholas2436f192015-06-25 14:42:34 -0700547
fmalitabd5d7e72015-06-26 07:18:24 -0700548 if (perp2Idx != perp1Idx) {
ethannicholasfab4a9b2016-08-26 11:03:32 -0700549 if (curve == kCurve_CurveState) {
fmalitabd5d7e72015-06-26 07:18:24 -0700550 // bevel or round depending upon curvature
551 SkScalar dotProd = normal1.dot(normal2);
552 if (dotProd < kRoundCapThreshold) {
553 // Currently we "round" by creating a single extra point, which produces
554 // good results for common cases. For thick strokes with high curvature, we will
555 // need to add more points; for the time being we simply fall back to software
556 // rendering for thick strokes.
557 SkPoint miter = previousRing.bisector(cur);
558 miter.setLength(-outset);
559 miter += fPts[originalIdx];
reed9730f4a2015-06-26 05:06:43 -0700560
fmalitabd5d7e72015-06-26 07:18:24 -0700561 // For very shallow angles all the corner points could fuse
562 if (!duplicate_pt(miter, this->point(perp1Idx))) {
563 int miterIdx;
ethannicholasfab4a9b2016-08-26 11:03:32 -0700564 miterIdx = this->addPt(miter, -outset, coverage, false, kSharp_CurveState);
fmalitabd5d7e72015-06-26 07:18:24 -0700565 nextRing->addIdx(miterIdx, originalIdx);
566 // The two triangles for the corner
567 this->addTri(originalIdx, perp1Idx, miterIdx);
568 this->addTri(originalIdx, miterIdx, perp2Idx);
569 }
570 } else {
571 this->addTri(originalIdx, perp1Idx, perp2Idx);
572 }
reed9730f4a2015-06-26 05:06:43 -0700573 } else {
fmalitabd5d7e72015-06-26 07:18:24 -0700574 switch (fJoin) {
575 case SkPaint::Join::kMiter_Join: {
576 // The bisector outset point
577 SkPoint miter = previousRing.bisector(cur);
578 SkScalar dotProd = normal1.dot(normal2);
Brian Salomon776a4112018-09-13 16:39:12 -0400579 // The max is because this could go slightly negative if precision causes
580 // us to become slightly concave.
581 SkScalar sinHalfAngleSq = SkTMax(SkScalarHalf(SK_Scalar1 + dotProd), 0.f);
Greg Danielba316402018-04-03 11:27:50 -0400582 SkScalar lengthSq = sk_ieee_float_divide(outsetSq, sinHalfAngleSq);
fmalitabd5d7e72015-06-26 07:18:24 -0700583 if (lengthSq > miterLimitSq) {
584 // just bevel it
585 this->addTri(originalIdx, perp1Idx, perp2Idx);
586 break;
587 }
588 miter.setLength(-SkScalarSqrt(lengthSq));
589 miter += fPts[originalIdx];
590
591 // For very shallow angles all the corner points could fuse
592 if (!duplicate_pt(miter, this->point(perp1Idx))) {
593 int miterIdx;
Ben Wagner63fd7602017-10-09 15:45:33 -0400594 miterIdx = this->addPt(miter, -outset, coverage, false,
ethannicholasfab4a9b2016-08-26 11:03:32 -0700595 kSharp_CurveState);
fmalitabd5d7e72015-06-26 07:18:24 -0700596 nextRing->addIdx(miterIdx, originalIdx);
597 // The two triangles for the corner
598 this->addTri(originalIdx, perp1Idx, miterIdx);
599 this->addTri(originalIdx, miterIdx, perp2Idx);
Brian Salomon20ea47a2018-09-04 11:38:28 -0400600 } else {
601 // ignore the miter point as it's so close to perp1/perp2 and simply
602 // bevel.
603 this->addTri(originalIdx, perp1Idx, perp2Idx);
fmalitabd5d7e72015-06-26 07:18:24 -0700604 }
605 break;
606 }
607 case SkPaint::Join::kBevel_Join:
608 this->addTri(originalIdx, perp1Idx, perp2Idx);
609 break;
610 default:
halcanary9d524f22016-03-29 09:03:52 -0700611 // kRound_Join is unsupported for now. GrAALinearizingConvexPathRenderer is
fmalitabd5d7e72015-06-26 07:18:24 -0700612 // only willing to draw mitered or beveled, so we should never get here.
613 SkASSERT(false);
614 }
reed9730f4a2015-06-26 05:06:43 -0700615 }
616
fmalitabd5d7e72015-06-26 07:18:24 -0700617 nextRing->addIdx(perp2Idx, originalIdx);
ethannicholas2436f192015-06-25 14:42:34 -0700618 }
fmalitabd5d7e72015-06-26 07:18:24 -0700619
620 if (0 == cur) {
621 // Store the index of the first perpendicular point to finish up
622 firstPerpIdx = perp1Idx;
623 SkASSERT(-1 == lastPerpIdx);
624 } else {
625 // The triangles for the previous edge
626 int prevIdx = previousRing.index(prev);
627 this->addTri(prevIdx, perp1Idx, originalIdx);
628 this->addTri(prevIdx, lastPerpIdx, perp1Idx);
629 }
630
631 // Track the last perpendicular outset point so we can construct the
632 // trailing edge triangles.
633 lastPerpIdx = perp2Idx;
634 prev = cur;
robertphillips84b00882015-05-06 05:15:57 -0700635 }
636
637 // pick up the final edge rect
fmalitabd5d7e72015-06-26 07:18:24 -0700638 int lastIdx = previousRing.index(numPts - 1);
639 this->addTri(lastIdx, firstPerpIdx, previousRing.index(0));
640 this->addTri(lastIdx, lastPerpIdx, firstPerpIdx);
robertphillips84b00882015-05-06 05:15:57 -0700641
642 this->validate();
643}
644
fmalitabd5d7e72015-06-26 07:18:24 -0700645// Something went wrong in the creation of the next ring. If we're filling the shape, just go ahead
646// and fan it.
robertphillips84b00882015-05-06 05:15:57 -0700647void GrAAConvexTessellator::terminate(const Ring& ring) {
Adrienne Walker890a8cc2018-04-25 15:44:02 -0700648 if (fStyle != SkStrokeRec::kStroke_Style && ring.numPts() > 0) {
fmalitabd5d7e72015-06-26 07:18:24 -0700649 this->fanRing(ring);
robertphillips84b00882015-05-06 05:15:57 -0700650 }
fmalitabd5d7e72015-06-26 07:18:24 -0700651}
robertphillips84b00882015-05-06 05:15:57 -0700652
halcanary9d524f22016-03-29 09:03:52 -0700653static SkScalar compute_coverage(SkScalar depth, SkScalar initialDepth, SkScalar initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700654 SkScalar targetDepth, SkScalar targetCoverage) {
655 if (SkScalarNearlyEqual(initialDepth, targetDepth)) {
656 return targetCoverage;
657 }
halcanary9d524f22016-03-29 09:03:52 -0700658 SkScalar result = (depth - initialDepth) / (targetDepth - initialDepth) *
fmalitabd5d7e72015-06-26 07:18:24 -0700659 (targetCoverage - initialCoverage) + initialCoverage;
660 return SkScalarClampMax(result, 1.0f);
robertphillips84b00882015-05-06 05:15:57 -0700661}
662
663// return true when processing is complete
halcanary9d524f22016-03-29 09:03:52 -0700664bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing,
665 SkScalar initialDepth, SkScalar initialCoverage,
666 SkScalar targetDepth, SkScalar targetCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700667 bool forceNew) {
robertphillips84b00882015-05-06 05:15:57 -0700668 bool done = false;
669
670 fCandidateVerts.rewind();
671
672 // Loop through all the points in the ring and find the intersection with the smallest depth
673 SkScalar minDist = SK_ScalarMax, minT = 0.0f;
674 int minEdgeIdx = -1;
675
676 for (int cur = 0; cur < lastRing.numPts(); ++cur) {
677 int next = (cur + 1) % lastRing.numPts();
robertphillips8c170972016-09-22 12:42:30 -0700678
679 SkScalar t;
680 bool result = intersect(this->point(lastRing.index(cur)), lastRing.bisector(cur),
681 this->point(lastRing.index(next)), lastRing.bisector(next),
682 &t);
Brian Salomon8e449eb2018-09-05 15:08:49 -0400683 // The bisectors may be parallel (!result) or the previous ring may have become slightly
684 // concave due to accumulated error (t <= 0).
685 if (!result || t <= 0) {
robertphillips8c170972016-09-22 12:42:30 -0700686 continue;
687 }
robertphillips84b00882015-05-06 05:15:57 -0700688 SkScalar dist = -t * lastRing.norm(cur).dot(lastRing.bisector(cur));
689
690 if (minDist > dist) {
691 minDist = dist;
692 minT = t;
693 minEdgeIdx = cur;
694 }
695 }
696
fmalitabd5d7e72015-06-26 07:18:24 -0700697 if (minEdgeIdx == -1) {
698 return false;
699 }
robertphillips84b00882015-05-06 05:15:57 -0700700 SkPoint newPt = lastRing.bisector(minEdgeIdx);
701 newPt.scale(minT);
702 newPt += this->point(lastRing.index(minEdgeIdx));
703
704 SkScalar depth = this->computeDepthFromEdge(lastRing.origEdgeID(minEdgeIdx), newPt);
fmalitabd5d7e72015-06-26 07:18:24 -0700705 if (depth >= targetDepth) {
robertphillips84b00882015-05-06 05:15:57 -0700706 // None of the bisectors intersect before reaching the desired depth.
707 // Just step them all to the desired depth
fmalitabd5d7e72015-06-26 07:18:24 -0700708 depth = targetDepth;
robertphillips84b00882015-05-06 05:15:57 -0700709 done = true;
710 }
711
712 // 'dst' stores where each point in the last ring maps to/transforms into
713 // in the next ring.
714 SkTDArray<int> dst;
715 dst.setCount(lastRing.numPts());
716
717 // Create the first point (who compares with no one)
718 if (!this->computePtAlongBisector(lastRing.index(0),
719 lastRing.bisector(0),
720 lastRing.origEdgeID(0),
721 depth, &newPt)) {
722 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700723 return true;
724 }
725 dst[0] = fCandidateVerts.addNewPt(newPt,
726 lastRing.index(0), lastRing.origEdgeID(0),
727 !this->movable(lastRing.index(0)));
728
729 // Handle the middle points (who only compare with the prior point)
730 for (int cur = 1; cur < lastRing.numPts()-1; ++cur) {
731 if (!this->computePtAlongBisector(lastRing.index(cur),
732 lastRing.bisector(cur),
733 lastRing.origEdgeID(cur),
734 depth, &newPt)) {
735 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700736 return true;
737 }
738 if (!duplicate_pt(newPt, fCandidateVerts.lastPoint())) {
739 dst[cur] = fCandidateVerts.addNewPt(newPt,
740 lastRing.index(cur), lastRing.origEdgeID(cur),
741 !this->movable(lastRing.index(cur)));
742 } else {
743 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
744 }
745 }
746
747 // Check on the last point (handling the wrap around)
748 int cur = lastRing.numPts()-1;
749 if (!this->computePtAlongBisector(lastRing.index(cur),
750 lastRing.bisector(cur),
751 lastRing.origEdgeID(cur),
752 depth, &newPt)) {
753 this->terminate(lastRing);
robertphillips84b00882015-05-06 05:15:57 -0700754 return true;
755 }
756 bool dupPrev = duplicate_pt(newPt, fCandidateVerts.lastPoint());
757 bool dupNext = duplicate_pt(newPt, fCandidateVerts.firstPoint());
758
759 if (!dupPrev && !dupNext) {
760 dst[cur] = fCandidateVerts.addNewPt(newPt,
761 lastRing.index(cur), lastRing.origEdgeID(cur),
762 !this->movable(lastRing.index(cur)));
763 } else if (dupPrev && !dupNext) {
764 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
765 } else if (!dupPrev && dupNext) {
766 dst[cur] = fCandidateVerts.fuseWithNext();
767 } else {
768 bool dupPrevVsNext = duplicate_pt(fCandidateVerts.firstPoint(), fCandidateVerts.lastPoint());
769
770 if (!dupPrevVsNext) {
771 dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur));
772 } else {
ethannicholas0dacc672015-07-07 12:41:52 -0700773 const int fused = fCandidateVerts.fuseWithBoth();
774 dst[cur] = fused;
775 const int targetIdx = dst[cur - 1];
776 for (int i = cur - 1; i >= 0 && dst[i] == targetIdx; i--) {
777 dst[i] = fused;
778 }
robertphillips84b00882015-05-06 05:15:57 -0700779 }
780 }
781
782 // Fold the new ring's points into the global pool
783 for (int i = 0; i < fCandidateVerts.numPts(); ++i) {
784 int newIdx;
fmalitabd5d7e72015-06-26 07:18:24 -0700785 if (fCandidateVerts.needsToBeNew(i) || forceNew) {
halcanary9d524f22016-03-29 09:03:52 -0700786 // if the originating index is still valid then this point wasn't
robertphillips84b00882015-05-06 05:15:57 -0700787 // fused (and is thus movable)
halcanary9d524f22016-03-29 09:03:52 -0700788 SkScalar coverage = compute_coverage(depth, initialDepth, initialCoverage,
fmalitabd5d7e72015-06-26 07:18:24 -0700789 targetDepth, targetCoverage);
790 newIdx = this->addPt(fCandidateVerts.point(i), depth, coverage,
ethannicholasfab4a9b2016-08-26 11:03:32 -0700791 fCandidateVerts.originatingIdx(i) != -1, kSharp_CurveState);
robertphillips84b00882015-05-06 05:15:57 -0700792 } else {
793 SkASSERT(fCandidateVerts.originatingIdx(i) != -1);
fmalitabd5d7e72015-06-26 07:18:24 -0700794 this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth,
795 targetCoverage);
robertphillips84b00882015-05-06 05:15:57 -0700796 newIdx = fCandidateVerts.originatingIdx(i);
797 }
798
799 nextRing->addIdx(newIdx, fCandidateVerts.origEdge(i));
800 }
801
802 // 'dst' currently has indices into the ring. Remap these to be indices
803 // into the global pool since the triangulation operates in that space.
804 for (int i = 0; i < dst.count(); ++i) {
805 dst[i] = nextRing->index(dst[i]);
806 }
807
robertphillips44c31282015-09-03 12:58:48 -0700808 for (int i = 0; i < lastRing.numPts(); ++i) {
809 int next = (i + 1) % lastRing.numPts();
robertphillips84b00882015-05-06 05:15:57 -0700810
robertphillips44c31282015-09-03 12:58:48 -0700811 this->addTri(lastRing.index(i), lastRing.index(next), dst[next]);
812 this->addTri(lastRing.index(i), dst[next], dst[i]);
robertphillips84b00882015-05-06 05:15:57 -0700813 }
814
robertphillips8c170972016-09-22 12:42:30 -0700815 if (done && fStyle != SkStrokeRec::kStroke_Style) {
816 // fill or stroke-and-fill
robertphillips84b00882015-05-06 05:15:57 -0700817 this->fanRing(*nextRing);
818 }
819
820 if (nextRing->numPts() < 3) {
821 done = true;
822 }
robertphillips84b00882015-05-06 05:15:57 -0700823 return done;
824}
825
826void GrAAConvexTessellator::validate() const {
robertphillips84b00882015-05-06 05:15:57 -0700827 SkASSERT(fPts.count() == fMovable.count());
Robert Phillips1d089982016-09-26 14:07:48 -0400828 SkASSERT(fPts.count() == fCoverages.count());
829 SkASSERT(fPts.count() == fCurveState.count());
robertphillips84b00882015-05-06 05:15:57 -0700830 SkASSERT(0 == (fIndices.count() % 3));
Robert Phillips1d089982016-09-26 14:07:48 -0400831 SkASSERT(!fBisectors.count() || fBisectors.count() == fNorms.count());
robertphillips84b00882015-05-06 05:15:57 -0700832}
833
834//////////////////////////////////////////////////////////////////////////////
835void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) {
836 this->computeNormals(tess);
robertphillips364ad002015-05-20 11:49:55 -0700837 this->computeBisectors(tess);
robertphillips84b00882015-05-06 05:15:57 -0700838}
839
840void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms,
841 const SkTDArray<SkVector>& bisectors) {
842 for (int i = 0; i < fPts.count(); ++i) {
843 fPts[i].fNorm = norms[i];
844 fPts[i].fBisector = bisectors[i];
845 }
846}
847
848// Compute the outward facing normal at each vertex.
849void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& tess) {
850 for (int cur = 0; cur < fPts.count(); ++cur) {
851 int next = (cur + 1) % fPts.count();
852
853 fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex);
fmalitabd5d7e72015-06-26 07:18:24 -0700854 SkPoint::Normalize(&fPts[cur].fNorm);
Brian Salomon0235c642018-08-31 12:04:18 -0400855 fPts[cur].fNorm = SkPointPriv::MakeOrthog(fPts[cur].fNorm, tess.side());
robertphillips84b00882015-05-06 05:15:57 -0700856 }
857}
858
robertphillips364ad002015-05-20 11:49:55 -0700859void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& tess) {
robertphillips84b00882015-05-06 05:15:57 -0700860 int prev = fPts.count() - 1;
861 for (int cur = 0; cur < fPts.count(); prev = cur, ++cur) {
862 fPts[cur].fBisector = fPts[cur].fNorm + fPts[prev].fNorm;
robertphillips364ad002015-05-20 11:49:55 -0700863 if (!fPts[cur].fBisector.normalize()) {
Brian Salomon0235c642018-08-31 12:04:18 -0400864 fPts[cur].fBisector =
865 SkPointPriv::MakeOrthog(fPts[cur].fNorm, (SkPointPriv::Side)-tess.side()) +
866 SkPointPriv::MakeOrthog(fPts[prev].fNorm, tess.side());
robertphillips364ad002015-05-20 11:49:55 -0700867 SkAssertResult(fPts[cur].fBisector.normalize());
868 } else {
869 fPts[cur].fBisector.negate(); // make the bisector face in
870 }
fmalitabd5d7e72015-06-26 07:18:24 -0700871 }
robertphillips84b00882015-05-06 05:15:57 -0700872}
873
874//////////////////////////////////////////////////////////////////////////////
875#ifdef SK_DEBUG
876// Is this ring convex?
877bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const {
878 if (fPts.count() < 3) {
fmalitabd5d7e72015-06-26 07:18:24 -0700879 return true;
robertphillips84b00882015-05-06 05:15:57 -0700880 }
881
882 SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.top().fIndex);
883 SkPoint cur = tess.point(fPts[1].fIndex) - tess.point(fPts[0].fIndex);
884 SkScalar minDot = prev.fX * cur.fY - prev.fY * cur.fX;
885 SkScalar maxDot = minDot;
886
887 prev = cur;
888 for (int i = 1; i < fPts.count(); ++i) {
889 int next = (i + 1) % fPts.count();
890
891 cur = tess.point(fPts[next].fIndex) - tess.point(fPts[i].fIndex);
892 SkScalar dot = prev.fX * cur.fY - prev.fY * cur.fX;
893
894 minDot = SkMinScalar(minDot, dot);
895 maxDot = SkMaxScalar(maxDot, dot);
896
897 prev = cur;
898 }
899
fmalitabd5d7e72015-06-26 07:18:24 -0700900 if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) {
901 maxDot = 0;
902 }
903 if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) {
904 minDot = 0;
905 }
906 return (maxDot >= 0.0f) == (minDot >= 0.0f);
robertphillips84b00882015-05-06 05:15:57 -0700907}
908
robertphillips84b00882015-05-06 05:15:57 -0700909#endif
910
Robert Phillips1d089982016-09-26 14:07:48 -0400911void GrAAConvexTessellator::lineTo(const SkPoint& p, CurveState curve) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700912 if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) {
913 return;
914 }
915
Brian Salomon0235c642018-08-31 12:04:18 -0400916 if (this->numPts() >= 2 &&
917 points_are_colinear_and_b_is_middle(fPts[fPts.count() - 2], fPts.top(), p)) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700918 // The old last point is on the line from the second to last to the new point
919 this->popLastPt();
ethannicholas1bcbc8f2016-01-08 14:09:18 -0800920 // double-check that the new last point is not a duplicate of the new point. In an ideal
921 // world this wouldn't be necessary (since it's only possible for non-convex paths), but
Robert Phillips1d089982016-09-26 14:07:48 -0400922 // floating point precision issues mean it can actually happen on paths that were
923 // determined to be convex.
ethannicholas1bcbc8f2016-01-08 14:09:18 -0800924 if (duplicate_pt(p, this->lastPoint())) {
925 return;
926 }
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700927 }
robertphillips8c170972016-09-22 12:42:30 -0700928 SkScalar initialRingCoverage = (SkStrokeRec::kFill_Style == fStyle) ? 0.5f : 1.0f;
ethannicholasfab4a9b2016-08-26 11:03:32 -0700929 this->addPt(p, 0.0f, initialRingCoverage, false, curve);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700930}
931
Mike Reed5f152f02019-08-19 14:40:16 -0400932void GrAAConvexTessellator::lineTo(const SkMatrix& m, const SkPoint& p, CurveState curve) {
933 this->lineTo(m.mapXY(p.fX, p.fY), curve);
fmalitabd5d7e72015-06-26 07:18:24 -0700934}
935
Robert Phillips1d089982016-09-26 14:07:48 -0400936void GrAAConvexTessellator::quadTo(const SkPoint pts[3]) {
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700937 int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance);
Mike Kleincc9856c2018-04-19 09:18:33 -0400938 fPointBuffer.setCount(maxCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700939 SkPoint* target = fPointBuffer.begin();
halcanary9d524f22016-03-29 09:03:52 -0700940 int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
Robert Phillips1d089982016-09-26 14:07:48 -0400941 kQuadTolerance, &target, maxCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700942 fPointBuffer.setCount(count);
ethannicholasfab4a9b2016-08-26 11:03:32 -0700943 for (int i = 0; i < count - 1; i++) {
Robert Phillips1d089982016-09-26 14:07:48 -0400944 this->lineTo(fPointBuffer[i], kCurve_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700945 }
Robert Phillips1d089982016-09-26 14:07:48 -0400946 this->lineTo(fPointBuffer[count - 1], kIndeterminate_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700947}
948
Mike Reed5f152f02019-08-19 14:40:16 -0400949void GrAAConvexTessellator::quadTo(const SkMatrix& m, const SkPoint srcPts[3]) {
950 SkPoint pts[3];
951 m.mapPoints(pts, srcPts, 3);
Robert Phillips1d089982016-09-26 14:07:48 -0400952 this->quadTo(pts);
fmalitabd5d7e72015-06-26 07:18:24 -0700953}
954
Mike Reed5f152f02019-08-19 14:40:16 -0400955void GrAAConvexTessellator::cubicTo(const SkMatrix& m, const SkPoint srcPts[4]) {
956 SkPoint pts[4];
957 m.mapPoints(pts, srcPts, 4);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700958 int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance);
Mike Kleincc9856c2018-04-19 09:18:33 -0400959 fPointBuffer.setCount(maxCount);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700960 SkPoint* target = fPointBuffer.begin();
halcanary9d524f22016-03-29 09:03:52 -0700961 int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700962 kCubicTolerance, &target, maxCount);
963 fPointBuffer.setCount(count);
ethannicholasfab4a9b2016-08-26 11:03:32 -0700964 for (int i = 0; i < count - 1; i++) {
Robert Phillips1d089982016-09-26 14:07:48 -0400965 this->lineTo(fPointBuffer[i], kCurve_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700966 }
Robert Phillips1d089982016-09-26 14:07:48 -0400967 this->lineTo(fPointBuffer[count - 1], kIndeterminate_CurveState);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700968}
969
970// include down here to avoid compilation errors caused by "-" overload in SkGeometry.h
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500971#include "src/core/SkGeometry.h"
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700972
Mike Reed5f152f02019-08-19 14:40:16 -0400973void GrAAConvexTessellator::conicTo(const SkMatrix& m, const SkPoint srcPts[3], SkScalar w) {
974 SkPoint pts[3];
975 m.mapPoints(pts, srcPts, 3);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700976 SkAutoConicToQuads quadder;
977 const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance);
978 SkPoint lastPoint = *(quads++);
979 int count = quadder.countQuads();
980 for (int i = 0; i < count; ++i) {
981 SkPoint quadPts[3];
982 quadPts[0] = lastPoint;
983 quadPts[1] = quads[0];
984 quadPts[2] = i == count - 1 ? pts[2] : quads[1];
Robert Phillips1d089982016-09-26 14:07:48 -0400985 this->quadTo(quadPts);
ethannicholas1a1b3ac2015-06-10 12:11:17 -0700986 lastPoint = quadPts[2];
987 quads += 2;
988 }
989}
990
robertphillips84b00882015-05-06 05:15:57 -0700991//////////////////////////////////////////////////////////////////////////////
992#if GR_AA_CONVEX_TESSELLATOR_VIZ
993static const SkScalar kPointRadius = 0.02f;
994static const SkScalar kArrowStrokeWidth = 0.0f;
995static const SkScalar kArrowLength = 0.2f;
996static const SkScalar kEdgeTextSize = 0.1f;
997static const SkScalar kPointTextSize = 0.02f;
998
999static void draw_point(SkCanvas* canvas, const SkPoint& p, SkScalar paramValue, bool stroke) {
1000 SkPaint paint;
1001 SkASSERT(paramValue <= 1.0f);
1002 int gs = int(255*paramValue);
1003 paint.setARGB(255, gs, gs, gs);
1004
1005 canvas->drawCircle(p.fX, p.fY, kPointRadius, paint);
1006
1007 if (stroke) {
1008 SkPaint stroke;
1009 stroke.setColor(SK_ColorYELLOW);
1010 stroke.setStyle(SkPaint::kStroke_Style);
1011 stroke.setStrokeWidth(kPointRadius/3.0f);
halcanary9d524f22016-03-29 09:03:52 -07001012 canvas->drawCircle(p.fX, p.fY, kPointRadius, stroke);
robertphillips84b00882015-05-06 05:15:57 -07001013 }
1014}
1015
1016static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkColor color) {
1017 SkPaint p;
1018 p.setColor(color);
1019
1020 canvas->drawLine(p0.fX, p0.fY, p1.fX, p1.fY, p);
1021}
1022
1023static void draw_arrow(SkCanvas*canvas, const SkPoint& p, const SkPoint &n,
1024 SkScalar len, SkColor color) {
1025 SkPaint paint;
1026 paint.setColor(color);
1027 paint.setStrokeWidth(kArrowStrokeWidth);
1028 paint.setStyle(SkPaint::kStroke_Style);
1029
1030 canvas->drawLine(p.fX, p.fY,
1031 p.fX + len * n.fX, p.fY + len * n.fY,
1032 paint);
1033}
1034
1035void GrAAConvexTessellator::Ring::draw(SkCanvas* canvas, const GrAAConvexTessellator& tess) const {
1036 SkPaint paint;
1037 paint.setTextSize(kEdgeTextSize);
1038
1039 for (int cur = 0; cur < fPts.count(); ++cur) {
1040 int next = (cur + 1) % fPts.count();
1041
1042 draw_line(canvas,
1043 tess.point(fPts[cur].fIndex),
1044 tess.point(fPts[next].fIndex),
1045 SK_ColorGREEN);
1046
1047 SkPoint mid = tess.point(fPts[cur].fIndex) + tess.point(fPts[next].fIndex);
1048 mid.scale(0.5f);
1049
1050 if (fPts.count()) {
1051 draw_arrow(canvas, mid, fPts[cur].fNorm, kArrowLength, SK_ColorRED);
1052 mid.fX += (kArrowLength/2) * fPts[cur].fNorm.fX;
1053 mid.fY += (kArrowLength/2) * fPts[cur].fNorm.fY;
1054 }
1055
1056 SkString num;
1057 num.printf("%d", this->origEdgeID(cur));
Cary Clark2a475ea2017-04-28 15:35:12 -04001058 canvas->drawString(num, mid.fX, mid.fY, paint);
robertphillips84b00882015-05-06 05:15:57 -07001059
1060 if (fPts.count()) {
1061 draw_arrow(canvas, tess.point(fPts[cur].fIndex), fPts[cur].fBisector,
1062 kArrowLength, SK_ColorBLUE);
1063 }
halcanary9d524f22016-03-29 09:03:52 -07001064 }
robertphillips84b00882015-05-06 05:15:57 -07001065}
1066
1067void GrAAConvexTessellator::draw(SkCanvas* canvas) const {
1068 for (int i = 0; i < fIndices.count(); i += 3) {
1069 SkASSERT(fIndices[i] < this->numPts()) ;
1070 SkASSERT(fIndices[i+1] < this->numPts()) ;
1071 SkASSERT(fIndices[i+2] < this->numPts()) ;
1072
1073 draw_line(canvas,
1074 this->point(this->fIndices[i]), this->point(this->fIndices[i+1]),
1075 SK_ColorBLACK);
1076 draw_line(canvas,
1077 this->point(this->fIndices[i+1]), this->point(this->fIndices[i+2]),
1078 SK_ColorBLACK);
1079 draw_line(canvas,
1080 this->point(this->fIndices[i+2]), this->point(this->fIndices[i]),
1081 SK_ColorBLACK);
1082 }
1083
1084 fInitialRing.draw(canvas, *this);
1085 for (int i = 0; i < fRings.count(); ++i) {
1086 fRings[i]->draw(canvas, *this);
1087 }
1088
1089 for (int i = 0; i < this->numPts(); ++i) {
1090 draw_point(canvas,
halcanary9d524f22016-03-29 09:03:52 -07001091 this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)),
robertphillips84b00882015-05-06 05:15:57 -07001092 !this->movable(i));
1093
1094 SkPaint paint;
1095 paint.setTextSize(kPointTextSize);
fmalitabd5d7e72015-06-26 07:18:24 -07001096 if (this->depth(i) <= -kAntialiasingRadius) {
robertphillips84b00882015-05-06 05:15:57 -07001097 paint.setColor(SK_ColorWHITE);
1098 }
1099
1100 SkString num;
1101 num.printf("%d", i);
Cary Clark2a475ea2017-04-28 15:35:12 -04001102 canvas->drawString(num,
halcanary9d524f22016-03-29 09:03:52 -07001103 this->point(i).fX, this->point(i).fY+(kPointRadius/2.0f),
robertphillips84b00882015-05-06 05:15:57 -07001104 paint);
1105 }
1106}
1107
1108#endif