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