blob: 5d86e4cae8c62735fffb7872c3c47e3677d2bcfa [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
Chris Dalton7cf3add2021-01-11 18:33:28 -070026#define DUMP_MESH(M) dump_mesh(M)
ethannicholase9709e82016-01-07 13:34:16 -080027#else
Brian Salomon120e7d62019-09-11 10:29:22 -040028#define TESS_LOG(...)
Chris Dalton7cf3add2021-01-11 18:33:28 -070029#define DUMP_MESH(M)
ethannicholase9709e82016-01-07 13:34:16 -080030#endif
31
Chris Dalton57ea1fc2021-01-05 13:37:44 -070032constexpr static float kCosMiterAngle = 0.97f; // Corresponds to an angle of ~14 degrees.
ethannicholase9709e82016-01-07 13:34:16 -080033
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;
Chris Dalton7cf3add2021-01-11 18:33:28 -070043using SSEdge = GrAATriangulator::SSEdge;
44using EventList = GrAATriangulator::EventList;
45using Event = GrAATriangulator::Event;
46using EventComparator = GrAATriangulator::EventComparator;
ethannicholase9709e82016-01-07 13:34:16 -080047
48template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070049static void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080050 t->*Prev = prev;
51 t->*Next = next;
52 if (prev) {
53 prev->*Next = t;
54 } else if (head) {
55 *head = t;
56 }
57 if (next) {
58 next->*Prev = t;
59 } else if (tail) {
60 *tail = t;
61 }
62}
63
64template <class T, T* T::*Prev, T* T::*Next>
Chris Dalton57ea1fc2021-01-05 13:37:44 -070065static void list_remove(T* t, T** head, T** tail) {
ethannicholase9709e82016-01-07 13:34:16 -080066 if (t->*Prev) {
67 t->*Prev->*Next = t->*Next;
68 } else if (head) {
69 *head = t->*Next;
70 }
71 if (t->*Next) {
72 t->*Next->*Prev = t->*Prev;
73 } else if (tail) {
74 *tail = t->*Prev;
75 }
76 t->*Prev = t->*Next = nullptr;
77}
78
ethannicholase9709e82016-01-07 13:34:16 -080079typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
80
Chris Dalton57ea1fc2021-01-05 13:37:44 -070081static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -050082 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
ethannicholase9709e82016-01-07 13:34:16 -080083}
84
Chris Dalton57ea1fc2021-01-05 13:37:44 -070085static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
Stephen White16a40cb2017-02-23 11:10:01 -050086 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
ethannicholase9709e82016-01-07 13:34:16 -080087}
88
Chris Dalton5045de32021-01-07 19:09:01 -070089bool GrTriangulator::Comparator::sweep_lt(const SkPoint& a, const SkPoint& b) const {
90 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
91}
Stephen White16a40cb2017-02-23 11:10:01 -050092
Chris Daltond60c9192021-01-07 18:30:08 +000093static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
Brian Osmanf9aabff2018-11-13 16:11:38 -050094 GrVertexWriter verts{data};
95 verts.write(v->fPoint);
96
Brian Osman80879d42019-01-07 16:15:27 -050097 if (emitCoverage) {
98 verts.write(GrNormalizeByteToFloat(v->fAlpha));
99 }
Brian Osman0995fd52019-01-09 09:52:25 -0500100
Brian Osmanf9aabff2018-11-13 16:11:38 -0500101 return verts.fPtr;
ethannicholase9709e82016-01-07 13:34:16 -0800102}
103
Chris Daltond60c9192021-01-07 18:30:08 +0000104static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400105 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
106 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
107 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
senorblancof57372d2016-08-31 10:36:19 -0700108#if TESSELLATOR_WIREFRAME
Brian Osman0995fd52019-01-09 09:52:25 -0500109 data = emit_vertex(v0, emitCoverage, data);
110 data = emit_vertex(v1, emitCoverage, data);
111 data = emit_vertex(v1, emitCoverage, data);
112 data = emit_vertex(v2, emitCoverage, data);
113 data = emit_vertex(v2, emitCoverage, data);
114 data = emit_vertex(v0, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800115#else
Brian Osman0995fd52019-01-09 09:52:25 -0500116 data = emit_vertex(v0, emitCoverage, data);
117 data = emit_vertex(v1, emitCoverage, data);
118 data = emit_vertex(v2, emitCoverage, data);
ethannicholase9709e82016-01-07 13:34:16 -0800119#endif
120 return data;
121}
122
Chris Dalton5045de32021-01-07 19:09:01 -0700123void GrTriangulator::VertexList::insert(Vertex* v, Vertex* prev, Vertex* next) {
124 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
125}
126
127void GrTriangulator::VertexList::remove(Vertex* v) {
128 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
129}
senorblancoe6eaa322016-03-08 09:06:44 -0800130
senorblancof57372d2016-08-31 10:36:19 -0700131// Round to nearest quarter-pixel. This is used for screenspace tessellation.
132
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700133static inline void round(SkPoint* p) {
senorblancof57372d2016-08-31 10:36:19 -0700134 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
135 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
136}
137
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700138static inline SkScalar double_to_clamped_scalar(double d) {
Stephen White94b7e542018-01-04 14:01:10 -0500139 return SkDoubleToScalar(std::min((double) SK_ScalarMax, std::max(d, (double) -SK_ScalarMax)));
140}
141
Chris Dalton5045de32021-01-07 19:09:01 -0700142bool GrTriangulator::Line::intersect(const Line& other, SkPoint* point) const {
143 double denom = fA * other.fB - fB * other.fA;
144 if (denom == 0.0) {
145 return false;
senorblanco49df8d12016-10-07 08:36:56 -0700146 }
Chris Dalton5045de32021-01-07 19:09:01 -0700147 double scale = 1.0 / denom;
148 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
149 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
150 round(point);
151 return point->isFinite();
152}
Chris Daltond60c9192021-01-07 18:30:08 +0000153
Chris Dalton5045de32021-01-07 19:09:01 -0700154bool GrTriangulator::Edge::intersect(const Edge& other, SkPoint* p, uint8_t* alpha) const {
155 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
156 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
157 if (fTop == other.fTop || fBottom == other.fBottom) {
158 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000159 }
Chris Dalton5045de32021-01-07 19:09:01 -0700160 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
161 if (denom == 0.0) {
162 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000163 }
Chris Dalton5045de32021-01-07 19:09:01 -0700164 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
165 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
166 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
167 double tNumer = dy * fLine.fB + dx * fLine.fA;
168 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
169 // This saves us doing the divide below unless absolutely necessary.
170 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
171 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
172 return false;
Chris Daltond60c9192021-01-07 18:30:08 +0000173 }
Chris Dalton5045de32021-01-07 19:09:01 -0700174 double s = sNumer / denom;
175 SkASSERT(s >= 0.0 && s <= 1.0);
176 p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
177 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
178 if (alpha) {
179 if (fType == EdgeType::kConnector) {
180 *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
181 } else if (other.fType == EdgeType::kConnector) {
182 double t = tNumer / denom;
183 *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
184 } else if (fType == EdgeType::kOuter && other.fType == EdgeType::kOuter) {
185 *alpha = 0;
186 } else {
187 *alpha = 255;
188 }
Chris Daltond60c9192021-01-07 18:30:08 +0000189 }
Chris Dalton5045de32021-01-07 19:09:01 -0700190 return true;
191}
senorblancof57372d2016-08-31 10:36:19 -0700192
Stephen Whitec4dbc372019-05-22 10:50:14 -0400193struct SSVertex {
194 SSVertex(Vertex* v) : fVertex(v), fPrev(nullptr), fNext(nullptr) {}
195 Vertex* fVertex;
196 SSEdge* fPrev;
197 SSEdge* fNext;
198};
199
Chris Dalton7cf3add2021-01-11 18:33:28 -0700200struct GrAATriangulator::SSEdge {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400201 SSEdge(Edge* edge, SSVertex* prev, SSVertex* next)
202 : fEdge(edge), fEvent(nullptr), fPrev(prev), fNext(next) {
203 }
204 Edge* fEdge;
205 Event* fEvent;
206 SSVertex* fPrev;
207 SSVertex* fNext;
208};
209
210typedef std::unordered_map<Vertex*, SSVertex*> SSVertexMap;
211typedef std::vector<SSEdge*> SSEdgeList;
212
Chris Dalton5045de32021-01-07 19:09:01 -0700213void GrTriangulator::EdgeList::insert(Edge* edge, Edge* prev, Edge* next) {
214 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
215}
216void GrTriangulator::EdgeList::remove(Edge* edge) {
217 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
218}
ethannicholase9709e82016-01-07 13:34:16 -0800219
Stephen Whitec4dbc372019-05-22 10:50:14 -0400220typedef std::priority_queue<Event*, std::vector<Event*>, EventComparator> EventPQ;
Stephen Whitee260c462017-12-19 18:09:54 -0500221
Chris Dalton7cf3add2021-01-11 18:33:28 -0700222struct GrAATriangulator::EventList : EventPQ {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400223 EventList(EventComparator comparison) : EventPQ(comparison) {
224 }
225};
226
Chris Dalton7cf3add2021-01-11 18:33:28 -0700227void GrAATriangulator::makeEvent(SSEdge* e, EventList* events) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400228 Vertex* prev = e->fPrev->fVertex;
229 Vertex* next = e->fNext->fVertex;
230 if (prev == next || !prev->fPartner || !next->fPartner) {
231 return;
232 }
Chris Dalton17ce8c52021-01-07 18:08:46 -0700233 Edge bisector1(prev, prev->fPartner, 1, EdgeType::kConnector);
234 Edge bisector2(next, next->fPartner, 1, EdgeType::kConnector);
Stephen Whitee260c462017-12-19 18:09:54 -0500235 SkPoint p;
236 uint8_t alpha;
237 if (bisector1.intersect(bisector2, &p, &alpha)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400238 TESS_LOG("found edge event for %g, %g (original %g -> %g), "
239 "will collapse to %g,%g alpha %d\n",
240 prev->fID, next->fID, e->fEdge->fTop->fID, e->fEdge->fBottom->fID, p.fX, p.fY,
241 alpha);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700242 e->fEvent = fAlloc.make<Event>(e, p, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400243 events->push(e->fEvent);
244 }
245}
246
Chris Dalton7cf3add2021-01-11 18:33:28 -0700247void GrAATriangulator::makeEvent(SSEdge* edge, Vertex* v, SSEdge* other, Vertex* dest,
248 EventList* events, const Comparator& c) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400249 if (!v->fPartner) {
250 return;
251 }
Stephen White8a3c0592019-05-29 11:26:16 -0400252 Vertex* top = edge->fEdge->fTop;
253 Vertex* bottom = edge->fEdge->fBottom;
254 if (!top || !bottom ) {
255 return;
256 }
Stephen Whitec4dbc372019-05-22 10:50:14 -0400257 Line line = edge->fEdge->fLine;
258 line.fC = -(dest->fPoint.fX * line.fA + dest->fPoint.fY * line.fB);
Chris Dalton17ce8c52021-01-07 18:08:46 -0700259 Edge bisector(v, v->fPartner, 1, EdgeType::kConnector);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400260 SkPoint p;
261 uint8_t alpha = dest->fAlpha;
Stephen White8a3c0592019-05-29 11:26:16 -0400262 if (line.intersect(bisector.fLine, &p) && !c.sweep_lt(p, top->fPoint) &&
263 c.sweep_lt(p, bottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400264 TESS_LOG("found p edge event for %g, %g (original %g -> %g), "
265 "will collapse to %g,%g alpha %d\n",
266 dest->fID, v->fID, top->fID, bottom->fID, p.fX, p.fY, alpha);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700267 edge->fEvent = fAlloc.make<Event>(edge, p, alpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400268 events->push(edge->fEvent);
Stephen Whitee260c462017-12-19 18:09:54 -0500269 }
270}
Stephen Whitee260c462017-12-19 18:09:54 -0500271
Chris Dalton5045de32021-01-07 19:09:01 -0700272void GrTriangulator::MonotonePoly::addEdge(Edge* edge) {
273 if (fSide == kRight_Side) {
274 SkASSERT(!edge->fUsedInRightPoly);
275 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
276 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
277 edge->fUsedInRightPoly = true;
278 } else {
279 SkASSERT(!edge->fUsedInLeftPoly);
280 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
281 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
282 edge->fUsedInLeftPoly = true;
Chris Dalton17ce8c52021-01-07 18:08:46 -0700283 }
Chris Dalton5045de32021-01-07 19:09:01 -0700284}
Chris Dalton17ce8c52021-01-07 18:08:46 -0700285
Chris Dalton9a4904f2021-01-07 19:10:14 -0700286void* GrTriangulator::emitMonotonePoly(const MonotonePoly* monotonePoly, void* data) {
287 Edge* e = monotonePoly->fFirstEdge;
Chris Dalton5045de32021-01-07 19:09:01 -0700288 VertexList vertices;
289 vertices.append(e->fTop);
290 int count = 1;
291 while (e != nullptr) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700292 if (kRight_Side == monotonePoly->fSide) {
Chris Dalton5045de32021-01-07 19:09:01 -0700293 vertices.append(e->fBottom);
294 e = e->fRightPolyNext;
295 } else {
296 vertices.prepend(e->fBottom);
297 e = e->fLeftPolyNext;
Chris Dalton17ce8c52021-01-07 18:08:46 -0700298 }
Chris Dalton5045de32021-01-07 19:09:01 -0700299 count++;
300 }
301 Vertex* first = vertices.fHead;
302 Vertex* v = first->fNext;
303 while (v != vertices.fTail) {
304 SkASSERT(v && v->fPrev && v->fNext);
305 Vertex* prev = v->fPrev;
306 Vertex* curr = v;
307 Vertex* next = v->fNext;
308 if (count == 3) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700309 return this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700310 }
311 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
312 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
313 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
314 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
315 if (ax * by - ay * bx >= 0.0) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700316 data = this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700317 v->fPrev->fNext = v->fNext;
318 v->fNext->fPrev = v->fPrev;
319 count--;
320 if (v->fPrev == first) {
Chris Dalton17ce8c52021-01-07 18:08:46 -0700321 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000322 } else {
Chris Dalton5045de32021-01-07 19:09:01 -0700323 v = v->fPrev;
Chris Daltond60c9192021-01-07 18:30:08 +0000324 }
Chris Dalton5045de32021-01-07 19:09:01 -0700325 } else {
326 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000327 }
Chris Dalton75887a22021-01-06 00:23:13 -0700328 }
Chris Dalton5045de32021-01-07 19:09:01 -0700329 return data;
330}
331
Chris Dalton9a4904f2021-01-07 19:10:14 -0700332void* GrTriangulator::emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding,
333 void* data) const {
334 if (winding < 0) {
Chris Dalton5045de32021-01-07 19:09:01 -0700335 // Ensure our triangles always wind in the same direction as if the path had been
336 // triangulated as a simple fan (a la red book).
337 std::swap(prev, next);
338 }
Chris Dalton9a4904f2021-01-07 19:10:14 -0700339 return emit_triangle(next, curr, prev, fEmitCoverage, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700340}
341
342Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
343 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
344 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
345 Poly* partner = fPartner;
346 Poly* poly = this;
347 if (side == kRight_Side) {
348 if (e->fUsedInRightPoly) {
349 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000350 }
Chris Dalton5045de32021-01-07 19:09:01 -0700351 } else {
352 if (e->fUsedInLeftPoly) {
353 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000354 }
Chris Dalton5045de32021-01-07 19:09:01 -0700355 }
356 if (partner) {
357 fPartner = partner->fPartner = nullptr;
358 }
359 if (!fTail) {
360 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
361 fCount += 2;
362 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
363 return poly;
364 } else if (side == fTail->fSide) {
365 fTail->addEdge(e);
366 fCount++;
367 } else {
368 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner);
369 fTail->addEdge(e);
370 fCount++;
371 if (partner) {
372 partner->addEdge(e, side, alloc);
373 poly = partner;
374 } else {
375 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
376 m->fPrev = fTail;
377 fTail->fNext = m;
378 fTail = m;
379 }
380 }
381 return poly;
382}
Chris Dalton9a4904f2021-01-07 19:10:14 -0700383void* GrTriangulator::emitPoly(const Poly* poly, void *data) {
384 if (poly->fCount < 3) {
ethannicholase9709e82016-01-07 13:34:16 -0800385 return data;
386 }
Chris Dalton5045de32021-01-07 19:09:01 -0700387 TESS_LOG("emit() %d, size %d\n", fID, fCount);
Chris Dalton9a4904f2021-01-07 19:10:14 -0700388 for (MonotonePoly* m = poly->fHead; m != nullptr; m = m->fNext) {
389 data = this->emitMonotonePoly(m, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700390 }
391 return data;
392}
ethannicholase9709e82016-01-07 13:34:16 -0800393
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700394static bool coincident(const SkPoint& a, const SkPoint& b) {
ethannicholase9709e82016-01-07 13:34:16 -0800395 return a == b;
396}
397
Chris Dalton7cf3add2021-01-11 18:33:28 -0700398Poly* GrTriangulator::makePoly(Poly** head, Vertex* v, int winding) {
399 Poly* poly = fAlloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800400 poly->fNext = *head;
401 *head = poly;
402 return poly;
403}
404
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700405void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) {
406 Vertex* v = fAlloc.make<Vertex>(p, 255);
Chris Dalton5045de32021-01-07 19:09:01 -0700407#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -0800408 static float gID = 0.0f;
409 v->fID = gID++;
410#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500411 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800412}
413
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700414static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
Stephen White36e4f062017-03-27 16:11:31 -0400415 SkQuadCoeff quad(pts);
416 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
417 SkPoint mid = to_point(quad.eval(t));
418 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400419 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
420 return 0;
421 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500422 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400423}
424
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700425void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
426 VertexList* contour) {
Stephen White36e4f062017-03-27 16:11:31 -0400427 SkQuadCoeff quad(pts);
428 Sk2s aa = quad.fA * quad.fA;
429 SkScalar denom = 2.0f * (aa[0] + aa[1]);
430 Sk2s ab = quad.fA * quad.fB;
431 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
432 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500433 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400434 // Test possible subdivision values only at the point of maximum curvature.
435 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500436 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400437 u = 1.0f / nPoints;
438 if (quad_error_at(pts, t, u) < toleranceSqd) {
439 break;
440 }
441 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800442 }
Stephen White36e4f062017-03-27 16:11:31 -0400443 for (int j = 1; j <= nPoints; j++) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700444 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
Stephen White36e4f062017-03-27 16:11:31 -0400445 }
ethannicholase9709e82016-01-07 13:34:16 -0800446}
447
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700448void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
449 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
450 int pointsLeft) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500451 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
452 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800453 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
454 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700455 this->appendPointToContour(p3, contour);
Stephen White3a9aab92017-03-07 14:07:18 -0500456 return;
ethannicholase9709e82016-01-07 13:34:16 -0800457 }
458 const SkPoint q[] = {
459 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
460 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
461 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
462 };
463 const SkPoint r[] = {
464 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
465 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
466 };
467 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
468 pointsLeft >>= 1;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700469 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
470 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800471}
472
473// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
474
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700475void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
476 VertexList* contours) {
ethannicholase9709e82016-01-07 13:34:16 -0800477 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton7156db22020-05-07 22:06:28 +0000478 SkPoint pts[4];
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700479 fIsLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500480 VertexList* contour = contours;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700481 SkPath::Iter iter(fPath, false);
482 if (fPath.isInverseFillType()) {
ethannicholase9709e82016-01-07 13:34:16 -0800483 SkPoint quad[4];
484 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700485 for (int i = 3; i >= 0; i--) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700486 this->appendPointToContour(quad[i], contours);
ethannicholase9709e82016-01-07 13:34:16 -0800487 }
Stephen White3a9aab92017-03-07 14:07:18 -0500488 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800489 }
490 SkAutoConicToQuads converter;
Chris Dalton7156db22020-05-07 22:06:28 +0000491 SkPath::Verb verb;
492 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800493 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +0000494 case SkPath::kConic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700495 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700496 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700497 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700498 break;
499 }
Chris Dalton7156db22020-05-07 22:06:28 +0000500 SkScalar weight = iter.conicWeight();
501 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800502 for (int i = 0; i < converter.countQuads(); ++i) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700503 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800504 quadPts += 2;
505 }
ethannicholase9709e82016-01-07 13:34:16 -0800506 break;
507 }
Chris Dalton7156db22020-05-07 22:06:28 +0000508 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500509 if (contour->fHead) {
510 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800511 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700512 this->appendPointToContour(pts[0], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800513 break;
Chris Dalton7156db22020-05-07 22:06:28 +0000514 case SkPath::kLine_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700515 this->appendPointToContour(pts[1], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800516 break;
517 }
Chris Dalton7156db22020-05-07 22:06:28 +0000518 case SkPath::kQuad_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700519 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700520 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700521 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700522 break;
523 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700524 this->appendQuadraticToContour(pts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800525 break;
526 }
Chris Dalton7156db22020-05-07 22:06:28 +0000527 case SkPath::kCubic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700528 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700529 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700530 this->appendPointToContour(pts[3], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700531 break;
532 }
ethannicholase9709e82016-01-07 13:34:16 -0800533 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700534 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
535 pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800536 break;
537 }
Chris Dalton7156db22020-05-07 22:06:28 +0000538 case SkPath::kClose_Verb:
539 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800540 break;
541 }
542 }
543}
544
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700545static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800546 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500547 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800548 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500549 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800550 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500551 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700552 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500553 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800554 return (winding & 1) == 1;
555 default:
556 SkASSERT(false);
557 return false;
558 }
559}
560
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700561static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500562 return poly && apply_fill_type(fillType, poly->fWinding);
563}
564
Chris Dalton7cf3add2021-01-11 18:33:28 -0700565Edge* GrTriangulator::makeEdge(Vertex* prev, Vertex* next, EdgeType type, const Comparator& c) {
Stephen White2f4686f2017-01-03 16:20:01 -0500566 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800567 Vertex* top = winding < 0 ? next : prev;
568 Vertex* bottom = winding < 0 ? prev : next;
Chris Dalton7cf3add2021-01-11 18:33:28 -0700569 return fAlloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800570}
571
Chris Daltond60c9192021-01-07 18:30:08 +0000572static void remove_edge(Edge* edge, EdgeList* edges) {
573 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
574 SkASSERT(edges->contains(edge));
575 edges->remove(edge);
576}
577
578static void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400579 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +0000580 SkASSERT(!edges->contains(edge));
581 Edge* next = prev ? prev->fRight : edges->fHead;
582 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800583}
584
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700585static void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500586 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800587 *left = v->fFirstEdgeAbove->fLeft;
588 *right = v->fLastEdgeAbove->fRight;
589 return;
590 }
591 Edge* next = nullptr;
592 Edge* prev;
593 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
594 if (prev->isLeftOf(v)) {
595 break;
596 }
597 next = prev;
598 }
599 *left = prev;
600 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800601}
602
Chris Dalton811dc6a2021-01-07 16:40:32 -0700603static void insert_edge_above(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800604 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500605 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800606 return;
607 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400608 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
609 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800610 Edge* prev = nullptr;
611 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000612 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800613 if (next->isRightOf(edge->fTop)) {
614 break;
615 }
616 prev = next;
617 }
senorblancoe6eaa322016-03-08 09:06:44 -0800618 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
Chris Daltond60c9192021-01-07 18:30:08 +0000619 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
ethannicholase9709e82016-01-07 13:34:16 -0800620}
621
Chris Dalton811dc6a2021-01-07 16:40:32 -0700622static void insert_edge_below(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800623 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500624 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800625 return;
626 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400627 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
628 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800629 Edge* prev = nullptr;
630 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000631 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -0800632 if (next->isRightOf(edge->fBottom)) {
633 break;
634 }
635 prev = next;
636 }
senorblancoe6eaa322016-03-08 09:06:44 -0800637 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
Chris Daltond60c9192021-01-07 18:30:08 +0000638 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
ethannicholase9709e82016-01-07 13:34:16 -0800639}
640
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700641static void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400642 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400643 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
644 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800645 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800646 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
647}
648
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700649static void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400650 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400651 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
652 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800653 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800654 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
655}
656
Chris Daltond60c9192021-01-07 18:30:08 +0000657static void disconnect(Edge* edge)
658{
659 remove_edge_above(edge);
660 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500661}
662
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700663static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700664 const Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400665
Chris Dalton811dc6a2021-01-07 16:40:32 -0700666static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400667 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
668 return;
669 }
670 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400671 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400672 while (v != dst) {
673 v = v->fPrev;
674 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +0000675 remove_edge(e, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400676 }
677 Edge* leftEdge = v->fLeftEnclosingEdge;
678 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +0000679 insert_edge(e, leftEdge, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400680 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -0500681 Vertex* top = e->fTop;
682 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
683 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
684 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
685 dst = top;
686 }
Stephen White3b5a3fa2017-06-06 14:51:19 -0400687 }
688 }
689 *current = v;
690}
691
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700692static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700693 const Comparator& c) {
Stephen Whitec03e6982020-02-06 16:32:14 -0500694 if (!activeEdges || !current) {
695 return;
696 }
697 Vertex* top = edge->fTop;
698 Vertex* bottom = edge->fBottom;
699 if (edge->fLeft) {
700 Vertex* leftTop = edge->fLeft->fTop;
701 Vertex* leftBottom = edge->fLeft->fBottom;
702 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
703 rewind(activeEdges, current, leftTop, c);
704 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
705 rewind(activeEdges, current, top, c);
706 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
707 !edge->fLeft->isLeftOf(bottom)) {
708 rewind(activeEdges, current, leftTop, c);
709 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
710 rewind(activeEdges, current, top, c);
711 }
712 }
713 if (edge->fRight) {
714 Vertex* rightTop = edge->fRight->fTop;
715 Vertex* rightBottom = edge->fRight->fBottom;
716 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
717 rewind(activeEdges, current, rightTop, c);
718 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
719 rewind(activeEdges, current, top, c);
720 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
721 !edge->fRight->isRightOf(bottom)) {
722 rewind(activeEdges, current, rightTop, c);
723 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
724 !edge->isLeftOf(rightBottom)) {
725 rewind(activeEdges, current, top, c);
726 }
727 }
728}
729
Chris Dalton811dc6a2021-01-07 16:40:32 -0700730static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
731 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800732 remove_edge_below(edge);
733 edge->fTop = v;
734 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000735 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500736 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400737 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800738}
739
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700740static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700741 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800742 remove_edge_above(edge);
743 edge->fBottom = v;
744 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000745 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500746 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400747 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800748}
749
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700750static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700751 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800752 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400753 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
754 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
755 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400756 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800757 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +0000758 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -0400759 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -0800760 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400761 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800762 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400763 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800764 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400765 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800766 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400767 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800768 }
769}
770
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700771static void merge_edges_below(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->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400774 TESS_LOG("merging coincident below 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->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400782 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800783 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400784 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800785 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400786 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800787 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400788 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800789 }
790}
791
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700792static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400793 if (!left || !right) {
794 return false;
795 }
796 return left->fTop->fPoint == right->fTop->fPoint ||
797 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
798}
799
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700800static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400801 if (!left || !right) {
802 return false;
803 }
804 return left->fBottom->fPoint == right->fBottom->fPoint ||
805 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
806}
807
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700808static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700809 const Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -0400810 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400811 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400812 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400813 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -0400814 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400815 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400816 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400817 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -0400818 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -0400819 } else {
820 break;
821 }
ethannicholase9709e82016-01-07 13:34:16 -0800822 }
Stephen Whited26b4d82018-07-26 10:02:27 -0400823 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
824 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
825 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
826 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -0800827}
828
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700829bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700830 const Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -0400831 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -0400832 return false;
Stephen White0cb31672017-06-08 14:41:01 -0400833 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400834 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
835 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400836 Vertex* top;
837 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -0400838 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800839 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400840 top = v;
841 bottom = edge->fTop;
842 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -0500843 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400844 top = edge->fBottom;
845 bottom = v;
846 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800847 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400848 top = v;
849 bottom = edge->fBottom;
850 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800851 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700852 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Chris Daltond60c9192021-01-07 18:30:08 +0000853 insert_edge_below(newEdge, top, c);
854 insert_edge_above(newEdge, bottom, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400855 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400856 return true;
857}
858
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700859bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700860 Vertex** current, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -0400861 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
862 return false;
863 }
Stephen White1c5fd182018-07-12 15:54:05 -0400864 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
865 return false;
866 }
Stephen White89042d52018-06-08 12:18:22 -0400867 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
868 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400869 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700870 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400871 }
872 } else {
873 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400874 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700875 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400876 }
877 }
878 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
879 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400880 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700881 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400882 }
883 } else {
884 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400885 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700886 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400887 }
888 }
889 return false;
ethannicholase9709e82016-01-07 13:34:16 -0800890}
891
Chris Dalton7cf3add2021-01-11 18:33:28 -0700892Edge* GrTriangulator::makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType type,
893 const Comparator& c, int windingScale) {
Stephen Whitee260c462017-12-19 18:09:54 -0500894 if (!prev || !next || prev->fPoint == next->fPoint) {
895 return nullptr;
896 }
Chris Dalton7cf3add2021-01-11 18:33:28 -0700897 Edge* edge = this->makeEdge(prev, next, type, c);
Chris Daltond60c9192021-01-07 18:30:08 +0000898 insert_edge_below(edge, edge->fTop, c);
899 insert_edge_above(edge, edge->fBottom, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700900 edge->fWinding *= windingScale;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400901 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -0700902 return edge;
903}
904
Chris Dalton811dc6a2021-01-07 16:40:32 -0700905static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400906 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
907 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -0500908 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -0400909 if (src->fPartner) {
910 src->fPartner->fPartner = dst;
911 }
Stephen White7b376942018-05-22 11:51:32 -0400912 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400913 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800914 }
Stephen White7b376942018-05-22 11:51:32 -0400915 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400916 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800917 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500918 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400919 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -0800920}
921
Chris Dalton7cf3add2021-01-11 18:33:28 -0700922Vertex* GrTriangulator::makeSortedVertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
923 Vertex* reference, const Comparator& c) {
Stephen White95152e12017-12-18 10:52:44 -0500924 Vertex* prevV = reference;
925 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
926 prevV = prevV->fPrev;
927 }
928 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
929 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
930 prevV = nextV;
931 nextV = nextV->fNext;
932 }
933 Vertex* v;
934 if (prevV && coincident(prevV->fPoint, p)) {
935 v = prevV;
936 } else if (nextV && coincident(nextV->fPoint, p)) {
937 v = nextV;
938 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -0700939 v = fAlloc.make<Vertex>(p, alpha);
Chris Dalton5045de32021-01-07 19:09:01 -0700940#if TRIANGULATOR_LOGGING
Stephen White95152e12017-12-18 10:52:44 -0500941 if (!prevV) {
942 v->fID = mesh->fHead->fID - 1.0f;
943 } else if (!nextV) {
944 v->fID = mesh->fTail->fID + 1.0f;
945 } else {
946 v->fID = (prevV->fID + nextV->fID) * 0.5f;
947 }
948#endif
949 mesh->insert(v, prevV, nextV);
950 }
951 return v;
952}
953
Stephen White53a02982018-05-30 22:47:46 -0400954// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
955// sort criterion, it may not be possible to split correctly, since there is no point which is
956// below the top and above the bottom. This function detects that case.
Chris Dalton811dc6a2021-01-07 16:40:32 -0700957static bool nearly_flat(const Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -0400958 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
959 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -0400960 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -0400961}
962
Chris Dalton811dc6a2021-01-07 16:40:32 -0700963static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -0400964 if (c.sweep_lt(p, min)) {
965 return min;
966 } else if (c.sweep_lt(max, p)) {
967 return max;
968 } else {
969 return p;
970 }
971}
972
Chris Dalton7cf3add2021-01-11 18:33:28 -0700973void GrTriangulator::computeBisector(Edge* edge1, Edge* edge2, Vertex* v) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400974 Line line1 = edge1->fLine;
975 Line line2 = edge2->fLine;
976 line1.normalize();
977 line2.normalize();
978 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
979 if (cosAngle > 0.999) {
980 return;
981 }
982 line1.fC += edge1->fWinding > 0 ? -1 : 1;
983 line2.fC += edge2->fWinding > 0 ? -1 : 1;
984 SkPoint p;
985 if (line1.intersect(line2, &p)) {
Chris Dalton17ce8c52021-01-07 18:08:46 -0700986 uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0;
Chris Dalton7cf3add2021-01-11 18:33:28 -0700987 v->fPartner = fAlloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -0400988 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 -0400989 }
990}
991
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700992bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700993 Vertex** current, VertexList* mesh, const Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -0400994 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400995 return false;
ethannicholase9709e82016-01-07 13:34:16 -0800996 }
Stephen White56158ae2017-01-30 14:31:31 -0500997 SkPoint p;
998 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -0400999 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001000 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001001 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001002 Vertex* top = *current;
1003 // If the intersection point is above the current vertex, rewind to the vertex above the
1004 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001005 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001006 top = top->fPrev;
1007 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001008 if (!nearly_flat(c, left)) {
1009 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001010 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001011 if (!nearly_flat(c, right)) {
1012 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001013 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001014 if (p == left->fTop->fPoint) {
1015 v = left->fTop;
1016 } else if (p == left->fBottom->fPoint) {
1017 v = left->fBottom;
1018 } else if (p == right->fTop->fPoint) {
1019 v = right->fTop;
1020 } else if (p == right->fBottom->fPoint) {
1021 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001022 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001023 v = this->makeSortedVertex(p, alpha, mesh, top, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001024 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001025 v->fSynthetic = true;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001026 this->computeBisector(left, right, v);
Stephen Whitee260c462017-12-19 18:09:54 -05001027 }
ethannicholase9709e82016-01-07 13:34:16 -08001028 }
Stephen White0cb31672017-06-08 14:41:01 -04001029 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001030 this->splitEdge(left, v, activeEdges, current, c);
1031 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001032 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001033 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001034 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001035 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001036}
1037
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001038void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
Stephen White3a9aab92017-03-07 14:07:18 -05001039 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1040 SkASSERT(contour->fHead);
1041 Vertex* prev = contour->fTail;
Chris Dalton854ee852021-01-05 15:12:59 -07001042 if (fRoundVerticesToQuarterPixel) {
Stephen White3a9aab92017-03-07 14:07:18 -05001043 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001044 }
Stephen White3a9aab92017-03-07 14:07:18 -05001045 for (Vertex* v = contour->fHead; v;) {
Chris Dalton854ee852021-01-05 15:12:59 -07001046 if (fRoundVerticesToQuarterPixel) {
senorblancof57372d2016-08-31 10:36:19 -07001047 round(&v->fPoint);
1048 }
Stephen White3a9aab92017-03-07 14:07:18 -05001049 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001050 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001051 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001052 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001053 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001054 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001055 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001056 contour->remove(v);
Chris Dalton854ee852021-01-05 15:12:59 -07001057 } else if (fCullCollinearVertices &&
Chris Dalton6ccc0322020-01-29 11:38:16 -07001058 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001059 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001060 contour->remove(v);
1061 } else {
1062 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001063 }
Stephen White3a9aab92017-03-07 14:07:18 -05001064 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001065 }
1066 }
1067}
1068
Chris Dalton811dc6a2021-01-07 16:40:32 -07001069bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001070 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001071 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001072 }
Stephen Whitee260c462017-12-19 18:09:54 -05001073 bool merged = false;
1074 for (Vertex* v = mesh->fHead->fNext; v;) {
1075 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001076 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1077 v->fPoint = v->fPrev->fPoint;
1078 }
1079 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001080 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001081 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001082 }
Stephen Whitee260c462017-12-19 18:09:54 -05001083 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001084 }
Stephen Whitee260c462017-12-19 18:09:54 -05001085 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001086}
1087
1088// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1089
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001090void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001091 const Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001092 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1093 Vertex* prev = contour->fTail;
1094 for (Vertex* v = contour->fHead; v;) {
1095 Vertex* next = v->fNext;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001096 this->makeConnectingEdge(prev, v, EdgeType::kInner, c);
Stephen White3a9aab92017-03-07 14:07:18 -05001097 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001098 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001099 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001100 }
1101 }
ethannicholase9709e82016-01-07 13:34:16 -08001102}
1103
Chris Dalton7cf3add2021-01-11 18:33:28 -07001104void GrAATriangulator::connectPartners(VertexList* mesh, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001105 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001106 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001107 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1108 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1109 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1110 // number.
Chris Dalton7cf3add2021-01-11 18:33:28 -07001111 this->makeConnectingEdge(outer, inner, EdgeType::kConnector, c, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001112 inner->fPartner = outer->fPartner = nullptr;
1113 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001114 }
1115 }
1116}
1117
1118template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001119static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001120 Vertex* a = front->fHead;
1121 Vertex* b = back->fHead;
1122 while (a && b) {
1123 if (sweep_lt(a->fPoint, b->fPoint)) {
1124 front->remove(a);
1125 result->append(a);
1126 a = front->fHead;
1127 } else {
1128 back->remove(b);
1129 result->append(b);
1130 b = back->fHead;
1131 }
1132 }
1133 result->append(*front);
1134 result->append(*back);
1135}
1136
Chris Dalton811dc6a2021-01-07 16:40:32 -07001137static void sorted_merge(VertexList* front, VertexList* back, VertexList* result,
1138 const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001139 if (c.fDirection == Comparator::Direction::kHorizontal) {
1140 sorted_merge<sweep_lt_horiz>(front, back, result);
1141 } else {
1142 sorted_merge<sweep_lt_vert>(front, back, result);
1143 }
Chris Dalton5045de32021-01-07 19:09:01 -07001144#if TRIANGULATOR_LOGGING
Stephen White3b5a3fa2017-06-06 14:51:19 -04001145 float id = 0.0f;
1146 for (Vertex* v = result->fHead; v; v = v->fNext) {
1147 v->fID = id++;
1148 }
1149#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001150}
1151
ethannicholase9709e82016-01-07 13:34:16 -08001152// Stage 3: sort the vertices by increasing sweep direction.
1153
Stephen White16a40cb2017-02-23 11:10:01 -05001154template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001155static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001156 Vertex* slow = vertices->fHead;
1157 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001158 return;
1159 }
Stephen White16a40cb2017-02-23 11:10:01 -05001160 Vertex* fast = slow->fNext;
1161 if (!fast) {
1162 return;
1163 }
1164 do {
1165 fast = fast->fNext;
1166 if (fast) {
1167 fast = fast->fNext;
1168 slow = slow->fNext;
1169 }
1170 } while (fast);
1171 VertexList front(vertices->fHead, slow);
1172 VertexList back(slow->fNext, vertices->fTail);
1173 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001174
Stephen White16a40cb2017-02-23 11:10:01 -05001175 merge_sort<sweep_lt>(&front);
1176 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001177
Stephen White16a40cb2017-02-23 11:10:01 -05001178 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001179 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001180}
1181
Chris Dalton5045de32021-01-07 19:09:01 -07001182#if TRIANGULATOR_LOGGING
Chris Dalton7cf3add2021-01-11 18:33:28 -07001183static void dump_mesh(const VertexList& mesh) {
Chris Daltond60c9192021-01-07 18:30:08 +00001184 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001185 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 -05001186 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001187 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1188 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001189 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001190 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001191 }
1192 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001193 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001194 }
1195 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001196 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001197 }
1198 }
Chris Daltond60c9192021-01-07 18:30:08 +00001199}
Chris Dalton7cf3add2021-01-11 18:33:28 -07001200#endif
Stephen White95152e12017-12-18 10:52:44 -05001201
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001202static void dump_skel(const SSEdgeList& ssEdges) {
Chris Dalton5045de32021-01-07 19:09:01 -07001203#if TRIANGULATOR_LOGGING
Stephen Whitec4dbc372019-05-22 10:50:14 -04001204 for (SSEdge* edge : ssEdges) {
1205 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001206 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001207 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001208 edge->fNext->fVertex->fID);
1209 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001210 TESS_LOG(" (original %g -> %g)\n",
1211 edge->fEdge->fTop->fID,
1212 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001213 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001214 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001215 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001216 }
1217 }
1218#endif
1219}
1220
Stephen White89042d52018-06-08 12:18:22 -04001221#ifdef SK_DEBUG
Chris Dalton811dc6a2021-01-07 16:40:32 -07001222static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001223 if (!left || !right) {
1224 return;
1225 }
1226 if (left->fTop == right->fTop) {
1227 SkASSERT(left->isLeftOf(right->fBottom));
1228 SkASSERT(right->isRightOf(left->fBottom));
1229 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1230 SkASSERT(left->isLeftOf(right->fTop));
1231 } else {
1232 SkASSERT(right->isRightOf(left->fTop));
1233 }
1234 if (left->fBottom == right->fBottom) {
1235 SkASSERT(left->isLeftOf(right->fTop));
1236 SkASSERT(right->isRightOf(left->fTop));
1237 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1238 SkASSERT(left->isLeftOf(right->fBottom));
1239 } else {
1240 SkASSERT(right->isRightOf(left->fBottom));
1241 }
1242}
1243
Chris Dalton811dc6a2021-01-07 16:40:32 -07001244static void validate_edge_list(EdgeList* edges, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001245 Edge* left = edges->fHead;
1246 if (!left) {
1247 return;
1248 }
1249 for (Edge* right = left->fRight; right; right = right->fRight) {
1250 validate_edge_pair(left, right, c);
1251 left = right;
1252 }
1253}
1254#endif
1255
ethannicholase9709e82016-01-07 13:34:16 -08001256// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1257
Chris Daltond60c9192021-01-07 18:30:08 +00001258static bool connected(Vertex* v) {
1259 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1260}
1261
Chris Dalton811dc6a2021-01-07 16:40:32 -07001262GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001263 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001264 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001265 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001266 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001267 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001268 continue;
1269 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001270 Edge* leftEnclosingEdge;
1271 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001272 bool restartChecks;
1273 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001274 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1275 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001276 restartChecks = false;
1277 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001278 v->fLeftEnclosingEdge = leftEnclosingEdge;
1279 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001280 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001281 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001282 if (this->checkForIntersection(
1283 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1284 this->checkForIntersection(
1285 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001286 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001287 return SimplifyResult::kAbort;
1288 }
1289 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001290 restartChecks = true;
1291 break;
1292 }
1293 }
1294 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001295 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1296 &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001297 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001298 return SimplifyResult::kAbort;
1299 }
1300 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001301 restartChecks = true;
1302 }
1303
1304 }
1305 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001306#ifdef SK_DEBUG
1307 validate_edge_list(&activeEdges, c);
1308#endif
ethannicholase9709e82016-01-07 13:34:16 -08001309 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +00001310 remove_edge(e, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001311 }
1312 Edge* leftEdge = leftEnclosingEdge;
1313 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001314 insert_edge(e, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001315 leftEdge = e;
1316 }
ethannicholase9709e82016-01-07 13:34:16 -08001317 }
Stephen Whitee260c462017-12-19 18:09:54 -05001318 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001319 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001320}
1321
1322// Stage 5: Tessellate the simplified mesh into monotone polygons.
1323
Chris Dalton7cf3add2021-01-11 18:33:28 -07001324Poly* GrTriangulator::tessellate(const VertexList& vertices, VertexList*, const Comparator&) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001325 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001326 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton854ee852021-01-05 15:12:59 -07001327 if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001328 maxWindMagnitude = 1;
1329 }
ethannicholase9709e82016-01-07 13:34:16 -08001330 EdgeList activeEdges;
1331 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001332 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001333 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001334 continue;
1335 }
Chris Dalton5045de32021-01-07 19:09:01 -07001336#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001337 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 -08001338#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001339 Edge* leftEnclosingEdge;
1340 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001341 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001342 Poly* leftPoly;
1343 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001344 if (v->fFirstEdgeAbove) {
1345 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1346 rightPoly = v->fLastEdgeAbove->fRightPoly;
1347 } else {
1348 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1349 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1350 }
Chris Dalton5045de32021-01-07 19:09:01 -07001351#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001352 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001353 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001354 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1355 e->fTop->fID, e->fBottom->fID,
1356 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1357 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001358 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001359 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001360 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001361 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1362 e->fTop->fID, e->fBottom->fID,
1363 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1364 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001365 }
1366#endif
1367 if (v->fFirstEdgeAbove) {
1368 if (leftPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001369 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001370 }
1371 if (rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001372 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001373 }
1374 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001375 Edge* rightEdge = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001376 remove_edge(e, &activeEdges);
Stephen White8a0bfc52017-02-21 15:24:13 -05001377 if (e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001378 e->fRightPoly->addEdge(e, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001379 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001380 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001381 rightEdge->fLeftPoly->addEdge(e, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001382 }
1383 }
Chris Daltond60c9192021-01-07 18:30:08 +00001384 remove_edge(v->fLastEdgeAbove, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001385 if (!v->fFirstEdgeBelow) {
1386 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1387 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1388 rightPoly->fPartner = leftPoly;
1389 leftPoly->fPartner = rightPoly;
1390 }
1391 }
1392 }
1393 if (v->fFirstEdgeBelow) {
1394 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001395 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001396 if (leftPoly == rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001397 if (leftPoly->fTail && leftPoly->fTail->fSide == kLeft_Side) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001398 leftPoly = this->makePoly(&polys, leftPoly->lastVertex(),
1399 leftPoly->fWinding);
senorblanco531237e2016-06-02 11:36:48 -07001400 leftEnclosingEdge->fRightPoly = leftPoly;
1401 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001402 rightPoly = this->makePoly(&polys, rightPoly->lastVertex(),
1403 rightPoly->fWinding);
senorblanco531237e2016-06-02 11:36:48 -07001404 rightEnclosingEdge->fLeftPoly = rightPoly;
1405 }
ethannicholase9709e82016-01-07 13:34:16 -08001406 }
Chris Daltond60c9192021-01-07 18:30:08 +00001407 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
Chris Dalton17ce8c52021-01-07 18:08:46 -07001408 EdgeType::kInner);
1409 leftPoly = leftPoly->addEdge(join, kRight_Side, fAlloc);
1410 rightPoly = rightPoly->addEdge(join, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001411 }
1412 }
1413 Edge* leftEdge = v->fFirstEdgeBelow;
1414 leftEdge->fLeftPoly = leftPoly;
Chris Daltond60c9192021-01-07 18:30:08 +00001415 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001416 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1417 rightEdge = rightEdge->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001418 insert_edge(rightEdge, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001419 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1420 winding += leftEdge->fWinding;
1421 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001422 if (abs(winding) > maxWindMagnitude) {
1423 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1424 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001425 Poly* poly = this->makePoly(&polys, v, winding);
ethannicholase9709e82016-01-07 13:34:16 -08001426 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1427 }
1428 leftEdge = rightEdge;
1429 }
1430 v->fLastEdgeBelow->fRightPoly = rightPoly;
1431 }
Chris Dalton5045de32021-01-07 19:09:01 -07001432#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001433 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001434 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001435 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1436 e->fTop->fID, e->fBottom->fID,
1437 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1438 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001439 }
1440#endif
1441 }
1442 return polys;
1443}
1444
Chris Dalton7cf3add2021-01-11 18:33:28 -07001445void GrAATriangulator::removeNonBoundaryEdges(const VertexList& mesh) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001446 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001447 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001448 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001449 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001450 continue;
1451 }
1452 Edge* leftEnclosingEdge;
1453 Edge* rightEnclosingEdge;
1454 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1455 bool prevFilled = leftEnclosingEdge &&
Chris Dalton7cf3add2021-01-11 18:33:28 -07001456 apply_fill_type(fPath.getFillType(), leftEnclosingEdge->fWinding);
Stephen White49789062017-02-21 10:35:49 -05001457 for (Edge* e = v->fFirstEdgeAbove; e;) {
1458 Edge* next = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001459 remove_edge(e, &activeEdges);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001460 bool filled = apply_fill_type(fPath.getFillType(), e->fWinding);
Stephen White49789062017-02-21 10:35:49 -05001461 if (filled == prevFilled) {
Chris Daltond60c9192021-01-07 18:30:08 +00001462 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001463 }
Stephen White49789062017-02-21 10:35:49 -05001464 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001465 e = next;
1466 }
Stephen White49789062017-02-21 10:35:49 -05001467 Edge* prev = leftEnclosingEdge;
1468 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1469 if (prev) {
1470 e->fWinding += prev->fWinding;
1471 }
Chris Daltond60c9192021-01-07 18:30:08 +00001472 insert_edge(e, prev, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001473 prev = e;
1474 }
senorblancof57372d2016-08-31 10:36:19 -07001475 }
senorblancof57372d2016-08-31 10:36:19 -07001476}
1477
Stephen White66412122017-03-01 11:48:27 -05001478// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001479static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001480 normal->set(SkDoubleToScalar(e->fLine.fA),
1481 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001482}
1483
1484// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1485// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1486// invert on stroking.
1487
Chris Dalton7cf3add2021-01-11 18:33:28 -07001488void GrAATriangulator::simplifyBoundary(EdgeList* boundary, const Comparator& c) {
senorblancof57372d2016-08-31 10:36:19 -07001489 Edge* prevEdge = boundary->fTail;
1490 SkVector prevNormal;
1491 get_edge_normal(prevEdge, &prevNormal);
1492 for (Edge* e = boundary->fHead; e != nullptr;) {
1493 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1494 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001495 double distPrev = e->dist(prev->fPoint);
1496 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001497 SkVector normal;
1498 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001499 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001500 if (prev == next) {
Chris Daltond60c9192021-01-07 18:30:08 +00001501 remove_edge(prevEdge, boundary);
1502 remove_edge(e, boundary);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001503 prevEdge = boundary->fTail;
1504 e = boundary->fHead;
1505 if (prevEdge) {
1506 get_edge_normal(prevEdge, &prevNormal);
1507 }
1508 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001509 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001510 Edge* join = this->makeEdge(prev, next, EdgeType::kInner, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001511 if (prev->fPoint != next->fPoint) {
1512 join->fLine.normalize();
1513 join->fLine = join->fLine * join->fWinding;
1514 }
Chris Daltond60c9192021-01-07 18:30:08 +00001515 insert_edge(join, e, boundary);
1516 remove_edge(prevEdge, boundary);
1517 remove_edge(e, boundary);
senorblancof57372d2016-08-31 10:36:19 -07001518 if (join->fLeft && join->fRight) {
1519 prevEdge = join->fLeft;
1520 e = join;
1521 } else {
1522 prevEdge = boundary->fTail;
1523 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1524 }
1525 get_edge_normal(prevEdge, &prevNormal);
1526 } else {
1527 prevEdge = e;
1528 prevNormal = normal;
1529 e = e->fRight;
1530 }
1531 }
1532}
1533
Chris Dalton7cf3add2021-01-11 18:33:28 -07001534void GrAATriangulator::connectSSEdge(Vertex* v, Vertex* dest, const Comparator& c) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001535 if (v == dest) {
1536 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001537 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001538 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001539 if (v->fSynthetic) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001540 this->makeConnectingEdge(v, dest, EdgeType::kConnector, c, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001541 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001542 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1543 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001544 v->fPartner->fPartner = dest;
1545 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001546 }
1547}
1548
Chris Dalton7cf3add2021-01-11 18:33:28 -07001549void GrAATriangulator::Event::apply(VertexList* mesh, const Comparator& c, EventList* events,
1550 GrAATriangulator* triangulator) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001551 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001552 return;
1553 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001554 Vertex* prev = fEdge->fPrev->fVertex;
1555 Vertex* next = fEdge->fNext->fVertex;
1556 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1557 SSEdge* nextEdge = fEdge->fNext->fNext;
1558 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1559 return;
Stephen White77169c82018-06-05 09:15:59 -04001560 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001561 Vertex* dest = triangulator->makeSortedVertex(fPoint, fAlpha, mesh, prev, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001562 dest->fSynthetic = true;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001563 SSVertex* ssv = triangulator->fAlloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001564 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1565 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1566 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001567 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001568
Chris Dalton7cf3add2021-01-11 18:33:28 -07001569 triangulator->connectSSEdge(prev, dest, c);
1570 triangulator->connectSSEdge(next, dest, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001571
Stephen Whitec4dbc372019-05-22 10:50:14 -04001572 prevEdge->fNext = nextEdge->fPrev = ssv;
1573 ssv->fPrev = prevEdge;
1574 ssv->fNext = nextEdge;
1575 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1576 return;
1577 }
1578 if (prevEdge->fEvent) {
1579 prevEdge->fEvent->fEdge = nullptr;
1580 }
1581 if (nextEdge->fEvent) {
1582 nextEdge->fEvent->fEdge = nullptr;
1583 }
1584 if (prevEdge->fPrev == nextEdge->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001585 triangulator->connectSSEdge(prevEdge->fPrev->fVertex, dest, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001586 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1587 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001588 triangulator->computeBisector(prevEdge->fEdge, nextEdge->fEdge, dest);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001589 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1590 if (dest->fPartner) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001591 triangulator->makeEvent(prevEdge, events);
1592 triangulator->makeEvent(nextEdge, events);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001593 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001594 triangulator->makeEvent(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c);
1595 triangulator->makeEvent(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001596 }
1597 }
Stephen Whitee260c462017-12-19 18:09:54 -05001598}
1599
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001600static bool is_overlap_edge(Edge* e) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001601 if (e->fType == EdgeType::kOuter) {
Stephen Whitee260c462017-12-19 18:09:54 -05001602 return e->fWinding != 0 && e->fWinding != 1;
Chris Dalton17ce8c52021-01-07 18:08:46 -07001603 } else if (e->fType == EdgeType::kInner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001604 return e->fWinding != 0 && e->fWinding != -2;
1605 } else {
1606 return false;
1607 }
1608}
1609
1610// This is a stripped-down version of tessellate() which computes edges which
1611// join two filled regions, which represent overlap regions, and collapses them.
Chris Dalton7cf3add2021-01-11 18:33:28 -07001612bool GrAATriangulator::collapseOverlapRegions(VertexList* mesh, const Comparator& c,
1613 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001614 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001615 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001616 EventList events(comp);
1617 SSVertexMap ssVertices;
1618 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001619 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001620 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001621 continue;
1622 }
1623 Edge* leftEnclosingEdge;
1624 Edge* rightEnclosingEdge;
1625 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001626 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001627 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
Chris Daltond60c9192021-01-07 18:30:08 +00001628 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001629 bool leftOverlap = prev && is_overlap_edge(prev);
1630 bool rightOverlap = is_overlap_edge(e);
Chris Dalton17ce8c52021-01-07 18:08:46 -07001631 bool isOuterBoundary = e->fType == EdgeType::kOuter &&
Stephen Whitec4dbc372019-05-22 10:50:14 -04001632 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001633 if (prev) {
1634 e->fWinding -= prev->fWinding;
1635 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001636 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001637 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1638 e->fTop->fID, e->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +00001639 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001640 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001641 TESS_LOG("found overlap edge %g -> %g%s\n",
1642 e->fTop->fID, e->fBottom->fID,
1643 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001644 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1645 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1646 SSVertex* ssPrev = ssVertices[prevVertex];
1647 if (!ssPrev) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001648 ssPrev = ssVertices[prevVertex] = fAlloc.make<SSVertex>(prevVertex);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001649 }
1650 SSVertex* ssNext = ssVertices[nextVertex];
1651 if (!ssNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001652 ssNext = ssVertices[nextVertex] = fAlloc.make<SSVertex>(nextVertex);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001653 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001654 SSEdge* ssEdge = fAlloc.make<SSEdge>(e, ssPrev, ssNext);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001655 ssEdges.push_back(ssEdge);
1656// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1657 ssPrev->fNext = ssNext->fPrev = ssEdge;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001658 this->makeEvent(ssEdge, &events);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001659 if (!isOuterBoundary) {
Chris Daltond60c9192021-01-07 18:30:08 +00001660 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001661 }
1662 }
1663 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001664 }
1665 Edge* prev = leftEnclosingEdge;
1666 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1667 if (prev) {
1668 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001669 }
Chris Daltond60c9192021-01-07 18:30:08 +00001670 insert_edge(e, prev, &activeEdges);
Stephen Whitee260c462017-12-19 18:09:54 -05001671 prev = e;
1672 }
1673 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001674 bool complex = events.size() > 0;
1675
Brian Salomon120e7d62019-09-11 10:29:22 -04001676 TESS_LOG("\ncollapsing overlap regions\n");
1677 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001678 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001679 while (events.size() > 0) {
1680 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001681 events.pop();
Chris Dalton7cf3add2021-01-11 18:33:28 -07001682 event->apply(mesh, c, &events, this);
Stephen Whitee260c462017-12-19 18:09:54 -05001683 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001684 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001685 dump_skel(ssEdges);
1686 for (SSEdge* edge : ssEdges) {
1687 if (Edge* e = edge->fEdge) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001688 this->makeConnectingEdge(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001689 }
1690 }
1691 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001692}
1693
Chris Dalton811dc6a2021-01-07 16:40:32 -07001694static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001695 if (!prev || !next) {
1696 return true;
1697 }
1698 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1699 return winding != origEdge->fWinding;
1700}
Stephen White92eba8a2017-02-06 09:50:27 -05001701
senorblancof57372d2016-08-31 10:36:19 -07001702// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1703// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1704// new antialiased mesh from those vertices.
1705
Chris Dalton7cf3add2021-01-11 18:33:28 -07001706void GrAATriangulator::strokeBoundary(EdgeList* boundary, VertexList* innerMesh,
1707 VertexList* outerMesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001708 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001709 // A boundary with fewer than 3 edges is degenerate.
1710 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1711 return;
1712 }
1713 Edge* prevEdge = boundary->fTail;
1714 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1715 SkVector prevNormal;
1716 get_edge_normal(prevEdge, &prevNormal);
1717 double radius = 0.5;
1718 Line prevInner(prevEdge->fLine);
1719 prevInner.fC -= radius;
1720 Line prevOuter(prevEdge->fLine);
1721 prevOuter.fC += radius;
1722 VertexList innerVertices;
1723 VertexList outerVertices;
1724 bool innerInversion = true;
1725 bool outerInversion = true;
1726 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1727 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1728 SkVector normal;
1729 get_edge_normal(e, &normal);
1730 Line inner(e->fLine);
1731 inner.fC -= radius;
1732 Line outer(e->fLine);
1733 outer.fC += radius;
1734 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001735 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001736 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1737 prevOuter.intersect(outer, &outerPoint)) {
1738 float cosAngle = normal.dot(prevNormal);
1739 if (cosAngle < -kCosMiterAngle) {
1740 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1741
1742 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1743 Line bisector(innerPoint, outerPoint);
1744 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1745 if (tangent.fA == 0 && tangent.fB == 0) {
1746 continue;
1747 }
1748 tangent.normalize();
1749 Line innerTangent(tangent);
1750 Line outerTangent(tangent);
1751 innerTangent.fC -= 0.5;
1752 outerTangent.fC += 0.5;
1753 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1754 if (prevNormal.cross(normal) > 0) {
1755 // Miter inner points
1756 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1757 !innerTangent.intersect(inner, &innerPoint2) ||
1758 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001759 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001760 }
1761 Line prevTangent(prevV->fPoint,
1762 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1763 Line nextTangent(nextV->fPoint,
1764 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001765 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001766 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001767 }
1768 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001769 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001770 }
1771 outerPoint1 = outerPoint2 = outerPoint;
1772 } else {
1773 // Miter outer points
1774 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1775 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001776 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001777 }
1778 Line prevTangent(prevV->fPoint,
1779 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1780 Line nextTangent(nextV->fPoint,
1781 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001782 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001783 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001784 }
1785 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001786 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001787 }
1788 innerPoint1 = innerPoint2 = innerPoint;
1789 }
Stephen Whiteea495232018-04-03 11:28:15 -04001790 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1791 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1792 continue;
1793 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001794 TESS_LOG("inner (%g, %g), (%g, %g), ",
1795 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1796 TESS_LOG("outer (%g, %g), (%g, %g)\n",
1797 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001798 Vertex* innerVertex1 = fAlloc.make<Vertex>(innerPoint1, 255);
1799 Vertex* innerVertex2 = fAlloc.make<Vertex>(innerPoint2, 255);
1800 Vertex* outerVertex1 = fAlloc.make<Vertex>(outerPoint1, 0);
1801 Vertex* outerVertex2 = fAlloc.make<Vertex>(outerPoint2, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001802 innerVertex1->fPartner = outerVertex1;
1803 innerVertex2->fPartner = outerVertex2;
1804 outerVertex1->fPartner = innerVertex1;
1805 outerVertex2->fPartner = innerVertex2;
1806 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1807 innerInversion = false;
1808 }
1809 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1810 outerInversion = false;
1811 }
1812 innerVertices.append(innerVertex1);
1813 innerVertices.append(innerVertex2);
1814 outerVertices.append(outerVertex1);
1815 outerVertices.append(outerVertex2);
1816 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001817 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1818 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001819 Vertex* innerVertex = fAlloc.make<Vertex>(innerPoint, 255);
1820 Vertex* outerVertex = fAlloc.make<Vertex>(outerPoint, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001821 innerVertex->fPartner = outerVertex;
1822 outerVertex->fPartner = innerVertex;
1823 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1824 innerInversion = false;
1825 }
1826 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
1827 outerInversion = false;
1828 }
1829 innerVertices.append(innerVertex);
1830 outerVertices.append(outerVertex);
1831 }
1832 }
1833 prevInner = inner;
1834 prevOuter = outer;
1835 prevV = v;
1836 prevEdge = e;
1837 prevNormal = normal;
1838 }
1839 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
1840 innerInversion = false;
1841 }
1842 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
1843 outerInversion = false;
1844 }
1845 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
1846 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
1847 // interior inverts).
1848 // For total inversion cases, the shape has now reversed handedness, so invert the winding
Chris Dalton7cf3add2021-01-11 18:33:28 -07001849 // so it will be detected during collapseOverlapRegions().
Stephen Whitee260c462017-12-19 18:09:54 -05001850 int innerWinding = innerInversion ? 2 : -2;
1851 int outerWinding = outerInversion ? -1 : 1;
1852 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001853 this->makeConnectingEdge(v, v->fNext, EdgeType::kInner, c, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001854 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001855 this->makeConnectingEdge(innerVertices.fTail, innerVertices.fHead, EdgeType::kInner, c,
1856 innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001857 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001858 this->makeConnectingEdge(v, v->fNext, EdgeType::kOuter, c, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001859 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001860 this->makeConnectingEdge(outerVertices.fTail, outerVertices.fHead, EdgeType::kOuter, c,
1861 outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001862 innerMesh->append(innerVertices);
1863 outerMesh->append(outerVertices);
1864}
senorblancof57372d2016-08-31 10:36:19 -07001865
Chris Dalton7cf3add2021-01-11 18:33:28 -07001866void GrAATriangulator::extractBoundary(EdgeList* boundary, Edge* e) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001867 TESS_LOG("\nextracting boundary\n");
Chris Dalton7cf3add2021-01-11 18:33:28 -07001868 bool down = apply_fill_type(fPath.getFillType(), e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04001869 Vertex* start = down ? e->fTop : e->fBottom;
1870 do {
senorblancof57372d2016-08-31 10:36:19 -07001871 e->fWinding = down ? 1 : -1;
1872 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05001873 e->fLine.normalize();
1874 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07001875 boundary->append(e);
1876 if (down) {
1877 // Find outgoing edge, in clockwise order.
1878 if ((next = e->fNextEdgeAbove)) {
1879 down = false;
1880 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1881 down = true;
1882 } else if ((next = e->fPrevEdgeAbove)) {
1883 down = false;
1884 }
1885 } else {
1886 // Find outgoing edge, in counter-clockwise order.
1887 if ((next = e->fPrevEdgeBelow)) {
1888 down = true;
1889 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1890 down = false;
1891 } else if ((next = e->fNextEdgeBelow)) {
1892 down = true;
1893 }
1894 }
Chris Daltond60c9192021-01-07 18:30:08 +00001895 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001896 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04001897 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07001898}
1899
Stephen White5ad721e2017-02-23 16:50:47 -05001900// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07001901
Chris Dalton7cf3add2021-01-11 18:33:28 -07001902void GrAATriangulator::extractBoundaries(const VertexList& inMesh, VertexList* innerVertices,
1903 VertexList* outerVertices, const Comparator& c) {
1904 this->removeNonBoundaryEdges(inMesh);
Stephen White5ad721e2017-02-23 16:50:47 -05001905 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001906 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05001907 EdgeList boundary;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001908 this->extractBoundary(&boundary, v->fFirstEdgeBelow);
1909 this->simplifyBoundary(&boundary, c);
1910 this->strokeBoundary(&boundary, innerVertices, outerVertices, c);
senorblancof57372d2016-08-31 10:36:19 -07001911 }
1912 }
senorblancof57372d2016-08-31 10:36:19 -07001913}
1914
Stephen Whitebda29c02017-03-13 15:10:13 -04001915// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08001916
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001917void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001918 const Comparator& c) {
Chris Dalton5045de32021-01-07 19:09:01 -07001919#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -08001920 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05001921 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08001922 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04001923 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001924 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001925 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08001926 }
1927 }
1928#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001929 this->sanitizeContours(contours, contourCnt);
1930 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07001931}
1932
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001933void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001934 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05001935 return;
ethannicholase9709e82016-01-07 13:34:16 -08001936 }
1937
1938 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05001939 if (c.fDirection == Comparator::Direction::kHorizontal) {
1940 merge_sort<sweep_lt_horiz>(vertices);
1941 } else {
1942 merge_sort<sweep_lt_vert>(vertices);
1943 }
Chris Dalton5045de32021-01-07 19:09:01 -07001944#if TRIANGULATOR_LOGGING
Stephen White2e2cb9b2017-01-09 13:11:18 -05001945 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001946 static float gID = 0.0f;
1947 v->fID = gID++;
1948 }
1949#endif
Stephen White2f4686f2017-01-03 16:20:01 -05001950}
1951
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001952Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt, VertexList* outerMesh) {
1953 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05001954 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
1955 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05001956 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001957 this->contoursToMesh(contours, contourCnt, &mesh, c);
1958 SortMesh(&mesh, c);
1959 this->mergeCoincidentVertices(&mesh, c);
1960 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001961 return nullptr;
1962 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001963 TESS_LOG("\nsimplified mesh:\n");
Chris Dalton7cf3add2021-01-11 18:33:28 -07001964 DUMP_MESH(mesh);
1965 return this->tessellate(mesh, outerMesh, c);
1966}
1967
1968Poly* GrAATriangulator::tessellate(const VertexList& mesh, VertexList* outerMesh,
1969 const Comparator& c) {
1970 VertexList innerMesh;
1971 this->extractBoundaries(mesh, &innerMesh, outerMesh, c);
1972 SortMesh(&innerMesh, c);
1973 SortMesh(outerMesh, c);
1974 this->mergeCoincidentVertices(&innerMesh, c);
1975 bool was_complex = this->mergeCoincidentVertices(outerMesh, c);
1976 auto result = this->simplify(&innerMesh, c);
1977 SkASSERT(SimplifyResult::kAbort != result);
1978 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
1979 result = this->simplify(outerMesh, c);
1980 SkASSERT(SimplifyResult::kAbort != result);
1981 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
1982 TESS_LOG("\ninner mesh before:\n");
1983 DUMP_MESH(innerMesh);
1984 TESS_LOG("\nouter mesh before:\n");
1985 DUMP_MESH(*outerMesh);
1986 EventComparator eventLT(EventComparator::Op::kLessThan);
1987 EventComparator eventGT(EventComparator::Op::kGreaterThan);
1988 was_complex = this->collapseOverlapRegions(&innerMesh, c, eventLT) || was_complex;
1989 was_complex = this->collapseOverlapRegions(outerMesh, c, eventGT) || was_complex;
1990 if (was_complex) {
1991 TESS_LOG("found complex mesh; taking slow path\n");
1992 VertexList aaMesh;
1993 TESS_LOG("\ninner mesh after:\n");
1994 DUMP_MESH(innerMesh);
1995 TESS_LOG("\nouter mesh after:\n");
1996 DUMP_MESH(*outerMesh);
1997 this->connectPartners(outerMesh, c);
1998 this->connectPartners(&innerMesh, c);
1999 sorted_merge(&innerMesh, outerMesh, &aaMesh, c);
2000 this->mergeCoincidentVertices(&aaMesh, c);
2001 result = this->simplify(&aaMesh, c);
Chris Dalton6ccc0322020-01-29 11:38:16 -07002002 SkASSERT(SimplifyResult::kAbort != result);
Chris Dalton7cf3add2021-01-11 18:33:28 -07002003 TESS_LOG("combined and simplified mesh:\n");
2004 DUMP_MESH(aaMesh);
2005 outerMesh->fHead = outerMesh->fTail = nullptr;
2006 return this->GrTriangulator::tessellate(aaMesh, outerMesh, c);
Stephen White49789062017-02-21 10:35:49 -05002007 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07002008 TESS_LOG("no complex polygons; taking fast path\n");
2009 return this->GrTriangulator::tessellate(innerMesh, outerMesh, c);
senorblancof57372d2016-08-31 10:36:19 -07002010 }
senorblancof57372d2016-08-31 10:36:19 -07002011}
2012
2013// Stage 6: Triangulate the monotone polygons into a vertex buffer.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002014void* GrTriangulator::polysToTriangles(Poly* polys, void* data, SkPathFillType overrideFillType) {
senorblancof57372d2016-08-31 10:36:19 -07002015 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002016 if (apply_fill_type(overrideFillType, poly)) {
Chris Dalton9a4904f2021-01-07 19:10:14 -07002017 data = this->emitPoly(poly, data);
senorblancof57372d2016-08-31 10:36:19 -07002018 }
2019 }
2020 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002021}
2022
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002023Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt,
2024 VertexList* outerMesh) {
2025 if (SkPathFillType_IsInverse(fPath.getFillType())) {
ethannicholase9709e82016-01-07 13:34:16 -08002026 contourCnt++;
2027 }
Stephen White3a9aab92017-03-07 14:07:18 -05002028 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
ethannicholase9709e82016-01-07 13:34:16 -08002029
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002030 this->pathToContours(tolerance, clipBounds, contours.get());
2031 return this->contoursToPolys(contours.get(), contourCnt, outerMesh);
ethannicholase9709e82016-01-07 13:34:16 -08002032}
2033
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002034static int get_contour_count(const SkPath& path, SkScalar tolerance) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002035 // We could theoretically be more aggressive about not counting empty contours, but we need to
2036 // actually match the exact number of contour linked lists the tessellator will create later on.
2037 int contourCnt = 1;
2038 bool hasPoints = false;
2039
Chris Dalton7156db22020-05-07 22:06:28 +00002040 SkPath::Iter iter(path, false);
2041 SkPath::Verb verb;
2042 SkPoint pts[4];
Chris Daltonc71b3d42020-01-08 21:29:59 -07002043 bool first = true;
Chris Dalton7156db22020-05-07 22:06:28 +00002044 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
Chris Daltonc71b3d42020-01-08 21:29:59 -07002045 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +00002046 case SkPath::kMove_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002047 if (!first) {
2048 ++contourCnt;
2049 }
John Stiles30212b72020-06-11 17:55:07 -04002050 [[fallthrough]];
Chris Dalton7156db22020-05-07 22:06:28 +00002051 case SkPath::kLine_Verb:
2052 case SkPath::kConic_Verb:
2053 case SkPath::kQuad_Verb:
2054 case SkPath::kCubic_Verb:
Chris Daltonc71b3d42020-01-08 21:29:59 -07002055 hasPoints = true;
John Stiles30212b72020-06-11 17:55:07 -04002056 break;
Chris Daltonc71b3d42020-01-08 21:29:59 -07002057 default:
2058 break;
2059 }
2060 first = false;
2061 }
2062 if (!hasPoints) {
Stephen White11f65e02017-02-16 19:00:39 -05002063 return 0;
ethannicholase9709e82016-01-07 13:34:16 -08002064 }
Stephen White11f65e02017-02-16 19:00:39 -05002065 return contourCnt;
ethannicholase9709e82016-01-07 13:34:16 -08002066}
2067
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002068static int64_t count_points(Poly* polys, SkPathFillType fillType) {
Greg Danield5b45932018-06-07 13:15:10 -04002069 int64_t count = 0;
ethannicholase9709e82016-01-07 13:34:16 -08002070 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002071 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002072 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
ethannicholase9709e82016-01-07 13:34:16 -08002073 }
2074 }
2075 return count;
2076}
2077
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002078static int64_t count_outer_mesh_points(const VertexList& outerMesh) {
Greg Danield5b45932018-06-07 13:15:10 -04002079 int64_t count = 0;
Stephen Whitebda29c02017-03-13 15:10:13 -04002080 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2081 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002082 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002083 }
2084 }
2085 return count;
2086}
2087
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002088static void* outer_mesh_to_triangles(const VertexList& outerMesh, bool emitCoverage, void* data) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002089 for (Vertex* v = outerMesh.fHead; v; v = v->fNext) {
2090 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2091 Vertex* v0 = e->fTop;
2092 Vertex* v1 = e->fBottom;
2093 Vertex* v2 = e->fBottom->fPartner;
2094 Vertex* v3 = e->fTop->fPartner;
Brian Osman0995fd52019-01-09 09:52:25 -05002095 data = emit_triangle(v0, v1, v2, emitCoverage, data);
2096 data = emit_triangle(v0, v2, v3, emitCoverage, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002097 }
2098 }
2099 return data;
2100}
2101
ethannicholase9709e82016-01-07 13:34:16 -08002102// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2103
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002104int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
2105 GrEagerVertexAllocator* vertexAllocator,
2106 SkPathFillType overrideFillType) {
2107 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002108 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002109 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002110 return 0;
2111 }
Stephen Whitebda29c02017-03-13 15:10:13 -04002112 VertexList outerMesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002113 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt, &outerMesh);
2114 int64_t count64 = count_points(polys, overrideFillType);
Chris Dalton854ee852021-01-05 15:12:59 -07002115 if (fEmitCoverage) {
Greg Danield5b45932018-06-07 13:15:10 -04002116 count64 += count_outer_mesh_points(outerMesh);
Stephen Whitebda29c02017-03-13 15:10:13 -04002117 }
Greg Danield5b45932018-06-07 13:15:10 -04002118 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002119 return 0;
2120 }
Greg Danield5b45932018-06-07 13:15:10 -04002121 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002122
Chris Dalton854ee852021-01-05 15:12:59 -07002123 size_t vertexStride = sizeof(SkPoint);
2124 if (fEmitCoverage) {
2125 vertexStride += sizeof(float);
2126 }
Chris Daltond081dce2020-01-23 12:09:04 -07002127 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002128 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002129 SkDebugf("Could not allocate vertices\n");
2130 return 0;
2131 }
senorblancof57372d2016-08-31 10:36:19 -07002132
Brian Salomon120e7d62019-09-11 10:29:22 -04002133 TESS_LOG("emitting %d verts\n", count);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002134 void* end = this->polysToTriangles(polys, verts, overrideFillType);
Brian Osman80879d42019-01-07 16:15:27 -05002135 end = outer_mesh_to_triangles(outerMesh, true, end);
Brian Osman80879d42019-01-07 16:15:27 -05002136
senorblancof57372d2016-08-31 10:36:19 -07002137 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002138 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002139 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002140 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002141 return actualCount;
2142}
2143
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002144int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2145 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002146 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002147 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002148 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002149 return 0;
2150 }
Chris Dalton854ee852021-01-05 15:12:59 -07002151 GrTriangulator triangulator(path);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002152 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt, nullptr);
Mike Reedcf0e3c62019-12-03 16:26:15 -05002153 SkPathFillType fillType = path.getFillType();
Greg Danield5b45932018-06-07 13:15:10 -04002154 int64_t count64 = count_points(polys, fillType);
2155 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002156 *verts = nullptr;
2157 return 0;
2158 }
Greg Danield5b45932018-06-07 13:15:10 -04002159 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002160
Chris Dalton17dc4182020-03-25 16:18:16 -06002161 *verts = new WindingVertex[count];
2162 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002163 SkPoint* points = new SkPoint[count];
2164 SkPoint* pointsEnd = points;
2165 for (Poly* poly = polys; poly; poly = poly->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07002166 if (apply_fill_type(fillType, poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002167 SkPoint* start = pointsEnd;
Chris Dalton9a4904f2021-01-07 19:10:14 -07002168 pointsEnd = static_cast<SkPoint*>(triangulator.emitPoly(poly, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002169 while (start != pointsEnd) {
2170 vertsEnd->fPos = *start;
2171 vertsEnd->fWinding = poly->fWinding;
2172 ++start;
2173 ++vertsEnd;
2174 }
2175 }
2176 }
2177 int actualCount = static_cast<int>(vertsEnd - *verts);
2178 SkASSERT(actualCount <= count);
2179 SkASSERT(pointsEnd - points == actualCount);
2180 delete[] points;
2181 return actualCount;
2182}