GrTessellator.cpp revision 6641212397be28b6263e91340b7ed76bd29b3035
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
135cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby#include "SkArenaAlloc.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
93e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace {
94e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9511f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen Whiteconst int kArenaChunkSize = 16 * 1024;
9611f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White
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
177e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
17816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY);
179e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
180e9709e831954c3427d5cb839e84221a177bfedebethannicholas
181e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_vert(const SkPoint& a, const SkPoint& b) {
18216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX);
183e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
184e9709e831954c3427d5cb839e84221a177bfedebethannicholas
18516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitestruct Comparator {
18616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    enum class Direction { kVertical, kHorizontal };
18716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Comparator(Direction direction) : fDirection(direction) {}
18816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
18916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
19016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
19116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Direction fDirection;
19216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White};
19316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White
194f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
195f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!aaParams) {
196f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint* d = static_cast<SkPoint*>(data);
197f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        *d++ = v->fPoint;
198f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
199f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
200f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (aaParams->fTweakAlpha) {
201f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
202f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d->fPosition = v->fPoint;
2038c8fceff4d4e8ec09f29748d77ed5510697a2995lsalzman        d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
204f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d++;
205f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
206f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
207f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
208f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fPosition = v->fPoint;
209f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fColor = aaParams->fColor;
210f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
211f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d++;
212f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return d;
213e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
214e9709e831954c3427d5cb839e84221a177bfedebethannicholas
215f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
21692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("emit_triangle (%g, %g) %d\n", v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
21792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("              (%g, %g) %d\n", v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
21892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("              (%g, %g) %d\n", v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
219f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#if TESSELLATOR_WIREFRAME
220f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
221f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
222f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
223f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
224f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
225f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
226e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
227f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
228f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
229f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
230e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
231e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return data;
232e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
233e9709e831954c3427d5cb839e84221a177bfedebethannicholas
234e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancostruct VertexList {
235e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList() : fHead(nullptr), fTail(nullptr) {}
23616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
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    }
248bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    void remove(Vertex* v) {
249bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
250bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    }
251f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
252f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
253f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fNext = fHead;
254f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fPrev = fTail;
255f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
256f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
257e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco};
258e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco
259f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Round to nearest quarter-pixel. This is used for screenspace tessellation.
260f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
261f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void round(SkPoint* p) {
262f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
263f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
264f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
265f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
26649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
26749df8d17c56ee08ecf860289d501913d356f67dcsenorblancostruct Line {
26849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
26949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(const SkPoint& p, const SkPoint& q)
27049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        : fA(static_cast<double>(q.fY) - p.fY)      // a = dY
27149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fB(static_cast<double>(p.fX) - q.fX)      // b = -dX
27249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fC(static_cast<double>(p.fY) * q.fX -     // c = cross(q, p)
27349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco             static_cast<double>(p.fX) * q.fY) {}
27449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double dist(const SkPoint& p) const {
27549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * p.fX + fB * p.fY + fC;
27649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
27749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double magSq() const {
27849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * fA + fB * fB;
27949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
28049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
28149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    // Compute the intersection of two (infinite) Lines.
28249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    bool intersect(const Line& other, SkPoint* point) {
28349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fA * other.fB - fB * other.fA;
28449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (denom == 0.0) {
28549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            return false;
28649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        }
28749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double scale = 1.0f / denom;
28849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale);
28949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale);
29049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return true;
29149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
29249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double fA, fB, fC;
29349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco};
29449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
295e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
296e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
297e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
298e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
299e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g.,
300e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
301e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division
302e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but
303e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case.
304e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
305e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic
306e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
307e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being
308e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
309e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
310e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file).
311e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
312e9709e831954c3427d5cb839e84221a177bfedebethannicholas
313e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge {
3142f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    enum class Type { kInner, kOuter, kConnector };
3152f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Edge(Vertex* top, Vertex* bottom, int winding, Type type)
316e9709e831954c3427d5cb839e84221a177bfedebethannicholas        : fWinding(winding)
317e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTop(top)
318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fBottom(bottom)
3192f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        , fType(type)
320e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeft(nullptr)
321e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fRight(nullptr)
322e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeAbove(nullptr)
323e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeAbove(nullptr)
324e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeBelow(nullptr)
325e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeBelow(nullptr)
326e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeftPoly(nullptr)
327531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPoly(nullptr)
328531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyPrev(nullptr)
329531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyNext(nullptr)
330531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPolyPrev(nullptr)
33170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fRightPolyNext(nullptr)
33270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInLeftPoly(false)
33349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fUsedInRightPoly(false)
33449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fLine(top, bottom) {
335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
336e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int      fWinding;          // 1 == edge goes downward; -1 = edge goes upward.
337e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fTop;              // The top vertex in vertex-sort-order (sweep_lt).
338e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fBottom;           // The bottom vertex in vertex-sort-order.
3392f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Type     fType;
340e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fLeft;             // The linked list of edges in the active edge list.
341e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fRight;            // "
342e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeAbove;    // The linked list of edges in the bottom Vertex's "edges above".
343e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeAbove;    // "
344e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeBelow;    // The linked list of edges in the top Vertex's "edges below".
345e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeBelow;    // "
346e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fLeftPoly;         // The Poly to the left of this edge, if any.
347e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fRightPoly;        // The Poly to the right of this edge, if any.
348531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyPrev;
349531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyNext;
350531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyPrev;
351531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyNext;
35270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInLeftPoly;
35370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInRightPoly;
35449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line     fLine;
355e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double dist(const SkPoint& p) const {
35649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(p);
357e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
358e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isRightOf(Vertex* v) const {
35949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) < 0.0;
360e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
361e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLeftOf(Vertex* v) const {
36249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) > 0.0;
363e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
364e9709e831954c3427d5cb839e84221a177bfedebethannicholas    void recompute() {
36549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        fLine = Line(fTop, fBottom);
366e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
367dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) {
368e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("intersecting %g -> %g with %g -> %g\n",
369e9709e831954c3427d5cb839e84221a177bfedebethannicholas               fTop->fID, fBottom->fID,
370e9709e831954c3427d5cb839e84221a177bfedebethannicholas               other.fTop->fID, other.fBottom->fID);
371e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fTop == other.fTop || fBottom == other.fBottom) {
372e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
373e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
37449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
375e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom == 0.0) {
376e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
377e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
3788a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
3798a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
3808a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
3818a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double tNumer = dy * fLine.fB + dx * fLine.fA;
382e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
383e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // This saves us doing the divide below unless absolutely necessary.
384e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
385e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
386e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
387e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
388e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double s = sNumer / denom;
389e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(s >= 0.0 && s <= 1.0);
39049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
39149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
39256158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        if (alpha) {
39392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            if (fType == Type::kConnector) {
39492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
39592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            } else if (other.fType == Type::kConnector) {
39692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                double t = tNumer / denom;
39792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
39856158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
39956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White                *alpha = 0;
40056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else {
40192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = 255;
40256158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            }
40356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        }
404e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return true;
405e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
406f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco};
407f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
408f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct EdgeList {
4095ad721e94682b51b9f773d95704f11990468fef6Stephen White    EdgeList() : fHead(nullptr), fTail(nullptr) {}
410f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fHead;
411f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fTail;
412f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void insert(Edge* edge, Edge* prev, Edge* next) {
413f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
414f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
415f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void append(Edge* e) {
416f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert(e, fTail, nullptr);
417f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
418f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void remove(Edge* edge) {
419f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
420f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
421f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
422f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
423f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fRight = fHead;
424f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fLeft = fTail;
425f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
426f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
427f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool contains(Edge* edge) const {
428f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return edge->fLeft || edge->fRight || fHead == edge;
429e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
430e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
431e9709e831954c3427d5cb839e84221a177bfedebethannicholas
432e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
433e9709e831954c3427d5cb839e84221a177bfedebethannicholas
434e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly {
435531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly(Vertex* v, int winding)
436531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        : fFirstVertex(v)
437531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fWinding(winding)
438e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fHead(nullptr)
439e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTail(nullptr)
440e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNext(nullptr)
441e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPartner(nullptr)
442e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fCount(0)
443e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {
444e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
445e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static int gID = 0;
446e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fID = gID++;
447e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("*** created Poly %d\n", fID);
448e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
449e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
450531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    typedef enum { kLeft_Side, kRight_Side } Side;
451e9709e831954c3427d5cb839e84221a177bfedebethannicholas    struct MonotonePoly {
452531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        MonotonePoly(Edge* edge, Side side)
453531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            : fSide(side)
454531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fFirstEdge(nullptr)
455531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fLastEdge(nullptr)
456e9709e831954c3427d5cb839e84221a177bfedebethannicholas            , fPrev(nullptr)
457531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fNext(nullptr) {
458531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            this->addEdge(edge);
459531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        }
460e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Side          fSide;
461531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fFirstEdge;
462531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fLastEdge;
463e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fPrev;
464e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fNext;
465531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        void addEdge(Edge* edge) {
466e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            if (fSide == kRight_Side) {
467212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInRightPoly);
468531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
469531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
47070f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInRightPoly = true;
471e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
472212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInLeftPoly);
473531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
474531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
47570f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInLeftPoly = true;
476e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
477e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
478e9709e831954c3427d5cb839e84221a177bfedebethannicholas
479f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        void* emit(const AAParams* aaParams, void* data) {
480531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Edge* e = fFirstEdge;
481531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            VertexList vertices;
482531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            vertices.append(e->fTop);
483531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (e != nullptr) {
484531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (kRight_Side == fSide) {
485531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.append(e->fBottom);
486531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fRightPolyNext;
487531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                } else {
488531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.prepend(e->fBottom);
489531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fLeftPolyNext;
490531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                }
491531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            }
492531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Vertex* first = vertices.fHead;
493e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* v = first->fNext;
494531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (v != vertices.fTail) {
495e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(v && v->fPrev && v->fNext);
496e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* prev = v->fPrev;
497e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* curr = v;
498e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* next = v->fNext;
499e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
500e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
501e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
502e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
503e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (ax * by - ay * bx >= 0.0) {
504f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    data = emit_triangle(prev, curr, next, aaParams, data);
505e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fPrev->fNext = v->fNext;
506e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fNext->fPrev = v->fPrev;
507e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (v->fPrev == first) {
508e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fNext;
509e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    } else {
510e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fPrev;
511e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
512e9709e831954c3427d5cb839e84221a177bfedebethannicholas                } else {
513e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v = v->fNext;
514e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
515e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
516e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
517e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
518e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
5195cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
52070f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        LOG("addEdge (%g -> %g) to poly %d, %s side\n",
52170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco               e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
522e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* partner = fPartner;
523e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* poly = this;
524212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        if (side == kRight_Side) {
525212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInRightPoly) {
526212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
527212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
528212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        } else {
529212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInLeftPoly) {
530212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
531212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
532212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        }
533e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (partner) {
534e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fPartner = partner->fPartner = nullptr;
535e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
536531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        if (!fTail) {
5375cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            fHead = fTail = alloc.make<MonotonePoly>(e, side);
538531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount += 2;
53993e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco        } else if (e->fBottom == fTail->fLastEdge->fBottom) {
54093e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco            return poly;
541531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else if (side == fTail->fSide) {
542531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
543531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
544531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else {
5455cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
546531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
547531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
548e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (partner) {
549531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                partner->addEdge(e, side, alloc);
550e9709e831954c3427d5cb839e84221a177bfedebethannicholas                poly = partner;
551e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
5525cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
553531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                m->fPrev = fTail;
554531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail->fNext = m;
555531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail = m;
556e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
557e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
558e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return poly;
559e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
560f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* emit(const AAParams* aaParams, void *data) {
561e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fCount < 3) {
562e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
563e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
564e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("emit() %d, size %d\n", fID, fCount);
565e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
566f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = m->emit(aaParams, data);
567e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
568e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return data;
569e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
570531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
571531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* fFirstVertex;
572e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fWinding;
573e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fHead;
574e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fTail;
575e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fNext;
576e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fPartner;
577e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fCount;
578e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
579e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fID;
580e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
581e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
582e9709e831954c3427d5cb839e84221a177bfedebethannicholas
583e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
584e9709e831954c3427d5cb839e84221a177bfedebethannicholas
585e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) {
586e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a == b;
587e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
588e9709e831954c3427d5cb839e84221a177bfedebethannicholas
5895cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
5905cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* poly = alloc.make<Poly>(v, winding);
591e9709e831954c3427d5cb839e84221a177bfedebethannicholas    poly->fNext = *head;
592e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = poly;
593e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return poly;
594e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
595e9709e831954c3427d5cb839e84221a177bfedebethannicholas
596e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
5975cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                SkArenaAlloc& alloc) {
5985cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Vertex* v = alloc.make<Vertex>(p, 255);
599e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
600e9709e831954c3427d5cb839e84221a177bfedebethannicholas    static float gID = 0.0f;
601e9709e831954c3427d5cb839e84221a177bfedebethannicholas    v->fID = gID++;
602e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
603e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
604e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = v;
605e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fPrev = prev;
606e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
607e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = v;
608e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
609e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return v;
610e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
611e9709e831954c3427d5cb839e84221a177bfedebethannicholas
612e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0,
613e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p1,
614e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p2,
615e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkScalar tolSqd,
616e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex* prev,
617e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex** head,
618e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  int pointsLeft,
6195cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                  SkArenaAlloc& alloc) {
620e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
621e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
622e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p2, prev, head, alloc);
623e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
624e9709e831954c3427d5cb839e84221a177bfedebethannicholas
625e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
626e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
627e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
628e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
629e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
630e9709e831954c3427d5cb839e84221a177bfedebethannicholas
631e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
632e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
633e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
634e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
635e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
636e9709e831954c3427d5cb839e84221a177bfedebethannicholas
637e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0,
638e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p1,
639e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p2,
640e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p3,
641e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkScalar tolSqd,
642e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex* prev,
643e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex** head,
644e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              int pointsLeft,
6455cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                              SkArenaAlloc& alloc) {
646e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
647e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
648e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
649e9709e831954c3427d5cb839e84221a177bfedebethannicholas        !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
650e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p3, prev, head, alloc);
651e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
652e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
653e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
654e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
655e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
656e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
657e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r[] = {
658e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
659e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
660e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
661e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
662e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
663e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
664e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
665e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
666e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
667e9709e831954c3427d5cb839e84221a177bfedebethannicholas
668e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
669e9709e831954c3427d5cb839e84221a177bfedebethannicholas
670e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
6715cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) {
672e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar toleranceSqd = tolerance * tolerance;
673e9709e831954c3427d5cb839e84221a177bfedebethannicholas
674e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint pts[4];
675e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool done = false;
676e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *isLinear = true;
677e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::Iter iter(path, false);
678e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
679e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* head = nullptr;
680e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (path.isInverseFillType()) {
681e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPoint quad[4];
682e9709e831954c3427d5cb839e84221a177bfedebethannicholas        clipBounds.toQuad(quad);
6837ab96e92196dd74d5b95d33c8477b256813f3046senorblanco        for (int i = 3; i >= 0; i--) {
684e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = append_point_to_contour(quad[i], prev, &head, alloc);
685e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
686e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head->fPrev = prev;
687e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = head;
688e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contours++ = head;
689e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head = prev = nullptr;
690e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
691e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoConicToQuads converter;
692e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (!done) {
693e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPath::Verb verb = iter.next(pts);
694e9709e831954c3427d5cb839e84221a177bfedebethannicholas        switch (verb) {
695e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kConic_Verb: {
696e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkScalar weight = iter.conicWeight();
697e9709e831954c3427d5cb839e84221a177bfedebethannicholas                const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
698e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (int i = 0; i < converter.countQuads(); ++i) {
699e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
700e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
701e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                     toleranceSqd, prev, &head, pointsLeft, alloc);
702e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    quadPts += 2;
703e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
704e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
705e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
706e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
707e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kMove_Verb:
708e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
709e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
710e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
711e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
712e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
713e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
714e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[0], prev, &head, alloc);
715e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
716e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kLine_Verb: {
717e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[1], prev, &head, alloc);
718e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
719e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
720e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kQuad_Verb: {
721e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
722e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
723e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                 &head, pointsLeft, alloc);
724e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
725e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
726e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
727e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kCubic_Verb: {
728e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
729e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
730e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                toleranceSqd, prev, &head, pointsLeft, alloc);
731e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
732e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
733e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
734e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kClose_Verb:
735e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
736e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
737e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
738e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
739e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
740e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
741e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
742e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kDone_Verb:
743e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
744e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
745e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
746e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
747e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
748e9709e831954c3427d5cb839e84221a177bfedebethannicholas                done = true;
749e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
750e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
751e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
752e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
753e9709e831954c3427d5cb839e84221a177bfedebethannicholas
754497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, int winding) {
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:
7617ab96e92196dd74d5b95d33c8477b256813f3046senorblanco            return winding == 1;
762e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseEvenOdd_FillType:
763e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) == 1;
764e9709e831954c3427d5cb839e84221a177bfedebethannicholas        default:
765e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkASSERT(false);
766e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
767e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
768e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
769e9709e831954c3427d5cb839e84221a177bfedebethannicholas
770497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
771497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    return poly && apply_fill_type(fillType, poly->fWinding);
772497890630b80a381a2ac4cbb9114b0320560bf8cStephen White}
773497890630b80a381a2ac4cbb9114b0320560bf8cStephen White
7745cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
7752f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
776e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = winding < 0 ? next : prev;
777e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = winding < 0 ? prev : next;
7785cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    return alloc.make<Edge>(top, bottom, winding, type);
779e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
780e9709e831954c3427d5cb839e84221a177bfedebethannicholas
781e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) {
782e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
783f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(edges->contains(edge));
784f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->remove(edge);
785e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
786e9709e831954c3427d5cb839e84221a177bfedebethannicholas
787e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
788e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
789f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(!edges->contains(edge));
790e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = prev ? prev->fRight : edges->fHead;
791f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->insert(edge, prev, next);
792e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
793e9709e831954c3427d5cb839e84221a177bfedebethannicholas
794e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
795e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (v->fFirstEdgeAbove) {
796e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *left = v->fFirstEdgeAbove->fLeft;
797e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *right = v->fLastEdgeAbove->fRight;
798e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
799e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
800e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = nullptr;
801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev;
802e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
803e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (prev->isLeftOf(v)) {
804e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
805e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
806e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next = prev;
807e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
808e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
809e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
810e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
811e9709e831954c3427d5cb839e84221a177bfedebethannicholas
812e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
813e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
814e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
815e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = edges->fHead; next != nullptr; next = next->fRight) {
816e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if ((c.sweep_lt(next->fTop->fPoint, edge->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
817e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White            (c.sweep_lt(edge->fTop->fPoint, next->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
818e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
819e9709e831954c3427d5cb839e84221a177bfedebethannicholas             next->isRightOf(edge->fBottom)) ||
820e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
821e9709e831954c3427d5cb839e84221a177bfedebethannicholas             edge->isLeftOf(next->fBottom))) {
822e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
823e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
824e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
825e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
826e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
827e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
828e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
829e9709e831954c3427d5cb839e84221a177bfedebethannicholas
830e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
8312f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (!activeEdges) {
8322f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
8332f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
8342f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (activeEdges->contains(edge)) {
835e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
836e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(edge, activeEdges);
837e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
838e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
839e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* left;
840e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* right;
841e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(edge, activeEdges, c, &left, &right);
842e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge(edge, left, activeEdges);
843e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
844e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
845e9709e831954c3427d5cb839e84221a177bfedebethannicholas
846e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
847e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
848e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
849e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
850e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
851e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
852e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
853e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
854e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
855e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fTop)) {
856e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
857e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
858e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
859e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
860e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
861e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
862e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
863e9709e831954c3427d5cb839e84221a177bfedebethannicholas
864e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
865e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
866e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
867e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
868e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
869e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
870e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
871e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
872e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
873e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fBottom)) {
874e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
875e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
876e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
877e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
878e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
879e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
880e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
881e9709e831954c3427d5cb839e84221a177bfedebethannicholas
882e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) {
883e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
884e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fBottom->fID);
885e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
886e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
887e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
888e9709e831954c3427d5cb839e84221a177bfedebethannicholas
889e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) {
890e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
891e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID);
892e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
893e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
894e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
895e9709e831954c3427d5cb839e84221a177bfedebethannicholas
896e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid disconnect(Edge* edge)
897e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White{
898e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
899e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
900e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White}
901e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White
902e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid erase_edge(Edge* edge, EdgeList* edges) {
903e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
904e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    disconnect(edge);
905f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (edges && edges->contains(edge)) {
906e9709e831954c3427d5cb839e84221a177bfedebethannicholas        remove_edge(edge, edges);
907e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
908e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
909e9709e831954c3427d5cb839e84221a177bfedebethannicholas
910e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
911e9709e831954c3427d5cb839e84221a177bfedebethannicholas
912e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
913e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
914e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fTop = v;
915e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
916e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_below(edge, v, c);
917e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
918e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
919e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
920e9709e831954c3427d5cb839e84221a177bfedebethannicholas
921e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
922e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
923e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fBottom = v;
924e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
925e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_above(edge, v, c);
926e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
927e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
928e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
929e9709e831954c3427d5cb839e84221a177bfedebethannicholas
930e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
931e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
932e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
933e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
934e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
935e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
936e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
937e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
938e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
939e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, other->fTop, activeEdges, c);
940e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
941e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
942e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(other, edge->fTop, activeEdges, c);
943e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
944e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
945e9709e831954c3427d5cb839e84221a177bfedebethannicholas
946e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
947e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
948e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
949e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
950e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
951e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
952e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
953e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
954e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
955e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(other, edge->fBottom, activeEdges, c);
956e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
957e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
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
9795cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc);
980e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9815cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& 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;
987e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
988e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
989e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (c.sweep_lt(top->fPoint, leftTop->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;
1001e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
1002e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, top, activeEdges, c, alloc);
1003e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (c.sweep_lt(top->fPoint, rightTop->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
10155cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& 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);
1021e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White    } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
1022e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
10245cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby        Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType);
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
10345cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
103548ded38da99c1171ba1bb469f6500f8214e9105cStephen White              int winding_scale = 1) {
1036bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    Edge* edge = new_edge(prev, next, type, c, alloc);
10378a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_below(edge, edge->fTop, c);
10388a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_above(edge, edge->fBottom, c);
103948ded38da99c1171ba1bb469f6500f8214e9105cStephen White    edge->fWinding *= winding_scale;
1040f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_collinear_edges(edge, nullptr, c);
1041f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return edge;
1042f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1043f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1044bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
10455cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    SkArenaAlloc& alloc) {
1046e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1047e9709e831954c3427d5cb839e84221a177bfedebethannicholas        src->fID, dst->fID);
1048f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
1049e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1050e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeAbove;
1051e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, dst, nullptr, c);
1052e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1053e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1054e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1055e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeBelow;
1056e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, dst, nullptr, c);
1057e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1058e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1059bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->remove(src);
1060e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1061e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1062f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancouint8_t max_edge_alpha(Edge* a, Edge* b) {
106356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) {
10642f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 255;
10652f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
10662f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 0;
10672f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else {
10682f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
10692f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White                      SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
10702f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
1071f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1072f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1073e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
10745cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1075e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!edge || !other) {
1076e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1077e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
107856158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    SkPoint p;
107956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    uint8_t alpha;
108056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (edge->intersect(*other, &p, &alpha)) {
1081e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v;
1082e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1083e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1084e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fTop, activeEdges, c, alloc);
1085e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fTop;
1086e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (p == edge->fBottom->fPoint || c.sweep_lt(edge->fBottom->fPoint, p)) {
1087e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fBottom, activeEdges, c, alloc);
1088e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fBottom;
1089e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1090e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fTop, activeEdges, c, alloc);
1091e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fTop;
1092e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (p == other->fBottom->fPoint || c.sweep_lt(other->fBottom->fPoint, p)) {
1093e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fBottom, activeEdges, c, alloc);
1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fBottom;
1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* nextV = edge->fTop;
1097e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(p, nextV->fPoint)) {
1098e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fPrev;
1099e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1100e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(nextV->fPoint, p)) {
1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fNext;
1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* prevV = nextV->fPrev;
1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(prevV->fPoint, p)) {
1105e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = prevV;
1106e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else if (coincident(nextV->fPoint, p)) {
1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = nextV;
1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
11095cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                v = alloc.make<Vertex>(p, alpha);
1110e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fID = (nextV->fID + prevV->fID) * 0.5f;
1115e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1116e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prevV;
1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext = nextV;
1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prevV->fNext = v;
1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV->fPrev = v;
1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1121e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, v, activeEdges, c, alloc);
1122e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, v, activeEdges, c, alloc);
1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
112492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        v->fAlpha = SkTMax(v->fAlpha, alpha);
1125e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return v;
1126e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return nullptr;
1128e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1130f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
1131e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1132e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(contours[i]);
11335926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        if (approximate) {
11345926f2da752d1bff0051fda4137bb967f9e91d5fStephen White            round(&contours[i]->fPrev->fPoint);
11355926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        }
1136e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i];;) {
1137f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (approximate) {
1138f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                round(&v->fPoint);
1139f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1140e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(v->fPrev->fPoint, v->fPoint)) {
1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v->fPrev == v) {
1143e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = nullptr;
1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    break;
1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1146e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev->fNext = v->fNext;
1147e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext->fPrev = v->fPrev;
1148e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (contours[i] == v) {
11495926f2da752d1bff0051fda4137bb967f9e91d5fStephen White                    contours[i] = v->fPrev;
1150e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1151e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fPrev;
1152e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1153e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fNext;
1154e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v == contours[i]) break;
1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1156e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1157e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1158e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas
11605cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1161bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) {
1162e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fPoint = v->fPrev->fPoint;
1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (coincident(v->fPrev->fPoint, v->fPoint)) {
1166bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            merge_vertices(v->fPrev, v, mesh, c, alloc);
1167e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1168e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1172e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1173bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c,
11745cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                 SkArenaAlloc& alloc) {
1175e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i]; v != nullptr;) {
1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* vNext = v->fNext;
1179bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            connect(v->fPrev, v, Edge::Type::kInner, c, alloc);
1180e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (prev) {
1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev->fNext = v;
1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prev;
1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1184bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                mesh->fHead = v;
1185e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1186e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = v;
1187e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = vNext;
1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v == contours[i]) break;
1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
1192bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        prev->fNext = mesh->fHead->fPrev = nullptr;
1193e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1194bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->fTail = prev;
1195e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1197e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction.
1198e9709e831954c3427d5cb839e84221a177bfedebethannicholas
119916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitetemplate <CompareFunc sweep_lt>
120016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitevoid merge_sort(VertexList* vertices) {
120116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* slow = vertices->fHead;
120216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!slow) {
1203e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
120516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* fast = slow->fNext;
120616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!fast) {
120716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return;
120816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
120916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    do {
121016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        fast = fast->fNext;
121116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (fast) {
121216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            fast = fast->fNext;
121316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            slow = slow->fNext;
121416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        }
121516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } while (fast);
121616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList front(vertices->fHead, slow);
121716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList back(slow->fNext, vertices->fTail);
121816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    front.fTail->fNext = back.fHead->fPrev = nullptr;
1219e9709e831954c3427d5cb839e84221a177bfedebethannicholas
122016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&front);
122116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&back);
1222e9709e831954c3427d5cb839e84221a177bfedebethannicholas
122316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    vertices->fHead = vertices->fTail = nullptr;
122416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* a = front.fHead;
122516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* b = back.fHead;
1226e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (a && b) {
122716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (sweep_lt(a->fPoint, b->fPoint)) {
1228e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = a->fNext;
122916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(a);
1230e9709e831954c3427d5cb839e84221a177bfedebethannicholas            a = next;
1231e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1232e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = b->fNext;
123316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(b);
1234e9709e831954c3427d5cb839e84221a177bfedebethannicholas            b = next;
1235e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1236e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1237e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (a) {
123816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(a, vertices->fTail, a->fNext);
1239e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1240e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (b) {
124116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(b, vertices->fTail, b->fNext);
1242e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1243e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1244e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1246e9709e831954c3427d5cb839e84221a177bfedebethannicholas
12475cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) {
1248e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("simplifying complex polygons\n");
1249e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1250bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1251e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1252e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1253e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1254e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1255f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1256e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
12578a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
12588a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1259e9709e831954c3427d5cb839e84221a177bfedebethannicholas        bool restartChecks;
1260e9709e831954c3427d5cb839e84221a177bfedebethannicholas        do {
1261e9709e831954c3427d5cb839e84221a177bfedebethannicholas            restartChecks = false;
1262e9709e831954c3427d5cb839e84221a177bfedebethannicholas            find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1263e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v->fFirstEdgeBelow) {
1264bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
1265e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1266e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1267e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1268e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1269e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1270e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1271e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1275e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                        &activeEdges, c, alloc)) {
1277e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1278e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = pv;
1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1280e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    restartChecks = true;
1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } while (restartChecks);
1285f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (v->fAlpha == 0) {
128648ded38da99c1171ba1bb469f6500f8214e9105cStephen White            if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
128748ded38da99c1171ba1bb469f6500f8214e9105cStephen White                (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
1288f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1289f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1290f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1291e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(e, &activeEdges);
1293e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEdge = leftEnclosingEdge;
1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(e, leftEdge, &activeEdges);
1297e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge = e;
1298e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1299e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fProcessed = true;
1300e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1301e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1302e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1303e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons.
1304e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13055cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
1306e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("tessellating simple polygons\n");
1307e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1308e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* polys = nullptr;
1309bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1310e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1311e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1312e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1314f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1315e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
13168a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
13178a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
13198a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* leftPoly;
13208a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* rightPoly;
1321e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1322e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1323e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = v->fLastEdgeAbove->fRightPoly;
1324e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1325e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1326e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1327e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1328e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1329e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges above:\n");
1330e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1331e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1332e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1333e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges below:\n");
1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1338e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1339e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (leftPoly) {
1342531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
1343e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (rightPoly) {
1345531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
1346e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1347e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* rightEdge = e->fNextEdgeAbove;
13498a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                SkASSERT(rightEdge->isRightOf(e->fTop));
13508a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                remove_edge(e, &activeEdges);
13518a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (e->fRightPoly) {
13528a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                    e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
1353e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
13548a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
1355531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
1356e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1357e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1358e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(v->fLastEdgeAbove, &activeEdges);
1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeBelow) {
1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftPoly && rightPoly && leftPoly != rightPoly) {
1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1362e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    rightPoly->fPartner = leftPoly;
1363e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftPoly->fPartner = rightPoly;
1364e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1365e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1367e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeBelow) {
1368e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeAbove) {
136993e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco                if (leftPoly && rightPoly) {
1370531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    if (leftPoly == rightPoly) {
1371531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1372531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1373531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 leftPoly->fWinding, alloc);
1374531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftEnclosingEdge->fRightPoly = leftPoly;
1375531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        } else {
1376531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1377531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 rightPoly->fWinding, alloc);
1378531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightEnclosingEdge->fLeftPoly = rightPoly;
1379531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        }
1380e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
13815cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
1382531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
1384e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1385e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1386e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Edge* leftEdge = v->fFirstEdgeBelow;
1387e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge->fLeftPoly = leftPoly;
1388e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1389e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1390e9709e831954c3427d5cb839e84221a177bfedebethannicholas                 rightEdge = rightEdge->fNextEdgeBelow) {
1391e9709e831954c3427d5cb839e84221a177bfedebethannicholas                insert_edge(rightEdge, leftEdge, &activeEdges);
1392e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1393e9709e831954c3427d5cb839e84221a177bfedebethannicholas                winding += leftEdge->fWinding;
1394e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (winding != 0) {
1395e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    Poly* poly = new_poly(&polys, v, winding, alloc);
1396e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas                leftEdge = rightEdge;
1399e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fLastEdgeBelow->fRightPoly = rightPoly;
1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("\nactive edges:\n");
1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1406e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1407e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1408e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1409e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1410e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return polys;
1411e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1412e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1413bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
14145cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1415497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    LOG("removing non-boundary edges\n");
1416497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    EdgeList activeEdges;
1417bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
1418497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1419497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            continue;
1420497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1421497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* leftEnclosingEdge;
1422497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* rightEnclosingEdge;
1423497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1424497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        bool prevFilled = leftEnclosingEdge &&
1425497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                          apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1426497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeAbove; e;) {
1427497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            Edge* next = e->fNextEdgeAbove;
1428497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            remove_edge(e, &activeEdges);
1429497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            bool filled = apply_fill_type(fillType, e->fWinding);
1430497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (filled == prevFilled) {
1431e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White                disconnect(e);
1432f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1433497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prevFilled = filled;
1434f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = next;
1435f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1436497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* prev = leftEnclosingEdge;
1437497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1438497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (prev) {
1439497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                e->fWinding += prev->fWinding;
1440497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            }
1441497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            insert_edge(e, prev, &activeEdges);
1442497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prev = e;
1443497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1444682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1445f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1446f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
14476641212397be28b6263e91340b7ed76bd29b3035Stephen White// Note: this is the normal to the edge, but not necessarily unit length.
1448f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid get_edge_normal(const Edge* e, SkVector* normal) {
14496641212397be28b6263e91340b7ed76bd29b3035Stephen White    normal->set(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
14506641212397be28b6263e91340b7ed76bd29b3035Stephen White                SkDoubleToScalar(e->fLine.fB) * e->fWinding);
1451f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1452f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1453f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1454f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1455f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// invert on stroking.
1456f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
14575cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
1458f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1459f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkVector prevNormal;
1460f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    get_edge_normal(prevEdge, &prevNormal);
1461f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr;) {
1462f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1463f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1464f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        double dist = e->dist(prev->fPoint);
1465f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkVector normal;
1466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        get_edge_normal(e, &normal);
14676641212397be28b6263e91340b7ed76bd29b3035Stephen White        double denom = 0.0625f * e->fLine.magSq();
1468f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
1469bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
1470f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            insert_edge(join, e, boundary);
1471f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(prevEdge, boundary);
1472f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(e, boundary);
1473f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (join->fLeft && join->fRight) {
1474f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = join->fLeft;
1475f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = join;
1476f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else {
1477f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = boundary->fTail;
1478f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1479f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            get_edge_normal(prevEdge, &prevNormal);
1481f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1482f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevEdge = e;
1483f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevNormal = normal;
1484f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = e->fRight;
1485f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1486f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1487f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1488f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1489dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagnervoid fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector,
149092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                    Edge* prevEdge, Comparator& c) {
149192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    if (!prev || !next) {
149292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        return;
149392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
149492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
149592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    SkPoint p;
149692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    uint8_t alpha;
1497dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) {
149892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fPoint = next->fPoint = p;
149992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fAlpha = next->fAlpha = alpha;
150092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
150192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White}
150292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White
1503f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1504f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1505f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// new antialiased mesh from those vertices.
1506f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15075cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
15088a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    // A boundary with fewer than 3 edges is degenerate.
15098a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
15108a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        return;
15118a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    }
1512f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1513f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    float radius = 0.5f;
151449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
15156641212397be28b6263e91340b7ed76bd29b3035Stephen White    Line prevInner(prevEdge->fLine);
1516f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevInner.fC -= offset;
15176641212397be28b6263e91340b7ed76bd29b3035Stephen White    Line prevOuter(prevEdge->fLine);
1518f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevOuter.fC += offset;
1519f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList innerVertices;
1520f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList outerVertices;
1521dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* prevBisector = nullptr;
1522f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
152349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
15246641212397be28b6263e91340b7ed76bd29b3035Stephen White        Line inner(e->fLine);
1525f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        inner.fC -= offset;
15266641212397be28b6263e91340b7ed76bd29b3035Stephen White        Line outer(e->fLine);
1527f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outer.fC += offset;
1528f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint innerPoint, outerPoint;
152949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (prevInner.intersect(inner, &innerPoint) &&
153049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            prevOuter.intersect(outer, &outerPoint)) {
15315cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
15325cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1533dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner            Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc);
153492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c);
153592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c);
153692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            innerVertices.append(innerVertex);
153792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            outerVertices.append(outerVertex);
153892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            prevBisector = bisector;
1539f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1540f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevInner = inner;
1541f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevOuter = outer;
154286cc841e23a81af69269de87224c57be274f40d1Stephen White        prevEdge = e;
1543f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1544f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    innerVertices.close();
1545f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    outerVertices.close();
1546f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1547f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* innerVertex = innerVertices.fHead;
1548f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* outerVertex = outerVertices.fHead;
1549f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!innerVertex || !outerVertex) {
1550f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return;
1551f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1552dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c,
1553dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner                              alloc);
155492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c);
155592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c);
1556f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    do {
155748ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connect vertices into a quad mesh. Outer edges get default (1) winding.
155848ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Inner edges get -2 winding. This ensures that the interior is always filled
155948ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // (-1 winding number for normal cases, 3 for thin features where the interior inverts).
156048ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connector edges get zero winding, since they're only structural (i.e., to ensure
156148ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number.
1562bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc);
156348ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2);
156448ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0);
1565f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* innerNext = innerVertex->fNext;
1566f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* outerNext = outerVertex->fNext;
1567f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(innerVertex);
1568f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(outerVertex);
1569f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        innerVertex = innerNext;
1570f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outerVertex = outerNext;
1571f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1572f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15745cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
1575497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    bool down = apply_fill_type(fillType, e->fWinding);
1576f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    while (e) {
1577f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e->fWinding = down ? 1 : -1;
1578f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Edge* next;
1579f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        boundary->append(e);
1580f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (down) {
1581f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in clockwise order.
1582f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fNextEdgeAbove)) {
1583f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1584f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fBottom->fLastEdgeBelow)) {
1585f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1586f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fPrevEdgeAbove)) {
1587f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1588f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1589f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in counter-clockwise order.
1591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fPrevEdgeBelow)) {
1592f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1593f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fTop->fFirstEdgeAbove)) {
1594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1595f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fNextEdgeBelow)) {
1596f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1597f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1598f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1599e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        disconnect(e);
1600f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e = next;
1601f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1602f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1603f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16045ad721e94682b51b9f773d95704f11990468fef6Stephen White// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
1605f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16065ad721e94682b51b9f773d95704f11990468fef6Stephen Whitevoid extract_boundaries(const VertexList& inMesh, VertexList* outMesh, SkPath::FillType fillType,
16075ad721e94682b51b9f773d95704f11990468fef6Stephen White                        Comparator& c, SkArenaAlloc& alloc) {
16085ad721e94682b51b9f773d95704f11990468fef6Stephen White    remove_non_boundary_edges(inMesh, fillType, alloc);
16095ad721e94682b51b9f773d95704f11990468fef6Stephen White    for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
1610f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        while (v->fFirstEdgeBelow) {
16115ad721e94682b51b9f773d95704f11990468fef6Stephen White            EdgeList boundary;
16125ad721e94682b51b9f773d95704f11990468fef6Stephen White            extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
16135ad721e94682b51b9f773d95704f11990468fef6Stephen White            simplify_boundary(&boundary, c, alloc);
16145ad721e94682b51b9f773d95704f11990468fef6Stephen White            boundary_to_aa_mesh(&boundary, outMesh, c, alloc);
1615f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1616f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1617f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1618f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1619f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// This is a driver function which calls stages 2-5 in turn.
1620f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1621bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
16225cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1623e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1624e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1625e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v = contours[i];
1626e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(v);
1627e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1628e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (v = v->fNext; v != contours[i]; v = v->fNext) {
1629e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1630e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1631e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1632e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1633f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    sanitize_contours(contours, contourCnt, antialias);
1634bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    build_edges(contours, contourCnt, mesh, c, alloc);
1635f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1636f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16375cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
1638bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    if (!vertices || !vertices->fHead) {
16392f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
1640e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1641e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1642e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // Sort vertices in Y (secondarily in X).
164316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (c.fDirection == Comparator::Direction::kHorizontal) {
164416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_horiz>(vertices);
164516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } else {
164616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_vert>(vertices);
164716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
1648f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_coincident_vertices(vertices, c, alloc);
1649e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
16502e2cb9bc768bd13e1beeafc9acf8b8f19bea3215Stephen White    for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
1651e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static float gID = 0.0f;
1652e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID = gID++;
1653e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1654e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1655f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    simplify(*vertices, c, alloc);
16562f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White}
16572f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White
1658f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1659f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                        const SkRect& pathBounds, bool antialias,
16605cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                        SkArenaAlloc& alloc) {
166116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
166216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White                                                          : Comparator::Direction::kVertical);
1663bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    VertexList mesh;
1664bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
1665497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    sort_and_simplify(&mesh, c, alloc);
1666f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (antialias) {
1667f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        VertexList aaMesh;
16685ad721e94682b51b9f773d95704f11990468fef6Stephen White        extract_boundaries(mesh, &aaMesh, fillType, c, alloc);
1669bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        sort_and_simplify(&aaMesh, c, alloc);
1670bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        return tessellate(aaMesh, alloc);
1671497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    } else {
1672497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        return tessellate(mesh, alloc);
1673f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1674f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1675f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1676f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1677f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1678f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                         void* data) {
1679f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Poly* poly = polys; poly; poly = poly->fNext) {
1680f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1681f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = poly->emit(aaParams, data);
1682f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1683f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1684f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return data;
1685e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1686e9709e831954c3427d5cb839e84221a177bfedebethannicholas
16879d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
16885cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) {
1689e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1690e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (SkPath::IsInverseFillType(fillType)) {
1691e9709e831954c3427d5cb839e84221a177bfedebethannicholas        contourCnt++;
1692e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
16937ecc59610de72043e9b7ebaf1ef45c43425e54fcBen Wagner    std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]);
1694e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1695e9709e831954c3427d5cb839e84221a177bfedebethannicholas    path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
1696f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1697f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                             antialias, alloc);
1698e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1699e9709e831954c3427d5cb839e84221a177bfedebethannicholas
170011f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen Whiteint get_contour_count(const SkPath& path, SkScalar tolerance) {
170111f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt;
170211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
1703e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts <= 0) {
170411f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1705e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1706e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts > ((int)SK_MaxU16 + 1)) {
1707e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
170811f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1709e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
171011f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    return contourCnt;
1711e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1712e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1713e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) {
1714e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = 0;
1715e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1716f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
1717e9709e831954c3427d5cb839e84221a177bfedebethannicholas            count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1718e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1719e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1720e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return count;
1721e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1722e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1723e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1724e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1725e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator {
1726e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1727e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1728e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17299d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1730f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1731f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    bool canTweakAlphaForCoverage, bool* isLinear) {
173211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1733e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1734e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *isLinear = true;
1735e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1736e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
173711f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1738f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1739f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                isLinear);
17407ab96e92196dd74d5b95d33c8477b256813f3046senorblanco    SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
1741e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1742e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1743e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1744e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1745e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1746f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* verts = vertexAllocator->lock(count);
17476599efffeef3168dfc68dca99c30454c5c23b859senorblanco    if (!verts) {
1748e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Could not allocate vertices\n");
1749e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1750e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1751f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1752f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    LOG("emitting %d verts\n", count);
1753f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    AAParams aaParams;
1754f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1755f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fColor = color;
1756f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1757f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1758f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1759f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                       / vertexAllocator->stride());
1760e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
17616599efffeef3168dfc68dca99c30454c5c23b859senorblanco    vertexAllocator->unlock(actualCount);
1762e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1763e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1764e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17659d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1766e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   GrTessellator::WindingVertex** verts) {
176711f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1768e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1769e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1770e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
177111f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1772e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLinear;
1773f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
1774e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1775e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1776e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1777e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *verts = nullptr;
1778e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1779e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1780e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1781e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *verts = new GrTessellator::WindingVertex[count];
1782e9709e831954c3427d5cb839e84221a177bfedebethannicholas    GrTessellator::WindingVertex* vertsEnd = *verts;
1783e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* points = new SkPoint[count];
1784e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* pointsEnd = points;
1785e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1786f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1787e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkPoint* start = pointsEnd;
1788f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
1789e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (start != pointsEnd) {
1790e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fPos = *start;
1791e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fWinding = poly->fWinding;
1792e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++start;
1793e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++vertsEnd;
1794e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1795e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1796e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1797e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int actualCount = static_cast<int>(vertsEnd - *verts);
1798e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
1799e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(pointsEnd - points == actualCount);
1800e9709e831954c3427d5cb839e84221a177bfedebethannicholas    delete[] points;
1801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1802e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1803e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1804e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1805