Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(290)

Side by Side Diff: src/gpu/GrTessellator.cpp

Issue 1152733009: Screenspace AA tessellated path rendering. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Make GLPrograms test generate only simple fills. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/GrTessellator.h ('k') | src/gpu/batches/GrTessellatingPathRenderer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "GrTessellator.h" 8 #include "GrTessellator.h"
9 9
10 #include "GrDefaultGeoProcFactory.h"
10 #include "GrPathUtils.h" 11 #include "GrPathUtils.h"
11 12
12 #include "SkChunkAlloc.h" 13 #include "SkChunkAlloc.h"
13 #include "SkGeometry.h" 14 #include "SkGeometry.h"
14 #include "SkPath.h" 15 #include "SkPath.h"
15 16
16 #include <stdio.h> 17 #include <stdio.h>
17 18
18 /* 19 /*
19 * There are six stages to the algorithm: 20 * There are six stages to the basic algorithm:
20 * 21 *
21 * 1) Linearize the path contours into piecewise linear segments (path_to_contou rs()). 22 * 1) Linearize the path contours into piecewise linear segments (path_to_contou rs()).
22 * 2) Build a mesh of edges connecting the vertices (build_edges()). 23 * 2) Build a mesh of edges connecting the vertices (build_edges()).
23 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). 24 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
24 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplif y()). 25 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplif y()).
25 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). 26 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
26 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_ triangles()). 27 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_ triangles()).
27 * 28 *
29 * For screenspace antialiasing, the algorithm is modified as follows:
30 *
31 * Run steps 1-5 above to produce polygons.
32 * 5b) Apply fill rules to extract boundary contours from the polygons (extract_ boundaries()).
33 * 5c) Simplify boundaries to remove "pointy" vertices which cause inversions (s implify_boundary()).
34 * 5d) Displace edges by half a pixel inward and outward along their normals. In tersect to find
35 * new vertices, and set zero alpha on the exterior and one alpha on the int erior. Build a new
36 * antialiased mesh from those vertices (boundary_to_aa_mesh()).
37 * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
38 *
28 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list 39 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
29 * of vertices (and the necessity of inserting new vertices on intersection). 40 * of vertices (and the necessity of inserting new vertices on intersection).
30 * 41 *
31 * Stages (4) and (5) use an active edge list, which a list of all edges for whi ch the 42 * Stages (4) and (5) use an active edge list, which a list of all edges for whi ch the
32 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorte d 43 * sweep line has crossed the top vertex, but not the bottom vertex. It's sorte d
33 * left-to-right based on the point where both edges are active (when both top v ertices 44 * left-to-right based on the point where both edges are active (when both top v ertices
34 * have been seen, so the "lower" top vertex of the two). If the top vertices ar e equal 45 * have been seen, so the "lower" top vertex of the two). If the top vertices ar e equal
35 * (shared), it's sorted based on the last point where both edges are active, so the 46 * (shared), it's sorted based on the last point where both edges are active, so the
36 * "upper" bottom vertex. 47 * "upper" bottom vertex.
37 * 48 *
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices 134 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
124 * are re-ordered by the merge sort according to the sweep_lt comparator (usuall y, increasing 135 * are re-ordered by the merge sort according to the sweep_lt comparator (usuall y, increasing
125 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid 136 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
126 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of 137 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
127 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePoly s, since 138 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePoly s, since
128 * an individual Vertex from the path mesh may belong to multiple 139 * an individual Vertex from the path mesh may belong to multiple
129 * MonotonePolys, so the original Vertices cannot be re-used. 140 * MonotonePolys, so the original Vertices cannot be re-used.
130 */ 141 */
131 142
132 struct Vertex { 143 struct Vertex {
133 Vertex(const SkPoint& point) 144 Vertex(const SkPoint& point, uint8_t alpha)
134 : fPoint(point), fPrev(nullptr), fNext(nullptr) 145 : fPoint(point), fPrev(nullptr), fNext(nullptr)
135 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr) 146 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
136 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr) 147 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
137 , fProcessed(false) 148 , fProcessed(false)
149 , fAlpha(alpha)
138 #if LOGGING_ENABLED 150 #if LOGGING_ENABLED
139 , fID (-1.0f) 151 , fID (-1.0f)
140 #endif 152 #endif
141 {} 153 {}
142 SkPoint fPoint; // Vertex position 154 SkPoint fPoint; // Vertex position
143 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices . 155 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices .
144 Vertex* fNext; // " 156 Vertex* fNext; // "
145 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. 157 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
146 Edge* fLastEdgeAbove; // " 158 Edge* fLastEdgeAbove; // "
147 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. 159 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
148 Edge* fLastEdgeBelow; // " 160 Edge* fLastEdgeBelow; // "
149 bool fProcessed; // Has this vertex been seen in simplify()? 161 bool fProcessed; // Has this vertex been seen in simplify()?
162 uint8_t fAlpha;
150 #if LOGGING_ENABLED 163 #if LOGGING_ENABLED
151 float fID; // Identifier used for logging. 164 float fID; // Identifier used for logging.
152 #endif 165 #endif
153 }; 166 };
154 167
155 /******************************************************************************* ********/ 168 /******************************************************************************* ********/
156 169
170 struct AAParams {
171 bool fTweakAlpha;
172 GrColor fColor;
173 };
174
157 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); 175 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
158 176
159 struct Comparator { 177 struct Comparator {
160 CompareFunc sweep_lt; 178 CompareFunc sweep_lt;
161 CompareFunc sweep_gt; 179 CompareFunc sweep_gt;
162 }; 180 };
163 181
164 bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 182 bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
165 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX; 183 return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
166 } 184 }
167 185
168 bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { 186 bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
169 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY; 187 return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
170 } 188 }
171 189
172 bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) { 190 bool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
173 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX; 191 return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
174 } 192 }
175 193
176 bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) { 194 bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
177 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; 195 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
178 } 196 }
179 197
180 inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) { 198 inline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
181 *data++ = v->fPoint; 199 if (!aaParams) {
182 return data; 200 SkPoint* d = static_cast<SkPoint*>(data);
201 *d++ = v->fPoint;
202 return d;
203 }
204 if (aaParams->fTweakAlpha) {
205 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
206 d->fPosition = v->fPoint;
207 d->fColor = SkAlphaMulQ(aaParams->fColor, v->fAlpha);
208 d++;
209 return d;
210 }
211 auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(da ta);
212 d->fPosition = v->fPoint;
213 d->fColor = aaParams->fColor;
214 d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
215 d++;
216 return d;
183 } 217 }
184 218
185 SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { 219 void* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams , void* data) {
186 #if WIREFRAME 220 #if TESSELLATOR_WIREFRAME
187 data = emit_vertex(v0, data); 221 data = emit_vertex(v0, aaParams, data);
188 data = emit_vertex(v1, data); 222 data = emit_vertex(v1, aaParams, data);
189 data = emit_vertex(v1, data); 223 data = emit_vertex(v1, aaParams, data);
190 data = emit_vertex(v2, data); 224 data = emit_vertex(v2, aaParams, data);
191 data = emit_vertex(v2, data); 225 data = emit_vertex(v2, aaParams, data);
192 data = emit_vertex(v0, data); 226 data = emit_vertex(v0, aaParams, data);
193 #else 227 #else
194 data = emit_vertex(v0, data); 228 data = emit_vertex(v0, aaParams, data);
195 data = emit_vertex(v1, data); 229 data = emit_vertex(v1, aaParams, data);
196 data = emit_vertex(v2, data); 230 data = emit_vertex(v2, aaParams, data);
197 #endif 231 #endif
198 return data; 232 return data;
199 } 233 }
200 234
201 struct EdgeList {
202 EdgeList() : fHead(nullptr), fTail(nullptr) {}
203 Edge* fHead;
204 Edge* fTail;
205 };
206
207 struct VertexList { 235 struct VertexList {
208 VertexList() : fHead(nullptr), fTail(nullptr) {} 236 VertexList() : fHead(nullptr), fTail(nullptr) {}
209 Vertex* fHead; 237 Vertex* fHead;
210 Vertex* fTail; 238 Vertex* fTail;
211 void insert(Vertex* v, Vertex* prev, Vertex* next) { 239 void insert(Vertex* v, Vertex* prev, Vertex* next) {
212 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHea d, &fTail); 240 list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHea d, &fTail);
213 } 241 }
214 void append(Vertex* v) { 242 void append(Vertex* v) {
215 insert(v, fTail, nullptr); 243 insert(v, fTail, nullptr);
216 } 244 }
217 void prepend(Vertex* v) { 245 void prepend(Vertex* v) {
218 insert(v, nullptr, fHead); 246 insert(v, nullptr, fHead);
219 } 247 }
248 void close() {
249 if (fHead && fTail) {
250 fTail->fNext = fHead;
251 fHead->fPrev = fTail;
252 }
253 }
220 }; 254 };
221 255
256 // Round to nearest quarter-pixel. This is used for screenspace tessellation.
257
258 inline void round(SkPoint* p) {
259 p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScal ar(0.25f);
260 p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScal ar(0.25f);
261 }
262
222 /** 263 /**
223 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and 264 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
224 * "edge below" a vertex as well as for the active edge list is handled by isLef tOf()/isRightOf(). 265 * "edge below" a vertex as well as for the active edge list is handled by isLef tOf()/isRightOf().
225 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (b ecause floating 266 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (b ecause floating
226 * point). For speed, that case is only tested by the callers which require it ( e.g., 267 * point). For speed, that case is only tested by the callers which require it ( e.g.,
227 * cleanup_active_edges()). Edges also handle checking for intersection with oth er edges. 268 * cleanup_active_edges()). Edges also handle checking for intersection with oth er edges.
228 * Currently, this converts the edges to the parametric form, in order to avoid doing a division 269 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
229 * until an intersection has been confirmed. This is slightly slower in the "fou nd" case, but 270 * until an intersection has been confirmed. This is slightly slower in the "fou nd" case, but
230 * a lot faster in the "not found" case. 271 * a lot faster in the "not found" case.
231 * 272 *
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNu mer > denom) 354 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNu mer > denom)
314 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNu mer < denom)) { 355 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNu mer < denom)) {
315 return false; 356 return false;
316 } 357 }
317 double s = sNumer / denom; 358 double s = sNumer / denom;
318 SkASSERT(s >= 0.0 && s <= 1.0); 359 SkASSERT(s >= 0.0 && s <= 1.0);
319 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX); 360 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
320 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY); 361 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
321 return true; 362 return true;
322 } 363 }
323 bool isActive(EdgeList* activeEdges) const { 364 };
324 return activeEdges && (fLeft || fRight || activeEdges->fHead == this); 365
366 struct EdgeList {
367 EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {}
368 Edge* fHead;
369 Edge* fTail;
370 EdgeList* fNext;
371 int fCount;
372 void insert(Edge* edge, Edge* prev, Edge* next) {
373 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
374 fCount++;
375 }
376 void append(Edge* e) {
377 insert(e, fTail, nullptr);
378 }
379 void remove(Edge* edge) {
380 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
381 fCount--;
382 }
383 void close() {
384 if (fHead && fTail) {
385 fTail->fRight = fHead;
386 fHead->fLeft = fTail;
387 }
388 }
389 bool contains(Edge* edge) const {
390 return edge->fLeft || edge->fRight || fHead == edge;
325 } 391 }
326 }; 392 };
327 393
328 /******************************************************************************* ********/ 394 /******************************************************************************* ********/
329 395
330 struct Poly { 396 struct Poly {
331 Poly(Vertex* v, int winding) 397 Poly(Vertex* v, int winding)
332 : fFirstVertex(v) 398 : fFirstVertex(v)
333 , fWinding(winding) 399 , fWinding(winding)
334 , fHead(nullptr) 400 , fHead(nullptr)
(...skipping 30 matching lines...) Expand all
365 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 431 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
366 edge->fUsedInRightPoly = true; 432 edge->fUsedInRightPoly = true;
367 } else { 433 } else {
368 SkASSERT(!edge->fUsedInLeftPoly); 434 SkASSERT(!edge->fUsedInLeftPoly);
369 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( 435 list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
370 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 436 edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
371 edge->fUsedInLeftPoly = true; 437 edge->fUsedInLeftPoly = true;
372 } 438 }
373 } 439 }
374 440
375 SkPoint* emit(SkPoint* data) { 441 void* emit(const AAParams* aaParams, void* data) {
376 Edge* e = fFirstEdge; 442 Edge* e = fFirstEdge;
377 e->fTop->fPrev = e->fTop->fNext = nullptr; 443 e->fTop->fPrev = e->fTop->fNext = nullptr;
378 VertexList vertices; 444 VertexList vertices;
379 vertices.append(e->fTop); 445 vertices.append(e->fTop);
380 while (e != nullptr) { 446 while (e != nullptr) {
381 e->fBottom->fPrev = e->fBottom->fNext = nullptr; 447 e->fBottom->fPrev = e->fBottom->fNext = nullptr;
382 if (kRight_Side == fSide) { 448 if (kRight_Side == fSide) {
383 vertices.append(e->fBottom); 449 vertices.append(e->fBottom);
384 e = e->fRightPolyNext; 450 e = e->fRightPolyNext;
385 } else { 451 } else {
386 vertices.prepend(e->fBottom); 452 vertices.prepend(e->fBottom);
387 e = e->fLeftPolyNext; 453 e = e->fLeftPolyNext;
388 } 454 }
389 } 455 }
390 Vertex* first = vertices.fHead; 456 Vertex* first = vertices.fHead;
391 Vertex* v = first->fNext; 457 Vertex* v = first->fNext;
392 while (v != vertices.fTail) { 458 while (v != vertices.fTail) {
393 SkASSERT(v && v->fPrev && v->fNext); 459 SkASSERT(v && v->fPrev && v->fNext);
394 Vertex* prev = v->fPrev; 460 Vertex* prev = v->fPrev;
395 Vertex* curr = v; 461 Vertex* curr = v;
396 Vertex* next = v->fNext; 462 Vertex* next = v->fNext;
397 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX; 463 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX;
398 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY; 464 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY;
399 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint. fX; 465 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint. fX;
400 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint. fY; 466 double by = static_cast<double>(next->fPoint.fY) - curr->fPoint. fY;
401 if (ax * by - ay * bx >= 0.0) { 467 if (ax * by - ay * bx >= 0.0) {
402 data = emit_triangle(prev, curr, next, data); 468 data = emit_triangle(prev, curr, next, aaParams, data);
403 v->fPrev->fNext = v->fNext; 469 v->fPrev->fNext = v->fNext;
404 v->fNext->fPrev = v->fPrev; 470 v->fNext->fPrev = v->fPrev;
405 if (v->fPrev == first) { 471 if (v->fPrev == first) {
406 v = v->fNext; 472 v = v->fNext;
407 } else { 473 } else {
408 v = v->fPrev; 474 v = v->fPrev;
409 } 475 }
410 } else { 476 } else {
411 v = v->fNext; 477 v = v->fNext;
412 } 478 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 poly = partner; 514 poly = partner;
449 } else { 515 } else {
450 MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc); 516 MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc);
451 m->fPrev = fTail; 517 m->fPrev = fTail;
452 fTail->fNext = m; 518 fTail->fNext = m;
453 fTail = m; 519 fTail = m;
454 } 520 }
455 } 521 }
456 return poly; 522 return poly;
457 } 523 }
458 SkPoint* emit(SkPoint *data) { 524 void* emit(const AAParams* aaParams, void *data) {
459 if (fCount < 3) { 525 if (fCount < 3) {
460 return data; 526 return data;
461 } 527 }
462 LOG("emit() %d, size %d\n", fID, fCount); 528 LOG("emit() %d, size %d\n", fID, fCount);
463 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { 529 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
464 data = m->emit(data); 530 data = m->emit(aaParams, data);
465 } 531 }
466 return data; 532 return data;
467 } 533 }
468 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFir stVertex; } 534 Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFir stVertex; }
469 Vertex* fFirstVertex; 535 Vertex* fFirstVertex;
470 int fWinding; 536 int fWinding;
471 MonotonePoly* fHead; 537 MonotonePoly* fHead;
472 MonotonePoly* fTail; 538 MonotonePoly* fTail;
473 Poly* fNext; 539 Poly* fNext;
474 Poly* fPartner; 540 Poly* fPartner;
475 int fCount; 541 int fCount;
476 #if LOGGING_ENABLED 542 #if LOGGING_ENABLED
477 int fID; 543 int fID;
478 #endif 544 #endif
479 }; 545 };
480 546
481 /******************************************************************************* ********/ 547 /******************************************************************************* ********/
482 548
483 bool coincident(const SkPoint& a, const SkPoint& b) { 549 bool coincident(const SkPoint& a, const SkPoint& b) {
484 return a == b; 550 return a == b;
485 } 551 }
486 552
487 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { 553 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
488 Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc); 554 Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc);
489 poly->fNext = *head; 555 poly->fNext = *head;
490 *head = poly; 556 *head = poly;
491 return poly; 557 return poly;
492 } 558 }
493 559
560 EdgeList* new_contour(EdgeList** head, SkChunkAlloc& alloc) {
561 EdgeList* contour = ALLOC_NEW(EdgeList, (), alloc);
562 contour->fNext = *head;
563 *head = contour;
564 return contour;
565 }
566
494 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 567 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
495 SkChunkAlloc& alloc) { 568 SkChunkAlloc& alloc) {
496 Vertex* v = ALLOC_NEW(Vertex, (p), alloc); 569 Vertex* v = ALLOC_NEW(Vertex, (p, 255), alloc);
497 #if LOGGING_ENABLED 570 #if LOGGING_ENABLED
498 static float gID = 0.0f; 571 static float gID = 0.0f;
499 v->fID = gID++; 572 v->fID = gID++;
500 #endif 573 #endif
501 if (prev) { 574 if (prev) {
502 prev->fNext = v; 575 prev->fNext = v;
503 v->fPrev = prev; 576 v->fPrev = prev;
504 } else { 577 } else {
505 *head = v; 578 *head = v;
506 } 579 }
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
571 644
572 SkPoint pts[4]; 645 SkPoint pts[4];
573 bool done = false; 646 bool done = false;
574 *isLinear = true; 647 *isLinear = true;
575 SkPath::Iter iter(path, false); 648 SkPath::Iter iter(path, false);
576 Vertex* prev = nullptr; 649 Vertex* prev = nullptr;
577 Vertex* head = nullptr; 650 Vertex* head = nullptr;
578 if (path.isInverseFillType()) { 651 if (path.isInverseFillType()) {
579 SkPoint quad[4]; 652 SkPoint quad[4];
580 clipBounds.toQuad(quad); 653 clipBounds.toQuad(quad);
581 for (int i = 3; i >= 0; i--) { 654 for (int i = 0; i < 4; i++) {
582 prev = append_point_to_contour(quad[i], prev, &head, alloc); 655 prev = append_point_to_contour(quad[i], prev, &head, alloc);
583 } 656 }
584 head->fPrev = prev; 657 head->fPrev = prev;
585 prev->fNext = head; 658 prev->fNext = head;
586 *contours++ = head; 659 *contours++ = head;
587 head = prev = nullptr; 660 head = prev = nullptr;
588 } 661 }
589 SkAutoConicToQuads converter; 662 SkAutoConicToQuads converter;
590 while (!done) { 663 while (!done) {
591 SkPath::Verb verb = iter.next(pts); 664 SkPath::Verb verb = iter.next(pts);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
642 head->fPrev = prev; 715 head->fPrev = prev;
643 prev->fNext = head; 716 prev->fNext = head;
644 *contours++ = head; 717 *contours++ = head;
645 } 718 }
646 done = true; 719 done = true;
647 break; 720 break;
648 } 721 }
649 } 722 }
650 } 723 }
651 724
652 inline bool apply_fill_type(SkPath::FillType fillType, int winding) { 725 inline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
726 if (!poly) {
727 return false;
728 }
729 int winding = poly->fWinding;
653 switch (fillType) { 730 switch (fillType) {
654 case SkPath::kWinding_FillType: 731 case SkPath::kWinding_FillType:
655 return winding != 0; 732 return winding != 0;
656 case SkPath::kEvenOdd_FillType: 733 case SkPath::kEvenOdd_FillType:
657 return (winding & 1) != 0; 734 return (winding & 1) != 0;
658 case SkPath::kInverseWinding_FillType: 735 case SkPath::kInverseWinding_FillType:
659 return winding == 1; 736 return winding == -1;
660 case SkPath::kInverseEvenOdd_FillType: 737 case SkPath::kInverseEvenOdd_FillType:
661 return (winding & 1) == 1; 738 return (winding & 1) == 1;
662 default: 739 default:
663 SkASSERT(false); 740 SkASSERT(false);
664 return false; 741 return false;
665 } 742 }
666 } 743 }
667 744
668 Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c) { 745 Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c,
669 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 746 int winding_scale = 1) {
747 int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? winding_scale : -wind ing_scale;
670 Vertex* top = winding < 0 ? next : prev; 748 Vertex* top = winding < 0 ? next : prev;
671 Vertex* bottom = winding < 0 ? prev : next; 749 Vertex* bottom = winding < 0 ? prev : next;
672 return ALLOC_NEW(Edge, (top, bottom, winding), alloc); 750 return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
673 } 751 }
674 752
675 void remove_edge(Edge* edge, EdgeList* edges) { 753 void remove_edge(Edge* edge, EdgeList* edges) {
676 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 754 LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
677 SkASSERT(edge->isActive(edges)); 755 SkASSERT(edges->contains(edge));
678 list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &edges->fHead, &edges-> fTail); 756 edges->remove(edge);
679 } 757 }
680 758
681 void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { 759 void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
682 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 760 LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
683 SkASSERT(!edge->isActive(edges)); 761 SkASSERT(!edges->contains(edge));
684 Edge* next = prev ? prev->fRight : edges->fHead; 762 Edge* next = prev ? prev->fRight : edges->fHead;
685 list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &edges->fHe ad, &edges->fTail); 763 edges->insert(edge, prev, next);
686 } 764 }
687 765
688 void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { 766 void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
689 if (v->fFirstEdgeAbove) { 767 if (v->fFirstEdgeAbove) {
690 *left = v->fFirstEdgeAbove->fLeft; 768 *left = v->fFirstEdgeAbove->fLeft;
691 *right = v->fLastEdgeAbove->fRight; 769 *right = v->fLastEdgeAbove->fRight;
692 return; 770 return;
693 } 771 }
694 Edge* next = nullptr; 772 Edge* next = nullptr;
695 Edge* prev; 773 Edge* prev;
(...skipping 19 matching lines...) Expand all
715 edge->isLeftOf(next->fBottom))) { 793 edge->isLeftOf(next->fBottom))) {
716 break; 794 break;
717 } 795 }
718 prev = next; 796 prev = next;
719 } 797 }
720 *left = prev; 798 *left = prev;
721 *right = next; 799 *right = next;
722 } 800 }
723 801
724 void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) { 802 void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
725 if (edge->isActive(activeEdges)) { 803 if (activeEdges && activeEdges->contains(edge)) {
726 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) { 804 if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
727 remove_edge(edge, activeEdges); 805 remove_edge(edge, activeEdges);
728 } 806 }
729 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) { 807 } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
730 Edge* left; 808 Edge* left;
731 Edge* right; 809 Edge* right;
732 find_enclosing_edges(edge, activeEdges, c, &left, &right); 810 find_enclosing_edges(edge, activeEdges, c, &left, &right);
733 insert_edge(edge, left, activeEdges); 811 insert_edge(edge, left, activeEdges);
734 } 812 }
735 } 813 }
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
784 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); 862 edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
785 } 863 }
786 864
787 void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) { 865 void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) {
788 if (edge->fWinding != 0) { 866 if (edge->fWinding != 0) {
789 return; 867 return;
790 } 868 }
791 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID); 869 LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
792 remove_edge_above(edge); 870 remove_edge_above(edge);
793 remove_edge_below(edge); 871 remove_edge_below(edge);
794 if (edge->isActive(edges)) { 872 if (edges && edges->contains(edge)) {
795 remove_edge(edge, edges); 873 remove_edge(edge, edges);
796 } 874 }
797 } 875 }
798 876
799 void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c); 877 void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
800 878
801 void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 879 void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
802 remove_edge_below(edge); 880 remove_edge_below(edge);
803 edge->fTop = v; 881 edge->fTop = v;
804 edge->recompute(); 882 edge->recompute();
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
921 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), allo c); 999 Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), allo c);
922 insert_edge_below(newEdge, v, c); 1000 insert_edge_below(newEdge, v, c);
923 insert_edge_above(newEdge, edge->fBottom, c); 1001 insert_edge_above(newEdge, edge->fBottom, c);
924 set_bottom(edge, v, activeEdges, c); 1002 set_bottom(edge, v, activeEdges, c);
925 cleanup_active_edges(edge, activeEdges, c, alloc); 1003 cleanup_active_edges(edge, activeEdges, c, alloc);
926 fix_active_state(newEdge, activeEdges, c); 1004 fix_active_state(newEdge, activeEdges, c);
927 merge_collinear_edges(newEdge, activeEdges, c); 1005 merge_collinear_edges(newEdge, activeEdges, c);
928 } 1006 }
929 } 1007 }
930 1008
1009 Edge* connect(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator c,
1010 int winding_scale = 1) {
1011 Edge* edge = new_edge(prev, next, alloc, c, winding_scale);
1012 if (edge->fWinding > 0) {
1013 insert_edge_below(edge, prev, c);
1014 insert_edge_above(edge, next, c);
1015 } else {
1016 insert_edge_below(edge, next, c);
1017 insert_edge_above(edge, prev, c);
1018 }
1019 merge_collinear_edges(edge, nullptr, c);
1020 return edge;
1021 }
1022
931 void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkCh unkAlloc& alloc) { 1023 void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkCh unkAlloc& alloc) {
932 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX , src->fPoint.fY, 1024 LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX , src->fPoint.fY,
933 src->fID, dst->fID); 1025 src->fID, dst->fID);
1026 dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
934 for (Edge* edge = src->fFirstEdgeAbove; edge;) { 1027 for (Edge* edge = src->fFirstEdgeAbove; edge;) {
935 Edge* next = edge->fNextEdgeAbove; 1028 Edge* next = edge->fNextEdgeAbove;
936 set_bottom(edge, dst, nullptr, c); 1029 set_bottom(edge, dst, nullptr, c);
937 edge = next; 1030 edge = next;
938 } 1031 }
939 for (Edge* edge = src->fFirstEdgeBelow; edge;) { 1032 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
940 Edge* next = edge->fNextEdgeBelow; 1033 Edge* next = edge->fNextEdgeBelow;
941 set_top(edge, dst, nullptr, c); 1034 set_top(edge, dst, nullptr, c);
942 edge = next; 1035 edge = next;
943 } 1036 }
944 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); 1037 list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr);
945 } 1038 }
946 1039
1040 uint8_t max_edge_alpha(Edge* a, Edge* b) {
1041 return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
1042 SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
1043 }
1044
947 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c, 1045 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c,
948 SkChunkAlloc& alloc) { 1046 SkChunkAlloc& alloc) {
949 SkPoint p; 1047 SkPoint p;
950 if (!edge || !other) { 1048 if (!edge || !other) {
951 return nullptr; 1049 return nullptr;
952 } 1050 }
953 if (edge->intersect(*other, &p)) { 1051 if (edge->intersect(*other, &p)) {
954 Vertex* v; 1052 Vertex* v;
955 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 1053 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
956 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { 1054 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
(...skipping 15 matching lines...) Expand all
972 } 1070 }
973 while (c.sweep_lt(nextV->fPoint, p)) { 1071 while (c.sweep_lt(nextV->fPoint, p)) {
974 nextV = nextV->fNext; 1072 nextV = nextV->fNext;
975 } 1073 }
976 Vertex* prevV = nextV->fPrev; 1074 Vertex* prevV = nextV->fPrev;
977 if (coincident(prevV->fPoint, p)) { 1075 if (coincident(prevV->fPoint, p)) {
978 v = prevV; 1076 v = prevV;
979 } else if (coincident(nextV->fPoint, p)) { 1077 } else if (coincident(nextV->fPoint, p)) {
980 v = nextV; 1078 v = nextV;
981 } else { 1079 } else {
982 v = ALLOC_NEW(Vertex, (p), alloc); 1080 uint8_t alpha = max_edge_alpha(edge, other);
1081 v = ALLOC_NEW(Vertex, (p, alpha), alloc);
983 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", 1082 LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
984 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, 1083 prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
985 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); 1084 nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
986 #if LOGGING_ENABLED 1085 #if LOGGING_ENABLED
987 v->fID = (nextV->fID + prevV->fID) * 0.5f; 1086 v->fID = (nextV->fID + prevV->fID) * 0.5f;
988 #endif 1087 #endif
989 v->fPrev = prevV; 1088 v->fPrev = prevV;
990 v->fNext = nextV; 1089 v->fNext = nextV;
991 prevV->fNext = v; 1090 prevV->fNext = v;
992 nextV->fPrev = v; 1091 nextV->fPrev = v;
993 } 1092 }
994 split_edge(edge, v, activeEdges, c, alloc); 1093 split_edge(edge, v, activeEdges, c, alloc);
995 split_edge(other, v, activeEdges, c, alloc); 1094 split_edge(other, v, activeEdges, c, alloc);
996 } 1095 }
997 return v; 1096 return v;
998 } 1097 }
999 return nullptr; 1098 return nullptr;
1000 } 1099 }
1001 1100
1002 void sanitize_contours(Vertex** contours, int contourCnt) { 1101 void sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
1003 for (int i = 0; i < contourCnt; ++i) { 1102 for (int i = 0; i < contourCnt; ++i) {
1004 SkASSERT(contours[i]); 1103 SkASSERT(contours[i]);
1005 for (Vertex* v = contours[i];;) { 1104 for (Vertex* v = contours[i];;) {
1105 if (approximate) {
1106 round(&v->fPoint);
1107 }
1006 if (coincident(v->fPrev->fPoint, v->fPoint)) { 1108 if (coincident(v->fPrev->fPoint, v->fPoint)) {
1007 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoi nt.fY); 1109 LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoi nt.fY);
1008 if (v->fPrev == v) { 1110 if (v->fPrev == v) {
1009 contours[i] = nullptr; 1111 contours[i] = nullptr;
1010 break; 1112 break;
1011 } 1113 }
1012 v->fPrev->fNext = v->fNext; 1114 v->fPrev->fNext = v->fNext;
1013 v->fNext->fPrev = v->fPrev; 1115 v->fNext->fPrev = v->fPrev;
1014 if (contours[i] == v) { 1116 if (contours[i] == v) {
1015 contours[i] = v->fNext; 1117 contours[i] = v->fNext;
(...skipping 19 matching lines...) Expand all
1035 } 1137 }
1036 1138
1037 // Stage 2: convert the contours to a mesh of edges connecting the vertices. 1139 // Stage 2: convert the contours to a mesh of edges connecting the vertices.
1038 1140
1039 Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAll oc& alloc) { 1141 Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAll oc& alloc) {
1040 Vertex* vertices = nullptr; 1142 Vertex* vertices = nullptr;
1041 Vertex* prev = nullptr; 1143 Vertex* prev = nullptr;
1042 for (int i = 0; i < contourCnt; ++i) { 1144 for (int i = 0; i < contourCnt; ++i) {
1043 for (Vertex* v = contours[i]; v != nullptr;) { 1145 for (Vertex* v = contours[i]; v != nullptr;) {
1044 Vertex* vNext = v->fNext; 1146 Vertex* vNext = v->fNext;
1045 Edge* edge = new_edge(v->fPrev, v, alloc, c); 1147 connect(v->fPrev, v, alloc, c);
1046 if (edge->fWinding > 0) {
1047 insert_edge_below(edge, v->fPrev, c);
1048 insert_edge_above(edge, v, c);
1049 } else {
1050 insert_edge_below(edge, v, c);
1051 insert_edge_above(edge, v->fPrev, c);
1052 }
1053 merge_collinear_edges(edge, nullptr, c);
1054 if (prev) { 1148 if (prev) {
1055 prev->fNext = v; 1149 prev->fNext = v;
1056 v->fPrev = prev; 1150 v->fPrev = prev;
1057 } else { 1151 } else {
1058 vertices = v; 1152 vertices = v;
1059 } 1153 }
1060 prev = v; 1154 prev = v;
1061 v = vNext; 1155 v = vNext;
1062 if (v == contours[i]) break; 1156 if (v == contours[i]) break;
1063 } 1157 }
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. 1232 // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1139 1233
1140 void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { 1234 void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) {
1141 LOG("simplifying complex polygons\n"); 1235 LOG("simplifying complex polygons\n");
1142 EdgeList activeEdges; 1236 EdgeList activeEdges;
1143 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1237 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1144 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1238 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1145 continue; 1239 continue;
1146 } 1240 }
1147 #if LOGGING_ENABLED 1241 #if LOGGING_ENABLED
1148 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); 1242 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint. fY, v->fAlpha);
1149 #endif 1243 #endif
1150 Edge* leftEnclosingEdge = nullptr; 1244 Edge* leftEnclosingEdge = nullptr;
1151 Edge* rightEnclosingEdge = nullptr; 1245 Edge* rightEnclosingEdge = nullptr;
1152 bool restartChecks; 1246 bool restartChecks;
1153 do { 1247 do {
1154 restartChecks = false; 1248 restartChecks = false;
1155 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEncl osingEdge); 1249 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEncl osingEdge);
1156 if (v->fFirstEdgeBelow) { 1250 if (v->fFirstEdgeBelow) {
1157 for (Edge* edge = v->fFirstEdgeBelow; edge != nullptr; edge = ed ge->fNextEdgeBelow) { 1251 for (Edge* edge = v->fFirstEdgeBelow; edge != nullptr; edge = ed ge->fNextEdgeBelow) {
1158 if (check_for_intersection(edge, leftEnclosingEdge, &activeE dges, c, alloc)) { 1252 if (check_for_intersection(edge, leftEnclosingEdge, &activeE dges, c, alloc)) {
1159 restartChecks = true; 1253 restartChecks = true;
1160 break; 1254 break;
1161 } 1255 }
1162 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) { 1256 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) {
1163 restartChecks = true; 1257 restartChecks = true;
1164 break; 1258 break;
1165 } 1259 }
1166 } 1260 }
1167 } else { 1261 } else {
1168 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge, 1262 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge,
1169 &activeEdges, c, alloc)) { 1263 &activeEdges, c, alloc)) {
1170 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1264 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1171 v = pv; 1265 v = pv;
1172 } 1266 }
1173 restartChecks = true; 1267 restartChecks = true;
1174 } 1268 }
1175 1269
1176 } 1270 }
1177 } while (restartChecks); 1271 } while (restartChecks);
1272 if (v->fAlpha == 0) {
1273 if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
1274 (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
1275 v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge );
1276 }
1277 }
1178 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1278 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1179 remove_edge(e, &activeEdges); 1279 remove_edge(e, &activeEdges);
1180 } 1280 }
1181 Edge* leftEdge = leftEnclosingEdge; 1281 Edge* leftEdge = leftEnclosingEdge;
1182 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1282 for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1183 insert_edge(e, leftEdge, &activeEdges); 1283 insert_edge(e, leftEdge, &activeEdges);
1184 leftEdge = e; 1284 leftEdge = e;
1185 } 1285 }
1186 v->fProcessed = true; 1286 v->fProcessed = true;
1187 } 1287 }
1188 } 1288 }
1189 1289
1190 // Stage 5: Tessellate the simplified mesh into monotone polygons. 1290 // Stage 5: Tessellate the simplified mesh into monotone polygons.
1191 1291
1192 Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { 1292 Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1193 LOG("tessellating simple polygons\n"); 1293 LOG("tessellating simple polygons\n");
1194 EdgeList activeEdges; 1294 EdgeList activeEdges;
1195 Poly* polys = nullptr; 1295 Poly* polys = nullptr;
1196 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1296 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1197 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1297 if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1198 continue; 1298 continue;
1199 } 1299 }
1200 #if LOGGING_ENABLED 1300 #if LOGGING_ENABLED
1201 LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); 1301 LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint. fY, v->fAlpha);
1202 #endif 1302 #endif
1203 Edge* leftEnclosingEdge = nullptr; 1303 Edge* leftEnclosingEdge = nullptr;
1204 Edge* rightEnclosingEdge = nullptr; 1304 Edge* rightEnclosingEdge = nullptr;
1205 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosin gEdge); 1305 find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosin gEdge);
1206 Poly* leftPoly = nullptr; 1306 Poly* leftPoly = nullptr;
1207 Poly* rightPoly = nullptr; 1307 Poly* rightPoly = nullptr;
1208 if (v->fFirstEdgeAbove) { 1308 if (v->fFirstEdgeAbove) {
1209 leftPoly = v->fFirstEdgeAbove->fLeftPoly; 1309 leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1210 rightPoly = v->fLastEdgeAbove->fRightPoly; 1310 rightPoly = v->fLastEdgeAbove->fRightPoly;
1211 } else { 1311 } else {
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 LOG("\nactive edges:\n"); 1391 LOG("\nactive edges:\n");
1292 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { 1392 for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1293 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1393 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1294 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight Poly->fID : -1); 1394 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight Poly->fID : -1);
1295 } 1395 }
1296 #endif 1396 #endif
1297 } 1397 }
1298 return polys; 1398 return polys;
1299 } 1399 }
1300 1400
1401 bool is_boundary_edge(Edge* edge, SkPath::FillType fillType) {
1402 return apply_fill_type(fillType, edge->fLeftPoly) !=
1403 apply_fill_type(fillType, edge->fRightPoly);
1404 }
1405
1406 bool is_boundary_start(Edge* edge, SkPath::FillType fillType) {
1407 return !apply_fill_type(fillType, edge->fLeftPoly) &&
1408 apply_fill_type(fillType, edge->fRightPoly);
1409 }
1410
1411 Vertex* remove_non_boundary_edges(Vertex* vertices, SkPath::FillType fillType,
1412 SkChunkAlloc& alloc) {
1413 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1414 for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) {
1415 Edge* next = e->fNextEdgeBelow;
1416 if (!is_boundary_edge(e, fillType)) {
1417 remove_edge_above(e);
1418 remove_edge_below(e);
1419 }
1420 e = next;
1421 }
1422 }
1423 return vertices;
1424 }
1425
1426 // This is different from Edge::intersect, in that it intersects lines, not line segments.
1427 bool intersect(const Edge& a, const Edge& b, SkPoint* point) {
1428 double denom = a.fDX * b.fDY - a.fDY * b.fDX;
1429 if (denom == 0.0) {
1430 return false;
1431 }
1432 double scale = 1.0f / denom;
1433 point->fX = SkDoubleToScalar((b.fDX * a.fC - a.fDX * b.fC) * scale);
1434 point->fY = SkDoubleToScalar((b.fDY * a.fC - a.fDY * b.fC) * scale);
1435 round(point);
1436 return true;
1437 }
1438
1439 void get_edge_normal(const Edge* e, SkVector* normal) {
1440 normal->setNormalize(SkDoubleToScalar(e->fDX) * e->fWinding,
1441 SkDoubleToScalar(e->fDY) * e->fWinding);
1442 }
1443
1444 // Stage 5c: detect and remove "pointy" vertices whose edge normals point in opp osite directions
1445 // and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1446 // invert on stroking.
1447
1448 void simplify_boundary(EdgeList* boundary, Comparator& c, SkChunkAlloc& alloc) {
1449 Edge* prevEdge = boundary->fTail;
1450 SkVector prevNormal;
1451 get_edge_normal(prevEdge, &prevNormal);
1452 for (Edge* e = boundary->fHead; e != nullptr;) {
1453 Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBot tom;
1454 Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1455 double dist = e->dist(prev->fPoint);
1456 SkVector normal;
1457 get_edge_normal(e, &normal);
1458 float denom = 0.25f * static_cast<float>(e->fDX * e->fDX + e->fDY * e->f DY);
1459 if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
1460 Edge* join = new_edge(prev, next, alloc, c);
1461 insert_edge(join, e, boundary);
1462 remove_edge(prevEdge, boundary);
1463 remove_edge(e, boundary);
1464 if (join->fLeft && join->fRight) {
1465 prevEdge = join->fLeft;
1466 e = join;
1467 } else {
1468 prevEdge = boundary->fTail;
1469 e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1470 }
1471 get_edge_normal(prevEdge, &prevNormal);
1472 } else {
1473 prevEdge = e;
1474 prevNormal = normal;
1475 e = e->fRight;
1476 }
1477 }
1478 }
1479
1480 // Stage 5d: Displace edges by half a pixel inward and outward along their norma ls. Intersect to
1481 // find new vertices, and set zero alpha on the exterior and one alpha on the in terior. Build a
1482 // new antialiased mesh from those vertices.
1483
1484 void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, Sk ChunkAlloc& alloc) {
1485 EdgeList outerContour;
1486 Edge* prevEdge = boundary->fTail;
1487 float radius = 0.5f;
1488 double offset = radius * sqrt(prevEdge->fDX * prevEdge->fDX + prevEdge->fDY * prevEdge->fDY)
1489 * prevEdge->fWinding;
1490 Edge prevInner(prevEdge->fTop, prevEdge->fBottom, prevEdge->fWinding);
1491 prevInner.fC -= offset;
1492 Edge prevOuter(prevEdge->fTop, prevEdge->fBottom, prevEdge->fWinding);
1493 prevOuter.fC += offset;
1494 VertexList innerVertices;
1495 VertexList outerVertices;
1496 SkScalar innerCount = SK_Scalar1, outerCount = SK_Scalar1;
1497 for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
1498 double offset = radius * sqrt(e->fDX * e->fDX + e->fDY * e->fDY) * e->fW inding;
1499 Edge inner(e->fTop, e->fBottom, e->fWinding);
1500 inner.fC -= offset;
1501 Edge outer(e->fTop, e->fBottom, e->fWinding);
1502 outer.fC += offset;
1503 SkPoint innerPoint, outerPoint;
1504 if (intersect(prevInner, inner, &innerPoint) &&
1505 intersect(prevOuter, outer, &outerPoint)) {
1506 Vertex* innerVertex = ALLOC_NEW(Vertex, (innerPoint, 255), alloc);
1507 Vertex* outerVertex = ALLOC_NEW(Vertex, (outerPoint, 0), alloc);
1508 if (innerVertices.fTail && outerVertices.fTail) {
1509 Edge innerEdge(innerVertices.fTail, innerVertex, 1);
1510 Edge outerEdge(outerVertices.fTail, outerVertex, 1);
1511 SkVector innerNormal;
1512 get_edge_normal(&innerEdge, &innerNormal);
1513 SkVector outerNormal;
1514 get_edge_normal(&outerEdge, &outerNormal);
1515 SkVector normal;
1516 get_edge_normal(prevEdge, &normal);
1517 if (normal.dot(innerNormal) < 0) {
1518 innerPoint += innerVertices.fTail->fPoint * innerCount;
1519 innerCount++;
1520 innerPoint *= SkScalarInvert(innerCount);
1521 innerVertices.fTail->fPoint = innerVertex->fPoint = innerPoi nt;
1522 } else {
1523 innerCount = SK_Scalar1;
1524 }
1525 if (normal.dot(outerNormal) < 0) {
1526 outerPoint += outerVertices.fTail->fPoint * outerCount;
1527 outerCount++;
1528 outerPoint *= SkScalarInvert(outerCount);
1529 outerVertices.fTail->fPoint = outerVertex->fPoint = outerPoi nt;
1530 } else {
1531 outerCount = SK_Scalar1;
1532 }
1533 }
1534 innerVertices.append(innerVertex);
1535 outerVertices.append(outerVertex);
1536 prevEdge = e;
1537 }
1538 prevInner = inner;
1539 prevOuter = outer;
1540 }
1541 innerVertices.close();
1542 outerVertices.close();
1543
1544 Vertex* innerVertex = innerVertices.fHead;
1545 Vertex* outerVertex = outerVertices.fHead;
1546 // Alternate clockwise and counterclockwise polys, so the tesselator
1547 // doesn't cancel out the interior edges.
1548 if (!innerVertex || !outerVertex) {
1549 return;
1550 }
1551 do {
1552 connect(outerVertex->fNext, outerVertex, alloc, c);
1553 connect(innerVertex->fNext, innerVertex, alloc, c, 2);
1554 connect(innerVertex, outerVertex->fNext, alloc, c, 2);
1555 connect(outerVertex, innerVertex, alloc, c, 2);
1556 Vertex* innerNext = innerVertex->fNext;
1557 Vertex* outerNext = outerVertex->fNext;
1558 mesh->append(innerVertex);
1559 mesh->append(outerVertex);
1560 innerVertex = innerNext;
1561 outerVertex = outerNext;
1562 } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices. fHead);
1563 }
1564
1565 void extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, Sk ChunkAlloc& alloc) {
1566 bool down = is_boundary_start(e, fillType);
1567 while (e) {
1568 e->fWinding = down ? 1 : -1;
1569 Edge* next;
1570 boundary->append(e);
1571 if (down) {
1572 // Find outgoing edge, in clockwise order.
1573 if ((next = e->fNextEdgeAbove)) {
1574 down = false;
1575 } else if ((next = e->fBottom->fLastEdgeBelow)) {
1576 down = true;
1577 } else if ((next = e->fPrevEdgeAbove)) {
1578 down = false;
1579 }
1580 } else {
1581 // Find outgoing edge, in counter-clockwise order.
1582 if ((next = e->fPrevEdgeBelow)) {
1583 down = true;
1584 } else if ((next = e->fTop->fFirstEdgeAbove)) {
1585 down = false;
1586 } else if ((next = e->fNextEdgeBelow)) {
1587 down = true;
1588 }
1589 }
1590 remove_edge_above(e);
1591 remove_edge_below(e);
1592 e = next;
1593 }
1594 }
1595
1596 // Stage 5b: Extract boundary edges.
1597
1598 EdgeList* extract_boundaries(Vertex* vertices, SkPath::FillType fillType, SkChun kAlloc& alloc) {
1599 LOG("extracting boundaries\n");
1600 vertices = remove_non_boundary_edges(vertices, fillType, alloc);
1601 EdgeList* boundaries = nullptr;
1602 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1603 while (v->fFirstEdgeBelow) {
1604 EdgeList* boundary = new_contour(&boundaries, alloc);
1605 extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc);
1606 }
1607 }
1608 return boundaries;
1609 }
1610
1301 // This is a driver function which calls stages 2-5 in turn. 1611 // This is a driver function which calls stages 2-5 in turn.
1302 1612
1303 Poly* contours_to_polys(Vertex** contours, int contourCnt, const SkRect& pathBou nds, 1613 Vertex* contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
1304 SkChunkAlloc& alloc) { 1614 Comparator& c, SkChunkAlloc& alloc) {
1305 Comparator c;
1306 if (pathBounds.width() > pathBounds.height()) {
1307 c.sweep_lt = sweep_lt_horiz;
1308 c.sweep_gt = sweep_gt_horiz;
1309 } else {
1310 c.sweep_lt = sweep_lt_vert;
1311 c.sweep_gt = sweep_gt_vert;
1312 }
1313 #if LOGGING_ENABLED 1615 #if LOGGING_ENABLED
1314 for (int i = 0; i < contourCnt; ++i) { 1616 for (int i = 0; i < contourCnt; ++i) {
1315 Vertex* v = contours[i]; 1617 Vertex* v = contours[i];
1316 SkASSERT(v); 1618 SkASSERT(v);
1317 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1619 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1318 for (v = v->fNext; v != contours[i]; v = v->fNext) { 1620 for (v = v->fNext; v != contours[i]; v = v->fNext) {
1319 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1621 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1320 } 1622 }
1321 } 1623 }
1322 #endif 1624 #endif
1323 sanitize_contours(contours, contourCnt); 1625 sanitize_contours(contours, contourCnt, antialias);
1324 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); 1626 return build_edges(contours, contourCnt, c, alloc);
1325 if (!vertices) { 1627 }
1628
1629 Poly* mesh_to_polys(Vertex** vertices, SkPath::FillType fillType, Comparator& c,
1630 SkChunkAlloc& alloc) {
1631 if (!vertices || !*vertices) {
1326 return nullptr; 1632 return nullptr;
1327 } 1633 }
1328 1634
1329 // Sort vertices in Y (secondarily in X). 1635 // Sort vertices in Y (secondarily in X).
1330 merge_sort(&vertices, c); 1636 merge_sort(vertices, c);
1331 merge_coincident_vertices(&vertices, c, alloc); 1637 merge_coincident_vertices(vertices, c, alloc);
1332 #if LOGGING_ENABLED 1638 #if LOGGING_ENABLED
1333 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1639 for (Vertex* v = *vertices; v != nullptr; v = v->fNext) {
1334 static float gID = 0.0f; 1640 static float gID = 0.0f;
1335 v->fID = gID++; 1641 v->fID = gID++;
1336 } 1642 }
1337 #endif 1643 #endif
1338 simplify(vertices, c, alloc); 1644 simplify(*vertices, c, alloc);
1339 return tessellate(vertices, alloc); 1645 return tessellate(*vertices, alloc);
1646 }
1647
1648 Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fill Type,
1649 const SkRect& pathBounds, bool antialias,
1650 SkChunkAlloc& alloc) {
1651 Comparator c;
1652 if (pathBounds.width() > pathBounds.height()) {
1653 c.sweep_lt = sweep_lt_horiz;
1654 c.sweep_gt = sweep_gt_horiz;
1655 } else {
1656 c.sweep_lt = sweep_lt_vert;
1657 c.sweep_gt = sweep_gt_vert;
1658 }
1659 Vertex* mesh = contours_to_mesh(contours, contourCnt, antialias, c, alloc);
1660 Poly* polys = mesh_to_polys(&mesh, fillType, c, alloc);
1661 if (antialias) {
1662 EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc);
1663 VertexList aaMesh;
1664 for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = bo undary->fNext) {
1665 simplify_boundary(boundary, c, alloc);
1666 if (boundary->fCount > 2) {
1667 boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
1668 }
1669 }
1670 return mesh_to_polys(&aaMesh.fHead, SkPath::kWinding_FillType, c, alloc) ;
1671 }
1672 return polys;
1673 }
1674
1675 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
1676 void* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1677 void* data) {
1678 for (Poly* poly = polys; poly; poly = poly->fNext) {
1679 if (apply_fill_type(fillType, poly)) {
1680 data = poly->emit(aaParams, data);
1681 }
1682 }
1683 return data;
1340 } 1684 }
1341 1685
1342 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds, 1686 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1343 int contourCnt, SkChunkAlloc& alloc, bool* isLinear) { 1687 int contourCnt, SkChunkAlloc& alloc, bool antialias, bool* i sLinear) {
1344 SkPath::FillType fillType = path.getFillType(); 1688 SkPath::FillType fillType = path.getFillType();
1345 if (SkPath::IsInverseFillType(fillType)) { 1689 if (SkPath::IsInverseFillType(fillType)) {
1346 contourCnt++; 1690 contourCnt++;
1347 } 1691 }
1348 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]); 1692 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
1349 1693
1350 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea r); 1694 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea r);
1351 return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc ); 1695 return contours_to_polys(contours.get(), contourCnt, path.getFillType(), pat h.getBounds(),
1696 antialias, alloc);
1352 } 1697 }
1353 1698
1354 void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt, 1699 void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt,
1355 int* sizeEstimate) { 1700 int* sizeEstimate) {
1356 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); 1701 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance);
1357 if (maxPts <= 0) { 1702 if (maxPts <= 0) {
1358 *contourCnt = 0; 1703 *contourCnt = 0;
1359 return; 1704 return;
1360 } 1705 }
1361 if (maxPts > ((int)SK_MaxU16 + 1)) { 1706 if (maxPts > ((int)SK_MaxU16 + 1)) {
1362 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); 1707 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1363 *contourCnt = 0; 1708 *contourCnt = 0;
1364 return; 1709 return;
1365 } 1710 }
1366 // For the initial size of the chunk allocator, estimate based on the point count: 1711 // For the initial size of the chunk allocator, estimate based on the point count:
1367 // one vertex per point for the initial passes, plus two for the vertices in the 1712 // one vertex per point for the initial passes, plus two for the vertices in the
1368 // resulting Polys, since the same point may end up in two Polys. Assume mi nimal 1713 // resulting Polys, since the same point may end up in two Polys. Assume mi nimal
1369 // connectivity of one Edge per Vertex (will grow for intersections). 1714 // connectivity of one Edge per Vertex (will grow for intersections).
1370 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); 1715 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge));
1371 } 1716 }
1372 1717
1373 int count_points(Poly* polys, SkPath::FillType fillType) { 1718 int count_points(Poly* polys, SkPath::FillType fillType) {
1374 int count = 0; 1719 int count = 0;
1375 for (Poly* poly = polys; poly; poly = poly->fNext) { 1720 for (Poly* poly = polys; poly; poly = poly->fNext) {
1376 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { 1721 if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
1377 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); 1722 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1378 } 1723 }
1379 } 1724 }
1380 return count; 1725 return count;
1381 } 1726 }
1382 1727
1383 } // namespace 1728 } // namespace
1384 1729
1385 namespace GrTessellator { 1730 namespace GrTessellator {
1386 1731
1387 // Stage 6: Triangulate the monotone polygons into a vertex buffer. 1732 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
1388 1733
1389 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds, 1734 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1390 VertexAllocator* vertexAllocator, bool* isLinear) { 1735 VertexAllocator* vertexAllocator, bool antialias, const GrCo lor& color,
1736 bool canTweakAlphaForCoverage, bool* isLinear) {
1391 int contourCnt; 1737 int contourCnt;
1392 int sizeEstimate; 1738 int sizeEstimate;
1393 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate); 1739 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate);
1394 if (contourCnt <= 0) { 1740 if (contourCnt <= 0) {
1395 *isLinear = true; 1741 *isLinear = true;
1396 return 0; 1742 return 0;
1397 } 1743 }
1398 SkChunkAlloc alloc(sizeEstimate); 1744 SkChunkAlloc alloc(sizeEstimate);
1399 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, isLinear); 1745 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1746 isLinear);
1400 SkPath::FillType fillType = path.getFillType(); 1747 SkPath::FillType fillType = path.getFillType();
1401 int count = count_points(polys, fillType); 1748 int count = count_points(polys, fillType);
1402 if (0 == count) { 1749 if (0 == count) {
1403 return 0; 1750 return 0;
1404 } 1751 }
1405 1752
1406 SkPoint* verts = vertexAllocator->lock(count); 1753 void* verts = vertexAllocator->lock(count);
1407 if (!verts) { 1754 if (!verts) {
1408 SkDebugf("Could not allocate vertices\n"); 1755 SkDebugf("Could not allocate vertices\n");
1409 return 0; 1756 return 0;
1410 } 1757 }
1411 SkPoint* end = verts; 1758
1412 for (Poly* poly = polys; poly; poly = poly->fNext) { 1759 LOG("emitting %d verts\n", count);
1413 if (apply_fill_type(fillType, poly->fWinding)) { 1760 AAParams aaParams;
1414 end = poly->emit(end); 1761 aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1415 } 1762 aaParams.fColor = color;
1416 } 1763
1417 int actualCount = static_cast<int>(end - verts); 1764 void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : null ptr, verts);
1418 LOG("actual count: %d\n", actualCount); 1765 int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast <uint8_t*>(verts))
1766 / vertexAllocator->stride());
1419 SkASSERT(actualCount <= count); 1767 SkASSERT(actualCount <= count);
1420 vertexAllocator->unlock(actualCount); 1768 vertexAllocator->unlock(actualCount);
1421 return actualCount; 1769 return actualCount;
1422 } 1770 }
1423 1771
1424 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou nds, 1772 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou nds,
1425 GrTessellator::WindingVertex** verts) { 1773 GrTessellator::WindingVertex** verts) {
1426 int contourCnt; 1774 int contourCnt;
1427 int sizeEstimate; 1775 int sizeEstimate;
1428 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate); 1776 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate);
1429 if (contourCnt <= 0) { 1777 if (contourCnt <= 0) {
1430 return 0; 1778 return 0;
1431 } 1779 }
1432 SkChunkAlloc alloc(sizeEstimate); 1780 SkChunkAlloc alloc(sizeEstimate);
1433 bool isLinear; 1781 bool isLinear;
1434 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, &isLinear); 1782 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
1435 SkPath::FillType fillType = path.getFillType(); 1783 SkPath::FillType fillType = path.getFillType();
1436 int count = count_points(polys, fillType); 1784 int count = count_points(polys, fillType);
1437 if (0 == count) { 1785 if (0 == count) {
1438 *verts = nullptr; 1786 *verts = nullptr;
1439 return 0; 1787 return 0;
1440 } 1788 }
1441 1789
1442 *verts = new GrTessellator::WindingVertex[count]; 1790 *verts = new GrTessellator::WindingVertex[count];
1443 GrTessellator::WindingVertex* vertsEnd = *verts; 1791 GrTessellator::WindingVertex* vertsEnd = *verts;
1444 SkPoint* points = new SkPoint[count]; 1792 SkPoint* points = new SkPoint[count];
1445 SkPoint* pointsEnd = points; 1793 SkPoint* pointsEnd = points;
1446 for (Poly* poly = polys; poly; poly = poly->fNext) { 1794 for (Poly* poly = polys; poly; poly = poly->fNext) {
1447 if (apply_fill_type(fillType, poly->fWinding)) { 1795 if (apply_fill_type(fillType, poly)) {
1448 SkPoint* start = pointsEnd; 1796 SkPoint* start = pointsEnd;
1449 pointsEnd = poly->emit(pointsEnd); 1797 pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
1450 while (start != pointsEnd) { 1798 while (start != pointsEnd) {
1451 vertsEnd->fPos = *start; 1799 vertsEnd->fPos = *start;
1452 vertsEnd->fWinding = poly->fWinding; 1800 vertsEnd->fWinding = poly->fWinding;
1453 ++start; 1801 ++start;
1454 ++vertsEnd; 1802 ++vertsEnd;
1455 } 1803 }
1456 } 1804 }
1457 } 1805 }
1458 int actualCount = static_cast<int>(vertsEnd - *verts); 1806 int actualCount = static_cast<int>(vertsEnd - *verts);
1459 SkASSERT(actualCount <= count); 1807 SkASSERT(actualCount <= count);
1460 SkASSERT(pointsEnd - points == actualCount); 1808 SkASSERT(pointsEnd - points == actualCount);
1461 delete[] points; 1809 delete[] points;
1462 return actualCount; 1810 return actualCount;
1463 } 1811 }
1464 1812
1465 } // namespace 1813 } // namespace
OLDNEW
« no previous file with comments | « src/gpu/GrTessellator.h ('k') | src/gpu/batches/GrTessellatingPathRenderer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698