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