GrTessellator.cpp revision 5926f2da752d1bff0051fda4137bb967f9e91d5f
1e9709e831954c3427d5cb839e84221a177bfedebethannicholas/* 2e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Copyright 2015 Google Inc. 3e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 4e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Use of this source code is governed by a BSD-style license that can be 5e9709e831954c3427d5cb839e84221a177bfedebethannicholas * found in the LICENSE file. 6e9709e831954c3427d5cb839e84221a177bfedebethannicholas */ 7e9709e831954c3427d5cb839e84221a177bfedebethannicholas 8e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include "GrTessellator.h" 9e9709e831954c3427d5cb839e84221a177bfedebethannicholas 10f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#include "GrDefaultGeoProcFactory.h" 11e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include "GrPathUtils.h" 12e9709e831954c3427d5cb839e84221a177bfedebethannicholas 135cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby#include "SkArenaAlloc.h" 146599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkGeometry.h" 156599efffeef3168dfc68dca99c30454c5c23b859senorblanco#include "SkPath.h" 16e9709e831954c3427d5cb839e84221a177bfedebethannicholas 17e9709e831954c3427d5cb839e84221a177bfedebethannicholas#include <stdio.h> 18e9709e831954c3427d5cb839e84221a177bfedebethannicholas 19e9709e831954c3427d5cb839e84221a177bfedebethannicholas/* 20f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * There are six stages to the basic algorithm: 21e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 22e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 1) Linearize the path contours into piecewise linear segments (path_to_contours()). 23e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 2) Build a mesh of edges connecting the vertices (build_edges()). 24e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). 25e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplify()). 26e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). 27e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_triangles()). 28e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 29f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * For screenspace antialiasing, the algorithm is modified as follows: 30f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 31f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * Run steps 1-5 above to produce polygons. 32f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5b) Apply fill rules to extract boundary contours from the polygons (extract_boundaries()). 33f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5c) Simplify boundaries to remove "pointy" vertices which cause inversions (simplify_boundary()). 34f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 5d) Displace edges by half a pixel inward and outward along their normals. Intersect to find 35f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a new 36f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * antialiased mesh from those vertices (boundary_to_aa_mesh()). 37f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * Run steps 3-6 above on the new mesh, and produce antialiased triangles. 38f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco * 39e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list 40e9709e831954c3427d5cb839e84221a177bfedebethannicholas * of vertices (and the necessity of inserting new vertices on intersection). 41e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 42e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Stages (4) and (5) use an active edge list, which a list of all edges for which the 43e9709e831954c3427d5cb839e84221a177bfedebethannicholas * sweep line has crossed the top vertex, but not the bottom vertex. It's sorted 44e9709e831954c3427d5cb839e84221a177bfedebethannicholas * left-to-right based on the point where both edges are active (when both top vertices 45e9709e831954c3427d5cb839e84221a177bfedebethannicholas * have been seen, so the "lower" top vertex of the two). If the top vertices are equal 46e9709e831954c3427d5cb839e84221a177bfedebethannicholas * (shared), it's sorted based on the last point where both edges are active, so the 47e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "upper" bottom vertex. 48e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 49e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The most complex step is the simplification (4). It's based on the Bentley-Ottman 50e9709e831954c3427d5cb839e84221a177bfedebethannicholas * line-sweep algorithm, but due to floating point inaccuracy, the intersection points are 51e9709e831954c3427d5cb839e84221a177bfedebethannicholas * not exact and may violate the mesh topology or active edge list ordering. We 52e9709e831954c3427d5cb839e84221a177bfedebethannicholas * accommodate this by adjusting the topology of the mesh and AEL to match the intersection 53e9709e831954c3427d5cb839e84221a177bfedebethannicholas * points. This occurs in three ways: 54e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 55e9709e831954c3427d5cb839e84221a177bfedebethannicholas * A) Intersections may cause a shortened edge to no longer be ordered with respect to its 56e9709e831954c3427d5cb839e84221a177bfedebethannicholas * neighbouring edges at the top or bottom vertex. This is handled by merging the 57e9709e831954c3427d5cb839e84221a177bfedebethannicholas * edges (merge_collinear_edges()). 58e9709e831954c3427d5cb839e84221a177bfedebethannicholas * B) Intersections may cause an edge to violate the left-to-right ordering of the 59e9709e831954c3427d5cb839e84221a177bfedebethannicholas * active edge list. This is handled by splitting the neighbour edge on the 60e9709e831954c3427d5cb839e84221a177bfedebethannicholas * intersected vertex (cleanup_active_edges()). 61e9709e831954c3427d5cb839e84221a177bfedebethannicholas * C) Shortening an edge may cause an active edge to become inactive or an inactive edge 62e9709e831954c3427d5cb839e84221a177bfedebethannicholas * to become active. This is handled by removing or inserting the edge in the active 63e9709e831954c3427d5cb839e84221a177bfedebethannicholas * edge list (fix_active_state()). 64e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 65e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The tessellation steps (5) and (6) are based on "Triangulating Simple Polygons and 66e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Equivalent Problems" (Fournier and Montuno); also a line-sweep algorithm. Note that it 67e9709e831954c3427d5cb839e84221a177bfedebethannicholas * currently uses a linked list for the active edge list, rather than a 2-3 tree as the 68e9709e831954c3427d5cb839e84221a177bfedebethannicholas * paper describes. The 2-3 tree gives O(lg N) lookups, but insertion and removal also 69e9709e831954c3427d5cb839e84221a177bfedebethannicholas * become O(lg N). In all the test cases, it was found that the cost of frequent O(lg N) 70e9709e831954c3427d5cb839e84221a177bfedebethannicholas * insertions and removals was greater than the cost of infrequent O(N) lookups with the 71e9709e831954c3427d5cb839e84221a177bfedebethannicholas * linked list implementation. With the latter, all removals are O(1), and most insertions 72e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are O(1), since we know the adjacent edge in the active edge list based on the topology. 73e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Only type 2 vertices (see paper) require the O(N) lookups, and these are much less 74e9709e831954c3427d5cb839e84221a177bfedebethannicholas * frequent. There may be other data structures worth investigating, however. 75e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 76e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that the orientation of the line sweep algorithms is determined by the aspect ratio of the 77e9709e831954c3427d5cb839e84221a177bfedebethannicholas * path bounds. When the path is taller than it is wide, we sort vertices based on increasing Y 78e9709e831954c3427d5cb839e84221a177bfedebethannicholas * coordinate, and secondarily by increasing X coordinate. When the path is wider than it is tall, 79e9709e831954c3427d5cb839e84221a177bfedebethannicholas * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordinate. This is so 80e9709e831954c3427d5cb839e84221a177bfedebethannicholas * that the "left" and "right" orientation in the code remains correct (edges to the left are 81e9709e831954c3427d5cb839e84221a177bfedebethannicholas * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 82e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degrees counterclockwise, rather that transposing. 83e9709e831954c3427d5cb839e84221a177bfedebethannicholas */ 84e9709e831954c3427d5cb839e84221a177bfedebethannicholas 85e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOGGING_ENABLED 0 86e9709e831954c3427d5cb839e84221a177bfedebethannicholas 87e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 88e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG printf 89e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else 90e9709e831954c3427d5cb839e84221a177bfedebethannicholas#define LOG(...) 91e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 92e9709e831954c3427d5cb839e84221a177bfedebethannicholas 93e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace { 94e9709e831954c3427d5cb839e84221a177bfedebethannicholas 95e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex; 96e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge; 97e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly; 98e9709e831954c3427d5cb839e84221a177bfedebethannicholas 99e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next> 100e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_insert(T* t, T* prev, T* next, T** head, T** tail) { 101e9709e831954c3427d5cb839e84221a177bfedebethannicholas t->*Prev = prev; 102e9709e831954c3427d5cb839e84221a177bfedebethannicholas t->*Next = next; 103e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (prev) { 104e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->*Next = t; 105e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (head) { 106e9709e831954c3427d5cb839e84221a177bfedebethannicholas *head = t; 107e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 108e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (next) { 109e9709e831954c3427d5cb839e84221a177bfedebethannicholas next->*Prev = t; 110e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (tail) { 111e9709e831954c3427d5cb839e84221a177bfedebethannicholas *tail = t; 112e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 113e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 114e9709e831954c3427d5cb839e84221a177bfedebethannicholas 115e9709e831954c3427d5cb839e84221a177bfedebethannicholastemplate <class T, T* T::*Prev, T* T::*Next> 116e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancovoid list_remove(T* t, T** head, T** tail) { 117e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (t->*Prev) { 118e9709e831954c3427d5cb839e84221a177bfedebethannicholas t->*Prev->*Next = t->*Next; 119e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (head) { 120e9709e831954c3427d5cb839e84221a177bfedebethannicholas *head = t->*Next; 121e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 122e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (t->*Next) { 123e9709e831954c3427d5cb839e84221a177bfedebethannicholas t->*Next->*Prev = t->*Prev; 124e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (tail) { 125e9709e831954c3427d5cb839e84221a177bfedebethannicholas *tail = t->*Prev; 126e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 127e9709e831954c3427d5cb839e84221a177bfedebethannicholas t->*Prev = t->*Next = nullptr; 128e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 129e9709e831954c3427d5cb839e84221a177bfedebethannicholas 130e9709e831954c3427d5cb839e84221a177bfedebethannicholas/** 131e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices are used in three ways: first, the path contours are converted into a 132e9709e831954c3427d5cb839e84221a177bfedebethannicholas * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices 133e9709e831954c3427d5cb839e84221a177bfedebethannicholas * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing 134e9709e831954c3427d5cb839e84221a177bfedebethannicholas * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid 135e9709e831954c3427d5cb839e84221a177bfedebethannicholas * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of 136e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since 137e9709e831954c3427d5cb839e84221a177bfedebethannicholas * an individual Vertex from the path mesh may belong to multiple 138e9709e831954c3427d5cb839e84221a177bfedebethannicholas * MonotonePolys, so the original Vertices cannot be re-used. 139e9709e831954c3427d5cb839e84221a177bfedebethannicholas */ 140e9709e831954c3427d5cb839e84221a177bfedebethannicholas 141e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Vertex { 142f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex(const SkPoint& point, uint8_t alpha) 143e9709e831954c3427d5cb839e84221a177bfedebethannicholas : fPoint(point), fPrev(nullptr), fNext(nullptr) 144e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr) 145e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr) 146e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fProcessed(false) 147f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco , fAlpha(alpha) 148e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 149e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fID (-1.0f) 150e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 151e9709e831954c3427d5cb839e84221a177bfedebethannicholas {} 152e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint fPoint; // Vertex position 153e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* fPrev; // Linked list of contours, then Y-sorted vertices. 154e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* fNext; // " 155e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. 156e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fLastEdgeAbove; // " 157e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. 158e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fLastEdgeBelow; // " 159e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool fProcessed; // Has this vertex been seen in simplify()? 160f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco uint8_t fAlpha; 161e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 162e9709e831954c3427d5cb839e84221a177bfedebethannicholas float fID; // Identifier used for logging. 163e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 164e9709e831954c3427d5cb839e84221a177bfedebethannicholas}; 165e9709e831954c3427d5cb839e84221a177bfedebethannicholas 166e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/ 167e9709e831954c3427d5cb839e84221a177bfedebethannicholas 168f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct AAParams { 169f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco bool fTweakAlpha; 170f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco GrColor fColor; 171f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}; 172f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 173e9709e831954c3427d5cb839e84221a177bfedebethannicholastypedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); 174e9709e831954c3427d5cb839e84221a177bfedebethannicholas 175e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Comparator { 176e9709e831954c3427d5cb839e84221a177bfedebethannicholas CompareFunc sweep_lt; 177e9709e831954c3427d5cb839e84221a177bfedebethannicholas CompareFunc sweep_gt; 178e9709e831954c3427d5cb839e84221a177bfedebethannicholas}; 179e9709e831954c3427d5cb839e84221a177bfedebethannicholas 180e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 181e9709e831954c3427d5cb839e84221a177bfedebethannicholas return a.fX == b.fX ? a.fY > b.fY : a.fX < b.fX; 182e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 183e9709e831954c3427d5cb839e84221a177bfedebethannicholas 184e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { 185e9709e831954c3427d5cb839e84221a177bfedebethannicholas return a.fY == b.fY ? a.fX < b.fX : a.fY < b.fY; 186e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 187e9709e831954c3427d5cb839e84221a177bfedebethannicholas 188e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_horiz(const SkPoint& a, const SkPoint& b) { 189e9709e831954c3427d5cb839e84221a177bfedebethannicholas return a.fX == b.fX ? a.fY < b.fY : a.fX > b.fX; 190e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 191e9709e831954c3427d5cb839e84221a177bfedebethannicholas 192e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool sweep_gt_vert(const SkPoint& a, const SkPoint& b) { 193e9709e831954c3427d5cb839e84221a177bfedebethannicholas return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; 194e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 195e9709e831954c3427d5cb839e84221a177bfedebethannicholas 196f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void* emit_vertex(Vertex* v, const AAParams* aaParams, void* data) { 197f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (!aaParams) { 198f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkPoint* d = static_cast<SkPoint*>(data); 199f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *d++ = v->fPoint; 200f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return d; 201f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 202f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (aaParams->fTweakAlpha) { 203f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco auto d = static_cast<GrDefaultGeoProcFactory::PositionColorAttr*>(data); 204f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d->fPosition = v->fPoint; 2058c8fceff4d4e8ec09f29748d77ed5510697a2995lsalzman d->fColor = SkAlphaMulQ(aaParams->fColor, SkAlpha255To256(v->fAlpha)); 206f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d++; 207f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return d; 208f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 209f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco auto d = static_cast<GrDefaultGeoProcFactory::PositionColorCoverageAttr*>(data); 210f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d->fPosition = v->fPoint; 211f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d->fColor = aaParams->fColor; 212f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d->fCoverage = GrNormalizeByteToFloat(v->fAlpha); 213f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco d++; 214f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return d; 215e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 216e9709e831954c3427d5cb839e84221a177bfedebethannicholas 217f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, const AAParams* aaParams, void* data) { 21892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White LOG("emit_triangle (%g, %g) %d\n", v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha); 21992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White LOG(" (%g, %g) %d\n", v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha); 22092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White LOG(" (%g, %g) %d\n", v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha); 221f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco#if TESSELLATOR_WIREFRAME 222f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v0, aaParams, data); 223f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v1, aaParams, data); 224f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v1, aaParams, data); 225f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v2, aaParams, data); 226f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v2, aaParams, data); 227f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v0, aaParams, data); 228e9709e831954c3427d5cb839e84221a177bfedebethannicholas#else 229f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v0, aaParams, data); 230f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v1, aaParams, data); 231f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_vertex(v2, aaParams, data); 232e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 233e9709e831954c3427d5cb839e84221a177bfedebethannicholas return data; 234e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 235e9709e831954c3427d5cb839e84221a177bfedebethannicholas 236e6eaa320e8dac34396dc364aa0863574d7b5291csenorblancostruct VertexList { 237e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco VertexList() : fHead(nullptr), fTail(nullptr) {} 238e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco Vertex* fHead; 239e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco Vertex* fTail; 240e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco void insert(Vertex* v, Vertex* prev, Vertex* next) { 241e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); 242e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco } 243e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco void append(Vertex* v) { 244e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco insert(v, fTail, nullptr); 245e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco } 246e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco void prepend(Vertex* v) { 247e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco insert(v, nullptr, fHead); 248e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco } 249bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White void remove(Vertex* v) { 250bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail); 251bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White } 252f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void close() { 253f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (fHead && fTail) { 254f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fTail->fNext = fHead; 255f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fHead->fPrev = fTail; 256f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 257f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 258e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco}; 259e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco 260f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Round to nearest quarter-pixel. This is used for screenspace tessellation. 261f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 262f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline void round(SkPoint* p) { 263f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco p->fX = SkScalarRoundToScalar(p->fX * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 264f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco p->fY = SkScalarRoundToScalar(p->fY * SkFloatToScalar(4.0f)) * SkFloatToScalar(0.25f); 265f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 266f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 26749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco// A line equation in implicit form. fA * x + fB * y + fC = 0, for all points (x, y) on the line. 26849df8d17c56ee08ecf860289d501913d356f67dcsenorblancostruct Line { 26949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line(Vertex* p, Vertex* q) : Line(p->fPoint, q->fPoint) {} 27049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line(const SkPoint& p, const SkPoint& q) 27149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco : fA(static_cast<double>(q.fY) - p.fY) // a = dY 27249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco , fB(static_cast<double>(p.fX) - q.fX) // b = -dX 27349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco , fC(static_cast<double>(p.fY) * q.fX - // c = cross(q, p) 27449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco static_cast<double>(p.fX) * q.fY) {} 27549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double dist(const SkPoint& p) const { 27649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return fA * p.fX + fB * p.fY + fC; 27749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco } 27849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double magSq() const { 27949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return fA * fA + fB * fB; 28049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco } 28149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco 28249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco // Compute the intersection of two (infinite) Lines. 28349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco bool intersect(const Line& other, SkPoint* point) { 28449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double denom = fA * other.fB - fB * other.fA; 28549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco if (denom == 0.0) { 28649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return false; 28749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco } 28849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double scale = 1.0f / denom; 28949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco point->fX = SkDoubleToScalar((fB * other.fC - other.fB * fC) * scale); 29049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco point->fY = SkDoubleToScalar((other.fA * fC - fA * other.fC) * scale); 29149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return true; 29249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco } 29349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double fA, fB, fC; 29449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco}; 29549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco 296e9709e831954c3427d5cb839e84221a177bfedebethannicholas/** 297e9709e831954c3427d5cb839e84221a177bfedebethannicholas * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and 298e9709e831954c3427d5cb839e84221a177bfedebethannicholas * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf(). 299e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Note that an Edge will give occasionally dist() != 0 for its own endpoints (because floating 300e9709e831954c3427d5cb839e84221a177bfedebethannicholas * point). For speed, that case is only tested by the callers which require it (e.g., 301e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cleanup_active_edges()). Edges also handle checking for intersection with other edges. 302e9709e831954c3427d5cb839e84221a177bfedebethannicholas * Currently, this converts the edges to the parametric form, in order to avoid doing a division 303e9709e831954c3427d5cb839e84221a177bfedebethannicholas * until an intersection has been confirmed. This is slightly slower in the "found" case, but 304e9709e831954c3427d5cb839e84221a177bfedebethannicholas * a lot faster in the "not found" case. 305e9709e831954c3427d5cb839e84221a177bfedebethannicholas * 306e9709e831954c3427d5cb839e84221a177bfedebethannicholas * The coefficients of the line equation stored in double precision to avoid catastrphic 307e9709e831954c3427d5cb839e84221a177bfedebethannicholas * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is 308e9709e831954c3427d5cb839e84221a177bfedebethannicholas * correct in float, since it's a polynomial of degree 2. The intersect() function, being 309e9709e831954c3427d5cb839e84221a177bfedebethannicholas * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its 310e9709e831954c3427d5cb839e84221a177bfedebethannicholas * output may be incorrect, and adjusting the mesh topology to match (see comment at the top of 311e9709e831954c3427d5cb839e84221a177bfedebethannicholas * this file). 312e9709e831954c3427d5cb839e84221a177bfedebethannicholas */ 313e9709e831954c3427d5cb839e84221a177bfedebethannicholas 314e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Edge { 3152f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White enum class Type { kInner, kOuter, kConnector }; 3162f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White Edge(Vertex* top, Vertex* bottom, int winding, Type type) 317e9709e831954c3427d5cb839e84221a177bfedebethannicholas : fWinding(winding) 318e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fTop(top) 319e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fBottom(bottom) 3202f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White , fType(type) 321e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fLeft(nullptr) 322e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fRight(nullptr) 323e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fPrevEdgeAbove(nullptr) 324e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fNextEdgeAbove(nullptr) 325e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fPrevEdgeBelow(nullptr) 326e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fNextEdgeBelow(nullptr) 327e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fLeftPoly(nullptr) 328531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fRightPoly(nullptr) 329531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fLeftPolyPrev(nullptr) 330531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fLeftPolyNext(nullptr) 331531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fRightPolyPrev(nullptr) 33270f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco , fRightPolyNext(nullptr) 33370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco , fUsedInLeftPoly(false) 33449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco , fUsedInRightPoly(false) 33549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco , fLine(top, bottom) { 336e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 337e9709e831954c3427d5cb839e84221a177bfedebethannicholas int fWinding; // 1 == edge goes downward; -1 = edge goes upward. 338e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). 339e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* fBottom; // The bottom vertex in vertex-sort-order. 3402f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White Type fType; 341e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fLeft; // The linked list of edges in the active edge list. 342e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fRight; // " 343e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above". 344e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fNextEdgeAbove; // " 345e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below". 346e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* fNextEdgeBelow; // " 347e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* fLeftPoly; // The Poly to the left of this edge, if any. 348e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* fRightPoly; // The Poly to the right of this edge, if any. 349531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fLeftPolyPrev; 350531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fLeftPolyNext; 351531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fRightPolyPrev; 352531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fRightPolyNext; 35370f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco bool fUsedInLeftPoly; 35470f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco bool fUsedInRightPoly; 35549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line fLine; 356e9709e831954c3427d5cb839e84221a177bfedebethannicholas double dist(const SkPoint& p) const { 35749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return fLine.dist(p); 358e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 359e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool isRightOf(Vertex* v) const { 36049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return fLine.dist(v->fPoint) < 0.0; 361e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 362e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool isLeftOf(Vertex* v) const { 36349df8d17c56ee08ecf860289d501913d356f67dcsenorblanco return fLine.dist(v->fPoint) > 0.0; 364e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 365e9709e831954c3427d5cb839e84221a177bfedebethannicholas void recompute() { 36649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco fLine = Line(fTop, fBottom); 367e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 36856158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White bool intersect(const Edge& other, SkPoint* p, uint8_t* alpha = nullptr) { 369e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("intersecting %g -> %g with %g -> %g\n", 370e9709e831954c3427d5cb839e84221a177bfedebethannicholas fTop->fID, fBottom->fID, 371e9709e831954c3427d5cb839e84221a177bfedebethannicholas other.fTop->fID, other.fBottom->fID); 372e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (fTop == other.fTop || fBottom == other.fBottom) { 373e9709e831954c3427d5cb839e84221a177bfedebethannicholas return false; 374e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 37549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double denom = fLine.fA * other.fLine.fB - fLine.fB * other.fLine.fA; 376e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (denom == 0.0) { 377e9709e831954c3427d5cb839e84221a177bfedebethannicholas return false; 378e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 379e9709e831954c3427d5cb839e84221a177bfedebethannicholas double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX; 380e9709e831954c3427d5cb839e84221a177bfedebethannicholas double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY; 38149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA; 38249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double tNumer = -dy * fLine.fB - dx * fLine.fA; 383e9709e831954c3427d5cb839e84221a177bfedebethannicholas // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. 384e9709e831954c3427d5cb839e84221a177bfedebethannicholas // This saves us doing the divide below unless absolutely necessary. 385e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) 386e9709e831954c3427d5cb839e84221a177bfedebethannicholas : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { 387e9709e831954c3427d5cb839e84221a177bfedebethannicholas return false; 388e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 389e9709e831954c3427d5cb839e84221a177bfedebethannicholas double s = sNumer / denom; 390e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(s >= 0.0 && s <= 1.0); 39149df8d17c56ee08ecf860289d501913d356f67dcsenorblanco p->fX = SkDoubleToScalar(fTop->fPoint.fX - s * fLine.fB); 39249df8d17c56ee08ecf860289d501913d356f67dcsenorblanco p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fLine.fA); 39356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White if (alpha) { 39492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White if (fType == Type::kConnector) { 39592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White *alpha = (1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha; 39692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White } else if (other.fType == Type::kConnector) { 39792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White double t = tNumer / denom; 39892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White *alpha = (1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha; 39956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White } else if (fType == Type::kOuter && other.fType == Type::kOuter) { 40056158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White *alpha = 0; 40156158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White } else { 40292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White *alpha = 255; 40356158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White } 40456158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White } 405e9709e831954c3427d5cb839e84221a177bfedebethannicholas return true; 406e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 407f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco}; 408f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 409f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancostruct EdgeList { 410f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {} 411f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* fHead; 412f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* fTail; 413f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco EdgeList* fNext; 414f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco int fCount; 415f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void insert(Edge* edge, Edge* prev, Edge* next) { 416f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); 417f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fCount++; 418f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 419f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void append(Edge* e) { 420f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert(e, fTail, nullptr); 421f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 422f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void remove(Edge* edge) { 423f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); 424f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fCount--; 425f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 426f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void close() { 427f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (fHead && fTail) { 428f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fTail->fRight = fHead; 429f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco fHead->fLeft = fTail; 430f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 431f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 432f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco bool contains(Edge* edge) const { 433f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return edge->fLeft || edge->fRight || fHead == edge; 434e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 435e9709e831954c3427d5cb839e84221a177bfedebethannicholas}; 436e9709e831954c3427d5cb839e84221a177bfedebethannicholas 437e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/ 438e9709e831954c3427d5cb839e84221a177bfedebethannicholas 439e9709e831954c3427d5cb839e84221a177bfedebethannicholasstruct Poly { 440531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Poly(Vertex* v, int winding) 441531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco : fFirstVertex(v) 442531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fWinding(winding) 443e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fHead(nullptr) 444e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fTail(nullptr) 445e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fNext(nullptr) 446e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fPartner(nullptr) 447e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fCount(0) 448e9709e831954c3427d5cb839e84221a177bfedebethannicholas { 449e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 450e9709e831954c3427d5cb839e84221a177bfedebethannicholas static int gID = 0; 451e9709e831954c3427d5cb839e84221a177bfedebethannicholas fID = gID++; 452e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("*** created Poly %d\n", fID); 453e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 454e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 455531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco typedef enum { kLeft_Side, kRight_Side } Side; 456e9709e831954c3427d5cb839e84221a177bfedebethannicholas struct MonotonePoly { 457531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco MonotonePoly(Edge* edge, Side side) 458531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco : fSide(side) 459531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fFirstEdge(nullptr) 460531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fLastEdge(nullptr) 461e9709e831954c3427d5cb839e84221a177bfedebethannicholas , fPrev(nullptr) 462531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco , fNext(nullptr) { 463531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco this->addEdge(edge); 464531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } 465e9709e831954c3427d5cb839e84221a177bfedebethannicholas Side fSide; 466531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fFirstEdge; 467531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* fLastEdge; 468e9709e831954c3427d5cb839e84221a177bfedebethannicholas MonotonePoly* fPrev; 469e9709e831954c3427d5cb839e84221a177bfedebethannicholas MonotonePoly* fNext; 470531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco void addEdge(Edge* edge) { 471e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco if (fSide == kRight_Side) { 472212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco SkASSERT(!edge->fUsedInRightPoly); 473531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( 474531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 47570f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco edge->fUsedInRightPoly = true; 476e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 477212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco SkASSERT(!edge->fUsedInLeftPoly); 478531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( 479531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); 48070f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco edge->fUsedInLeftPoly = true; 481e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 482e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 483e9709e831954c3427d5cb839e84221a177bfedebethannicholas 484f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void* emit(const AAParams* aaParams, void* data) { 485531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Edge* e = fFirstEdge; 486531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco e->fTop->fPrev = e->fTop->fNext = nullptr; 487531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco VertexList vertices; 488531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco vertices.append(e->fTop); 489531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco while (e != nullptr) { 490531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco e->fBottom->fPrev = e->fBottom->fNext = nullptr; 491531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco if (kRight_Side == fSide) { 492531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco vertices.append(e->fBottom); 493531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco e = e->fRightPolyNext; 494531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } else { 495531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco vertices.prepend(e->fBottom); 496531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco e = e->fLeftPolyNext; 497531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } 498531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } 499531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Vertex* first = vertices.fHead; 500e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* v = first->fNext; 501531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco while (v != vertices.fTail) { 502e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(v && v->fPrev && v->fNext); 503e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prev = v->fPrev; 504e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* curr = v; 505e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* next = v->fNext; 506e9709e831954c3427d5cb839e84221a177bfedebethannicholas double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; 507e9709e831954c3427d5cb839e84221a177bfedebethannicholas double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; 508e9709e831954c3427d5cb839e84221a177bfedebethannicholas double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; 509e9709e831954c3427d5cb839e84221a177bfedebethannicholas double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; 510e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (ax * by - ay * bx >= 0.0) { 511f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = emit_triangle(prev, curr, next, aaParams, data); 512e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPrev->fNext = v->fNext; 513e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fNext->fPrev = v->fPrev; 514e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fPrev == first) { 515e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = v->fNext; 516e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 517e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = v->fPrev; 518e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 519e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 520e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = v->fNext; 521e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 522e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 523e9709e831954c3427d5cb839e84221a177bfedebethannicholas return data; 524e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 525e9709e831954c3427d5cb839e84221a177bfedebethannicholas }; 5265cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Poly* addEdge(Edge* e, Side side, SkArenaAlloc& alloc) { 52770f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco LOG("addEdge (%g -> %g) to poly %d, %s side\n", 52870f5251cc59191040a14cd0f1567aa2129e1f7c6senorblanco e->fTop->fID, e->fBottom->fID, fID, side == kLeft_Side ? "left" : "right"); 529e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* partner = fPartner; 530e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* poly = this; 531212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco if (side == kRight_Side) { 532212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco if (e->fUsedInRightPoly) { 533212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco return this; 534212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco } 535212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco } else { 536212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco if (e->fUsedInLeftPoly) { 537212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco return this; 538212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco } 539212c7c389e81053b84a3ed7775f2031d76d5ee70senorblanco } 540e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (partner) { 541e9709e831954c3427d5cb839e84221a177bfedebethannicholas fPartner = partner->fPartner = nullptr; 542e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 543531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco if (!fTail) { 5445cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby fHead = fTail = alloc.make<MonotonePoly>(e, side); 545531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fCount += 2; 54693e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco } else if (e->fBottom == fTail->fLastEdge->fBottom) { 54793e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco return poly; 548531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } else if (side == fTail->fSide) { 549531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fTail->addEdge(e); 550531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fCount++; 551531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } else { 5525cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby e = alloc.make<Edge>(fTail->fLastEdge->fBottom, e->fBottom, 1, Edge::Type::kInner); 553531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fTail->addEdge(e); 554531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fCount++; 555e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (partner) { 556531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco partner->addEdge(e, side, alloc); 557e9709e831954c3427d5cb839e84221a177bfedebethannicholas poly = partner; 558e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 5595cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby MonotonePoly* m = alloc.make<MonotonePoly>(e, side); 560531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco m->fPrev = fTail; 561531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fTail->fNext = m; 562531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco fTail = m; 563e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 564e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 565e9709e831954c3427d5cb839e84221a177bfedebethannicholas return poly; 566e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 567f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void* emit(const AAParams* aaParams, void *data) { 568e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (fCount < 3) { 569e9709e831954c3427d5cb839e84221a177bfedebethannicholas return data; 570e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 571e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("emit() %d, size %d\n", fID, fCount); 572e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { 573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = m->emit(aaParams, data); 574e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 575e9709e831954c3427d5cb839e84221a177bfedebethannicholas return data; 576e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 577531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Vertex* lastVertex() const { return fTail ? fTail->fLastEdge->fBottom : fFirstVertex; } 578531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco Vertex* fFirstVertex; 579e9709e831954c3427d5cb839e84221a177bfedebethannicholas int fWinding; 580e9709e831954c3427d5cb839e84221a177bfedebethannicholas MonotonePoly* fHead; 581e9709e831954c3427d5cb839e84221a177bfedebethannicholas MonotonePoly* fTail; 582e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* fNext; 583e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* fPartner; 584e9709e831954c3427d5cb839e84221a177bfedebethannicholas int fCount; 585e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 586e9709e831954c3427d5cb839e84221a177bfedebethannicholas int fID; 587e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 588e9709e831954c3427d5cb839e84221a177bfedebethannicholas}; 589e9709e831954c3427d5cb839e84221a177bfedebethannicholas 590e9709e831954c3427d5cb839e84221a177bfedebethannicholas/***************************************************************************************/ 591e9709e831954c3427d5cb839e84221a177bfedebethannicholas 592e9709e831954c3427d5cb839e84221a177bfedebethannicholasbool coincident(const SkPoint& a, const SkPoint& b) { 593e9709e831954c3427d5cb839e84221a177bfedebethannicholas return a == b; 594e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 595e9709e831954c3427d5cb839e84221a177bfedebethannicholas 5965cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* new_poly(Poly** head, Vertex* v, int winding, SkArenaAlloc& alloc) { 5975cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Poly* poly = alloc.make<Poly>(v, winding); 598e9709e831954c3427d5cb839e84221a177bfedebethannicholas poly->fNext = *head; 599e9709e831954c3427d5cb839e84221a177bfedebethannicholas *head = poly; 600e9709e831954c3427d5cb839e84221a177bfedebethannicholas return poly; 601e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 602e9709e831954c3427d5cb839e84221a177bfedebethannicholas 6035cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdgeList* new_contour(EdgeList** head, SkArenaAlloc& alloc) { 6045cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby EdgeList* contour = alloc.make<EdgeList>(); 605f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco contour->fNext = *head; 606f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco *head = contour; 607f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return contour; 608f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 609f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 610e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 6115cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 6125cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Vertex* v = alloc.make<Vertex>(p, 255); 613e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 614e9709e831954c3427d5cb839e84221a177bfedebethannicholas static float gID = 0.0f; 615e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fID = gID++; 616e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 617e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (prev) { 618e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = v; 619e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPrev = prev; 620e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 621e9709e831954c3427d5cb839e84221a177bfedebethannicholas *head = v; 622e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 623e9709e831954c3427d5cb839e84221a177bfedebethannicholas return v; 624e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 625e9709e831954c3427d5cb839e84221a177bfedebethannicholas 626e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_quadratic_points(const SkPoint& p0, 627e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint& p1, 628e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint& p2, 629e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar tolSqd, 630e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prev, 631e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex** head, 632e9709e831954c3427d5cb839e84221a177bfedebethannicholas int pointsLeft, 6335cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 634e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2); 635e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) { 636e9709e831954c3427d5cb839e84221a177bfedebethannicholas return append_point_to_contour(p2, prev, head, alloc); 637e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 638e9709e831954c3427d5cb839e84221a177bfedebethannicholas 639e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint q[] = { 640e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 641e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 642e9709e831954c3427d5cb839e84221a177bfedebethannicholas }; 643e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint r = { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }; 644e9709e831954c3427d5cb839e84221a177bfedebethannicholas 645e9709e831954c3427d5cb839e84221a177bfedebethannicholas pointsLeft >>= 1; 646e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_quadratic_points(p0, q[0], r, tolSqd, prev, head, pointsLeft, alloc); 647e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_quadratic_points(r, q[1], p2, tolSqd, prev, head, pointsLeft, alloc); 648e9709e831954c3427d5cb839e84221a177bfedebethannicholas return prev; 649e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 650e9709e831954c3427d5cb839e84221a177bfedebethannicholas 651e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* generate_cubic_points(const SkPoint& p0, 652e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint& p1, 653e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint& p2, 654e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint& p3, 655e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar tolSqd, 656e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prev, 657e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex** head, 658e9709e831954c3427d5cb839e84221a177bfedebethannicholas int pointsLeft, 6595cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 660e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3); 661e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3); 662e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || 663e9709e831954c3427d5cb839e84221a177bfedebethannicholas !SkScalarIsFinite(d1) || !SkScalarIsFinite(d2)) { 664e9709e831954c3427d5cb839e84221a177bfedebethannicholas return append_point_to_contour(p3, prev, head, alloc); 665e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 666e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint q[] = { 667e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(p0.fX, p1.fX), SkScalarAve(p0.fY, p1.fY) }, 668e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(p1.fX, p2.fX), SkScalarAve(p1.fY, p2.fY) }, 669e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(p2.fX, p3.fX), SkScalarAve(p2.fY, p3.fY) } 670e9709e831954c3427d5cb839e84221a177bfedebethannicholas }; 671e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint r[] = { 672e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(q[0].fX, q[1].fX), SkScalarAve(q[0].fY, q[1].fY) }, 673e9709e831954c3427d5cb839e84221a177bfedebethannicholas { SkScalarAve(q[1].fX, q[2].fX), SkScalarAve(q[1].fY, q[2].fY) } 674e9709e831954c3427d5cb839e84221a177bfedebethannicholas }; 675e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint s = { SkScalarAve(r[0].fX, r[1].fX), SkScalarAve(r[0].fY, r[1].fY) }; 676e9709e831954c3427d5cb839e84221a177bfedebethannicholas pointsLeft >>= 1; 677e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLeft, alloc); 678e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLeft, alloc); 679e9709e831954c3427d5cb839e84221a177bfedebethannicholas return prev; 680e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 681e9709e831954c3427d5cb839e84221a177bfedebethannicholas 682e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 1: convert the input path to a set of linear contours (linked list of Vertices). 683e9709e831954c3427d5cb839e84221a177bfedebethannicholas 684e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 6855cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Vertex** contours, SkArenaAlloc& alloc, bool *isLinear) { 686e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar toleranceSqd = tolerance * tolerance; 687e9709e831954c3427d5cb839e84221a177bfedebethannicholas 688e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint pts[4]; 689e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool done = false; 690e9709e831954c3427d5cb839e84221a177bfedebethannicholas *isLinear = true; 691e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPath::Iter iter(path, false); 692e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prev = nullptr; 693e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* head = nullptr; 694e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (path.isInverseFillType()) { 695e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint quad[4]; 696e9709e831954c3427d5cb839e84221a177bfedebethannicholas clipBounds.toQuad(quad); 6977ab96e92196dd74d5b95d33c8477b256813f3046senorblanco for (int i = 3; i >= 0; i--) { 698e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = append_point_to_contour(quad[i], prev, &head, alloc); 699e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 700e9709e831954c3427d5cb839e84221a177bfedebethannicholas head->fPrev = prev; 701e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = head; 702e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contours++ = head; 703e9709e831954c3427d5cb839e84221a177bfedebethannicholas head = prev = nullptr; 704e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 705e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkAutoConicToQuads converter; 706e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (!done) { 707e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPath::Verb verb = iter.next(pts); 708e9709e831954c3427d5cb839e84221a177bfedebethannicholas switch (verb) { 709e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kConic_Verb: { 710e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkScalar weight = iter.conicWeight(); 711e9709e831954c3427d5cb839e84221a177bfedebethannicholas const SkPoint* quadPts = converter.computeQuads(pts, weight, toleranceSqd); 712e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (int i = 0; i < converter.countQuads(); ++i) { 713e9709e831954c3427d5cb839e84221a177bfedebethannicholas int pointsLeft = GrPathUtils::quadraticPointCount(quadPts, tolerance); 714e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_quadratic_points(quadPts[0], quadPts[1], quadPts[2], 715e9709e831954c3427d5cb839e84221a177bfedebethannicholas toleranceSqd, prev, &head, pointsLeft, alloc); 716e9709e831954c3427d5cb839e84221a177bfedebethannicholas quadPts += 2; 717e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 718e9709e831954c3427d5cb839e84221a177bfedebethannicholas *isLinear = false; 719e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 720e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 721e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kMove_Verb: 722e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (head) { 723e9709e831954c3427d5cb839e84221a177bfedebethannicholas head->fPrev = prev; 724e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = head; 725e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contours++ = head; 726e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 727e9709e831954c3427d5cb839e84221a177bfedebethannicholas head = prev = nullptr; 728e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = append_point_to_contour(pts[0], prev, &head, alloc); 729e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 730e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kLine_Verb: { 731e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = append_point_to_contour(pts[1], prev, &head, alloc); 732e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 733e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 734e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kQuad_Verb: { 735e9709e831954c3427d5cb839e84221a177bfedebethannicholas int pointsLeft = GrPathUtils::quadraticPointCount(pts, tolerance); 736e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_quadratic_points(pts[0], pts[1], pts[2], toleranceSqd, prev, 737e9709e831954c3427d5cb839e84221a177bfedebethannicholas &head, pointsLeft, alloc); 738e9709e831954c3427d5cb839e84221a177bfedebethannicholas *isLinear = false; 739e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 740e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 741e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kCubic_Verb: { 742e9709e831954c3427d5cb839e84221a177bfedebethannicholas int pointsLeft = GrPathUtils::cubicPointCount(pts, tolerance); 743e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = generate_cubic_points(pts[0], pts[1], pts[2], pts[3], 744e9709e831954c3427d5cb839e84221a177bfedebethannicholas toleranceSqd, prev, &head, pointsLeft, alloc); 745e9709e831954c3427d5cb839e84221a177bfedebethannicholas *isLinear = false; 746e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 747e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 748e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kClose_Verb: 749e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (head) { 750e9709e831954c3427d5cb839e84221a177bfedebethannicholas head->fPrev = prev; 751e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = head; 752e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contours++ = head; 753e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 754e9709e831954c3427d5cb839e84221a177bfedebethannicholas head = prev = nullptr; 755e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 756e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kDone_Verb: 757e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (head) { 758e9709e831954c3427d5cb839e84221a177bfedebethannicholas head->fPrev = prev; 759e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = head; 760e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contours++ = head; 761e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 762e9709e831954c3427d5cb839e84221a177bfedebethannicholas done = true; 763e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 764e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 765e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 766e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 767e9709e831954c3427d5cb839e84221a177bfedebethannicholas 768f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoinline bool apply_fill_type(SkPath::FillType fillType, Poly* poly) { 769f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (!poly) { 770f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return false; 771f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 772f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco int winding = poly->fWinding; 773e9709e831954c3427d5cb839e84221a177bfedebethannicholas switch (fillType) { 774e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kWinding_FillType: 775e9709e831954c3427d5cb839e84221a177bfedebethannicholas return winding != 0; 776e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kEvenOdd_FillType: 777e9709e831954c3427d5cb839e84221a177bfedebethannicholas return (winding & 1) != 0; 778e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kInverseWinding_FillType: 7797ab96e92196dd74d5b95d33c8477b256813f3046senorblanco return winding == 1; 780e9709e831954c3427d5cb839e84221a177bfedebethannicholas case SkPath::kInverseEvenOdd_FillType: 781e9709e831954c3427d5cb839e84221a177bfedebethannicholas return (winding & 1) == 1; 782e9709e831954c3427d5cb839e84221a177bfedebethannicholas default: 783e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(false); 784e9709e831954c3427d5cb839e84221a177bfedebethannicholas return false; 785e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 786e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 787e9709e831954c3427d5cb839e84221a177bfedebethannicholas 7885cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* new_edge(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc) { 7892f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 790e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* top = winding < 0 ? next : prev; 791e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* bottom = winding < 0 ? prev : next; 7925cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby return alloc.make<Edge>(top, bottom, winding, type); 793e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 794e9709e831954c3427d5cb839e84221a177bfedebethannicholas 795e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge(Edge* edge, EdgeList* edges) { 796e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 797f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkASSERT(edges->contains(edge)); 798f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco edges->remove(edge); 799e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 800e9709e831954c3427d5cb839e84221a177bfedebethannicholas 801e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { 802e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); 803f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkASSERT(!edges->contains(edge)); 804e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next = prev ? prev->fRight : edges->fHead; 805f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco edges->insert(edge, prev, next); 806e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 807e9709e831954c3427d5cb839e84221a177bfedebethannicholas 808e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { 809e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fFirstEdgeAbove) { 810e9709e831954c3427d5cb839e84221a177bfedebethannicholas *left = v->fFirstEdgeAbove->fLeft; 811e9709e831954c3427d5cb839e84221a177bfedebethannicholas *right = v->fLastEdgeAbove->fRight; 812e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 813e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 814e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next = nullptr; 815e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* prev; 816e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (prev = edges->fTail; prev != nullptr; prev = prev->fLeft) { 817e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (prev->isLeftOf(v)) { 818e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 819e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 820e9709e831954c3427d5cb839e84221a177bfedebethannicholas next = prev; 821e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 822e9709e831954c3427d5cb839e84221a177bfedebethannicholas *left = prev; 823e9709e831954c3427d5cb839e84221a177bfedebethannicholas *right = next; 824e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 825e9709e831954c3427d5cb839e84221a177bfedebethannicholas 826e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid find_enclosing_edges(Edge* edge, EdgeList* edges, Comparator& c, Edge** left, Edge** right) { 827e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* prev = nullptr; 828e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next; 829e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (next = edges->fHead; next != nullptr; next = next->fRight) { 830e9709e831954c3427d5cb839e84221a177bfedebethannicholas if ((c.sweep_gt(edge->fTop->fPoint, next->fTop->fPoint) && next->isRightOf(edge->fTop)) || 831e9709e831954c3427d5cb839e84221a177bfedebethannicholas (c.sweep_gt(next->fTop->fPoint, edge->fTop->fPoint) && edge->isLeftOf(next->fTop)) || 832e9709e831954c3427d5cb839e84221a177bfedebethannicholas (c.sweep_lt(edge->fBottom->fPoint, next->fBottom->fPoint) && 833e9709e831954c3427d5cb839e84221a177bfedebethannicholas next->isRightOf(edge->fBottom)) || 834e9709e831954c3427d5cb839e84221a177bfedebethannicholas (c.sweep_lt(next->fBottom->fPoint, edge->fBottom->fPoint) && 835e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->isLeftOf(next->fBottom))) { 836e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 837e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 838e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = next; 839e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 840e9709e831954c3427d5cb839e84221a177bfedebethannicholas *left = prev; 841e9709e831954c3427d5cb839e84221a177bfedebethannicholas *right = next; 842e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 843e9709e831954c3427d5cb839e84221a177bfedebethannicholas 844e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) { 8452f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White if (!activeEdges) { 8462f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White return; 8472f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White } 8482f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White if (activeEdges->contains(edge)) { 849e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fBottom->fProcessed || !edge->fTop->fProcessed) { 850e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge(edge, activeEdges); 851e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 852e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (edge->fTop->fProcessed && !edge->fBottom->fProcessed) { 853e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* left; 854e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* right; 855e9709e831954c3427d5cb839e84221a177bfedebethannicholas find_enclosing_edges(edge, activeEdges, c, &left, &right); 856e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge(edge, left, activeEdges); 857e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 858e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 859e9709e831954c3427d5cb839e84221a177bfedebethannicholas 860e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { 861e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fTop->fPoint == edge->fBottom->fPoint || 862e9709e831954c3427d5cb839e84221a177bfedebethannicholas c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { 863e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 864e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 865e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("insert edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 866e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* prev = nullptr; 867e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next; 868e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { 869e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (next->isRightOf(edge->fTop)) { 870e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 871e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 872e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = next; 873e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 874e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 875e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); 876e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 877e9709e831954c3427d5cb839e84221a177bfedebethannicholas 878e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { 879e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fTop->fPoint == edge->fBottom->fPoint || 880e9709e831954c3427d5cb839e84221a177bfedebethannicholas c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { 881e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 882e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 883e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("insert edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, v->fID); 884e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* prev = nullptr; 885e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next; 886e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { 887e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (next->isRightOf(edge->fBottom)) { 888e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 889e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 890e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = next; 891e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 892e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 893e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); 894e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 895e9709e831954c3427d5cb839e84221a177bfedebethannicholas 896e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_above(Edge* edge) { 897e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 898e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fBottom->fID); 899e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( 900e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); 901e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 902e9709e831954c3427d5cb839e84221a177bfedebethannicholas 903e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid remove_edge_below(Edge* edge) { 904e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("removing edge (%g -> %g) below vertex %g\n", edge->fTop->fID, edge->fBottom->fID, 905e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fTop->fID); 906e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( 907e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); 908e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 909e9709e831954c3427d5cb839e84221a177bfedebethannicholas 910e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid disconnect(Edge* edge) 911e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White{ 912e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge_above(edge); 913e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge_below(edge); 914e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White} 915e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White 916e7a364d435843a868dcac2c61d78c34e5d3e326cStephen Whitevoid erase_edge(Edge* edge, EdgeList* edges) { 917e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White LOG("erasing edge (%g -> %g)\n", edge->fTop->fID, edge->fBottom->fID); 918e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White disconnect(edge); 919f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (edges && edges->contains(edge)) { 920e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge(edge, edges); 921e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 922e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 923e9709e831954c3427d5cb839e84221a177bfedebethannicholas 924e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c); 925e9709e831954c3427d5cb839e84221a177bfedebethannicholas 926e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 927e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge_below(edge); 928e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fTop = v; 929e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->recompute(); 930e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge_below(edge, v, c); 931e9709e831954c3427d5cb839e84221a177bfedebethannicholas fix_active_state(edge, activeEdges, c); 932e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_collinear_edges(edge, activeEdges, c); 933e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 934e9709e831954c3427d5cb839e84221a177bfedebethannicholas 935e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { 936e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge_above(edge); 937e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fBottom = v; 938e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->recompute(); 939e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge_above(edge, v, c); 940e9709e831954c3427d5cb839e84221a177bfedebethannicholas fix_active_state(edge, activeEdges, c); 941e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_collinear_edges(edge, activeEdges, c); 942e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 943e9709e831954c3427d5cb839e84221a177bfedebethannicholas 944e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_above(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 945e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { 946e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", 947e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 948e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 949e9709e831954c3427d5cb839e84221a177bfedebethannicholas other->fWinding += edge->fWinding; 950e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White erase_edge(edge, activeEdges); 951e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { 952e9709e831954c3427d5cb839e84221a177bfedebethannicholas other->fWinding += edge->fWinding; 953e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_bottom(edge, other->fTop, activeEdges, c); 954e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 955e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fWinding += other->fWinding; 956e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_bottom(other, edge->fTop, activeEdges, c); 957e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 958e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 959e9709e831954c3427d5cb839e84221a177bfedebethannicholas 960e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_edges_below(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c) { 961e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { 962e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", 963e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, 964e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); 965e9709e831954c3427d5cb839e84221a177bfedebethannicholas other->fWinding += edge->fWinding; 966e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White erase_edge(edge, activeEdges); 967e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { 968e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fWinding += other->fWinding; 969e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_top(other, edge->fBottom, activeEdges, c); 970e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 971e9709e831954c3427d5cb839e84221a177bfedebethannicholas other->fWinding += edge->fWinding; 972e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_top(edge, other->fBottom, activeEdges, c); 973e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 974e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 975e9709e831954c3427d5cb839e84221a177bfedebethannicholas 976e9709e831954c3427d5cb839e84221a177bfedebethannicholasvoid merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) { 977e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fPrevEdgeAbove && (edge->fTop == edge->fPrevEdgeAbove->fTop || 978e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->fPrevEdgeAbove->isLeftOf(edge->fTop))) { 979e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_edges_above(edge, edge->fPrevEdgeAbove, activeEdges, c); 980e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (edge->fNextEdgeAbove && (edge->fTop == edge->fNextEdgeAbove->fTop || 981e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->isLeftOf(edge->fNextEdgeAbove->fTop))) { 982e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_edges_above(edge, edge->fNextEdgeAbove, activeEdges, c); 983e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 984e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || 985e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom))) { 986e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c); 987e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->fBottom || 988e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->isLeftOf(edge->fNextEdgeBelow->fBottom))) { 989e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c); 990e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 991e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 992e9709e831954c3427d5cb839e84221a177bfedebethannicholas 9935cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc); 994e9709e831954c3427d5cb839e84221a177bfedebethannicholas 9955cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) { 996e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* top = edge->fTop; 997e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* bottom = edge->fBottom; 998e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fLeft) { 999e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* leftTop = edge->fLeft->fTop; 1000e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* leftBottom = edge->fLeft->fBottom; 1001e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) { 1002e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); 1003e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) { 1004e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, leftTop, activeEdges, c, alloc); 1005e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && 1006e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->fLeft->isLeftOf(bottom)) { 1007e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge->fLeft, bottom, activeEdges, c, alloc); 1008e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && !edge->isRightOf(leftBottom)) { 1009e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, leftBottom, activeEdges, c, alloc); 1010e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1011e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1012e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (edge->fRight) { 1013e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* rightTop = edge->fRight->fTop; 1014e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* rightBottom = edge->fRight->fBottom; 1015e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) { 1016e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge->fRight, top, activeEdges, c, alloc); 1017e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) { 1018e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, rightTop, activeEdges, c, alloc); 1019e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && 1020e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->fRight->isRightOf(bottom)) { 1021e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge->fRight, bottom, activeEdges, c, alloc); 1022e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && 1023e9709e831954c3427d5cb839e84221a177bfedebethannicholas !edge->isLeftOf(rightBottom)) { 1024e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, rightBottom, activeEdges, c, alloc); 1025e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1026e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1027e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1028e9709e831954c3427d5cb839e84221a177bfedebethannicholas 10295cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkArenaAlloc& alloc) { 1030e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", 1031e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge->fTop->fID, edge->fBottom->fID, 1032e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fID, v->fPoint.fX, v->fPoint.fY); 1033e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { 1034e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_top(edge, v, activeEdges, c); 1035e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (c.sweep_gt(v->fPoint, edge->fBottom->fPoint)) { 1036e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_bottom(edge, v, activeEdges, c); 1037e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 10385cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Edge* newEdge = alloc.make<Edge>(v, edge->fBottom, edge->fWinding, edge->fType); 1039e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge_below(newEdge, v, c); 1040e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge_above(newEdge, edge->fBottom, c); 1041e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_bottom(edge, v, activeEdges, c); 1042e9709e831954c3427d5cb839e84221a177bfedebethannicholas cleanup_active_edges(edge, activeEdges, c, alloc); 1043e9709e831954c3427d5cb839e84221a177bfedebethannicholas fix_active_state(newEdge, activeEdges, c); 1044e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_collinear_edges(newEdge, activeEdges, c); 1045e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1046e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1047e9709e831954c3427d5cb839e84221a177bfedebethannicholas 10485cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyEdge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc, 104948ded38da99c1171ba1bb469f6500f8214e9105cStephen White int winding_scale = 1) { 1050bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White Edge* edge = new_edge(prev, next, type, c, alloc); 1051f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (edge->fWinding > 0) { 1052f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert_edge_below(edge, prev, c); 1053f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert_edge_above(edge, next, c); 1054f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else { 1055f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert_edge_below(edge, next, c); 1056f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert_edge_above(edge, prev, c); 1057f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 105848ded38da99c1171ba1bb469f6500f8214e9105cStephen White edge->fWinding *= winding_scale; 1059f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco merge_collinear_edges(edge, nullptr, c); 1060f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return edge; 1061f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1062f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1063bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid merge_vertices(Vertex* src, Vertex* dst, VertexList* mesh, Comparator& c, 10645cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1065e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, 1066e9709e831954c3427d5cb839e84221a177bfedebethannicholas src->fID, dst->fID); 1067f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco dst->fAlpha = SkTMax(src->fAlpha, dst->fAlpha); 1068e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* edge = src->fFirstEdgeAbove; edge;) { 1069e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next = edge->fNextEdgeAbove; 1070e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_bottom(edge, dst, nullptr, c); 1071e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge = next; 1072e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1073e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* edge = src->fFirstEdgeBelow; edge;) { 1074e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* next = edge->fNextEdgeBelow; 1075e9709e831954c3427d5cb839e84221a177bfedebethannicholas set_top(edge, dst, nullptr, c); 1076e9709e831954c3427d5cb839e84221a177bfedebethannicholas edge = next; 1077e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1078bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White mesh->remove(src); 1079e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1080e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1081f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancouint8_t max_edge_alpha(Edge* a, Edge* b) { 108256158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White if (a->fType == Edge::Type::kInner || b->fType == Edge::Type::kInner) { 10832f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White return 255; 10842f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White } else if (a->fType == Edge::Type::kOuter && b->fType == Edge::Type::kOuter) { 10852f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White return 0; 10862f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White } else { 10872f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White return SkTMax(SkTMax(a->fTop->fAlpha, a->fBottom->fAlpha), 10882f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White SkTMax(b->fTop->fAlpha, b->fBottom->fAlpha)); 10892f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White } 1090f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1091f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1092e9709e831954c3427d5cb839e84221a177bfedebethannicholasVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, 10935cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1094e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (!edge || !other) { 1095e9709e831954c3427d5cb839e84221a177bfedebethannicholas return nullptr; 1096e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 109756158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White SkPoint p; 109856158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White uint8_t alpha; 109956158ae600a73ac2e74f5a5862de0fab466b52b6Stephen White if (edge->intersect(*other, &p, &alpha)) { 1100e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* v; 1101e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 1102e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { 1103e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(other, edge->fTop, activeEdges, c, alloc); 1104e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = edge->fTop; 1105e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (p == edge->fBottom->fPoint || c.sweep_gt(p, edge->fBottom->fPoint)) { 1106e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(other, edge->fBottom, activeEdges, c, alloc); 1107e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = edge->fBottom; 1108e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (p == other->fTop->fPoint || c.sweep_lt(p, other->fTop->fPoint)) { 1109e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, other->fTop, activeEdges, c, alloc); 1110e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = other->fTop; 1111e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (p == other->fBottom->fPoint || c.sweep_gt(p, other->fBottom->fPoint)) { 1112e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, other->fBottom, activeEdges, c, alloc); 1113e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = other->fBottom; 1114e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1115e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* nextV = edge->fTop; 1116e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (c.sweep_lt(p, nextV->fPoint)) { 1117e9709e831954c3427d5cb839e84221a177bfedebethannicholas nextV = nextV->fPrev; 1118e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1119e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (c.sweep_lt(nextV->fPoint, p)) { 1120e9709e831954c3427d5cb839e84221a177bfedebethannicholas nextV = nextV->fNext; 1121e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1122e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prevV = nextV->fPrev; 1123e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (coincident(prevV->fPoint, p)) { 1124e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = prevV; 1125e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else if (coincident(nextV->fPoint, p)) { 1126e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = nextV; 1127e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 11285cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby v = alloc.make<Vertex>(p, alpha); 1129e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", 1130e9709e831954c3427d5cb839e84221a177bfedebethannicholas prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, 1131e9709e831954c3427d5cb839e84221a177bfedebethannicholas nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); 1132e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1133e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fID = (nextV->fID + prevV->fID) * 0.5f; 1134e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1135e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPrev = prevV; 1136e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fNext = nextV; 1137e9709e831954c3427d5cb839e84221a177bfedebethannicholas prevV->fNext = v; 1138e9709e831954c3427d5cb839e84221a177bfedebethannicholas nextV->fPrev = v; 1139e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1140e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(edge, v, activeEdges, c, alloc); 1141e9709e831954c3427d5cb839e84221a177bfedebethannicholas split_edge(other, v, activeEdges, c, alloc); 1142e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 114392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White v->fAlpha = SkTMax(v->fAlpha, alpha); 1144e9709e831954c3427d5cb839e84221a177bfedebethannicholas return v; 1145e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1146e9709e831954c3427d5cb839e84221a177bfedebethannicholas return nullptr; 1147e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1148e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1149f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid sanitize_contours(Vertex** contours, int contourCnt, bool approximate) { 1150e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (int i = 0; i < contourCnt; ++i) { 1151e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(contours[i]); 11525926f2da752d1bff0051fda4137bb967f9e91d5fStephen White if (approximate) { 11535926f2da752d1bff0051fda4137bb967f9e91d5fStephen White round(&contours[i]->fPrev->fPoint); 11545926f2da752d1bff0051fda4137bb967f9e91d5fStephen White } 1155e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Vertex* v = contours[i];;) { 1156f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (approximate) { 1157f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco round(&v->fPoint); 1158f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1159e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (coincident(v->fPrev->fPoint, v->fPoint)) { 1160e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); 1161e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fPrev == v) { 1162e9709e831954c3427d5cb839e84221a177bfedebethannicholas contours[i] = nullptr; 1163e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 1164e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1165e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPrev->fNext = v->fNext; 1166e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fNext->fPrev = v->fPrev; 1167e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (contours[i] == v) { 11685926f2da752d1bff0051fda4137bb967f9e91d5fStephen White contours[i] = v->fPrev; 1169e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1170e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = v->fPrev; 1171e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1172e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = v->fNext; 1173e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v == contours[i]) break; 1174e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1175e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1176e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1177e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1178e9709e831954c3427d5cb839e84221a177bfedebethannicholas 11795cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid merge_coincident_vertices(VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1180bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Vertex* v = mesh->fHead->fNext; v != nullptr; v = v->fNext) { 1181e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { 1182e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPoint = v->fPrev->fPoint; 1183e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1184e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (coincident(v->fPrev->fPoint, v->fPoint)) { 1185bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White merge_vertices(v->fPrev, v, mesh, c, alloc); 1186e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1187e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1188e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1189e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1190e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 2: convert the contours to a mesh of edges connecting the vertices. 1191e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1192bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid build_edges(Vertex** contours, int contourCnt, VertexList* mesh, Comparator& c, 11935cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1194e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* prev = nullptr; 1195e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (int i = 0; i < contourCnt; ++i) { 1196e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Vertex* v = contours[i]; v != nullptr;) { 1197e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* vNext = v->fNext; 1198bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White connect(v->fPrev, v, Edge::Type::kInner, c, alloc); 1199e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (prev) { 1200e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev->fNext = v; 1201e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fPrev = prev; 1202e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1203bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White mesh->fHead = v; 1204e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1205e9709e831954c3427d5cb839e84221a177bfedebethannicholas prev = v; 1206e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = vNext; 1207e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v == contours[i]) break; 1208e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1209e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1210e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (prev) { 1211bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White prev->fNext = mesh->fHead->fPrev = nullptr; 1212e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1213bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White mesh->fTail = prev; 1214e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1215e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1216e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 3: sort the vertices by increasing sweep direction. 1217e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1218bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c); 1219e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1220bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid front_back_split(VertexList* v, VertexList* front, VertexList* back) { 1221e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* fast; 1222e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* slow; 1223bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White if (!v->fHead || !v->fHead->fNext) { 1224bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White *front = *v; 1225e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1226bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White slow = v->fHead; 1227bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White fast = v->fHead->fNext; 1228e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1229e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (fast != nullptr) { 1230e9709e831954c3427d5cb839e84221a177bfedebethannicholas fast = fast->fNext; 1231e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (fast != nullptr) { 1232e9709e831954c3427d5cb839e84221a177bfedebethannicholas slow = slow->fNext; 1233e9709e831954c3427d5cb839e84221a177bfedebethannicholas fast = fast->fNext; 1234e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1235e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1236bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White front->fHead = v->fHead; 1237bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White front->fTail = slow; 1238bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White back->fHead = slow->fNext; 1239bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White back->fTail = v->fTail; 1240e9709e831954c3427d5cb839e84221a177bfedebethannicholas slow->fNext->fPrev = nullptr; 1241e9709e831954c3427d5cb839e84221a177bfedebethannicholas slow->fNext = nullptr; 1242e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1243bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White v->fHead = v->fTail = nullptr; 1244e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1245e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1246bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid merge_sort(VertexList* mesh, Comparator& c) { 1247bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White if (!mesh->fHead || !mesh->fHead->fNext) { 1248e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 1249e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1250e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1251bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White VertexList a; 1252bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White VertexList b; 1253bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White front_back_split(mesh, &a, &b); 1254e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1255e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_sort(&a, c); 1256e9709e831954c3427d5cb839e84221a177bfedebethannicholas merge_sort(&b, c); 1257e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1258bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White sorted_merge(a.fHead, b.fHead, mesh, c); 1259e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1260e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1261bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid sorted_merge(Vertex* a, Vertex* b, VertexList* result, Comparator& c) { 1262e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco VertexList vertices; 1263e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (a && b) { 1264e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_lt(a->fPoint, b->fPoint)) { 1265e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* next = a->fNext; 1266e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco vertices.append(a); 1267e9709e831954c3427d5cb839e84221a177bfedebethannicholas a = next; 1268e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1269e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* next = b->fNext; 1270e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco vertices.append(b); 1271e9709e831954c3427d5cb839e84221a177bfedebethannicholas b = next; 1272e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1273e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1274e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (a) { 1275e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco vertices.insert(a, vertices.fTail, a->fNext); 1276e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1277e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (b) { 1278e6eaa320e8dac34396dc364aa0863574d7b5291csenorblanco vertices.insert(b, vertices.fTail, b->fNext); 1279e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1280bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White *result = vertices; 1281e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1282e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1283e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. 1284e9709e831954c3427d5cb839e84221a177bfedebethannicholas 12855cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) { 1286e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("simplifying complex polygons\n"); 1287e9709e831954c3427d5cb839e84221a177bfedebethannicholas EdgeList activeEdges; 1288bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1289e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1290e9709e831954c3427d5cb839e84221a177bfedebethannicholas continue; 1291e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1292e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1293f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1294e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1295e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* leftEnclosingEdge = nullptr; 1296e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* rightEnclosingEdge = nullptr; 1297e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool restartChecks; 1298e9709e831954c3427d5cb839e84221a177bfedebethannicholas do { 1299e9709e831954c3427d5cb839e84221a177bfedebethannicholas restartChecks = false; 1300e9709e831954c3427d5cb839e84221a177bfedebethannicholas find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1301e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fFirstEdgeBelow) { 1302bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { 1303e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (check_for_intersection(edge, leftEnclosingEdge, &activeEdges, c, alloc)) { 1304e9709e831954c3427d5cb839e84221a177bfedebethannicholas restartChecks = true; 1305e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 1306e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1307e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (check_for_intersection(edge, rightEnclosingEdge, &activeEdges, c, alloc)) { 1308e9709e831954c3427d5cb839e84221a177bfedebethannicholas restartChecks = true; 1309e9709e831954c3427d5cb839e84221a177bfedebethannicholas break; 1310e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1311e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1312e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1313e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, 1314e9709e831954c3427d5cb839e84221a177bfedebethannicholas &activeEdges, c, alloc)) { 1315e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1316e9709e831954c3427d5cb839e84221a177bfedebethannicholas v = pv; 1317e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1318e9709e831954c3427d5cb839e84221a177bfedebethannicholas restartChecks = true; 1319e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1320e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1321e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1322e9709e831954c3427d5cb839e84221a177bfedebethannicholas } while (restartChecks); 1323f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (v->fAlpha == 0) { 132448ded38da99c1171ba1bb469f6500f8214e9105cStephen White if ((leftEnclosingEdge && leftEnclosingEdge->fWinding < 0) && 132548ded38da99c1171ba1bb469f6500f8214e9105cStephen White (rightEnclosingEdge && rightEnclosingEdge->fWinding > 0)) { 1326f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco v->fAlpha = max_edge_alpha(leftEnclosingEdge, rightEnclosingEdge); 1327f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1328f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1329e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1330e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge(e, &activeEdges); 1331e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1332e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* leftEdge = leftEnclosingEdge; 1333e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1334e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge(e, leftEdge, &activeEdges); 1335e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftEdge = e; 1336e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1337e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fProcessed = true; 1338e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1339e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1340e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1341e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 5: Tessellate the simplified mesh into monotone polygons. 1342e9709e831954c3427d5cb839e84221a177bfedebethannicholas 13435cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) { 1344e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("tessellating simple polygons\n"); 1345e9709e831954c3427d5cb839e84221a177bfedebethannicholas EdgeList activeEdges; 1346e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* polys = nullptr; 1347bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { 1348e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { 1349e9709e831954c3427d5cb839e84221a177bfedebethannicholas continue; 1350e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1351e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1352f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); 1353e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1354e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* leftEnclosingEdge = nullptr; 1355e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* rightEnclosingEdge = nullptr; 1356e9709e831954c3427d5cb839e84221a177bfedebethannicholas find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); 1357e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* leftPoly = nullptr; 1358e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* rightPoly = nullptr; 1359e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fFirstEdgeAbove) { 1360e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftPoly = v->fFirstEdgeAbove->fLeftPoly; 1361e9709e831954c3427d5cb839e84221a177bfedebethannicholas rightPoly = v->fLastEdgeAbove->fRightPoly; 1362e9709e831954c3427d5cb839e84221a177bfedebethannicholas } else { 1363e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; 1364e9709e831954c3427d5cb839e84221a177bfedebethannicholas rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; 1365e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1366e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1367e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("edges above:\n"); 1368e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1369e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1370e9709e831954c3427d5cb839e84221a177bfedebethannicholas e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1371e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1372e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("edges below:\n"); 1373e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { 1374e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1375e9709e831954c3427d5cb839e84221a177bfedebethannicholas e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1376e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1377e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1378e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fFirstEdgeAbove) { 1379e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (leftPoly) { 1380531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Poly::kRight_Side, alloc); 1381e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1382e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (rightPoly) { 1383531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc); 1384e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1385e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { 1386e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* leftEdge = e; 1387e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* rightEdge = e->fNextEdgeAbove; 1388e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(rightEdge->isRightOf(leftEdge->fTop)); 1389e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge(leftEdge, &activeEdges); 1390e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (leftEdge->fRightPoly) { 1391531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc); 1392e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1393531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco if (rightEdge->fLeftPoly) { 1394531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc); 1395e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1396e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1397e9709e831954c3427d5cb839e84221a177bfedebethannicholas remove_edge(v->fLastEdgeAbove, &activeEdges); 1398e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (!v->fFirstEdgeBelow) { 1399e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (leftPoly && rightPoly && leftPoly != rightPoly) { 1400e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); 1401e9709e831954c3427d5cb839e84221a177bfedebethannicholas rightPoly->fPartner = leftPoly; 1402e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftPoly->fPartner = rightPoly; 1403e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1404e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1405e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1406e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (v->fFirstEdgeBelow) { 1407e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (!v->fFirstEdgeAbove) { 140893e3fff79eaaa86bc2fb740a42111a074ccc73absenorblanco if (leftPoly && rightPoly) { 1409531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco if (leftPoly == rightPoly) { 1410531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco if (leftPoly->fTail && leftPoly->fTail->fSide == Poly::kLeft_Side) { 1411531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftPoly = new_poly(&polys, leftPoly->lastVertex(), 1412531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftPoly->fWinding, alloc); 1413531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftEnclosingEdge->fRightPoly = leftPoly; 1414531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } else { 1415531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightPoly = new_poly(&polys, rightPoly->lastVertex(), 1416531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightPoly->fWinding, alloc); 1417531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightEnclosingEdge->fLeftPoly = rightPoly; 1418531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco } 1419e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 14205cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Edge* join = alloc.make<Edge>(leftPoly->lastVertex(), v, 1, Edge::Type::kInner); 1421531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco leftPoly = leftPoly->addEdge(join, Poly::kRight_Side, alloc); 1422531237ef3aaf0d3c86e0853fde3b4c8f517bc662senorblanco rightPoly = rightPoly->addEdge(join, Poly::kLeft_Side, alloc); 1423e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1424e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1425e9709e831954c3427d5cb839e84221a177bfedebethannicholas Edge* leftEdge = v->fFirstEdgeBelow; 1426e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftEdge->fLeftPoly = leftPoly; 1427e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge(leftEdge, leftEnclosingEdge, &activeEdges); 1428e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; 1429e9709e831954c3427d5cb839e84221a177bfedebethannicholas rightEdge = rightEdge->fNextEdgeBelow) { 1430e9709e831954c3427d5cb839e84221a177bfedebethannicholas insert_edge(rightEdge, leftEdge, &activeEdges); 1431e9709e831954c3427d5cb839e84221a177bfedebethannicholas int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; 1432e9709e831954c3427d5cb839e84221a177bfedebethannicholas winding += leftEdge->fWinding; 1433e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (winding != 0) { 1434e9709e831954c3427d5cb839e84221a177bfedebethannicholas Poly* poly = new_poly(&polys, v, winding, alloc); 1435e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; 1436e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1437e9709e831954c3427d5cb839e84221a177bfedebethannicholas leftEdge = rightEdge; 1438e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1439e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fLastEdgeBelow->fRightPoly = rightPoly; 1440e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1441e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1442e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("\nactive edges:\n"); 1443e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { 1444e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1445e9709e831954c3427d5cb839e84221a177bfedebethannicholas e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRightPoly->fID : -1); 1446e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1447e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1448e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1449e9709e831954c3427d5cb839e84221a177bfedebethannicholas return polys; 1450e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1451e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1452f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancobool is_boundary_edge(Edge* edge, SkPath::FillType fillType) { 1453f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return apply_fill_type(fillType, edge->fLeftPoly) != 1454f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco apply_fill_type(fillType, edge->fRightPoly); 1455f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 14569992bdef8ae97b3e5b109d278ccfab84c66bcbf0senorblanco 1457f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancobool is_boundary_start(Edge* edge, SkPath::FillType fillType) { 1458f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return !apply_fill_type(fillType, edge->fLeftPoly) && 1459f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco apply_fill_type(fillType, edge->fRightPoly); 1460f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1461f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1462bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid remove_non_boundary_edges(const VertexList& mesh, SkPath::FillType fillType, 14635cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1464bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { 1465f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco for (Edge* e = v->fFirstEdgeBelow; e != nullptr;) { 1466f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* next = e->fNextEdgeBelow; 1467f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (!is_boundary_edge(e, fillType)) { 1468e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White disconnect(e); 1469f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1470f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e = next; 1471f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1472682580fb204b72925a48d1d6fe8c9c30fa53bb67senorblanco } 1473f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1474f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1475f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid get_edge_normal(const Edge* e, SkVector* normal) { 1476eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White normal->setNormalize(SkDoubleToScalar(e->fLine.fA) * e->fWinding, 1477eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White SkDoubleToScalar(e->fLine.fB) * e->fWinding); 1478f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1479f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1480f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5c: detect and remove "pointy" vertices whose edge normals point in opposite directions 1481f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// and whose adjacent vertices are less than a quarter pixel from an edge. These are guaranteed to 1482f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// invert on stroking. 1483f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 14845cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid simplify_boundary(EdgeList* boundary, Comparator& c, SkArenaAlloc& alloc) { 1485f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* prevEdge = boundary->fTail; 1486f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkVector prevNormal; 1487f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco get_edge_normal(prevEdge, &prevNormal); 1488f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco for (Edge* e = boundary->fHead; e != nullptr;) { 1489f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* prev = prevEdge->fWinding == 1 ? prevEdge->fTop : prevEdge->fBottom; 1490f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* next = e->fWinding == 1 ? e->fBottom : e->fTop; 1491f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco double dist = e->dist(prev->fPoint); 1492f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkVector normal; 1493f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco get_edge_normal(e, &normal); 1494eaf0079d81beac9fea2da7a20c3587ebbbbe6463Stephen White float denom = 0.0625f * static_cast<float>(e->fLine.magSq()); 1495f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (prevNormal.dot(normal) < 0.0 && (dist * dist) <= denom) { 1496bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White Edge* join = new_edge(prev, next, Edge::Type::kInner, c, alloc); 1497f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco insert_edge(join, e, boundary); 1498f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco remove_edge(prevEdge, boundary); 1499f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco remove_edge(e, boundary); 1500f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (join->fLeft && join->fRight) { 1501f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevEdge = join->fLeft; 1502f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e = join; 1503f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else { 1504f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevEdge = boundary->fTail; 1505f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e = boundary->fHead; // join->fLeft ? join->fLeft : join; 1506f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1507f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco get_edge_normal(prevEdge, &prevNormal); 1508f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else { 1509f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevEdge = e; 1510f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevNormal = normal; 1511f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e = e->fRight; 1512f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1513f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1514f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1515f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 151692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen Whitevoid fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBisector, 151792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White Edge* prevEdge, Comparator& c) { 151892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White if (!prev || !next) { 151992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White return; 152092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White } 152192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; 152292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White SkPoint p; 152392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White uint8_t alpha; 152492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White if (winding != prevEdge->fWinding && prevBisector->intersect(*nextBisector, &p, &alpha)) { 152592eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White prev->fPoint = next->fPoint = p; 152692eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White prev->fAlpha = next->fAlpha = alpha; 152792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White } 152892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White} 152992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White 1530f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5d: Displace edges by half a pixel inward and outward along their normals. Intersect to 1531f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// find new vertices, and set zero alpha on the exterior and one alpha on the interior. Build a 1532f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// new antialiased mesh from those vertices. 1533f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 15345cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1535f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* prevEdge = boundary->fTail; 1536f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco float radius = 0.5f; 153749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding; 153849df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line prevInner(prevEdge->fTop, prevEdge->fBottom); 1539f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevInner.fC -= offset; 154049df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line prevOuter(prevEdge->fTop, prevEdge->fBottom); 1541f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevOuter.fC += offset; 1542f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco VertexList innerVertices; 1543f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco VertexList outerVertices; 154492eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White Edge* prevBisector = nullptr; 1545f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco for (Edge* e = boundary->fHead; e != nullptr; e = e->fRight) { 154649df8d17c56ee08ecf860289d501913d356f67dcsenorblanco double offset = radius * sqrt(e->fLine.magSq()) * e->fWinding; 154749df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line inner(e->fTop, e->fBottom); 1548f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco inner.fC -= offset; 154949df8d17c56ee08ecf860289d501913d356f67dcsenorblanco Line outer(e->fTop, e->fBottom); 1550f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco outer.fC += offset; 1551f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco SkPoint innerPoint, outerPoint; 155286cc841e23a81af69269de87224c57be274f40d1Stephen White SkVector normal; 155386cc841e23a81af69269de87224c57be274f40d1Stephen White get_edge_normal(e, &normal); 155449df8d17c56ee08ecf860289d501913d356f67dcsenorblanco if (prevInner.intersect(inner, &innerPoint) && 155549df8d17c56ee08ecf860289d501913d356f67dcsenorblanco prevOuter.intersect(outer, &outerPoint)) { 15565cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Vertex* innerVertex = alloc.make<Vertex>(innerPoint, 255); 15575cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby Vertex* outerVertex = alloc.make<Vertex>(outerPoint, 0); 155892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White Edge* bisector = new_edge(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc); 155992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White fix_inversions(innerVertices.fTail, innerVertex, prevBisector, bisector, prevEdge, c); 156092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White fix_inversions(outerVertices.fTail, outerVertex, prevBisector, bisector, prevEdge, c); 156192eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White innerVertices.append(innerVertex); 156292eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White outerVertices.append(outerVertex); 156392eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White prevBisector = bisector; 1564f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1565f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevInner = inner; 1566f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco prevOuter = outer; 156786cc841e23a81af69269de87224c57be274f40d1Stephen White prevEdge = e; 1568f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1569f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco innerVertices.close(); 1570f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco outerVertices.close(); 1571f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1572f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* innerVertex = innerVertices.fHead; 1573f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* outerVertex = outerVertices.fHead; 1574f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (!innerVertex || !outerVertex) { 1575f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return; 1576f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 157792eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White Edge* bisector = new_edge(outerVertices.fHead, innerVertices.fHead, Edge::Type::kConnector, c, 157892eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White alloc); 157992eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White fix_inversions(innerVertices.fTail, innerVertices.fHead, prevBisector, bisector, prevEdge, c); 158092eba8a5efc0860d4e95ba7f25052474a0bfca0cStephen White fix_inversions(outerVertices.fTail, outerVertices.fHead, prevBisector, bisector, prevEdge, c); 1581f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco do { 158248ded38da99c1171ba1bb469f6500f8214e9105cStephen White // Connect vertices into a quad mesh. Outer edges get default (1) winding. 158348ded38da99c1171ba1bb469f6500f8214e9105cStephen White // Inner edges get -2 winding. This ensures that the interior is always filled 158448ded38da99c1171ba1bb469f6500f8214e9105cStephen White // (-1 winding number for normal cases, 3 for thin features where the interior inverts). 158548ded38da99c1171ba1bb469f6500f8214e9105cStephen White // Connector edges get zero winding, since they're only structural (i.e., to ensure 158648ded38da99c1171ba1bb469f6500f8214e9105cStephen White // no 0-0-0 alpha triangles are produced), and shouldn't affect the poly winding number. 1587bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White connect(outerVertex->fPrev, outerVertex, Edge::Type::kOuter, c, alloc); 158848ded38da99c1171ba1bb469f6500f8214e9105cStephen White connect(innerVertex->fPrev, innerVertex, Edge::Type::kInner, c, alloc, -2); 158948ded38da99c1171ba1bb469f6500f8214e9105cStephen White connect(outerVertex, innerVertex, Edge::Type::kConnector, c, alloc, 0); 1590f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* innerNext = innerVertex->fNext; 1591f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Vertex* outerNext = outerVertex->fNext; 1592f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco mesh->append(innerVertex); 1593f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco mesh->append(outerVertex); 1594f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco innerVertex = innerNext; 1595f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco outerVertex = outerNext; 1596f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } while (innerVertex != innerVertices.fHead && outerVertex != outerVertices.fHead); 1597f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1598f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 15995cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid extract_boundary(EdgeList* boundary, Edge* e, SkPath::FillType fillType, SkArenaAlloc& alloc) { 1600f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco bool down = is_boundary_start(e, fillType); 1601f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco while (e) { 1602f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e->fWinding = down ? 1 : -1; 1603f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Edge* next; 1604f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco boundary->append(e); 1605f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (down) { 1606f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco // Find outgoing edge, in clockwise order. 1607f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if ((next = e->fNextEdgeAbove)) { 1608f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = false; 1609f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else if ((next = e->fBottom->fLastEdgeBelow)) { 1610f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = true; 1611f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else if ((next = e->fPrevEdgeAbove)) { 1612f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = false; 1613f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1614f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else { 1615f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco // Find outgoing edge, in counter-clockwise order. 1616f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if ((next = e->fPrevEdgeBelow)) { 1617f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = true; 1618f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else if ((next = e->fTop->fFirstEdgeAbove)) { 1619f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = false; 1620f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else if ((next = e->fNextEdgeBelow)) { 1621f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco down = true; 1622f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1623f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1624e7a364d435843a868dcac2c61d78c34e5d3e326cStephen White disconnect(e); 1625f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco e = next; 1626f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1627f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1628f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1629f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 5b: Extract boundary edges. 1630f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1631bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen WhiteEdgeList* extract_boundaries(const VertexList& mesh, SkPath::FillType fillType, 16325cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1633f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco LOG("extracting boundaries\n"); 1634bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White remove_non_boundary_edges(mesh, fillType, alloc); 1635f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco EdgeList* boundaries = nullptr; 1636bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White for (Vertex* v = mesh.fHead; v != nullptr; v = v->fNext) { 1637f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco while (v->fFirstEdgeBelow) { 1638f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco EdgeList* boundary = new_contour(&boundaries, alloc); 1639f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco extract_boundary(boundary, v->fFirstEdgeBelow, fillType, alloc); 1640f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1641f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1642f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return boundaries; 1643f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1644f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1645f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// This is a driver function which calls stages 2-5 in turn. 1646f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1647bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen Whitevoid contours_to_mesh(Vertex** contours, int contourCnt, bool antialias, 16485cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) { 1649e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 1650e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (int i = 0; i < contourCnt; ++i) { 1651e9709e831954c3427d5cb839e84221a177bfedebethannicholas Vertex* v = contours[i]; 1652e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(v); 1653e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1654e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (v = v->fNext; v != contours[i]; v = v->fNext) { 1655e9709e831954c3427d5cb839e84221a177bfedebethannicholas LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1656e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1657e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1658e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1659f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco sanitize_contours(contours, contourCnt, antialias); 1660bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White build_edges(contours, contourCnt, mesh, c, alloc); 1661f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1662f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 16635cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derbyvoid sort_and_simplify(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { 1664bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White if (!vertices || !vertices->fHead) { 16652f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White return; 1666e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1667e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1668e9709e831954c3427d5cb839e84221a177bfedebethannicholas // Sort vertices in Y (secondarily in X). 1669f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco merge_sort(vertices, c); 1670f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco merge_coincident_vertices(vertices, c, alloc); 1671e9709e831954c3427d5cb839e84221a177bfedebethannicholas#if LOGGING_ENABLED 16722e2cb9bc768bd13e1beeafc9acf8b8f19bea3215Stephen White for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { 1673e9709e831954c3427d5cb839e84221a177bfedebethannicholas static float gID = 0.0f; 1674e9709e831954c3427d5cb839e84221a177bfedebethannicholas v->fID = gID++; 1675e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1676e9709e831954c3427d5cb839e84221a177bfedebethannicholas#endif 1677f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco simplify(*vertices, c, alloc); 16782f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White} 16792f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White 16805cdc9dda330db41d34e452a91b6b0995b5a57626Herb DerbyPoly* mesh_to_polys(VertexList* vertices, Comparator& c, SkArenaAlloc& alloc) { 16812f4686fa25ef7a14961f68be18e63de3567d0a15Stephen White sort_and_simplify(vertices, c, alloc); 1682f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return tessellate(*vertices, alloc); 1683f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1684f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1685f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancoPoly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fillType, 1686f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco const SkRect& pathBounds, bool antialias, 16875cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc& alloc) { 1688f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Comparator c; 1689f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (pathBounds.width() > pathBounds.height()) { 1690f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco c.sweep_lt = sweep_lt_horiz; 1691f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco c.sweep_gt = sweep_gt_horiz; 1692f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } else { 1693f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco c.sweep_lt = sweep_lt_vert; 1694f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco c.sweep_gt = sweep_gt_vert; 1695f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1696bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White VertexList mesh; 1697bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White contours_to_mesh(contours, contourCnt, antialias, &mesh, c, alloc); 16987ab96e92196dd74d5b95d33c8477b256813f3046senorblanco Poly* polys = mesh_to_polys(&mesh, c, alloc); 1699f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (antialias) { 1700f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco EdgeList* boundaries = extract_boundaries(mesh, fillType, alloc); 1701f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco VertexList aaMesh; 1702f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) { 1703f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco simplify_boundary(boundary, c, alloc); 1704f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (boundary->fCount > 2) { 1705f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco boundary_to_aa_mesh(boundary, &aaMesh, c, alloc); 1706f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1707f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1708bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White sort_and_simplify(&aaMesh, c, alloc); 1709bf6137e08b703abd98b3e4d938e1cccef2350f4eStephen White return tessellate(aaMesh, alloc); 1710f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1711f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return polys; 1712f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco} 1713f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1714f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1715f57372daf0562a187c24d427366ac6d0cb980c9bsenorblancovoid* polys_to_triangles(Poly* polys, SkPath::FillType fillType, const AAParams* aaParams, 1716f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void* data) { 1717f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco for (Poly* poly = polys; poly; poly = poly->fNext) { 1718f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (apply_fill_type(fillType, poly)) { 1719f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco data = poly->emit(aaParams, data); 1720f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1721f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco } 1722f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return data; 1723e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1724e9709e831954c3427d5cb839e84221a177bfedebethannicholas 17259d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryPoly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 17265cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby int contourCnt, SkArenaAlloc& alloc, bool antialias, bool* isLinear) { 1727e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPath::FillType fillType = path.getFillType(); 1728e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (SkPath::IsInverseFillType(fillType)) { 1729e9709e831954c3427d5cb839e84221a177bfedebethannicholas contourCnt++; 1730e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 17317ecc59610de72043e9b7ebaf1ef45c43425e54fcBen Wagner std::unique_ptr<Vertex*[]> contours(new Vertex* [contourCnt]); 1732e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1733e9709e831954c3427d5cb839e84221a177bfedebethannicholas path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); 1734f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco return contours_to_polys(contours.get(), contourCnt, path.getFillType(), path.getBounds(), 1735f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco antialias, alloc); 1736e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1737e9709e831954c3427d5cb839e84221a177bfedebethannicholas 17389d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryvoid get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt, 1739e9709e831954c3427d5cb839e84221a177bfedebethannicholas int* sizeEstimate) { 1740e9709e831954c3427d5cb839e84221a177bfedebethannicholas int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); 1741e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (maxPts <= 0) { 1742e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contourCnt = 0; 1743e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 1744e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1745e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (maxPts > ((int)SK_MaxU16 + 1)) { 1746e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); 1747e9709e831954c3427d5cb839e84221a177bfedebethannicholas *contourCnt = 0; 1748e9709e831954c3427d5cb839e84221a177bfedebethannicholas return; 1749e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1750e9709e831954c3427d5cb839e84221a177bfedebethannicholas // For the initial size of the chunk allocator, estimate based on the point count: 1751e9709e831954c3427d5cb839e84221a177bfedebethannicholas // one vertex per point for the initial passes, plus two for the vertices in the 1752e9709e831954c3427d5cb839e84221a177bfedebethannicholas // resulting Polys, since the same point may end up in two Polys. Assume minimal 1753e9709e831954c3427d5cb839e84221a177bfedebethannicholas // connectivity of one Edge per Vertex (will grow for intersections). 1754e9709e831954c3427d5cb839e84221a177bfedebethannicholas *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); 1755e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1756e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1757e9709e831954c3427d5cb839e84221a177bfedebethannicholasint count_points(Poly* polys, SkPath::FillType fillType) { 1758e9709e831954c3427d5cb839e84221a177bfedebethannicholas int count = 0; 1759e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Poly* poly = polys; poly; poly = poly->fNext) { 1760f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (apply_fill_type(fillType, poly) && poly->fCount >= 3) { 1761e9709e831954c3427d5cb839e84221a177bfedebethannicholas count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); 1762e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1763e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1764e9709e831954c3427d5cb839e84221a177bfedebethannicholas return count; 1765e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1766e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1767e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace 1768e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1769e9709e831954c3427d5cb839e84221a177bfedebethannicholasnamespace GrTessellator { 1770e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1771e9709e831954c3427d5cb839e84221a177bfedebethannicholas// Stage 6: Triangulate the monotone polygons into a vertex buffer. 1772e9709e831954c3427d5cb839e84221a177bfedebethannicholas 17739d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1774f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco VertexAllocator* vertexAllocator, bool antialias, const GrColor& color, 1775f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco bool canTweakAlphaForCoverage, bool* isLinear) { 1776e9709e831954c3427d5cb839e84221a177bfedebethannicholas int contourCnt; 1777e9709e831954c3427d5cb839e84221a177bfedebethannicholas int sizeEstimate; 1778e9709e831954c3427d5cb839e84221a177bfedebethannicholas get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1779e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (contourCnt <= 0) { 1780e9709e831954c3427d5cb839e84221a177bfedebethannicholas *isLinear = true; 1781e9709e831954c3427d5cb839e84221a177bfedebethannicholas return 0; 1782e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 17835cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc alloc(sizeEstimate); 1784f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, antialias, 1785f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco isLinear); 17867ab96e92196dd74d5b95d33c8477b256813f3046senorblanco SkPath::FillType fillType = antialias ? SkPath::kWinding_FillType : path.getFillType(); 1787e9709e831954c3427d5cb839e84221a177bfedebethannicholas int count = count_points(polys, fillType); 1788e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (0 == count) { 1789e9709e831954c3427d5cb839e84221a177bfedebethannicholas return 0; 1790e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1791e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1792f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void* verts = vertexAllocator->lock(count); 17936599efffeef3168dfc68dca99c30454c5c23b859senorblanco if (!verts) { 1794e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkDebugf("Could not allocate vertices\n"); 1795e9709e831954c3427d5cb839e84221a177bfedebethannicholas return 0; 1796e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1797f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1798f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco LOG("emitting %d verts\n", count); 1799f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco AAParams aaParams; 1800f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco aaParams.fTweakAlpha = canTweakAlphaForCoverage; 1801f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco aaParams.fColor = color; 1802f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco 1803f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco void* end = polys_to_triangles(polys, fillType, antialias ? &aaParams : nullptr, verts); 1804f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco int actualCount = static_cast<int>((static_cast<uint8_t*>(end) - static_cast<uint8_t*>(verts)) 1805f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco / vertexAllocator->stride()); 1806e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(actualCount <= count); 18076599efffeef3168dfc68dca99c30454c5c23b859senorblanco vertexAllocator->unlock(actualCount); 1808e9709e831954c3427d5cb839e84221a177bfedebethannicholas return actualCount; 1809e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1810e9709e831954c3427d5cb839e84221a177bfedebethannicholas 18119d524f22bfde5dc3dc8f48e1be39bdebd3bb0304halcanaryint PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, 1812e9709e831954c3427d5cb839e84221a177bfedebethannicholas GrTessellator::WindingVertex** verts) { 1813e9709e831954c3427d5cb839e84221a177bfedebethannicholas int contourCnt; 1814e9709e831954c3427d5cb839e84221a177bfedebethannicholas int sizeEstimate; 1815e9709e831954c3427d5cb839e84221a177bfedebethannicholas get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate); 1816e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (contourCnt <= 0) { 1817e9709e831954c3427d5cb839e84221a177bfedebethannicholas return 0; 1818e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 18195cdc9dda330db41d34e452a91b6b0995b5a57626Herb Derby SkArenaAlloc alloc(sizeEstimate); 1820e9709e831954c3427d5cb839e84221a177bfedebethannicholas bool isLinear; 1821f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, false, &isLinear); 1822e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPath::FillType fillType = path.getFillType(); 1823e9709e831954c3427d5cb839e84221a177bfedebethannicholas int count = count_points(polys, fillType); 1824e9709e831954c3427d5cb839e84221a177bfedebethannicholas if (0 == count) { 1825e9709e831954c3427d5cb839e84221a177bfedebethannicholas *verts = nullptr; 1826e9709e831954c3427d5cb839e84221a177bfedebethannicholas return 0; 1827e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1828e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1829e9709e831954c3427d5cb839e84221a177bfedebethannicholas *verts = new GrTessellator::WindingVertex[count]; 1830e9709e831954c3427d5cb839e84221a177bfedebethannicholas GrTessellator::WindingVertex* vertsEnd = *verts; 1831e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint* points = new SkPoint[count]; 1832e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint* pointsEnd = points; 1833e9709e831954c3427d5cb839e84221a177bfedebethannicholas for (Poly* poly = polys; poly; poly = poly->fNext) { 1834f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco if (apply_fill_type(fillType, poly)) { 1835e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkPoint* start = pointsEnd; 1836f57372daf0562a187c24d427366ac6d0cb980c9bsenorblanco pointsEnd = static_cast<SkPoint*>(poly->emit(nullptr, pointsEnd)); 1837e9709e831954c3427d5cb839e84221a177bfedebethannicholas while (start != pointsEnd) { 1838e9709e831954c3427d5cb839e84221a177bfedebethannicholas vertsEnd->fPos = *start; 1839e9709e831954c3427d5cb839e84221a177bfedebethannicholas vertsEnd->fWinding = poly->fWinding; 1840e9709e831954c3427d5cb839e84221a177bfedebethannicholas ++start; 1841e9709e831954c3427d5cb839e84221a177bfedebethannicholas ++vertsEnd; 1842e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1843e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1844e9709e831954c3427d5cb839e84221a177bfedebethannicholas } 1845e9709e831954c3427d5cb839e84221a177bfedebethannicholas int actualCount = static_cast<int>(vertsEnd - *verts); 1846e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(actualCount <= count); 1847e9709e831954c3427d5cb839e84221a177bfedebethannicholas SkASSERT(pointsEnd - points == actualCount); 1848e9709e831954c3427d5cb839e84221a177bfedebethannicholas delete[] points; 1849e9709e831954c3427d5cb839e84221a177bfedebethannicholas return actualCount; 1850e9709e831954c3427d5cb839e84221a177bfedebethannicholas} 1851e9709e831954c3427d5cb839e84221a177bfedebethannicholas 1852e9709e831954c3427d5cb839e84221a177bfedebethannicholas} // namespace 1853