1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/GrTriangulator.h"
9
10 #include "src/gpu/GrEagerVertexAllocator.h"
11 #include "src/gpu/GrVertexWriter.h"
12 #include "src/gpu/geometry/GrPathUtils.h"
13
14 #include "src/core/SkGeometry.h"
15 #include "src/core/SkPointPriv.h"
16
17 #include <algorithm>
18
19
20 #if TRIANGULATOR_LOGGING
21 #define TESS_LOG printf
22 #define DUMP_MESH(M) (M).dump()
23 #else
24 #define TESS_LOG(...)
25 #define DUMP_MESH(M)
26 #endif
27
28 using EdgeType = GrTriangulator::EdgeType;
29 using Vertex = GrTriangulator::Vertex;
30 using VertexList = GrTriangulator::VertexList;
31 using Line = GrTriangulator::Line;
32 using Edge = GrTriangulator::Edge;
33 using EdgeList = GrTriangulator::EdgeList;
34 using Poly = GrTriangulator::Poly;
35 using MonotonePoly = GrTriangulator::MonotonePoly;
36 using Comparator = GrTriangulator::Comparator;
37
38 template <class T, T* T::*Prev, T* T::*Next>
list_insert(T * t,T * prev,T * next,T ** head,T ** tail)39 static void list_insert(T* t, T* prev, T* next, T** head, T** tail) {
40 t->*Prev = prev;
41 t->*Next = next;
42 if (prev) {
43 prev->*Next = t;
44 } else if (head) {
45 *head = t;
46 }
47 if (next) {
48 next->*Prev = t;
49 } else if (tail) {
50 *tail = t;
51 }
52 }
53
54 template <class T, T* T::*Prev, T* T::*Next>
list_remove(T * t,T ** head,T ** tail)55 static void list_remove(T* t, T** head, T** tail) {
56 if (t->*Prev) {
57 t->*Prev->*Next = t->*Next;
58 } else if (head) {
59 *head = t->*Next;
60 }
61 if (t->*Next) {
62 t->*Next->*Prev = t->*Prev;
63 } else if (tail) {
64 *tail = t->*Prev;
65 }
66 t->*Prev = t->*Next = nullptr;
67 }
68
69 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
70
sweep_lt_horiz(const SkPoint & a,const SkPoint & b)71 static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
72 return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
73 }
74
sweep_lt_vert(const SkPoint & a,const SkPoint & b)75 static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
76 return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
77 }
78
sweep_lt(const SkPoint & a,const SkPoint & b) const79 bool GrTriangulator::Comparator::sweep_lt(const SkPoint& a, const SkPoint& b) const {
80 return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
81 }
82
emit_vertex(Vertex * v,bool emitCoverage,void * data)83 static inline void* emit_vertex(Vertex* v, bool emitCoverage, void* data) {
84 GrVertexWriter verts{data};
85 verts.write(v->fPoint);
86
87 if (emitCoverage) {
88 verts.write(GrNormalizeByteToFloat(v->fAlpha));
89 }
90
91 return verts.fPtr;
92 }
93
emit_triangle(Vertex * v0,Vertex * v1,Vertex * v2,bool emitCoverage,void * data)94 static void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, bool emitCoverage, void* data) {
95 TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
96 TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
97 TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
98 #if TESSELLATOR_WIREFRAME
99 data = emit_vertex(v0, emitCoverage, data);
100 data = emit_vertex(v1, emitCoverage, data);
101 data = emit_vertex(v1, emitCoverage, data);
102 data = emit_vertex(v2, emitCoverage, data);
103 data = emit_vertex(v2, emitCoverage, data);
104 data = emit_vertex(v0, emitCoverage, data);
105 #else
106 data = emit_vertex(v0, emitCoverage, data);
107 data = emit_vertex(v1, emitCoverage, data);
108 data = emit_vertex(v2, emitCoverage, data);
109 #endif
110 return data;
111 }
112
insert(Vertex * v,Vertex * prev,Vertex * next)113 void GrTriangulator::VertexList::insert(Vertex* v, Vertex* prev, Vertex* next) {
114 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
115 }
116
remove(Vertex * v)117 void GrTriangulator::VertexList::remove(Vertex* v) {
118 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
119 }
120
121 // Round to nearest quarter-pixel. This is used for screenspace tessellation.
122
round(SkPoint * p)123 static inline void round(SkPoint* p) {
124 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
125 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
126 }
127
double_to_clamped_scalar(double d)128 static inline SkScalar double_to_clamped_scalar(double d) {
129 // Clamps large values to what's finitely representable when cast back to a float.
130 static const double kMaxLimit = (double) SK_ScalarMax;
131 // It's not perfect, but a using a value larger than float_min helps protect from denormalized
132 // values and ill-conditions in intermediate calculations on coordinates.
133 static const double kNearZeroLimit = 16 * (double) std::numeric_limits<float>::min();
134 if (std::abs(d) < kNearZeroLimit) {
135 d = 0.f;
136 }
137 return SkDoubleToScalar(std::max(-kMaxLimit, std::min(d, kMaxLimit)));
138 }
139
intersect(const Line & other,SkPoint * point) const140 bool GrTriangulator::Line::intersect(const Line& other, SkPoint* point) const {
141 double denom = fA * other.fB - fB * other.fA;
142 if (denom == 0.0) {
143 return false;
144 }
145 double scale = 1.0 / denom;
146 point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale);
147 point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale);
148 round(point);
149 return point->isFinite();
150 }
151
intersect(const Edge & other,SkPoint * p,uint8_t * alpha) const152 bool GrTriangulator::Edge::intersect(const Edge& other, SkPoint* p, uint8_t* alpha) const {
153 TESS_LOG("intersecting %g -> %g with %g -> %g\n",
154 fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID);
155 if (fTop == other.fTop || fBottom == other.fBottom) {
156 return false;
157 }
158 double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
159 if (denom == 0.0) {
160 return false;
161 }
162 double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
163 double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
164 double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
165 double tNumer = dy * fLine.fB + dx * fLine.fA;
166 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
167 // This saves us doing the divide below unless absolutely necessary.
168 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
169 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
170 return false;
171 }
172 double s = sNumer / denom;
173 SkASSERT(s >= 0.0 && s <= 1.0);
174 p->fX = double_to_clamped_scalar(fTop->fPoint.fX - s * fLine.fB);
175 p->fY = double_to_clamped_scalar(fTop->fPoint.fY + s * fLine.fA);
176 if (alpha) {
177 if (fType == EdgeType::kInner || other.fType == EdgeType::kInner) {
178 // If the intersection is on any interior edge, it needs to stay fully opaque or later
179 // triangulation could leech transparency into the inner fill region.
180 *alpha = 255;
181 } else if (fType == EdgeType::kOuter && other.fType == EdgeType::kOuter) {
182 // Trivially, the intersection will be fully transparent since since it is by
183 // construction on the outer edge.
184 *alpha = 0;
185 } else {
186 // Could be two connectors crossing, or a connector crossing an outer edge.
187 // Take the max interpolated alpha
188 SkASSERT(fType == EdgeType::kConnector || other.fType == EdgeType::kConnector);
189 double t = tNumer / denom;
190 *alpha = std::max((1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha,
191 (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha);
192 }
193 }
194 return true;
195 }
196
insert(Edge * edge,Edge * prev,Edge * next)197 void GrTriangulator::EdgeList::insert(Edge* edge, Edge* prev, Edge* next) {
198 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
199 }
200
remove(Edge * edge)201 void GrTriangulator::EdgeList::remove(Edge* edge) {
202 TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
203 SkASSERT(this->contains(edge));
204 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
205 }
206
addEdge(Edge * edge)207 void GrTriangulator::MonotonePoly::addEdge(Edge* edge) {
208 if (fSide == kRight_Side) {
209 SkASSERT(!edge->fUsedInRightPoly);
210 list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
211 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
212 edge->fUsedInRightPoly = true;
213 } else {
214 SkASSERT(!edge->fUsedInLeftPoly);
215 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
216 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
217 edge->fUsedInLeftPoly = true;
218 }
219 }
220
emitMonotonePoly(const MonotonePoly * monotonePoly,void * data) const221 void* GrTriangulator::emitMonotonePoly(const MonotonePoly* monotonePoly, void* data) const {
222 SkASSERT(monotonePoly->fWinding != 0);
223 Edge* e = monotonePoly->fFirstEdge;
224 VertexList vertices;
225 vertices.append(e->fTop);
226 int count = 1;
227 while (e != nullptr) {
228 if (kRight_Side == monotonePoly->fSide) {
229 vertices.append(e->fBottom);
230 e = e->fRightPolyNext;
231 } else {
232 vertices.prepend(e->fBottom);
233 e = e->fLeftPolyNext;
234 }
235 count++;
236 }
237 Vertex* first = vertices.fHead;
238 Vertex* v = first->fNext;
239 while (v != vertices.fTail) {
240 SkASSERT(v && v->fPrev && v->fNext);
241 Vertex* prev = v->fPrev;
242 Vertex* curr = v;
243 Vertex* next = v->fNext;
244 if (count == 3) {
245 return this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
246 }
247 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
248 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
249 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
250 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
251 if (ax * by - ay * bx >= 0.0) {
252 data = this->emitTriangle(prev, curr, next, monotonePoly->fWinding, data);
253 v->fPrev->fNext = v->fNext;
254 v->fNext->fPrev = v->fPrev;
255 count--;
256 if (v->fPrev == first) {
257 v = v->fNext;
258 } else {
259 v = v->fPrev;
260 }
261 } else {
262 v = v->fNext;
263 }
264 }
265 return data;
266 }
267
emitTriangle(Vertex * prev,Vertex * curr,Vertex * next,int winding,void * data) const268 void* GrTriangulator::emitTriangle(Vertex* prev, Vertex* curr, Vertex* next, int winding,
269 void* data) const {
270 if (winding > 0) {
271 // Ensure our triangles always wind in the same direction as if the path had been
272 // triangulated as a simple fan (a la red book).
273 std::swap(prev, next);
274 }
275 if (fCollectBreadcrumbTriangles && abs(winding) > 1 &&
276 fPath.getFillType() == SkPathFillType::kWinding) {
277 // The first winding count will come from the actual triangle we emit. The remaining counts
278 // come from the breadcrumb triangle.
279 fBreadcrumbList.append(fAlloc, prev->fPoint, curr->fPoint, next->fPoint, abs(winding) - 1);
280 }
281 return emit_triangle(prev, curr, next, fEmitCoverage, data);
282 }
283
Poly(Vertex * v,int winding)284 GrTriangulator::Poly::Poly(Vertex* v, int winding)
285 : fFirstVertex(v)
286 , fWinding(winding)
287 , fHead(nullptr)
288 , fTail(nullptr)
289 , fNext(nullptr)
290 , fPartner(nullptr)
291 , fCount(0)
292 {
293 #if TRIANGULATOR_LOGGING
294 static int gID = 0;
295 fID = gID++;
296 TESS_LOG("*** created Poly %d\n", fID);
297 #endif
298 }
299
addEdge(Edge * e,Side side,SkArenaAlloc * alloc)300 Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, SkArenaAlloc* alloc) {
301 TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n",
302 e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
303 Poly* partner = fPartner;
304 Poly* poly = this;
305 if (side == kRight_Side) {
306 if (e->fUsedInRightPoly) {
307 return this;
308 }
309 } else {
310 if (e->fUsedInLeftPoly) {
311 return this;
312 }
313 }
314 if (partner) {
315 fPartner = partner->fPartner = nullptr;
316 }
317 if (!fTail) {
318 fHead = fTail = alloc->make<MonotonePoly>(e, side, fWinding);
319 fCount += 2;
320 } else if (e->fBottom == fTail->fLastEdge->fBottom) {
321 return poly;
322 } else if (side == fTail->fSide) {
323 fTail->addEdge(e);
324 fCount++;
325 } else {
326 e = alloc->make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner);
327 fTail->addEdge(e);
328 fCount++;
329 if (partner) {
330 partner->addEdge(e, side, alloc);
331 poly = partner;
332 } else {
333 MonotonePoly* m = alloc->make<MonotonePoly>(e, side, fWinding);
334 m->fPrev = fTail;
335 fTail->fNext = m;
336 fTail = m;
337 }
338 }
339 return poly;
340 }
emitPoly(const Poly * poly,void * data) const341 void* GrTriangulator::emitPoly(const Poly* poly, void *data) const {
342 if (poly->fCount < 3) {
343 return data;
344 }
345 TESS_LOG("emit() %d, size %d\n", poly->fID, poly->fCount);
346 for (MonotonePoly* m = poly->fHead; m != nullptr; m = m->fNext) {
347 data = this->emitMonotonePoly(m, data);
348 }
349 return data;
350 }
351
coincident(const SkPoint & a,const SkPoint & b)352 static bool coincident(const SkPoint& a, const SkPoint& b) {
353 return a == b;
354 }
355
makePoly(Poly ** head,Vertex * v,int winding) const356 Poly* GrTriangulator::makePoly(Poly** head, Vertex* v, int winding) const {
357 Poly* poly = fAlloc->make<Poly>(v, winding);
358 poly->fNext = *head;
359 *head = poly;
360 return poly;
361 }
362
appendPointToContour(const SkPoint & p,VertexList * contour) const363 void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) const {
364 Vertex* v = fAlloc->make<Vertex>(p, 255);
365 #if TRIANGULATOR_LOGGING
366 static float gID = 0.0f;
367 v->fID = gID++;
368 #endif
369 contour->append(v);
370 }
371
quad_error_at(const SkPoint pts[3],SkScalar t,SkScalar u)372 static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) {
373 SkQuadCoeff quad(pts);
374 SkPoint p0 = to_point(quad.eval(t - 0.5f * u));
375 SkPoint mid = to_point(quad.eval(t));
376 SkPoint p1 = to_point(quad.eval(t + 0.5f * u));
377 if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) {
378 return 0;
379 }
380 return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1);
381 }
382
appendQuadraticToContour(const SkPoint pts[3],SkScalar toleranceSqd,VertexList * contour) const383 void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd,
384 VertexList* contour) const {
385 SkQuadCoeff quad(pts);
386 Sk2s aa = quad.fA * quad.fA;
387 SkScalar denom = 2.0f * (aa[0] + aa[1]);
388 Sk2s ab = quad.fA * quad.fB;
389 SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f;
390 int nPoints = 1;
391 SkScalar u = 1.0f;
392 // Test possible subdivision values only at the point of maximum curvature.
393 // If it passes the flatness metric there, it'll pass everywhere.
394 while (nPoints < GrPathUtils::kMaxPointsPerCurve) {
395 u = 1.0f / nPoints;
396 if (quad_error_at(pts, t, u) < toleranceSqd) {
397 break;
398 }
399 nPoints++;
400 }
401 for (int j = 1; j <= nPoints; j++) {
402 this->appendPointToContour(to_point(quad.eval(j * u)), contour);
403 }
404 }
405
generateCubicPoints(const SkPoint & p0,const SkPoint & p1,const SkPoint & p2,const SkPoint & p3,SkScalar tolSqd,VertexList * contour,int pointsLeft) const406 void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
407 const SkPoint& p3, SkScalar tolSqd, VertexList* contour,
408 int pointsLeft) const {
409 SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3);
410 SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3);
411 if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
412 !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
413 this->appendPointToContour(p3, contour);
414 return;
415 }
416 const SkPoint q[] = {
417 { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
418 { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
419 { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
420 };
421 const SkPoint r[] = {
422 { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
423 { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
424 };
425 const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
426 pointsLeft >>= 1;
427 this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft);
428 this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft);
429 }
430
431 // Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
432
pathToContours(float tolerance,const SkRect & clipBounds,VertexList * contours,bool * isLinear) const433 void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds,
434 VertexList* contours, bool* isLinear) const {
435 SkScalar toleranceSqd = tolerance * tolerance;
436 SkPoint pts[4];
437 *isLinear = true;
438 VertexList* contour = contours;
439 SkPath::Iter iter(fPath, false);
440 if (fPath.isInverseFillType()) {
441 SkPoint quad[4];
442 clipBounds.toQuad(quad);
443 for (int i = 3; i >= 0; i--) {
444 this->appendPointToContour(quad[i], contours);
445 }
446 contour++;
447 }
448 SkAutoConicToQuads converter;
449 SkPath::Verb verb;
450 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
451 switch (verb) {
452 case SkPath::kConic_Verb: {
453 *isLinear = false;
454 if (toleranceSqd == 0) {
455 this->appendPointToContour(pts[2], contour);
456 break;
457 }
458 SkScalar weight = iter.conicWeight();
459 const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
460 for (int i = 0; i < converter.countQuads(); ++i) {
461 this->appendQuadraticToContour(quadPts, toleranceSqd, contour);
462 quadPts += 2;
463 }
464 break;
465 }
466 case SkPath::kMove_Verb:
467 if (contour->fHead) {
468 contour++;
469 }
470 this->appendPointToContour(pts[0], contour);
471 break;
472 case SkPath::kLine_Verb: {
473 this->appendPointToContour(pts[1], contour);
474 break;
475 }
476 case SkPath::kQuad_Verb: {
477 *isLinear = false;
478 if (toleranceSqd == 0) {
479 this->appendPointToContour(pts[2], contour);
480 break;
481 }
482 this->appendQuadraticToContour(pts, toleranceSqd, contour);
483 break;
484 }
485 case SkPath::kCubic_Verb: {
486 *isLinear = false;
487 if (toleranceSqd == 0) {
488 this->appendPointToContour(pts[3], contour);
489 break;
490 }
491 int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
492 this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour,
493 pointsLeft);
494 break;
495 }
496 case SkPath::kClose_Verb:
497 case SkPath::kDone_Verb:
498 break;
499 }
500 }
501 }
502
apply_fill_type(SkPathFillType fillType,int winding)503 static inline bool apply_fill_type(SkPathFillType fillType, int winding) {
504 switch (fillType) {
505 case SkPathFillType::kWinding:
506 return winding != 0;
507 case SkPathFillType::kEvenOdd:
508 return (winding & 1) != 0;
509 case SkPathFillType::kInverseWinding:
510 return winding == 1;
511 case SkPathFillType::kInverseEvenOdd:
512 return (winding & 1) == 1;
513 default:
514 SkASSERT(false);
515 return false;
516 }
517 }
518
applyFillType(int winding) const519 bool GrTriangulator::applyFillType(int winding) const {
520 return apply_fill_type(fPath.getFillType(), winding);
521 }
522
apply_fill_type(SkPathFillType fillType,Poly * poly)523 static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) {
524 return poly && apply_fill_type(fillType, poly->fWinding);
525 }
526
makeEdge(Vertex * prev,Vertex * next,EdgeType type,const Comparator & c) const527 Edge* GrTriangulator::makeEdge(Vertex* prev, Vertex* next, EdgeType type,
528 const Comparator& c) const {
529 SkASSERT(prev->fPoint != next->fPoint);
530 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
531 Vertex* top = winding < 0 ? next : prev;
532 Vertex* bottom = winding < 0 ? prev : next;
533 return fAlloc->make<Edge>(top, bottom, winding, type);
534 }
535
insert(Edge * edge,Edge * prev)536 void EdgeList::insert(Edge* edge, Edge* prev) {
537 TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
538 SkASSERT(!this->contains(edge));
539 Edge* next = prev ? prev->fRight : fHead;
540 this->insert(edge, prev, next);
541 }
542
FindEnclosingEdges(Vertex * v,EdgeList * edges,Edge ** left,Edge ** right)543 void GrTriangulator::FindEnclosingEdges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
544 if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
545 *left = v->fFirstEdgeAbove->fLeft;
546 *right = v->fLastEdgeAbove->fRight;
547 return;
548 }
549 Edge* next = nullptr;
550 Edge* prev;
551 for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
552 if (prev->isLeftOf(v)) {
553 break;
554 }
555 next = prev;
556 }
557 *left = prev;
558 *right = next;
559 }
560
insertAbove(Vertex * v,const Comparator & c)561 void GrTriangulator::Edge::insertAbove(Vertex* v, const Comparator& c) {
562 if (fTop->fPoint == fBottom->fPoint ||
563 c.sweep_lt(fBottom->fPoint, fTop->fPoint)) {
564 return;
565 }
566 TESS_LOG("insert edge (%g -> %g) above vertex %g\n", fTop->fID, fBottom->fID, v->fID);
567 Edge* prev = nullptr;
568 Edge* next;
569 for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
570 if (next->isRightOf(fTop)) {
571 break;
572 }
573 prev = next;
574 }
575 list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
576 this, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
577 }
578
insertBelow(Vertex * v,const Comparator & c)579 void GrTriangulator::Edge::insertBelow(Vertex* v, const Comparator& c) {
580 if (fTop->fPoint == fBottom->fPoint ||
581 c.sweep_lt(fBottom->fPoint, fTop->fPoint)) {
582 return;
583 }
584 TESS_LOG("insert edge (%g -> %g) below vertex %g\n", fTop->fID, fBottom->fID, v->fID);
585 Edge* prev = nullptr;
586 Edge* next;
587 for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
588 if (next->isRightOf(fBottom)) {
589 break;
590 }
591 prev = next;
592 }
593 list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
594 this, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
595 }
596
remove_edge_above(Edge * edge)597 static void remove_edge_above(Edge* edge) {
598 SkASSERT(edge->fTop && edge->fBottom);
599 TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
600 edge->fBottom->fID);
601 list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
602 edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
603 }
604
remove_edge_below(Edge * edge)605 static void remove_edge_below(Edge* edge) {
606 SkASSERT(edge->fTop && edge->fBottom);
607 TESS_LOG("removing edge (%g -> %g) below vertex %g\n",
608 edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID);
609 list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
610 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
611 }
612
disconnect()613 void GrTriangulator::Edge::disconnect() {
614 remove_edge_above(this);
615 remove_edge_below(this);
616 }
617
rewind(EdgeList * activeEdges,Vertex ** current,Vertex * dst,const Comparator & c)618 static void rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) {
619 if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) {
620 return;
621 }
622 Vertex* v = *current;
623 TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID);
624 while (v != dst) {
625 v = v->fPrev;
626 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
627 activeEdges->remove(e);
628 }
629 Edge* leftEdge = v->fLeftEnclosingEdge;
630 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
631 activeEdges->insert(e, leftEdge);
632 leftEdge = e;
633 Vertex* top = e->fTop;
634 if (c.sweep_lt(top->fPoint, dst->fPoint) &&
635 ((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(e->fTop)) ||
636 (top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(e->fTop)))) {
637 dst = top;
638 }
639 }
640 }
641 *current = v;
642 }
643
rewind_if_necessary(Edge * edge,EdgeList * activeEdges,Vertex ** current,const Comparator & c)644 static void rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current,
645 const Comparator& c) {
646 if (!activeEdges || !current) {
647 return;
648 }
649 Vertex* top = edge->fTop;
650 Vertex* bottom = edge->fBottom;
651 if (edge->fLeft) {
652 Vertex* leftTop = edge->fLeft->fTop;
653 Vertex* leftBottom = edge->fLeft->fBottom;
654 if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
655 rewind(activeEdges, current, leftTop, c);
656 } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
657 rewind(activeEdges, current, top, c);
658 } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
659 !edge->fLeft->isLeftOf(bottom)) {
660 rewind(activeEdges, current, leftTop, c);
661 } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
662 rewind(activeEdges, current, top, c);
663 }
664 }
665 if (edge->fRight) {
666 Vertex* rightTop = edge->fRight->fTop;
667 Vertex* rightBottom = edge->fRight->fBottom;
668 if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
669 rewind(activeEdges, current, rightTop, c);
670 } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
671 rewind(activeEdges, current, top, c);
672 } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
673 !edge->fRight->isRightOf(bottom)) {
674 rewind(activeEdges, current, rightTop, c);
675 } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
676 !edge->isLeftOf(rightBottom)) {
677 rewind(activeEdges, current, top, c);
678 }
679 }
680 }
681
setTop(Edge * edge,Vertex * v,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const682 void GrTriangulator::setTop(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
683 const Comparator& c) const {
684 remove_edge_below(edge);
685 if (fCollectBreadcrumbTriangles) {
686 fBreadcrumbList.append(fAlloc, edge->fTop->fPoint, edge->fBottom->fPoint, v->fPoint,
687 edge->fWinding);
688 }
689 edge->fTop = v;
690 edge->recompute();
691 edge->insertBelow(v, c);
692 rewind_if_necessary(edge, activeEdges, current, c);
693 this->mergeCollinearEdges(edge, activeEdges, current, c);
694 }
695
setBottom(Edge * edge,Vertex * v,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const696 void GrTriangulator::setBottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
697 const Comparator& c) const {
698 remove_edge_above(edge);
699 if (fCollectBreadcrumbTriangles) {
700 fBreadcrumbList.append(fAlloc, edge->fTop->fPoint, edge->fBottom->fPoint, v->fPoint,
701 edge->fWinding);
702 }
703 edge->fBottom = v;
704 edge->recompute();
705 edge->insertAbove(v, c);
706 rewind_if_necessary(edge, activeEdges, current, c);
707 this->mergeCollinearEdges(edge, activeEdges, current, c);
708 }
709
mergeEdgesAbove(Edge * edge,Edge * other,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const710 void GrTriangulator::mergeEdgesAbove(Edge* edge, Edge* other, EdgeList* activeEdges,
711 Vertex** current, const Comparator& c) const {
712 if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
713 TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
714 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
715 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
716 rewind(activeEdges, current, edge->fTop, c);
717 other->fWinding += edge->fWinding;
718 edge->disconnect();
719 edge->fTop = edge->fBottom = nullptr;
720 } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
721 rewind(activeEdges, current, edge->fTop, c);
722 other->fWinding += edge->fWinding;
723 this->setBottom(edge, other->fTop, activeEdges, current, c);
724 } else {
725 rewind(activeEdges, current, other->fTop, c);
726 edge->fWinding += other->fWinding;
727 this->setBottom(other, edge->fTop, activeEdges, current, c);
728 }
729 }
730
mergeEdgesBelow(Edge * edge,Edge * other,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const731 void GrTriangulator::mergeEdgesBelow(Edge* edge, Edge* other, EdgeList* activeEdges,
732 Vertex** current, const Comparator& c) const {
733 if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
734 TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
735 edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
736 edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
737 rewind(activeEdges, current, edge->fTop, c);
738 other->fWinding += edge->fWinding;
739 edge->disconnect();
740 edge->fTop = edge->fBottom = nullptr;
741 } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
742 rewind(activeEdges, current, other->fTop, c);
743 edge->fWinding += other->fWinding;
744 this->setTop(other, edge->fBottom, activeEdges, current, c);
745 } else {
746 rewind(activeEdges, current, edge->fTop, c);
747 other->fWinding += edge->fWinding;
748 this->setTop(edge, other->fBottom, activeEdges, current, c);
749 }
750 }
751
top_collinear(Edge * left,Edge * right)752 static bool top_collinear(Edge* left, Edge* right) {
753 if (!left || !right) {
754 return false;
755 }
756 return left->fTop->fPoint == right->fTop->fPoint ||
757 !left->isLeftOf(right->fTop) || !right->isRightOf(left->fTop);
758 }
759
bottom_collinear(Edge * left,Edge * right)760 static bool bottom_collinear(Edge* left, Edge* right) {
761 if (!left || !right) {
762 return false;
763 }
764 return left->fBottom->fPoint == right->fBottom->fPoint ||
765 !left->isLeftOf(right->fBottom) || !right->isRightOf(left->fBottom);
766 }
767
mergeCollinearEdges(Edge * edge,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const768 void GrTriangulator::mergeCollinearEdges(Edge* edge, EdgeList* activeEdges, Vertex** current,
769 const Comparator& c) const {
770 for (;;) {
771 if (top_collinear(edge->fPrevEdgeAbove, edge)) {
772 this->mergeEdgesAbove(edge->fPrevEdgeAbove, edge, activeEdges, current, c);
773 } else if (top_collinear(edge, edge->fNextEdgeAbove)) {
774 this->mergeEdgesAbove(edge->fNextEdgeAbove, edge, activeEdges, current, c);
775 } else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) {
776 this->mergeEdgesBelow(edge->fPrevEdgeBelow, edge, activeEdges, current, c);
777 } else if (bottom_collinear(edge, edge->fNextEdgeBelow)) {
778 this->mergeEdgesBelow(edge->fNextEdgeBelow, edge, activeEdges, current, c);
779 } else {
780 break;
781 }
782 }
783 SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge));
784 SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove));
785 SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge));
786 SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow));
787 }
788
splitEdge(Edge * edge,Vertex * v,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const789 bool GrTriangulator::splitEdge(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current,
790 const Comparator& c) const {
791 if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) {
792 return false;
793 }
794 TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
795 edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY);
796 Vertex* top;
797 Vertex* bottom;
798 int winding = edge->fWinding;
799 if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
800 top = v;
801 bottom = edge->fTop;
802 this->setTop(edge, v, activeEdges, current, c);
803 } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
804 top = edge->fBottom;
805 bottom = v;
806 this->setBottom(edge, v, activeEdges, current, c);
807 } else {
808 top = v;
809 bottom = edge->fBottom;
810 this->setBottom(edge, v, activeEdges, current, c);
811 }
812 Edge* newEdge = fAlloc->make<Edge>(top, bottom, winding, edge->fType);
813 newEdge->insertBelow(top, c);
814 newEdge->insertAbove(bottom, c);
815 this->mergeCollinearEdges(newEdge, activeEdges, current, c);
816 return true;
817 }
818
intersectEdgePair(Edge * left,Edge * right,EdgeList * activeEdges,Vertex ** current,const Comparator & c) const819 bool GrTriangulator::intersectEdgePair(Edge* left, Edge* right, EdgeList* activeEdges,
820 Vertex** current, const Comparator& c) const {
821 if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) {
822 return false;
823 }
824 if (left->fTop == right->fTop || left->fBottom == right->fBottom) {
825 return false;
826 }
827 if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
828 if (!left->isLeftOf(right->fTop)) {
829 rewind(activeEdges, current, right->fTop, c);
830 return this->splitEdge(left, right->fTop, activeEdges, current, c);
831 }
832 } else {
833 if (!right->isRightOf(left->fTop)) {
834 rewind(activeEdges, current, left->fTop, c);
835 return this->splitEdge(right, left->fTop, activeEdges, current, c);
836 }
837 }
838 if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
839 if (!left->isLeftOf(right->fBottom)) {
840 rewind(activeEdges, current, right->fBottom, c);
841 return this->splitEdge(left, right->fBottom, activeEdges, current, c);
842 }
843 } else {
844 if (!right->isRightOf(left->fBottom)) {
845 rewind(activeEdges, current, left->fBottom, c);
846 return this->splitEdge(right, left->fBottom, activeEdges, current, c);
847 }
848 }
849 return false;
850 }
851
makeConnectingEdge(Vertex * prev,Vertex * next,EdgeType type,const Comparator & c,int windingScale) const852 Edge* GrTriangulator::makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType type,
853 const Comparator& c, int windingScale) const {
854 if (!prev || !next || prev->fPoint == next->fPoint) {
855 return nullptr;
856 }
857 Edge* edge = this->makeEdge(prev, next, type, c);
858 edge->insertBelow(edge->fTop, c);
859 edge->insertAbove(edge->fBottom, c);
860 edge->fWinding *= windingScale;
861 this->mergeCollinearEdges(edge, nullptr, nullptr, c);
862 return edge;
863 }
864
mergeVertices(Vertex * src,Vertex * dst,VertexList * mesh,const Comparator & c) const865 void GrTriangulator::mergeVertices(Vertex* src, Vertex* dst, VertexList* mesh,
866 const Comparator& c) const {
867 TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n",
868 src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID);
869 dst->fAlpha = std::max(src->fAlpha, dst->fAlpha);
870 if (src->fPartner) {
871 src->fPartner->fPartner = dst;
872 }
873 while (Edge* edge = src->fFirstEdgeAbove) {
874 this->setBottom(edge, dst, nullptr, nullptr, c);
875 }
876 while (Edge* edge = src->fFirstEdgeBelow) {
877 this->setTop(edge, dst, nullptr, nullptr, c);
878 }
879 mesh->remove(src);
880 dst->fSynthetic = true;
881 }
882
makeSortedVertex(const SkPoint & p,uint8_t alpha,VertexList * mesh,Vertex * reference,const Comparator & c) const883 Vertex* GrTriangulator::makeSortedVertex(const SkPoint& p, uint8_t alpha, VertexList* mesh,
884 Vertex* reference, const Comparator& c) const {
885 Vertex* prevV = reference;
886 while (prevV && c.sweep_lt(p, prevV->fPoint)) {
887 prevV = prevV->fPrev;
888 }
889 Vertex* nextV = prevV ? prevV->fNext : mesh->fHead;
890 while (nextV && c.sweep_lt(nextV->fPoint, p)) {
891 prevV = nextV;
892 nextV = nextV->fNext;
893 }
894 Vertex* v;
895 if (prevV && coincident(prevV->fPoint, p)) {
896 v = prevV;
897 } else if (nextV && coincident(nextV->fPoint, p)) {
898 v = nextV;
899 } else {
900 v = fAlloc->make<Vertex>(p, alpha);
901 #if TRIANGULATOR_LOGGING
902 if (!prevV) {
903 v->fID = mesh->fHead->fID - 1.0f;
904 } else if (!nextV) {
905 v->fID = mesh->fTail->fID + 1.0f;
906 } else {
907 v->fID = (prevV->fID + nextV->fID) * 0.5f;
908 }
909 #endif
910 mesh->insert(v, prevV, nextV);
911 }
912 return v;
913 }
914
915 // If an edge's top and bottom points differ only by 1/2 machine epsilon in the primary
916 // sort criterion, it may not be possible to split correctly, since there is no point which is
917 // below the top and above the bottom. This function detects that case.
nearly_flat(const Comparator & c,Edge * edge)918 static bool nearly_flat(const Comparator& c, Edge* edge) {
919 SkPoint diff = edge->fBottom->fPoint - edge->fTop->fPoint;
920 float primaryDiff = c.fDirection == Comparator::Direction::kHorizontal ? diff.fX : diff.fY;
921 return fabs(primaryDiff) <= std::numeric_limits<float>::epsilon() && primaryDiff != 0.0f;
922 }
923
clamp(SkPoint p,SkPoint min,SkPoint max,const Comparator & c)924 static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) {
925 if (c.sweep_lt(p, min)) {
926 return min;
927 } else if (c.sweep_lt(max, p)) {
928 return max;
929 } else {
930 return p;
931 }
932 }
933
computeBisector(Edge * edge1,Edge * edge2,Vertex * v) const934 void GrTriangulator::computeBisector(Edge* edge1, Edge* edge2, Vertex* v) const {
935 SkASSERT(fEmitCoverage); // Edge-AA only!
936 Line line1 = edge1->fLine;
937 Line line2 = edge2->fLine;
938 line1.normalize();
939 line2.normalize();
940 double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB;
941 if (cosAngle > 0.999) {
942 return;
943 }
944 line1.fC += edge1->fWinding > 0 ? -1 : 1;
945 line2.fC += edge2->fWinding > 0 ? -1 : 1;
946 SkPoint p;
947 if (line1.intersect(line2, &p)) {
948 uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0;
949 v->fPartner = fAlloc->make<Vertex>(p, alpha);
950 TESS_LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID);
951 }
952 }
953
checkForIntersection(Edge * left,Edge * right,EdgeList * activeEdges,Vertex ** current,VertexList * mesh,const Comparator & c) const954 bool GrTriangulator::checkForIntersection(Edge* left, Edge* right, EdgeList* activeEdges,
955 Vertex** current, VertexList* mesh,
956 const Comparator& c) const {
957 if (!left || !right) {
958 return false;
959 }
960 SkPoint p;
961 uint8_t alpha;
962 if (left->intersect(*right, &p, &alpha) && p.isFinite()) {
963 Vertex* v;
964 TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
965 Vertex* top = *current;
966 // If the intersection point is above the current vertex, rewind to the vertex above the
967 // intersection.
968 while (top && c.sweep_lt(p, top->fPoint)) {
969 top = top->fPrev;
970 }
971 bool leftFlat = nearly_flat(c, left);
972 bool rightFlat = nearly_flat(c, right);
973 if (leftFlat && rightFlat) {
974 return false;
975 }
976 if (!leftFlat) {
977 p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c);
978 }
979 if (!rightFlat) {
980 p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c);
981 }
982 if (coincident(p, left->fTop->fPoint)) {
983 v = left->fTop;
984 } else if (coincident(p, left->fBottom->fPoint)) {
985 v = left->fBottom;
986 } else if (coincident(p, right->fTop->fPoint)) {
987 v = right->fTop;
988 } else if (coincident(p, right->fBottom->fPoint)) {
989 v = right->fBottom;
990 } else {
991 v = this->makeSortedVertex(p, alpha, mesh, top, c);
992 if (left->fTop->fPartner) {
993 SkASSERT(fEmitCoverage); // Edge-AA only!
994 v->fSynthetic = true;
995 this->computeBisector(left, right, v);
996 }
997 }
998 rewind(activeEdges, current, top ? top : v, c);
999 this->splitEdge(left, v, activeEdges, current, c);
1000 this->splitEdge(right, v, activeEdges, current, c);
1001 v->fAlpha = std::max(v->fAlpha, alpha);
1002 return true;
1003 }
1004 return this->intersectEdgePair(left, right, activeEdges, current, c);
1005 }
1006
sanitizeContours(VertexList * contours,int contourCnt) const1007 void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) const {
1008 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1009 SkASSERT(contour->fHead);
1010 Vertex* prev = contour->fTail;
1011 prev->fPoint.fX = double_to_clamped_scalar((double) prev->fPoint.fX);
1012 prev->fPoint.fY = double_to_clamped_scalar((double) prev->fPoint.fY);
1013 if (fRoundVerticesToQuarterPixel) {
1014 round(&prev->fPoint);
1015 }
1016 for (Vertex* v = contour->fHead; v;) {
1017 v->fPoint.fX = double_to_clamped_scalar((double) v->fPoint.fX);
1018 v->fPoint.fY = double_to_clamped_scalar((double) v->fPoint.fY);
1019 if (fRoundVerticesToQuarterPixel) {
1020 round(&v->fPoint);
1021 }
1022 Vertex* next = v->fNext;
1023 Vertex* nextWrap = next ? next : contour->fHead;
1024 if (coincident(prev->fPoint, v->fPoint)) {
1025 TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1026 contour->remove(v);
1027 } else if (!v->fPoint.isFinite()) {
1028 TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY);
1029 contour->remove(v);
1030 } else if (!fPreserveCollinearVertices &&
1031 Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) {
1032 TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY);
1033 contour->remove(v);
1034 } else {
1035 prev = v;
1036 }
1037 v = next;
1038 }
1039 }
1040 }
1041
mergeCoincidentVertices(VertexList * mesh,const Comparator & c) const1042 bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) const {
1043 if (!mesh->fHead) {
1044 return false;
1045 }
1046 bool merged = false;
1047 for (Vertex* v = mesh->fHead->fNext; v;) {
1048 Vertex* next = v->fNext;
1049 if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1050 v->fPoint = v->fPrev->fPoint;
1051 }
1052 if (coincident(v->fPrev->fPoint, v->fPoint)) {
1053 this->mergeVertices(v, v->fPrev, mesh, c);
1054 merged = true;
1055 }
1056 v = next;
1057 }
1058 return merged;
1059 }
1060
1061 // Stage 2: convert the contours to a mesh of edges connecting the vertices.
1062
buildEdges(VertexList * contours,int contourCnt,VertexList * mesh,const Comparator & c) const1063 void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh,
1064 const Comparator& c) const {
1065 for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) {
1066 Vertex* prev = contour->fTail;
1067 for (Vertex* v = contour->fHead; v;) {
1068 Vertex* next = v->fNext;
1069 this->makeConnectingEdge(prev, v, EdgeType::kInner, c);
1070 mesh->append(v);
1071 prev = v;
1072 v = next;
1073 }
1074 }
1075 }
1076
1077 template <CompareFunc sweep_lt>
sorted_merge(VertexList * front,VertexList * back,VertexList * result)1078 static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) {
1079 Vertex* a = front->fHead;
1080 Vertex* b = back->fHead;
1081 while (a && b) {
1082 if (sweep_lt(a->fPoint, b->fPoint)) {
1083 front->remove(a);
1084 result->append(a);
1085 a = front->fHead;
1086 } else {
1087 back->remove(b);
1088 result->append(b);
1089 b = back->fHead;
1090 }
1091 }
1092 result->append(*front);
1093 result->append(*back);
1094 }
1095
SortedMerge(VertexList * front,VertexList * back,VertexList * result,const Comparator & c)1096 void GrTriangulator::SortedMerge(VertexList* front, VertexList* back, VertexList* result,
1097 const Comparator& c) {
1098 if (c.fDirection == Comparator::Direction::kHorizontal) {
1099 sorted_merge<sweep_lt_horiz>(front, back, result);
1100 } else {
1101 sorted_merge<sweep_lt_vert>(front, back, result);
1102 }
1103 #if TRIANGULATOR_LOGGING
1104 float id = 0.0f;
1105 for (Vertex* v = result->fHead; v; v = v->fNext) {
1106 v->fID = id++;
1107 }
1108 #endif
1109 }
1110
1111 // Stage 3: sort the vertices by increasing sweep direction.
1112
1113 template <CompareFunc sweep_lt>
merge_sort(VertexList * vertices)1114 static void merge_sort(VertexList* vertices) {
1115 Vertex* slow = vertices->fHead;
1116 if (!slow) {
1117 return;
1118 }
1119 Vertex* fast = slow->fNext;
1120 if (!fast) {
1121 return;
1122 }
1123 do {
1124 fast = fast->fNext;
1125 if (fast) {
1126 fast = fast->fNext;
1127 slow = slow->fNext;
1128 }
1129 } while (fast);
1130 VertexList front(vertices->fHead, slow);
1131 VertexList back(slow->fNext, vertices->fTail);
1132 front.fTail->fNext = back.fHead->fPrev = nullptr;
1133
1134 merge_sort<sweep_lt>(&front);
1135 merge_sort<sweep_lt>(&back);
1136
1137 vertices->fHead = vertices->fTail = nullptr;
1138 sorted_merge<sweep_lt>(&front, &back, vertices);
1139 }
1140
1141 #if TRIANGULATOR_LOGGING
dump() const1142 void VertexList::dump() const {
1143 for (Vertex* v = fHead; v; v = v->fNext) {
1144 TESS_LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1145 if (Vertex* p = v->fPartner) {
1146 TESS_LOG(", partner %g (%g, %g) alpha %d\n",
1147 p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha);
1148 } else {
1149 TESS_LOG(", null partner\n");
1150 }
1151 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1152 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1153 }
1154 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1155 TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding);
1156 }
1157 }
1158 }
1159 #endif
1160
1161 #ifdef SK_DEBUG
validate_edge_pair(Edge * left,Edge * right,const Comparator & c)1162 static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) {
1163 if (!left || !right) {
1164 return;
1165 }
1166 if (left->fTop == right->fTop) {
1167 SkASSERT(left->isLeftOf(right->fBottom));
1168 SkASSERT(right->isRightOf(left->fBottom));
1169 } else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) {
1170 SkASSERT(left->isLeftOf(right->fTop));
1171 } else {
1172 SkASSERT(right->isRightOf(left->fTop));
1173 }
1174 if (left->fBottom == right->fBottom) {
1175 SkASSERT(left->isLeftOf(right->fTop));
1176 SkASSERT(right->isRightOf(left->fTop));
1177 } else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) {
1178 SkASSERT(left->isLeftOf(right->fBottom));
1179 } else {
1180 SkASSERT(right->isRightOf(left->fBottom));
1181 }
1182 }
1183
validate_edge_list(EdgeList * edges,const Comparator & c)1184 static void validate_edge_list(EdgeList* edges, const Comparator& c) {
1185 Edge* left = edges->fHead;
1186 if (!left) {
1187 return;
1188 }
1189 for (Edge* right = left->fRight; right; right = right->fRight) {
1190 validate_edge_pair(left, right, c);
1191 left = right;
1192 }
1193 }
1194 #endif
1195
1196 // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1197
simplify(VertexList * mesh,const Comparator & c) const1198 GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh,
1199 const Comparator& c) const {
1200 TESS_LOG("simplifying complex polygons\n");
1201 EdgeList activeEdges;
1202 auto result = SimplifyResult::kAlreadySimple;
1203 for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) {
1204 if (!v->isConnected()) {
1205 continue;
1206 }
1207 Edge* leftEnclosingEdge;
1208 Edge* rightEnclosingEdge;
1209 bool restartChecks;
1210 do {
1211 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n",
1212 v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1213 restartChecks = false;
1214 FindEnclosingEdges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1215 v->fLeftEnclosingEdge = leftEnclosingEdge;
1216 v->fRightEnclosingEdge = rightEnclosingEdge;
1217 if (v->fFirstEdgeBelow) {
1218 for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
1219 if (this->checkForIntersection(
1220 leftEnclosingEdge, edge, &activeEdges, &v, mesh, c) ||
1221 this->checkForIntersection(
1222 edge, rightEnclosingEdge, &activeEdges, &v, mesh, c)) {
1223 result = SimplifyResult::kFoundSelfIntersection;
1224 restartChecks = true;
1225 break;
1226 }
1227 }
1228 } else {
1229 if (this->checkForIntersection(leftEnclosingEdge, rightEnclosingEdge, &activeEdges,
1230 &v, mesh, c)) {
1231 result = SimplifyResult::kFoundSelfIntersection;
1232 restartChecks = true;
1233 }
1234
1235 }
1236 } while (restartChecks);
1237 #ifdef SK_DEBUG
1238 validate_edge_list(&activeEdges, c);
1239 #endif
1240 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1241 activeEdges.remove(e);
1242 }
1243 Edge* leftEdge = leftEnclosingEdge;
1244 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1245 activeEdges.insert(e, leftEdge);
1246 leftEdge = e;
1247 }
1248 }
1249 SkASSERT(!activeEdges.fHead && !activeEdges.fTail);
1250 return result;
1251 }
1252
1253 // Stage 5: Tessellate the simplified mesh into monotone polygons.
1254
tessellate(const VertexList & vertices,const Comparator &) const1255 Poly* GrTriangulator::tessellate(const VertexList& vertices, const Comparator&) const {
1256 TESS_LOG("\ntessellating simple polygons\n");
1257 EdgeList activeEdges;
1258 Poly* polys = nullptr;
1259 for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1260 if (!v->isConnected()) {
1261 continue;
1262 }
1263 #if TRIANGULATOR_LOGGING
1264 TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1265 #endif
1266 Edge* leftEnclosingEdge;
1267 Edge* rightEnclosingEdge;
1268 FindEnclosingEdges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1269 Poly* leftPoly;
1270 Poly* rightPoly;
1271 if (v->fFirstEdgeAbove) {
1272 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1273 rightPoly = v->fLastEdgeAbove->fRightPoly;
1274 } else {
1275 leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1276 rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1277 }
1278 #if TRIANGULATOR_LOGGING
1279 TESS_LOG("edges above:\n");
1280 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1281 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1282 e->fTop->fID, e->fBottom->fID,
1283 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1284 e->fRightPoly ? e->fRightPoly->fID : -1);
1285 }
1286 TESS_LOG("edges below:\n");
1287 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1288 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1289 e->fTop->fID, e->fBottom->fID,
1290 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1291 e->fRightPoly ? e->fRightPoly->fID : -1);
1292 }
1293 #endif
1294 if (v->fFirstEdgeAbove) {
1295 if (leftPoly) {
1296 leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, kRight_Side, fAlloc);
1297 }
1298 if (rightPoly) {
1299 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, kLeft_Side, fAlloc);
1300 }
1301 for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1302 Edge* rightEdge = e->fNextEdgeAbove;
1303 activeEdges.remove(e);
1304 if (e->fRightPoly) {
1305 e->fRightPoly->addEdge(e, kLeft_Side, fAlloc);
1306 }
1307 if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
1308 rightEdge->fLeftPoly->addEdge(e, kRight_Side, fAlloc);
1309 }
1310 }
1311 activeEdges.remove(v->fLastEdgeAbove);
1312 if (!v->fFirstEdgeBelow) {
1313 if (leftPoly && rightPoly && leftPoly != rightPoly) {
1314 SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1315 rightPoly->fPartner = leftPoly;
1316 leftPoly->fPartner = rightPoly;
1317 }
1318 }
1319 }
1320 if (v->fFirstEdgeBelow) {
1321 if (!v->fFirstEdgeAbove) {
1322 if (leftPoly && rightPoly) {
1323 if (leftPoly == rightPoly) {
1324 if (leftPoly->fTail && leftPoly->fTail->fSide == kLeft_Side) {
1325 leftPoly = this->makePoly(&polys, leftPoly->lastVertex(),
1326 leftPoly->fWinding);
1327 leftEnclosingEdge->fRightPoly = leftPoly;
1328 } else {
1329 rightPoly = this->makePoly(&polys, rightPoly->lastVertex(),
1330 rightPoly->fWinding);
1331 rightEnclosingEdge->fLeftPoly = rightPoly;
1332 }
1333 }
1334 Edge* join = fAlloc->make<Edge>(leftPoly->lastVertex(), v, 1,
1335 EdgeType::kInner);
1336 leftPoly = leftPoly->addEdge(join, kRight_Side, fAlloc);
1337 rightPoly = rightPoly->addEdge(join, kLeft_Side, fAlloc);
1338 }
1339 }
1340 Edge* leftEdge = v->fFirstEdgeBelow;
1341 leftEdge->fLeftPoly = leftPoly;
1342 activeEdges.insert(leftEdge, leftEnclosingEdge);
1343 for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1344 rightEdge = rightEdge->fNextEdgeBelow) {
1345 activeEdges.insert(rightEdge, leftEdge);
1346 int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1347 winding += leftEdge->fWinding;
1348 if (winding != 0) {
1349 Poly* poly = this->makePoly(&polys, v, winding);
1350 leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1351 }
1352 leftEdge = rightEdge;
1353 }
1354 v->fLastEdgeBelow->fRightPoly = rightPoly;
1355 }
1356 #if TRIANGULATOR_LOGGING
1357 TESS_LOG("\nactive edges:\n");
1358 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1359 TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n",
1360 e->fTop->fID, e->fBottom->fID,
1361 e->fLeftPoly ? e->fLeftPoly->fID : -1,
1362 e->fRightPoly ? e->fRightPoly->fID : -1);
1363 }
1364 #endif
1365 }
1366 return polys;
1367 }
1368
1369 // This is a driver function that calls stages 2-5 in turn.
1370
contoursToMesh(VertexList * contours,int contourCnt,VertexList * mesh,const Comparator & c) const1371 void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh,
1372 const Comparator& c) const {
1373 #if TRIANGULATOR_LOGGING
1374 for (int i = 0; i < contourCnt; ++i) {
1375 Vertex* v = contours[i].fHead;
1376 SkASSERT(v);
1377 TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1378 for (v = v->fNext; v; v = v->fNext) {
1379 TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1380 }
1381 }
1382 #endif
1383 this->sanitizeContours(contours, contourCnt);
1384 this->buildEdges(contours, contourCnt, mesh, c);
1385 }
1386
SortMesh(VertexList * vertices,const Comparator & c)1387 void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) {
1388 if (!vertices || !vertices->fHead) {
1389 return;
1390 }
1391
1392 // Sort vertices in Y (secondarily in X).
1393 if (c.fDirection == Comparator::Direction::kHorizontal) {
1394 merge_sort<sweep_lt_horiz>(vertices);
1395 } else {
1396 merge_sort<sweep_lt_vert>(vertices);
1397 }
1398 #if TRIANGULATOR_LOGGING
1399 for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
1400 static float gID = 0.0f;
1401 v->fID = gID++;
1402 }
1403 #endif
1404 }
1405
contoursToPolys(VertexList * contours,int contourCnt) const1406 Poly* GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt) const {
1407 const SkRect& pathBounds = fPath.getBounds();
1408 Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
1409 : Comparator::Direction::kVertical);
1410 VertexList mesh;
1411 this->contoursToMesh(contours, contourCnt, &mesh, c);
1412 TESS_LOG("\ninitial mesh:\n");
1413 DUMP_MESH(mesh);
1414 SortMesh(&mesh, c);
1415 TESS_LOG("\nsorted mesh:\n");
1416 DUMP_MESH(mesh);
1417 this->mergeCoincidentVertices(&mesh, c);
1418 TESS_LOG("\nsorted+merged mesh:\n");
1419 DUMP_MESH(mesh);
1420 this->simplify(&mesh, c);
1421 TESS_LOG("\nsimplified mesh:\n");
1422 DUMP_MESH(mesh);
1423 return this->tessellate(mesh, c);
1424 }
1425
1426 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
polysToTriangles(Poly * polys,void * data,SkPathFillType overrideFillType) const1427 void* GrTriangulator::polysToTriangles(Poly* polys, void* data,
1428 SkPathFillType overrideFillType) const {
1429 for (Poly* poly = polys; poly; poly = poly->fNext) {
1430 if (apply_fill_type(overrideFillType, poly)) {
1431 data = this->emitPoly(poly, data);
1432 }
1433 }
1434 return data;
1435 }
1436
get_contour_count(const SkPath & path,SkScalar tolerance)1437 static int get_contour_count(const SkPath& path, SkScalar tolerance) {
1438 // We could theoretically be more aggressive about not counting empty contours, but we need to
1439 // actually match the exact number of contour linked lists the tessellator will create later on.
1440 int contourCnt = 1;
1441 bool hasPoints = false;
1442
1443 SkPath::Iter iter(path, false);
1444 SkPath::Verb verb;
1445 SkPoint pts[4];
1446 bool first = true;
1447 while ((verb = iter.next(pts)) != SkPath::kDone_Verb) {
1448 switch (verb) {
1449 case SkPath::kMove_Verb:
1450 if (!first) {
1451 ++contourCnt;
1452 }
1453 [[fallthrough]];
1454 case SkPath::kLine_Verb:
1455 case SkPath::kConic_Verb:
1456 case SkPath::kQuad_Verb:
1457 case SkPath::kCubic_Verb:
1458 hasPoints = true;
1459 break;
1460 default:
1461 break;
1462 }
1463 first = false;
1464 }
1465 if (!hasPoints) {
1466 return 0;
1467 }
1468 return contourCnt;
1469 }
1470
pathToPolys(float tolerance,const SkRect & clipBounds,bool * isLinear) const1471 Poly* GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, bool* isLinear) const {
1472 int contourCnt = get_contour_count(fPath, tolerance);
1473 if (contourCnt <= 0) {
1474 *isLinear = true;
1475 return nullptr;
1476 }
1477
1478 if (SkPathFillType_IsInverse(fPath.getFillType())) {
1479 contourCnt++;
1480 }
1481 std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]);
1482
1483 this->pathToContours(tolerance, clipBounds, contours.get(), isLinear);
1484 return this->contoursToPolys(contours.get(), contourCnt);
1485 }
1486
CountPoints(Poly * polys,SkPathFillType overrideFillType)1487 int64_t GrTriangulator::CountPoints(Poly* polys, SkPathFillType overrideFillType) {
1488 int64_t count = 0;
1489 for (Poly* poly = polys; poly; poly = poly->fNext) {
1490 if (apply_fill_type(overrideFillType, poly) && poly->fCount >= 3) {
1491 count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3);
1492 }
1493 }
1494 return count;
1495 }
1496
1497 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
1498
polysToTriangles(Poly * polys,GrEagerVertexAllocator * vertexAllocator) const1499 int GrTriangulator::polysToTriangles(Poly* polys, GrEagerVertexAllocator* vertexAllocator) const {
1500 int64_t count64 = CountPoints(polys, fPath.getFillType());
1501 if (0 == count64 || count64 > SK_MaxS32) {
1502 return 0;
1503 }
1504 int count = count64;
1505
1506 size_t vertexStride = sizeof(SkPoint);
1507 if (fEmitCoverage) {
1508 vertexStride += sizeof(float);
1509 }
1510 void* verts = vertexAllocator->lock(vertexStride, count);
1511 if (!verts) {
1512 SkDebugf("Could not allocate vertices\n");
1513 return 0;
1514 }
1515
1516 TESS_LOG("emitting %d verts\n", count);
1517 void* end = this->polysToTriangles(polys, verts, fPath.getFillType());
1518
1519 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1520 / vertexStride);
1521 SkASSERT(actualCount <= count);
1522 vertexAllocator->unlock(actualCount);
1523 return actualCount;
1524 }
1525