GrTessellator.cpp revision 90732fd36e22cbf0699ab1d0f08eb7e247dda30b
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);
290b56dedf70bdbf1a5e1e04dfbc83a374bffe6b00fStephen White        round(point);
29149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return true;
29249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
29349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double fA, fB, fC;
29449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco};
29549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
296e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
297e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
298e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
299e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
300e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g.,
301e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
302e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division
303e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but
304e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case.
305e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
306e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic
307e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
308e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being
309e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
310e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
311e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file).
312e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
313e9709e831954c3427d5cb839e84221a177bfedebethannicholas
314e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge {
3152f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    enum class Type { kInner, kOuter, kConnector };
3162f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Edge(Vertex* top, Vertex* bottom, int winding, Type type)
317e9709e831954c3427d5cb839e84221a177bfedebethannicholas        : fWinding(winding)
318e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTop(top)
319e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fBottom(bottom)
3202f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        , fType(type)
321e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeft(nullptr)
322e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fRight(nullptr)
323e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeAbove(nullptr)
324e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeAbove(nullptr)
325e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeBelow(nullptr)
326e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeBelow(nullptr)
327e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeftPoly(nullptr)
328531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPoly(nullptr)
329531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyPrev(nullptr)
330531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyNext(nullptr)
331531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPolyPrev(nullptr)
33270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fRightPolyNext(nullptr)
33370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInLeftPoly(false)
33449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fUsedInRightPoly(false)
33549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fLine(top, bottom) {
336e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
337e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int      fWinding;          // 1 == edge goes downward; -1 = edge goes upward.
338e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fTop;              // The top vertex in vertex-sort-order (sweep_lt).
339e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fBottom;           // The bottom vertex in vertex-sort-order.
3402f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Type     fType;
341e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fLeft;             // The linked list of edges in the active edge list.
342e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fRight;            // "
343e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeAbove;    // The linked list of edges in the bottom Vertex's "edges above".
344e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeAbove;    // "
345e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeBelow;    // The linked list of edges in the top Vertex's "edges below".
346e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeBelow;    // "
347e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fLeftPoly;         // The Poly to the left of this edge, if any.
348e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fRightPoly;        // The Poly to the right of this edge, if any.
349531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyPrev;
350531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyNext;
351531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyPrev;
352531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyNext;
35370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInLeftPoly;
35470f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInRightPoly;
35549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line     fLine;
356e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double dist(const SkPoint& p) const {
35749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(p);
358e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
359e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isRightOf(Vertex* v) const {
36049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) < 0.0;
361e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
362e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLeftOf(Vertex* v) const {
36349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) > 0.0;
364e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
365e9709e831954c3427d5cb839e84221a177bfedebethannicholas    void recompute() {
36649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        fLine = Line(fTop, fBottom);
367e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
368dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) {
369e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("intersecting %g -> %g with %g -> %g\n",
370e9709e831954c3427d5cb839e84221a177bfedebethannicholas               fTop->fID, fBottom->fID,
371e9709e831954c3427d5cb839e84221a177bfedebethannicholas               other.fTop->fID, other.fBottom->fID);
372e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fTop == other.fTop || fBottom == other.fBottom) {
373e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
374e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
37549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
376e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom == 0.0) {
377e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
378e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
3798a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
3808a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
3818a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
3828a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double tNumer = dy * fLine.fB + dx * fLine.fA;
383e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
384e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // This saves us doing the divide below unless absolutely necessary.
385e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
386e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
387e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
388e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
389e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double s = sNumer / denom;
390e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(s >= 0.0 && s <= 1.0);
39149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
39249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
39356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        if (alpha) {
39492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            if (fType == Type::kConnector) {
39592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
39692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            } else if (other.fType == Type::kConnector) {
39792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                double t = tNumer / denom;
39892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
39956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
40056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White                *alpha = 0;
40156158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else {
40292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = 255;
40356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            }
40456158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        }
405e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return true;
406e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
407f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco};
408f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
409f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct EdgeList {
4105ad721e94682b51b9f773d95704f11990468fef6Stephen White    EdgeList() : fHead(nullptr), fTail(nullptr) {}
411f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fHead;
412f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fTail;
413f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void insert(Edge* edge, Edge* prev, Edge* next) {
414f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
415f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
416f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void append(Edge* e) {
417f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert(e, fTail, nullptr);
418f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
419f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void remove(Edge* edge) {
420f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
421f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
422f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
423f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
424f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fRight = fHead;
425f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fLeft = fTail;
426f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
427f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
428f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool contains(Edge* edge) const {
429f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return edge->fLeft || edge->fRight || fHead == edge;
430e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
431e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
432e9709e831954c3427d5cb839e84221a177bfedebethannicholas
433e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
434e9709e831954c3427d5cb839e84221a177bfedebethannicholas
435e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly {
436531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly(Vertex* v, int winding)
437531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        : fFirstVertex(v)
438531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fWinding(winding)
439e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fHead(nullptr)
440e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTail(nullptr)
441e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNext(nullptr)
442e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPartner(nullptr)
443e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fCount(0)
444e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {
445e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
446e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static int gID = 0;
447e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fID = gID++;
448e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("*** created Poly %d\n", fID);
449e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
450e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
451531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    typedef enum { kLeft_Side, kRight_Side } Side;
452e9709e831954c3427d5cb839e84221a177bfedebethannicholas    struct MonotonePoly {
453531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        MonotonePoly(Edge* edge, Side side)
454531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            : fSide(side)
455531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fFirstEdge(nullptr)
456531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fLastEdge(nullptr)
457e9709e831954c3427d5cb839e84221a177bfedebethannicholas            , fPrev(nullptr)
458531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fNext(nullptr) {
459531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            this->addEdge(edge);
460531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        }
461e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Side          fSide;
462531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fFirstEdge;
463531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fLastEdge;
464e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fPrev;
465e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fNext;
466531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        void addEdge(Edge* edge) {
467e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            if (fSide == kRight_Side) {
468212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInRightPoly);
469531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
470531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
47170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInRightPoly = true;
472e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
473212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInLeftPoly);
474531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
475531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
47670f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInLeftPoly = true;
477e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
478e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
479e9709e831954c3427d5cb839e84221a177bfedebethannicholas
480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        void* emit(const AAParams* aaParams, void* data) {
481531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Edge* e = fFirstEdge;
482531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            VertexList vertices;
483531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            vertices.append(e->fTop);
484531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (e != nullptr) {
485531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (kRight_Side == fSide) {
486531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.append(e->fBottom);
487531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fRightPolyNext;
488531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                } else {
489531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.prepend(e->fBottom);
490531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fLeftPolyNext;
491531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                }
492531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            }
493531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Vertex* first = vertices.fHead;
494e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* v = first->fNext;
495531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (v != vertices.fTail) {
496e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(v && v->fPrev && v->fNext);
497e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* prev = v->fPrev;
498e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* curr = v;
499e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* next = v->fNext;
500e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
501e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
502e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
503e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
504e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (ax * by - ay * bx >= 0.0) {
505f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    data = emit_triangle(prev, curr, next, aaParams, data);
506e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fPrev->fNext = v->fNext;
507e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fNext->fPrev = v->fPrev;
508e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (v->fPrev == first) {
509e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fNext;
510e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    } else {
511e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fPrev;
512e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
513e9709e831954c3427d5cb839e84221a177bfedebethannicholas                } else {
514e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v = v->fNext;
515e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
516e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
517e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
518e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
519e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
5205cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
52170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        LOG("addEdge (%g -> %g) to poly %d, %s side\n",
52270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco               e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
523e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* partner = fPartner;
524e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* poly = this;
525212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        if (side == kRight_Side) {
526212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInRightPoly) {
527212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
528212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
529212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        } else {
530212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInLeftPoly) {
531212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
532212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
533212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        }
534e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (partner) {
535e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fPartner = partner->fPartner = nullptr;
536e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
537531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        if (!fTail) {
5385cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            fHead = fTail = alloc.make<MonotonePoly>(e, side);
539531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount += 2;
54093e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco        } else if (e->fBottom == fTail->fLastEdge->fBottom) {
54193e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco            return poly;
542531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else if (side == fTail->fSide) {
543531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
544531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
545531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else {
5465cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
547531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
548531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
549e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (partner) {
550531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                partner->addEdge(e, side, alloc);
551e9709e831954c3427d5cb839e84221a177bfedebethannicholas                poly = partner;
552e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
5535cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
554531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                m->fPrev = fTail;
555531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail->fNext = m;
556531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail = m;
557e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
558e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
559e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return poly;
560e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
561f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* emit(const AAParams* aaParams, void *data) {
562e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fCount < 3) {
563e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
564e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
565e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("emit() %d, size %d\n", fID, fCount);
566e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
567f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = m->emit(aaParams, data);
568e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
569e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return data;
570e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
571531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
572531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* fFirstVertex;
573e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fWinding;
574e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fHead;
575e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fTail;
576e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fNext;
577e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fPartner;
578e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fCount;
579e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
580e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fID;
581e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
582e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
583e9709e831954c3427d5cb839e84221a177bfedebethannicholas
584e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
585e9709e831954c3427d5cb839e84221a177bfedebethannicholas
586e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) {
587e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a == b;
588e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
589e9709e831954c3427d5cb839e84221a177bfedebethannicholas
5905cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
5915cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* poly = alloc.make<Poly>(v, winding);
592e9709e831954c3427d5cb839e84221a177bfedebethannicholas    poly->fNext = *head;
593e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = poly;
594e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return poly;
595e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
596e9709e831954c3427d5cb839e84221a177bfedebethannicholas
597e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
5985cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                SkArenaAlloc& alloc) {
5995cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Vertex* v = alloc.make<Vertex>(p, 255);
600e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
601e9709e831954c3427d5cb839e84221a177bfedebethannicholas    static float gID = 0.0f;
602e9709e831954c3427d5cb839e84221a177bfedebethannicholas    v->fID = gID++;
603e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
604e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
605e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = v;
606e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fPrev = prev;
607e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
608e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = v;
609e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
610e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return v;
611e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
612e9709e831954c3427d5cb839e84221a177bfedebethannicholas
613e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0,
614e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p1,
615e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p2,
616e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkScalar tolSqd,
617e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex* prev,
618e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex** head,
619e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  int pointsLeft,
6205cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                  SkArenaAlloc& alloc) {
621e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
622e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
623e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p2, prev, head, alloc);
624e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
625e9709e831954c3427d5cb839e84221a177bfedebethannicholas
626e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
627e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
628e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
629e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
630e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
631e9709e831954c3427d5cb839e84221a177bfedebethannicholas
632e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
633e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
634e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
635e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
636e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
637e9709e831954c3427d5cb839e84221a177bfedebethannicholas
638e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0,
639e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p1,
640e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p2,
641e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p3,
642e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkScalar tolSqd,
643e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex* prev,
644e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex** head,
645e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              int pointsLeft,
6465cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                              SkArenaAlloc& alloc) {
647e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
648e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
649e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
650e9709e831954c3427d5cb839e84221a177bfedebethannicholas        !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
651e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p3, prev, head, alloc);
652e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
653e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
654e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
655e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
656e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
657e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
658e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r[] = {
659e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
660e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
661e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
662e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
663e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
664e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
665e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
666e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
667e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
668e9709e831954c3427d5cb839e84221a177bfedebethannicholas
669e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
670e9709e831954c3427d5cb839e84221a177bfedebethannicholas
671e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
6725cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) {
673e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar toleranceSqd = tolerance * tolerance;
674e9709e831954c3427d5cb839e84221a177bfedebethannicholas
675e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint pts[4];
676e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool done = false;
677e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *isLinear = true;
678e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::Iter iter(path, false);
679e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
680e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* head = nullptr;
681e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (path.isInverseFillType()) {
682e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPoint quad[4];
683e9709e831954c3427d5cb839e84221a177bfedebethannicholas        clipBounds.toQuad(quad);
6847ab96e92196dd74d5b95d33c8477b256813f3046senorblanco        for (int i = 3; i >= 0; i--) {
685e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = append_point_to_contour(quad[i], prev, &head, alloc);
686e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
687e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head->fPrev = prev;
688e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = head;
689e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contours++ = head;
690e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head = prev = nullptr;
691e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
692e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoConicToQuads converter;
693e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (!done) {
694e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPath::Verb verb = iter.next(pts);
695e9709e831954c3427d5cb839e84221a177bfedebethannicholas        switch (verb) {
696e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kConic_Verb: {
697e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkScalar weight = iter.conicWeight();
698e9709e831954c3427d5cb839e84221a177bfedebethannicholas                const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
699e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (int i = 0; i < converter.countQuads(); ++i) {
700e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
701e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
702e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                     toleranceSqd, prev, &head, pointsLeft, alloc);
703e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    quadPts += 2;
704e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
705e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
706e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
707e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
708e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kMove_Verb:
709e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
710e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
711e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
712e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
713e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
714e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
715e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[0], prev, &head, alloc);
716e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
717e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kLine_Verb: {
718e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[1], prev, &head, alloc);
719e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
720e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
721e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kQuad_Verb: {
722e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
723e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
724e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                 &head, pointsLeft, alloc);
725e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
726e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
727e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
728e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kCubic_Verb: {
729e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
730e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
731e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                toleranceSqd, prev, &head, pointsLeft, alloc);
732e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
733e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
734e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
735e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kClose_Verb:
736e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
737e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
738e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
739e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
740e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
741e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
742e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
743e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kDone_Verb:
744e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
745e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
746e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
747e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
748e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
749e9709e831954c3427d5cb839e84221a177bfedebethannicholas                done = true;
750e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
751e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
752e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
753e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
754e9709e831954c3427d5cb839e84221a177bfedebethannicholas
755497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, int winding) {
756e9709e831954c3427d5cb839e84221a177bfedebethannicholas    switch (fillType) {
757e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kWinding_FillType:
758e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return winding != 0;
759e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kEvenOdd_FillType:
760e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) != 0;
761e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseWinding_FillType:
7627ab96e92196dd74d5b95d33c8477b256813f3046senorblanco            return winding == 1;
763e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseEvenOdd_FillType:
764e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) == 1;
765e9709e831954c3427d5cb839e84221a177bfedebethannicholas        default:
766e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkASSERT(false);
767e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
768e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
769e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
770e9709e831954c3427d5cb839e84221a177bfedebethannicholas
771497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
772497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    return poly && apply_fill_type(fillType, poly->fWinding);
773497890630b80a381a2ac4cbb9114b0320560bf8cStephen White}
774497890630b80a381a2ac4cbb9114b0320560bf8cStephen White
7755cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
7762f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
777e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = winding < 0 ? next : prev;
778e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = winding < 0 ? prev : next;
7795cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    return alloc.make<Edge>(top, bottom, winding, type);
780e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
781e9709e831954c3427d5cb839e84221a177bfedebethannicholas
782e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) {
783e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
784f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(edges->contains(edge));
785f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->remove(edge);
786e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
787e9709e831954c3427d5cb839e84221a177bfedebethannicholas
788e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
789e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
790f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(!edges->contains(edge));
791e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = prev ? prev->fRight : edges->fHead;
792f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->insert(edge, prev, next);
793e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
794e9709e831954c3427d5cb839e84221a177bfedebethannicholas
795e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
79690732fd36e22cbf0699ab1d0f08eb7e247dda30bStephen White    if (v->fFirstEdgeAbove && v->fLastEdgeAbove) {
797e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *left = v->fFirstEdgeAbove->fLeft;
798e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *right = v->fLastEdgeAbove->fRight;
799e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
800e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = nullptr;
802e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev;
803e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
804e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (prev->isLeftOf(v)) {
805e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
806e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
807e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next = prev;
808e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
809e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
810e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
811e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
812e9709e831954c3427d5cb839e84221a177bfedebethannicholas
813e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
814e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
815e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
816e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = edges->fHead; next != nullptr; next = next->fRight) {
817e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if ((c.sweep_lt(next->fTop->fPoint, edge->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
818e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White            (c.sweep_lt(edge->fTop->fPoint, next->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
819e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
820e9709e831954c3427d5cb839e84221a177bfedebethannicholas             next->isRightOf(edge->fBottom)) ||
821e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
822e9709e831954c3427d5cb839e84221a177bfedebethannicholas             edge->isLeftOf(next->fBottom))) {
823e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
824e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
825e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
826e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
827e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
828e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
829e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
830e9709e831954c3427d5cb839e84221a177bfedebethannicholas
831e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
8322f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (!activeEdges) {
8332f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
8342f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
8352f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (activeEdges->contains(edge)) {
836e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
837e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(edge, activeEdges);
838e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
839e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
840e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* left;
841e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* right;
842e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(edge, activeEdges, c, &left, &right);
843e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge(edge, left, activeEdges);
844e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
845e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
846e9709e831954c3427d5cb839e84221a177bfedebethannicholas
847e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
848e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
849e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
850e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
851e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
852e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
853e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
854e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
855e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
856e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fTop)) {
857e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
858e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
859e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
860e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
861e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
862e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
863e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
864e9709e831954c3427d5cb839e84221a177bfedebethannicholas
865e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
866e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
867e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        c.sweep_lt(edge->fBottom->fPoint, edge->fTop->fPoint)) {
868e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
869e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
870e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
871e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
872e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
873e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
874e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fBottom)) {
875e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
876e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
877e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
878e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
879e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
880e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
881e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
882e9709e831954c3427d5cb839e84221a177bfedebethannicholas
883e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) {
884e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
885e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fBottom->fID);
886e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
887e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
888e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
889e9709e831954c3427d5cb839e84221a177bfedebethannicholas
890e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) {
891e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
892e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID);
893e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
894e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
895e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
896e9709e831954c3427d5cb839e84221a177bfedebethannicholas
897e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid disconnect(Edge* edge)
898e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White{
899e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
900e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
901e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White}
902e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White
903e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid erase_edge(Edge* edge, EdgeList* edges) {
904e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
905e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    disconnect(edge);
906f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (edges && edges->contains(edge)) {
907e9709e831954c3427d5cb839e84221a177bfedebethannicholas        remove_edge(edge, edges);
908e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
909e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
910e9709e831954c3427d5cb839e84221a177bfedebethannicholas
911e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
912e9709e831954c3427d5cb839e84221a177bfedebethannicholas
913e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
914e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
915e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fTop = v;
916e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
917e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_below(edge, v, c);
918e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
919e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
920e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
921e9709e831954c3427d5cb839e84221a177bfedebethannicholas
922e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
923e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
924e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fBottom = v;
925e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
926e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_above(edge, v, c);
927e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
928e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
929e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
930e9709e831954c3427d5cb839e84221a177bfedebethannicholas
931e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
932e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
933e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
934e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
935e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
936e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
937e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
938e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
939e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
940e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, other->fTop, activeEdges, c);
941e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
942e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
943e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(other, edge->fTop, activeEdges, c);
944e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
945e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
946e9709e831954c3427d5cb839e84221a177bfedebethannicholas
947e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
948e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
949e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
950e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
951e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
952e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
953e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
954e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
955e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
956e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(other, edge->fBottom, activeEdges, c);
957e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
958e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
959e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, other->fBottom, activeEdges, c);
960e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
961e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
962e9709e831954c3427d5cb839e84221a177bfedebethannicholas
963e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
964e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
965e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
966e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
967e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
968e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
969e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
970e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
971e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
972e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
973e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
974e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
975e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
976e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
977e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
978e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
979e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9805cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc);
981e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9825cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
983e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = edge->fTop;
984e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = edge->fBottom;
985e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fLeft) {
986e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftTop = edge->fLeft->fTop;
987e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftBottom = edge->fLeft->fBottom;
988e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(top)) {
989e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
990e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(leftTop)) {
991e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftTop, activeEdges, c, alloc);
992e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
993e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fLeft->isLeftOf(bottom)) {
994e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
995e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
996e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftBottom, activeEdges, c, alloc);
997e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
998e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
999e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fRight) {
1000e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightTop = edge->fRight->fTop;
1001e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightBottom = edge->fRight->fBottom;
1002e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(top)) {
1003e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, top, activeEdges, c, alloc);
1004e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(rightTop)) {
1005e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightTop, activeEdges, c, alloc);
1006e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1007e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fRight->isRightOf(bottom)) {
1008e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, bottom, activeEdges, c, alloc);
1009e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1010e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->isLeftOf(rightBottom)) {
1011e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightBottom, activeEdges, c, alloc);
1012e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1013e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1014e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1015e9709e831954c3427d5cb839e84221a177bfedebethannicholas
10165cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
1017e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1018e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID, edge->fBottom->fID,
1019e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID, v->fPoint.fX, v->fPoint.fY);
1020e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
1021e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, v, activeEdges, c);
1022e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White    } else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) {
1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1024e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
10255cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby        Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType);
1026e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_below(newEdge, v, c);
1027e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_above(newEdge, edge->fBottom, c);
1028e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1029e9709e831954c3427d5cb839e84221a177bfedebethannicholas        cleanup_active_edges(edge, activeEdges, c, alloc);
1030e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fix_active_state(newEdge, activeEdges, c);
1031e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_collinear_edges(newEdge, activeEdges, c);
1032e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1033e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1034e9709e831954c3427d5cb839e84221a177bfedebethannicholas
10355cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
103648ded38da99c1171ba1bb469f6500f8214e9105cStephen White              int winding_scale = 1) {
1037bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    Edge* edge = new_edge(prev, next, type, c, alloc);
10388a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_below(edge, edge->fTop, c);
10398a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_above(edge, edge->fBottom, c);
104048ded38da99c1171ba1bb469f6500f8214e9105cStephen White    edge->fWinding *= winding_scale;
1041f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_collinear_edges(edge, nullptr, c);
1042f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return edge;
1043f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1044f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1045bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
10465cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    SkArenaAlloc& alloc) {
1047e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1048e9709e831954c3427d5cb839e84221a177bfedebethannicholas        src->fID, dst->fID);
1049f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
1050e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1051e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeAbove;
1052e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, dst, nullptr, c);
1053e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1054e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1055e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1056e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeBelow;
1057e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, dst, nullptr, c);
1058e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1059e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1060bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->remove(src);
1061e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1062e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1063f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancouint8_t max_edge_alpha(Edge* a, Edge* b) {
106456158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) {
10652f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 255;
10662f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
10672f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 0;
10682f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else {
10692f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
10702f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White                      SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
10712f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
1072f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1073f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1074e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
10755cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1076e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!edge || !other) {
1077e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1078e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
107956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    SkPoint p;
108056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    uint8_t alpha;
108156158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (edge->intersect(*other, &p, &alpha)) {
1082e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v;
1083e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1084e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1085e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fTop, activeEdges, c, alloc);
1086e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fTop;
1087e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (p == edge->fBottom->fPoint || c.sweep_lt(edge->fBottom->fPoint, p)) {
1088e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fBottom, activeEdges, c, alloc);
1089e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fBottom;
1090e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1091e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fTop, activeEdges, c, alloc);
1092e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fTop;
1093e30cf80c8893db8ec61d29322f1992591dad3b6bStephen White        } else if (p == other->fBottom->fPoint || c.sweep_lt(other->fBottom->fPoint, p)) {
1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fBottom, activeEdges, c, alloc);
1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fBottom;
1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1097e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* nextV = edge->fTop;
1098e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(p, nextV->fPoint)) {
1099e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fPrev;
1100e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(nextV->fPoint, p)) {
1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fNext;
1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* prevV = nextV->fPrev;
1105e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(prevV->fPoint, p)) {
1106e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = prevV;
1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else if (coincident(nextV->fPoint, p)) {
1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = nextV;
1109e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
11105cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                v = alloc.make<Vertex>(p, alpha);
1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1115e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fID = (nextV->fID + prevV->fID) * 0.5f;
1116e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prevV;
1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext = nextV;
1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prevV->fNext = v;
1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV->fPrev = v;
1121e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1122e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, v, activeEdges, c, alloc);
1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, v, activeEdges, c, alloc);
1124e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
112592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        v->fAlpha = SkTMax(v->fAlpha, alpha);
1126e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return v;
1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1128e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return nullptr;
1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1130e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1131f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
1132e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1133e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(contours[i]);
11345926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        if (approximate) {
11355926f2da752d1bff0051fda4137bb967f9e91d5fStephen White            round(&contours[i]->fPrev->fPoint);
11365926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        }
1137e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i];;) {
1138f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (approximate) {
1139f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                round(&v->fPoint);
1140f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(v->fPrev->fPoint, v->fPoint)) {
1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1143e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v->fPrev == v) {
1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = nullptr;
1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    break;
1146e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1147e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev->fNext = v->fNext;
1148e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext->fPrev = v->fPrev;
1149e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (contours[i] == v) {
11505926f2da752d1bff0051fda4137bb967f9e91d5fStephen White                    contours[i] = v->fPrev;
1151e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1152e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fPrev;
1153e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1154e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fNext;
1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v == contours[i]) break;
1156e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1157e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1158e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1160e9709e831954c3427d5cb839e84221a177bfedebethannicholas
11615cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1162bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) {
1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fPoint = v->fPrev->fPoint;
1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1166e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (coincident(v->fPrev->fPoint, v->fPoint)) {
1167bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            merge_vertices(v->fPrev, v, mesh, c, alloc);
1168e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1172e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1173e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1174bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c,
11755cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                 SkArenaAlloc& alloc) {
1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i]; v != nullptr;) {
1179e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* vNext = v->fNext;
1180bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            connect(v->fPrev, v, Edge::Type::kInner, c, alloc);
1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (prev) {
1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev->fNext = v;
1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prev;
1184e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1185bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                mesh->fHead = v;
1186e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1187e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = v;
1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = vNext;
1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v == contours[i]) break;
1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1192e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
1193bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        prev->fNext = mesh->fHead->fPrev = nullptr;
1194e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1195bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->fTail = prev;
1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1197e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1198e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction.
1199e9709e831954c3427d5cb839e84221a177bfedebethannicholas
120016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitetemplate <CompareFunc sweep_lt>
120116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitevoid merge_sort(VertexList* vertices) {
120216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* slow = vertices->fHead;
120316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!slow) {
1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1205e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
120616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* fast = slow->fNext;
120716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!fast) {
120816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return;
120916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
121016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    do {
121116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        fast = fast->fNext;
121216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (fast) {
121316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            fast = fast->fNext;
121416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            slow = slow->fNext;
121516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        }
121616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } while (fast);
121716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList front(vertices->fHead, slow);
121816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList back(slow->fNext, vertices->fTail);
121916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    front.fTail->fNext = back.fHead->fPrev = nullptr;
1220e9709e831954c3427d5cb839e84221a177bfedebethannicholas
122116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&front);
122216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&back);
1223e9709e831954c3427d5cb839e84221a177bfedebethannicholas
122416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    vertices->fHead = vertices->fTail = nullptr;
122516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* a = front.fHead;
122616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* b = back.fHead;
1227e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (a && b) {
122816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (sweep_lt(a->fPoint, b->fPoint)) {
1229e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = a->fNext;
123016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(a);
1231e9709e831954c3427d5cb839e84221a177bfedebethannicholas            a = next;
1232e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1233e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = b->fNext;
123416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(b);
1235e9709e831954c3427d5cb839e84221a177bfedebethannicholas            b = next;
1236e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1237e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1238e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (a) {
123916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(a, vertices->fTail, a->fNext);
1240e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1241e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (b) {
124216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(b, vertices->fTail, b->fNext);
1243e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1244e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1246e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1247e9709e831954c3427d5cb839e84221a177bfedebethannicholas
12485cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) {
1249e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("simplifying complex polygons\n");
1250e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1251bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1252e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1253e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1254e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1255e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1256f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1257e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
12588a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
12598a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1260e9709e831954c3427d5cb839e84221a177bfedebethannicholas        bool restartChecks;
1261e9709e831954c3427d5cb839e84221a177bfedebethannicholas        do {
1262e9709e831954c3427d5cb839e84221a177bfedebethannicholas            restartChecks = false;
1263e9709e831954c3427d5cb839e84221a177bfedebethannicholas            find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1264e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v->fFirstEdgeBelow) {
1265bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
1266e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1267e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1268e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1269e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1270e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1271e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1275e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1277e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                        &activeEdges, c, alloc)) {
1278e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = pv;
1280e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    restartChecks = true;
1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1285e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } while (restartChecks);
1286f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (v->fAlpha == 0) {
128748ded38da99c1171ba1bb469f6500f8214e9105cStephen White            if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
128848ded38da99c1171ba1bb469f6500f8214e9105cStephen White                (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
1289f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1290f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1291f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1293e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(e, &activeEdges);
1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEdge = leftEnclosingEdge;
1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1297e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(e, leftEdge, &activeEdges);
1298e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge = e;
1299e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1300e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fProcessed = true;
1301e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1302e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1303e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1304e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons.
1305e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13065cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
1307e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("tessellating simple polygons\n");
1308e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1309e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* polys = nullptr;
1310bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1311e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1312e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1314e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1315f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1316e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
13178a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
13188a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1319e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
13208a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* leftPoly;
13218a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* rightPoly;
1322e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1323e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1324e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = v->fLastEdgeAbove->fRightPoly;
1325e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1326e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1327e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1328e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1329e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1330e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges above:\n");
1331e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1332e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1333e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges below:\n");
1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1338e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1339e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1342e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (leftPoly) {
1343531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1345e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (rightPoly) {
1346531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
1347e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1349e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* rightEdge = e->fNextEdgeAbove;
13508a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                SkASSERT(rightEdge->isRightOf(e->fTop));
13518a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                remove_edge(e, &activeEdges);
13528a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (e->fRightPoly) {
13538a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                    e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
1354e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
13558a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
1356531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
1357e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1358e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(v->fLastEdgeAbove, &activeEdges);
1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeBelow) {
1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftPoly && rightPoly && leftPoly != rightPoly) {
1362e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1363e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    rightPoly->fPartner = leftPoly;
1364e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftPoly->fPartner = rightPoly;
1365e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1367e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1368e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeBelow) {
1369e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeAbove) {
137093e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco                if (leftPoly && rightPoly) {
1371531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    if (leftPoly == rightPoly) {
1372531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1373531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1374531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 leftPoly->fWinding, alloc);
1375531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftEnclosingEdge->fRightPoly = leftPoly;
1376531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        } else {
1377531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1378531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 rightPoly->fWinding, alloc);
1379531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightEnclosingEdge->fLeftPoly = rightPoly;
1380531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        }
1381e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
13825cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
1383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1384531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
1385e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1386e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1387e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Edge* leftEdge = v->fFirstEdgeBelow;
1388e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge->fLeftPoly = leftPoly;
1389e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1390e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1391e9709e831954c3427d5cb839e84221a177bfedebethannicholas                 rightEdge = rightEdge->fNextEdgeBelow) {
1392e9709e831954c3427d5cb839e84221a177bfedebethannicholas                insert_edge(rightEdge, leftEdge, &activeEdges);
1393e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1394e9709e831954c3427d5cb839e84221a177bfedebethannicholas                winding += leftEdge->fWinding;
1395e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (winding != 0) {
1396e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    Poly* poly = new_poly(&polys, v, winding, alloc);
1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1399e9709e831954c3427d5cb839e84221a177bfedebethannicholas                leftEdge = rightEdge;
1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fLastEdgeBelow->fRightPoly = rightPoly;
1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("\nactive edges:\n");
1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1406e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1407e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1408e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1409e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1410e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1411e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return polys;
1412e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1413e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1414bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
14155cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1416497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    LOG("removing non-boundary edges\n");
1417497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    EdgeList activeEdges;
1418bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
1419497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1420497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            continue;
1421497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1422497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* leftEnclosingEdge;
1423497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* rightEnclosingEdge;
1424497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1425497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        bool prevFilled = leftEnclosingEdge &&
1426497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                          apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1427497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeAbove; e;) {
1428497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            Edge* next = e->fNextEdgeAbove;
1429497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            remove_edge(e, &activeEdges);
1430497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            bool filled = apply_fill_type(fillType, e->fWinding);
1431497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (filled == prevFilled) {
1432e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White                disconnect(e);
1433f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1434497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prevFilled = filled;
1435f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = next;
1436f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1437497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* prev = leftEnclosingEdge;
1438497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1439497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (prev) {
1440497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                e->fWinding += prev->fWinding;
1441497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            }
1442497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            insert_edge(e, prev, &activeEdges);
1443497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prev = e;
1444497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1445682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1446f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1447f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
14486641212397be28b6263e91340b7ed76bd29b3035Stephen White// Note: this is the normal to the edge, but not necessarily unit length.
1449f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid get_edge_normal(const Edge* e, SkVector* normal) {
14506641212397be28b6263e91340b7ed76bd29b3035Stephen White    normal->set(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
14516641212397be28b6263e91340b7ed76bd29b3035Stephen White                SkDoubleToScalar(e->fLine.fB) * e->fWinding);
1452f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1453f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1454f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1455f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1456f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// invert on stroking.
1457f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
14585cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
1459f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1460f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkVector prevNormal;
1461f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    get_edge_normal(prevEdge, &prevNormal);
1462f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr;) {
1463f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1464f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1465f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        double dist = e->dist(prev->fPoint);
1466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkVector normal;
1467f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        get_edge_normal(e, &normal);
14686641212397be28b6263e91340b7ed76bd29b3035Stephen White        double denom = 0.0625f * e->fLine.magSq();
1469f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
1470bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
1471f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            insert_edge(join, e, boundary);
1472f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(prevEdge, boundary);
1473f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(e, boundary);
1474f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (join->fLeft && join->fRight) {
1475f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = join->fLeft;
1476f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = join;
1477f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else {
1478f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = boundary->fTail;
1479f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1481f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            get_edge_normal(prevEdge, &prevNormal);
1482f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1483f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevEdge = e;
1484f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevNormal = normal;
1485f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = e->fRight;
1486f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1487f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1488f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1489f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1490dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagnervoid fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector,
149192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                    Edge* prevEdge, Comparator& c) {
149292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    if (!prev || !next) {
149392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        return;
149492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
149592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
149692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    SkPoint p;
149792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    uint8_t alpha;
1498dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) {
149992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fPoint = next->fPoint = p;
150092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fAlpha = next->fAlpha = alpha;
150192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
150292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White}
150392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White
1504f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1505f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1506f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// new antialiased mesh from those vertices.
1507f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15085cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
15098a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    // A boundary with fewer than 3 edges is degenerate.
15108a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
15118a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        return;
15128a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    }
1513f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1514f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    float radius = 0.5f;
151549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
15166641212397be28b6263e91340b7ed76bd29b3035Stephen White    Line prevInner(prevEdge->fLine);
1517f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevInner.fC -= offset;
15186641212397be28b6263e91340b7ed76bd29b3035Stephen White    Line prevOuter(prevEdge->fLine);
1519f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevOuter.fC += offset;
1520f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList innerVertices;
1521f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList outerVertices;
1522dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* prevBisector = nullptr;
1523f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
152449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
15256641212397be28b6263e91340b7ed76bd29b3035Stephen White        Line inner(e->fLine);
1526f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        inner.fC -= offset;
15276641212397be28b6263e91340b7ed76bd29b3035Stephen White        Line outer(e->fLine);
1528f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outer.fC += offset;
1529f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint innerPoint, outerPoint;
153049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (prevInner.intersect(inner, &innerPoint) &&
153149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            prevOuter.intersect(outer, &outerPoint)) {
15325cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
15335cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1534dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner            Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc);
153592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c);
153692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c);
153792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            innerVertices.append(innerVertex);
153892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            outerVertices.append(outerVertex);
153992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            prevBisector = bisector;
1540f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1541f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevInner = inner;
1542f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevOuter = outer;
154386cc841e23a81af69269de87224c57be274f40d1Stephen White        prevEdge = e;
1544f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1545f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    innerVertices.close();
1546f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    outerVertices.close();
1547f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1548f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* innerVertex = innerVertices.fHead;
1549f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* outerVertex = outerVertices.fHead;
1550f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!innerVertex || !outerVertex) {
1551f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return;
1552f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1553dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c,
1554dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner                              alloc);
155592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c);
155692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c);
1557f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    do {
155848ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connect vertices into a quad mesh. Outer edges get default (1) winding.
155948ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Inner edges get -2 winding. This ensures that the interior is always filled
156048ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // (-1 winding number for normal cases, 3 for thin features where the interior inverts).
156148ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connector edges get zero winding, since they're only structural (i.e., to ensure
156248ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number.
1563bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc);
156448ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2);
156548ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0);
1566f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* innerNext = innerVertex->fNext;
1567f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* outerNext = outerVertex->fNext;
1568f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(innerVertex);
1569f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(outerVertex);
1570f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        innerVertex = innerNext;
1571f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outerVertex = outerNext;
1572f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1574f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15755cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
1576497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    bool down = apply_fill_type(fillType, e->fWinding);
1577f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    while (e) {
1578f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e->fWinding = down ? 1 : -1;
1579f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Edge* next;
1580f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        boundary->append(e);
1581f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (down) {
1582f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in clockwise order.
1583f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fNextEdgeAbove)) {
1584f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1585f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fBottom->fLastEdgeBelow)) {
1586f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1587f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fPrevEdgeAbove)) {
1588f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1589f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in counter-clockwise order.
1592f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fPrevEdgeBelow)) {
1593f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fTop->fFirstEdgeAbove)) {
1595f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1596f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fNextEdgeBelow)) {
1597f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1598f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1599f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1600e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        disconnect(e);
1601f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e = next;
1602f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1603f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1604f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16055ad721e94682b51b9f773d95704f11990468fef6Stephen White// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
1606f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16075ad721e94682b51b9f773d95704f11990468fef6Stephen Whitevoid extract_boundaries(const VertexList& inMesh, VertexList* outMesh, SkPath::FillType fillType,
16085ad721e94682b51b9f773d95704f11990468fef6Stephen White                        Comparator& c, SkArenaAlloc& alloc) {
16095ad721e94682b51b9f773d95704f11990468fef6Stephen White    remove_non_boundary_edges(inMesh, fillType, alloc);
16105ad721e94682b51b9f773d95704f11990468fef6Stephen White    for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
1611f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        while (v->fFirstEdgeBelow) {
16125ad721e94682b51b9f773d95704f11990468fef6Stephen White            EdgeList boundary;
16135ad721e94682b51b9f773d95704f11990468fef6Stephen White            extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
16145ad721e94682b51b9f773d95704f11990468fef6Stephen White            simplify_boundary(&boundary, c, alloc);
16155ad721e94682b51b9f773d95704f11990468fef6Stephen White            boundary_to_aa_mesh(&boundary, outMesh, c, alloc);
1616f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1617f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1618f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1619f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1620f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// This is a driver function which calls stages 2-5 in turn.
1621f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1622bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
16235cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1624e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1625e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1626e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v = contours[i];
1627e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(v);
1628e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1629e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (v = v->fNext; v != contours[i]; v = v->fNext) {
1630e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1631e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1632e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1633e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1634f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    sanitize_contours(contours, contourCnt, antialias);
1635bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    build_edges(contours, contourCnt, mesh, c, alloc);
1636f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1637f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16385cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
1639bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    if (!vertices || !vertices->fHead) {
16402f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
1641e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1642e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1643e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // Sort vertices in Y (secondarily in X).
164416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (c.fDirection == Comparator::Direction::kHorizontal) {
164516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_horiz>(vertices);
164616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } else {
164716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_vert>(vertices);
164816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
1649f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_coincident_vertices(vertices, c, alloc);
1650e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
16512e2cb9bc768bd13e1beeafc9acf8b8f19bea3215Stephen White    for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
1652e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static float gID = 0.0f;
1653e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID = gID++;
1654e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1655e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1656f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    simplify(*vertices, c, alloc);
16572f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White}
16582f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White
1659f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1660f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                        const SkRect& pathBounds, bool antialias,
16615cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                        SkArenaAlloc& alloc) {
166216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
166316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White                                                          : Comparator::Direction::kVertical);
1664bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    VertexList mesh;
1665bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
1666497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    sort_and_simplify(&mesh, c, alloc);
1667f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (antialias) {
1668f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        VertexList aaMesh;
16695ad721e94682b51b9f773d95704f11990468fef6Stephen White        extract_boundaries(mesh, &aaMesh, fillType, c, alloc);
1670bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        sort_and_simplify(&aaMesh, c, alloc);
1671bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        return tessellate(aaMesh, alloc);
1672497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    } else {
1673497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        return tessellate(mesh, alloc);
1674f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1675f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1676f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1677f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1678f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1679f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                         void* data) {
1680f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Poly* poly = polys; poly; poly = poly->fNext) {
1681f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1682f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = poly->emit(aaParams, data);
1683f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1684f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1685f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return data;
1686e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1687e9709e831954c3427d5cb839e84221a177bfedebethannicholas
16889d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
16895cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) {
1690e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1691e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (SkPath::IsInverseFillType(fillType)) {
1692e9709e831954c3427d5cb839e84221a177bfedebethannicholas        contourCnt++;
1693e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
16947ecc59610de72043e9b7ebaf1ef45c43425e54fcBen Wagner    std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]);
1695e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1696e9709e831954c3427d5cb839e84221a177bfedebethannicholas    path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
1697f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1698f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                             antialias, alloc);
1699e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1700e9709e831954c3427d5cb839e84221a177bfedebethannicholas
170111f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen Whiteint get_contour_count(const SkPath& path, SkScalar tolerance) {
170211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt;
170311f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
1704e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts <= 0) {
170511f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1706e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1707e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts > ((int)SK_MaxU16 + 1)) {
1708e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
170911f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1710e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
171111f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    return contourCnt;
1712e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1713e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1714e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) {
1715e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = 0;
1716e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1717f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
1718e9709e831954c3427d5cb839e84221a177bfedebethannicholas            count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1719e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1720e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1721e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return count;
1722e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1723e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1724e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1725e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1726e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator {
1727e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1728e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1729e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17309d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1731f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1732f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    bool canTweakAlphaForCoverage, bool* isLinear) {
173311f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1734e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1735e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *isLinear = true;
1736e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1737e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
173811f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1739f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1740f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                isLinear);
17417ab96e92196dd74d5b95d33c8477b256813f3046senorblanco    SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
1742e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1743e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1744e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1745e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1746e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1747f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* verts = vertexAllocator->lock(count);
17486599efffeef3168dfc68dca99c30454c5c23b859senorblanco    if (!verts) {
1749e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Could not allocate vertices\n");
1750e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1751e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1752f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1753f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    LOG("emitting %d verts\n", count);
1754f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    AAParams aaParams;
1755f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1756f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fColor = color;
1757f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1758f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1759f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1760f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                       / vertexAllocator->stride());
1761e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
17626599efffeef3168dfc68dca99c30454c5c23b859senorblanco    vertexAllocator->unlock(actualCount);
1763e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1764e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1765e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17669d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1767e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   GrTessellator::WindingVertex** verts) {
176811f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1769e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1770e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1771e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
177211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1773e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLinear;
1774f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
1775e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1776e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1777e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1778e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *verts = nullptr;
1779e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1780e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1781e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1782e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *verts = new GrTessellator::WindingVertex[count];
1783e9709e831954c3427d5cb839e84221a177bfedebethannicholas    GrTessellator::WindingVertex* vertsEnd = *verts;
1784e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* points = new SkPoint[count];
1785e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* pointsEnd = points;
1786e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1787f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1788e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkPoint* start = pointsEnd;
1789f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
1790e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (start != pointsEnd) {
1791e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fPos = *start;
1792e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fWinding = poly->fWinding;
1793e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++start;
1794e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++vertsEnd;
1795e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1796e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1797e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1798e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int actualCount = static_cast<int>(vertsEnd - *verts);
1799e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
1800e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(pointsEnd - points == actualCount);
1801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    delete[] points;
1802e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1803e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1804e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1805e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1806