GrTessellator.cpp revision 8c8fceff4d4e8ec09f29748d77ed5510697a2995
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
10f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#include "GrDefaultGeoProcFactory.h"
11e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include "GrPathUtils.h"
12e9709e831954c3427d5cb839e84221a177bfedebethannicholas
136599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkChunkAlloc.h"
146599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkGeometry.h"
156599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkPath.h"
16e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include <stdio.h>
18e9709e831954c3427d5cb839e84221a177bfedebethannicholas
19e9709e831954c3427d5cb839e84221a177bfedebethannicholas/*
20f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * There are six stages to the basic algorithm:
21e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
22e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 1) Linearize the path contours into piecewise linear segments (path_to_contours()).
23e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 2) Build a mesh of edges connecting the vertices (build_edges()).
24e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()).
25e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()).
26e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 5) Tessellate the simplified mesh into monotone polygons (tessellate()).
27e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()).
28e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
29f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * For screenspace antialiasing, the algorithm is modified as follows:
30f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *
31f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * Run steps 1-5 above to produce polygons.
32f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()).
33f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5c) Simplify boundaries to remove "pointy" vertices which cause inversions (simplify_boundary()).
34f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find
35f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *     new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new
36f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *     antialiased mesh from those vertices (boundary_to_aa_mesh()).
37f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * Run steps 3-6 above on the new mesh, and produce antialiased triangles.
38f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *
39e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list
40e9709e831954c3427d5cb839e84221a177bfedebethannicholas * of vertices (and the necessity of inserting new vertices on intersection).
41e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
42e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Stages (4) and (5) use an active edge list, which a list of all edges for which the
43e9709e831954c3427d5cb839e84221a177bfedebethannicholas * sweep line has crossed the top vertex, but not the bottom vertex.  It's sorted
44e9709e831954c3427d5cb839e84221a177bfedebethannicholas * left-to-right based on the point where both edges are active (when both top vertices
45e9709e831954c3427d5cb839e84221a177bfedebethannicholas * have been seen, so the "lower" top vertex of the two). If the top vertices are equal
46e9709e831954c3427d5cb839e84221a177bfedebethannicholas * (shared), it's sorted based on the last point where both edges are active, so the
47e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "upper" bottom vertex.
48e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
49e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The most complex step is the simplification (4). It's based on the Bentley-Ottman
50e9709e831954c3427d5cb839e84221a177bfedebethannicholas * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are
51e9709e831954c3427d5cb839e84221a177bfedebethannicholas * not exact and may violate the mesh topology or active edge list ordering. We
52e9709e831954c3427d5cb839e84221a177bfedebethannicholas * accommodate this by adjusting the topology of the mesh and AEL to match the intersection
53e9709e831954c3427d5cb839e84221a177bfedebethannicholas * points. This occurs in three ways:
54e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
55e9709e831954c3427d5cb839e84221a177bfedebethannicholas * A) Intersections may cause a shortened edge to no longer be ordered with respect to its
56e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    neighbouring edges at the top or bottom vertex. This is handled by merging the
57e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    edges (merge_collinear_edges()).
58e9709e831954c3427d5cb839e84221a177bfedebethannicholas * B) Intersections may cause an edge to violate the left-to-right ordering of the
59e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    active edge list. This is handled by splitting the neighbour edge on the
60e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    intersected vertex (cleanup_active_edges()).
61e9709e831954c3427d5cb839e84221a177bfedebethannicholas * C) Shortening an edge may cause an active edge to become inactive or an inactive edge
62e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    to become active. This is handled by removing or inserting the edge in the active
63e9709e831954c3427d5cb839e84221a177bfedebethannicholas *    edge list (fix_active_state()).
64e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
65e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and
66e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it
67e9709e831954c3427d5cb839e84221a177bfedebethannicholas * currently uses a linked list for the active edge list, rather than a 2-3 tree as the
68e9709e831954c3427d5cb839e84221a177bfedebethannicholas * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also
69e9709e831954c3427d5cb839e84221a177bfedebethannicholas * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N)
70e9709e831954c3427d5cb839e84221a177bfedebethannicholas * insertions and removals was greater than the cost of infrequent O(N) lookups with the
71e9709e831954c3427d5cb839e84221a177bfedebethannicholas * linked list implementation. With the latter, all removals are O(1), and most insertions
72e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are O(1), since we know the adjacent edge in the active edge list based on the topology.
73e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less
74e9709e831954c3427d5cb839e84221a177bfedebethannicholas * frequent. There may be other data structures worth investigating, however.
75e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
76e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the
77e9709e831954c3427d5cb839e84221a177bfedebethannicholas * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y
78e9709e831954c3427d5cb839e84221a177bfedebethannicholas * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall,
79e9709e831954c3427d5cb839e84221a177bfedebethannicholas * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so
80e9709e831954c3427d5cb839e84221a177bfedebethannicholas * that the "left" and "right" orientation in the code remains correct (edges to the left are
81e9709e831954c3427d5cb839e84221a177bfedebethannicholas * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90
82e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degrees counterclockwise, rather that transposing.
83e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
84e9709e831954c3427d5cb839e84221a177bfedebethannicholas
85e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOGGING_ENABLED 0
86e9709e831954c3427d5cb839e84221a177bfedebethannicholas
87e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
88e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG printf
89e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
90e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG(...)
91e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
92e9709e831954c3427d5cb839e84221a177bfedebethannicholas
93e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args
94e9709e831954c3427d5cb839e84221a177bfedebethannicholas
95e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace {
96e9709e831954c3427d5cb839e84221a177bfedebethannicholas
97e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex;
98e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge;
99e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly;
100e9709e831954c3427d5cb839e84221a177bfedebethannicholas
101e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next>
102e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_insert(T* t, T* prev, T* next, T** head, T** tail) {
103e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Prev = prev;
104e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Next = next;
105e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
106e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->*Next = t;
107e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (head) {
108e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = t;
109e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
110e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (next) {
111e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next->*Prev = t;
112e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (tail) {
113e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *tail = t;
114e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
115e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
116e9709e831954c3427d5cb839e84221a177bfedebethannicholas
117e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next>
118e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_remove(T* t, T** head, T** tail) {
119e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (t->*Prev) {
120e9709e831954c3427d5cb839e84221a177bfedebethannicholas        t->*Prev->*Next = t->*Next;
121e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (head) {
122e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = t->*Next;
123e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
124e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (t->*Next) {
125e9709e831954c3427d5cb839e84221a177bfedebethannicholas        t->*Next->*Prev = t->*Prev;
126e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (tail) {
127e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *tail = t->*Prev;
128e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
129e9709e831954c3427d5cb839e84221a177bfedebethannicholas    t->*Prev = t->*Next = nullptr;
130e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
131e9709e831954c3427d5cb839e84221a177bfedebethannicholas
132e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
133e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices are used in three ways: first, the path contours are converted into a
134e9709e831954c3427d5cb839e84221a177bfedebethannicholas * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
135e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing
136e9709e831954c3427d5cb839e84221a177bfedebethannicholas * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
137e9709e831954c3427d5cb839e84221a177bfedebethannicholas * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
138e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since
139e9709e831954c3427d5cb839e84221a177bfedebethannicholas * an individual Vertex from the path mesh may belong to multiple
140e9709e831954c3427d5cb839e84221a177bfedebethannicholas * MonotonePolys, so the original Vertices cannot be re-used.
141e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
142e9709e831954c3427d5cb839e84221a177bfedebethannicholas
143e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex {
144f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco  Vertex(const SkPoint& point, uint8_t alpha)
145e9709e831954c3427d5cb839e84221a177bfedebethannicholas    : fPoint(point), fPrev(nullptr), fNext(nullptr)
146e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
147e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
148e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fProcessed(false)
149f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    , fAlpha(alpha)
150e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
151e9709e831954c3427d5cb839e84221a177bfedebethannicholas    , fID (-1.0f)
152e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
153e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {}
154e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint fPoint;           // Vertex position
155e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fPrev;            // Linked list of contours, then Y-sorted vertices.
156e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fNext;            // "
157e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fFirstEdgeAbove;  // Linked list of edges above this vertex.
158e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fLastEdgeAbove;   // "
159e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fFirstEdgeBelow;  // Linked list of edges below this vertex.
160e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*   fLastEdgeBelow;   // "
161e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool    fProcessed;       // Has this vertex been seen in simplify()?
162f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    uint8_t fAlpha;
163e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
164e9709e831954c3427d5cb839e84221a177bfedebethannicholas    float   fID;              // Identifier used for logging.
165e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
166e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
167e9709e831954c3427d5cb839e84221a177bfedebethannicholas
168e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
169e9709e831954c3427d5cb839e84221a177bfedebethannicholas
170f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct AAParams {
171f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool fTweakAlpha;
172f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    GrColor fColor;
173f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco};
174f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
175e9709e831954c3427d5cb839e84221a177bfedebethannicholastypedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
176e9709e831954c3427d5cb839e84221a177bfedebethannicholas
177e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Comparator {
178e9709e831954c3427d5cb839e84221a177bfedebethannicholas    CompareFunc sweep_lt;
179e9709e831954c3427d5cb839e84221a177bfedebethannicholas    CompareFunc sweep_gt;
180e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
181e9709e831954c3427d5cb839e84221a177bfedebethannicholas
182e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
183e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX;
184e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
185e9709e831954c3427d5cb839e84221a177bfedebethannicholas
186e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
187e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY;
188e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
189e9709e831954c3427d5cb839e84221a177bfedebethannicholas
190e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
191e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX;
192e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
193e9709e831954c3427d5cb839e84221a177bfedebethannicholas
194e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
195e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
196e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
197e9709e831954c3427d5cb839e84221a177bfedebethannicholas
198f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
199f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!aaParams) {
200f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint* d = static_cast<SkPoint*>(data);
201f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        *d++ = v->fPoint;
202f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
203f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
204f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (aaParams->fTweakAlpha) {
205f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
206f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d->fPosition = v->fPoint;
2078c8fceff4d4e8ec09f29748d77ed5510697a2995lsalzman        d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
208f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d++;
209f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
210f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
211f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
212f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fPosition = v->fPoint;
213f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fColor = aaParams->fColor;
214f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
215f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d++;
216f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return d;
217e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
218e9709e831954c3427d5cb839e84221a177bfedebethannicholas
219f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
220f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#if TESSELLATOR_WIREFRAME
221f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
222f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
223f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
224f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
225f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
226f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
227e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
228f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
229f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
230f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
231e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
232e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return data;
233e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
234e9709e831954c3427d5cb839e84221a177bfedebethannicholas
235e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancostruct VertexList {
236e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList() : fHead(nullptr), fTail(nullptr) {}
237e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fHead;
238e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fTail;
239e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void insert(Vertex* v, Vertex* prev, Vertex* next) {
240e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
241e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
242e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void append(Vertex* v) {
243e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, fTail, nullptr);
244e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
245e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void prepend(Vertex* v) {
246e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, nullptr, fHead);
247e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
248f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
249f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
250f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fNext = fHead;
251f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fPrev = fTail;
252f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
253f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
254e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco};
255e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco
256f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Round to nearest quarter-pixel. This is used for screenspace tessellation.
257f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
258f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void round(SkPoint* p) {
259f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
260f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
261f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
262f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
26349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
26449df8d17c56ee08ecf860289d501913d356f67dcsenorblancostruct Line {
26549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
26649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(const SkPoint& p, const SkPoint& q)
26749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        : fA(static_cast<double>(q.fY) - p.fY)      // a = dY
26849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fB(static_cast<double>(p.fX) - q.fX)      // b = -dX
26949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fC(static_cast<double>(p.fY) * q.fX -     // c = cross(q, p)
27049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco             static_cast<double>(p.fX) * q.fY) {}
27149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double dist(const SkPoint& p) const {
27249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * p.fX + fB * p.fY + fC;
27349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
27449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double magSq() const {
27549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * fA + fB * fB;
27649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
27749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
27849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    // Compute the intersection of two (infinite) Lines.
27949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    bool intersect(const Line& other, SkPoint* point) {
28049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fA * other.fB - fB * other.fA;
28149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (denom == 0.0) {
28249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            return false;
28349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        }
28449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double scale = 1.0f / denom;
28549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale);
28649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale);
28749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        round(point);
28849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return true;
28949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
29049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double fA, fB, fC;
29149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco};
29249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
293e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
294e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
295e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
296e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
297e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g.,
298e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
299e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division
300e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but
301e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case.
302e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
303e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic
304e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
305e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being
306e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
307e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
308e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file).
309e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
310e9709e831954c3427d5cb839e84221a177bfedebethannicholas
311e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge {
312e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge(Vertex* top, Vertex* bottom, int winding)
313e9709e831954c3427d5cb839e84221a177bfedebethannicholas        : fWinding(winding)
314e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTop(top)
315e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fBottom(bottom)
316e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeft(nullptr)
317e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fRight(nullptr)
318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeAbove(nullptr)
319e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeAbove(nullptr)
320e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeBelow(nullptr)
321e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeBelow(nullptr)
322e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeftPoly(nullptr)
323531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPoly(nullptr)
324531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyPrev(nullptr)
325531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyNext(nullptr)
326531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPolyPrev(nullptr)
32770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fRightPolyNext(nullptr)
32870f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInLeftPoly(false)
32949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fUsedInRightPoly(false)
33049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fLine(top, bottom) {
331e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
332e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int      fWinding;          // 1 == edge goes downward; -1 = edge goes upward.
333e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fTop;              // The top vertex in vertex-sort-order (sweep_lt).
334e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fBottom;           // The bottom vertex in vertex-sort-order.
335e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fLeft;             // The linked list of edges in the active edge list.
336e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fRight;            // "
337e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeAbove;    // The linked list of edges in the bottom Vertex's "edges above".
338e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeAbove;    // "
339e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeBelow;    // The linked list of edges in the top Vertex's "edges below".
340e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeBelow;    // "
341e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fLeftPoly;         // The Poly to the left of this edge, if any.
342e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fRightPoly;        // The Poly to the right of this edge, if any.
343531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyPrev;
344531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyNext;
345531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyPrev;
346531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyNext;
34770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInLeftPoly;
34870f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInRightPoly;
34949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line     fLine;
350e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double dist(const SkPoint& p) const {
35149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(p);
352e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
353e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isRightOf(Vertex* v) const {
35449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) < 0.0;
355e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
356e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLeftOf(Vertex* v) const {
35749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) > 0.0;
358e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
359e9709e831954c3427d5cb839e84221a177bfedebethannicholas    void recompute() {
36049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        fLine = Line(fTop, fBottom);
361e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
362e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool intersect(const Edge& other, SkPoint* p) {
363e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("intersecting %g -> %g with %g -> %g\n",
364e9709e831954c3427d5cb839e84221a177bfedebethannicholas               fTop->fID, fBottom->fID,
365e9709e831954c3427d5cb839e84221a177bfedebethannicholas               other.fTop->fID, other.fBottom->fID);
366e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fTop == other.fTop || fBottom == other.fBottom) {
367e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
368e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
36949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
370e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom == 0.0) {
371e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
372e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
373e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
374e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
37549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA;
37649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double tNumer = -dy * fLine.fB - dx * fLine.fA;
377e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
378e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // This saves us doing the divide below unless absolutely necessary.
379e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
380e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
381e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
382e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
383e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double s = sNumer / denom;
384e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(s >= 0.0 && s <= 1.0);
38549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
38649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
387e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return true;
388e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
389f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco};
390f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
391f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct EdgeList {
392f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {}
393f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fHead;
394f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fTail;
395f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    EdgeList* fNext;
396f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int fCount;
397f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void insert(Edge* edge, Edge* prev, Edge* next) {
398f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
399f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        fCount++;
400f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
401f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void append(Edge* e) {
402f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert(e, fTail, nullptr);
403f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
404f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void remove(Edge* edge) {
405f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
406f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        fCount--;
407f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
408f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
409f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
410f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fRight = fHead;
411f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fLeft = fTail;
412f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
413f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
414f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool contains(Edge* edge) const {
415f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return edge->fLeft || edge->fRight || fHead == edge;
416e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
417e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
418e9709e831954c3427d5cb839e84221a177bfedebethannicholas
419e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
420e9709e831954c3427d5cb839e84221a177bfedebethannicholas
421e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly {
422531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly(Vertex* v, int winding)
423531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        : fFirstVertex(v)
424531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fWinding(winding)
425e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fHead(nullptr)
426e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTail(nullptr)
427e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNext(nullptr)
428e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPartner(nullptr)
429e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fCount(0)
430e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {
431e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
432e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static int gID = 0;
433e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fID = gID++;
434e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("*** created Poly %d\n", fID);
435e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
436e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
437531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    typedef enum { kLeft_Side, kRight_Side } Side;
438e9709e831954c3427d5cb839e84221a177bfedebethannicholas    struct MonotonePoly {
439531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        MonotonePoly(Edge* edge, Side side)
440531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            : fSide(side)
441531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fFirstEdge(nullptr)
442531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fLastEdge(nullptr)
443e9709e831954c3427d5cb839e84221a177bfedebethannicholas            , fPrev(nullptr)
444531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fNext(nullptr) {
445531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            this->addEdge(edge);
446531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        }
447e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Side          fSide;
448531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fFirstEdge;
449531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fLastEdge;
450e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fPrev;
451e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fNext;
452531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        void addEdge(Edge* edge) {
453e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            if (fSide == kRight_Side) {
454212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInRightPoly);
455531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
456531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
45770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInRightPoly = true;
458e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
459212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInLeftPoly);
460531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
461531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
46270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInLeftPoly = true;
463e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
464e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
465e9709e831954c3427d5cb839e84221a177bfedebethannicholas
466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        void* emit(const AAParams* aaParams, void* data) {
467531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Edge* e = fFirstEdge;
468531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            e->fTop->fPrev = e->fTop->fNext = nullptr;
469531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            VertexList vertices;
470531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            vertices.append(e->fTop);
471531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (e != nullptr) {
472531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                e->fBottom->fPrev = e->fBottom->fNext = nullptr;
473531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (kRight_Side == fSide) {
474531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.append(e->fBottom);
475531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fRightPolyNext;
476531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                } else {
477531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.prepend(e->fBottom);
478531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fLeftPolyNext;
479531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                }
480531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            }
481531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Vertex* first = vertices.fHead;
482e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* v = first->fNext;
483531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (v != vertices.fTail) {
484e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(v && v->fPrev && v->fNext);
485e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* prev = v->fPrev;
486e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* curr = v;
487e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* next = v->fNext;
488e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
489e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
490e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
491e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
492e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (ax * by - ay * bx >= 0.0) {
493f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    data = emit_triangle(prev, curr, next, aaParams, data);
494e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fPrev->fNext = v->fNext;
495e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fNext->fPrev = v->fPrev;
496e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (v->fPrev == first) {
497e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fNext;
498e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    } else {
499e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fPrev;
500e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
501e9709e831954c3427d5cb839e84221a177bfedebethannicholas                } else {
502e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v = v->fNext;
503e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
504e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
505e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
506e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
507e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
508531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly* addEdge(Edge* e, Side side, SkChunkAlloc& alloc) {
50970f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        LOG("addEdge (%g -> %g) to poly %d, %s side\n",
51070f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco               e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
511e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* partner = fPartner;
512e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* poly = this;
513212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        if (side == kRight_Side) {
514212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInRightPoly) {
515212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
516212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
517212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        } else {
518212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInLeftPoly) {
519212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
520212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
521212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        }
522e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (partner) {
523e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fPartner = partner->fPartner = nullptr;
524e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
525531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        if (!fTail) {
526531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fHead = fTail = ALLOC_NEW(MonotonePoly, (e, side), alloc);
527531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount += 2;
52893e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco        } else if (e->fBottom == fTail->fLastEdge->fBottom) {
52993e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco            return poly;
530531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else if (side == fTail->fSide) {
531531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
532531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
533531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else {
534531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            e = ALLOC_NEW(Edge, (fTail->fLastEdge->fBottom, e->fBottom, 1), alloc);
535531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
536531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
537e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (partner) {
538531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                partner->addEdge(e, side, alloc);
539e9709e831954c3427d5cb839e84221a177bfedebethannicholas                poly = partner;
540e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
541531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                MonotonePoly* m = ALLOC_NEW(MonotonePoly, (e, side), alloc);
542531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                m->fPrev = fTail;
543531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail->fNext = m;
544531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail = m;
545e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
546e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
547e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return poly;
548e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
549f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* emit(const AAParams* aaParams, void *data) {
550e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fCount < 3) {
551e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
552e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
553e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("emit() %d, size %d\n", fID, fCount);
554e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
555f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = m->emit(aaParams, data);
556e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
557e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return data;
558e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
559531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
560531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* fFirstVertex;
561e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fWinding;
562e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fHead;
563e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fTail;
564e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fNext;
565e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fPartner;
566e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fCount;
567e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
568e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fID;
569e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
570e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
571e9709e831954c3427d5cb839e84221a177bfedebethannicholas
572e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
573e9709e831954c3427d5cb839e84221a177bfedebethannicholas
574e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) {
575e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a == b;
576e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
577e9709e831954c3427d5cb839e84221a177bfedebethannicholas
578e9709e831954c3427d5cb839e84221a177bfedebethannicholasPoly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
579531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly* poly = ALLOC_NEW(Poly, (v, winding), alloc);
580e9709e831954c3427d5cb839e84221a177bfedebethannicholas    poly->fNext = *head;
581e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = poly;
582e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return poly;
583e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
584e9709e831954c3427d5cb839e84221a177bfedebethannicholas
585f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoEdgeList* new_contour(EdgeList** head, SkChunkAlloc& alloc) {
586f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    EdgeList* contour = ALLOC_NEW(EdgeList, (), alloc);
587f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    contour->fNext = *head;
588f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    *head = contour;
589f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return contour;
590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
592e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
593e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                SkChunkAlloc& alloc) {
594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* v = ALLOC_NEW(Vertex, (p, 255), alloc);
595e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
596e9709e831954c3427d5cb839e84221a177bfedebethannicholas    static float gID = 0.0f;
597e9709e831954c3427d5cb839e84221a177bfedebethannicholas    v->fID = gID++;
598e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
599e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
600e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = v;
601e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fPrev = prev;
602e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
603e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = v;
604e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
605e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return v;
606e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
607e9709e831954c3427d5cb839e84221a177bfedebethannicholas
608e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0,
609e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p1,
610e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p2,
611e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkScalar tolSqd,
612e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex* prev,
613e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex** head,
614e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  int pointsLeft,
615e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkChunkAlloc& alloc) {
616e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
617e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
618e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p2, prev, head, alloc);
619e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
620e9709e831954c3427d5cb839e84221a177bfedebethannicholas
621e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
622e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
623e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
624e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
625e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
626e9709e831954c3427d5cb839e84221a177bfedebethannicholas
627e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
628e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
629e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
630e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
631e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
632e9709e831954c3427d5cb839e84221a177bfedebethannicholas
633e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0,
634e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p1,
635e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p2,
636e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p3,
637e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkScalar tolSqd,
638e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex* prev,
639e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex** head,
640e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              int pointsLeft,
641e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkChunkAlloc& alloc) {
642e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
643e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
644e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
645e9709e831954c3427d5cb839e84221a177bfedebethannicholas        !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
646e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p3, prev, head, alloc);
647e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
648e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
649e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
650e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
651e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
652e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
653e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r[] = {
654e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
655e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
656e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
657e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
658e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
659e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
660e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
661e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
662e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
663e9709e831954c3427d5cb839e84221a177bfedebethannicholas
664e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
665e9709e831954c3427d5cb839e84221a177bfedebethannicholas
666e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
667e9709e831954c3427d5cb839e84221a177bfedebethannicholas                      Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) {
668e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar toleranceSqd = tolerance * tolerance;
669e9709e831954c3427d5cb839e84221a177bfedebethannicholas
670e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint pts[4];
671e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool done = false;
672e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *isLinear = true;
673e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::Iter iter(path, false);
674e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
675e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* head = nullptr;
676e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (path.isInverseFillType()) {
677e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPoint quad[4];
678e9709e831954c3427d5cb839e84221a177bfedebethannicholas        clipBounds.toQuad(quad);
679f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        for (int i = 0; i < 4; i++) {
680e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = append_point_to_contour(quad[i], prev, &head, alloc);
681e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
682e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head->fPrev = prev;
683e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = head;
684e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contours++ = head;
685e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head = prev = nullptr;
686e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
687e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoConicToQuads converter;
688e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (!done) {
689e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPath::Verb verb = iter.next(pts);
690e9709e831954c3427d5cb839e84221a177bfedebethannicholas        switch (verb) {
691e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kConic_Verb: {
692e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkScalar weight = iter.conicWeight();
693e9709e831954c3427d5cb839e84221a177bfedebethannicholas                const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
694e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (int i = 0; i < converter.countQuads(); ++i) {
695e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
696e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
697e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                     toleranceSqd, prev, &head, pointsLeft, alloc);
698e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    quadPts += 2;
699e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
700e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
701e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
702e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
703e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kMove_Verb:
704e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
705e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
706e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
707e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
708e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
709e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
710e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[0], prev, &head, alloc);
711e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
712e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kLine_Verb: {
713e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[1], prev, &head, alloc);
714e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
715e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
716e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kQuad_Verb: {
717e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
718e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
719e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                 &head, pointsLeft, alloc);
720e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
721e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
722e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
723e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kCubic_Verb: {
724e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
725e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
726e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                toleranceSqd, prev, &head, pointsLeft, alloc);
727e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
728e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
729e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
730e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kClose_Verb:
731e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
732e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
733e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
734e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
735e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
736e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
737e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
738e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kDone_Verb:
739e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
740e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
741e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
742e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
743e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
744e9709e831954c3427d5cb839e84221a177bfedebethannicholas                done = true;
745e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
746e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
747e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
748e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
749e9709e831954c3427d5cb839e84221a177bfedebethannicholas
750f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
751f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!poly) {
752f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return false;
753f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
754f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int winding = poly->fWinding;
755e9709e831954c3427d5cb839e84221a177bfedebethannicholas    switch (fillType) {
756e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kWinding_FillType:
757e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return winding != 0;
758e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kEvenOdd_FillType:
759e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) != 0;
760e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseWinding_FillType:
761f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            return winding == -1;
762e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseEvenOdd_FillType:
763e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) == 1;
764e9709e831954c3427d5cb839e84221a177bfedebethannicholas        default:
765e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkASSERT(false);
766e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
767e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
768e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
769e9709e831954c3427d5cb839e84221a177bfedebethannicholas
770f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoEdge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c,
771f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco               int winding_scale = 1) {
772f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? winding_scale : -winding_scale;
773e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = winding < 0 ? next : prev;
774e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = winding < 0 ? prev : next;
775e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return ALLOC_NEW(Edge, (top, bottom, winding), alloc);
776e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
777e9709e831954c3427d5cb839e84221a177bfedebethannicholas
778e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) {
779e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
780f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(edges->contains(edge));
781f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->remove(edge);
782e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
783e9709e831954c3427d5cb839e84221a177bfedebethannicholas
784e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
785e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
786f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(!edges->contains(edge));
787e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = prev ? prev->fRight : edges->fHead;
788f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->insert(edge, prev, next);
789e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
790e9709e831954c3427d5cb839e84221a177bfedebethannicholas
791e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
792e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (v->fFirstEdgeAbove) {
793e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *left = v->fFirstEdgeAbove->fLeft;
794e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *right = v->fLastEdgeAbove->fRight;
795e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
796e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
797e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = nullptr;
798e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev;
799e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
800e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (prev->isLeftOf(v)) {
801e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
802e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
803e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next = prev;
804e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
805e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
806e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
807e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
808e9709e831954c3427d5cb839e84221a177bfedebethannicholas
809e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
810e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
811e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
812e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = edges->fHead; next != nullptr; next = next->fRight) {
813e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
814e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
815e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
816e9709e831954c3427d5cb839e84221a177bfedebethannicholas             next->isRightOf(edge->fBottom)) ||
817e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
818e9709e831954c3427d5cb839e84221a177bfedebethannicholas             edge->isLeftOf(next->fBottom))) {
819e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
820e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
821e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
822e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
823e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
824e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
825e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
826e9709e831954c3427d5cb839e84221a177bfedebethannicholas
827e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
828f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (activeEdges && activeEdges->contains(edge)) {
829e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
830e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(edge, activeEdges);
831e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
832e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
833e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* left;
834e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* right;
835e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(edge, activeEdges, c, &left, &right);
836e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge(edge, left, activeEdges);
837e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
838e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
839e9709e831954c3427d5cb839e84221a177bfedebethannicholas
840e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
841e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
842e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
843e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
844e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
845e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
846e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
847e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
848e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
849e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fTop)) {
850e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
851e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
852e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
853e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
854e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
855e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
856e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
857e9709e831954c3427d5cb839e84221a177bfedebethannicholas
858e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
859e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
860e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
861e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
862e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
863e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
864e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
865e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
866e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
867e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fBottom)) {
868e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
869e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
870e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
871e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
872e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
873e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
874e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
875e9709e831954c3427d5cb839e84221a177bfedebethannicholas
876e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) {
877e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
878e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fBottom->fID);
879e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
880e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
881e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
882e9709e831954c3427d5cb839e84221a177bfedebethannicholas
883e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) {
884e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
885e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID);
886e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
887e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
888e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
889e9709e831954c3427d5cb839e84221a177bfedebethannicholas
890e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) {
891e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fWinding != 0) {
892e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
893e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
894e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
895e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
896e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
897f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (edges && edges->contains(edge)) {
898e9709e831954c3427d5cb839e84221a177bfedebethannicholas        remove_edge(edge, edges);
899e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
900e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
901e9709e831954c3427d5cb839e84221a177bfedebethannicholas
902e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
903e9709e831954c3427d5cb839e84221a177bfedebethannicholas
904e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
905e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
906e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fTop = v;
907e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
908e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_below(edge, v, c);
909e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
910e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
911e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
912e9709e831954c3427d5cb839e84221a177bfedebethannicholas
913e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
914e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
915e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fBottom = v;
916e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
917e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_above(edge, v, c);
918e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
919e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
920e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
921e9709e831954c3427d5cb839e84221a177bfedebethannicholas
922e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
923e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
924e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
925e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
926e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
927e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
928e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
929e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding = 0;
930e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
931e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
932e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
933e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
934e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, other->fTop, activeEdges, c);
935e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
936e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
937e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
938e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(other, edge->fTop, activeEdges, c);
939e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
940e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
941e9709e831954c3427d5cb839e84221a177bfedebethannicholas
942e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
943e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
944e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
945e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
946e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
947e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
948e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
949e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding = 0;
950e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
951e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
952e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
953e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(edge, activeEdges);
954e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(other, edge->fBottom, activeEdges, c);
955e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
956e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
957e9709e831954c3427d5cb839e84221a177bfedebethannicholas        erase_edge_if_zero_winding(other, activeEdges);
958e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, other->fBottom, activeEdges, c);
959e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
960e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
961e9709e831954c3427d5cb839e84221a177bfedebethannicholas
962e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
963e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
964e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
965e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
966e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
967e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
968e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
969e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
970e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
971e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
972e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
973e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
974e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
975e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
976e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
977e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
978e9709e831954c3427d5cb839e84221a177bfedebethannicholas
979e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc);
980e9709e831954c3427d5cb839e84221a177bfedebethannicholas
981e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
982e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = edge->fTop;
983e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = edge->fBottom;
984e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fLeft) {
985e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftTop = edge->fLeft->fTop;
986e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftBottom = edge->fLeft->fBottom;
987e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
988e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
989e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
990e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftTop, activeEdges, c, alloc);
991e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
992e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fLeft->isLeftOf(bottom)) {
993e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
994e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
995e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftBottom, activeEdges, c, alloc);
996e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
997e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
998e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fRight) {
999e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightTop = edge->fRight->fTop;
1000e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightBottom = edge->fRight->fBottom;
1001e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
1002e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, top, activeEdges, c, alloc);
1003e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
1004e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightTop, activeEdges, c, alloc);
1005e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1006e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fRight->isRightOf(bottom)) {
1007e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, bottom, activeEdges, c, alloc);
1008e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1009e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->isLeftOf(rightBottom)) {
1010e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightBottom, activeEdges, c, alloc);
1011e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1012e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1013e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1014e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1015e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) {
1016e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1017e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID, edge->fBottom->fID,
1018e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID, v->fPoint.fX, v->fPoint.fY);
1019e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
1020e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, v, activeEdges, c);
1021e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
1022e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
1024e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* newEdge = ALLOC_NEW(Edge, (v, edge->fBottom, edge->fWinding), alloc);
1025e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_below(newEdge, v, c);
1026e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_above(newEdge, edge->fBottom, c);
1027e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1028e9709e831954c3427d5cb839e84221a177bfedebethannicholas        cleanup_active_edges(edge, activeEdges, c, alloc);
1029e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fix_active_state(newEdge, activeEdges, c);
1030e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_collinear_edges(newEdge, activeEdges, c);
1031e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1032e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1033e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1034f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoEdge* connect(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator c,
1035f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco              int winding_scale = 1) {
1036f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* edge = new_edge(prev, next, alloc, c, winding_scale);
1037f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (edge->fWinding > 0) {
1038f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert_edge_below(edge, prev, c);
1039f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert_edge_above(edge, next, c);
1040f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } else {
1041f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert_edge_below(edge, next, c);
1042f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert_edge_above(edge, prev, c);
1043f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1044f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_collinear_edges(edge, nullptr, c);
1045f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return edge;
1046f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1047f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1048e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) {
1049e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1050e9709e831954c3427d5cb839e84221a177bfedebethannicholas        src->fID, dst->fID);
1051f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
1052e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1053e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeAbove;
1054e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, dst, nullptr, c);
1055e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1056e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1057e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1058e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeBelow;
1059e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, dst, nullptr, c);
1060e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1061e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1062e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr);
1063e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1064e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1065f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancouint8_t max_edge_alpha(Edge* a, Edge* b) {
1066f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
1067f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                  SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
1068f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1069f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1070e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
1071e9709e831954c3427d5cb839e84221a177bfedebethannicholas                               SkChunkAlloc& alloc) {
1072e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint p;
1073e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!edge || !other) {
1074e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1075e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1076e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->intersect(*other, &p)) {
1077e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v;
1078e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1079e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1080e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fTop, activeEdges, c, alloc);
1081e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fTop;
1082e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
1083e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fBottom, activeEdges, c, alloc);
1084e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fBottom;
1085e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1086e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fTop, activeEdges, c, alloc);
1087e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fTop;
1088e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
1089e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fBottom, activeEdges, c, alloc);
1090e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fBottom;
1091e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1092e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* nextV = edge->fTop;
1093e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(p, nextV->fPoint)) {
1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fPrev;
1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(nextV->fPoint, p)) {
1097e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fNext;
1098e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1099e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* prevV = nextV->fPrev;
1100e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(prevV->fPoint, p)) {
1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = prevV;
1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else if (coincident(nextV->fPoint, p)) {
1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = nextV;
1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1105f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                uint8_t alpha = max_edge_alpha(edge, other);
1106f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                v = ALLOC_NEW(Vertex, (p, alpha), alloc);
1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1109e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1110e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fID = (nextV->fID + prevV->fID) * 0.5f;
1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prevV;
1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext = nextV;
1115e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prevV->fNext = v;
1116e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV->fPrev = v;
1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, v, activeEdges, c, alloc);
1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, v, activeEdges, c, alloc);
1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1121e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return v;
1122e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return nullptr;
1124e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1125e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1126f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1128e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(contours[i]);
1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i];;) {
1130f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (approximate) {
1131f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                round(&v->fPoint);
1132f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1133e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(v->fPrev->fPoint, v->fPoint)) {
1134e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1135e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v->fPrev == v) {
1136e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = nullptr;
1137e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    break;
1138e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1139e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev->fNext = v->fNext;
1140e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext->fPrev = v->fPrev;
1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (contours[i] == v) {
1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = v->fNext;
1143e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fPrev;
1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1146e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fNext;
1147e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v == contours[i]) break;
1148e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1149e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1150e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1151e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1152e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1153e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) {
1154e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = (*vertices)->fNext; v != nullptr; v = v->fNext) {
1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1156e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fPoint = v->fPrev->fPoint;
1157e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1158e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (coincident(v->fPrev->fPoint, v->fPoint)) {
1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas            merge_vertices(v->fPrev, v, vertices, c, alloc);
1160e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1161e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1162e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1166e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) {
1167e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* vertices = nullptr;
1168e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i]; v != nullptr;) {
1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* vNext = v->fNext;
1172f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            connect(v->fPrev, v, alloc, c);
1173e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (prev) {
1174e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev->fNext = v;
1175e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prev;
1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertices = v;
1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1179e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = v;
1180e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = vNext;
1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v == contours[i]) break;
1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1184e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
1185e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = vertices->fPrev = nullptr;
1186e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1187e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return vertices;
1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction.
1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1192e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c);
1193e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1194e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) {
1195e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* fast;
1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* slow;
1197e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!v || !v->fNext) {
1198e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pFront = v;
1199e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pBack = nullptr;
1200e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
1201e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow = v;
1202e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fast = v->fNext;
1203e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas        while (fast != nullptr) {
1205e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fast = fast->fNext;
1206e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (fast != nullptr) {
1207e9709e831954c3427d5cb839e84221a177bfedebethannicholas                slow = slow->fNext;
1208e9709e831954c3427d5cb839e84221a177bfedebethannicholas                fast = fast->fNext;
1209e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1210e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1211e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1212e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pFront = v;
1213e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *pBack = slow->fNext;
1214e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow->fNext->fPrev = nullptr;
1215e9709e831954c3427d5cb839e84221a177bfedebethannicholas        slow->fNext = nullptr;
1216e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1217e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1218e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1219e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_sort(Vertex** head, Comparator& c) {
1220e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!*head || !(*head)->fNext) {
1221e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1222e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1223e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1224e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* a;
1225e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* b;
1226e9709e831954c3427d5cb839e84221a177bfedebethannicholas    front_back_split(*head, &a, &b);
1227e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1228e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_sort(&a, c);
1229e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_sort(&b, c);
1230e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1231e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = sorted_merge(a, b, c);
1232e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1233e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1234e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) {
1235e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList vertices;
1236e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1237e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (a && b) {
1238e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(a->fPoint, b->fPoint)) {
1239e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = a->fNext;
1240e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            vertices.append(a);
1241e9709e831954c3427d5cb839e84221a177bfedebethannicholas            a = next;
1242e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1243e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = b->fNext;
1244e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            vertices.append(b);
1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas            b = next;
1246e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1247e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1248e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (a) {
1249e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        vertices.insert(a, vertices.fTail, a->fNext);
1250e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1251e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (b) {
1252e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        vertices.insert(b, vertices.fTail, b->fNext);
1253e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1254e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    return vertices.fHead;
1255e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1256e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1257e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1258e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1259e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) {
1260e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("simplifying complex polygons\n");
1261e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1262e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1263e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1264e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1265e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1266e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1267f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1268e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1269e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEnclosingEdge = nullptr;
1270e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* rightEnclosingEdge = nullptr;
1271e9709e831954c3427d5cb839e84221a177bfedebethannicholas        bool restartChecks;
1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas        do {
1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas            restartChecks = false;
1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas            find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1275e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v->fFirstEdgeBelow) {
1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (Edge* edge = v->fFirstEdgeBelow; edge != nullptr; edge = edge->fNextEdgeBelow) {
1277e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1278e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1280e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1285e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1286e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1287e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1288e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                        &activeEdges, c, alloc)) {
1289e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1290e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = pv;
1291e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    restartChecks = true;
1293e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } while (restartChecks);
1297f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (v->fAlpha == 0) {
1298f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
1299f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
1300f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1301f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1302f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1303e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1304e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(e, &activeEdges);
1305e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1306e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEdge = leftEnclosingEdge;
1307e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1308e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(e, leftEdge, &activeEdges);
1309e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge = e;
1310e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1311e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fProcessed = true;
1312e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1314e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1315e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons.
1316e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1317e9709e831954c3427d5cb839e84221a177bfedebethannicholasPoly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) {
1318e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("tessellating simple polygons\n");
1319e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1320e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* polys = nullptr;
1321e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1322e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1323e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1324e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1325e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1326f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1327e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1328e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEnclosingEdge = nullptr;
1329e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* rightEnclosingEdge = nullptr;
1330e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1331e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* leftPoly = nullptr;
1332e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* rightPoly = nullptr;
1333e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = v->fLastEdgeAbove->fRightPoly;
1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1338e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1339e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges above:\n");
1342e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1343e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1345e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1346e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges below:\n");
1347e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1349e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1350e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1351e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1352e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1353e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (leftPoly) {
1354531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
1355e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1356e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (rightPoly) {
1357531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
1358e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* leftEdge = e;
1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* rightEdge = e->fNextEdgeAbove;
1362e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
1363e9709e831954c3427d5cb839e84221a177bfedebethannicholas                remove_edge(leftEdge, &activeEdges);
1364e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftEdge->fRightPoly) {
1365531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1367531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (rightEdge->fLeftPoly) {
1368531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
1369e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1370e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1371e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(v->fLastEdgeAbove, &activeEdges);
1372e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeBelow) {
1373e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftPoly && rightPoly && leftPoly != rightPoly) {
1374e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1375e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    rightPoly->fPartner = leftPoly;
1376e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftPoly->fPartner = rightPoly;
1377e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1378e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1379e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1380e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeBelow) {
1381e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeAbove) {
138293e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco                if (leftPoly && rightPoly) {
1383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    if (leftPoly == rightPoly) {
1384531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1385531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1386531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 leftPoly->fWinding, alloc);
1387531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftEnclosingEdge->fRightPoly = leftPoly;
1388531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        } else {
1389531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1390531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 rightPoly->fWinding, alloc);
1391531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightEnclosingEdge->fLeftPoly = rightPoly;
1392531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        }
1393e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1394531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    Edge* join = ALLOC_NEW(Edge, (leftPoly->lastVertex(), v, 1), alloc);
1395531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1396531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1399e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Edge* leftEdge = v->fFirstEdgeBelow;
1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge->fLeftPoly = leftPoly;
1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas                 rightEdge = rightEdge->fNextEdgeBelow) {
1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas                insert_edge(rightEdge, leftEdge, &activeEdges);
1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1406e9709e831954c3427d5cb839e84221a177bfedebethannicholas                winding += leftEdge->fWinding;
1407e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (winding != 0) {
1408e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    Poly* poly = new_poly(&polys, v, winding, alloc);
1409e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1410e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1411e9709e831954c3427d5cb839e84221a177bfedebethannicholas                leftEdge = rightEdge;
1412e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1413e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fLastEdgeBelow->fRightPoly = rightPoly;
1414e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1415e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1416e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("\nactive edges:\n");
1417e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1418e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1419e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1420e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1421e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1422e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1423e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return polys;
1424e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1425e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1426f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancobool is_boundary_edge(Edge* edge, SkPath::FillType fillType) {
1427f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return apply_fill_type(fillType, edge->fLeftPoly) !=
1428f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco           apply_fill_type(fillType, edge->fRightPoly);
1429f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
14309992bdef8ae97b3e5b109d278ccfab84c66bcbf0senorblanco
1431f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancobool is_boundary_start(Edge* edge, SkPath::FillType fillType) {
1432f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return !apply_fill_type(fillType, edge->fLeftPoly) &&
1433f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            apply_fill_type(fillType, edge->fRightPoly);
1434f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1435f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1436f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoVertex* remove_non_boundary_edges(Vertex* vertices, SkPath::FillType fillType,
1437f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                  SkChunkAlloc& alloc) {
1438f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1439f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) {
1440f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            Edge* next = e->fNextEdgeBelow;
1441f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (!is_boundary_edge(e, fillType)) {
1442f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                remove_edge_above(e);
1443f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                remove_edge_below(e);
1444f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1445f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = next;
1446f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1447682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1448f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return vertices;
1449f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1450f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1451f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid get_edge_normal(const Edge* e, SkVector* normal) {
145249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    normal->setNormalize(SkDoubleToScalar(-e->fLine.fB) * e->fWinding,
145349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco                         SkDoubleToScalar(e->fLine.fA) * e->fWinding);
1454f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1455f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1456f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1457f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1458f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// invert on stroking.
1459f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1460f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid simplify_boundary(EdgeList* boundary, Comparator& c, SkChunkAlloc& alloc) {
1461f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1462f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkVector prevNormal;
1463f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    get_edge_normal(prevEdge, &prevNormal);
1464f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr;) {
1465f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1467f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        double dist = e->dist(prev->fPoint);
1468f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkVector normal;
1469f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        get_edge_normal(e, &normal);
147049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        float denom = 0.25f * static_cast<float>(e->fLine.magSq());
1471f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
1472f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            Edge* join = new_edge(prev, next, alloc, c);
1473f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            insert_edge(join, e, boundary);
1474f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(prevEdge, boundary);
1475f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(e, boundary);
1476f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (join->fLeft && join->fRight) {
1477f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = join->fLeft;
1478f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = join;
1479f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else {
1480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = boundary->fTail;
1481f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1482f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1483f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            get_edge_normal(prevEdge, &prevNormal);
1484f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1485f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevEdge = e;
1486f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevNormal = normal;
1487f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = e->fRight;
1488f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1489f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1490f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1491f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1492f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1493f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1494f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// new antialiased mesh from those vertices.
1495f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1496f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkChunkAlloc& alloc) {
1497f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    EdgeList outerContour;
1498f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1499f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    float radius = 0.5f;
150049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
150149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line prevInner(prevEdge->fTop, prevEdge->fBottom);
1502f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevInner.fC -= offset;
150349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line prevOuter(prevEdge->fTop, prevEdge->fBottom);
1504f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevOuter.fC += offset;
1505f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList innerVertices;
1506f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList outerVertices;
1507f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkScalar innerCount = SK_Scalar1, outerCount = SK_Scalar1;
1508f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
150949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
151049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        Line inner(e->fTop, e->fBottom);
1511f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        inner.fC -= offset;
151249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        Line outer(e->fTop, e->fBottom);
1513f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outer.fC += offset;
1514f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint innerPoint, outerPoint;
151549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (prevInner.intersect(inner, &innerPoint) &&
151649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            prevOuter.intersect(outer, &outerPoint)) {
1517f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            Vertex* innerVertex = ALLOC_NEW(Vertex, (innerPoint, 255), alloc);
1518f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            Vertex* outerVertex = ALLOC_NEW(Vertex, (outerPoint, 0), alloc);
1519f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (innerVertices.fTail && outerVertices.fTail) {
1520f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                Edge innerEdge(innerVertices.fTail, innerVertex, 1);
1521f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                Edge outerEdge(outerVertices.fTail, outerVertex, 1);
1522f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                SkVector innerNormal;
1523f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                get_edge_normal(&innerEdge, &innerNormal);
1524f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                SkVector outerNormal;
1525f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                get_edge_normal(&outerEdge, &outerNormal);
1526f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                SkVector normal;
1527f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                get_edge_normal(prevEdge, &normal);
1528f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                if (normal.dot(innerNormal) < 0) {
1529f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    innerPoint += innerVertices.fTail->fPoint * innerCount;
1530f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    innerCount++;
1531f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    innerPoint *= SkScalarInvert(innerCount);
1532f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    innerVertices.fTail->fPoint = innerVertex->fPoint = innerPoint;
1533f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                } else {
1534f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    innerCount = SK_Scalar1;
1535f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                }
1536f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                if (normal.dot(outerNormal) < 0) {
1537f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    outerPoint += outerVertices.fTail->fPoint * outerCount;
1538f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    outerCount++;
1539f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    outerPoint *= SkScalarInvert(outerCount);
1540f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    outerVertices.fTail->fPoint = outerVertex->fPoint = outerPoint;
1541f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                } else {
1542f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    outerCount = SK_Scalar1;
1543f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                }
1544f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1545f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            innerVertices.append(innerVertex);
1546f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            outerVertices.append(outerVertex);
1547f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevEdge = e;
1548f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1549f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevInner = inner;
1550f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevOuter = outer;
1551f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1552f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    innerVertices.close();
1553f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    outerVertices.close();
1554f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1555f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* innerVertex = innerVertices.fHead;
1556f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* outerVertex = outerVertices.fHead;
1557f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    // Alternate clockwise and counterclockwise polys, so the tesselator
1558f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    // doesn't cancel out the interior edges.
1559f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!innerVertex || !outerVertex) {
1560f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return;
1561f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1562f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    do {
1563f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        connect(outerVertex->fNext, outerVertex, alloc, c);
1564f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        connect(innerVertex->fNext, innerVertex, alloc, c, 2);
1565f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        connect(innerVertex, outerVertex->fNext, alloc, c, 2);
1566f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        connect(outerVertex, innerVertex, alloc, c, 2);
1567f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* innerNext = innerVertex->fNext;
1568f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* outerNext = outerVertex->fNext;
1569f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(innerVertex);
1570f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(outerVertex);
1571f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        innerVertex = innerNext;
1572f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outerVertex = outerNext;
1573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1574f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1575f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1576f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkChunkAlloc& alloc) {
1577f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool down = is_boundary_start(e, fillType);
1578f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    while (e) {
1579f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e->fWinding = down ? 1 : -1;
1580f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Edge* next;
1581f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        boundary->append(e);
1582f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (down) {
1583f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in clockwise order.
1584f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fNextEdgeAbove)) {
1585f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1586f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fBottom->fLastEdgeBelow)) {
1587f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1588f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fPrevEdgeAbove)) {
1589f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1592f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in counter-clockwise order.
1593f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fPrevEdgeBelow)) {
1594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1595f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fTop->fFirstEdgeAbove)) {
1596f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1597f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fNextEdgeBelow)) {
1598f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1599f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1600f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1601f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        remove_edge_above(e);
1602f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        remove_edge_below(e);
1603f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e = next;
1604f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1605f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1606f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1607f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5b: Extract boundary edges.
1608f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1609f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoEdgeList* extract_boundaries(Vertex* vertices, SkPath::FillType fillType, SkChunkAlloc& alloc) {
1610f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    LOG("extracting boundaries\n");
1611f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    vertices = remove_non_boundary_edges(vertices, fillType, alloc);
1612f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    EdgeList* boundaries = nullptr;
1613f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1614f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        while (v->fFirstEdgeBelow) {
1615f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            EdgeList* boundary = new_contour(&boundaries, alloc);
1616f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc);
1617f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1618f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1619f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return boundaries;
1620f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1621f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1622f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// This is a driver function which calls stages 2-5 in turn.
1623f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1624f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoVertex* contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
1625f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                         Comparator& c, SkChunkAlloc& alloc) {
1626e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1627e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1628e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v = contours[i];
1629e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(v);
1630e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1631e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (v = v->fNext; v != contours[i]; v = v->fNext) {
1632e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1633e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1634e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1635e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1636f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    sanitize_contours(contours, contourCnt, antialias);
1637f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return build_edges(contours, contourCnt, c, alloc);
1638f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1639f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1640f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* mesh_to_polys(Vertex** vertices, SkPath::FillType fillType, Comparator& c,
1641f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    SkChunkAlloc& alloc) {
1642f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!vertices || !*vertices) {
1643e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1644e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1645e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1646e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // Sort vertices in Y (secondarily in X).
1647f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_sort(vertices, c);
1648f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_coincident_vertices(vertices, c, alloc);
1649e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1650f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Vertex* v = *vertices; v != nullptr; v = v->fNext) {
1651e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static float gID = 0.0f;
1652e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID = gID++;
1653e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1654e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1655f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    simplify(*vertices, c, alloc);
1656f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return tessellate(*vertices, alloc);
1657f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1658f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1659f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1660f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                        const SkRect& pathBounds, bool antialias,
1661f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                        SkChunkAlloc& alloc) {
1662f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Comparator c;
1663f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (pathBounds.width() > pathBounds.height()) {
1664f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        c.sweep_lt = sweep_lt_horiz;
1665f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        c.sweep_gt = sweep_gt_horiz;
1666f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } else {
1667f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        c.sweep_lt = sweep_lt_vert;
1668f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        c.sweep_gt = sweep_gt_vert;
1669f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1670f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* mesh = contours_to_mesh(contours, contourCnt, antialias, c, alloc);
1671f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = mesh_to_polys(&mesh, fillType, c, alloc);
1672f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (antialias) {
1673f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc);
1674f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        VertexList aaMesh;
1675f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) {
1676f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            simplify_boundary(boundary, c, alloc);
1677f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (boundary->fCount > 2) {
1678f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
1679f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1680f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1681f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return mesh_to_polys(&aaMesh.fHead, SkPath::kWinding_FillType, c, alloc);
1682f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1683f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return polys;
1684f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1685f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1686f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1687f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1688f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                         void* data) {
1689f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Poly* poly = polys; poly; poly = poly->fNext) {
1690f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1691f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = poly->emit(aaParams, data);
1692f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1693f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1694f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return data;
1695e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1696e9709e831954c3427d5cb839e84221a177bfedebethannicholas
16979d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1698f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    int contourCnt, SkChunkAlloc& alloc, bool antialias, bool* isLinear) {
1699e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1700e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (SkPath::IsInverseFillType(fillType)) {
1701e9709e831954c3427d5cb839e84221a177bfedebethannicholas        contourCnt++;
1702e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1703e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
1704e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1705e9709e831954c3427d5cb839e84221a177bfedebethannicholas    path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
1706f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1707f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                             antialias, alloc);
1708e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1709e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17109d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryvoid get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt,
1711e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                         int* sizeEstimate) {
1712e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance);
1713e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts <= 0) {
1714e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contourCnt = 0;
1715e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1716e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1717e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts > ((int)SK_MaxU16 + 1)) {
1718e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1719e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contourCnt = 0;
1720e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1721e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1722e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // For the initial size of the chunk allocator, estimate based on the point count:
1723e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // one vertex per point for the initial passes, plus two for the vertices in the
1724e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // resulting Polys, since the same point may end up in two Polys.  Assume minimal
1725e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // connectivity of one Edge per Vertex (will grow for intersections).
1726e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge));
1727e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1728e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1729e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) {
1730e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = 0;
1731e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1732f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
1733e9709e831954c3427d5cb839e84221a177bfedebethannicholas            count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1734e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1735e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1736e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return count;
1737e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1738e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1739e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1740e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1741e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator {
1742e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1743e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1744e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17459d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1746f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1747f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    bool canTweakAlphaForCoverage, bool* isLinear) {
1748e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int contourCnt;
1749e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int sizeEstimate;
1750e9709e831954c3427d5cb839e84221a177bfedebethannicholas    get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1751e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1752e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *isLinear = true;
1753e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1754e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1755e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkChunkAlloc alloc(sizeEstimate);
1756f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1757f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                isLinear);
1758e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1759e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1760e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1761e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1762e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1763e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1764f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* verts = vertexAllocator->lock(count);
17656599efffeef3168dfc68dca99c30454c5c23b859senorblanco    if (!verts) {
1766e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Could not allocate vertices\n");
1767e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1768e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1769f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1770f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    LOG("emitting %d verts\n", count);
1771f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    AAParams aaParams;
1772f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1773f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fColor = color;
1774f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1775f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1776f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1777f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                       / vertexAllocator->stride());
1778e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
17796599efffeef3168dfc68dca99c30454c5c23b859senorblanco    vertexAllocator->unlock(actualCount);
1780e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1781e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1782e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17839d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1784e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   GrTessellator::WindingVertex** verts) {
1785e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int contourCnt;
1786e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int sizeEstimate;
1787e9709e831954c3427d5cb839e84221a177bfedebethannicholas    get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
1788e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1789e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1790e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1791e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkChunkAlloc alloc(sizeEstimate);
1792e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLinear;
1793f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
1794e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1795e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1796e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1797e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *verts = nullptr;
1798e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1799e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1800e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *verts = new GrTessellator::WindingVertex[count];
1802e9709e831954c3427d5cb839e84221a177bfedebethannicholas    GrTessellator::WindingVertex* vertsEnd = *verts;
1803e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* points = new SkPoint[count];
1804e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* pointsEnd = points;
1805e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1806f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1807e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkPoint* start = pointsEnd;
1808f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
1809e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (start != pointsEnd) {
1810e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fPos = *start;
1811e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fWinding = poly->fWinding;
1812e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++start;
1813e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++vertsEnd;
1814e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1815e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1816e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1817e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int actualCount = static_cast<int>(vertsEnd - *verts);
1818e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
1819e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(pointsEnd - points == actualCount);
1820e9709e831954c3427d5cb839e84221a177bfedebethannicholas    delete[] points;
1821e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1822e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1823e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1824e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1825