blob: d0ed4d3692e00f0750826d16e247edf39a172835 [file] [log] [blame]
ethannicholase9709e82016-01-07 13:34:16 -08001/*
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
Chris Dalton17dc4182020-03-25 16:18:16 -06008#include "src/gpu/GrTriangulator.h"
ethannicholase9709e82016-01-07 13:34:16 -08009
Chris Daltond081dce2020-01-23 12:09:04 -070010#include "src/gpu/GrEagerVertexAllocator.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040012#include "src/gpu/geometry/GrPathUtils.h"
Chris Daltond60c9192021-01-07 18:30:08 +000013
14#include "src/core/SkGeometry.h"
15#include "src/core/SkPointPriv.h"
16
Stephen White94b7e542018-01-04 14:01:10 -050017#include <algorithm>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040018#include <cstdio>
Stephen Whitec4dbc372019-05-22 10:50:14 -040019#include <queue>
20#include <unordered_map>
Ben Wagnerf08d1d02018-06-18 15:11:00 -040021#include <utility>
ethannicholase9709e82016-01-07 13:34:16 -080022
Chris Daltond60c9192021-01-07 18:30:08 +000023
Chris Dalton5045de32021-01-07 19:09:01 -070024#if TRIANGULATOR_LOGGING
Chris Daltond60c9192021-01-07 18:30:08 +000025#define TESS_LOG printf
ethannicholase9709e82016-01-07 13:34:16 -080026#else
Brian Salomon120e7d62019-09-11 10:29:22 -040027#define TESS_LOG(...)
ethannicholase9709e82016-01-07 13:34:16 -080028#endif
29
Chris Dalton57ea1fc2021-01-05 13:37:44 -070030constexpr static float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
ethannicholase9709e82016-01-07 13:34:16 -080031
Stephen Whitee260c462017-12-19 18:09:54 -050032struct Event;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070033
Chris Dalton17ce8c52021-01-07 18:08:46 -070034using EdgeType = GrTriangulator::EdgeType;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070035using Vertex = GrTriangulator::Vertex;
36using VertexList = GrTriangulator::VertexList;
Chris Dalton17ce8c52021-01-07 18:08:46 -070037using Line = GrTriangulator::Line;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070038using Edge = GrTriangulator::Edge;
39using EdgeList = GrTriangulator::EdgeList;
40using Poly = GrTriangulator::Poly;
Chris Dalton17ce8c52021-01-07 18:08:46 -070041using MonotonePoly = GrTriangulator::MonotonePoly;
Chris Dalton57ea1fc2021-01-05 13:37:44 -070042using Comparator = GrTriangulator::Comparator;
ethannicholase9709e82016-01-07 13:34:16 -080043
44template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070045static void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080046 t->*Prev = prev;
47 t->*Next = next;
48 if (prev) {
49 prev->*Next = t;
50 } else if (head) {
51 *head = t;
52 }
53 if (next) {
54 next->*Prev = t;
55 } else if (tail) {
56 *tail = t;
57 }
58}
59
60template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070061static void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080062 if (t->*Prev) {
63 t->*Prev->*Next = t->*Next;
64 } else if (head) {
65 *head = t->*Next;
66 }
67 if (t->*Next) {
68 t->*Next->*Prev = t->*Prev;
69 } else if (tail) {
70 *tail = t->*Prev;
71 }
72 t->*Prev = t->*Next = nullptr;
73}
74
ethannicholase9709e82016-01-07 13:34:16 -080075typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
76
Chris Dalton57ea1fc2021-01-05 13:37:44 -070077static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -050078 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -080079}
80
Chris Dalton57ea1fc2021-01-05 13:37:44 -070081static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -050082 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -080083}
84
Chris Dalton5045de32021-01-07 19:09:01 -070085bool GrTriangulator::Comparator::sweep_lt(const SkPoint& a, const SkPoint& b) const {
86 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
87}
Stephen White16a40cb2017-02-23 11:10:01 -050088
Chris Daltond60c9192021-01-07 18:30:08 +000089static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -050090 GrVertexWriter verts{data};
91 verts.write(v->fPoint);
92
Brian Osman80879d42019-01-07 16:15:27 -050093 if (emitCoverage) {
94 verts.write(GrNormalizeByteToFloat(v->fAlpha));
95 }
Brian Osman0995fd52019-01-09 09:52:25 -050096
Brian Osmanf9aabff2018-11-13 16:11:38 -050097 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -080098}
99
Chris Daltond60c9192021-01-07 18:30:08 +0000100static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400101 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
102 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
103 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700104#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500105 data = emit_vertex(v0, emitCoverage, data);
106 data = emit_vertex(v1, emitCoverage, data);
107 data = emit_vertex(v1, emitCoverage, data);
108 data = emit_vertex(v2, emitCoverage, data);
109 data = emit_vertex(v2, emitCoverage, data);
110 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800111#else
Brian Osman0995fd52019-01-09 09:52:25 -0500112 data = emit_vertex(v0, emitCoverage, data);
113 data = emit_vertex(v1, emitCoverage, data);
114 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800115#endif
116 return data;
117}
118
Chris Dalton5045de32021-01-07 19:09:01 -0700119void GrTriangulator::VertexList::insert(Vertex* v, Vertex* prev, Vertex* next) {
120 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
121}
122
123void GrTriangulator::VertexList::remove(Vertex* v) {
124 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
125}
senorblancoe6eaa322016-03-08 09:06:44 -0800126
senorblancof57372d2016-08-31 10:36:19 -0700127// Round to nearest quarter-pixel. This is used for screenspace tessellation.
128
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700129static inline void round(SkPoint* p) {
senorblancof57372d2016-08-31 10:36:19 -0700130 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
131 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
132}
133
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700134static inline SkScalar double_to_clamped_scalar(double d) {
Stephen White94b7e542018-01-04 14:01:10 -0500135 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
136}
137
Chris Dalton5045de32021-01-07 19:09:01 -0700138bool GrTriangulator::Line::intersect(const Line& other, SkPoint* point) const {
139 double denom = fA * other.fB - fB * other.fA;
140 if (denom == 0.0) {
141 return false;
senorblanco49df8d12016-10-07 08:36:56 -0700142 }
Chris Dalton5045de32021-01-07 19:09:01 -0700143 double scale = 1.0 / denom;
144 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
145 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
146 round(point);
147 return point->isFinite();
148}
Chris Daltond60c9192021-01-07 18:30:08 +0000149
Chris Dalton5045de32021-01-07 19:09:01 -0700150bool GrTriangulator::Edge::intersect(const Edge& other, SkPoint* p, uint8_t* alpha) const {
151 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
152 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
153 if (fTop == other.fTop || fBottom == other.fBottom) {
154 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000155 }
Chris Dalton5045de32021-01-07 19:09:01 -0700156 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
157 if (denom == 0.0) {
158 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000159 }
Chris Dalton5045de32021-01-07 19:09:01 -0700160 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
161 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
162 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
163 double tNumer = dy * fLine.fB + dx * fLine.fA;
164 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
165 // This saves us doing the divide below unless absolutely necessary.
166 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
167 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
168 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000169 }
Chris Dalton5045de32021-01-07 19:09:01 -0700170 double s = sNumer / denom;
171 SkASSERT(s >= 0.0 && s <= 1.0);
172 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
173 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
174 if (alpha) {
175 if (fType == EdgeType::kConnector) {
176 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
177 } else if (other.fType == EdgeType::kConnector) {
178 double t = tNumer / denom;
179 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
180 } else if (fType == EdgeType::kOuter && other.fType == EdgeType::kOuter) {
181 *alpha = 0;
182 } else {
183 *alpha = 255;
184 }
Chris Daltond60c9192021-01-07 18:30:08 +0000185 }
Chris Dalton5045de32021-01-07 19:09:01 -0700186 return true;
187}
senorblancof57372d2016-08-31 10:36:19 -0700188
Stephen Whitec4dbc372019-05-22 10:50:14 -0400189struct SSEdge;
190
191struct SSVertex {
192 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
193 Vertex* fVertex;
194 SSEdge* fPrev;
195 SSEdge* fNext;
196};
197
198struct SSEdge {
199 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
200 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
201 }
202 Edge* fEdge;
203 Event* fEvent;
204 SSVertex* fPrev;
205 SSVertex* fNext;
206};
207
208typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
209typedef std::vector<SSEdge*> SSEdgeList;
210
Chris Dalton5045de32021-01-07 19:09:01 -0700211void GrTriangulator::EdgeList::insert(Edge* edge, Edge* prev, Edge* next) {
212 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
213}
214void GrTriangulator::EdgeList::remove(Edge* edge) {
215 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
216}
ethannicholase9709e82016-01-07 13:34:16 -0800217
Stephen Whitec4dbc372019-05-22 10:50:14 -0400218struct EventList;
219
Stephen Whitee260c462017-12-19 18:09:54 -0500220struct Event {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400221 Event(SSEdge* edge, const SkPoint& point, uint8_t alpha)
222 : fEdge(edge), fPoint(point), fAlpha(alpha) {
Stephen Whitee260c462017-12-19 18:09:54 -0500223 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400224 SSEdge* fEdge;
Stephen Whitee260c462017-12-19 18:09:54 -0500225 SkPoint fPoint;
226 uint8_t fAlpha;
Chris Dalton811dc6a2021-01-07 16:40:32 -0700227 void apply(VertexList* mesh, const Comparator& c, EventList* events, SkArenaAlloc& alloc);
Stephen Whitee260c462017-12-19 18:09:54 -0500228};
229
Stephen Whitec4dbc372019-05-22 10:50:14 -0400230struct EventComparator {
231 enum class Op { kLessThan, kGreaterThan };
232 EventComparator(Op op) : fOp(op) {}
233 bool operator() (Event* const &e1, Event* const &e2) {
234 return fOp == Op::kLessThan ? e1->fAlpha < e2->fAlpha
235 : e1->fAlpha > e2->fAlpha;
236 }
237 Op fOp;
238};
Stephen Whitee260c462017-12-19 18:09:54 -0500239
Stephen Whitec4dbc372019-05-22 10:50:14 -0400240typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500241
Stephen Whitec4dbc372019-05-22 10:50:14 -0400242struct EventList : EventPQ {
243 EventList(EventComparator comparison) : EventPQ(comparison) {
244 }
245};
246
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700247static void create_event(SSEdge* e, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400248 Vertex* prev = e->fPrev->fVertex;
249 Vertex* next = e->fNext->fVertex;
250 if (prev == next || !prev->fPartner || !next->fPartner) {
251 return;
252 }
Chris Dalton17ce8c52021-01-07 18:08:46 -0700253 Edge bisector1(prev, prev->fPartner, 1, EdgeType::kConnector);
254 Edge bisector2(next, next->fPartner, 1, EdgeType::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500255 SkPoint p;
256 uint8_t alpha;
257 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400258 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
259 "will collapse to %g,%g alpha %d\n",
260 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
261 alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400262 e->fEvent = alloc.make<Event>(e, p, alpha);
263 events->push(e->fEvent);
264 }
265}
266
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700267static void create_event(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest, EventList* events,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700268 const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400269 if (!v->fPartner) {
270 return;
271 }
Stephen White8a3c0592019-05-29 11:26:16 -0400272 Vertex* top = edge->fEdge->fTop;
273 Vertex* bottom = edge->fEdge->fBottom;
274 if (!top || !bottom ) {
275 return;
276 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400277 Line line = edge->fEdge->fLine;
278 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
Chris Dalton17ce8c52021-01-07 18:08:46 -0700279 Edge bisector(v, v->fPartner, 1, EdgeType::kConnector);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400280 SkPoint p;
281 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400282 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
283 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400284 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
285 "will collapse to %g,%g alpha %d\n",
286 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400287 edge->fEvent = alloc.make<Event>(edge, p, alpha);
288 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500289 }
290}
Stephen Whitee260c462017-12-19 18:09:54 -0500291
Chris Dalton5045de32021-01-07 19:09:01 -0700292void GrTriangulator::MonotonePoly::addEdge(Edge* edge) {
293 if (fSide == kRight_Side) {
294 SkASSERT(!edge->fUsedInRightPoly);
295 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
296 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
297 edge->fUsedInRightPoly = true;
298 } else {
299 SkASSERT(!edge->fUsedInLeftPoly);
300 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
301 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
302 edge->fUsedInLeftPoly = true;
Chris Dalton17ce8c52021-01-07 18:08:46 -0700303 }
Chris Dalton5045de32021-01-07 19:09:01 -0700304}
Chris Dalton17ce8c52021-01-07 18:08:46 -0700305
Chris Dalton5045de32021-01-07 19:09:01 -0700306void* GrTriangulator::MonotonePoly::emit(bool emitCoverage, void* data) {
307 Edge* e = fFirstEdge;
308 VertexList vertices;
309 vertices.append(e->fTop);
310 int count = 1;
311 while (e != nullptr) {
312 if (kRight_Side == fSide) {
313 vertices.append(e->fBottom);
314 e = e->fRightPolyNext;
315 } else {
316 vertices.prepend(e->fBottom);
317 e = e->fLeftPolyNext;
Chris Dalton17ce8c52021-01-07 18:08:46 -0700318 }
Chris Dalton5045de32021-01-07 19:09:01 -0700319 count++;
320 }
321 Vertex* first = vertices.fHead;
322 Vertex* v = first->fNext;
323 while (v != vertices.fTail) {
324 SkASSERT(v && v->fPrev && v->fNext);
325 Vertex* prev = v->fPrev;
326 Vertex* curr = v;
327 Vertex* next = v->fNext;
328 if (count == 3) {
329 return this->emitTriangle(prev, curr, next, emitCoverage, data);
330 }
331 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
332 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
333 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
334 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
335 if (ax * by - ay * bx >= 0.0) {
336 data = this->emitTriangle(prev, curr, next, emitCoverage, data);
337 v->fPrev->fNext = v->fNext;
338 v->fNext->fPrev = v->fPrev;
339 count--;
340 if (v->fPrev == first) {
Chris Dalton17ce8c52021-01-07 18:08:46 -0700341 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000342 } else {
Chris Dalton5045de32021-01-07 19:09:01 -0700343 v = v->fPrev;
Chris Daltond60c9192021-01-07 18:30:08 +0000344 }
Chris Dalton5045de32021-01-07 19:09:01 -0700345 } else {
346 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000347 }
Chris Dalton75887a22021-01-06 00:23:13 -0700348 }
Chris Dalton5045de32021-01-07 19:09:01 -0700349 return data;
350}
351
352void* GrTriangulator::MonotonePoly::emitTriangle(Vertex* prev, Vertex* curr, Vertex* next,
353 bool emitCoverage, void* data) const {
354 if (fWinding < 0) {
355 // Ensure our triangles always wind in the same direction as if the path had been
356 // triangulated as a simple fan (a la red book).
357 std::swap(prev, next);
358 }
359 return emit_triangle(next, curr, prev, emitCoverage, data);
360}
361
362Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
363 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
364 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
365 Poly* partner = fPartner;
366 Poly* poly = this;
367 if (side == kRight_Side) {
368 if (e->fUsedInRightPoly) {
369 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000370 }
Chris Dalton5045de32021-01-07 19:09:01 -0700371 } else {
372 if (e->fUsedInLeftPoly) {
373 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000374 }
Chris Dalton5045de32021-01-07 19:09:01 -0700375 }
376 if (partner) {
377 fPartner = partner->fPartner = nullptr;
378 }
379 if (!fTail) {
380 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
381 fCount += 2;
382 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
383 return poly;
384 } else if (side == fTail->fSide) {
385 fTail->addEdge(e);
386 fCount++;
387 } else {
388 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner);
389 fTail->addEdge(e);
390 fCount++;
391 if (partner) {
392 partner->addEdge(e, side, alloc);
393 poly = partner;
394 } else {
395 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
396 m->fPrev = fTail;
397 fTail->fNext = m;
398 fTail = m;
399 }
400 }
401 return poly;
402}
403void* GrTriangulator::Poly::emit(bool emitCoverage, void *data) {
404 if (fCount < 3) {
ethannicholase9709e82016-01-07 13:34:16 -0800405 return data;
406 }
Chris Dalton5045de32021-01-07 19:09:01 -0700407 TESS_LOG("emit() %d, size %d\n", fID, fCount);
408 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
409 data = m->emit(emitCoverage, data);
410 }
411 return data;
412}
ethannicholase9709e82016-01-07 13:34:16 -0800413
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700414static bool coincident(const SkPoint& a, const SkPoint& b) {
ethannicholase9709e82016-01-07 13:34:16 -0800415 return a == b;
416}
417
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700418static Poly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500419 Poly* poly = alloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800420 poly->fNext = *head;
421 *head = poly;
422 return poly;
423}
424
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700425void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) {
426 Vertex* v = fAlloc.make<Vertex>(p, 255);
Chris Dalton5045de32021-01-07 19:09:01 -0700427#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -0800428 static float gID = 0.0f;
429 v->fID = gID++;
430#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500431 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800432}
433
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700434static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
Stephen White36e4f062017-03-27 16:11:31 -0400435 SkQuadCoeff quad(pts);
436 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
437 SkPoint mid = to_point(quad.eval(t));
438 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400439 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
440 return 0;
441 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500442 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400443}
444
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700445void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
446 VertexList* contour) {
Stephen White36e4f062017-03-27 16:11:31 -0400447 SkQuadCoeff quad(pts);
448 Sk2s aa = quad.fA * quad.fA;
449 SkScalar denom = 2.0f * (aa[0] + aa[1]);
450 Sk2s ab = quad.fA * quad.fB;
451 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
452 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500453 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400454 // Test possible subdivision values only at the point of maximum curvature.
455 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500456 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400457 u = 1.0f / nPoints;
458 if (quad_error_at(pts, t, u) < toleranceSqd) {
459 break;
460 }
461 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800462 }
Stephen White36e4f062017-03-27 16:11:31 -0400463 for (int j = 1; j <= nPoints; j++) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700464 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
Stephen White36e4f062017-03-27 16:11:31 -0400465 }
ethannicholase9709e82016-01-07 13:34:16 -0800466}
467
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700468void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
469 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
470 int pointsLeft) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500471 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
472 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800473 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
474 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700475 this->appendPointToContour(p3, contour);
Stephen White3a9aab92017-03-07 14:07:18 -0500476 return;
ethannicholase9709e82016-01-07 13:34:16 -0800477 }
478 const SkPoint q[] = {
479 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
480 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
481 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
482 };
483 const SkPoint r[] = {
484 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
485 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
486 };
487 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
488 pointsLeft >>= 1;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700489 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
490 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800491}
492
493// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
494
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700495void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
496 VertexList* contours) {
ethannicholase9709e82016-01-07 13:34:16 -0800497 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton7156db22020-05-07 22:06:28 +0000498 SkPoint pts[4];
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700499 fIsLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500500 VertexList* contour = contours;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700501 SkPath::Iter iter(fPath, false);
502 if (fPath.isInverseFillType()) {
ethannicholase9709e82016-01-07 13:34:16 -0800503 SkPoint quad[4];
504 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700505 for (int i = 3; i >= 0; i--) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700506 this->appendPointToContour(quad[i], contours);
ethannicholase9709e82016-01-07 13:34:16 -0800507 }
Stephen White3a9aab92017-03-07 14:07:18 -0500508 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800509 }
510 SkAutoConicToQuads converter;
Chris Dalton7156db22020-05-07 22:06:28 +0000511 SkPath::Verb verb;
512 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800513 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +0000514 case SkPath::kConic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700515 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700516 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700517 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700518 break;
519 }
Chris Dalton7156db22020-05-07 22:06:28 +0000520 SkScalar weight = iter.conicWeight();
521 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800522 for (int i = 0; i < converter.countQuads(); ++i) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700523 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800524 quadPts += 2;
525 }
ethannicholase9709e82016-01-07 13:34:16 -0800526 break;
527 }
Chris Dalton7156db22020-05-07 22:06:28 +0000528 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500529 if (contour->fHead) {
530 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800531 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700532 this->appendPointToContour(pts[0], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800533 break;
Chris Dalton7156db22020-05-07 22:06:28 +0000534 case SkPath::kLine_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700535 this->appendPointToContour(pts[1], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800536 break;
537 }
Chris Dalton7156db22020-05-07 22:06:28 +0000538 case SkPath::kQuad_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700539 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700540 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700541 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700542 break;
543 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700544 this->appendQuadraticToContour(pts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800545 break;
546 }
Chris Dalton7156db22020-05-07 22:06:28 +0000547 case SkPath::kCubic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700548 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700549 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700550 this->appendPointToContour(pts[3], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700551 break;
552 }
ethannicholase9709e82016-01-07 13:34:16 -0800553 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700554 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
555 pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800556 break;
557 }
Chris Dalton7156db22020-05-07 22:06:28 +0000558 case SkPath::kClose_Verb:
559 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800560 break;
561 }
562 }
563}
564
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700565static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800566 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500567 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800568 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500569 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800570 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500571 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700572 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500573 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800574 return (winding & 1) == 1;
575 default:
576 SkASSERT(false);
577 return false;
578 }
579}
580
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700581static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500582 return poly && apply_fill_type(fillType, poly->fWinding);
583}
584
Chris Dalton17ce8c52021-01-07 18:08:46 -0700585static Edge* new_edge(Vertex* prev, Vertex* next, EdgeType type, const Comparator& c,
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700586 SkArenaAlloc& alloc) {
Stephen White2f4686f2017-01-03 16:20:01 -0500587 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800588 Vertex* top = winding < 0 ? next : prev;
589 Vertex* bottom = winding < 0 ? prev : next;
Herb Derby5cdc9dd2017-02-13 12:10:46 -0500590 return alloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800591}
592
Chris Daltond60c9192021-01-07 18:30:08 +0000593static void remove_edge(Edge* edge, EdgeList* edges) {
594 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
595 SkASSERT(edges->contains(edge));
596 edges->remove(edge);
597}
598
599static void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400600 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +0000601 SkASSERT(!edges->contains(edge));
602 Edge* next = prev ? prev->fRight : edges->fHead;
603 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800604}
605
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700606static void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500607 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800608 *left = v->fFirstEdgeAbove->fLeft;
609 *right = v->fLastEdgeAbove->fRight;
610 return;
611 }
612 Edge* next = nullptr;
613 Edge* prev;
614 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
615 if (prev->isLeftOf(v)) {
616 break;
617 }
618 next = prev;
619 }
620 *left = prev;
621 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800622}
623
Chris Dalton811dc6a2021-01-07 16:40:32 -0700624static void insert_edge_above(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800625 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500626 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800627 return;
628 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400629 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
630 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800631 Edge* prev = nullptr;
632 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000633 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800634 if (next->isRightOf(edge->fTop)) {
635 break;
636 }
637 prev = next;
638 }
senorblancoe6eaa322016-03-08 09:06:44 -0800639 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
Chris Daltond60c9192021-01-07 18:30:08 +0000640 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
ethannicholase9709e82016-01-07 13:34:16 -0800641}
642
Chris Dalton811dc6a2021-01-07 16:40:32 -0700643static void insert_edge_below(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800644 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500645 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800646 return;
647 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400648 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
649 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800650 Edge* prev = nullptr;
651 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000652 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -0800653 if (next->isRightOf(edge->fBottom)) {
654 break;
655 }
656 prev = next;
657 }
senorblancoe6eaa322016-03-08 09:06:44 -0800658 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
Chris Daltond60c9192021-01-07 18:30:08 +0000659 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
ethannicholase9709e82016-01-07 13:34:16 -0800660}
661
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700662static void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400663 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400664 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
665 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800666 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800667 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
668}
669
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700670static void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400671 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400672 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
673 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800674 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800675 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
676}
677
Chris Daltond60c9192021-01-07 18:30:08 +0000678static void disconnect(Edge* edge)
679{
680 remove_edge_above(edge);
681 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500682}
683
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700684static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700685 const Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400686
Chris Dalton811dc6a2021-01-07 16:40:32 -0700687static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400688 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
689 return;
690 }
691 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400692 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400693 while (v != dst) {
694 v = v->fPrev;
695 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +0000696 remove_edge(e, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400697 }
698 Edge* leftEdge = v->fLeftEnclosingEdge;
699 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +0000700 insert_edge(e, leftEdge, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400701 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -0500702 Vertex* top = e->fTop;
703 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
704 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
705 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
706 dst = top;
707 }
Stephen White3b5a3fa2017-06-06 14:51:19 -0400708 }
709 }
710 *current = v;
711}
712
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700713static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700714 const Comparator& c) {
Stephen Whitec03e6982020-02-06 16:32:14 -0500715 if (!activeEdges || !current) {
716 return;
717 }
718 Vertex* top = edge->fTop;
719 Vertex* bottom = edge->fBottom;
720 if (edge->fLeft) {
721 Vertex* leftTop = edge->fLeft->fTop;
722 Vertex* leftBottom = edge->fLeft->fBottom;
723 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
724 rewind(activeEdges, current, leftTop, c);
725 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
726 rewind(activeEdges, current, top, c);
727 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
728 !edge->fLeft->isLeftOf(bottom)) {
729 rewind(activeEdges, current, leftTop, c);
730 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
731 rewind(activeEdges, current, top, c);
732 }
733 }
734 if (edge->fRight) {
735 Vertex* rightTop = edge->fRight->fTop;
736 Vertex* rightBottom = edge->fRight->fBottom;
737 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
738 rewind(activeEdges, current, rightTop, c);
739 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
740 rewind(activeEdges, current, top, c);
741 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
742 !edge->fRight->isRightOf(bottom)) {
743 rewind(activeEdges, current, rightTop, c);
744 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
745 !edge->isLeftOf(rightBottom)) {
746 rewind(activeEdges, current, top, c);
747 }
748 }
749}
750
Chris Dalton811dc6a2021-01-07 16:40:32 -0700751static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
752 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800753 remove_edge_below(edge);
754 edge->fTop = v;
755 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000756 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500757 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400758 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800759}
760
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700761static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700762 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800763 remove_edge_above(edge);
764 edge->fBottom = v;
765 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000766 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500767 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400768 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800769}
770
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700771static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700772 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800773 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400774 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
775 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
776 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400777 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800778 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +0000779 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -0400780 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -0800781 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400782 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800783 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400784 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800785 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400786 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800787 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400788 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800789 }
790}
791
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700792static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700793 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800794 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400795 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
796 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
797 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400798 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800799 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +0000800 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -0400801 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -0800802 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400803 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800804 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400805 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800806 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400807 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800808 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400809 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800810 }
811}
812
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700813static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400814 if (!left || !right) {
815 return false;
816 }
817 return left->fTop->fPoint == right->fTop->fPoint ||
818 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
819}
820
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700821static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400822 if (!left || !right) {
823 return false;
824 }
825 return left->fBottom->fPoint == right->fBottom->fPoint ||
826 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
827}
828
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700829static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700830 const Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -0400831 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400832 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400833 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400834 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -0400835 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400836 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400837 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400838 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -0400839 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -0400840 } else {
841 break;
842 }
ethannicholase9709e82016-01-07 13:34:16 -0800843 }
Stephen Whited26b4d82018-07-26 10:02:27 -0400844 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
845 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
846 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
847 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -0800848}
849
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700850bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700851 const Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -0400852 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -0400853 return false;
Stephen White0cb31672017-06-08 14:41:01 -0400854 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400855 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
856 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400857 Vertex* top;
858 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -0400859 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800860 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400861 top = v;
862 bottom = edge->fTop;
863 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -0500864 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400865 top = edge->fBottom;
866 bottom = v;
867 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800868 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400869 top = v;
870 bottom = edge->fBottom;
871 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800872 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700873 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Chris Daltond60c9192021-01-07 18:30:08 +0000874 insert_edge_below(newEdge, top, c);
875 insert_edge_above(newEdge, bottom, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400876 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400877 return true;
878}
879
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700880bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700881 Vertex** current, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -0400882 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
883 return false;
884 }
Stephen White1c5fd182018-07-12 15:54:05 -0400885 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
886 return false;
887 }
Stephen White89042d52018-06-08 12:18:22 -0400888 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
889 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400890 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700891 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400892 }
893 } else {
894 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400895 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700896 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400897 }
898 }
899 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
900 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400901 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700902 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400903 }
904 } else {
905 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400906 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700907 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400908 }
909 }
910 return false;
ethannicholase9709e82016-01-07 13:34:16 -0800911}
912
Chris Dalton17ce8c52021-01-07 18:08:46 -0700913static Edge* connect(Vertex* prev, Vertex* next, EdgeType type, const Comparator& c,
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700914 SkArenaAlloc& alloc, int winding_scale = 1) {
Stephen Whitee260c462017-12-19 18:09:54 -0500915 if (!prev || !next || prev->fPoint == next->fPoint) {
916 return nullptr;
917 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500918 Edge* edge = new_edge(prev, next, type, c, alloc);
Chris Daltond60c9192021-01-07 18:30:08 +0000919 insert_edge_below(edge, edge->fTop, c);
920 insert_edge_above(edge, edge->fBottom, c);
Stephen White48ded382017-02-03 10:15:16 -0500921 edge->fWinding *= winding_scale;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400922 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -0700923 return edge;
924}
925
Chris Dalton811dc6a2021-01-07 16:40:32 -0700926static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400927 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
928 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -0500929 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -0400930 if (src->fPartner) {
931 src->fPartner->fPartner = dst;
932 }
Stephen White7b376942018-05-22 11:51:32 -0400933 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400934 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800935 }
Stephen White7b376942018-05-22 11:51:32 -0400936 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400937 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800938 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500939 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400940 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -0800941}
942
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700943static Vertex* create_sorted_vertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700944 Vertex* reference, const Comparator& c, SkArenaAlloc& alloc) {
Stephen White95152e12017-12-18 10:52:44 -0500945 Vertex* prevV = reference;
946 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
947 prevV = prevV->fPrev;
948 }
949 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
950 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
951 prevV = nextV;
952 nextV = nextV->fNext;
953 }
954 Vertex* v;
955 if (prevV && coincident(prevV->fPoint, p)) {
956 v = prevV;
957 } else if (nextV && coincident(nextV->fPoint, p)) {
958 v = nextV;
959 } else {
960 v = alloc.make<Vertex>(p, alpha);
Chris Dalton5045de32021-01-07 19:09:01 -0700961#if TRIANGULATOR_LOGGING
Stephen White95152e12017-12-18 10:52:44 -0500962 if (!prevV) {
963 v->fID = mesh->fHead->fID - 1.0f;
964 } else if (!nextV) {
965 v->fID = mesh->fTail->fID + 1.0f;
966 } else {
967 v->fID = (prevV->fID + nextV->fID) * 0.5f;
968 }
969#endif
970 mesh->insert(v, prevV, nextV);
971 }
972 return v;
973}
974
Stephen White53a02982018-05-30 22:47:46 -0400975// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
976// sort criterion, it may not be possible to split correctly, since there is no point which is
977// below the top and above the bottom. This function detects that case.
Chris Dalton811dc6a2021-01-07 16:40:32 -0700978static bool nearly_flat(const Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -0400979 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
980 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -0400981 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -0400982}
983
Chris Dalton811dc6a2021-01-07 16:40:32 -0700984static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -0400985 if (c.sweep_lt(p, min)) {
986 return min;
987 } else if (c.sweep_lt(max, p)) {
988 return max;
989 } else {
990 return p;
991 }
992}
993
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700994static void compute_bisector(Edge* edge1, Edge* edge2, Vertex* v, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400995 Line line1 = edge1->fLine;
996 Line line2 = edge2->fLine;
997 line1.normalize();
998 line2.normalize();
999 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
1000 if (cosAngle > 0.999) {
1001 return;
1002 }
1003 line1.fC += edge1->fWinding > 0 ? -1 : 1;
1004 line2.fC += edge2->fWinding > 0 ? -1 : 1;
1005 SkPoint p;
1006 if (line1.intersect(line2, &p)) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001007 uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001008 v->fPartner = alloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -04001009 TESS_LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001010 }
1011}
1012
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001013bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001014 Vertex** current, VertexList* mesh, const Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001015 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001016 return false;
ethannicholase9709e82016-01-07 13:34:16 -08001017 }
Stephen White56158ae2017-01-30 14:31:31 -05001018 SkPoint p;
1019 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001020 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001021 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001022 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001023 Vertex* top = *current;
1024 // If the intersection point is above the current vertex, rewind to the vertex above the
1025 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001026 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001027 top = top->fPrev;
1028 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001029 if (!nearly_flat(c, left)) {
1030 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001031 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001032 if (!nearly_flat(c, right)) {
1033 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001034 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001035 if (p == left->fTop->fPoint) {
1036 v = left->fTop;
1037 } else if (p == left->fBottom->fPoint) {
1038 v = left->fBottom;
1039 } else if (p == right->fTop->fPoint) {
1040 v = right->fTop;
1041 } else if (p == right->fBottom->fPoint) {
1042 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001043 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001044 v = create_sorted_vertex(p, alpha, mesh, top, c, fAlloc);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001045 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001046 v->fSynthetic = true;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001047 compute_bisector(left, right, v, fAlloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001048 }
ethannicholase9709e82016-01-07 13:34:16 -08001049 }
Stephen White0cb31672017-06-08 14:41:01 -04001050 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001051 this->splitEdge(left, v, activeEdges, current, c);
1052 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001053 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001054 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001055 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001056 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001057}
1058
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001059void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
Stephen White3a9aab92017-03-07 14:07:18 -05001060 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1061 SkASSERT(contour->fHead);
1062 Vertex* prev = contour->fTail;
Chris Dalton854ee852021-01-05 15:12:59 -07001063 if (fRoundVerticesToQuarterPixel) {
Stephen White3a9aab92017-03-07 14:07:18 -05001064 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001065 }
Stephen White3a9aab92017-03-07 14:07:18 -05001066 for (Vertex* v = contour->fHead; v;) {
Chris Dalton854ee852021-01-05 15:12:59 -07001067 if (fRoundVerticesToQuarterPixel) {
senorblancof57372d2016-08-31 10:36:19 -07001068 round(&v->fPoint);
1069 }
Stephen White3a9aab92017-03-07 14:07:18 -05001070 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001071 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001072 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001073 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001074 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001075 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001076 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001077 contour->remove(v);
Chris Dalton854ee852021-01-05 15:12:59 -07001078 } else if (fCullCollinearVertices &&
Chris Dalton6ccc0322020-01-29 11:38:16 -07001079 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001080 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001081 contour->remove(v);
1082 } else {
1083 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001084 }
Stephen White3a9aab92017-03-07 14:07:18 -05001085 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001086 }
1087 }
1088}
1089
Chris Dalton811dc6a2021-01-07 16:40:32 -07001090bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001091 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001092 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001093 }
Stephen Whitee260c462017-12-19 18:09:54 -05001094 bool merged = false;
1095 for (Vertex* v = mesh->fHead->fNext; v;) {
1096 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001097 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1098 v->fPoint = v->fPrev->fPoint;
1099 }
1100 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001101 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001102 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001103 }
Stephen Whitee260c462017-12-19 18:09:54 -05001104 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001105 }
Stephen Whitee260c462017-12-19 18:09:54 -05001106 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001107}
1108
1109// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1110
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001111void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001112 const Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001113 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1114 Vertex* prev = contour->fTail;
1115 for (Vertex* v = contour->fHead; v;) {
1116 Vertex* next = v->fNext;
Chris Dalton17ce8c52021-01-07 18:08:46 -07001117 connect(prev, v, EdgeType::kInner, c, fAlloc);
Stephen White3a9aab92017-03-07 14:07:18 -05001118 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001119 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001120 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001121 }
1122 }
ethannicholase9709e82016-01-07 13:34:16 -08001123}
1124
Chris Dalton811dc6a2021-01-07 16:40:32 -07001125static void connect_partners(VertexList* mesh, const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitee260c462017-12-19 18:09:54 -05001126 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001127 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001128 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1129 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1130 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1131 // number.
Chris Dalton17ce8c52021-01-07 18:08:46 -07001132 connect(outer, inner, EdgeType::kConnector, c, alloc, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001133 inner->fPartner = outer->fPartner = nullptr;
1134 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001135 }
1136 }
1137}
1138
1139template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001140static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001141 Vertex* a = front->fHead;
1142 Vertex* b = back->fHead;
1143 while (a && b) {
1144 if (sweep_lt(a->fPoint, b->fPoint)) {
1145 front->remove(a);
1146 result->append(a);
1147 a = front->fHead;
1148 } else {
1149 back->remove(b);
1150 result->append(b);
1151 b = back->fHead;
1152 }
1153 }
1154 result->append(*front);
1155 result->append(*back);
1156}
1157
Chris Dalton811dc6a2021-01-07 16:40:32 -07001158static void sorted_merge(VertexList* front, VertexList* back, VertexList* result,
1159 const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001160 if (c.fDirection == Comparator::Direction::kHorizontal) {
1161 sorted_merge<sweep_lt_horiz>(front, back, result);
1162 } else {
1163 sorted_merge<sweep_lt_vert>(front, back, result);
1164 }
Chris Dalton5045de32021-01-07 19:09:01 -07001165#if TRIANGULATOR_LOGGING
Stephen White3b5a3fa2017-06-06 14:51:19 -04001166 float id = 0.0f;
1167 for (Vertex* v = result->fHead; v; v = v->fNext) {
1168 v->fID = id++;
1169 }
1170#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001171}
1172
ethannicholase9709e82016-01-07 13:34:16 -08001173// Stage 3: sort the vertices by increasing sweep direction.
1174
Stephen White16a40cb2017-02-23 11:10:01 -05001175template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001176static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001177 Vertex* slow = vertices->fHead;
1178 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001179 return;
1180 }
Stephen White16a40cb2017-02-23 11:10:01 -05001181 Vertex* fast = slow->fNext;
1182 if (!fast) {
1183 return;
1184 }
1185 do {
1186 fast = fast->fNext;
1187 if (fast) {
1188 fast = fast->fNext;
1189 slow = slow->fNext;
1190 }
1191 } while (fast);
1192 VertexList front(vertices->fHead, slow);
1193 VertexList back(slow->fNext, vertices->fTail);
1194 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001195
Stephen White16a40cb2017-02-23 11:10:01 -05001196 merge_sort<sweep_lt>(&front);
1197 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001198
Stephen White16a40cb2017-02-23 11:10:01 -05001199 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001200 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001201}
1202
Chris Daltond60c9192021-01-07 18:30:08 +00001203static void dump_mesh(const VertexList& mesh) {
Chris Dalton5045de32021-01-07 19:09:01 -07001204#if TRIANGULATOR_LOGGING
Chris Daltond60c9192021-01-07 18:30:08 +00001205 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001206 TESS_LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001207 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001208 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1209 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001210 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001211 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001212 }
1213 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001214 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001215 }
1216 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001217 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001218 }
1219 }
Chris Dalton75887a22021-01-06 00:23:13 -07001220#endif
Chris Daltond60c9192021-01-07 18:30:08 +00001221}
Stephen White95152e12017-12-18 10:52:44 -05001222
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001223static void dump_skel(const SSEdgeList& ssEdges) {
Chris Dalton5045de32021-01-07 19:09:01 -07001224#if TRIANGULATOR_LOGGING
Stephen Whitec4dbc372019-05-22 10:50:14 -04001225 for (SSEdge* edge : ssEdges) {
1226 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001227 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001228 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001229 edge->fNext->fVertex->fID);
1230 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001231 TESS_LOG(" (original %g -> %g)\n",
1232 edge->fEdge->fTop->fID,
1233 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001234 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001235 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001236 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001237 }
1238 }
1239#endif
1240}
1241
Stephen White89042d52018-06-08 12:18:22 -04001242#ifdef SK_DEBUG
Chris Dalton811dc6a2021-01-07 16:40:32 -07001243static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001244 if (!left || !right) {
1245 return;
1246 }
1247 if (left->fTop == right->fTop) {
1248 SkASSERT(left->isLeftOf(right->fBottom));
1249 SkASSERT(right->isRightOf(left->fBottom));
1250 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1251 SkASSERT(left->isLeftOf(right->fTop));
1252 } else {
1253 SkASSERT(right->isRightOf(left->fTop));
1254 }
1255 if (left->fBottom == right->fBottom) {
1256 SkASSERT(left->isLeftOf(right->fTop));
1257 SkASSERT(right->isRightOf(left->fTop));
1258 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1259 SkASSERT(left->isLeftOf(right->fBottom));
1260 } else {
1261 SkASSERT(right->isRightOf(left->fBottom));
1262 }
1263}
1264
Chris Dalton811dc6a2021-01-07 16:40:32 -07001265static void validate_edge_list(EdgeList* edges, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001266 Edge* left = edges->fHead;
1267 if (!left) {
1268 return;
1269 }
1270 for (Edge* right = left->fRight; right; right = right->fRight) {
1271 validate_edge_pair(left, right, c);
1272 left = right;
1273 }
1274}
1275#endif
1276
ethannicholase9709e82016-01-07 13:34:16 -08001277// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1278
Chris Daltond60c9192021-01-07 18:30:08 +00001279static bool connected(Vertex* v) {
1280 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1281}
1282
Chris Dalton811dc6a2021-01-07 16:40:32 -07001283GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001284 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001285 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001286 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001287 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001288 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001289 continue;
1290 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001291 Edge* leftEnclosingEdge;
1292 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001293 bool restartChecks;
1294 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001295 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1296 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001297 restartChecks = false;
1298 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001299 v->fLeftEnclosingEdge = leftEnclosingEdge;
1300 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001301 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001302 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001303 if (this->checkForIntersection(
1304 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1305 this->checkForIntersection(
1306 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001307 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001308 return SimplifyResult::kAbort;
1309 }
1310 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001311 restartChecks = true;
1312 break;
1313 }
1314 }
1315 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001316 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1317 &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001318 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001319 return SimplifyResult::kAbort;
1320 }
1321 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001322 restartChecks = true;
1323 }
1324
1325 }
1326 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001327#ifdef SK_DEBUG
1328 validate_edge_list(&activeEdges, c);
1329#endif
ethannicholase9709e82016-01-07 13:34:16 -08001330 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +00001331 remove_edge(e, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001332 }
1333 Edge* leftEdge = leftEnclosingEdge;
1334 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001335 insert_edge(e, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001336 leftEdge = e;
1337 }
ethannicholase9709e82016-01-07 13:34:16 -08001338 }
Stephen Whitee260c462017-12-19 18:09:54 -05001339 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001340 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001341}
1342
1343// Stage 5: Tessellate the simplified mesh into monotone polygons.
1344
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001345Poly* GrTriangulator::tessellate(const VertexList& vertices) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001346 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001347 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton854ee852021-01-05 15:12:59 -07001348 if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001349 maxWindMagnitude = 1;
1350 }
ethannicholase9709e82016-01-07 13:34:16 -08001351 EdgeList activeEdges;
1352 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001353 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001354 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001355 continue;
1356 }
Chris Dalton5045de32021-01-07 19:09:01 -07001357#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001358 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001359#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001360 Edge* leftEnclosingEdge;
1361 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001362 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001363 Poly* leftPoly;
1364 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001365 if (v->fFirstEdgeAbove) {
1366 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1367 rightPoly = v->fLastEdgeAbove->fRightPoly;
1368 } else {
1369 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1370 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1371 }
Chris Dalton5045de32021-01-07 19:09:01 -07001372#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001373 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001374 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001375 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1376 e->fTop->fID, e->fBottom->fID,
1377 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1378 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001379 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001380 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001381 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001382 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1383 e->fTop->fID, e->fBottom->fID,
1384 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1385 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001386 }
1387#endif
1388 if (v->fFirstEdgeAbove) {
1389 if (leftPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001390 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001391 }
1392 if (rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001393 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001394 }
1395 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001396 Edge* rightEdge = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001397 remove_edge(e, &activeEdges);
Stephen White8a0bfc52017-02-21 15:24:13 -05001398 if (e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001399 e->fRightPoly->addEdge(e, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001400 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001401 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001402 rightEdge->fLeftPoly->addEdge(e, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001403 }
1404 }
Chris Daltond60c9192021-01-07 18:30:08 +00001405 remove_edge(v->fLastEdgeAbove, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001406 if (!v->fFirstEdgeBelow) {
1407 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1408 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1409 rightPoly->fPartner = leftPoly;
1410 leftPoly->fPartner = rightPoly;
1411 }
1412 }
1413 }
1414 if (v->fFirstEdgeBelow) {
1415 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001416 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001417 if (leftPoly == rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001418 if (leftPoly->fTail && leftPoly->fTail->fSide == kLeft_Side) {
senorblanco531237e2016-06-02 11:36:48 -07001419 leftPoly = new_poly(&polys, leftPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001420 leftPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001421 leftEnclosingEdge->fRightPoly = leftPoly;
1422 } else {
1423 rightPoly = new_poly(&polys, rightPoly->lastVertex(),
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001424 rightPoly->fWinding, fAlloc);
senorblanco531237e2016-06-02 11:36:48 -07001425 rightEnclosingEdge->fLeftPoly = rightPoly;
1426 }
ethannicholase9709e82016-01-07 13:34:16 -08001427 }
Chris Daltond60c9192021-01-07 18:30:08 +00001428 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
Chris Dalton17ce8c52021-01-07 18:08:46 -07001429 EdgeType::kInner);
1430 leftPoly = leftPoly->addEdge(join, kRight_Side, fAlloc);
1431 rightPoly = rightPoly->addEdge(join, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001432 }
1433 }
1434 Edge* leftEdge = v->fFirstEdgeBelow;
1435 leftEdge->fLeftPoly = leftPoly;
Chris Daltond60c9192021-01-07 18:30:08 +00001436 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001437 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1438 rightEdge = rightEdge->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001439 insert_edge(rightEdge, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001440 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1441 winding += leftEdge->fWinding;
1442 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001443 if (abs(winding) > maxWindMagnitude) {
1444 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1445 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001446 Poly* poly = new_poly(&polys, v, winding, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001447 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1448 }
1449 leftEdge = rightEdge;
1450 }
1451 v->fLastEdgeBelow->fRightPoly = rightPoly;
1452 }
Chris Dalton5045de32021-01-07 19:09:01 -07001453#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001454 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001455 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001456 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1457 e->fTop->fID, e->fBottom->fID,
1458 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1459 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001460 }
1461#endif
1462 }
1463 return polys;
1464}
1465
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001466static void remove_non_boundary_edges(const VertexList& mesh, SkPathFillType fillType,
1467 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001468 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001469 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001470 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001471 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001472 continue;
1473 }
1474 Edge* leftEnclosingEdge;
1475 Edge* rightEnclosingEdge;
1476 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1477 bool prevFilled = leftEnclosingEdge &&
1478 apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1479 for (Edge* e = v->fFirstEdgeAbove; e;) {
1480 Edge* next = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001481 remove_edge(e, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001482 bool filled = apply_fill_type(fillType, e->fWinding);
1483 if (filled == prevFilled) {
Chris Daltond60c9192021-01-07 18:30:08 +00001484 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001485 }
Stephen White49789062017-02-21 10:35:49 -05001486 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001487 e = next;
1488 }
Stephen White49789062017-02-21 10:35:49 -05001489 Edge* prev = leftEnclosingEdge;
1490 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1491 if (prev) {
1492 e->fWinding += prev->fWinding;
1493 }
Chris Daltond60c9192021-01-07 18:30:08 +00001494 insert_edge(e, prev, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001495 prev = e;
1496 }
senorblancof57372d2016-08-31 10:36:19 -07001497 }
senorblancof57372d2016-08-31 10:36:19 -07001498}
1499
Stephen White66412122017-03-01 11:48:27 -05001500// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001501static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001502 normal->set(SkDoubleToScalar(e->fLine.fA),
1503 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001504}
1505
1506// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1507// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1508// invert on stroking.
1509
Chris Dalton811dc6a2021-01-07 16:40:32 -07001510static void simplify_boundary(EdgeList* boundary, const Comparator& c, SkArenaAlloc& alloc) {
senorblancof57372d2016-08-31 10:36:19 -07001511 Edge* prevEdge = boundary->fTail;
1512 SkVector prevNormal;
1513 get_edge_normal(prevEdge, &prevNormal);
1514 for (Edge* e = boundary->fHead; e != nullptr;) {
1515 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1516 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001517 double distPrev = e->dist(prev->fPoint);
1518 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001519 SkVector normal;
1520 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001521 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001522 if (prev == next) {
Chris Daltond60c9192021-01-07 18:30:08 +00001523 remove_edge(prevEdge, boundary);
1524 remove_edge(e, boundary);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001525 prevEdge = boundary->fTail;
1526 e = boundary->fHead;
1527 if (prevEdge) {
1528 get_edge_normal(prevEdge, &prevNormal);
1529 }
1530 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001531 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001532 Edge* join = new_edge(prev, next, EdgeType::kInner, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001533 if (prev->fPoint != next->fPoint) {
1534 join->fLine.normalize();
1535 join->fLine = join->fLine * join->fWinding;
1536 }
Chris Daltond60c9192021-01-07 18:30:08 +00001537 insert_edge(join, e, boundary);
1538 remove_edge(prevEdge, boundary);
1539 remove_edge(e, boundary);
senorblancof57372d2016-08-31 10:36:19 -07001540 if (join->fLeft && join->fRight) {
1541 prevEdge = join->fLeft;
1542 e = join;
1543 } else {
1544 prevEdge = boundary->fTail;
1545 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1546 }
1547 get_edge_normal(prevEdge, &prevNormal);
1548 } else {
1549 prevEdge = e;
1550 prevNormal = normal;
1551 e = e->fRight;
1552 }
1553 }
1554}
1555
Chris Dalton811dc6a2021-01-07 16:40:32 -07001556static void ss_connect(Vertex* v, Vertex* dest, const Comparator& c, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001557 if (v == dest) {
1558 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001559 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001560 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001561 if (v->fSynthetic) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001562 connect(v, dest, EdgeType::kConnector, c, alloc, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001563 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001564 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1565 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001566 v->fPartner->fPartner = dest;
1567 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001568 }
1569}
1570
Chris Dalton811dc6a2021-01-07 16:40:32 -07001571void Event::apply(VertexList* mesh, const Comparator& c, EventList* events, SkArenaAlloc& alloc) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001572 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001573 return;
1574 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001575 Vertex* prev = fEdge->fPrev->fVertex;
1576 Vertex* next = fEdge->fNext->fVertex;
1577 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1578 SSEdge* nextEdge = fEdge->fNext->fNext;
1579 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1580 return;
Stephen White77169c82018-06-05 09:15:59 -04001581 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001582 Vertex* dest = create_sorted_vertex(fPoint, fAlpha, mesh, prev, c, alloc);
1583 dest->fSynthetic = true;
1584 SSVertex* ssv = alloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001585 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1586 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1587 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001588 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001589
Stephen Whitec4dbc372019-05-22 10:50:14 -04001590 ss_connect(prev, dest, c, alloc);
1591 ss_connect(next, dest, c, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001592
Stephen Whitec4dbc372019-05-22 10:50:14 -04001593 prevEdge->fNext = nextEdge->fPrev = ssv;
1594 ssv->fPrev = prevEdge;
1595 ssv->fNext = nextEdge;
1596 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1597 return;
1598 }
1599 if (prevEdge->fEvent) {
1600 prevEdge->fEvent->fEdge = nullptr;
1601 }
1602 if (nextEdge->fEvent) {
1603 nextEdge->fEvent->fEdge = nullptr;
1604 }
1605 if (prevEdge->fPrev == nextEdge->fNext) {
1606 ss_connect(prevEdge->fPrev->fVertex, dest, c, alloc);
1607 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1608 } else {
1609 compute_bisector(prevEdge->fEdge, nextEdge->fEdge, dest, alloc);
1610 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1611 if (dest->fPartner) {
1612 create_event(prevEdge, events, alloc);
1613 create_event(nextEdge, events, alloc);
1614 } else {
1615 create_event(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c, alloc);
1616 create_event(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c, alloc);
1617 }
1618 }
Stephen Whitee260c462017-12-19 18:09:54 -05001619}
1620
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001621static bool is_overlap_edge(Edge* e) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001622 if (e->fType == EdgeType::kOuter) {
Stephen Whitee260c462017-12-19 18:09:54 -05001623 return e->fWinding != 0 && e->fWinding != 1;
Chris Dalton17ce8c52021-01-07 18:08:46 -07001624 } else if (e->fType == EdgeType::kInner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001625 return e->fWinding != 0 && e->fWinding != -2;
1626 } else {
1627 return false;
1628 }
1629}
1630
1631// This is a stripped-down version of tessellate() which computes edges which
1632// join two filled regions, which represent overlap regions, and collapses them.
Chris Dalton811dc6a2021-01-07 16:40:32 -07001633static bool collapse_overlap_regions(VertexList* mesh, const Comparator& c, SkArenaAlloc& alloc,
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001634 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001635 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001636 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001637 EventList events(comp);
1638 SSVertexMap ssVertices;
1639 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001640 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001641 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001642 continue;
1643 }
1644 Edge* leftEnclosingEdge;
1645 Edge* rightEnclosingEdge;
1646 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001647 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001648 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
Chris Daltond60c9192021-01-07 18:30:08 +00001649 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001650 bool leftOverlap = prev && is_overlap_edge(prev);
1651 bool rightOverlap = is_overlap_edge(e);
Chris Dalton17ce8c52021-01-07 18:08:46 -07001652 bool isOuterBoundary = e->fType == EdgeType::kOuter &&
Stephen Whitec4dbc372019-05-22 10:50:14 -04001653 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001654 if (prev) {
1655 e->fWinding -= prev->fWinding;
1656 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001657 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001658 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1659 e->fTop->fID, e->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +00001660 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001661 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001662 TESS_LOG("found overlap edge %g -> %g%s\n",
1663 e->fTop->fID, e->fBottom->fID,
1664 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001665 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1666 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1667 SSVertex* ssPrev = ssVertices[prevVertex];
1668 if (!ssPrev) {
1669 ssPrev = ssVertices[prevVertex] = alloc.make<SSVertex>(prevVertex);
1670 }
1671 SSVertex* ssNext = ssVertices[nextVertex];
1672 if (!ssNext) {
1673 ssNext = ssVertices[nextVertex] = alloc.make<SSVertex>(nextVertex);
1674 }
1675 SSEdge* ssEdge = alloc.make<SSEdge>(e, ssPrev, ssNext);
1676 ssEdges.push_back(ssEdge);
1677// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1678 ssPrev->fNext = ssNext->fPrev = ssEdge;
1679 create_event(ssEdge, &events, alloc);
1680 if (!isOuterBoundary) {
Chris Daltond60c9192021-01-07 18:30:08 +00001681 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001682 }
1683 }
1684 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001685 }
1686 Edge* prev = leftEnclosingEdge;
1687 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1688 if (prev) {
1689 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001690 }
Chris Daltond60c9192021-01-07 18:30:08 +00001691 insert_edge(e, prev, &activeEdges);
Stephen Whitee260c462017-12-19 18:09:54 -05001692 prev = e;
1693 }
1694 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001695 bool complex = events.size() > 0;
1696
Brian Salomon120e7d62019-09-11 10:29:22 -04001697 TESS_LOG("\ncollapsing overlap regions\n");
1698 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001699 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001700 while (events.size() > 0) {
1701 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001702 events.pop();
Stephen Whitec4dbc372019-05-22 10:50:14 -04001703 event->apply(mesh, c, &events, alloc);
Stephen Whitee260c462017-12-19 18:09:54 -05001704 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001705 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001706 dump_skel(ssEdges);
1707 for (SSEdge* edge : ssEdges) {
1708 if (Edge* e = edge->fEdge) {
1709 connect(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, alloc, 0);
1710 }
1711 }
1712 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001713}
1714
Chris Dalton811dc6a2021-01-07 16:40:32 -07001715static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001716 if (!prev || !next) {
1717 return true;
1718 }
1719 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1720 return winding != origEdge->fWinding;
1721}
Stephen White92eba8a2017-02-06 09:50:27 -05001722
senorblancof57372d2016-08-31 10:36:19 -07001723// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1724// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1725// new antialiased mesh from those vertices.
1726
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001727static void stroke_boundary(EdgeList* boundary, VertexList* innerMesh, VertexList* outerMesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001728 const Comparator& c, SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001729 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001730 // A boundary with fewer than 3 edges is degenerate.
1731 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1732 return;
1733 }
1734 Edge* prevEdge = boundary->fTail;
1735 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1736 SkVector prevNormal;
1737 get_edge_normal(prevEdge, &prevNormal);
1738 double radius = 0.5;
1739 Line prevInner(prevEdge->fLine);
1740 prevInner.fC -= radius;
1741 Line prevOuter(prevEdge->fLine);
1742 prevOuter.fC += radius;
1743 VertexList innerVertices;
1744 VertexList outerVertices;
1745 bool innerInversion = true;
1746 bool outerInversion = true;
1747 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1748 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1749 SkVector normal;
1750 get_edge_normal(e, &normal);
1751 Line inner(e->fLine);
1752 inner.fC -= radius;
1753 Line outer(e->fLine);
1754 outer.fC += radius;
1755 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001756 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001757 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1758 prevOuter.intersect(outer, &outerPoint)) {
1759 float cosAngle = normal.dot(prevNormal);
1760 if (cosAngle < -kCosMiterAngle) {
1761 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1762
1763 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1764 Line bisector(innerPoint, outerPoint);
1765 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1766 if (tangent.fA == 0 && tangent.fB == 0) {
1767 continue;
1768 }
1769 tangent.normalize();
1770 Line innerTangent(tangent);
1771 Line outerTangent(tangent);
1772 innerTangent.fC -= 0.5;
1773 outerTangent.fC += 0.5;
1774 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1775 if (prevNormal.cross(normal) > 0) {
1776 // Miter inner points
1777 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1778 !innerTangent.intersect(inner, &innerPoint2) ||
1779 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001780 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001781 }
1782 Line prevTangent(prevV->fPoint,
1783 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1784 Line nextTangent(nextV->fPoint,
1785 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001786 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001787 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001788 }
1789 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001790 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001791 }
1792 outerPoint1 = outerPoint2 = outerPoint;
1793 } else {
1794 // Miter outer points
1795 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1796 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001797 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001798 }
1799 Line prevTangent(prevV->fPoint,
1800 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1801 Line nextTangent(nextV->fPoint,
1802 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001803 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001804 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001805 }
1806 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001807 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001808 }
1809 innerPoint1 = innerPoint2 = innerPoint;
1810 }
Stephen Whiteea495232018-04-03 11:28:15 -04001811 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1812 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1813 continue;
1814 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001815 TESS_LOG("inner (%g, %g), (%g, %g), ",
1816 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1817 TESS_LOG("outer (%g, %g), (%g, %g)\n",
1818 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001819 Vertex* innerVertex1 = alloc.make<Vertex>(innerPoint1, 255);
1820 Vertex* innerVertex2 = alloc.make<Vertex>(innerPoint2, 255);
1821 Vertex* outerVertex1 = alloc.make<Vertex>(outerPoint1, 0);
1822 Vertex* outerVertex2 = alloc.make<Vertex>(outerPoint2, 0);
1823 innerVertex1->fPartner = outerVertex1;
1824 innerVertex2->fPartner = outerVertex2;
1825 outerVertex1->fPartner = innerVertex1;
1826 outerVertex2->fPartner = innerVertex2;
1827 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1828 innerInversion = false;
1829 }
1830 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1831 outerInversion = false;
1832 }
1833 innerVertices.append(innerVertex1);
1834 innerVertices.append(innerVertex2);
1835 outerVertices.append(outerVertex1);
1836 outerVertices.append(outerVertex2);
1837 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001838 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1839 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001840 Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
1841 Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1842 innerVertex->fPartner = outerVertex;
1843 outerVertex->fPartner = innerVertex;
1844 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1845 innerInversion = false;
1846 }
1847 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
1848 outerInversion = false;
1849 }
1850 innerVertices.append(innerVertex);
1851 outerVertices.append(outerVertex);
1852 }
1853 }
1854 prevInner = inner;
1855 prevOuter = outer;
1856 prevV = v;
1857 prevEdge = e;
1858 prevNormal = normal;
1859 }
1860 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
1861 innerInversion = false;
1862 }
1863 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
1864 outerInversion = false;
1865 }
1866 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
1867 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
1868 // interior inverts).
1869 // For total inversion cases, the shape has now reversed handedness, so invert the winding
1870 // so it will be detected during collapse_overlap_regions().
1871 int innerWinding = innerInversion ? 2 : -2;
1872 int outerWinding = outerInversion ? -1 : 1;
1873 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001874 connect(v, v->fNext, EdgeType::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001875 }
Chris Dalton17ce8c52021-01-07 18:08:46 -07001876 connect(innerVertices.fTail, innerVertices.fHead, EdgeType::kInner, c, alloc, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001877 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001878 connect(v, v->fNext, EdgeType::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001879 }
Chris Dalton17ce8c52021-01-07 18:08:46 -07001880 connect(outerVertices.fTail, outerVertices.fHead, EdgeType::kOuter, c, alloc, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001881 innerMesh->append(innerVertices);
1882 outerMesh->append(outerVertices);
1883}
senorblancof57372d2016-08-31 10:36:19 -07001884
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001885static void extract_boundary(EdgeList* boundary, Edge* e, SkPathFillType fillType,
1886 SkArenaAlloc& alloc) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001887 TESS_LOG("\nextracting boundary\n");
Stephen White49789062017-02-21 10:35:49 -05001888 bool down = apply_fill_type(fillType, e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04001889 Vertex* start = down ? e->fTop : e->fBottom;
1890 do {
senorblancof57372d2016-08-31 10:36:19 -07001891 e->fWinding = down ? 1 : -1;
1892 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05001893 e->fLine.normalize();
1894 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07001895 boundary->append(e);
1896 if (down) {
1897 // Find outgoing edge, in clockwise order.
1898 if ((next = e->fNextEdgeAbove)) {
1899 down = false;
1900 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1901 down = true;
1902 } else if ((next = e->fPrevEdgeAbove)) {
1903 down = false;
1904 }
1905 } else {
1906 // Find outgoing edge, in counter-clockwise order.
1907 if ((next = e->fPrevEdgeBelow)) {
1908 down = true;
1909 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1910 down = false;
1911 } else if ((next = e->fNextEdgeBelow)) {
1912 down = true;
1913 }
1914 }
Chris Daltond60c9192021-01-07 18:30:08 +00001915 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001916 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04001917 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07001918}
1919
Stephen White5ad721e2017-02-23 16:50:47 -05001920// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07001921
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001922static void extract_boundaries(const VertexList& inMesh, VertexList* innerVertices,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001923 VertexList* outerVertices, SkPathFillType fillType,
1924 const Comparator& c, SkArenaAlloc& alloc) {
Stephen White5ad721e2017-02-23 16:50:47 -05001925 remove_non_boundary_edges(inMesh, fillType, alloc);
1926 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001927 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05001928 EdgeList boundary;
1929 extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
1930 simplify_boundary(&boundary, c, alloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04001931 stroke_boundary(&boundary, innerVertices, outerVertices, c, alloc);
senorblancof57372d2016-08-31 10:36:19 -07001932 }
1933 }
senorblancof57372d2016-08-31 10:36:19 -07001934}
1935
Stephen Whitebda29c02017-03-13 15:10:13 -04001936// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08001937
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001938void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001939 const Comparator& c) {
Chris Dalton5045de32021-01-07 19:09:01 -07001940#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -08001941 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05001942 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08001943 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04001944 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001945 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001946 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08001947 }
1948 }
1949#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001950 this->sanitizeContours(contours, contourCnt);
1951 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07001952}
1953
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001954void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001955 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05001956 return;
ethannicholase9709e82016-01-07 13:34:16 -08001957 }
1958
1959 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05001960 if (c.fDirection == Comparator::Direction::kHorizontal) {
1961 merge_sort<sweep_lt_horiz>(vertices);
1962 } else {
1963 merge_sort<sweep_lt_vert>(vertices);
1964 }
Chris Dalton5045de32021-01-07 19:09:01 -07001965#if TRIANGULATOR_LOGGING
Stephen White2e2cb9b2017-01-09 13:11:18 -05001966 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001967 static float gID = 0.0f;
1968 v->fID = gID++;
1969 }
1970#endif
Stephen White2f4686f2017-01-03 16:20:01 -05001971}
1972
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001973Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh) {
1974 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05001975 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
1976 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05001977 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001978 this->contoursToMesh(contours, contourCnt, &mesh, c);
1979 SortMesh(&mesh, c);
1980 this->mergeCoincidentVertices(&mesh, c);
1981 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001982 return nullptr;
1983 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001984 TESS_LOG("\nsimplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00001985 dump_mesh(mesh);
Chris Dalton854ee852021-01-05 15:12:59 -07001986 if (fEmitCoverage) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001987 VertexList innerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001988 extract_boundaries(mesh, &innerMesh, outerMesh, fPath.getFillType(), c, fAlloc);
1989 SortMesh(&innerMesh, c);
1990 SortMesh(outerMesh, c);
1991 this->mergeCoincidentVertices(&innerMesh, c);
1992 bool was_complex = this->mergeCoincidentVertices(outerMesh, c);
1993 auto result = this->simplify(&innerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001994 SkASSERT(SimplifyResult::kAbort != result);
1995 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001996 result = this->simplify(outerMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001997 SkASSERT(SimplifyResult::kAbort != result);
1998 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Brian Salomon120e7d62019-09-11 10:29:22 -04001999 TESS_LOG("\ninner mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002000 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002001 TESS_LOG("\nouter mesh before:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002002 dump_mesh(*outerMesh);
Stephen Whitec4dbc372019-05-22 10:50:14 -04002003 EventComparator eventLT(EventComparator::Op::kLessThan);
2004 EventComparator eventGT(EventComparator::Op::kGreaterThan);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002005 was_complex = collapse_overlap_regions(&innerMesh, c, fAlloc, eventLT) || was_complex;
2006 was_complex = collapse_overlap_regions(outerMesh, c, fAlloc, eventGT) || was_complex;
Stephen Whitee260c462017-12-19 18:09:54 -05002007 if (was_complex) {
Brian Salomon120e7d62019-09-11 10:29:22 -04002008 TESS_LOG("found complex mesh; taking slow path\n");
Stephen Whitebda29c02017-03-13 15:10:13 -04002009 VertexList aaMesh;
Brian Salomon120e7d62019-09-11 10:29:22 -04002010 TESS_LOG("\ninner mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002011 dump_mesh(innerMesh);
Brian Salomon120e7d62019-09-11 10:29:22 -04002012 TESS_LOG("\nouter mesh after:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002013 dump_mesh(*outerMesh);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002014 connect_partners(outerMesh, c, fAlloc);
2015 connect_partners(&innerMesh, c, fAlloc);
Stephen Whitebda29c02017-03-13 15:10:13 -04002016 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002017 this->mergeCoincidentVertices(&aaMesh, c);
2018 result = this->simplify(&aaMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002019 SkASSERT(SimplifyResult::kAbort != result);
Brian Salomon120e7d62019-09-11 10:29:22 -04002020 TESS_LOG("combined and simplified mesh:\n");
Chris Daltond60c9192021-01-07 18:30:08 +00002021 dump_mesh(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002022 outerMesh->fHead = outerMesh->fTail = nullptr;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002023 return this->tessellate(aaMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002024 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04002025 TESS_LOG("no complex polygons; taking fast path\n");
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002026 return this->tessellate(innerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002027 }
Stephen White49789062017-02-21 10:35:49 -05002028 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002029 return this->tessellate(mesh);
senorblancof57372d2016-08-31 10:36:19 -07002030 }
senorblancof57372d2016-08-31 10:36:19 -07002031}
2032
2033// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002034void* GrTriangulator::polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType) {
senorblancof57372d2016-08-31 10:36:19 -07002035 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002036 if (apply_fill_type(overrideFillType, poly)) {
Chris Daltond60c9192021-01-07 18:30:08 +00002037 data = poly->emit(fEmitCoverage, data);
senorblancof57372d2016-08-31 10:36:19 -07002038 }
2039 }
2040 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002041}
2042
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002043Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
2044 VertexList* outerMesh) {
2045 if (SkPathFillType_IsInverse(fPath.getFillType())) {
ethannicholase9709e82016-01-07 13:34:16 -08002046 contourCnt++;
2047 }
Stephen White3a9aab92017-03-07 14:07:18 -05002048 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002049
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002050 this->pathToContours(tolerance, clipBounds, contours.get());
2051 return this->contoursToPolys(contours.get(), contourCnt, outerMesh);
ethannicholase9709e82016-01-07 13:34:16 -08002052}
2053
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002054static int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002055 // We could theoretically be more aggressive about not counting empty contours, but we need to
2056 // actually match the exact number of contour linked lists the tessellator will create later on.
2057 int contourCnt = 1;
2058 bool hasPoints = false;
2059
Chris Dalton7156db22020-05-07 22:06:28 +00002060 SkPath::Iter iter(path, false);
2061 SkPath::Verb verb;
2062 SkPoint pts[4];
Chris Daltonc71b3d42020-01-08 21:29:59 -07002063 bool first = true;
Chris Dalton7156db22020-05-07 22:06:28 +00002064 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002065 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +00002066 case SkPath::kMove_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002067 if (!first) {
2068 ++contourCnt;
2069 }
John Stiles30212b72020-06-11 17:55:07 -04002070 [[fallthrough]];
Chris Dalton7156db22020-05-07 22:06:28 +00002071 case SkPath::kLine_Verb:
2072 case SkPath::kConic_Verb:
2073 case SkPath::kQuad_Verb:
2074 case SkPath::kCubic_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002075 hasPoints = true;
John Stiles30212b72020-06-11 17:55:07 -04002076 break;
Chris Daltonc71b3d42020-01-08 21:29:59 -07002077 default:
2078 break;
2079 }
2080 first = false;
2081 }
2082 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002083 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002084 }
Stephen White11f65e02017-02-16 19:00:39 -05002085 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002086}
2087
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002088static int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002089 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002090 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002091 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002092 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002093 }
2094 }
2095 return count;
2096}
2097
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002098static int64_t count_outer_mesh_points(const VertexList& outerMesh) {
Greg Danield5b45932018-06-07 13:15:10 -04002099 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002100 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2101 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002102 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002103 }
2104 }
2105 return count;
2106}
2107
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002108static void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002109 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2110 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2111 Vertex* v0 = e->fTop;
2112 Vertex* v1 = e->fBottom;
2113 Vertex* v2 = e->fBottom->fPartner;
2114 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002115 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2116 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002117 }
2118 }
2119 return data;
2120}
2121
ethannicholase9709e82016-01-07 13:34:16 -08002122// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2123
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002124int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
2125 GrEagerVertexAllocator* vertexAllocator,
2126 SkPathFillType overrideFillType) {
2127 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002128 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002129 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002130 return 0;
2131 }
Stephen Whitebda29c02017-03-13 15:10:13 -04002132 VertexList outerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002133 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt, &outerMesh);
2134 int64_t count64 = count_points(polys, overrideFillType);
Chris Dalton854ee852021-01-05 15:12:59 -07002135 if (fEmitCoverage) {
Greg Danield5b45932018-06-07 13:15:10 -04002136 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002137 }
Greg Danield5b45932018-06-07 13:15:10 -04002138 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002139 return 0;
2140 }
Greg Danield5b45932018-06-07 13:15:10 -04002141 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002142
Chris Dalton854ee852021-01-05 15:12:59 -07002143 size_t vertexStride = sizeof(SkPoint);
2144 if (fEmitCoverage) {
2145 vertexStride += sizeof(float);
2146 }
Chris Daltond081dce2020-01-23 12:09:04 -07002147 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002148 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002149 SkDebugf("Could not allocate vertices\n");
2150 return 0;
2151 }
senorblancof57372d2016-08-31 10:36:19 -07002152
Brian Salomon120e7d62019-09-11 10:29:22 -04002153 TESS_LOG("emitting %d verts\n", count);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002154 void* end = this->polysToTriangles(polys, verts, overrideFillType);
Brian Osman80879d42019-01-07 16:15:27 -05002155 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002156
senorblancof57372d2016-08-31 10:36:19 -07002157 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002158 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002159 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002160 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002161 return actualCount;
2162}
2163
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002164int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2165 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002166 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002167 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002168 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002169 return 0;
2170 }
Chris Dalton854ee852021-01-05 15:12:59 -07002171 GrTriangulator triangulator(path);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002172 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002173 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002174 int64_t count64 = count_points(polys, fillType);
2175 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002176 *verts = nullptr;
2177 return 0;
2178 }
Greg Danield5b45932018-06-07 13:15:10 -04002179 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002180
Chris Dalton17dc4182020-03-25 16:18:16 -06002181 *verts = new WindingVertex[count];
2182 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002183 SkPoint* points = new SkPoint[count];
2184 SkPoint* pointsEnd = points;
2185 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002186 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002187 SkPoint* start = pointsEnd;
Chris Daltond60c9192021-01-07 18:30:08 +00002188 pointsEnd = static_cast<SkPoint*>(poly->emit(false, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002189 while (start != pointsEnd) {
2190 vertsEnd->fPos = *start;
2191 vertsEnd->fWinding = poly->fWinding;
2192 ++start;
2193 ++vertsEnd;
2194 }
2195 }
2196 }
2197 int actualCount = static_cast<int>(vertsEnd - *verts);
2198 SkASSERT(actualCount <= count);
2199 SkASSERT(pointsEnd - points == actualCount);
2200 delete[] points;
2201 return actualCount;
2202}