GrTessellator.cpp revision 682580fb204b72925a48d1d6fe8c9c30fa53bb67
1e9709e831954c3427d5cb839e84221a177bfedebethannicholas/*
2e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Copyright 2015 Google Inc.
3e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
4e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Use of this source code is governed by a BSD-style license that can be
5e9709e831954c3427d5cb839e84221a177bfedebethannicholas * found in the LICENSE file.
6e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
7e9709e831954c3427d5cb839e84221a177bfedebethannicholas
8e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include "GrTessellator.h"
9e9709e831954c3427d5cb839e84221a177bfedebethannicholas
10e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include "GrPathUtils.h"
11e9709e831954c3427d5cb839e84221a177bfedebethannicholas
126599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkChunkAlloc.h"
136599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkGeometry.h"
146599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkPath.h"
15e9709e831954c3427d5cb839e84221a177bfedebethannicholas
16e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include <stdio.h>
17e9709e831954c3427d5cb839e84221a177bfedebethannicholas
18e9709e831954c3427d5cb839e84221a177bfedebethannicholas/*
19682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco * There are six stages to the algorithm:
20e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
21e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
22e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 2) Build a mesh of edges connecting the vertices (build_edges()).
23e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
24e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
25e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
26e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
27e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
28e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
29e9709e831954c3427d5cb839e84221a177bfedebethannicholas * of vertices (and the necessity of inserting new vertices on intersection).
30e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
31e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Stages (4) and (5) use an active edge list, which a list of all edges for which the
32e9709e831954c3427d5cb839e84221a177bfedebethannicholas * sweep line has crossed the top vertex, but not the bottom vertex.  It's sorted
33e9709e831954c3427d5cb839e84221a177bfedebethannicholas * left-to-right based on the point where both edges are active (when both top vertices
34e9709e831954c3427d5cb839e84221a177bfedebethannicholas * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
35e9709e831954c3427d5cb839e84221a177bfedebethannicholas * (shared), it's sorted based on the last point where both edges are active, so the
36e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "upper" bottom vertex.
37e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
38e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The most complex step is the simplification (4). It's based on the Bentley-Ottman
39e9709e831954c3427d5cb839e84221a177bfedebethannicholas * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
40e9709e831954c3427d5cb839e84221a177bfedebethannicholas * not exact and may violate the mesh topology or active edge list ordering. We
41e9709e831954c3427d5cb839e84221a177bfedebethannicholas * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
42e9709e831954c3427d5cb839e84221a177bfedebethannicholas * points. This occurs in three ways:
43e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
44e9709e831954c3427d5cb839e84221a177bfedebethannicholas * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
45e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    neighbouring edges at the top or bottom vertex. This is handled by merging the
46e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    edges (merge_collinear_edges()).
47e9709e831954c3427d5cb839e84221a177bfedebethannicholas * B) Intersections may cause an edge to violate the left-to-right ordering of the
48e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    active edge list. This is handled by splitting the neighbour edge on the
49e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    intersected vertex (cleanup_active_edges()).
50e9709e831954c3427d5cb839e84221a177bfedebethannicholas * C) Shortening an edge may cause an active edge to become inactive or an inactive edge
51e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    to become active. This is handled by removing or inserting the edge in the active
52e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    edge list (fix_active_state()).
53e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
54e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
55e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
56e9709e831954c3427d5cb839e84221a177bfedebethannicholas * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
57e9709e831954c3427d5cb839e84221a177bfedebethannicholas * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
58e9709e831954c3427d5cb839e84221a177bfedebethannicholas * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
59e9709e831954c3427d5cb839e84221a177bfedebethannicholas * insertions and removals was greater than the cost of infrequent O(N) lookups with the
60e9709e831954c3427d5cb839e84221a177bfedebethannicholas * linked list implementation. With the latter, all removals are O(1), and most insertions
61e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are O(1), since we know the adjacent edge in the active edge list based on the topology.
62e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
63e9709e831954c3427d5cb839e84221a177bfedebethannicholas * frequent. There may be other data structures worth investigating, however.
64e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
65e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
66e9709e831954c3427d5cb839e84221a177bfedebethannicholas * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
67e9709e831954c3427d5cb839e84221a177bfedebethannicholas * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
68e9709e831954c3427d5cb839e84221a177bfedebethannicholas * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
69e9709e831954c3427d5cb839e84221a177bfedebethannicholas * that the "left" and "right" orientation in the code remains correct (edges to the left are
70e9709e831954c3427d5cb839e84221a177bfedebethannicholas * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
71e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degrees counterclockwise, rather that transposing.
72e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
73e9709e831954c3427d5cb839e84221a177bfedebethannicholas
74e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOGGING_ENABLED 0
75e9709e831954c3427d5cb839e84221a177bfedebethannicholas
76e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
77e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG printf
78e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
79e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG(...)
80e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
81e9709e831954c3427d5cb839e84221a177bfedebethannicholas
82e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args
83e9709e831954c3427d5cb839e84221a177bfedebethannicholas
84e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace {
85e9709e831954c3427d5cb839e84221a177bfedebethannicholas
86e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex;
87e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge;
88e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly;
89e9709e831954c3427d5cb839e84221a177bfedebethannicholas
90e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next>
91e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_insert(T* t, T* prev, T* next, T** head, T** tail) {
92e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Prev = prev;
93e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Next = next;
94e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
95e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->*Next = t;
96e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (head) {
97e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = t;
98e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
99e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (next) {
100e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next->*Prev = t;
101e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (tail) {
102e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *tail = t;
103e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
104e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
105e9709e831954c3427d5cb839e84221a177bfedebethannicholas
106e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next>
107e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_remove(T* t, T** head, T** tail) {
108e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (t->*Prev) {
109e9709e831954c3427d5cb839e84221a177bfedebethannicholas        t->*Prev->*Next = t->*Next;
110e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (head) {
111e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = t->*Next;
112e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
113e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (t->*Next) {
114e9709e831954c3427d5cb839e84221a177bfedebethannicholas        t->*Next->*Prev = t->*Prev;
115e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (tail) {
116e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *tail = t->*Prev;
117e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
118e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Prev = t->*Next = nullptr;
119e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
120e9709e831954c3427d5cb839e84221a177bfedebethannicholas
121e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
122e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices are used in three ways: first, the path contours are converted into a
123e9709e831954c3427d5cb839e84221a177bfedebethannicholas * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
124e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
125e9709e831954c3427d5cb839e84221a177bfedebethannicholas * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
126e9709e831954c3427d5cb839e84221a177bfedebethannicholas * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
127e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
128e9709e831954c3427d5cb839e84221a177bfedebethannicholas * an individual Vertex from the path mesh may belong to multiple
129e9709e831954c3427d5cb839e84221a177bfedebethannicholas * MonotonePolys, so the original Vertices cannot be re-used.
130e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
131e9709e831954c3427d5cb839e84221a177bfedebethannicholas
132e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex {
133682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco  Vertex(const SkPoint& point)
134e9709e831954c3427d5cb839e84221a177bfedebethannicholas    : fPoint(point), fPrev(nullptr), fNext(nullptr)
135e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
136e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
137e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fProcessed(false)
138e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
139e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fID (-1.0f)
140e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
141e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {}
142e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint fPoint;           // Vertex position
143e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fPrev;            // Linked list of contours, then Y-sorted vertices.
144e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fNext;            // "
145e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fFirstEdgeAbove;  // Linked list of edges above this vertex.
146e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fLastEdgeAbove;   // "
147e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fFirstEdgeBelow;  // Linked list of edges below this vertex.
148e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fLastEdgeBelow;   // "
149e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool    fProcessed;       // Has this vertex been seen in simplify()?
150e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
151e9709e831954c3427d5cb839e84221a177bfedebethannicholas    float   fID;              // Identifier used for logging.
152e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
153e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
154e9709e831954c3427d5cb839e84221a177bfedebethannicholas
155e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
156e9709e831954c3427d5cb839e84221a177bfedebethannicholas
157e9709e831954c3427d5cb839e84221a177bfedebethannicholastypedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
158e9709e831954c3427d5cb839e84221a177bfedebethannicholas
159e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Comparator {
160e9709e831954c3427d5cb839e84221a177bfedebethannicholas    CompareFunc sweep_lt;
161e9709e831954c3427d5cb839e84221a177bfedebethannicholas    CompareFunc sweep_gt;
162e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
163e9709e831954c3427d5cb839e84221a177bfedebethannicholas
164e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
165e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
166e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
167e9709e831954c3427d5cb839e84221a177bfedebethannicholas
168e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
169e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
170e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
171e9709e831954c3427d5cb839e84221a177bfedebethannicholas
172e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
173e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
174e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
175e9709e831954c3427d5cb839e84221a177bfedebethannicholas
176e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
177e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
178e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
179e9709e831954c3427d5cb839e84221a177bfedebethannicholas
180682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancoinline SkPoint* emit_vertex(Vertex* v, SkPoint* data) {
181682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    *data++ = v->fPoint;
182682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    return data;
183e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
184e9709e831954c3427d5cb839e84221a177bfedebethannicholas
185682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancoSkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) {
186682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco#if WIREFRAME
187682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v0, data);
188682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v1, data);
189682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v1, data);
190682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v2, data);
191682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v2, data);
192682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v0, data);
193e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
194682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v0, data);
195682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v1, data);
196682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    data = emit_vertex(v2, data);
197e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
198e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return data;
199e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
200e9709e831954c3427d5cb839e84221a177bfedebethannicholas
201682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancostruct EdgeList {
202682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    EdgeList() : fHead(nullptr), fTail(nullptr) {}
203682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Edge* fHead;
204682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Edge* fTail;
205682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco};
206682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco
207e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancostruct VertexList {
208e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList() : fHead(nullptr), fTail(nullptr) {}
209e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fHead;
210e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fTail;
211e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void insert(Vertex* v, Vertex* prev, Vertex* next) {
212e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
213e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
214e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void append(Vertex* v) {
215e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, fTail, nullptr);
216e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
217e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void prepend(Vertex* v) {
218e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, nullptr, fHead);
219e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
220e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco};
221e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco
222e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
223e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
224e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
225e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
226e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g.,
227e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
228e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division
229e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but
230e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case.
231e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
232e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic
233e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
234e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being
235e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
236e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
237e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file).
238e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
239e9709e831954c3427d5cb839e84221a177bfedebethannicholas
240e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge {
241e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge(Vertex* top, Vertex* bottom, int winding)
242e9709e831954c3427d5cb839e84221a177bfedebethannicholas        : fWinding(winding)
243e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTop(top)
244e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fBottom(bottom)
245e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeft(nullptr)
246e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fRight(nullptr)
247e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeAbove(nullptr)
248e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeAbove(nullptr)
249e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeBelow(nullptr)
250e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeBelow(nullptr)
251e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeftPoly(nullptr)
252531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPoly(nullptr)
253531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyPrev(nullptr)
254531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyNext(nullptr)
255531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPolyPrev(nullptr)
25670f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fRightPolyNext(nullptr)
25770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInLeftPoly(false)
25870f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInRightPoly(false) {
259e9709e831954c3427d5cb839e84221a177bfedebethannicholas            recompute();
260e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
261e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int      fWinding;          // 1 == edge goes downward; -1 = edge goes upward.
262e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fTop;              // The top vertex in vertex-sort-order (sweep_lt).
263e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fBottom;           // The bottom vertex in vertex-sort-order.
264e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fLeft;             // The linked list of edges in the active edge list.
265e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fRight;            // "
266e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeAbove;    // The linked list of edges in the bottom Vertex's "edges above".
267e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeAbove;    // "
268e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeBelow;    // The linked list of edges in the top Vertex's "edges below".
269e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeBelow;    // "
270e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fLeftPoly;         // The Poly to the left of this edge, if any.
271e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fRightPoly;        // The Poly to the right of this edge, if any.
272531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyPrev;
273531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyNext;
274531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyPrev;
275531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyNext;
27670f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInLeftPoly;
27770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInRightPoly;
278e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double   fDX;               // The line equation for this edge, in implicit form.
279e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double   fDY;               // fDY * x + fDX * y + fC = 0, for point (x, y) on the line.
280e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double   fC;
281e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double dist(const SkPoint& p) const {
282e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return fDY * p.fX - fDX * p.fY + fC;
283e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
284e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isRightOf(Vertex* v) const {
285e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return dist(v->fPoint) < 0.0;
286e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
287e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLeftOf(Vertex* v) const {
288e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return dist(v->fPoint) > 0.0;
289e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
290e9709e831954c3427d5cb839e84221a177bfedebethannicholas    void recompute() {
291e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
292e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
293e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
294e9709e831954c3427d5cb839e84221a177bfedebethannicholas             static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
295e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
296e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool intersect(const Edge& other, SkPoint* p) {
297e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("intersecting %g -> %g with %g -> %g\n",
298e9709e831954c3427d5cb839e84221a177bfedebethannicholas               fTop->fID, fBottom->fID,
299e9709e831954c3427d5cb839e84221a177bfedebethannicholas               other.fTop->fID, other.fBottom->fID);
300e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fTop == other.fTop || fBottom == other.fBottom) {
301e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
302e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
303e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double denom = fDX * other.fDY - fDY * other.fDX;
304e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom == 0.0) {
305e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
306e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
307e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
308e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
309e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double sNumer = dy * other.fDX - dx * other.fDY;
310e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double tNumer = dy * fDX - dx * fDY;
311e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
312e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // This saves us doing the divide below unless absolutely necessary.
313e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
314e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
315e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
316e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
317e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double s = sNumer / denom;
318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(s >= 0.0 && s <= 1.0);
319e9709e831954c3427d5cb839e84221a177bfedebethannicholas        p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
320e9709e831954c3427d5cb839e84221a177bfedebethannicholas        p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
321e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return true;
322e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
323682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    bool isActive(EdgeList* activeEdges) const {
324682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
325e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
326e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
327e9709e831954c3427d5cb839e84221a177bfedebethannicholas
328e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
329e9709e831954c3427d5cb839e84221a177bfedebethannicholas
330e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly {
331531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly(Vertex* v, int winding)
332531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        : fFirstVertex(v)
333531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fWinding(winding)
334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fHead(nullptr)
335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTail(nullptr)
336e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNext(nullptr)
337e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPartner(nullptr)
338e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fCount(0)
339e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {
340e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
341e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static int gID = 0;
342e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fID = gID++;
343e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("*** created Poly %d\n", fID);
344e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
345e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
346531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    typedef enum { kLeft_Side, kRight_Side } Side;
347e9709e831954c3427d5cb839e84221a177bfedebethannicholas    struct MonotonePoly {
348531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        MonotonePoly(Edge* edge, Side side)
349531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            : fSide(side)
350531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fFirstEdge(nullptr)
351531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fLastEdge(nullptr)
352e9709e831954c3427d5cb839e84221a177bfedebethannicholas            , fPrev(nullptr)
353531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fNext(nullptr) {
354531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            this->addEdge(edge);
355531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        }
356e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Side          fSide;
357531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fFirstEdge;
358531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fLastEdge;
359e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fPrev;
360e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fNext;
361531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        void addEdge(Edge* edge) {
362e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            if (fSide == kRight_Side) {
363212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInRightPoly);
364531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
365531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
36670f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInRightPoly = true;
367e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
368212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInLeftPoly);
369531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
370531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
37170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInLeftPoly = true;
372e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
373e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
374e9709e831954c3427d5cb839e84221a177bfedebethannicholas
375682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        SkPoint* emit(SkPoint* data) {
376531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Edge* e = fFirstEdge;
377531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            e->fTop->fPrev = e->fTop->fNext = nullptr;
378531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            VertexList vertices;
379531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            vertices.append(e->fTop);
380531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (e != nullptr) {
381531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                e->fBottom->fPrev = e->fBottom->fNext = nullptr;
382531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (kRight_Side == fSide) {
383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.append(e->fBottom);
384531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fRightPolyNext;
385531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                } else {
386531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.prepend(e->fBottom);
387531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fLeftPolyNext;
388531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                }
389531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            }
390531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Vertex* first = vertices.fHead;
391e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* v = first->fNext;
392531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (v != vertices.fTail) {
393e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(v && v->fPrev && v->fNext);
394e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* prev = v->fPrev;
395e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* curr = v;
396e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* next = v->fNext;
397e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
398e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
399e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
400e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
401e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (ax * by - ay * bx >= 0.0) {
402682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                    data = emit_triangle(prev, curr, next, data);
403e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fPrev->fNext = v->fNext;
404e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fNext->fPrev = v->fPrev;
405e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (v->fPrev == first) {
406e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fNext;
407e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    } else {
408e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fPrev;
409e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
410e9709e831954c3427d5cb839e84221a177bfedebethannicholas                } else {
411e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v = v->fNext;
412e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
413e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
414e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
415e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
416e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
417531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly* addEdge(Edge* e, Side side, SkChunkAlloc& alloc) {
41870f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        LOG("addEdge (%g -> %g) to poly %d, %s side\n",
41970f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco               e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
420e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* partner = fPartner;
421e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* poly = this;
422212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        if (side == kRight_Side) {
423212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInRightPoly) {
424212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
425212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
426212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        } else {
427212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInLeftPoly) {
428212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
429212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
430212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        }
431e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (partner) {
432e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fPartner = partner->fPartner = nullptr;
433e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
434531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        if (!fTail) {
435531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fHead = fTail = ALLOC_NEW(MonotonePoly, (e, side), alloc);
436531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount += 2;
43793e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco        } else if (e->fBottom == fTail->fLastEdge->fBottom) {
43893e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco            return poly;
439531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else if (side == fTail->fSide) {
440531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
441531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
442531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else {
443531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            e = ALLOC_NEW(Edge, (fTail->fLastEdge->fBottom, e->fBottom, 1), alloc);
444531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
445531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
446e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (partner) {
447531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                partner->addEdge(e, side, alloc);
448e9709e831954c3427d5cb839e84221a177bfedebethannicholas                poly = partner;
449e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
450531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc);
451531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                m->fPrev = fTail;
452531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail->fNext = m;
453531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail = m;
454e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
455e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
456e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return poly;
457e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
458682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    SkPoint* emit(SkPoint *data) {
459e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fCount < 3) {
460e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
461e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
462e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("emit() %d, size %d\n", fID, fCount);
463e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
464682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            data = m->emit(data);
465e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
466e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return data;
467e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
468531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
469531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* fFirstVertex;
470e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fWinding;
471e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fHead;
472e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fTail;
473e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fNext;
474e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fPartner;
475e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fCount;
476e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
477e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fID;
478e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
479e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
480e9709e831954c3427d5cb839e84221a177bfedebethannicholas
481e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
482e9709e831954c3427d5cb839e84221a177bfedebethannicholas
483e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) {
484e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a == b;
485e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
486e9709e831954c3427d5cb839e84221a177bfedebethannicholas
487e9709e831954c3427d5cb839e84221a177bfedebethannicholasPoly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
488531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc);
489e9709e831954c3427d5cb839e84221a177bfedebethannicholas    poly->fNext = *head;
490e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = poly;
491e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return poly;
492e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
493e9709e831954c3427d5cb839e84221a177bfedebethannicholas
494e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
495e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                SkChunkAlloc& alloc) {
496682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
497e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
498e9709e831954c3427d5cb839e84221a177bfedebethannicholas    static float gID = 0.0f;
499e9709e831954c3427d5cb839e84221a177bfedebethannicholas    v->fID = gID++;
500e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
501e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
502e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = v;
503e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fPrev = prev;
504e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
505e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = v;
506e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
507e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return v;
508e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
509e9709e831954c3427d5cb839e84221a177bfedebethannicholas
510e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0,
511e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p1,
512e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p2,
513e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkScalar tolSqd,
514e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex* prev,
515e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex** head,
516e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  int pointsLeft,
517e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkChunkAlloc& alloc) {
518e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
519e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
520e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p2, prev, head, alloc);
521e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
522e9709e831954c3427d5cb839e84221a177bfedebethannicholas
523e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
524e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
525e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
526e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
527e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
528e9709e831954c3427d5cb839e84221a177bfedebethannicholas
529e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
530e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
531e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
532e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
533e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
534e9709e831954c3427d5cb839e84221a177bfedebethannicholas
535e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0,
536e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p1,
537e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p2,
538e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p3,
539e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkScalar tolSqd,
540e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex* prev,
541e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex** head,
542e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              int pointsLeft,
543e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkChunkAlloc& alloc) {
544e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
545e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
546e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
547e9709e831954c3427d5cb839e84221a177bfedebethannicholas        !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
548e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p3, prev, head, alloc);
549e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
550e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
551e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
552e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
553e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
554e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
555e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r[] = {
556e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
557e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
558e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
559e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
560e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
561e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
562e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
563e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
564e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
565e9709e831954c3427d5cb839e84221a177bfedebethannicholas
566e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
567e9709e831954c3427d5cb839e84221a177bfedebethannicholas
568e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
569e9709e831954c3427d5cb839e84221a177bfedebethannicholas                      Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) {
570e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar toleranceSqd = tolerance * tolerance;
571e9709e831954c3427d5cb839e84221a177bfedebethannicholas
572e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint pts[4];
573e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool done = false;
574e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *isLinear = true;
575e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::Iter iter(path, false);
576e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
577e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* head = nullptr;
578e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (path.isInverseFillType()) {
579e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPoint quad[4];
580e9709e831954c3427d5cb839e84221a177bfedebethannicholas        clipBounds.toQuad(quad);
581682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        for (int i = 3; i >= 0; i--) {
582e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = append_point_to_contour(quad[i], prev, &head, alloc);
583e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
584e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head->fPrev = prev;
585e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = head;
586e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contours++ = head;
587e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head = prev = nullptr;
588e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
589e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoConicToQuads converter;
590e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (!done) {
591e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPath::Verb verb = iter.next(pts);
592e9709e831954c3427d5cb839e84221a177bfedebethannicholas        switch (verb) {
593e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kConic_Verb: {
594e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkScalar weight = iter.conicWeight();
595e9709e831954c3427d5cb839e84221a177bfedebethannicholas                const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
596e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (int i = 0; i < converter.countQuads(); ++i) {
597e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
598e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
599e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                     toleranceSqd, prev, &head, pointsLeft, alloc);
600e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    quadPts += 2;
601e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
602e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
603e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
604e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
605e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kMove_Verb:
606e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
607e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
608e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
609e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
610e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
611e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
612e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[0], prev, &head, alloc);
613e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
614e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kLine_Verb: {
615e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[1], prev, &head, alloc);
616e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
617e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
618e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kQuad_Verb: {
619e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
620e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
621e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                 &head, pointsLeft, alloc);
622e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
623e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
624e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
625e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kCubic_Verb: {
626e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
627e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
628e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                toleranceSqd, prev, &head, pointsLeft, alloc);
629e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
630e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
631e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
632e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kClose_Verb:
633e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
634e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
635e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
636e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
637e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
638e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
639e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
640e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kDone_Verb:
641e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
642e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
643e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
644e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
645e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
646e9709e831954c3427d5cb839e84221a177bfedebethannicholas                done = true;
647e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
648e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
649e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
650e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
651e9709e831954c3427d5cb839e84221a177bfedebethannicholas
652682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancoinline bool apply_fill_type(SkPath::FillType fillType, int winding) {
653e9709e831954c3427d5cb839e84221a177bfedebethannicholas    switch (fillType) {
654e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kWinding_FillType:
655e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return winding != 0;
656e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kEvenOdd_FillType:
657e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) != 0;
658e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseWinding_FillType:
659682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            return winding == 1;
660e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseEvenOdd_FillType:
661e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) == 1;
662e9709e831954c3427d5cb839e84221a177bfedebethannicholas        default:
663e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkASSERT(false);
664e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
665e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
666e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
667e9709e831954c3427d5cb839e84221a177bfedebethannicholas
668682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancoEdge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c) {
669682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
670e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = winding < 0 ? next : prev;
671e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = winding < 0 ? prev : next;
672e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
673e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
674e9709e831954c3427d5cb839e84221a177bfedebethannicholas
675e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) {
676e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
677682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    SkASSERT(edge->isActive(edges));
678682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &edges->fHead, &edges->fTail);
679e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
680e9709e831954c3427d5cb839e84221a177bfedebethannicholas
681e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
682e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
683682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    SkASSERT(!edge->isActive(edges));
684e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = prev ? prev->fRight : edges->fHead;
685682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &edges->fHead, &edges->fTail);
686e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
687e9709e831954c3427d5cb839e84221a177bfedebethannicholas
688e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
689e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (v->fFirstEdgeAbove) {
690e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *left = v->fFirstEdgeAbove->fLeft;
691e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *right = v->fLastEdgeAbove->fRight;
692e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
693e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
694e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = nullptr;
695e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev;
696e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
697e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (prev->isLeftOf(v)) {
698e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
699e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
700e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next = prev;
701e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
702e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
703e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
704e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
705e9709e831954c3427d5cb839e84221a177bfedebethannicholas
706e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
707e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
708e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
709e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = edges->fHead; next != nullptr; next = next->fRight) {
710e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
711e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
712e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
713e9709e831954c3427d5cb839e84221a177bfedebethannicholas             next->isRightOf(edge->fBottom)) ||
714e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
715e9709e831954c3427d5cb839e84221a177bfedebethannicholas             edge->isLeftOf(next->fBottom))) {
716e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
717e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
718e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
719e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
720e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
721e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
722e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
723e9709e831954c3427d5cb839e84221a177bfedebethannicholas
724e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
725682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    if (edge->isActive(activeEdges)) {
726e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
727e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(edge, activeEdges);
728e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
729e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
730e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* left;
731e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* right;
732e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(edge, activeEdges, c, &left, &right);
733e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge(edge, left, activeEdges);
734e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
735e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
736e9709e831954c3427d5cb839e84221a177bfedebethannicholas
737e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
738e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
739e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
740e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
741e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
742e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
743e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
744e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
745e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
746e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fTop)) {
747e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
748e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
749e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
750e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
751e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
752e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
753e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
754e9709e831954c3427d5cb839e84221a177bfedebethannicholas
755e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
756e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
757e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
758e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
759e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
760e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
761e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
762e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
763e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
764e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fBottom)) {
765e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
766e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
767e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
768e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
769e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
770e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
771e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
772e9709e831954c3427d5cb839e84221a177bfedebethannicholas
773e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) {
774e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
775e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fBottom->fID);
776e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
777e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
778e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
779e9709e831954c3427d5cb839e84221a177bfedebethannicholas
780e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) {
781e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
782e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID);
783e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
784e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
785e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
786e9709e831954c3427d5cb839e84221a177bfedebethannicholas
787e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) {
788e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fWinding != 0) {
789e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
790e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
791e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
792e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
793e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
794682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    if (edge->isActive(edges)) {
795e9709e831954c3427d5cb839e84221a177bfedebethannicholas        remove_edge(edge, edges);
796e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
797e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
798e9709e831954c3427d5cb839e84221a177bfedebethannicholas
799e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
800e9709e831954c3427d5cb839e84221a177bfedebethannicholas
801e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
802e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
803e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fTop = v;
804e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
805e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_below(edge, v, c);
806e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
807e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
808e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
809e9709e831954c3427d5cb839e84221a177bfedebethannicholas
810e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
811e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
812e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fBottom = v;
813e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
814e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_above(edge, v, c);
815e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
816e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
817e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
818e9709e831954c3427d5cb839e84221a177bfedebethannicholas
819e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
820e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
821e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
822e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
823e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
824e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
825e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
826e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding = 0;
827e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
828e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
829e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
830e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
831e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, other->fTop, activeEdges, c);
832e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
833e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
834e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
835e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(other, edge->fTop, activeEdges, c);
836e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
837e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
838e9709e831954c3427d5cb839e84221a177bfedebethannicholas
839e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
840e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
841e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
842e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
843e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
844e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
845e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
846e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding = 0;
847e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
848e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
849e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
850e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
851e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(other, edge->fBottom, activeEdges, c);
852e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
853e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
854e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
855e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, other->fBottom, activeEdges, c);
856e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
857e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
858e9709e831954c3427d5cb839e84221a177bfedebethannicholas
859e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
860e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
861e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
862e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
863e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
864e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
865e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
866e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
867e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
868e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
869e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
870e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
871e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
872e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
873e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
874e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
875e9709e831954c3427d5cb839e84221a177bfedebethannicholas
876e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc);
877e9709e831954c3427d5cb839e84221a177bfedebethannicholas
878e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
879e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = edge->fTop;
880e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = edge->fBottom;
881e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fLeft) {
882e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftTop = edge->fLeft->fTop;
883e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftBottom = edge->fLeft->fBottom;
884e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
885e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
886e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
887e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftTop, activeEdges, c, alloc);
888e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
889e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fLeft->isLeftOf(bottom)) {
890e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
891e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
892e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftBottom, activeEdges, c, alloc);
893e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
894e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
895e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fRight) {
896e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightTop = edge->fRight->fTop;
897e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightBottom = edge->fRight->fBottom;
898e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
899e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, top, activeEdges, c, alloc);
900e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
901e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightTop, activeEdges, c, alloc);
902e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
903e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fRight->isRightOf(bottom)) {
904e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, bottom, activeEdges, c, alloc);
905e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
906e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->isLeftOf(rightBottom)) {
907e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightBottom, activeEdges, c, alloc);
908e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
909e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
910e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
911e9709e831954c3427d5cb839e84221a177bfedebethannicholas
912e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
913e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
914e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID, edge->fBottom->fID,
915e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID, v->fPoint.fX, v->fPoint.fY);
916e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
917e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, v, activeEdges, c);
918e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
919e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
920e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
921e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc);
922e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_below(newEdge, v, c);
923e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_above(newEdge, edge->fBottom, c);
924e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
925e9709e831954c3427d5cb839e84221a177bfedebethannicholas        cleanup_active_edges(edge, activeEdges, c, alloc);
926e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fix_active_state(newEdge, activeEdges, c);
927e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_collinear_edges(newEdge, activeEdges, c);
928e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
929e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
930e9709e831954c3427d5cb839e84221a177bfedebethannicholas
931e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) {
932e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
933e9709e831954c3427d5cb839e84221a177bfedebethannicholas        src->fID, dst->fID);
934e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeAbove; edge;) {
935e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeAbove;
936e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, dst, nullptr, c);
937e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
938e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
939e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeBelow; edge;) {
940e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeBelow;
941e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, dst, nullptr, c);
942e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
943e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
944e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr);
945e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
946e9709e831954c3427d5cb839e84221a177bfedebethannicholas
947e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
948e9709e831954c3427d5cb839e84221a177bfedebethannicholas                               SkChunkAlloc& alloc) {
949e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint p;
950e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!edge || !other) {
951e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
952e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
953e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->intersect(*other, &p)) {
954e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v;
955e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
956e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
957e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fTop, activeEdges, c, alloc);
958e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fTop;
959e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
960e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fBottom, activeEdges, c, alloc);
961e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fBottom;
962e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
963e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fTop, activeEdges, c, alloc);
964e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fTop;
965e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
966e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fBottom, activeEdges, c, alloc);
967e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fBottom;
968e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
969e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* nextV = edge->fTop;
970e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(p, nextV->fPoint)) {
971e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fPrev;
972e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
973e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(nextV->fPoint, p)) {
974e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fNext;
975e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
976e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* prevV = nextV->fPrev;
977e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(prevV->fPoint, p)) {
978e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = prevV;
979e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else if (coincident(nextV->fPoint, p)) {
980e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = nextV;
981e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
982682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                v = ALLOC_NEW(Vertex, (p), alloc);
983e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
984e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
985e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
986e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
987e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fID = (nextV->fID + prevV->fID) * 0.5f;
988e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
989e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prevV;
990e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext = nextV;
991e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prevV->fNext = v;
992e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV->fPrev = v;
993e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
994e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, v, activeEdges, c, alloc);
995e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, v, activeEdges, c, alloc);
996e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
997e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return v;
998e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
999e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return nullptr;
1000e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1001e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1002682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancovoid sanitize_contours(Vertex** contours, int contourCnt) {
1003e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1004e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(contours[i]);
1005e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i];;) {
1006e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(v->fPrev->fPoint, v->fPoint)) {
1007e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1008e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v->fPrev == v) {
1009e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = nullptr;
1010e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    break;
1011e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1012e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev->fNext = v->fNext;
1013e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext->fPrev = v->fPrev;
1014e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (contours[i] == v) {
1015e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = v->fNext;
1016e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1017e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fPrev;
1018e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1019e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fNext;
1020e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v == contours[i]) break;
1021e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1022e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1024e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1025e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1026e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) {
1027e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = (*vertices)->fNext; v != nullptr; v = v->fNext) {
1028e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1029e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fPoint = v->fPrev->fPoint;
1030e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1031e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (coincident(v->fPrev->fPoint, v->fPoint)) {
1032e9709e831954c3427d5cb839e84221a177bfedebethannicholas            merge_vertices(v->fPrev, v, vertices, c, alloc);
1033e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1034e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1035e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1036e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1037e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1038e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1039e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
1040e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* vertices = nullptr;
1041e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
1042e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1043e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i]; v != nullptr;) {
1044e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* vNext = v->fNext;
1045682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            Edge* edge = new_edge(v->fPrev, v, alloc, c);
1046682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            if (edge->fWinding > 0) {
1047682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                insert_edge_below(edge, v->fPrev, c);
1048682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                insert_edge_above(edge, v, c);
1049682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            } else {
1050682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                insert_edge_below(edge, v, c);
1051682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                insert_edge_above(edge, v->fPrev, c);
1052682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            }
1053682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            merge_collinear_edges(edge, nullptr, c);
1054e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (prev) {
1055e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev->fNext = v;
1056e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prev;
1057e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1058e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertices = v;
1059e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1060e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = v;
1061e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = vNext;
1062e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v == contours[i]) break;
1063e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1064e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1065e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
1066e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = vertices->fPrev = nullptr;
1067e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1068e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return vertices;
1069e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1070e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1071e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction.
1072e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1073e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c);
1074e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1075e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) {
1076e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fast;
1077e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* slow;
1078e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!v || !v->fNext) {
1079e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pFront = v;
1080e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pBack = nullptr;
1081e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
1082e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow = v;
1083e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fast = v->fNext;
1084e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1085e9709e831954c3427d5cb839e84221a177bfedebethannicholas        while (fast != nullptr) {
1086e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fast = fast->fNext;
1087e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (fast != nullptr) {
1088e9709e831954c3427d5cb839e84221a177bfedebethannicholas                slow = slow->fNext;
1089e9709e831954c3427d5cb839e84221a177bfedebethannicholas                fast = fast->fNext;
1090e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1091e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1092e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1093e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pFront = v;
1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pBack = slow->fNext;
1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow->fNext->fPrev = nullptr;
1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow->fNext = nullptr;
1097e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1098e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1099e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1100e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_sort(Vertex** head, Comparator& c) {
1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!*head || !(*head)->fNext) {
1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1105e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* a;
1106e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* b;
1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas    front_back_split(*head, &a, &b);
1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1109e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_sort(&a, c);
1110e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_sort(&b, c);
1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = sorted_merge(a, b, c);
1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1115e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) {
1116e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList vertices;
1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (a && b) {
1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(a->fPoint, b->fPoint)) {
1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = a->fNext;
1121e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            vertices.append(a);
1122e9709e831954c3427d5cb839e84221a177bfedebethannicholas            a = next;
1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1124e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = b->fNext;
1125e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            vertices.append(b);
1126e9709e831954c3427d5cb839e84221a177bfedebethannicholas            b = next;
1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1128e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (a) {
1130e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        vertices.insert(a, vertices.fTail, a->fNext);
1131e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1132e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (b) {
1133e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        vertices.insert(b, vertices.fTail, b->fNext);
1134e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1135e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    return vertices.fHead;
1136e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1137e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1138e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1139e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1140e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) {
1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("simplifying complex polygons\n");
1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1143e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1146e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1147e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1148682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1149e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1150e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEnclosingEdge = nullptr;
1151e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* rightEnclosingEdge = nullptr;
1152e9709e831954c3427d5cb839e84221a177bfedebethannicholas        bool restartChecks;
1153e9709e831954c3427d5cb839e84221a177bfedebethannicholas        do {
1154e9709e831954c3427d5cb839e84221a177bfedebethannicholas            restartChecks = false;
1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas            find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1156e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v->fFirstEdgeBelow) {
1157e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (Edge* edge = v->fFirstEdgeBelow; edge != nullptr; edge = edge->fNextEdgeBelow) {
1158e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1160e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1161e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1162e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1166e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1167e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1168e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                        &activeEdges, c, alloc)) {
1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = pv;
1172e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1173e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    restartChecks = true;
1174e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1175e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } while (restartChecks);
1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1179e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(e, &activeEdges);
1180e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEdge = leftEnclosingEdge;
1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(e, leftEdge, &activeEdges);
1184e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge = e;
1185e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1186e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fProcessed = true;
1187e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons.
1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1192e9709e831954c3427d5cb839e84221a177bfedebethannicholasPoly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1193e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("tessellating simple polygons\n");
1194e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1195e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* polys = nullptr;
1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1197e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1198e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1199e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1200e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1201682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY);
1202e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1203e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEnclosingEdge = nullptr;
1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* rightEnclosingEdge = nullptr;
1205e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1206e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* leftPoly = nullptr;
1207e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* rightPoly = nullptr;
1208e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1209e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1210e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = v->fLastEdgeAbove->fRightPoly;
1211e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1212e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1213e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1214e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1215e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1216e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges above:\n");
1217e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1218e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1219e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1220e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1221e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges below:\n");
1222e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1223e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1224e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1225e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1226e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1227e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1228e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (leftPoly) {
1229531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
1230e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1231e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (rightPoly) {
1232531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
1233e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1234e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1235e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* leftEdge = e;
1236e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* rightEdge = e->fNextEdgeAbove;
1237e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1238e9709e831954c3427d5cb839e84221a177bfedebethannicholas                remove_edge(leftEdge, &activeEdges);
1239e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftEdge->fRightPoly) {
1240531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
1241e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1242531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (rightEdge->fLeftPoly) {
1243531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
1244e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1246e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(v->fLastEdgeAbove, &activeEdges);
1247e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeBelow) {
1248e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftPoly && rightPoly && leftPoly != rightPoly) {
1249e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1250e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    rightPoly->fPartner = leftPoly;
1251e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftPoly->fPartner = rightPoly;
1252e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1253e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1254e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1255e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeBelow) {
1256e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeAbove) {
125793e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco                if (leftPoly && rightPoly) {
1258531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    if (leftPoly == rightPoly) {
1259531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1260531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1261531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 leftPoly->fWinding, alloc);
1262531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftEnclosingEdge->fRightPoly = leftPoly;
1263531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        } else {
1264531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1265531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 rightPoly->fWinding, alloc);
1266531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightEnclosingEdge->fLeftPoly = rightPoly;
1267531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        }
1268e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1269531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    Edge* join = ALLOC_NEW(Edge, (leftPoly->lastVertex(), v, 1), alloc);
1270531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1271531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Edge* leftEdge = v->fFirstEdgeBelow;
1275e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge->fLeftPoly = leftPoly;
1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1277e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1278e9709e831954c3427d5cb839e84221a177bfedebethannicholas                 rightEdge = rightEdge->fNextEdgeBelow) {
1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas                insert_edge(rightEdge, leftEdge, &activeEdges);
1280e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas                winding += leftEdge->fWinding;
1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (winding != 0) {
1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    Poly* poly = new_poly(&polys, v, winding, alloc);
1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1285e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1286e9709e831954c3427d5cb839e84221a177bfedebethannicholas                leftEdge = rightEdge;
1287e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1288e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fLastEdgeBelow->fRightPoly = rightPoly;
1289e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1290e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1291e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("\nactive edges:\n");
1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1293e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1297e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1298e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return polys;
1299e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1300e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13019992bdef8ae97b3e5b109d278ccfab84c66bcbf0senorblanco// This is a driver function which calls stages 2-5 in turn.
13029992bdef8ae97b3e5b109d278ccfab84c66bcbf0senorblanco
1303682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, const SkRect& pathBounds,
1304682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                        SkChunkAlloc& alloc) {
1305682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Comparator c;
1306682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    if (pathBounds.width() > pathBounds.height()) {
1307682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        c.sweep_lt = sweep_lt_horiz;
1308682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        c.sweep_gt = sweep_gt_horiz;
1309682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    } else {
1310682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        c.sweep_lt = sweep_lt_vert;
1311682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        c.sweep_gt = sweep_gt_vert;
1312682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1314e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1315e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v = contours[i];
1316e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(v);
1317e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (v = v->fNext; v != contours[i]; v = v->fNext) {
1319e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1320e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1321e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1322e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1323682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    sanitize_contours(contours, contourCnt);
1324682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Vertex* vertices = build_edges(contours, contourCnt, c, alloc);
1325682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    if (!vertices) {
1326e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1327e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1328e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1329e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // Sort vertices in Y (secondarily in X).
1330682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    merge_sort(&vertices, c);
1331682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    merge_coincident_vertices(&vertices, c, alloc);
1332e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1333682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static float gID = 0.0f;
1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID = gID++;
1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1338682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    simplify(vertices, c, alloc);
1339682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    return tessellate(vertices, alloc);
1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13429d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1343682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                    int contourCnt, SkChunkAlloc& alloc, bool* isLinear) {
1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1345e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (SkPath::IsInverseFillType(fillType)) {
1346e9709e831954c3427d5cb839e84221a177bfedebethannicholas        contourCnt++;
1347e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
1349e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1350e9709e831954c3427d5cb839e84221a177bfedebethannicholas    path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
1351682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc);
1352e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1353e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13549d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryvoid get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt,
1355e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                         int* sizeEstimate) {
1356e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance);
1357e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts <= 0) {
1358e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contourCnt = 0;
1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts > ((int)SK_MaxU16 + 1)) {
1362e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1363e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contourCnt = 0;
1364e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1365e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // For the initial size of the chunk allocator, estimate based on the point count:
1367e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // one vertex per point for the initial passes, plus two for the vertices in the
1368e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // resulting Polys, since the same point may end up in two Polys.  Assume minimal
1369e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // connectivity of one Edge per Vertex (will grow for intersections).
1370e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge));
1371e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1372e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1373e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) {
1374e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = 0;
1375e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1376682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1377e9709e831954c3427d5cb839e84221a177bfedebethannicholas            count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1378e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1379e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1380e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return count;
1381e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1382e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1383e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1384e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1385e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator {
1386e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1387e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1388e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13899d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1390682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco                    VertexAllocator* vertexAllocator, bool* isLinear) {
1391e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int contourCnt;
1392e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int sizeEstimate;
1393e9709e831954c3427d5cb839e84221a177bfedebethannicholas    get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1394e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1395e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *isLinear = true;
1396e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkChunkAlloc alloc(sizeEstimate);
1399682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, isLinear);
1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1406682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    SkPoint* verts = vertexAllocator->lock(count);
14076599efffeef3168dfc68dca99c30454c5c23b859senorblanco    if (!verts) {
1408e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Could not allocate vertices\n");
1409e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1410e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1411682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    SkPoint* end = verts;
1412682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    for (Poly* poly = polys; poly; poly = poly->fNext) {
1413682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        if (apply_fill_type(fillType, poly->fWinding)) {
1414682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            end = poly->emit(end);
1415682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        }
1416682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1417682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    int actualCount = static_cast<int>(end - verts);
1418682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    LOG("actual count: %d\n", actualCount);
1419e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
14206599efffeef3168dfc68dca99c30454c5c23b859senorblanco    vertexAllocator->unlock(actualCount);
1421e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1422e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1423e9709e831954c3427d5cb839e84221a177bfedebethannicholas
14249d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1425e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   GrTessellator::WindingVertex** verts) {
1426e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int contourCnt;
1427e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int sizeEstimate;
1428e9709e831954c3427d5cb839e84221a177bfedebethannicholas    get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1429e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1430e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1431e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1432e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkChunkAlloc alloc(sizeEstimate);
1433e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLinear;
1434682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, &isLinear);
1435e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1436e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1437e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1438e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *verts = nullptr;
1439e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1440e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1441e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1442e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *verts = new GrTessellator::WindingVertex[count];
1443e9709e831954c3427d5cb839e84221a177bfedebethannicholas    GrTessellator::WindingVertex* vertsEnd = *verts;
1444e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* points = new SkPoint[count];
1445e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* pointsEnd = points;
1446e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1447682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco        if (apply_fill_type(fillType, poly->fWinding)) {
1448e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkPoint* start = pointsEnd;
1449682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco            pointsEnd = poly->emit(pointsEnd);
1450e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (start != pointsEnd) {
1451e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fPos = *start;
1452e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fWinding = poly->fWinding;
1453e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++start;
1454e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++vertsEnd;
1455e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1456e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1457e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1458e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int actualCount = static_cast<int>(vertsEnd - *verts);
1459e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
1460e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(pointsEnd - points == actualCount);
1461e9709e831954c3427d5cb839e84221a177bfedebethannicholas    delete[] points;
1462e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1463e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1464e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1465e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1466