GrTessellator.cpp revision 5ad721e94682b51b9f773d95704f11990468fef6
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
185e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) {
18616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    return a.fX > b.fX || (a.fX == b.fX && a.fY < b.fY);
187e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
188e9709e831954c3427d5cb839e84221a177bfedebethannicholas
189e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
19016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    return a.fY > b.fY || (a.fY == b.fY && a.fX > b.fX);
191e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
192e9709e831954c3427d5cb839e84221a177bfedebethannicholas
19316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitestruct Comparator {
19416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    enum class Direction { kVertical, kHorizontal };
19516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Comparator(Direction direction) : fDirection(direction) {}
19616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    bool sweep_lt(const SkPoint& a, const SkPoint& b) const {
19716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b);
19816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
19916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    bool sweep_gt(const SkPoint& a, const SkPoint& b) const {
20016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return fDirection == Direction::kHorizontal ? sweep_gt_horiz(a, b) : sweep_gt_vert(a, b);
20116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
20216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Direction fDirection;
20316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White};
20416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White
205f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) {
206f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!aaParams) {
207f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint* d = static_cast<SkPoint*>(data);
208f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        *d++ = v->fPoint;
209f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
210f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
211f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (aaParams->fTweakAlpha) {
212f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data);
213f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d->fPosition = v->fPoint;
2148c8fceff4d4e8ec09f29748d77ed5510697a2995lsalzman        d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha));
215f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        d++;
216f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return d;
217f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
218f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data);
219f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fPosition = v->fPoint;
220f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fColor = aaParams->fColor;
221f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d->fCoverage = GrNormalizeByteToFloat(v->fAlpha);
222f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    d++;
223f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return d;
224e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
225e9709e831954c3427d5cb839e84221a177bfedebethannicholas
226f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) {
22792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("emit_triangle (%g, %g) %d\n", v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha);
22892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("              (%g, %g) %d\n", v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha);
22992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    LOG("              (%g, %g) %d\n", v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha);
230f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#if TESSELLATOR_WIREFRAME
231f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
232f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
233f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
234f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
235f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
236f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
237e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else
238f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v0, aaParams, data);
239f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v1, aaParams, data);
240f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    data = emit_vertex(v2, aaParams, data);
241e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
242e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return data;
243e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
244e9709e831954c3427d5cb839e84221a177bfedebethannicholas
245e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancostruct VertexList {
246e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    VertexList() : fHead(nullptr), fTail(nullptr) {}
24716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList(Vertex* head, Vertex* tail) : fHead(head), fTail(tail) {}
248e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fHead;
249e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    Vertex* fTail;
250e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void insert(Vertex* v, Vertex* prev, Vertex* next) {
251e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail);
252e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
253e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void append(Vertex* v) {
254e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, fTail, nullptr);
255e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
256e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    void prepend(Vertex* v) {
257e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco        insert(v, nullptr, fHead);
258e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    }
259bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    void remove(Vertex* v) {
260bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail);
261bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    }
262f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
263f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
264f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fNext = fHead;
265f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fPrev = fTail;
266f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
267f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
268e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco};
269e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco
270f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Round to nearest quarter-pixel. This is used for screenspace tessellation.
271f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
272f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void round(SkPoint* p) {
273f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
274f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f);
275f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
276f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
27749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line.
27849df8d17c56ee08ecf860289d501913d356f67dcsenorblancostruct Line {
27949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {}
28049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line(const SkPoint& p, const SkPoint& q)
28149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        : fA(static_cast<double>(q.fY) - p.fY)      // a = dY
28249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fB(static_cast<double>(p.fX) - q.fX)      // b = -dX
28349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fC(static_cast<double>(p.fY) * q.fX -     // c = cross(q, p)
28449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco             static_cast<double>(p.fX) * q.fY) {}
28549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double dist(const SkPoint& p) const {
28649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * p.fX + fB * p.fY + fC;
28749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
28849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double magSq() const {
28949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fA * fA + fB * fB;
29049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
29149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
29249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    // Compute the intersection of two (infinite) Lines.
29349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    bool intersect(const Line& other, SkPoint* point) {
29449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fA * other.fB - fB * other.fA;
29549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (denom == 0.0) {
29649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            return false;
29749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        }
29849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double scale = 1.0f / denom;
29949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale);
30049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale);
30149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return true;
30249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    }
30349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double fA, fB, fC;
30449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco};
30549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco
306e9709e831954c3427d5cb839e84221a177bfedebethannicholas/**
307e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
308e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf().
309e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating
310e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g.,
311e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges.
312e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division
313e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but
314e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case.
315e9709e831954c3427d5cb839e84221a177bfedebethannicholas *
316e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic
317e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
318e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being
319e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
320e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of
321e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file).
322e9709e831954c3427d5cb839e84221a177bfedebethannicholas */
323e9709e831954c3427d5cb839e84221a177bfedebethannicholas
324e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge {
3252f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    enum class Type { kInner, kOuter, kConnector };
3262f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Edge(Vertex* top, Vertex* bottom, int winding, Type type)
327e9709e831954c3427d5cb839e84221a177bfedebethannicholas        : fWinding(winding)
328e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTop(top)
329e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fBottom(bottom)
3302f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        , fType(type)
331e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeft(nullptr)
332e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fRight(nullptr)
333e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeAbove(nullptr)
334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeAbove(nullptr)
335e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPrevEdgeBelow(nullptr)
336e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNextEdgeBelow(nullptr)
337e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fLeftPoly(nullptr)
338531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPoly(nullptr)
339531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyPrev(nullptr)
340531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fLeftPolyNext(nullptr)
341531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fRightPolyPrev(nullptr)
34270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fRightPolyNext(nullptr)
34370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        , fUsedInLeftPoly(false)
34449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fUsedInRightPoly(false)
34549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        , fLine(top, bottom) {
346e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
347e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int      fWinding;          // 1 == edge goes downward; -1 = edge goes upward.
348e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fTop;              // The top vertex in vertex-sort-order (sweep_lt).
349e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex*  fBottom;           // The bottom vertex in vertex-sort-order.
3502f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    Type     fType;
351e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fLeft;             // The linked list of edges in the active edge list.
352e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fRight;            // "
353e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeAbove;    // The linked list of edges in the bottom Vertex's "edges above".
354e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeAbove;    // "
355e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fPrevEdgeBelow;    // The linked list of edges in the top Vertex's "edges below".
356e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge*    fNextEdgeBelow;    // "
357e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fLeftPoly;         // The Poly to the left of this edge, if any.
358e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly*    fRightPoly;        // The Poly to the right of this edge, if any.
359531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyPrev;
360531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fLeftPolyNext;
361531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyPrev;
362531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Edge*    fRightPolyNext;
36370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInLeftPoly;
36470f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco    bool     fUsedInRightPoly;
36549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line     fLine;
366e9709e831954c3427d5cb839e84221a177bfedebethannicholas    double dist(const SkPoint& p) const {
36749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(p);
368e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
369e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isRightOf(Vertex* v) const {
37049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) < 0.0;
371e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
372e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLeftOf(Vertex* v) const {
37349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        return fLine.dist(v->fPoint) > 0.0;
374e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
375e9709e831954c3427d5cb839e84221a177bfedebethannicholas    void recompute() {
37649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        fLine = Line(fTop, fBottom);
377e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
378dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) {
379e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("intersecting %g -> %g with %g -> %g\n",
380e9709e831954c3427d5cb839e84221a177bfedebethannicholas               fTop->fID, fBottom->fID,
381e9709e831954c3427d5cb839e84221a177bfedebethannicholas               other.fTop->fID, other.fBottom->fID);
382e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fTop == other.fTop || fBottom == other.fBottom) {
383e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
384e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
38549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA;
386e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom == 0.0) {
387e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
388e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
3898a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
3908a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
3918a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
3928a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        double tNumer = dy * fLine.fB + dx * fLine.fA;
393e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
394e9709e831954c3427d5cb839e84221a177bfedebethannicholas        // This saves us doing the divide below unless absolutely necessary.
395e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
396e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
397e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
398e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
399e9709e831954c3427d5cb839e84221a177bfedebethannicholas        double s = sNumer / denom;
400e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(s >= 0.0 && s <= 1.0);
40149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB);
40249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA);
40356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        if (alpha) {
40492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            if (fType == Type::kConnector) {
40592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha;
40692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            } else if (other.fType == Type::kConnector) {
40792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                double t = tNumer / denom;
40892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha;
40956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else if (fType == Type::kOuter && other.fType == Type::kOuter) {
41056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White                *alpha = 0;
41156158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            } else {
41292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                *alpha = 255;
41356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White            }
41456158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White        }
415e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return true;
416e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
417f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco};
418f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
419f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct EdgeList {
4205ad721e94682b51b9f773d95704f11990468fef6Stephen White    EdgeList() : fHead(nullptr), fTail(nullptr) {}
421f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fHead;
422f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* fTail;
423f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void insert(Edge* edge, Edge* prev, Edge* next) {
424f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
425f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
426f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void append(Edge* e) {
427f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        insert(e, fTail, nullptr);
428f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
429f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void remove(Edge* edge) {
430f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
431f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
432f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void close() {
433f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (fHead && fTail) {
434f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fTail->fRight = fHead;
435f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            fHead->fLeft = fTail;
436f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
437f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
438f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    bool contains(Edge* edge) const {
439f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return edge->fLeft || edge->fRight || fHead == edge;
440e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
441e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
442e9709e831954c3427d5cb839e84221a177bfedebethannicholas
443e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
444e9709e831954c3427d5cb839e84221a177bfedebethannicholas
445e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly {
446531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Poly(Vertex* v, int winding)
447531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        : fFirstVertex(v)
448531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        , fWinding(winding)
449e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fHead(nullptr)
450e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fTail(nullptr)
451e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fNext(nullptr)
452e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fPartner(nullptr)
453e9709e831954c3427d5cb839e84221a177bfedebethannicholas        , fCount(0)
454e9709e831954c3427d5cb839e84221a177bfedebethannicholas    {
455e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
456e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static int gID = 0;
457e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fID = gID++;
458e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("*** created Poly %d\n", fID);
459e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
460e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
461531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    typedef enum { kLeft_Side, kRight_Side } Side;
462e9709e831954c3427d5cb839e84221a177bfedebethannicholas    struct MonotonePoly {
463531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        MonotonePoly(Edge* edge, Side side)
464531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            : fSide(side)
465531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fFirstEdge(nullptr)
466531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fLastEdge(nullptr)
467e9709e831954c3427d5cb839e84221a177bfedebethannicholas            , fPrev(nullptr)
468531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            , fNext(nullptr) {
469531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            this->addEdge(edge);
470531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        }
471e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Side          fSide;
472531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fFirstEdge;
473531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        Edge*         fLastEdge;
474e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fPrev;
475e9709e831954c3427d5cb839e84221a177bfedebethannicholas        MonotonePoly* fNext;
476531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        void addEdge(Edge* edge) {
477e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco            if (fSide == kRight_Side) {
478212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInRightPoly);
479531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>(
480531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
48170f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInRightPoly = true;
482e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
483212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                SkASSERT(!edge->fUsedInLeftPoly);
484531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>(
485531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge);
48670f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco                edge->fUsedInLeftPoly = true;
487e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
488e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
489e9709e831954c3427d5cb839e84221a177bfedebethannicholas
490f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        void* emit(const AAParams* aaParams, void* data) {
491531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Edge* e = fFirstEdge;
492531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            e->fTop->fPrev = e->fTop->fNext = nullptr;
493531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            VertexList vertices;
494531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            vertices.append(e->fTop);
495531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (e != nullptr) {
496531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                e->fBottom->fPrev = e->fBottom->fNext = nullptr;
497531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                if (kRight_Side == fSide) {
498531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.append(e->fBottom);
499531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fRightPolyNext;
500531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                } else {
501531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    vertices.prepend(e->fBottom);
502531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    e = e->fLeftPolyNext;
503531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                }
504531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            }
505531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            Vertex* first = vertices.fHead;
506e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* v = first->fNext;
507531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            while (v != vertices.fTail) {
508e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkASSERT(v && v->fPrev && v->fNext);
509e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* prev = v->fPrev;
510e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* curr = v;
511e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Vertex* next = v->fNext;
512e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX;
513e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY;
514e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX;
515e9709e831954c3427d5cb839e84221a177bfedebethannicholas                double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY;
516e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (ax * by - ay * bx >= 0.0) {
517f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    data = emit_triangle(prev, curr, next, aaParams, data);
518e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fPrev->fNext = v->fNext;
519e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v->fNext->fPrev = v->fPrev;
520e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (v->fPrev == first) {
521e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fNext;
522e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    } else {
523e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = v->fPrev;
524e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
525e9709e831954c3427d5cb839e84221a177bfedebethannicholas                } else {
526e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    v = v->fNext;
527e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
528e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
529e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
530e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
531e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
5325cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) {
53370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco        LOG("addEdge (%g -> %g) to poly %d, %s side\n",
53470f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco               e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right");
535e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* partner = fPartner;
536e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Poly* poly = this;
537212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        if (side == kRight_Side) {
538212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInRightPoly) {
539212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
540212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
541212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        } else {
542212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            if (e->fUsedInLeftPoly) {
543212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco                return this;
544212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco            }
545212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco        }
546e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (partner) {
547e9709e831954c3427d5cb839e84221a177bfedebethannicholas            fPartner = partner->fPartner = nullptr;
548e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
549531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        if (!fTail) {
5505cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            fHead = fTail = alloc.make<MonotonePoly>(e, side);
551531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount += 2;
55293e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco        } else if (e->fBottom == fTail->fLastEdge->fBottom) {
55393e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco            return poly;
554531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else if (side == fTail->fSide) {
555531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
556531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
557531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco        } else {
5585cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner);
559531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fTail->addEdge(e);
560531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco            fCount++;
561e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (partner) {
562531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                partner->addEdge(e, side, alloc);
563e9709e831954c3427d5cb839e84221a177bfedebethannicholas                poly = partner;
564e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
5655cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                MonotonePoly* m = alloc.make<MonotonePoly>(e, side);
566531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                m->fPrev = fTail;
567531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail->fNext = m;
568531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                fTail = m;
569e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
570e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
571e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return poly;
572e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* emit(const AAParams* aaParams, void *data) {
574e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (fCount < 3) {
575e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return data;
576e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
577e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("emit() %d, size %d\n", fID, fCount);
578e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
579f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = m->emit(aaParams, data);
580e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
581e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return data;
582e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
583531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; }
584531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco    Vertex* fFirstVertex;
585e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fWinding;
586e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fHead;
587e9709e831954c3427d5cb839e84221a177bfedebethannicholas    MonotonePoly* fTail;
588e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fNext;
589e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* fPartner;
590e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fCount;
591e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
592e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int fID;
593e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
594e9709e831954c3427d5cb839e84221a177bfedebethannicholas};
595e9709e831954c3427d5cb839e84221a177bfedebethannicholas
596e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/
597e9709e831954c3427d5cb839e84221a177bfedebethannicholas
598e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) {
599e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return a == b;
600e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
601e9709e831954c3427d5cb839e84221a177bfedebethannicholas
6025cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) {
6035cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Poly* poly = alloc.make<Poly>(v, winding);
604e9709e831954c3427d5cb839e84221a177bfedebethannicholas    poly->fNext = *head;
605e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *head = poly;
606e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return poly;
607e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
608e9709e831954c3427d5cb839e84221a177bfedebethannicholas
609e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head,
6105cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                SkArenaAlloc& alloc) {
6115cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    Vertex* v = alloc.make<Vertex>(p, 255);
612e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
613e9709e831954c3427d5cb839e84221a177bfedebethannicholas    static float gID = 0.0f;
614e9709e831954c3427d5cb839e84221a177bfedebethannicholas    v->fID = gID++;
615e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
616e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
617e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = v;
618e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fPrev = prev;
619e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
620e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *head = v;
621e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
622e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return v;
623e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
624e9709e831954c3427d5cb839e84221a177bfedebethannicholas
625e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0,
626e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p1,
627e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  const SkPoint& p2,
628e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  SkScalar tolSqd,
629e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex* prev,
630e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  Vertex** head,
631e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                  int pointsLeft,
6325cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                                  SkArenaAlloc& alloc) {
633e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2);
634e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) {
635e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p2, prev, head, alloc);
636e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
637e9709e831954c3427d5cb839e84221a177bfedebethannicholas
638e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
639e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
640e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
641e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
642e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) };
643e9709e831954c3427d5cb839e84221a177bfedebethannicholas
644e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
645e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc);
646e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc);
647e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
648e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
649e9709e831954c3427d5cb839e84221a177bfedebethannicholas
650e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0,
651e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p1,
652e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p2,
653e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              const SkPoint& p3,
654e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              SkScalar tolSqd,
655e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex* prev,
656e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              Vertex** head,
657e9709e831954c3427d5cb839e84221a177bfedebethannicholas                              int pointsLeft,
6585cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                              SkArenaAlloc& alloc) {
659e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3);
660e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3);
661e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) ||
662e9709e831954c3427d5cb839e84221a177bfedebethannicholas        !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) {
663e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return append_point_to_contour(p3, prev, head, alloc);
664e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
665e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint q[] = {
666e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) },
667e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) },
668e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) }
669e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
670e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint r[] = {
671e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) },
672e9709e831954c3427d5cb839e84221a177bfedebethannicholas        { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) }
673e9709e831954c3427d5cb839e84221a177bfedebethannicholas    };
674e9709e831954c3427d5cb839e84221a177bfedebethannicholas    const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) };
675e9709e831954c3427d5cb839e84221a177bfedebethannicholas    pointsLeft >>= 1;
676e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc);
677e9709e831954c3427d5cb839e84221a177bfedebethannicholas    prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc);
678e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return prev;
679e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
680e9709e831954c3427d5cb839e84221a177bfedebethannicholas
681e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices).
682e9709e831954c3427d5cb839e84221a177bfedebethannicholas
683e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
6845cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) {
685e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkScalar toleranceSqd = tolerance * tolerance;
686e9709e831954c3427d5cb839e84221a177bfedebethannicholas
687e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint pts[4];
688e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool done = false;
689e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *isLinear = true;
690e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::Iter iter(path, false);
691e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
692e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* head = nullptr;
693e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (path.isInverseFillType()) {
694e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPoint quad[4];
695e9709e831954c3427d5cb839e84221a177bfedebethannicholas        clipBounds.toQuad(quad);
6967ab96e92196dd74d5b95d33c8477b256813f3046senorblanco        for (int i = 3; i >= 0; i--) {
697e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = append_point_to_contour(quad[i], prev, &head, alloc);
698e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
699e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head->fPrev = prev;
700e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev->fNext = head;
701e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *contours++ = head;
702e9709e831954c3427d5cb839e84221a177bfedebethannicholas        head = prev = nullptr;
703e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
704e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkAutoConicToQuads converter;
705e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (!done) {
706e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkPath::Verb verb = iter.next(pts);
707e9709e831954c3427d5cb839e84221a177bfedebethannicholas        switch (verb) {
708e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kConic_Verb: {
709e9709e831954c3427d5cb839e84221a177bfedebethannicholas                SkScalar weight = iter.conicWeight();
710e9709e831954c3427d5cb839e84221a177bfedebethannicholas                const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd);
711e9709e831954c3427d5cb839e84221a177bfedebethannicholas                for (int i = 0; i < converter.countQuads(); ++i) {
712e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance);
713e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2],
714e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                     toleranceSqd, prev, &head, pointsLeft, alloc);
715e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    quadPts += 2;
716e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
717e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
718e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
719e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
720e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kMove_Verb:
721e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
722e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
723e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
724e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
725e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
726e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
727e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[0], prev, &head, alloc);
728e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
729e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kLine_Verb: {
730e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = append_point_to_contour(pts[1], prev, &head, alloc);
731e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
732e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
733e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kQuad_Verb: {
734e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance);
735e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev,
736e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                 &head, pointsLeft, alloc);
737e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
738e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
739e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
740e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kCubic_Verb: {
741e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance);
742e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3],
743e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                toleranceSqd, prev, &head, pointsLeft, alloc);
744e9709e831954c3427d5cb839e84221a177bfedebethannicholas                *isLinear = false;
745e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
746e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
747e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kClose_Verb:
748e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
749e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
750e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
751e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
752e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
753e9709e831954c3427d5cb839e84221a177bfedebethannicholas                head = prev = nullptr;
754e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
755e9709e831954c3427d5cb839e84221a177bfedebethannicholas            case SkPath::kDone_Verb:
756e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (head) {
757e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    head->fPrev = prev;
758e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prev->fNext = head;
759e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    *contours++ = head;
760e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
761e9709e831954c3427d5cb839e84221a177bfedebethannicholas                done = true;
762e9709e831954c3427d5cb839e84221a177bfedebethannicholas                break;
763e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
764e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
765e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
766e9709e831954c3427d5cb839e84221a177bfedebethannicholas
767497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, int winding) {
768e9709e831954c3427d5cb839e84221a177bfedebethannicholas    switch (fillType) {
769e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kWinding_FillType:
770e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return winding != 0;
771e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kEvenOdd_FillType:
772e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) != 0;
773e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseWinding_FillType:
7747ab96e92196dd74d5b95d33c8477b256813f3046senorblanco            return winding == 1;
775e9709e831954c3427d5cb839e84221a177bfedebethannicholas        case SkPath::kInverseEvenOdd_FillType:
776e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return (winding & 1) == 1;
777e9709e831954c3427d5cb839e84221a177bfedebethannicholas        default:
778e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkASSERT(false);
779e9709e831954c3427d5cb839e84221a177bfedebethannicholas            return false;
780e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
781e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
782e9709e831954c3427d5cb839e84221a177bfedebethannicholas
783497890630b80a381a2ac4cbb9114b0320560bf8cStephen Whiteinline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) {
784497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    return poly && apply_fill_type(fillType, poly->fWinding);
785497890630b80a381a2ac4cbb9114b0320560bf8cStephen White}
786497890630b80a381a2ac4cbb9114b0320560bf8cStephen White
7875cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) {
7882f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
789e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = winding < 0 ? next : prev;
790e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = winding < 0 ? prev : next;
7915cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby    return alloc.make<Edge>(top, bottom, winding, type);
792e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
793e9709e831954c3427d5cb839e84221a177bfedebethannicholas
794e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) {
795e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
796f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(edges->contains(edge));
797f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->remove(edge);
798e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
799e9709e831954c3427d5cb839e84221a177bfedebethannicholas
800e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) {
801e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID);
802f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkASSERT(!edges->contains(edge));
803e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = prev ? prev->fRight : edges->fHead;
804f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    edges->insert(edge, prev, next);
805e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
806e9709e831954c3427d5cb839e84221a177bfedebethannicholas
807e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) {
808e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (v->fFirstEdgeAbove) {
809e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *left = v->fFirstEdgeAbove->fLeft;
810e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *right = v->fLastEdgeAbove->fRight;
811e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
812e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
813e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next = nullptr;
814e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev;
815e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) {
816e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (prev->isLeftOf(v)) {
817e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
818e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
819e9709e831954c3427d5cb839e84221a177bfedebethannicholas        next = prev;
820e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
821e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
822e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
823e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
824e9709e831954c3427d5cb839e84221a177bfedebethannicholas
825e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) {
826e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
827e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
828e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = edges->fHead; next != nullptr; next = next->fRight) {
829e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) ||
830e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) ||
831e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) &&
832e9709e831954c3427d5cb839e84221a177bfedebethannicholas             next->isRightOf(edge->fBottom)) ||
833e9709e831954c3427d5cb839e84221a177bfedebethannicholas            (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) &&
834e9709e831954c3427d5cb839e84221a177bfedebethannicholas             edge->isLeftOf(next->fBottom))) {
835e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
836e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
837e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
838e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
839e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *left = prev;
840e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *right = next;
841e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
842e9709e831954c3427d5cb839e84221a177bfedebethannicholas
843e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) {
8442f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (!activeEdges) {
8452f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
8462f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
8472f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    if (activeEdges->contains(edge)) {
848e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) {
849e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(edge, activeEdges);
850e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
851e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) {
852e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* left;
853e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* right;
854e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(edge, activeEdges, c, &left, &right);
855e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge(edge, left, activeEdges);
856e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
857e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
858e9709e831954c3427d5cb839e84221a177bfedebethannicholas
859e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) {
860e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
861e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
862e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
863e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
864e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
865e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
866e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
867e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) {
868e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fTop)) {
869e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
870e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
871e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
872e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
873e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
874e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove);
875e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
876e9709e831954c3427d5cb839e84221a177bfedebethannicholas
877e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) {
878e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fTop->fPoint == edge->fBottom->fPoint ||
879e9709e831954c3427d5cb839e84221a177bfedebethannicholas        c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) {
880e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
881e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
882e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID);
883e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* prev = nullptr;
884e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Edge* next;
885e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) {
886e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (next->isRightOf(edge->fBottom)) {
887e9709e831954c3427d5cb839e84221a177bfedebethannicholas            break;
888e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
889e9709e831954c3427d5cb839e84221a177bfedebethannicholas        prev = next;
890e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
891e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
892e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow);
893e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
894e9709e831954c3427d5cb839e84221a177bfedebethannicholas
895e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) {
896e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
897e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fBottom->fID);
898e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>(
899e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove);
900e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
901e9709e831954c3427d5cb839e84221a177bfedebethannicholas
902e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) {
903e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID,
904e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID);
905e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco    list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>(
906e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow);
907e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
908e9709e831954c3427d5cb839e84221a177bfedebethannicholas
909e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid disconnect(Edge* edge)
910e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White{
911e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
912e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
913e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White}
914e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White
915e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid erase_edge(Edge* edge, EdgeList* edges) {
916e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID);
917e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White    disconnect(edge);
918f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (edges && edges->contains(edge)) {
919e9709e831954c3427d5cb839e84221a177bfedebethannicholas        remove_edge(edge, edges);
920e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
921e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
922e9709e831954c3427d5cb839e84221a177bfedebethannicholas
923e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c);
924e9709e831954c3427d5cb839e84221a177bfedebethannicholas
925e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
926e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_below(edge);
927e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fTop = v;
928e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
929e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_below(edge, v, c);
930e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
931e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
932e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
933e9709e831954c3427d5cb839e84221a177bfedebethannicholas
934e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) {
935e9709e831954c3427d5cb839e84221a177bfedebethannicholas    remove_edge_above(edge);
936e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->fBottom = v;
937e9709e831954c3427d5cb839e84221a177bfedebethannicholas    edge->recompute();
938e9709e831954c3427d5cb839e84221a177bfedebethannicholas    insert_edge_above(edge, v, c);
939e9709e831954c3427d5cb839e84221a177bfedebethannicholas    fix_active_state(edge, activeEdges, c);
940e9709e831954c3427d5cb839e84221a177bfedebethannicholas    merge_collinear_edges(edge, activeEdges, c);
941e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
942e9709e831954c3427d5cb839e84221a177bfedebethannicholas
943e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
944e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) {
945e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n",
946e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
947e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
948e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
949e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
950e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) {
951e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
952e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, other->fTop, activeEdges, c);
953e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
954e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
955e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(other, edge->fTop, activeEdges, c);
956e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
957e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
958e9709e831954c3427d5cb839e84221a177bfedebethannicholas
959e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) {
960e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) {
961e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n",
962e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fTop->fPoint.fX, edge->fTop->fPoint.fY,
963e9709e831954c3427d5cb839e84221a177bfedebethannicholas            edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY);
964e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
965e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        erase_edge(edge, activeEdges);
966e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) {
967e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fWinding += other->fWinding;
968e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(other, edge->fBottom, activeEdges, c);
969e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
970e9709e831954c3427d5cb839e84221a177bfedebethannicholas        other->fWinding += edge->fWinding;
971e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, other->fBottom, activeEdges, c);
972e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
973e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
974e9709e831954c3427d5cb839e84221a177bfedebethannicholas
975e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) {
976e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop ||
977e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) {
978e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c);
979e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop ||
980e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) {
981e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c);
982e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
983e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
984e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) {
985e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
986e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom ||
987e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                        !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) {
988e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
989e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
990e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
991e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9925cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc);
993e9709e831954c3427d5cb839e84221a177bfedebethannicholas
9945cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
995e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* top = edge->fTop;
996e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* bottom = edge->fBottom;
997e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fLeft) {
998e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftTop = edge->fLeft->fTop;
999e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* leftBottom = edge->fLeft->fBottom;
1000e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) {
1001e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
1002e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) {
1003e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftTop, activeEdges, c, alloc);
1004e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) &&
1005e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fLeft->isLeftOf(bottom)) {
1006e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fLeft, bottom, activeEdges, c, alloc);
1007e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) {
1008e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, leftBottom, activeEdges, c, alloc);
1009e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1010e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1011e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (edge->fRight) {
1012e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightTop = edge->fRight->fTop;
1013e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* rightBottom = edge->fRight->fBottom;
1014e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) {
1015e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, top, activeEdges, c, alloc);
1016e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) {
1017e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightTop, activeEdges, c, alloc);
1018e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) &&
1019e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->fRight->isRightOf(bottom)) {
1020e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge->fRight, bottom, activeEdges, c, alloc);
1021e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) &&
1022e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   !edge->isLeftOf(rightBottom)) {
1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, rightBottom, activeEdges, c, alloc);
1024e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1025e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1026e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1027e9709e831954c3427d5cb839e84221a177bfedebethannicholas
10285cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) {
1029e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n",
1030e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge->fTop->fID, edge->fBottom->fID,
1031e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID, v->fPoint.fX, v->fPoint.fY);
1032e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) {
1033e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, v, activeEdges, c);
1034e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) {
1035e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1036e9709e831954c3427d5cb839e84221a177bfedebethannicholas    } else {
10375cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby        Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType);
1038e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_below(newEdge, v, c);
1039e9709e831954c3427d5cb839e84221a177bfedebethannicholas        insert_edge_above(newEdge, edge->fBottom, c);
1040e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, v, activeEdges, c);
1041e9709e831954c3427d5cb839e84221a177bfedebethannicholas        cleanup_active_edges(edge, activeEdges, c, alloc);
1042e9709e831954c3427d5cb839e84221a177bfedebethannicholas        fix_active_state(newEdge, activeEdges, c);
1043e9709e831954c3427d5cb839e84221a177bfedebethannicholas        merge_collinear_edges(newEdge, activeEdges, c);
1044e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1045e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1046e9709e831954c3427d5cb839e84221a177bfedebethannicholas
10475cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
104848ded38da99c1171ba1bb469f6500f8214e9105cStephen White              int winding_scale = 1) {
1049bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    Edge* edge = new_edge(prev, next, type, c, alloc);
10508a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_below(edge, edge->fTop, c);
10518a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    insert_edge_above(edge, edge->fBottom, c);
105248ded38da99c1171ba1bb469f6500f8214e9105cStephen White    edge->fWinding *= winding_scale;
1053f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_collinear_edges(edge, nullptr, c);
1054f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return edge;
1055f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1056f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1057bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c,
10585cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    SkArenaAlloc& alloc) {
1059e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY,
1060e9709e831954c3427d5cb839e84221a177bfedebethannicholas        src->fID, dst->fID);
1061f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha);
1062e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeAbove; edge;) {
1063e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeAbove;
1064e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_bottom(edge, dst, nullptr, c);
1065e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1066e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1067e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Edge* edge = src->fFirstEdgeBelow; edge;) {
1068e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* next = edge->fNextEdgeBelow;
1069e9709e831954c3427d5cb839e84221a177bfedebethannicholas        set_top(edge, dst, nullptr, c);
1070e9709e831954c3427d5cb839e84221a177bfedebethannicholas        edge = next;
1071e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1072bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->remove(src);
1073e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1074e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1075f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancouint8_t max_edge_alpha(Edge* a, Edge* b) {
107656158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) {
10772f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 255;
10782f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) {
10792f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return 0;
10802f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    } else {
10812f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha),
10822f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White                      SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha));
10832f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White    }
1084f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1085f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1086e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c,
10875cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1088e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (!edge || !other) {
1089e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return nullptr;
1090e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
109156158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    SkPoint p;
109256158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    uint8_t alpha;
109356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White    if (edge->intersect(*other, &p, &alpha)) {
1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v;
1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
1097e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fTop, activeEdges, c, alloc);
1098e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fTop;
1099e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) {
1100e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, edge->fBottom, activeEdges, c, alloc);
1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = edge->fBottom;
1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) {
1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fTop, activeEdges, c, alloc);
1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fTop;
1105e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) {
1106e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, other->fBottom, activeEdges, c, alloc);
1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = other->fBottom;
1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1109e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* nextV = edge->fTop;
1110e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(p, nextV->fPoint)) {
1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fPrev;
1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (c.sweep_lt(nextV->fPoint, p)) {
1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV = nextV->fNext;
1115e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1116e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* prevV = nextV->fPrev;
1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(prevV->fPoint, p)) {
1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = prevV;
1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else if (coincident(nextV->fPoint, p)) {
1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = nextV;
1121e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
11225cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                v = alloc.make<Vertex>(p, alpha);
1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("inserting between %g (%g, %g) and %g (%g, %g)\n",
1124e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY,
1125e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY);
1126e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fID = (nextV->fID + prevV->fID) * 0.5f;
1128e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prevV;
1130e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext = nextV;
1131e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prevV->fNext = v;
1132e9709e831954c3427d5cb839e84221a177bfedebethannicholas                nextV->fPrev = v;
1133e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1134e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(edge, v, activeEdges, c, alloc);
1135e9709e831954c3427d5cb839e84221a177bfedebethannicholas            split_edge(other, v, activeEdges, c, alloc);
1136e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
113792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        v->fAlpha = SkTMax(v->fAlpha, alpha);
1138e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return v;
1139e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1140e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return nullptr;
1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1143f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid sanitize_contours(Vertex** contours, int contourCnt, bool approximate) {
1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(contours[i]);
11465926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        if (approximate) {
11475926f2da752d1bff0051fda4137bb967f9e91d5fStephen White            round(&contours[i]->fPrev->fPoint);
11485926f2da752d1bff0051fda4137bb967f9e91d5fStephen White        }
1149e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i];;) {
1150f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (approximate) {
1151f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                round(&v->fPoint);
1152f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1153e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (coincident(v->fPrev->fPoint, v->fPoint)) {
1154e9709e831954c3427d5cb839e84221a177bfedebethannicholas                LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY);
1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v->fPrev == v) {
1156e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    contours[i] = nullptr;
1157e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    break;
1158e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev->fNext = v->fNext;
1160e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fNext->fPrev = v->fPrev;
1161e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (contours[i] == v) {
11625926f2da752d1bff0051fda4137bb967f9e91d5fStephen White                    contours[i] = v->fPrev;
1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fPrev;
1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1166e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v = v->fNext;
1167e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (v == contours[i]) break;
1168e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1172e9709e831954c3427d5cb839e84221a177bfedebethannicholas
11735cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1174bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) {
1175e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) {
1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fPoint = v->fPrev->fPoint;
1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (coincident(v->fPrev->fPoint, v->fPoint)) {
1179bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            merge_vertices(v->fPrev, v, mesh, c, alloc);
1180e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1184e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices.
1185e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1186bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c,
11875cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                 SkArenaAlloc& alloc) {
1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Vertex* prev = nullptr;
1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Vertex* v = contours[i]; v != nullptr;) {
1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* vNext = v->fNext;
1192bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            connect(v->fPrev, v, Edge::Type::kInner, c, alloc);
1193e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (prev) {
1194e9709e831954c3427d5cb839e84221a177bfedebethannicholas                prev->fNext = v;
1195e9709e831954c3427d5cb839e84221a177bfedebethannicholas                v->fPrev = prev;
1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1197bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                mesh->fHead = v;
1198e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1199e9709e831954c3427d5cb839e84221a177bfedebethannicholas            prev = v;
1200e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v = vNext;
1201e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v == contours[i]) break;
1202e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1203e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (prev) {
1205bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        prev->fNext = mesh->fHead->fPrev = nullptr;
1206e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1207bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    mesh->fTail = prev;
1208e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1209e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1210e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction.
1211e9709e831954c3427d5cb839e84221a177bfedebethannicholas
121216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitetemplate <CompareFunc sweep_lt>
121316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen Whitevoid merge_sort(VertexList* vertices) {
121416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* slow = vertices->fHead;
121516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!slow) {
1216e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return;
1217e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
121816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* fast = slow->fNext;
121916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (!fast) {
122016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        return;
122116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
122216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    do {
122316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        fast = fast->fNext;
122416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (fast) {
122516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            fast = fast->fNext;
122616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            slow = slow->fNext;
122716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        }
122816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } while (fast);
122916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList front(vertices->fHead, slow);
123016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    VertexList back(slow->fNext, vertices->fTail);
123116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    front.fTail->fNext = back.fHead->fPrev = nullptr;
1232e9709e831954c3427d5cb839e84221a177bfedebethannicholas
123316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&front);
123416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    merge_sort<sweep_lt>(&back);
1235e9709e831954c3427d5cb839e84221a177bfedebethannicholas
123616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    vertices->fHead = vertices->fTail = nullptr;
123716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* a = front.fHead;
123816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Vertex* b = back.fHead;
1239e9709e831954c3427d5cb839e84221a177bfedebethannicholas    while (a && b) {
124016a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        if (sweep_lt(a->fPoint, b->fPoint)) {
1241e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = a->fNext;
124216a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(a);
1243e9709e831954c3427d5cb839e84221a177bfedebethannicholas            a = next;
1244e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Vertex* next = b->fNext;
124616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White            vertices->append(b);
1247e9709e831954c3427d5cb839e84221a177bfedebethannicholas            b = next;
1248e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1249e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1250e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (a) {
125116a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(a, vertices->fTail, a->fNext);
1252e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1253e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (b) {
125416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        vertices->insert(b, vertices->fTail, b->fNext);
1255e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1256e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1257e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1258e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges.
1259e9709e831954c3427d5cb839e84221a177bfedebethannicholas
12605cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) {
1261e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("simplifying complex polygons\n");
1262e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1263bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1264e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1265e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1266e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1267e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1268f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1269e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
12708a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
12718a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas        bool restartChecks;
1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas        do {
1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas            restartChecks = false;
1275e9709e831954c3427d5cb839e84221a177bfedebethannicholas            find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (v->fFirstEdgeBelow) {
1277bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White                for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) {
1278e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) {
1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1280e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) {
1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        restartChecks = true;
1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        break;
1285e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1286e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1287e9709e831954c3427d5cb839e84221a177bfedebethannicholas            } else {
1288e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge,
1289e9709e831954c3427d5cb839e84221a177bfedebethannicholas                                                        &activeEdges, c, alloc)) {
1290e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1291e9709e831954c3427d5cb839e84221a177bfedebethannicholas                        v = pv;
1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
1293e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    restartChecks = true;
1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1297e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } while (restartChecks);
1298f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (v->fAlpha == 0) {
129948ded38da99c1171ba1bb469f6500f8214e9105cStephen White            if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) &&
130048ded38da99c1171ba1bb469f6500f8214e9105cStephen White                (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) {
1301f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge);
1302f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1303f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1304e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1305e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(e, &activeEdges);
1306e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1307e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Edge* leftEdge = leftEnclosingEdge;
1308e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1309e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(e, leftEdge, &activeEdges);
1310e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge = e;
1311e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1312e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fProcessed = true;
1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1314e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1315e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1316e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons.
1317e9709e831954c3427d5cb839e84221a177bfedebethannicholas
13185cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
1319e9709e831954c3427d5cb839e84221a177bfedebethannicholas    LOG("tessellating simple polygons\n");
1320e9709e831954c3427d5cb839e84221a177bfedebethannicholas    EdgeList activeEdges;
1321e9709e831954c3427d5cb839e84221a177bfedebethannicholas    Poly* polys = nullptr;
1322bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) {
1323e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1324e9709e831954c3427d5cb839e84221a177bfedebethannicholas            continue;
1325e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1326e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1327f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
1328e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
13298a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* leftEnclosingEdge;
13308a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Edge* rightEnclosingEdge;
1331e9709e831954c3427d5cb839e84221a177bfedebethannicholas        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
13328a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* leftPoly;
13338a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        Poly* rightPoly;
1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = v->fFirstEdgeAbove->fLeftPoly;
1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = v->fLastEdgeAbove->fRightPoly;
1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas        } else {
1338e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr;
1339e9709e831954c3427d5cb839e84221a177bfedebethannicholas            rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr;
1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1342e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges above:\n");
1343e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1345e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1346e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1347e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("edges below:\n");
1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1349e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1350e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1351e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1352e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1353e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeAbove) {
1354e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (leftPoly) {
1355531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc);
1356e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1357e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (rightPoly) {
1358531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas                Edge* rightEdge = e->fNextEdgeAbove;
13628a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                SkASSERT(rightEdge->isRightOf(e->fTop));
13638a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                remove_edge(e, &activeEdges);
13648a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (e->fRightPoly) {
13658a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                    e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
13678a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White                if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
1368531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
1369e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1370e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1371e9709e831954c3427d5cb839e84221a177bfedebethannicholas            remove_edge(v->fLastEdgeAbove, &activeEdges);
1372e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeBelow) {
1373e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (leftPoly && rightPoly && leftPoly != rightPoly) {
1374e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr);
1375e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    rightPoly->fPartner = leftPoly;
1376e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftPoly->fPartner = rightPoly;
1377e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1378e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1379e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1380e9709e831954c3427d5cb839e84221a177bfedebethannicholas        if (v->fFirstEdgeBelow) {
1381e9709e831954c3427d5cb839e84221a177bfedebethannicholas            if (!v->fFirstEdgeAbove) {
138293e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco                if (leftPoly && rightPoly) {
1383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    if (leftPoly == rightPoly) {
1384531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) {
1385531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftPoly = new_poly(&polys, leftPoly->lastVertex(),
1386531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 leftPoly->fWinding, alloc);
1387531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            leftEnclosingEdge->fRightPoly = leftPoly;
1388531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        } else {
1389531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightPoly = new_poly(&polys, rightPoly->lastVertex(),
1390531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                                                 rightPoly->fWinding, alloc);
1391531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                            rightEnclosingEdge->fLeftPoly = rightPoly;
1392531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                        }
1393e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    }
13945cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner);
1395531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc);
1396531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco                    rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc);
1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1399e9709e831954c3427d5cb839e84221a177bfedebethannicholas            Edge* leftEdge = v->fFirstEdgeBelow;
1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas            leftEdge->fLeftPoly = leftPoly;
1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas            insert_edge(leftEdge, leftEnclosingEdge, &activeEdges);
1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas            for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge;
1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas                 rightEdge = rightEdge->fNextEdgeBelow) {
1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas                insert_edge(rightEdge, leftEdge, &activeEdges);
1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas                int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0;
1406e9709e831954c3427d5cb839e84221a177bfedebethannicholas                winding += leftEdge->fWinding;
1407e9709e831954c3427d5cb839e84221a177bfedebethannicholas                if (winding != 0) {
1408e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    Poly* poly = new_poly(&polys, v, winding, alloc);
1409e9709e831954c3427d5cb839e84221a177bfedebethannicholas                    leftEdge->fRightPoly = rightEdge->fLeftPoly = poly;
1410e9709e831954c3427d5cb839e84221a177bfedebethannicholas                }
1411e9709e831954c3427d5cb839e84221a177bfedebethannicholas                leftEdge = rightEdge;
1412e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1413e9709e831954c3427d5cb839e84221a177bfedebethannicholas            v->fLastEdgeBelow->fRightPoly = rightPoly;
1414e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1415e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1416e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("\nactive edges:\n");
1417e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) {
1418e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID,
1419e9709e831954c3427d5cb839e84221a177bfedebethannicholas                e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1);
1420e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1421e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1422e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1423e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return polys;
1424e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1425e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1426bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType,
14275cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                               SkArenaAlloc& alloc) {
1428497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    LOG("removing non-boundary edges\n");
1429497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    EdgeList activeEdges;
1430bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) {
1431497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) {
1432497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            continue;
1433497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1434497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* leftEnclosingEdge;
1435497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* rightEnclosingEdge;
1436497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
1437497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        bool prevFilled = leftEnclosingEdge &&
1438497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                          apply_fill_type(fillType, leftEnclosingEdge->fWinding);
1439497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeAbove; e;) {
1440497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            Edge* next = e->fNextEdgeAbove;
1441497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            remove_edge(e, &activeEdges);
1442497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            bool filled = apply_fill_type(fillType, e->fWinding);
1443497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (filled == prevFilled) {
1444e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White                disconnect(e);
1445f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1446497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prevFilled = filled;
1447f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = next;
1448f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1449497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        Edge* prev = leftEnclosingEdge;
1450497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) {
1451497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            if (prev) {
1452497890630b80a381a2ac4cbb9114b0320560bf8cStephen White                e->fWinding += prev->fWinding;
1453497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            }
1454497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            insert_edge(e, prev, &activeEdges);
1455497890630b80a381a2ac4cbb9114b0320560bf8cStephen White            prev = e;
1456497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        }
1457682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco    }
1458f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1459f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1460f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid get_edge_normal(const Edge* e, SkVector* normal) {
1461eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White    normal->setNormalize(SkDoubleToScalar(e->fLine.fA) * e->fWinding,
1462eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White                         SkDoubleToScalar(e->fLine.fB) * e->fWinding);
1463f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1464f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1465f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions
1466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to
1467f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// invert on stroking.
1468f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
14695cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) {
1470f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1471f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    SkVector prevNormal;
1472f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    get_edge_normal(prevEdge, &prevNormal);
1473f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr;) {
1474f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom;
1475f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop;
1476f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        double dist = e->dist(prev->fPoint);
1477f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkVector normal;
1478f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        get_edge_normal(e, &normal);
1479eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White        float denom = 0.0625f * static_cast<float>(e->fLine.magSq());
1480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) {
1481bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White            Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc);
1482f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            insert_edge(join, e, boundary);
1483f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(prevEdge, boundary);
1484f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            remove_edge(e, boundary);
1485f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if (join->fLeft && join->fRight) {
1486f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = join->fLeft;
1487f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = join;
1488f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else {
1489f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                prevEdge = boundary->fTail;
1490f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                e = boundary->fHead; // join->fLeft ? join->fLeft : join;
1491f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1492f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            get_edge_normal(prevEdge, &prevNormal);
1493f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1494f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevEdge = e;
1495f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            prevNormal = normal;
1496f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            e = e->fRight;
1497f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1498f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1499f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1500f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1501dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagnervoid fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector,
150292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White                    Edge* prevEdge, Comparator& c) {
150392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    if (!prev || !next) {
150492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        return;
150592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
150692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1;
150792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    SkPoint p;
150892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    uint8_t alpha;
1509dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) {
151092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fPoint = next->fPoint = p;
151192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White        prev->fAlpha = next->fAlpha = alpha;
151292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    }
151392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White}
151492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White
1515f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to
1516f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a
1517f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// new antialiased mesh from those vertices.
1518f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15195cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
15208a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    // A boundary with fewer than 3 edges is degenerate.
15218a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
15228a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White        return;
15238a0bfc5201f14c994fe8062ecf1ca34a172de9d3Stephen White    }
1524f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Edge* prevEdge = boundary->fTail;
1525f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    float radius = 0.5f;
152649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
152749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line prevInner(prevEdge->fTop, prevEdge->fBottom);
1528f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevInner.fC -= offset;
152949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco    Line prevOuter(prevEdge->fTop, prevEdge->fBottom);
1530f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    prevOuter.fC += offset;
1531f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList innerVertices;
1532f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    VertexList outerVertices;
1533dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* prevBisector = nullptr;
1534f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) {
153549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding;
153649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        Line inner(e->fTop, e->fBottom);
1537f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        inner.fC -= offset;
153849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        Line outer(e->fTop, e->fBottom);
1539f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outer.fC += offset;
1540f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        SkPoint innerPoint, outerPoint;
154149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco        if (prevInner.intersect(inner, &innerPoint) &&
154249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco            prevOuter.intersect(outer, &outerPoint)) {
15435cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255);
15445cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby            Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0);
1545dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner            Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc);
154692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c);
154792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c);
154892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            innerVertices.append(innerVertex);
154992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            outerVertices.append(outerVertex);
155092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White            prevBisector = bisector;
1551f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1552f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevInner = inner;
1553f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        prevOuter = outer;
155486cc841e23a81af69269de87224c57be274f40d1Stephen White        prevEdge = e;
1555f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1556f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    innerVertices.close();
1557f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    outerVertices.close();
1558f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1559f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* innerVertex = innerVertices.fHead;
1560f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Vertex* outerVertex = outerVertices.fHead;
1561f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (!innerVertex || !outerVertex) {
1562f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        return;
1563f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1564dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner    Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c,
1565dab4811661720e8f14675a09ca1a3b8f82e45680Ben Wagner                              alloc);
156692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c);
156792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White    fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c);
1568f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    do {
156948ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connect vertices into a quad mesh. Outer edges get default (1) winding.
157048ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Inner edges get -2 winding. This ensures that the interior is always filled
157148ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // (-1 winding number for normal cases, 3 for thin features where the interior inverts).
157248ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // Connector edges get zero winding, since they're only structural (i.e., to ensure
157348ded38da99c1171ba1bb469f6500f8214e9105cStephen White        // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number.
1574bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc);
157548ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2);
157648ded38da99c1171ba1bb469f6500f8214e9105cStephen White        connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0);
1577f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* innerNext = innerVertex->fNext;
1578f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Vertex* outerNext = outerVertex->fNext;
1579f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(innerVertex);
1580f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        mesh->append(outerVertex);
1581f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        innerVertex = innerNext;
1582f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        outerVertex = outerNext;
1583f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead);
1584f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1585f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
15865cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) {
1587497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    bool down = apply_fill_type(fillType, e->fWinding);
1588f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    while (e) {
1589f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e->fWinding = down ? 1 : -1;
1590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        Edge* next;
1591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        boundary->append(e);
1592f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (down) {
1593f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in clockwise order.
1594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fNextEdgeAbove)) {
1595f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1596f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fBottom->fLastEdgeBelow)) {
1597f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1598f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fPrevEdgeAbove)) {
1599f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1600f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1601f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        } else {
1602f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            // Find outgoing edge, in counter-clockwise order.
1603f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            if ((next = e->fPrevEdgeBelow)) {
1604f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1605f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fTop->fFirstEdgeAbove)) {
1606f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = false;
1607f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            } else if ((next = e->fNextEdgeBelow)) {
1608f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                down = true;
1609f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            }
1610f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1611e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White        disconnect(e);
1612f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        e = next;
1613f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1614f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1615f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16165ad721e94682b51b9f773d95704f11990468fef6Stephen White// Stage 5b: Extract boundaries from mesh, simplify and stroke them into a new mesh.
1617f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16185ad721e94682b51b9f773d95704f11990468fef6Stephen Whitevoid extract_boundaries(const VertexList& inMesh, VertexList* outMesh, SkPath::FillType fillType,
16195ad721e94682b51b9f773d95704f11990468fef6Stephen White                        Comparator& c, SkArenaAlloc& alloc) {
16205ad721e94682b51b9f773d95704f11990468fef6Stephen White    remove_non_boundary_edges(inMesh, fillType, alloc);
16215ad721e94682b51b9f773d95704f11990468fef6Stephen White    for (Vertex* v = inMesh.fHead; v; v = v->fNext) {
1622f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        while (v->fFirstEdgeBelow) {
16235ad721e94682b51b9f773d95704f11990468fef6Stephen White            EdgeList boundary;
16245ad721e94682b51b9f773d95704f11990468fef6Stephen White            extract_boundary(&boundary, v->fFirstEdgeBelow, fillType, alloc);
16255ad721e94682b51b9f773d95704f11990468fef6Stephen White            simplify_boundary(&boundary, c, alloc);
16265ad721e94682b51b9f773d95704f11990468fef6Stephen White            boundary_to_aa_mesh(&boundary, outMesh, c, alloc);
1627f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1628f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1629f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1630f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1631f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// This is a driver function which calls stages 2-5 in turn.
1632f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1633bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid contours_to_mesh(Vertex** contours, int contourCnt, bool antialias,
16345cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                      VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
1635e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
1636e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (int i = 0; i < contourCnt; ++i) {
1637e9709e831954c3427d5cb839e84221a177bfedebethannicholas        Vertex* v = contours[i];
1638e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkASSERT(v);
1639e9709e831954c3427d5cb839e84221a177bfedebethannicholas        LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1640e9709e831954c3427d5cb839e84221a177bfedebethannicholas        for (v = v->fNext; v != contours[i]; v = v->fNext) {
1641e9709e831954c3427d5cb839e84221a177bfedebethannicholas            LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1642e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1643e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1644e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1645f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    sanitize_contours(contours, contourCnt, antialias);
1646bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    build_edges(contours, contourCnt, mesh, c, alloc);
1647f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1648f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
16495cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) {
1650bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    if (!vertices || !vertices->fHead) {
16512f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White        return;
1652e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1653e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1654e9709e831954c3427d5cb839e84221a177bfedebethannicholas    // Sort vertices in Y (secondarily in X).
165516a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    if (c.fDirection == Comparator::Direction::kHorizontal) {
165616a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_horiz>(vertices);
165716a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    } else {
165816a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White        merge_sort<sweep_lt_vert>(vertices);
165916a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    }
1660f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    merge_coincident_vertices(vertices, c, alloc);
1661e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED
16622e2cb9bc768bd13e1beeafc9acf8b8f19bea3215Stephen White    for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) {
1663e9709e831954c3427d5cb839e84221a177bfedebethannicholas        static float gID = 0.0f;
1664e9709e831954c3427d5cb839e84221a177bfedebethannicholas        v->fID = gID++;
1665e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1666e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif
1667f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    simplify(*vertices, c, alloc);
16682f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White}
16692f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White
1670f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType,
1671f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                        const SkRect& pathBounds, bool antialias,
16725cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                        SkArenaAlloc& alloc) {
167316a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White    Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal
167416a40cb65adeb4d94b84479f9946c360ed73e1bbStephen White                                                          : Comparator::Direction::kVertical);
1675bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    VertexList mesh;
1676bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White    contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc);
1677497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    sort_and_simplify(&mesh, c, alloc);
1678f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    if (antialias) {
1679f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        VertexList aaMesh;
16805ad721e94682b51b9f773d95704f11990468fef6Stephen White        extract_boundaries(mesh, &aaMesh, fillType, c, alloc);
1681bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        sort_and_simplify(&aaMesh, c, alloc);
1682bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White        return tessellate(aaMesh, alloc);
1683497890630b80a381a2ac4cbb9114b0320560bf8cStephen White    } else {
1684497890630b80a381a2ac4cbb9114b0320560bf8cStephen White        return tessellate(mesh, alloc);
1685f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1686f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}
1687f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1688f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1689f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams,
1690f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                         void* data) {
1691f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    for (Poly* poly = polys; poly; poly = poly->fNext) {
1692f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1693f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            data = poly->emit(aaParams, data);
1694f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        }
1695f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    }
1696f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return data;
1697e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1698e9709e831954c3427d5cb839e84221a177bfedebethannicholas
16999d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
17005cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby                    int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) {
1701e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1702e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (SkPath::IsInverseFillType(fillType)) {
1703e9709e831954c3427d5cb839e84221a177bfedebethannicholas        contourCnt++;
1704e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
17057ecc59610de72043e9b7ebaf1ef45c43425e54fcBen Wagner    std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]);
1706e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1707e9709e831954c3427d5cb839e84221a177bfedebethannicholas    path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
1708f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(),
1709f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                             antialias, alloc);
1710e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1711e9709e831954c3427d5cb839e84221a177bfedebethannicholas
171211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen Whiteint get_contour_count(const SkPath& path, SkScalar tolerance) {
171311f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt;
171411f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
1715e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts <= 0) {
171611f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1717e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1718e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (maxPts > ((int)SK_MaxU16 + 1)) {
1719e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
172011f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White        return 0;
1721e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
172211f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    return contourCnt;
1723e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1724e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1725e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) {
1726e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = 0;
1727e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1728f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly) && poly->fCount >= 3) {
1729e9709e831954c3427d5cb839e84221a177bfedebethannicholas            count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1730e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1731e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1732e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return count;
1733e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1734e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1735e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1736e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1737e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator {
1738e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1739e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer.
1740e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17419d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1742f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    VertexAllocator* vertexAllocator, bool antialias, const GrColor& color,
1743f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                    bool canTweakAlphaForCoverage, bool* isLinear) {
174411f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1745e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1746e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *isLinear = true;
1747e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1748e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
174911f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1750f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias,
1751f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                isLinear);
17527ab96e92196dd74d5b95d33c8477b256813f3046senorblanco    SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType();
1753e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1754e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1755e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1756e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1757e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1758f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* verts = vertexAllocator->lock(count);
17596599efffeef3168dfc68dca99c30454c5c23b859senorblanco    if (!verts) {
1760e9709e831954c3427d5cb839e84221a177bfedebethannicholas        SkDebugf("Could not allocate vertices\n");
1761e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1762e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1763f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1764f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    LOG("emitting %d verts\n", count);
1765f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    AAParams aaParams;
1766f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fTweakAlpha = canTweakAlphaForCoverage;
1767f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    aaParams.fColor = color;
1768f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco
1769f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts);
1770f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts))
1771f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco                                       / vertexAllocator->stride());
1772e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
17736599efffeef3168dfc68dca99c30454c5c23b859senorblanco    vertexAllocator->unlock(actualCount);
1774e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1775e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1776e9709e831954c3427d5cb839e84221a177bfedebethannicholas
17779d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
1778e9709e831954c3427d5cb839e84221a177bfedebethannicholas                   GrTessellator::WindingVertex** verts) {
177911f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    int contourCnt = get_contour_count(path, tolerance);
1780e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (contourCnt <= 0) {
1781e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1782e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
178311f65e0bb44f33b18e8bbb792d4f904ce1da0d5bStephen White    SkArenaAlloc alloc(kArenaChunkSize);
1784e9709e831954c3427d5cb839e84221a177bfedebethannicholas    bool isLinear;
1785f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco    Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear);
1786e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPath::FillType fillType = path.getFillType();
1787e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int count = count_points(polys, fillType);
1788e9709e831954c3427d5cb839e84221a177bfedebethannicholas    if (0 == count) {
1789e9709e831954c3427d5cb839e84221a177bfedebethannicholas        *verts = nullptr;
1790e9709e831954c3427d5cb839e84221a177bfedebethannicholas        return 0;
1791e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1792e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1793e9709e831954c3427d5cb839e84221a177bfedebethannicholas    *verts = new GrTessellator::WindingVertex[count];
1794e9709e831954c3427d5cb839e84221a177bfedebethannicholas    GrTessellator::WindingVertex* vertsEnd = *verts;
1795e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* points = new SkPoint[count];
1796e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkPoint* pointsEnd = points;
1797e9709e831954c3427d5cb839e84221a177bfedebethannicholas    for (Poly* poly = polys; poly; poly = poly->fNext) {
1798f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco        if (apply_fill_type(fillType, poly)) {
1799e9709e831954c3427d5cb839e84221a177bfedebethannicholas            SkPoint* start = pointsEnd;
1800f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco            pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd));
1801e9709e831954c3427d5cb839e84221a177bfedebethannicholas            while (start != pointsEnd) {
1802e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fPos = *start;
1803e9709e831954c3427d5cb839e84221a177bfedebethannicholas                vertsEnd->fWinding = poly->fWinding;
1804e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++start;
1805e9709e831954c3427d5cb839e84221a177bfedebethannicholas                ++vertsEnd;
1806e9709e831954c3427d5cb839e84221a177bfedebethannicholas            }
1807e9709e831954c3427d5cb839e84221a177bfedebethannicholas        }
1808e9709e831954c3427d5cb839e84221a177bfedebethannicholas    }
1809e9709e831954c3427d5cb839e84221a177bfedebethannicholas    int actualCount = static_cast<int>(vertsEnd - *verts);
1810e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(actualCount <= count);
1811e9709e831954c3427d5cb839e84221a177bfedebethannicholas    SkASSERT(pointsEnd - points == actualCount);
1812e9709e831954c3427d5cb839e84221a177bfedebethannicholas    delete[] points;
1813e9709e831954c3427d5cb839e84221a177bfedebethannicholas    return actualCount;
1814e9709e831954c3427d5cb839e84221a177bfedebethannicholas}
1815e9709e831954c3427d5cb839e84221a177bfedebethannicholas
1816e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace
1817