blob: 916457a10e020d133f6fd0292627050b3c99d60f [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) {
Chris Dalton93c2d812021-01-11 19:51:59 -0700287 SkASSERT(monotonePoly->fWinding != 0);
Chris Dalton9a4904f2021-01-07 19:10:14 -0700288 Edge* e = monotonePoly->fFirstEdge;
Chris Dalton5045de32021-01-07 19:09:01 -0700289 VertexList vertices;
290 vertices.append(e->fTop);
291 int count = 1;
292 while (e != nullptr) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700293 if (kRight_Side == monotonePoly->fSide) {
Chris Dalton5045de32021-01-07 19:09:01 -0700294 vertices.append(e->fBottom);
295 e = e->fRightPolyNext;
296 } else {
297 vertices.prepend(e->fBottom);
298 e = e->fLeftPolyNext;
Chris Dalton17ce8c52021-01-07 18:08:46 -0700299 }
Chris Dalton5045de32021-01-07 19:09:01 -0700300 count++;
301 }
302 Vertex* first = vertices.fHead;
303 Vertex* v = first->fNext;
304 while (v != vertices.fTail) {
305 SkASSERT(v && v->fPrev && v->fNext);
306 Vertex* prev = v->fPrev;
307 Vertex* curr = v;
308 Vertex* next = v->fNext;
309 if (count == 3) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700310 return this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700311 }
312 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
313 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
314 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
315 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
316 if (ax * by - ay * bx >= 0.0) {
Chris Dalton9a4904f2021-01-07 19:10:14 -0700317 data = this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700318 v->fPrev->fNext = v->fNext;
319 v->fNext->fPrev = v->fPrev;
320 count--;
321 if (v->fPrev == first) {
Chris Dalton17ce8c52021-01-07 18:08:46 -0700322 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000323 } else {
Chris Dalton5045de32021-01-07 19:09:01 -0700324 v = v->fPrev;
Chris Daltond60c9192021-01-07 18:30:08 +0000325 }
Chris Dalton5045de32021-01-07 19:09:01 -0700326 } else {
327 v = v->fNext;
Chris Daltond60c9192021-01-07 18:30:08 +0000328 }
Chris Dalton75887a22021-01-06 00:23:13 -0700329 }
Chris Dalton5045de32021-01-07 19:09:01 -0700330 return data;
331}
332
Chris Dalton9a4904f2021-01-07 19:10:14 -0700333void* GrTriangulator::emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding,
334 void* data) const {
Chris Dalton93c2d812021-01-11 19:51:59 -0700335 if (winding > 0) {
Chris Dalton5045de32021-01-07 19:09:01 -0700336 // Ensure our triangles always wind in the same direction as if the path had been
337 // triangulated as a simple fan (a la red book).
338 std::swap(prev, next);
339 }
Chris Dalton93c2d812021-01-11 19:51:59 -0700340 return emit_triangle(prev, curr, next, fEmitCoverage, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700341}
342
343Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
344 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
345 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
346 Poly* partner = fPartner;
347 Poly* poly = this;
348 if (side == kRight_Side) {
349 if (e->fUsedInRightPoly) {
350 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000351 }
Chris Dalton5045de32021-01-07 19:09:01 -0700352 } else {
353 if (e->fUsedInLeftPoly) {
354 return this;
Chris Daltond60c9192021-01-07 18:30:08 +0000355 }
Chris Dalton5045de32021-01-07 19:09:01 -0700356 }
357 if (partner) {
358 fPartner = partner->fPartner = nullptr;
359 }
360 if (!fTail) {
361 fHead = fTail = alloc.make<MonotonePoly>(e, side, fWinding);
362 fCount += 2;
363 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
364 return poly;
365 } else if (side == fTail->fSide) {
366 fTail->addEdge(e);
367 fCount++;
368 } else {
369 e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner);
370 fTail->addEdge(e);
371 fCount++;
372 if (partner) {
373 partner->addEdge(e, side, alloc);
374 poly = partner;
375 } else {
376 MonotonePoly* m = alloc.make<MonotonePoly>(e, side, fWinding);
377 m->fPrev = fTail;
378 fTail->fNext = m;
379 fTail = m;
380 }
381 }
382 return poly;
383}
Chris Dalton9a4904f2021-01-07 19:10:14 -0700384void* GrTriangulator::emitPoly(const Poly* poly, void *data) {
385 if (poly->fCount < 3) {
ethannicholase9709e82016-01-07 13:34:16 -0800386 return data;
387 }
Chris Dalton5045de32021-01-07 19:09:01 -0700388 TESS_LOG("emit() %d, size %d\n", fID, fCount);
Chris Dalton9a4904f2021-01-07 19:10:14 -0700389 for (MonotonePoly* m = poly->fHead; m != nullptr; m = m->fNext) {
390 data = this->emitMonotonePoly(m, data);
Chris Dalton5045de32021-01-07 19:09:01 -0700391 }
392 return data;
393}
ethannicholase9709e82016-01-07 13:34:16 -0800394
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700395static bool coincident(const SkPoint& a, const SkPoint& b) {
ethannicholase9709e82016-01-07 13:34:16 -0800396 return a == b;
397}
398
Chris Dalton7cf3add2021-01-11 18:33:28 -0700399Poly* GrTriangulator::makePoly(Poly** head, Vertex* v, int winding) {
400 Poly* poly = fAlloc.make<Poly>(v, winding);
ethannicholase9709e82016-01-07 13:34:16 -0800401 poly->fNext = *head;
402 *head = poly;
403 return poly;
404}
405
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700406void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) {
407 Vertex* v = fAlloc.make<Vertex>(p, 255);
Chris Dalton5045de32021-01-07 19:09:01 -0700408#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -0800409 static float gID = 0.0f;
410 v->fID = gID++;
411#endif
Stephen White3a9aab92017-03-07 14:07:18 -0500412 contour->append(v);
ethannicholase9709e82016-01-07 13:34:16 -0800413}
414
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700415static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
Stephen White36e4f062017-03-27 16:11:31 -0400416 SkQuadCoeff quad(pts);
417 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
418 SkPoint mid = to_point(quad.eval(t));
419 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
Stephen Whitee3a0be72017-06-12 11:43:18 -0400420 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
421 return 0;
422 }
Cary Clarkdf429f32017-11-08 11:44:31 -0500423 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
Stephen White36e4f062017-03-27 16:11:31 -0400424}
425
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700426void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
427 VertexList* contour) {
Stephen White36e4f062017-03-27 16:11:31 -0400428 SkQuadCoeff quad(pts);
429 Sk2s aa = quad.fA * quad.fA;
430 SkScalar denom = 2.0f * (aa[0] + aa[1]);
431 Sk2s ab = quad.fA * quad.fB;
432 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
433 int nPoints = 1;
Stephen Whitee40c3612018-01-09 11:49:08 -0500434 SkScalar u = 1.0f;
Stephen White36e4f062017-03-27 16:11:31 -0400435 // Test possible subdivision values only at the point of maximum curvature.
436 // If it passes the flatness metric there, it'll pass everywhere.
Stephen Whitee40c3612018-01-09 11:49:08 -0500437 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
Stephen White36e4f062017-03-27 16:11:31 -0400438 u = 1.0f / nPoints;
439 if (quad_error_at(pts, t, u) < toleranceSqd) {
440 break;
441 }
442 nPoints++;
ethannicholase9709e82016-01-07 13:34:16 -0800443 }
Stephen White36e4f062017-03-27 16:11:31 -0400444 for (int j = 1; j <= nPoints; j++) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700445 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
Stephen White36e4f062017-03-27 16:11:31 -0400446 }
ethannicholase9709e82016-01-07 13:34:16 -0800447}
448
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700449void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
450 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
451 int pointsLeft) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500452 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
453 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
ethannicholase9709e82016-01-07 13:34:16 -0800454 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
455 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700456 this->appendPointToContour(p3, contour);
Stephen White3a9aab92017-03-07 14:07:18 -0500457 return;
ethannicholase9709e82016-01-07 13:34:16 -0800458 }
459 const SkPoint q[] = {
460 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
461 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
462 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
463 };
464 const SkPoint r[] = {
465 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
466 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
467 };
468 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
469 pointsLeft >>= 1;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700470 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
471 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800472}
473
474// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
475
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700476void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
477 VertexList* contours) {
ethannicholase9709e82016-01-07 13:34:16 -0800478 SkScalar toleranceSqd = tolerance * tolerance;
Chris Dalton7156db22020-05-07 22:06:28 +0000479 SkPoint pts[4];
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700480 fIsLinear = true;
Stephen White3a9aab92017-03-07 14:07:18 -0500481 VertexList* contour = contours;
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700482 SkPath::Iter iter(fPath, false);
483 if (fPath.isInverseFillType()) {
ethannicholase9709e82016-01-07 13:34:16 -0800484 SkPoint quad[4];
485 clipBounds.toQuad(quad);
senorblanco7ab96e92016-10-12 06:47:44 -0700486 for (int i = 3; i >= 0; i--) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700487 this->appendPointToContour(quad[i], contours);
ethannicholase9709e82016-01-07 13:34:16 -0800488 }
Stephen White3a9aab92017-03-07 14:07:18 -0500489 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800490 }
491 SkAutoConicToQuads converter;
Chris Dalton7156db22020-05-07 22:06:28 +0000492 SkPath::Verb verb;
493 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
ethannicholase9709e82016-01-07 13:34:16 -0800494 switch (verb) {
Chris Dalton7156db22020-05-07 22:06:28 +0000495 case SkPath::kConic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700496 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700497 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700498 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700499 break;
500 }
Chris Dalton7156db22020-05-07 22:06:28 +0000501 SkScalar weight = iter.conicWeight();
502 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
ethannicholase9709e82016-01-07 13:34:16 -0800503 for (int i = 0; i < converter.countQuads(); ++i) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700504 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800505 quadPts += 2;
506 }
ethannicholase9709e82016-01-07 13:34:16 -0800507 break;
508 }
Chris Dalton7156db22020-05-07 22:06:28 +0000509 case SkPath::kMove_Verb:
Stephen White3a9aab92017-03-07 14:07:18 -0500510 if (contour->fHead) {
511 contour++;
ethannicholase9709e82016-01-07 13:34:16 -0800512 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700513 this->appendPointToContour(pts[0], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800514 break;
Chris Dalton7156db22020-05-07 22:06:28 +0000515 case SkPath::kLine_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700516 this->appendPointToContour(pts[1], contour);
ethannicholase9709e82016-01-07 13:34:16 -0800517 break;
518 }
Chris Dalton7156db22020-05-07 22:06:28 +0000519 case SkPath::kQuad_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700520 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700521 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700522 this->appendPointToContour(pts[2], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700523 break;
524 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700525 this->appendQuadraticToContour(pts, toleranceSqd, contour);
ethannicholase9709e82016-01-07 13:34:16 -0800526 break;
527 }
Chris Dalton7156db22020-05-07 22:06:28 +0000528 case SkPath::kCubic_Verb: {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700529 fIsLinear = false;
Chris Dalton854ee852021-01-05 15:12:59 -0700530 if (fSimpleInnerPolygons) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700531 this->appendPointToContour(pts[3], contour);
Chris Dalton6ccc0322020-01-29 11:38:16 -0700532 break;
533 }
ethannicholase9709e82016-01-07 13:34:16 -0800534 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700535 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
536 pointsLeft);
ethannicholase9709e82016-01-07 13:34:16 -0800537 break;
538 }
Chris Dalton7156db22020-05-07 22:06:28 +0000539 case SkPath::kClose_Verb:
540 case SkPath::kDone_Verb:
ethannicholase9709e82016-01-07 13:34:16 -0800541 break;
542 }
543 }
544}
545
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700546static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
ethannicholase9709e82016-01-07 13:34:16 -0800547 switch (fillType) {
Mike Reed7d34dc72019-11-26 12:17:17 -0500548 case SkPathFillType::kWinding:
ethannicholase9709e82016-01-07 13:34:16 -0800549 return winding != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500550 case SkPathFillType::kEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800551 return (winding & 1) != 0;
Mike Reed7d34dc72019-11-26 12:17:17 -0500552 case SkPathFillType::kInverseWinding:
senorblanco7ab96e92016-10-12 06:47:44 -0700553 return winding == 1;
Mike Reed7d34dc72019-11-26 12:17:17 -0500554 case SkPathFillType::kInverseEvenOdd:
ethannicholase9709e82016-01-07 13:34:16 -0800555 return (winding & 1) == 1;
556 default:
557 SkASSERT(false);
558 return false;
559 }
560}
561
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700562static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
Stephen White49789062017-02-21 10:35:49 -0500563 return poly && apply_fill_type(fillType, poly->fWinding);
564}
565
Chris Dalton7cf3add2021-01-11 18:33:28 -0700566Edge* GrTriangulator::makeEdge(Vertex* prev, Vertex* next, EdgeType type, const Comparator& c) {
Stephen White2f4686f2017-01-03 16:20:01 -0500567 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
ethannicholase9709e82016-01-07 13:34:16 -0800568 Vertex* top = winding < 0 ? next : prev;
569 Vertex* bottom = winding < 0 ? prev : next;
Chris Dalton7cf3add2021-01-11 18:33:28 -0700570 return fAlloc.make<Edge>(top, bottom, winding, type);
ethannicholase9709e82016-01-07 13:34:16 -0800571}
572
Chris Daltond60c9192021-01-07 18:30:08 +0000573static void remove_edge(Edge* edge, EdgeList* edges) {
574 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
575 SkASSERT(edges->contains(edge));
576 edges->remove(edge);
577}
578
579static void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400580 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +0000581 SkASSERT(!edges->contains(edge));
582 Edge* next = prev ? prev->fRight : edges->fHead;
583 edges->insert(edge, prev, next);
ethannicholase9709e82016-01-07 13:34:16 -0800584}
585
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700586static void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
Stephen White90732fd2017-03-02 16:16:33 -0500587 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800588 *left = v->fFirstEdgeAbove->fLeft;
589 *right = v->fLastEdgeAbove->fRight;
590 return;
591 }
592 Edge* next = nullptr;
593 Edge* prev;
594 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
595 if (prev->isLeftOf(v)) {
596 break;
597 }
598 next = prev;
599 }
600 *left = prev;
601 *right = next;
ethannicholase9709e82016-01-07 13:34:16 -0800602}
603
Chris Dalton811dc6a2021-01-07 16:40:32 -0700604static void insert_edge_above(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800605 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500606 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800607 return;
608 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400609 TESS_LOG("insert edge (%g -> %g) above vertex %g\n",
610 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800611 Edge* prev = nullptr;
612 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000613 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -0800614 if (next->isRightOf(edge->fTop)) {
615 break;
616 }
617 prev = next;
618 }
senorblancoe6eaa322016-03-08 09:06:44 -0800619 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
Chris Daltond60c9192021-01-07 18:30:08 +0000620 edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
ethannicholase9709e82016-01-07 13:34:16 -0800621}
622
Chris Dalton811dc6a2021-01-07 16:40:32 -0700623static void insert_edge_below(Edge* edge, Vertex* v, const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800624 if (edge->fTop->fPoint == edge->fBottom->fPoint ||
Stephen Whitee30cf802017-02-27 11:37:55 -0500625 c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
ethannicholase9709e82016-01-07 13:34:16 -0800626 return;
627 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400628 TESS_LOG("insert edge (%g -> %g) below vertex %g\n",
629 edge->fTop->fID, edge->fBottom->fID, v->fID);
ethannicholase9709e82016-01-07 13:34:16 -0800630 Edge* prev = nullptr;
631 Edge* next;
Chris Daltond60c9192021-01-07 18:30:08 +0000632 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
ethannicholase9709e82016-01-07 13:34:16 -0800633 if (next->isRightOf(edge->fBottom)) {
634 break;
635 }
636 prev = next;
637 }
senorblancoe6eaa322016-03-08 09:06:44 -0800638 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
Chris Daltond60c9192021-01-07 18:30:08 +0000639 edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
ethannicholase9709e82016-01-07 13:34:16 -0800640}
641
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700642static void remove_edge_above(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400643 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400644 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
645 edge->fBottom->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800646 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
ethannicholase9709e82016-01-07 13:34:16 -0800647 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
648}
649
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700650static void remove_edge_below(Edge* edge) {
Stephen White7b376942018-05-22 11:51:32 -0400651 SkASSERT(edge->fTop && edge->fBottom);
Brian Salomon120e7d62019-09-11 10:29:22 -0400652 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
653 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
senorblancoe6eaa322016-03-08 09:06:44 -0800654 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
ethannicholase9709e82016-01-07 13:34:16 -0800655 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
656}
657
Chris Daltond60c9192021-01-07 18:30:08 +0000658static void disconnect(Edge* edge)
659{
660 remove_edge_above(edge);
661 remove_edge_below(edge);
Stephen Whitee7a364d2017-01-11 16:19:26 -0500662}
663
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700664static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700665 const Comparator& c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400666
Chris Dalton811dc6a2021-01-07 16:40:32 -0700667static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400668 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
669 return;
670 }
671 Vertex* v = *current;
Brian Salomon120e7d62019-09-11 10:29:22 -0400672 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400673 while (v != dst) {
674 v = v->fPrev;
675 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +0000676 remove_edge(e, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400677 }
678 Edge* leftEdge = v->fLeftEnclosingEdge;
679 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +0000680 insert_edge(e, leftEdge, activeEdges);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400681 leftEdge = e;
Stephen Whitec03e6982020-02-06 16:32:14 -0500682 Vertex* top = e->fTop;
683 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
684 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
685 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
686 dst = top;
687 }
Stephen White3b5a3fa2017-06-06 14:51:19 -0400688 }
689 }
690 *current = v;
691}
692
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700693static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700694 const Comparator& c) {
Stephen Whitec03e6982020-02-06 16:32:14 -0500695 if (!activeEdges || !current) {
696 return;
697 }
698 Vertex* top = edge->fTop;
699 Vertex* bottom = edge->fBottom;
700 if (edge->fLeft) {
701 Vertex* leftTop = edge->fLeft->fTop;
702 Vertex* leftBottom = edge->fLeft->fBottom;
703 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
704 rewind(activeEdges, current, leftTop, c);
705 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
706 rewind(activeEdges, current, top, c);
707 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
708 !edge->fLeft->isLeftOf(bottom)) {
709 rewind(activeEdges, current, leftTop, c);
710 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
711 rewind(activeEdges, current, top, c);
712 }
713 }
714 if (edge->fRight) {
715 Vertex* rightTop = edge->fRight->fTop;
716 Vertex* rightBottom = edge->fRight->fBottom;
717 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
718 rewind(activeEdges, current, rightTop, c);
719 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
720 rewind(activeEdges, current, top, c);
721 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
722 !edge->fRight->isRightOf(bottom)) {
723 rewind(activeEdges, current, rightTop, c);
724 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
725 !edge->isLeftOf(rightBottom)) {
726 rewind(activeEdges, current, top, c);
727 }
728 }
729}
730
Chris Dalton811dc6a2021-01-07 16:40:32 -0700731static void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
732 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800733 remove_edge_below(edge);
734 edge->fTop = v;
735 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000736 insert_edge_below(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500737 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400738 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800739}
740
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700741static void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700742 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800743 remove_edge_above(edge);
744 edge->fBottom = v;
745 edge->recompute();
Chris Daltond60c9192021-01-07 18:30:08 +0000746 insert_edge_above(edge, v, c);
Stephen Whitec03e6982020-02-06 16:32:14 -0500747 rewind_if_necessary(edge, activeEdges, current, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400748 merge_collinear_edges(edge, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800749}
750
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700751static void merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700752 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800753 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400754 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
755 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
756 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400757 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800758 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +0000759 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -0400760 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -0800761 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400762 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800763 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400764 set_bottom(edge, other->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800765 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400766 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800767 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400768 set_bottom(other, edge->fTop, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800769 }
770}
771
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700772static void merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700773 const Comparator& c) {
ethannicholase9709e82016-01-07 13:34:16 -0800774 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400775 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
776 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
777 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400778 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800779 other->fWinding += edge->fWinding;
Chris Daltond60c9192021-01-07 18:30:08 +0000780 disconnect(edge);
Stephen Whiteec79c392018-05-18 11:49:21 -0400781 edge->fTop = edge->fBottom = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -0800782 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400783 rewind(activeEdges, current, other->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800784 edge->fWinding += other->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400785 set_top(other, edge->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800786 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400787 rewind(activeEdges, current, edge->fTop, c);
ethannicholase9709e82016-01-07 13:34:16 -0800788 other->fWinding += edge->fWinding;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400789 set_top(edge, other->fBottom, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800790 }
791}
792
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700793static bool top_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400794 if (!left || !right) {
795 return false;
796 }
797 return left->fTop->fPoint == right->fTop->fPoint ||
798 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
799}
800
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700801static bool bottom_collinear(Edge* left, Edge* right) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400802 if (!left || !right) {
803 return false;
804 }
805 return left->fBottom->fPoint == right->fBottom->fPoint ||
806 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
807}
808
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700809static void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700810 const Comparator& c) {
Stephen White6eca90f2017-05-25 14:47:11 -0400811 for (;;) {
Stephen Whited26b4d82018-07-26 10:02:27 -0400812 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400813 merge_edges_above(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400814 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
Stephen White24289e02018-06-29 17:02:21 -0400815 merge_edges_above(edge->fNextEdgeAbove, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400816 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
Stephen White24289e02018-06-29 17:02:21 -0400817 merge_edges_below(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
Stephen Whited26b4d82018-07-26 10:02:27 -0400818 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
Stephen White24289e02018-06-29 17:02:21 -0400819 merge_edges_below(edge->fNextEdgeBelow, edge, activeEdges, current, c);
Stephen White6eca90f2017-05-25 14:47:11 -0400820 } else {
821 break;
822 }
ethannicholase9709e82016-01-07 13:34:16 -0800823 }
Stephen Whited26b4d82018-07-26 10:02:27 -0400824 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
825 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
826 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
827 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
ethannicholase9709e82016-01-07 13:34:16 -0800828}
829
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700830bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700831 const Comparator& c) {
Stephen Whiteec79c392018-05-18 11:49:21 -0400832 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
Stephen White89042d52018-06-08 12:18:22 -0400833 return false;
Stephen White0cb31672017-06-08 14:41:01 -0400834 }
Brian Salomon120e7d62019-09-11 10:29:22 -0400835 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
836 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400837 Vertex* top;
838 Vertex* bottom;
Stephen White531a48e2018-06-01 09:49:39 -0400839 int winding = edge->fWinding;
ethannicholase9709e82016-01-07 13:34:16 -0800840 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400841 top = v;
842 bottom = edge->fTop;
843 set_top(edge, v, activeEdges, current, c);
Stephen Whitee30cf802017-02-27 11:37:55 -0500844 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400845 top = edge->fBottom;
846 bottom = v;
847 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800848 } else {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400849 top = v;
850 bottom = edge->fBottom;
851 set_bottom(edge, v, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -0800852 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700853 Edge* newEdge = fAlloc.make<Edge>(top, bottom, winding, edge->fType);
Chris Daltond60c9192021-01-07 18:30:08 +0000854 insert_edge_below(newEdge, top, c);
855 insert_edge_above(newEdge, bottom, c);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400856 merge_collinear_edges(newEdge, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400857 return true;
858}
859
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700860bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700861 Vertex** current, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -0400862 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
863 return false;
864 }
Stephen White1c5fd182018-07-12 15:54:05 -0400865 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
866 return false;
867 }
Stephen White89042d52018-06-08 12:18:22 -0400868 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
869 if (!left->isLeftOf(right->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400870 rewind(activeEdges, current, right->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700871 return this->splitEdge(left, right->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400872 }
873 } else {
874 if (!right->isRightOf(left->fTop)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400875 rewind(activeEdges, current, left->fTop, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700876 return this->splitEdge(right, left->fTop, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400877 }
878 }
879 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
880 if (!left->isLeftOf(right->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400881 rewind(activeEdges, current, right->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700882 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400883 }
884 } else {
885 if (!right->isRightOf(left->fBottom)) {
Stephen White1c5fd182018-07-12 15:54:05 -0400886 rewind(activeEdges, current, left->fBottom, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700887 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
Stephen White89042d52018-06-08 12:18:22 -0400888 }
889 }
890 return false;
ethannicholase9709e82016-01-07 13:34:16 -0800891}
892
Chris Dalton7cf3add2021-01-11 18:33:28 -0700893Edge* GrTriangulator::makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType type,
894 const Comparator& c, int windingScale) {
Stephen Whitee260c462017-12-19 18:09:54 -0500895 if (!prev || !next || prev->fPoint == next->fPoint) {
896 return nullptr;
897 }
Chris Dalton7cf3add2021-01-11 18:33:28 -0700898 Edge* edge = this->makeEdge(prev, next, type, c);
Chris Daltond60c9192021-01-07 18:30:08 +0000899 insert_edge_below(edge, edge->fTop, c);
900 insert_edge_above(edge, edge->fBottom, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -0700901 edge->fWinding *= windingScale;
Stephen White3b5a3fa2017-06-06 14:51:19 -0400902 merge_collinear_edges(edge, nullptr, nullptr, c);
senorblancof57372d2016-08-31 10:36:19 -0700903 return edge;
904}
905
Chris Dalton811dc6a2021-01-07 16:40:32 -0700906static void merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -0400907 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
908 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
Brian Osman788b9162020-02-07 10:36:46 -0500909 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
Stephen Whitebda29c02017-03-13 15:10:13 -0400910 if (src->fPartner) {
911 src->fPartner->fPartner = dst;
912 }
Stephen White7b376942018-05-22 11:51:32 -0400913 while (Edge* edge = src->fFirstEdgeAbove) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400914 set_bottom(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800915 }
Stephen White7b376942018-05-22 11:51:32 -0400916 while (Edge* edge = src->fFirstEdgeBelow) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400917 set_top(edge, dst, nullptr, nullptr, c);
ethannicholase9709e82016-01-07 13:34:16 -0800918 }
Stephen Whitebf6137e2017-01-04 15:43:26 -0500919 mesh->remove(src);
Stephen Whitec4dbc372019-05-22 10:50:14 -0400920 dst->fSynthetic = true;
ethannicholase9709e82016-01-07 13:34:16 -0800921}
922
Chris Dalton7cf3add2021-01-11 18:33:28 -0700923Vertex* GrTriangulator::makeSortedVertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
924 Vertex* reference, const Comparator& c) {
Stephen White95152e12017-12-18 10:52:44 -0500925 Vertex* prevV = reference;
926 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
927 prevV = prevV->fPrev;
928 }
929 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
930 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
931 prevV = nextV;
932 nextV = nextV->fNext;
933 }
934 Vertex* v;
935 if (prevV && coincident(prevV->fPoint, p)) {
936 v = prevV;
937 } else if (nextV && coincident(nextV->fPoint, p)) {
938 v = nextV;
939 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -0700940 v = fAlloc.make<Vertex>(p, alpha);
Chris Dalton5045de32021-01-07 19:09:01 -0700941#if TRIANGULATOR_LOGGING
Stephen White95152e12017-12-18 10:52:44 -0500942 if (!prevV) {
943 v->fID = mesh->fHead->fID - 1.0f;
944 } else if (!nextV) {
945 v->fID = mesh->fTail->fID + 1.0f;
946 } else {
947 v->fID = (prevV->fID + nextV->fID) * 0.5f;
948 }
949#endif
950 mesh->insert(v, prevV, nextV);
951 }
952 return v;
953}
954
Stephen White53a02982018-05-30 22:47:46 -0400955// If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
956// sort criterion, it may not be possible to split correctly, since there is no point which is
957// below the top and above the bottom. This function detects that case.
Chris Dalton811dc6a2021-01-07 16:40:32 -0700958static bool nearly_flat(const Comparator& c, Edge* edge) {
Stephen White53a02982018-05-30 22:47:46 -0400959 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
960 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
Stephen White13f3d8d2018-06-22 10:19:20 -0400961 return fabs(primaryDiff) < std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
Stephen White53a02982018-05-30 22:47:46 -0400962}
963
Chris Dalton811dc6a2021-01-07 16:40:32 -0700964static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) {
Stephen Whitee62999f2018-06-05 18:45:07 -0400965 if (c.sweep_lt(p, min)) {
966 return min;
967 } else if (c.sweep_lt(max, p)) {
968 return max;
969 } else {
970 return p;
971 }
972}
973
Chris Dalton7cf3add2021-01-11 18:33:28 -0700974void GrTriangulator::computeBisector(Edge* edge1, Edge* edge2, Vertex* v) {
Stephen Whitec4dbc372019-05-22 10:50:14 -0400975 Line line1 = edge1->fLine;
976 Line line2 = edge2->fLine;
977 line1.normalize();
978 line2.normalize();
979 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
980 if (cosAngle > 0.999) {
981 return;
982 }
983 line1.fC += edge1->fWinding > 0 ? -1 : 1;
984 line2.fC += edge2->fWinding > 0 ? -1 : 1;
985 SkPoint p;
986 if (line1.intersect(line2, &p)) {
Chris Dalton17ce8c52021-01-07 18:08:46 -0700987 uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0;
Chris Dalton7cf3add2021-01-11 18:33:28 -0700988 v->fPartner = fAlloc.make<Vertex>(p, alpha);
Brian Salomon120e7d62019-09-11 10:29:22 -0400989 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 -0400990 }
991}
992
Chris Dalton57ea1fc2021-01-05 13:37:44 -0700993bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
Chris Dalton811dc6a2021-01-07 16:40:32 -0700994 Vertex** current, VertexList* mesh, const Comparator& c) {
Stephen Whiteb141fcb2018-06-14 10:15:47 -0400995 if (!left || !right) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400996 return false;
ethannicholase9709e82016-01-07 13:34:16 -0800997 }
Stephen White56158ae2017-01-30 14:31:31 -0500998 SkPoint p;
999 uint8_t alpha;
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001000 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
Ravi Mistrybfe95982018-05-29 18:19:07 +00001001 Vertex* v;
Brian Salomon120e7d62019-09-11 10:29:22 -04001002 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001003 Vertex* top = *current;
1004 // If the intersection point is above the current vertex, rewind to the vertex above the
1005 // intersection.
Stephen White0cb31672017-06-08 14:41:01 -04001006 while (top && c.sweep_lt(p, top->fPoint)) {
Stephen White3b5a3fa2017-06-06 14:51:19 -04001007 top = top->fPrev;
1008 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001009 if (!nearly_flat(c, left)) {
1010 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001011 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001012 if (!nearly_flat(c, right)) {
1013 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
Stephen Whitee62999f2018-06-05 18:45:07 -04001014 }
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001015 if (p == left->fTop->fPoint) {
1016 v = left->fTop;
1017 } else if (p == left->fBottom->fPoint) {
1018 v = left->fBottom;
1019 } else if (p == right->fTop->fPoint) {
1020 v = right->fTop;
1021 } else if (p == right->fBottom->fPoint) {
1022 v = right->fBottom;
Ravi Mistrybfe95982018-05-29 18:19:07 +00001023 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001024 v = this->makeSortedVertex(p, alpha, mesh, top, c);
Stephen Whiteb141fcb2018-06-14 10:15:47 -04001025 if (left->fTop->fPartner) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001026 v->fSynthetic = true;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001027 this->computeBisector(left, right, v);
Stephen Whitee260c462017-12-19 18:09:54 -05001028 }
ethannicholase9709e82016-01-07 13:34:16 -08001029 }
Stephen White0cb31672017-06-08 14:41:01 -04001030 rewind(activeEdges, current, top ? top : v, c);
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001031 this->splitEdge(left, v, activeEdges, current, c);
1032 this->splitEdge(right, v, activeEdges, current, c);
Brian Osman788b9162020-02-07 10:36:46 -05001033 v->fAlpha = std::max(v->fAlpha, alpha);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001034 return true;
ethannicholase9709e82016-01-07 13:34:16 -08001035 }
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001036 return this->intersectEdgePair(left, right, activeEdges, current, c);
ethannicholase9709e82016-01-07 13:34:16 -08001037}
1038
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001039void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) {
Stephen White3a9aab92017-03-07 14:07:18 -05001040 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1041 SkASSERT(contour->fHead);
1042 Vertex* prev = contour->fTail;
Chris Dalton854ee852021-01-05 15:12:59 -07001043 if (fRoundVerticesToQuarterPixel) {
Stephen White3a9aab92017-03-07 14:07:18 -05001044 round(&prev->fPoint);
Stephen White5926f2d2017-02-13 13:55:42 -05001045 }
Stephen White3a9aab92017-03-07 14:07:18 -05001046 for (Vertex* v = contour->fHead; v;) {
Chris Dalton854ee852021-01-05 15:12:59 -07001047 if (fRoundVerticesToQuarterPixel) {
senorblancof57372d2016-08-31 10:36:19 -07001048 round(&v->fPoint);
1049 }
Stephen White3a9aab92017-03-07 14:07:18 -05001050 Vertex* next = v->fNext;
Stephen White3de40f82018-06-28 09:36:49 -04001051 Vertex* nextWrap = next ? next : contour->fHead;
Stephen White3a9aab92017-03-07 14:07:18 -05001052 if (coincident(prev->fPoint, v->fPoint)) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001053 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001054 contour->remove(v);
Stephen White73e7f802017-08-23 13:56:07 -04001055 } else if (!v->fPoint.isFinite()) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001056 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White73e7f802017-08-23 13:56:07 -04001057 contour->remove(v);
Chris Dalton854ee852021-01-05 15:12:59 -07001058 } else if (fCullCollinearVertices &&
Chris Dalton6ccc0322020-01-29 11:38:16 -07001059 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001060 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
Stephen White06768ca2018-05-25 14:50:56 -04001061 contour->remove(v);
1062 } else {
1063 prev = v;
ethannicholase9709e82016-01-07 13:34:16 -08001064 }
Stephen White3a9aab92017-03-07 14:07:18 -05001065 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001066 }
1067 }
1068}
1069
Chris Dalton811dc6a2021-01-07 16:40:32 -07001070bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001071 if (!mesh->fHead) {
Stephen Whitee260c462017-12-19 18:09:54 -05001072 return false;
Stephen Whitebda29c02017-03-13 15:10:13 -04001073 }
Stephen Whitee260c462017-12-19 18:09:54 -05001074 bool merged = false;
1075 for (Vertex* v = mesh->fHead->fNext; v;) {
1076 Vertex* next = v->fNext;
ethannicholase9709e82016-01-07 13:34:16 -08001077 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1078 v->fPoint = v->fPrev->fPoint;
1079 }
1080 if (coincident(v->fPrev->fPoint, v->fPoint)) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001081 merge_vertices(v, v->fPrev, mesh, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001082 merged = true;
ethannicholase9709e82016-01-07 13:34:16 -08001083 }
Stephen Whitee260c462017-12-19 18:09:54 -05001084 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001085 }
Stephen Whitee260c462017-12-19 18:09:54 -05001086 return merged;
ethannicholase9709e82016-01-07 13:34:16 -08001087}
1088
1089// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1090
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001091void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001092 const Comparator& c) {
Stephen White3a9aab92017-03-07 14:07:18 -05001093 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1094 Vertex* prev = contour->fTail;
1095 for (Vertex* v = contour->fHead; v;) {
1096 Vertex* next = v->fNext;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001097 this->makeConnectingEdge(prev, v, EdgeType::kInner, c);
Stephen White3a9aab92017-03-07 14:07:18 -05001098 mesh->append(v);
ethannicholase9709e82016-01-07 13:34:16 -08001099 prev = v;
Stephen White3a9aab92017-03-07 14:07:18 -05001100 v = next;
ethannicholase9709e82016-01-07 13:34:16 -08001101 }
1102 }
ethannicholase9709e82016-01-07 13:34:16 -08001103}
1104
Chris Dalton7cf3add2021-01-11 18:33:28 -07001105void GrAATriangulator::connectPartners(VertexList* mesh, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001106 for (Vertex* outer = mesh->fHead; outer; outer = outer->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001107 if (Vertex* inner = outer->fPartner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001108 if ((inner->fPrev || inner->fNext) && (outer->fPrev || outer->fNext)) {
1109 // Connector edges get zero winding, since they're only structural (i.e., to ensure
1110 // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding
1111 // number.
Chris Dalton7cf3add2021-01-11 18:33:28 -07001112 this->makeConnectingEdge(outer, inner, EdgeType::kConnector, c, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001113 inner->fPartner = outer->fPartner = nullptr;
1114 }
Stephen Whitebda29c02017-03-13 15:10:13 -04001115 }
1116 }
1117}
1118
1119template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001120static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001121 Vertex* a = front->fHead;
1122 Vertex* b = back->fHead;
1123 while (a && b) {
1124 if (sweep_lt(a->fPoint, b->fPoint)) {
1125 front->remove(a);
1126 result->append(a);
1127 a = front->fHead;
1128 } else {
1129 back->remove(b);
1130 result->append(b);
1131 b = back->fHead;
1132 }
1133 }
1134 result->append(*front);
1135 result->append(*back);
1136}
1137
Chris Dalton811dc6a2021-01-07 16:40:32 -07001138static void sorted_merge(VertexList* front, VertexList* back, VertexList* result,
1139 const Comparator& c) {
Stephen Whitebda29c02017-03-13 15:10:13 -04001140 if (c.fDirection == Comparator::Direction::kHorizontal) {
1141 sorted_merge<sweep_lt_horiz>(front, back, result);
1142 } else {
1143 sorted_merge<sweep_lt_vert>(front, back, result);
1144 }
Chris Dalton5045de32021-01-07 19:09:01 -07001145#if TRIANGULATOR_LOGGING
Stephen White3b5a3fa2017-06-06 14:51:19 -04001146 float id = 0.0f;
1147 for (Vertex* v = result->fHead; v; v = v->fNext) {
1148 v->fID = id++;
1149 }
1150#endif
Stephen Whitebda29c02017-03-13 15:10:13 -04001151}
1152
ethannicholase9709e82016-01-07 13:34:16 -08001153// Stage 3: sort the vertices by increasing sweep direction.
1154
Stephen White16a40cb2017-02-23 11:10:01 -05001155template <CompareFunc sweep_lt>
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001156static void merge_sort(VertexList* vertices) {
Stephen White16a40cb2017-02-23 11:10:01 -05001157 Vertex* slow = vertices->fHead;
1158 if (!slow) {
ethannicholase9709e82016-01-07 13:34:16 -08001159 return;
1160 }
Stephen White16a40cb2017-02-23 11:10:01 -05001161 Vertex* fast = slow->fNext;
1162 if (!fast) {
1163 return;
1164 }
1165 do {
1166 fast = fast->fNext;
1167 if (fast) {
1168 fast = fast->fNext;
1169 slow = slow->fNext;
1170 }
1171 } while (fast);
1172 VertexList front(vertices->fHead, slow);
1173 VertexList back(slow->fNext, vertices->fTail);
1174 front.fTail->fNext = back.fHead->fPrev = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08001175
Stephen White16a40cb2017-02-23 11:10:01 -05001176 merge_sort<sweep_lt>(&front);
1177 merge_sort<sweep_lt>(&back);
ethannicholase9709e82016-01-07 13:34:16 -08001178
Stephen White16a40cb2017-02-23 11:10:01 -05001179 vertices->fHead = vertices->fTail = nullptr;
Stephen Whitebda29c02017-03-13 15:10:13 -04001180 sorted_merge<sweep_lt>(&front, &back, vertices);
ethannicholase9709e82016-01-07 13:34:16 -08001181}
1182
Chris Dalton5045de32021-01-07 19:09:01 -07001183#if TRIANGULATOR_LOGGING
Chris Dalton7cf3add2021-01-11 18:33:28 -07001184static void dump_mesh(const VertexList& mesh) {
Chris Daltond60c9192021-01-07 18:30:08 +00001185 for (Vertex* v = mesh.fHead; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001186 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 -05001187 if (Vertex* p = v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001188 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1189 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
Stephen White95152e12017-12-18 10:52:44 -05001190 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001191 TESS_LOG(", null partner\n");
Stephen White95152e12017-12-18 10:52:44 -05001192 }
1193 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001194 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001195 }
1196 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001197 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
Stephen White95152e12017-12-18 10:52:44 -05001198 }
1199 }
Chris Daltond60c9192021-01-07 18:30:08 +00001200}
Chris Dalton7cf3add2021-01-11 18:33:28 -07001201#endif
Stephen White95152e12017-12-18 10:52:44 -05001202
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001203static void dump_skel(const SSEdgeList& ssEdges) {
Chris Dalton5045de32021-01-07 19:09:01 -07001204#if TRIANGULATOR_LOGGING
Stephen Whitec4dbc372019-05-22 10:50:14 -04001205 for (SSEdge* edge : ssEdges) {
1206 if (edge->fEdge) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001207 TESS_LOG("skel edge %g -> %g",
Stephen Whitec4dbc372019-05-22 10:50:14 -04001208 edge->fPrev->fVertex->fID,
Stephen White8a3c0592019-05-29 11:26:16 -04001209 edge->fNext->fVertex->fID);
1210 if (edge->fEdge->fTop && edge->fEdge->fBottom) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001211 TESS_LOG(" (original %g -> %g)\n",
1212 edge->fEdge->fTop->fID,
1213 edge->fEdge->fBottom->fID);
Stephen White8a3c0592019-05-29 11:26:16 -04001214 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001215 TESS_LOG("\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001216 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001217 }
1218 }
1219#endif
1220}
1221
Stephen White89042d52018-06-08 12:18:22 -04001222#ifdef SK_DEBUG
Chris Dalton811dc6a2021-01-07 16:40:32 -07001223static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001224 if (!left || !right) {
1225 return;
1226 }
1227 if (left->fTop == right->fTop) {
1228 SkASSERT(left->isLeftOf(right->fBottom));
1229 SkASSERT(right->isRightOf(left->fBottom));
1230 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1231 SkASSERT(left->isLeftOf(right->fTop));
1232 } else {
1233 SkASSERT(right->isRightOf(left->fTop));
1234 }
1235 if (left->fBottom == right->fBottom) {
1236 SkASSERT(left->isLeftOf(right->fTop));
1237 SkASSERT(right->isRightOf(left->fTop));
1238 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1239 SkASSERT(left->isLeftOf(right->fBottom));
1240 } else {
1241 SkASSERT(right->isRightOf(left->fBottom));
1242 }
1243}
1244
Chris Dalton811dc6a2021-01-07 16:40:32 -07001245static void validate_edge_list(EdgeList* edges, const Comparator& c) {
Stephen White89042d52018-06-08 12:18:22 -04001246 Edge* left = edges->fHead;
1247 if (!left) {
1248 return;
1249 }
1250 for (Edge* right = left->fRight; right; right = right->fRight) {
1251 validate_edge_pair(left, right, c);
1252 left = right;
1253 }
1254}
1255#endif
1256
ethannicholase9709e82016-01-07 13:34:16 -08001257// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1258
Chris Daltond60c9192021-01-07 18:30:08 +00001259static bool connected(Vertex* v) {
1260 return v->fFirstEdgeAbove || v->fFirstEdgeBelow;
1261}
1262
Chris Dalton811dc6a2021-01-07 16:40:32 -07001263GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001264 TESS_LOG("simplifying complex polygons\n");
ethannicholase9709e82016-01-07 13:34:16 -08001265 EdgeList activeEdges;
Chris Dalton6ccc0322020-01-29 11:38:16 -07001266 auto result = SimplifyResult::kAlreadySimple;
Stephen White0cb31672017-06-08 14:41:01 -04001267 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001268 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001269 continue;
1270 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001271 Edge* leftEnclosingEdge;
1272 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001273 bool restartChecks;
1274 do {
Brian Salomon120e7d62019-09-11 10:29:22 -04001275 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1276 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
ethannicholase9709e82016-01-07 13:34:16 -08001277 restartChecks = false;
1278 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White3b5a3fa2017-06-06 14:51:19 -04001279 v->fLeftEnclosingEdge = leftEnclosingEdge;
1280 v->fRightEnclosingEdge = rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001281 if (v->fFirstEdgeBelow) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001282 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001283 if (this->checkForIntersection(
1284 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1285 this->checkForIntersection(
1286 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001287 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001288 return SimplifyResult::kAbort;
1289 }
1290 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001291 restartChecks = true;
1292 break;
1293 }
1294 }
1295 } else {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001296 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1297 &v, mesh, c)) {
Chris Dalton854ee852021-01-05 15:12:59 -07001298 if (fSimpleInnerPolygons) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001299 return SimplifyResult::kAbort;
1300 }
1301 result = SimplifyResult::kFoundSelfIntersection;
ethannicholase9709e82016-01-07 13:34:16 -08001302 restartChecks = true;
1303 }
1304
1305 }
1306 } while (restartChecks);
Stephen White89042d52018-06-08 12:18:22 -04001307#ifdef SK_DEBUG
1308 validate_edge_list(&activeEdges, c);
1309#endif
ethannicholase9709e82016-01-07 13:34:16 -08001310 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Chris Daltond60c9192021-01-07 18:30:08 +00001311 remove_edge(e, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001312 }
1313 Edge* leftEdge = leftEnclosingEdge;
1314 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001315 insert_edge(e, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001316 leftEdge = e;
1317 }
ethannicholase9709e82016-01-07 13:34:16 -08001318 }
Stephen Whitee260c462017-12-19 18:09:54 -05001319 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
Chris Dalton6ccc0322020-01-29 11:38:16 -07001320 return result;
ethannicholase9709e82016-01-07 13:34:16 -08001321}
1322
1323// Stage 5: Tessellate the simplified mesh into monotone polygons.
1324
Chris Dalton93c2d812021-01-11 19:51:59 -07001325Poly* GrTriangulator::tessellate(const VertexList& vertices, const Comparator&) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001326 TESS_LOG("\ntessellating simple polygons\n");
Chris Dalton6ccc0322020-01-29 11:38:16 -07001327 int maxWindMagnitude = std::numeric_limits<int>::max();
Chris Dalton854ee852021-01-05 15:12:59 -07001328 if (fSimpleInnerPolygons && !SkPathFillType_IsEvenOdd(fPath.getFillType())) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001329 maxWindMagnitude = 1;
1330 }
ethannicholase9709e82016-01-07 13:34:16 -08001331 EdgeList activeEdges;
1332 Poly* polys = nullptr;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001333 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001334 if (!connected(v)) {
ethannicholase9709e82016-01-07 13:34:16 -08001335 continue;
1336 }
Chris Dalton5045de32021-01-07 19:09:01 -07001337#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001338 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 -08001339#endif
Stephen White8a0bfc52017-02-21 15:24:13 -05001340 Edge* leftEnclosingEdge;
1341 Edge* rightEnclosingEdge;
ethannicholase9709e82016-01-07 13:34:16 -08001342 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen White8a0bfc52017-02-21 15:24:13 -05001343 Poly* leftPoly;
1344 Poly* rightPoly;
ethannicholase9709e82016-01-07 13:34:16 -08001345 if (v->fFirstEdgeAbove) {
1346 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1347 rightPoly = v->fLastEdgeAbove->fRightPoly;
1348 } else {
1349 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1350 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1351 }
Chris Dalton5045de32021-01-07 19:09:01 -07001352#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001353 TESS_LOG("edges above:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001354 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001355 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1356 e->fTop->fID, e->fBottom->fID,
1357 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1358 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001359 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001360 TESS_LOG("edges below:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001361 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001362 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1363 e->fTop->fID, e->fBottom->fID,
1364 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1365 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001366 }
1367#endif
1368 if (v->fFirstEdgeAbove) {
1369 if (leftPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001370 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001371 }
1372 if (rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001373 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001374 }
1375 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
ethannicholase9709e82016-01-07 13:34:16 -08001376 Edge* rightEdge = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001377 remove_edge(e, &activeEdges);
Stephen White8a0bfc52017-02-21 15:24:13 -05001378 if (e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001379 e->fRightPoly->addEdge(e, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001380 }
Stephen White8a0bfc52017-02-21 15:24:13 -05001381 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001382 rightEdge->fLeftPoly->addEdge(e, kRight_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001383 }
1384 }
Chris Daltond60c9192021-01-07 18:30:08 +00001385 remove_edge(v->fLastEdgeAbove, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001386 if (!v->fFirstEdgeBelow) {
1387 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1388 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1389 rightPoly->fPartner = leftPoly;
1390 leftPoly->fPartner = rightPoly;
1391 }
1392 }
1393 }
1394 if (v->fFirstEdgeBelow) {
1395 if (!v->fFirstEdgeAbove) {
senorblanco93e3fff2016-06-07 12:36:00 -07001396 if (leftPoly && rightPoly) {
senorblanco531237e2016-06-02 11:36:48 -07001397 if (leftPoly == rightPoly) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001398 if (leftPoly->fTail && leftPoly->fTail->fSide == kLeft_Side) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001399 leftPoly = this->makePoly(&polys, leftPoly->lastVertex(),
1400 leftPoly->fWinding);
senorblanco531237e2016-06-02 11:36:48 -07001401 leftEnclosingEdge->fRightPoly = leftPoly;
1402 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001403 rightPoly = this->makePoly(&polys, rightPoly->lastVertex(),
1404 rightPoly->fWinding);
senorblanco531237e2016-06-02 11:36:48 -07001405 rightEnclosingEdge->fLeftPoly = rightPoly;
1406 }
ethannicholase9709e82016-01-07 13:34:16 -08001407 }
Chris Daltond60c9192021-01-07 18:30:08 +00001408 Edge* join = fAlloc.make<Edge>(leftPoly->lastVertex(), v, 1,
Chris Dalton17ce8c52021-01-07 18:08:46 -07001409 EdgeType::kInner);
1410 leftPoly = leftPoly->addEdge(join, kRight_Side, fAlloc);
1411 rightPoly = rightPoly->addEdge(join, kLeft_Side, fAlloc);
ethannicholase9709e82016-01-07 13:34:16 -08001412 }
1413 }
1414 Edge* leftEdge = v->fFirstEdgeBelow;
1415 leftEdge->fLeftPoly = leftPoly;
Chris Daltond60c9192021-01-07 18:30:08 +00001416 insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001417 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1418 rightEdge = rightEdge->fNextEdgeBelow) {
Chris Daltond60c9192021-01-07 18:30:08 +00001419 insert_edge(rightEdge, leftEdge, &activeEdges);
ethannicholase9709e82016-01-07 13:34:16 -08001420 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1421 winding += leftEdge->fWinding;
1422 if (winding != 0) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001423 if (abs(winding) > maxWindMagnitude) {
1424 return nullptr; // We can't have weighted wind in kSimpleInnerPolygons mode
1425 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001426 Poly* poly = this->makePoly(&polys, v, winding);
ethannicholase9709e82016-01-07 13:34:16 -08001427 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1428 }
1429 leftEdge = rightEdge;
1430 }
1431 v->fLastEdgeBelow->fRightPoly = rightPoly;
1432 }
Chris Dalton5045de32021-01-07 19:09:01 -07001433#if TRIANGULATOR_LOGGING
Brian Salomon120e7d62019-09-11 10:29:22 -04001434 TESS_LOG("\nactive edges:\n");
ethannicholase9709e82016-01-07 13:34:16 -08001435 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001436 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1437 e->fTop->fID, e->fBottom->fID,
1438 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1439 e->fRightPoly ? e->fRightPoly->fID : -1);
ethannicholase9709e82016-01-07 13:34:16 -08001440 }
1441#endif
1442 }
1443 return polys;
1444}
1445
Chris Dalton7cf3add2021-01-11 18:33:28 -07001446void GrAATriangulator::removeNonBoundaryEdges(const VertexList& mesh) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001447 TESS_LOG("removing non-boundary edges\n");
Stephen White49789062017-02-21 10:35:49 -05001448 EdgeList activeEdges;
Stephen Whitebf6137e2017-01-04 15:43:26 -05001449 for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001450 if (!connected(v)) {
Stephen White49789062017-02-21 10:35:49 -05001451 continue;
1452 }
1453 Edge* leftEnclosingEdge;
1454 Edge* rightEnclosingEdge;
1455 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1456 bool prevFilled = leftEnclosingEdge &&
Chris Dalton7cf3add2021-01-11 18:33:28 -07001457 apply_fill_type(fPath.getFillType(), leftEnclosingEdge->fWinding);
Stephen White49789062017-02-21 10:35:49 -05001458 for (Edge* e = v->fFirstEdgeAbove; e;) {
1459 Edge* next = e->fNextEdgeAbove;
Chris Daltond60c9192021-01-07 18:30:08 +00001460 remove_edge(e, &activeEdges);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001461 bool filled = apply_fill_type(fPath.getFillType(), e->fWinding);
Stephen White49789062017-02-21 10:35:49 -05001462 if (filled == prevFilled) {
Chris Daltond60c9192021-01-07 18:30:08 +00001463 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001464 }
Stephen White49789062017-02-21 10:35:49 -05001465 prevFilled = filled;
senorblancof57372d2016-08-31 10:36:19 -07001466 e = next;
1467 }
Stephen White49789062017-02-21 10:35:49 -05001468 Edge* prev = leftEnclosingEdge;
1469 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1470 if (prev) {
1471 e->fWinding += prev->fWinding;
1472 }
Chris Daltond60c9192021-01-07 18:30:08 +00001473 insert_edge(e, prev, &activeEdges);
Stephen White49789062017-02-21 10:35:49 -05001474 prev = e;
1475 }
senorblancof57372d2016-08-31 10:36:19 -07001476 }
senorblancof57372d2016-08-31 10:36:19 -07001477}
1478
Stephen White66412122017-03-01 11:48:27 -05001479// Note: this is the normal to the edge, but not necessarily unit length.
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001480static void get_edge_normal(const Edge* e, SkVector* normal) {
Stephen Whitee260c462017-12-19 18:09:54 -05001481 normal->set(SkDoubleToScalar(e->fLine.fA),
1482 SkDoubleToScalar(e->fLine.fB));
senorblancof57372d2016-08-31 10:36:19 -07001483}
1484
1485// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1486// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1487// invert on stroking.
1488
Chris Dalton7cf3add2021-01-11 18:33:28 -07001489void GrAATriangulator::simplifyBoundary(EdgeList* boundary, const Comparator& c) {
senorblancof57372d2016-08-31 10:36:19 -07001490 Edge* prevEdge = boundary->fTail;
1491 SkVector prevNormal;
1492 get_edge_normal(prevEdge, &prevNormal);
1493 for (Edge* e = boundary->fHead; e != nullptr;) {
1494 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1495 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
Stephen Whitecfe12642018-09-26 17:25:59 -04001496 double distPrev = e->dist(prev->fPoint);
1497 double distNext = prevEdge->dist(next->fPoint);
senorblancof57372d2016-08-31 10:36:19 -07001498 SkVector normal;
1499 get_edge_normal(e, &normal);
Stephen Whitecfe12642018-09-26 17:25:59 -04001500 constexpr double kQuarterPixelSq = 0.25f * 0.25f;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001501 if (prev == next) {
Chris Daltond60c9192021-01-07 18:30:08 +00001502 remove_edge(prevEdge, boundary);
1503 remove_edge(e, boundary);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001504 prevEdge = boundary->fTail;
1505 e = boundary->fHead;
1506 if (prevEdge) {
1507 get_edge_normal(prevEdge, &prevNormal);
1508 }
1509 } else if (prevNormal.dot(normal) < 0.0 &&
Stephen Whitecfe12642018-09-26 17:25:59 -04001510 (distPrev * distPrev <= kQuarterPixelSq || distNext * distNext <= kQuarterPixelSq)) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001511 Edge* join = this->makeEdge(prev, next, EdgeType::kInner, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001512 if (prev->fPoint != next->fPoint) {
1513 join->fLine.normalize();
1514 join->fLine = join->fLine * join->fWinding;
1515 }
Chris Daltond60c9192021-01-07 18:30:08 +00001516 insert_edge(join, e, boundary);
1517 remove_edge(prevEdge, boundary);
1518 remove_edge(e, boundary);
senorblancof57372d2016-08-31 10:36:19 -07001519 if (join->fLeft && join->fRight) {
1520 prevEdge = join->fLeft;
1521 e = join;
1522 } else {
1523 prevEdge = boundary->fTail;
1524 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1525 }
1526 get_edge_normal(prevEdge, &prevNormal);
1527 } else {
1528 prevEdge = e;
1529 prevNormal = normal;
1530 e = e->fRight;
1531 }
1532 }
1533}
1534
Chris Dalton7cf3add2021-01-11 18:33:28 -07001535void GrAATriangulator::connectSSEdge(Vertex* v, Vertex* dest, const Comparator& c) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001536 if (v == dest) {
1537 return;
Stephen Whitee260c462017-12-19 18:09:54 -05001538 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001539 TESS_LOG("ss_connecting vertex %g to vertex %g\n", v->fID, dest->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001540 if (v->fSynthetic) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001541 this->makeConnectingEdge(v, dest, EdgeType::kConnector, c, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001542 } else if (v->fPartner) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001543 TESS_LOG("setting %g's partner to %g ", v->fPartner->fID, dest->fID);
1544 TESS_LOG("and %g's partner to null\n", v->fID);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001545 v->fPartner->fPartner = dest;
1546 v->fPartner = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001547 }
1548}
1549
Chris Dalton7cf3add2021-01-11 18:33:28 -07001550void GrAATriangulator::Event::apply(VertexList* mesh, const Comparator& c, EventList* events,
1551 GrAATriangulator* triangulator) {
Stephen Whitec4dbc372019-05-22 10:50:14 -04001552 if (!fEdge) {
Stephen Whitee260c462017-12-19 18:09:54 -05001553 return;
1554 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001555 Vertex* prev = fEdge->fPrev->fVertex;
1556 Vertex* next = fEdge->fNext->fVertex;
1557 SSEdge* prevEdge = fEdge->fPrev->fPrev;
1558 SSEdge* nextEdge = fEdge->fNext->fNext;
1559 if (!prevEdge || !nextEdge || !prevEdge->fEdge || !nextEdge->fEdge) {
1560 return;
Stephen White77169c82018-06-05 09:15:59 -04001561 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001562 Vertex* dest = triangulator->makeSortedVertex(fPoint, fAlpha, mesh, prev, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001563 dest->fSynthetic = true;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001564 SSVertex* ssv = triangulator->fAlloc.make<SSVertex>(dest);
Brian Salomon120e7d62019-09-11 10:29:22 -04001565 TESS_LOG("collapsing %g, %g (original edge %g -> %g) to %g (%g, %g) alpha %d\n",
1566 prev->fID, next->fID, fEdge->fEdge->fTop->fID, fEdge->fEdge->fBottom->fID, dest->fID,
1567 fPoint.fX, fPoint.fY, fAlpha);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001568 fEdge->fEdge = nullptr;
Stephen Whitee260c462017-12-19 18:09:54 -05001569
Chris Dalton7cf3add2021-01-11 18:33:28 -07001570 triangulator->connectSSEdge(prev, dest, c);
1571 triangulator->connectSSEdge(next, dest, c);
Stephen Whitee260c462017-12-19 18:09:54 -05001572
Stephen Whitec4dbc372019-05-22 10:50:14 -04001573 prevEdge->fNext = nextEdge->fPrev = ssv;
1574 ssv->fPrev = prevEdge;
1575 ssv->fNext = nextEdge;
1576 if (!prevEdge->fEdge || !nextEdge->fEdge) {
1577 return;
1578 }
1579 if (prevEdge->fEvent) {
1580 prevEdge->fEvent->fEdge = nullptr;
1581 }
1582 if (nextEdge->fEvent) {
1583 nextEdge->fEvent->fEdge = nullptr;
1584 }
1585 if (prevEdge->fPrev == nextEdge->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001586 triangulator->connectSSEdge(prevEdge->fPrev->fVertex, dest, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001587 prevEdge->fEdge = nextEdge->fEdge = nullptr;
1588 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001589 triangulator->computeBisector(prevEdge->fEdge, nextEdge->fEdge, dest);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001590 SkASSERT(prevEdge != fEdge && nextEdge != fEdge);
1591 if (dest->fPartner) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001592 triangulator->makeEvent(prevEdge, events);
1593 triangulator->makeEvent(nextEdge, events);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001594 } else {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001595 triangulator->makeEvent(prevEdge, prevEdge->fPrev->fVertex, nextEdge, dest, events, c);
1596 triangulator->makeEvent(nextEdge, nextEdge->fNext->fVertex, prevEdge, dest, events, c);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001597 }
1598 }
Stephen Whitee260c462017-12-19 18:09:54 -05001599}
1600
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001601static bool is_overlap_edge(Edge* e) {
Chris Dalton17ce8c52021-01-07 18:08:46 -07001602 if (e->fType == EdgeType::kOuter) {
Stephen Whitee260c462017-12-19 18:09:54 -05001603 return e->fWinding != 0 && e->fWinding != 1;
Chris Dalton17ce8c52021-01-07 18:08:46 -07001604 } else if (e->fType == EdgeType::kInner) {
Stephen Whitee260c462017-12-19 18:09:54 -05001605 return e->fWinding != 0 && e->fWinding != -2;
1606 } else {
1607 return false;
1608 }
1609}
1610
1611// This is a stripped-down version of tessellate() which computes edges which
1612// join two filled regions, which represent overlap regions, and collapses them.
Chris Dalton7cf3add2021-01-11 18:33:28 -07001613bool GrAATriangulator::collapseOverlapRegions(VertexList* mesh, const Comparator& c,
1614 EventComparator comp) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001615 TESS_LOG("\nfinding overlap regions\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001616 EdgeList activeEdges;
Stephen Whitec4dbc372019-05-22 10:50:14 -04001617 EventList events(comp);
1618 SSVertexMap ssVertices;
1619 SSEdgeList ssEdges;
Stephen Whitee260c462017-12-19 18:09:54 -05001620 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
Chris Daltond60c9192021-01-07 18:30:08 +00001621 if (!connected(v)) {
Stephen Whitee260c462017-12-19 18:09:54 -05001622 continue;
1623 }
1624 Edge* leftEnclosingEdge;
1625 Edge* rightEnclosingEdge;
1626 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001627 for (Edge* e = v->fLastEdgeAbove; e && e != leftEnclosingEdge;) {
Stephen Whitee260c462017-12-19 18:09:54 -05001628 Edge* prev = e->fPrevEdgeAbove ? e->fPrevEdgeAbove : leftEnclosingEdge;
Chris Daltond60c9192021-01-07 18:30:08 +00001629 remove_edge(e, &activeEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001630 bool leftOverlap = prev && is_overlap_edge(prev);
1631 bool rightOverlap = is_overlap_edge(e);
Chris Dalton17ce8c52021-01-07 18:08:46 -07001632 bool isOuterBoundary = e->fType == EdgeType::kOuter &&
Stephen Whitec4dbc372019-05-22 10:50:14 -04001633 (!prev || prev->fWinding == 0 || e->fWinding == 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001634 if (prev) {
1635 e->fWinding -= prev->fWinding;
1636 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001637 if (leftOverlap && rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001638 TESS_LOG("found interior overlap edge %g -> %g, disconnecting\n",
1639 e->fTop->fID, e->fBottom->fID);
Chris Daltond60c9192021-01-07 18:30:08 +00001640 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001641 } else if (leftOverlap || rightOverlap) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001642 TESS_LOG("found overlap edge %g -> %g%s\n",
1643 e->fTop->fID, e->fBottom->fID,
1644 isOuterBoundary ? ", is outer boundary" : "");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001645 Vertex* prevVertex = e->fWinding < 0 ? e->fBottom : e->fTop;
1646 Vertex* nextVertex = e->fWinding < 0 ? e->fTop : e->fBottom;
1647 SSVertex* ssPrev = ssVertices[prevVertex];
1648 if (!ssPrev) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001649 ssPrev = ssVertices[prevVertex] = fAlloc.make<SSVertex>(prevVertex);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001650 }
1651 SSVertex* ssNext = ssVertices[nextVertex];
1652 if (!ssNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001653 ssNext = ssVertices[nextVertex] = fAlloc.make<SSVertex>(nextVertex);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001654 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001655 SSEdge* ssEdge = fAlloc.make<SSEdge>(e, ssPrev, ssNext);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001656 ssEdges.push_back(ssEdge);
1657// SkASSERT(!ssPrev->fNext && !ssNext->fPrev);
1658 ssPrev->fNext = ssNext->fPrev = ssEdge;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001659 this->makeEvent(ssEdge, &events);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001660 if (!isOuterBoundary) {
Chris Daltond60c9192021-01-07 18:30:08 +00001661 disconnect(e);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001662 }
1663 }
1664 e = prev;
Stephen Whitee260c462017-12-19 18:09:54 -05001665 }
1666 Edge* prev = leftEnclosingEdge;
1667 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1668 if (prev) {
1669 e->fWinding += prev->fWinding;
Stephen Whitee260c462017-12-19 18:09:54 -05001670 }
Chris Daltond60c9192021-01-07 18:30:08 +00001671 insert_edge(e, prev, &activeEdges);
Stephen Whitee260c462017-12-19 18:09:54 -05001672 prev = e;
1673 }
1674 }
Stephen Whitec4dbc372019-05-22 10:50:14 -04001675 bool complex = events.size() > 0;
1676
Brian Salomon120e7d62019-09-11 10:29:22 -04001677 TESS_LOG("\ncollapsing overlap regions\n");
1678 TESS_LOG("skeleton before:\n");
Stephen White8a3c0592019-05-29 11:26:16 -04001679 dump_skel(ssEdges);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001680 while (events.size() > 0) {
1681 Event* event = events.top();
Stephen Whitee260c462017-12-19 18:09:54 -05001682 events.pop();
Chris Dalton7cf3add2021-01-11 18:33:28 -07001683 event->apply(mesh, c, &events, this);
Stephen Whitee260c462017-12-19 18:09:54 -05001684 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001685 TESS_LOG("skeleton after:\n");
Stephen Whitec4dbc372019-05-22 10:50:14 -04001686 dump_skel(ssEdges);
1687 for (SSEdge* edge : ssEdges) {
1688 if (Edge* e = edge->fEdge) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001689 this->makeConnectingEdge(edge->fPrev->fVertex, edge->fNext->fVertex, e->fType, c, 0);
Stephen Whitec4dbc372019-05-22 10:50:14 -04001690 }
1691 }
1692 return complex;
Stephen Whitee260c462017-12-19 18:09:54 -05001693}
1694
Chris Dalton811dc6a2021-01-07 16:40:32 -07001695static bool inversion(Vertex* prev, Vertex* next, Edge* origEdge, const Comparator& c) {
Stephen Whitee260c462017-12-19 18:09:54 -05001696 if (!prev || !next) {
1697 return true;
1698 }
1699 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
1700 return winding != origEdge->fWinding;
1701}
Stephen White92eba8a2017-02-06 09:50:27 -05001702
senorblancof57372d2016-08-31 10:36:19 -07001703// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1704// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1705// new antialiased mesh from those vertices.
1706
Chris Dalton7cf3add2021-01-11 18:33:28 -07001707void GrAATriangulator::strokeBoundary(EdgeList* boundary, VertexList* innerMesh,
Chris Dalton93c2d812021-01-11 19:51:59 -07001708 const Comparator& c) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001709 TESS_LOG("\nstroking boundary\n");
Stephen Whitee260c462017-12-19 18:09:54 -05001710 // A boundary with fewer than 3 edges is degenerate.
1711 if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
1712 return;
1713 }
1714 Edge* prevEdge = boundary->fTail;
1715 Vertex* prevV = prevEdge->fWinding > 0 ? prevEdge->fTop : prevEdge->fBottom;
1716 SkVector prevNormal;
1717 get_edge_normal(prevEdge, &prevNormal);
1718 double radius = 0.5;
1719 Line prevInner(prevEdge->fLine);
1720 prevInner.fC -= radius;
1721 Line prevOuter(prevEdge->fLine);
1722 prevOuter.fC += radius;
1723 VertexList innerVertices;
1724 VertexList outerVertices;
1725 bool innerInversion = true;
1726 bool outerInversion = true;
1727 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1728 Vertex* v = e->fWinding > 0 ? e->fTop : e->fBottom;
1729 SkVector normal;
1730 get_edge_normal(e, &normal);
1731 Line inner(e->fLine);
1732 inner.fC -= radius;
1733 Line outer(e->fLine);
1734 outer.fC += radius;
1735 SkPoint innerPoint, outerPoint;
Brian Salomon120e7d62019-09-11 10:29:22 -04001736 TESS_LOG("stroking vertex %g (%g, %g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
Stephen Whitee260c462017-12-19 18:09:54 -05001737 if (!prevEdge->fLine.nearParallel(e->fLine) && prevInner.intersect(inner, &innerPoint) &&
1738 prevOuter.intersect(outer, &outerPoint)) {
1739 float cosAngle = normal.dot(prevNormal);
1740 if (cosAngle < -kCosMiterAngle) {
1741 Vertex* nextV = e->fWinding > 0 ? e->fBottom : e->fTop;
1742
1743 // This is a pointy vertex whose angle is smaller than the threshold; miter it.
1744 Line bisector(innerPoint, outerPoint);
1745 Line tangent(v->fPoint, v->fPoint + SkPoint::Make(bisector.fA, bisector.fB));
1746 if (tangent.fA == 0 && tangent.fB == 0) {
1747 continue;
1748 }
1749 tangent.normalize();
1750 Line innerTangent(tangent);
1751 Line outerTangent(tangent);
1752 innerTangent.fC -= 0.5;
1753 outerTangent.fC += 0.5;
1754 SkPoint innerPoint1, innerPoint2, outerPoint1, outerPoint2;
1755 if (prevNormal.cross(normal) > 0) {
1756 // Miter inner points
1757 if (!innerTangent.intersect(prevInner, &innerPoint1) ||
1758 !innerTangent.intersect(inner, &innerPoint2) ||
1759 !outerTangent.intersect(bisector, &outerPoint)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001760 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001761 }
1762 Line prevTangent(prevV->fPoint,
1763 prevV->fPoint + SkVector::Make(prevOuter.fA, prevOuter.fB));
1764 Line nextTangent(nextV->fPoint,
1765 nextV->fPoint + SkVector::Make(outer.fA, outer.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001766 if (prevTangent.dist(outerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001767 bisector.intersect(prevTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001768 }
1769 if (nextTangent.dist(outerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001770 bisector.intersect(nextTangent, &outerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001771 }
1772 outerPoint1 = outerPoint2 = outerPoint;
1773 } else {
1774 // Miter outer points
1775 if (!outerTangent.intersect(prevOuter, &outerPoint1) ||
1776 !outerTangent.intersect(outer, &outerPoint2)) {
Stephen Whitef470b7e2018-01-04 16:45:51 -05001777 continue;
Stephen Whitee260c462017-12-19 18:09:54 -05001778 }
1779 Line prevTangent(prevV->fPoint,
1780 prevV->fPoint + SkVector::Make(prevInner.fA, prevInner.fB));
1781 Line nextTangent(nextV->fPoint,
1782 nextV->fPoint + SkVector::Make(inner.fA, inner.fB));
Stephen Whitee260c462017-12-19 18:09:54 -05001783 if (prevTangent.dist(innerPoint) > 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001784 bisector.intersect(prevTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001785 }
1786 if (nextTangent.dist(innerPoint) < 0) {
Stephen White4f34fca2018-01-11 16:14:04 -05001787 bisector.intersect(nextTangent, &innerPoint);
Stephen Whitee260c462017-12-19 18:09:54 -05001788 }
1789 innerPoint1 = innerPoint2 = innerPoint;
1790 }
Stephen Whiteea495232018-04-03 11:28:15 -04001791 if (!innerPoint1.isFinite() || !innerPoint2.isFinite() ||
1792 !outerPoint1.isFinite() || !outerPoint2.isFinite()) {
1793 continue;
1794 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001795 TESS_LOG("inner (%g, %g), (%g, %g), ",
1796 innerPoint1.fX, innerPoint1.fY, innerPoint2.fX, innerPoint2.fY);
1797 TESS_LOG("outer (%g, %g), (%g, %g)\n",
1798 outerPoint1.fX, outerPoint1.fY, outerPoint2.fX, outerPoint2.fY);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001799 Vertex* innerVertex1 = fAlloc.make<Vertex>(innerPoint1, 255);
1800 Vertex* innerVertex2 = fAlloc.make<Vertex>(innerPoint2, 255);
1801 Vertex* outerVertex1 = fAlloc.make<Vertex>(outerPoint1, 0);
1802 Vertex* outerVertex2 = fAlloc.make<Vertex>(outerPoint2, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001803 innerVertex1->fPartner = outerVertex1;
1804 innerVertex2->fPartner = outerVertex2;
1805 outerVertex1->fPartner = innerVertex1;
1806 outerVertex2->fPartner = innerVertex2;
1807 if (!inversion(innerVertices.fTail, innerVertex1, prevEdge, c)) {
1808 innerInversion = false;
1809 }
1810 if (!inversion(outerVertices.fTail, outerVertex1, prevEdge, c)) {
1811 outerInversion = false;
1812 }
1813 innerVertices.append(innerVertex1);
1814 innerVertices.append(innerVertex2);
1815 outerVertices.append(outerVertex1);
1816 outerVertices.append(outerVertex2);
1817 } else {
Brian Salomon120e7d62019-09-11 10:29:22 -04001818 TESS_LOG("inner (%g, %g), ", innerPoint.fX, innerPoint.fY);
1819 TESS_LOG("outer (%g, %g)\n", outerPoint.fX, outerPoint.fY);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001820 Vertex* innerVertex = fAlloc.make<Vertex>(innerPoint, 255);
1821 Vertex* outerVertex = fAlloc.make<Vertex>(outerPoint, 0);
Stephen Whitee260c462017-12-19 18:09:54 -05001822 innerVertex->fPartner = outerVertex;
1823 outerVertex->fPartner = innerVertex;
1824 if (!inversion(innerVertices.fTail, innerVertex, prevEdge, c)) {
1825 innerInversion = false;
1826 }
1827 if (!inversion(outerVertices.fTail, outerVertex, prevEdge, c)) {
1828 outerInversion = false;
1829 }
1830 innerVertices.append(innerVertex);
1831 outerVertices.append(outerVertex);
1832 }
1833 }
1834 prevInner = inner;
1835 prevOuter = outer;
1836 prevV = v;
1837 prevEdge = e;
1838 prevNormal = normal;
1839 }
1840 if (!inversion(innerVertices.fTail, innerVertices.fHead, prevEdge, c)) {
1841 innerInversion = false;
1842 }
1843 if (!inversion(outerVertices.fTail, outerVertices.fHead, prevEdge, c)) {
1844 outerInversion = false;
1845 }
1846 // Outer edges get 1 winding, and inner edges get -2 winding. This ensures that the interior
1847 // is always filled (1 + -2 = -1 for normal cases, 1 + 2 = 3 for thin features where the
1848 // interior inverts).
1849 // For total inversion cases, the shape has now reversed handedness, so invert the winding
Chris Dalton7cf3add2021-01-11 18:33:28 -07001850 // so it will be detected during collapseOverlapRegions().
Stephen Whitee260c462017-12-19 18:09:54 -05001851 int innerWinding = innerInversion ? 2 : -2;
1852 int outerWinding = outerInversion ? -1 : 1;
1853 for (Vertex* v = innerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001854 this->makeConnectingEdge(v, v->fNext, EdgeType::kInner, c, innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001855 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001856 this->makeConnectingEdge(innerVertices.fTail, innerVertices.fHead, EdgeType::kInner, c,
1857 innerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001858 for (Vertex* v = outerVertices.fHead; v && v->fNext; v = v->fNext) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001859 this->makeConnectingEdge(v, v->fNext, EdgeType::kOuter, c, outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001860 }
Chris Dalton7cf3add2021-01-11 18:33:28 -07001861 this->makeConnectingEdge(outerVertices.fTail, outerVertices.fHead, EdgeType::kOuter, c,
1862 outerWinding);
Stephen Whitee260c462017-12-19 18:09:54 -05001863 innerMesh->append(innerVertices);
Chris Dalton93c2d812021-01-11 19:51:59 -07001864 fOuterMesh.append(outerVertices);
Stephen Whitee260c462017-12-19 18:09:54 -05001865}
senorblancof57372d2016-08-31 10:36:19 -07001866
Chris Dalton7cf3add2021-01-11 18:33:28 -07001867void GrAATriangulator::extractBoundary(EdgeList* boundary, Edge* e) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001868 TESS_LOG("\nextracting boundary\n");
Chris Dalton7cf3add2021-01-11 18:33:28 -07001869 bool down = apply_fill_type(fPath.getFillType(), e->fWinding);
Stephen White0c72ed32019-06-13 13:13:13 -04001870 Vertex* start = down ? e->fTop : e->fBottom;
1871 do {
senorblancof57372d2016-08-31 10:36:19 -07001872 e->fWinding = down ? 1 : -1;
1873 Edge* next;
Stephen Whitee260c462017-12-19 18:09:54 -05001874 e->fLine.normalize();
1875 e->fLine = e->fLine * e->fWinding;
senorblancof57372d2016-08-31 10:36:19 -07001876 boundary->append(e);
1877 if (down) {
1878 // Find outgoing edge, in clockwise order.
1879 if ((next = e->fNextEdgeAbove)) {
1880 down = false;
1881 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1882 down = true;
1883 } else if ((next = e->fPrevEdgeAbove)) {
1884 down = false;
1885 }
1886 } else {
1887 // Find outgoing edge, in counter-clockwise order.
1888 if ((next = e->fPrevEdgeBelow)) {
1889 down = true;
1890 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1891 down = false;
1892 } else if ((next = e->fNextEdgeBelow)) {
1893 down = true;
1894 }
1895 }
Chris Daltond60c9192021-01-07 18:30:08 +00001896 disconnect(e);
senorblancof57372d2016-08-31 10:36:19 -07001897 e = next;
Stephen White0c72ed32019-06-13 13:13:13 -04001898 } while (e && (down ? e->fTop : e->fBottom) != start);
senorblancof57372d2016-08-31 10:36:19 -07001899}
1900
Stephen White5ad721e2017-02-23 16:50:47 -05001901// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
senorblancof57372d2016-08-31 10:36:19 -07001902
Chris Dalton7cf3add2021-01-11 18:33:28 -07001903void GrAATriangulator::extractBoundaries(const VertexList& inMesh, VertexList* innerVertices,
Chris Dalton93c2d812021-01-11 19:51:59 -07001904 const Comparator& c) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001905 this->removeNonBoundaryEdges(inMesh);
Stephen White5ad721e2017-02-23 16:50:47 -05001906 for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
senorblancof57372d2016-08-31 10:36:19 -07001907 while (v->fFirstEdgeBelow) {
Stephen White5ad721e2017-02-23 16:50:47 -05001908 EdgeList boundary;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001909 this->extractBoundary(&boundary, v->fFirstEdgeBelow);
1910 this->simplifyBoundary(&boundary, c);
Chris Dalton93c2d812021-01-11 19:51:59 -07001911 this->strokeBoundary(&boundary, innerVertices, c);
senorblancof57372d2016-08-31 10:36:19 -07001912 }
1913 }
senorblancof57372d2016-08-31 10:36:19 -07001914}
1915
Stephen Whitebda29c02017-03-13 15:10:13 -04001916// This is a driver function that calls stages 2-5 in turn.
ethannicholase9709e82016-01-07 13:34:16 -08001917
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001918void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
Chris Dalton811dc6a2021-01-07 16:40:32 -07001919 const Comparator& c) {
Chris Dalton5045de32021-01-07 19:09:01 -07001920#if TRIANGULATOR_LOGGING
ethannicholase9709e82016-01-07 13:34:16 -08001921 for (int i = 0; i < contourCnt; ++i) {
Stephen White3a9aab92017-03-07 14:07:18 -05001922 Vertex* v = contours[i].fHead;
ethannicholase9709e82016-01-07 13:34:16 -08001923 SkASSERT(v);
Brian Salomon120e7d62019-09-11 10:29:22 -04001924 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
Stephen White3a9aab92017-03-07 14:07:18 -05001925 for (v = v->fNext; v; v = v->fNext) {
Brian Salomon120e7d62019-09-11 10:29:22 -04001926 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
ethannicholase9709e82016-01-07 13:34:16 -08001927 }
1928 }
1929#endif
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001930 this->sanitizeContours(contours, contourCnt);
1931 this->buildEdges(contours, contourCnt, mesh, c);
senorblancof57372d2016-08-31 10:36:19 -07001932}
1933
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001934void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
Stephen Whitebf6137e2017-01-04 15:43:26 -05001935 if (!vertices || !vertices->fHead) {
Stephen White2f4686f2017-01-03 16:20:01 -05001936 return;
ethannicholase9709e82016-01-07 13:34:16 -08001937 }
1938
1939 // Sort vertices in Y (secondarily in X).
Stephen White16a40cb2017-02-23 11:10:01 -05001940 if (c.fDirection == Comparator::Direction::kHorizontal) {
1941 merge_sort<sweep_lt_horiz>(vertices);
1942 } else {
1943 merge_sort<sweep_lt_vert>(vertices);
1944 }
Chris Dalton5045de32021-01-07 19:09:01 -07001945#if TRIANGULATOR_LOGGING
Stephen White2e2cb9b2017-01-09 13:11:18 -05001946 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
ethannicholase9709e82016-01-07 13:34:16 -08001947 static float gID = 0.0f;
1948 v->fID = gID++;
1949 }
1950#endif
Stephen White2f4686f2017-01-03 16:20:01 -05001951}
1952
Chris Dalton93c2d812021-01-11 19:51:59 -07001953Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001954 const SkRect& pathBounds = fPath.getBounds();
Stephen White16a40cb2017-02-23 11:10:01 -05001955 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
1956 : Comparator::Direction::kVertical);
Stephen Whitebf6137e2017-01-04 15:43:26 -05001957 VertexList mesh;
Chris Dalton57ea1fc2021-01-05 13:37:44 -07001958 this->contoursToMesh(contours, contourCnt, &mesh, c);
1959 SortMesh(&mesh, c);
1960 this->mergeCoincidentVertices(&mesh, c);
1961 if (SimplifyResult::kAbort == this->simplify(&mesh, c)) {
Chris Dalton6ccc0322020-01-29 11:38:16 -07001962 return nullptr;
1963 }
Brian Salomon120e7d62019-09-11 10:29:22 -04001964 TESS_LOG("\nsimplified mesh:\n");
Chris Dalton7cf3add2021-01-11 18:33:28 -07001965 DUMP_MESH(mesh);
Chris Dalton93c2d812021-01-11 19:51:59 -07001966 return this->tessellate(mesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001967}
1968
Chris Dalton93c2d812021-01-11 19:51:59 -07001969Poly* GrAATriangulator::tessellate(const VertexList& mesh, const Comparator& c) {
Chris Dalton7cf3add2021-01-11 18:33:28 -07001970 VertexList innerMesh;
Chris Dalton93c2d812021-01-11 19:51:59 -07001971 this->extractBoundaries(mesh, &innerMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001972 SortMesh(&innerMesh, c);
Chris Dalton93c2d812021-01-11 19:51:59 -07001973 SortMesh(&fOuterMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001974 this->mergeCoincidentVertices(&innerMesh, c);
Chris Dalton93c2d812021-01-11 19:51:59 -07001975 bool was_complex = this->mergeCoincidentVertices(&fOuterMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001976 auto result = this->simplify(&innerMesh, c);
1977 SkASSERT(SimplifyResult::kAbort != result);
1978 was_complex = (SimplifyResult::kFoundSelfIntersection == result) || was_complex;
Chris Dalton93c2d812021-01-11 19:51:59 -07001979 result = this->simplify(&fOuterMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001980 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");
Chris Dalton93c2d812021-01-11 19:51:59 -07001985 DUMP_MESH(fOuterMesh);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001986 EventComparator eventLT(EventComparator::Op::kLessThan);
1987 EventComparator eventGT(EventComparator::Op::kGreaterThan);
1988 was_complex = this->collapseOverlapRegions(&innerMesh, c, eventLT) || was_complex;
Chris Dalton93c2d812021-01-11 19:51:59 -07001989 was_complex = this->collapseOverlapRegions(&fOuterMesh, c, eventGT) || was_complex;
Chris Dalton7cf3add2021-01-11 18:33:28 -07001990 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");
Chris Dalton93c2d812021-01-11 19:51:59 -07001996 DUMP_MESH(fOuterMesh);
1997 this->connectPartners(&fOuterMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07001998 this->connectPartners(&innerMesh, c);
Chris Dalton93c2d812021-01-11 19:51:59 -07001999 sorted_merge(&innerMesh, &fOuterMesh, &aaMesh, c);
Chris Dalton7cf3add2021-01-11 18:33:28 -07002000 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);
Chris Dalton93c2d812021-01-11 19:51:59 -07002005 fOuterMesh.fHead = fOuterMesh.fTail = nullptr;
2006 return this->GrTriangulator::tessellate(aaMesh, 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");
Chris Dalton93c2d812021-01-11 19:51:59 -07002009 return this->GrTriangulator::tessellate(innerMesh, 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 Dalton93c2d812021-01-11 19:51:59 -07002014void* GrTriangulator::polysToTrianglesImpl(Poly* polys, void* data,
2015 SkPathFillType overrideFillType) {
senorblancof57372d2016-08-31 10:36:19 -07002016 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002017 if (apply_fill_type(overrideFillType, poly)) {
Chris Dalton9a4904f2021-01-07 19:10:14 -07002018 data = this->emitPoly(poly, data);
senorblancof57372d2016-08-31 10:36:19 -07002019 }
2020 }
2021 return data;
ethannicholase9709e82016-01-07 13:34:16 -08002022}
2023
Chris Dalton93c2d812021-01-11 19:51:59 -07002024Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, int contourCnt) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002025 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());
Chris Dalton93c2d812021-01-11 19:51:59 -07002031 return this->contoursToPolys(contours.get(), contourCnt);
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 Dalton93c2d812021-01-11 19:51:59 -07002068int64_t GrTriangulator::countPointsImpl(Poly* polys, SkPathFillType overrideFillType) const {
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) {
Chris Dalton93c2d812021-01-11 19:51:59 -07002071 if (apply_fill_type(overrideFillType, 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 Dalton93c2d812021-01-11 19:51:59 -07002078int64_t GrAATriangulator::countPoints(Poly* polys) const {
2079 int64_t count = this->countPointsImpl(polys, SkPathFillType::kWinding);
2080 // Count the points from the outer mesh.
2081 for (Vertex* v = fOuterMesh.fHead; v; v = v->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002082 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
Chris Dalton17dc4182020-03-25 16:18:16 -06002083 count += TRIANGULATOR_WIREFRAME ? 12 : 6;
Stephen Whitebda29c02017-03-13 15:10:13 -04002084 }
2085 }
2086 return count;
2087}
2088
Chris Dalton93c2d812021-01-11 19:51:59 -07002089void* GrAATriangulator::polysToTriangles(Poly* polys, void* data) {
2090 data = this->polysToTrianglesImpl(polys, data, SkPathFillType::kWinding);
2091 // Emit the triangles from the outer mesh.
2092 for (Vertex* v = fOuterMesh.fHead; v; v = v->fNext) {
Stephen Whitebda29c02017-03-13 15:10:13 -04002093 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
2094 Vertex* v0 = e->fTop;
2095 Vertex* v1 = e->fBottom;
2096 Vertex* v2 = e->fBottom->fPartner;
2097 Vertex* v3 = e->fTop->fPartner;
Chris Dalton93c2d812021-01-11 19:51:59 -07002098 data = this->emitTriangle(v0, v1, v2, 0/*winding*/, data);
2099 data = this->emitTriangle(v0, v2, v3, 0/*winding*/, data);
Stephen Whitebda29c02017-03-13 15:10:13 -04002100 }
2101 }
2102 return data;
2103}
2104
ethannicholase9709e82016-01-07 13:34:16 -08002105// Stage 6: Triangulate the monotone polygons into a vertex buffer.
2106
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002107int GrTriangulator::pathToTriangles(float tolerance, const SkRect& clipBounds,
Chris Dalton93c2d812021-01-11 19:51:59 -07002108 GrEagerVertexAllocator* vertexAllocator) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002109 int contourCnt = get_contour_count(fPath, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002110 if (contourCnt <= 0) {
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002111 fIsLinear = true;
ethannicholase9709e82016-01-07 13:34:16 -08002112 return 0;
2113 }
Chris Dalton93c2d812021-01-11 19:51:59 -07002114 Poly* polys = this->pathToPolys(tolerance, clipBounds, contourCnt);
2115 int64_t count64 = this->countPoints(polys);
Greg Danield5b45932018-06-07 13:15:10 -04002116 if (0 == count64 || count64 > SK_MaxS32) {
Stephen Whiteff60b172017-05-05 15:54:52 -04002117 return 0;
2118 }
Greg Danield5b45932018-06-07 13:15:10 -04002119 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002120
Chris Dalton854ee852021-01-05 15:12:59 -07002121 size_t vertexStride = sizeof(SkPoint);
2122 if (fEmitCoverage) {
2123 vertexStride += sizeof(float);
2124 }
Chris Daltond081dce2020-01-23 12:09:04 -07002125 void* verts = vertexAllocator->lock(vertexStride, count);
senorblanco6599eff2016-03-10 08:38:45 -08002126 if (!verts) {
ethannicholase9709e82016-01-07 13:34:16 -08002127 SkDebugf("Could not allocate vertices\n");
2128 return 0;
2129 }
senorblancof57372d2016-08-31 10:36:19 -07002130
Brian Salomon120e7d62019-09-11 10:29:22 -04002131 TESS_LOG("emitting %d verts\n", count);
Chris Dalton93c2d812021-01-11 19:51:59 -07002132 void* end = this->polysToTriangles(polys, verts);
Brian Osman80879d42019-01-07 16:15:27 -05002133
senorblancof57372d2016-08-31 10:36:19 -07002134 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
Chris Daltond081dce2020-01-23 12:09:04 -07002135 / vertexStride);
ethannicholase9709e82016-01-07 13:34:16 -08002136 SkASSERT(actualCount <= count);
senorblanco6599eff2016-03-10 08:38:45 -08002137 vertexAllocator->unlock(actualCount);
ethannicholase9709e82016-01-07 13:34:16 -08002138 return actualCount;
2139}
2140
Chris Dalton57ea1fc2021-01-05 13:37:44 -07002141int GrTriangulator::PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
2142 WindingVertex** verts) {
Stephen White11f65e02017-02-16 19:00:39 -05002143 int contourCnt = get_contour_count(path, tolerance);
ethannicholase9709e82016-01-07 13:34:16 -08002144 if (contourCnt <= 0) {
Chris Dalton84403d72018-02-13 21:46:17 -05002145 *verts = nullptr;
ethannicholase9709e82016-01-07 13:34:16 -08002146 return 0;
2147 }
Chris Dalton854ee852021-01-05 15:12:59 -07002148 GrTriangulator triangulator(path);
Chris Dalton93c2d812021-01-11 19:51:59 -07002149 Poly* polys = triangulator.pathToPolys(tolerance, clipBounds, contourCnt);
2150 int64_t count64 = triangulator.countPoints(polys);
Greg Danield5b45932018-06-07 13:15:10 -04002151 if (0 == count64 || count64 > SK_MaxS32) {
ethannicholase9709e82016-01-07 13:34:16 -08002152 *verts = nullptr;
2153 return 0;
2154 }
Greg Danield5b45932018-06-07 13:15:10 -04002155 int count = count64;
ethannicholase9709e82016-01-07 13:34:16 -08002156
Chris Dalton17dc4182020-03-25 16:18:16 -06002157 *verts = new WindingVertex[count];
2158 WindingVertex* vertsEnd = *verts;
ethannicholase9709e82016-01-07 13:34:16 -08002159 SkPoint* points = new SkPoint[count];
2160 SkPoint* pointsEnd = points;
2161 for (Poly* poly = polys; poly; poly = poly->fNext) {
Chris Dalton93c2d812021-01-11 19:51:59 -07002162 if (apply_fill_type(path.getFillType(), poly)) {
ethannicholase9709e82016-01-07 13:34:16 -08002163 SkPoint* start = pointsEnd;
Chris Dalton9a4904f2021-01-07 19:10:14 -07002164 pointsEnd = static_cast<SkPoint*>(triangulator.emitPoly(poly, pointsEnd));
ethannicholase9709e82016-01-07 13:34:16 -08002165 while (start != pointsEnd) {
2166 vertsEnd->fPos = *start;
2167 vertsEnd->fWinding = poly->fWinding;
2168 ++start;
2169 ++vertsEnd;
2170 }
2171 }
2172 }
2173 int actualCount = static_cast<int>(vertsEnd - *verts);
2174 SkASSERT(actualCount <= count);
2175 SkASSERT(pointsEnd - points == actualCount);
2176 delete[] points;
2177 return actualCount;
2178}