| 1 |
/* |
| 2 |
* Copyright 2015 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/gpu/ganesh/geometry/GrTriangulator.h" |
| 9 |
|
| 10 |
#include "include/core/SkPathTypes.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/private/base/SkDebug.h" |
| 13 |
#include "include/private/base/SkFloatingPoint.h" |
| 14 |
#include "include/private/base/SkMath.h" |
| 15 |
#include "include/private/base/SkTPin.h" |
| 16 |
#include "src/base/SkVx.h" |
| 17 |
#include "src/core/SkGeometry.h" |
| 18 |
#include "src/core/SkPointPriv.h" |
| 19 |
#include "src/gpu/BufferWriter.h" |
| 20 |
#include "src/gpu/ganesh/GrColor.h" |
| 21 |
#include "src/gpu/ganesh/GrEagerVertexAllocator.h" |
| 22 |
#include "src/gpu/ganesh/geometry/GrPathUtils.h" |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <cstddef> |
| 26 |
#include <limits> |
| 27 |
#include <memory> |
| 28 |
#include <tuple> |
| 29 |
#include <utility> |
| 30 |
|
| 31 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 32 |
|
| 33 |
#if TRIANGULATOR_LOGGING |
| 34 |
#define TESS_LOG printf |
| 35 |
#define DUMP_MESH(M) (M).dump() |
| 36 |
#else |
| 37 |
#define TESS_LOG(...) |
| 38 |
#define DUMP_MESH(M) |
| 39 |
#endif |
| 40 |
|
| 41 |
using EdgeType = GrTriangulator::EdgeType; |
| 42 |
using Vertex = GrTriangulator::Vertex; |
| 43 |
using VertexList = GrTriangulator::VertexList; |
| 44 |
using Line = GrTriangulator::Line; |
| 45 |
using Edge = GrTriangulator::Edge; |
| 46 |
using EdgeList = GrTriangulator::EdgeList; |
| 47 |
using Poly = GrTriangulator::Poly; |
| 48 |
using MonotonePoly = GrTriangulator::MonotonePoly; |
| 49 |
using Comparator = GrTriangulator::Comparator; |
| 50 |
|
| 51 |
template <class T, T* T::*Prev, T* T::*Next> |
| 52 |
static void list_insert(T* t, T* prev, T* next, T** head, T** tail) { |
| 53 |
t->*Prev = prev; |
| 54 |
t->*Next = next; |
| 55 |
if (prev) { |
| 56 |
prev->*Next = t; |
| 57 |
} else if (head) { |
| 58 |
*head = t; |
| 59 |
} |
| 60 |
if (next) { |
| 61 |
next->*Prev = t; |
| 62 |
} else if (tail) { |
| 63 |
*tail = t; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
template <class T, T* T::*Prev, T* T::*Next> |
| 68 |
static void list_remove(T* t, T** head, T** tail) { |
| 69 |
if (t->*Prev) { |
| 70 |
t->*Prev->*Next = t->*Next; |
| 71 |
} else if (head) { |
| 72 |
*head = t->*Next; |
| 73 |
} |
| 74 |
if (t->*Next) { |
| 75 |
t->*Next->*Prev = t->*Prev; |
| 76 |
} else if (tail) { |
| 77 |
*tail = t->*Prev; |
| 78 |
} |
| 79 |
t->*Prev = t->*Next = nullptr; |
| 80 |
} |
| 81 |
|
| 82 |
typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); |
| 83 |
|
| 84 |
static bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { |
| 85 |
return a.fX < b.fX || (a.fX == b.fX && a.fY > b.fY); |
| 86 |
} |
| 87 |
|
| 88 |
static bool sweep_lt_vert(const SkPoint& a, const SkPoint& b) { |
| 89 |
return a.fY < b.fY || (a.fY == b.fY && a.fX < b.fX); |
| 90 |
} |
| 91 |
|
| 92 |
bool GrTriangulator::Comparator::sweep_lt(const SkPoint& a, const SkPoint& b) const { |
| 93 |
return fDirection == Direction::kHorizontal ? sweep_lt_horiz(a, b) : sweep_lt_vert(a, b); |
| 94 |
} |
| 95 |
|
| 96 |
static inline skgpu::VertexWriter emit_vertex(Vertex* v, |
| 97 |
bool emitCoverage, |
| 98 |
skgpu::VertexWriter data) { |
| 99 |
data << v->fPoint; |
| 100 |
|
| 101 |
if (emitCoverage) { |
| 102 |
data << GrNormalizeByteToFloat(v->fAlpha); |
| 103 |
} |
| 104 |
|
| 105 |
return data; |
| 106 |
} |
| 107 |
|
| 108 |
static skgpu::VertexWriter emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, |
| 109 |
bool emitCoverage, skgpu::VertexWriter data) { |
| 110 |
TESS_LOG("emit_triangle %g (%g, %g) %d\n", v0->fID, v0->fPoint.fX, v0->fPoint.fY, v0->fAlpha); |
| 111 |
TESS_LOG(" %g (%g, %g) %d\n", v1->fID, v1->fPoint.fX, v1->fPoint.fY, v1->fAlpha); |
| 112 |
TESS_LOG(" %g (%g, %g) %d\n", v2->fID, v2->fPoint.fX, v2->fPoint.fY, v2->fAlpha); |
| 113 |
#if TRIANGULATOR_WIREFRAME |
| 114 |
data = emit_vertex(v0, emitCoverage, std::move(data)); |
| 115 |
data = emit_vertex(v1, emitCoverage, std::move(data)); |
| 116 |
data = emit_vertex(v1, emitCoverage, std::move(data)); |
| 117 |
data = emit_vertex(v2, emitCoverage, std::move(data)); |
| 118 |
data = emit_vertex(v2, emitCoverage, std::move(data)); |
| 119 |
data = emit_vertex(v0, emitCoverage, std::move(data)); |
| 120 |
#else |
| 121 |
data = emit_vertex(v0, emitCoverage, std::move(data)); |
| 122 |
data = emit_vertex(v1, emitCoverage, std::move(data)); |
| 123 |
data = emit_vertex(v2, emitCoverage, std::move(data)); |
| 124 |
#endif |
| 125 |
return data; |
| 126 |
} |
| 127 |
|
| 128 |
void GrTriangulator::VertexList::insert(Vertex* v, Vertex* prev, Vertex* next) { |
| 129 |
list_insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, prev, next, &fHead, &fTail); |
| 130 |
} |
| 131 |
|
| 132 |
void GrTriangulator::VertexList::remove(Vertex* v) { |
| 133 |
list_remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, &fHead, &fTail); |
| 134 |
} |
| 135 |
|
| 136 |
// Round to nearest quarter-pixel. This is used for screenspace tessellation. |
| 137 |
|
| 138 |
static inline void round(SkPoint* p) { |
| 139 |
p->fX = SkScalarRoundToScalar(p->fX * 4.0f) * 0.25f; |
| 140 |
p->fY = SkScalarRoundToScalar(p->fY * 4.0f) * 0.25f; |
| 141 |
} |
| 142 |
|
| 143 |
static inline SkScalar double_to_clamped_scalar(double d) { |
| 144 |
// Clamps large values to what's finitely representable when cast back to a float. |
| 145 |
static const double kMaxLimit = (double) SK_ScalarMax; |
| 146 |
// It's not perfect, but a using a value larger than float_min helps protect from denormalized |
| 147 |
// values and ill-conditions in intermediate calculations on coordinates. |
| 148 |
static const double kNearZeroLimit = 16 * (double) std::numeric_limits<float>::min(); |
| 149 |
if (std::abs(d) < kNearZeroLimit) { |
| 150 |
d = 0.f; |
| 151 |
} |
| 152 |
return SkDoubleToScalar(std::max(-kMaxLimit, std::min(d, kMaxLimit))); |
| 153 |
} |
| 154 |
|
| 155 |
bool GrTriangulator::Line::intersect(const Line& other, SkPoint* point) const { |
| 156 |
double denom = fA * other.fB - fB * other.fA; |
| 157 |
if (denom == 0.0) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
double scale = 1.0 / denom; |
| 161 |
point->fX = double_to_clamped_scalar((fB * other.fC - other.fB * fC) * scale); |
| 162 |
point->fY = double_to_clamped_scalar((other.fA * fC - fA * other.fC) * scale); |
| 163 |
round(point); |
| 164 |
return point->isFinite(); |
| 165 |
} |
| 166 |
|
| 167 |
// If the edge's vertices differ by many orders of magnitude, the computed line equation can have |
| 168 |
// significant error in its distance and intersection tests. To avoid this, we recursively subdivide |
| 169 |
// long edges and effectively perform a binary search to perform a more accurate intersection test. |
| 170 |
static bool edge_line_needs_recursion(const SkPoint& p0, const SkPoint& p1) { |
| 171 |
// ilogbf(0) returns an implementation-defined constant, but we are choosing to saturate |
| 172 |
// negative exponents to 0 for comparisons sake. We're only trying to recurse on lines with |
| 173 |
// very large coordinates. |
| 174 |
int expDiffX = std::abs((std::abs(p0.fX) < 1.f ? 0 : std::ilogbf(p0.fX)) - |
| 175 |
(std::abs(p1.fX) < 1.f ? 0 : std::ilogbf(p1.fX))); |
| 176 |
int expDiffY = std::abs((std::abs(p0.fY) < 1.f ? 0 : std::ilogbf(p0.fY)) - |
| 177 |
(std::abs(p1.fY) < 1.f ? 0 : std::ilogbf(p1.fY))); |
| 178 |
// Differ by more than 2^20, or roughly a factor of one million. |
| 179 |
return expDiffX > 20 || expDiffY > 20; |
| 180 |
} |
| 181 |
|
| 182 |
static bool recursive_edge_intersect(const Line& u, SkPoint u0, SkPoint u1, |
| 183 |
const Line& v, SkPoint v0, SkPoint v1, |
| 184 |
SkPoint* p, double* s, double* t) { |
| 185 |
// First check if the bounding boxes of [u0,u1] intersects [v0,v1]. If they do not, then the |
| 186 |
// two line segments cannot intersect in their domain (even if the lines themselves might). |
| 187 |
// - don't use SkRect::intersect since the vertices aren't sorted and horiz/vertical lines |
| 188 |
// appear as empty rects, which then never "intersect" according to SkRect. |
| 189 |
if (std::min(u0.fX, u1.fX) > std::max(v0.fX, v1.fX) || |
| 190 |
std::max(u0.fX, u1.fX) < std::min(v0.fX, v1.fX) || |
| 191 |
std::min(u0.fY, u1.fY) > std::max(v0.fY, v1.fY) || |
| 192 |
std::max(u0.fY, u1.fY) < std::min(v0.fY, v1.fY)) { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
// Compute intersection based on current segment vertices; if an intersection is found but the |
| 197 |
// vertices differ too much in magnitude, we recurse using the midpoint of the segment to |
| 198 |
// reject false positives. We don't currently try to avoid false negatives (e.g. large magnitude |
| 199 |
// line reports no intersection but there is one). |
| 200 |
double denom = u.fA * v.fB - u.fB * v.fA; |
| 201 |
if (denom == 0.0) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
double dx = static_cast<double>(v0.fX) - u0.fX; |
| 205 |
double dy = static_cast<double>(v0.fY) - u0.fY; |
| 206 |
double sNumer = dy * v.fB + dx * v.fA; |
| 207 |
double tNumer = dy * u.fB + dx * u.fA; |
| 208 |
// If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early. |
| 209 |
// This saves us doing the divide below unless absolutely necessary. |
| 210 |
if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom) |
| 211 |
: (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
*s = sNumer / denom; |
| 216 |
*t = tNumer / denom; |
| 217 |
SkASSERT(*s >= 0.0 && *s <= 1.0 && *t >= 0.0 && *t <= 1.0); |
| 218 |
|
| 219 |
const bool uNeedsSplit = edge_line_needs_recursion(u0, u1); |
| 220 |
const bool vNeedsSplit = edge_line_needs_recursion(v0, v1); |
| 221 |
if (!uNeedsSplit && !vNeedsSplit) { |
| 222 |
p->fX = double_to_clamped_scalar(u0.fX - (*s) * u.fB); |
| 223 |
p->fY = double_to_clamped_scalar(u0.fY + (*s) * u.fA); |
| 224 |
return true; |
| 225 |
} else { |
| 226 |
double sScale = 1.0, sShift = 0.0; |
| 227 |
double tScale = 1.0, tShift = 0.0; |
| 228 |
|
| 229 |
if (uNeedsSplit) { |
| 230 |
SkPoint uM = {(float) (0.5 * u0.fX + 0.5 * u1.fX), |
| 231 |
(float) (0.5 * u0.fY + 0.5 * u1.fY)}; |
| 232 |
sScale = 0.5; |
| 233 |
if (*s >= 0.5) { |
| 234 |
u0 = uM; |
| 235 |
sShift = 0.5; |
| 236 |
} else { |
| 237 |
u1 = uM; |
| 238 |
} |
| 239 |
} |
| 240 |
if (vNeedsSplit) { |
| 241 |
SkPoint vM = {(float) (0.5 * v0.fX + 0.5 * v1.fX), |
| 242 |
(float) (0.5 * v0.fY + 0.5 * v1.fY)}; |
| 243 |
tScale = 0.5; |
| 244 |
if (*t >= 0.5) { |
| 245 |
v0 = vM; |
| 246 |
tShift = 0.5; |
| 247 |
} else { |
| 248 |
v1 = vM; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// Just recompute both lines, even if only one was split; we're already in a slow path. |
| 253 |
if (recursive_edge_intersect(Line(u0, u1), u0, u1, Line(v0, v1), v0, v1, p, s, t)) { |
| 254 |
// Adjust s and t back to full range |
| 255 |
*s = sScale * (*s) + sShift; |
| 256 |
*t = tScale * (*t) + tShift; |
| 257 |
return true; |
| 258 |
} else { |
| 259 |
// False positive |
| 260 |
return false; |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
bool GrTriangulator::Edge::intersect(const Edge& other, SkPoint* p, uint8_t* alpha) const { |
| 266 |
TESS_LOG("intersecting %g -> %g with %g -> %g\n", |
| 267 |
fTop->fID, fBottom->fID, other.fTop->fID, other.fBottom->fID); |
| 268 |
if (fTop == other.fTop || fBottom == other.fBottom || |
| 269 |
fTop == other.fBottom || fBottom == other.fTop) { |
| 270 |
// If the two edges share a vertex by construction, they have already been split and |
| 271 |
// shouldn't be considered "intersecting" anymore. |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
double s, t; // needed to interpolate vertex alpha |
| 276 |
const bool intersects = recursive_edge_intersect( |
| 277 |
fLine, fTop->fPoint, fBottom->fPoint, |
| 278 |
other.fLine, other.fTop->fPoint, other.fBottom->fPoint, |
| 279 |
p, &s, &t); |
| 280 |
if (!intersects) { |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
if (alpha) { |
| 285 |
if (fType == EdgeType::kInner || other.fType == EdgeType::kInner) { |
| 286 |
// If the intersection is on any interior edge, it needs to stay fully opaque or later |
| 287 |
// triangulation could leech transparency into the inner fill region. |
| 288 |
*alpha = 255; |
| 289 |
} else if (fType == EdgeType::kOuter && other.fType == EdgeType::kOuter) { |
| 290 |
// Trivially, the intersection will be fully transparent since since it is by |
| 291 |
// construction on the outer edge. |
| 292 |
*alpha = 0; |
| 293 |
} else { |
| 294 |
// Could be two connectors crossing, or a connector crossing an outer edge. |
| 295 |
// Take the max interpolated alpha |
| 296 |
SkASSERT(fType == EdgeType::kConnector || other.fType == EdgeType::kConnector); |
| 297 |
*alpha = std::max((1.0 - s) * fTop->fAlpha + s * fBottom->fAlpha, |
| 298 |
(1.0 - t) * other.fTop->fAlpha + t * other.fBottom->fAlpha); |
| 299 |
} |
| 300 |
} |
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
void GrTriangulator::EdgeList::insert(Edge* edge, Edge* prev, Edge* next) { |
| 305 |
list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail); |
| 306 |
} |
| 307 |
|
| 308 |
bool GrTriangulator::EdgeList::remove(Edge* edge) { |
| 309 |
TESS_LOG("removing edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
| 310 |
// SkASSERT(this->contains(edge)); // Leave this here for future debugging. |
| 311 |
if (!this->contains(edge)) { |
| 312 |
return false; |
| 313 |
} |
| 314 |
list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail); |
| 315 |
return true; |
| 316 |
} |
| 317 |
|
| 318 |
void GrTriangulator::MonotonePoly::addEdge(Edge* edge) { |
| 319 |
if (fSide == Side::kRight) { |
| 320 |
SkASSERT(!edge->fUsedInRightPoly); |
| 321 |
list_insert<Edge, &Edge::fRightPolyPrev, &Edge::fRightPolyNext>( |
| 322 |
edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
| 323 |
edge->fUsedInRightPoly = true; |
| 324 |
} else { |
| 325 |
SkASSERT(!edge->fUsedInLeftPoly); |
| 326 |
list_insert<Edge, &Edge::fLeftPolyPrev, &Edge::fLeftPolyNext>( |
| 327 |
edge, fLastEdge, nullptr, &fFirstEdge, &fLastEdge); |
| 328 |
edge->fUsedInLeftPoly = true; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
skgpu::VertexWriter GrTriangulator::emitMonotonePoly(const MonotonePoly* monotonePoly, |
| 333 |
skgpu::VertexWriter data) const { |
| 334 |
SkASSERT(monotonePoly->fWinding != 0); |
| 335 |
Edge* e = monotonePoly->fFirstEdge; |
| 336 |
VertexList vertices; |
| 337 |
vertices.append(e->fTop); |
| 338 |
int count = 1; |
| 339 |
while (e != nullptr) { |
| 340 |
if (Side::kRight == monotonePoly->fSide) { |
| 341 |
vertices.append(e->fBottom); |
| 342 |
e = e->fRightPolyNext; |
| 343 |
} else { |
| 344 |
vertices.prepend(e->fBottom); |
| 345 |
e = e->fLeftPolyNext; |
| 346 |
} |
| 347 |
count++; |
| 348 |
} |
| 349 |
Vertex* first = vertices.fHead; |
| 350 |
Vertex* v = first->fNext; |
| 351 |
while (v != vertices.fTail) { |
| 352 |
SkASSERT(v && v->fPrev && v->fNext); |
| 353 |
Vertex* prev = v->fPrev; |
| 354 |
Vertex* curr = v; |
| 355 |
Vertex* next = v->fNext; |
| 356 |
if (count == 3) { |
| 357 |
return this->emitTriangle(prev, curr, next, monotonePoly->fWinding, std::move(data)); |
| 358 |
} |
| 359 |
double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| 360 |
double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| 361 |
double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| 362 |
double by = static_cast<double>(next->fPoint.fY) - curr->fPoint.fY; |
| 363 |
if (ax * by - ay * bx >= 0.0) { |
| 364 |
data = this->emitTriangle(prev, curr, next, monotonePoly->fWinding, std::move(data)); |
| 365 |
v->fPrev->fNext = v->fNext; |
| 366 |
v->fNext->fPrev = v->fPrev; |
| 367 |
count--; |
| 368 |
if (v->fPrev == first) { |
| 369 |
v = v->fNext; |
| 370 |
} else { |
| 371 |
v = v->fPrev; |
| 372 |
} |
| 373 |
} else { |
| 374 |
v = v->fNext; |
| 375 |
} |
| 376 |
} |
| 377 |
return data; |
| 378 |
} |
| 379 |
|
| 380 |
skgpu::VertexWriter GrTriangulator::emitTriangle( |
| 381 |
Vertex* prev, Vertex* curr, Vertex* next, int winding, skgpu::VertexWriter data) const { |
| 382 |
if (winding > 0) { |
| 383 |
// Ensure our triangles always wind in the same direction as if the path had been |
| 384 |
// triangulated as a simple fan (a la red book). |
| 385 |
std::swap(prev, next); |
| 386 |
} |
| 387 |
if (fCollectBreadcrumbTriangles && abs(winding) > 1 && |
| 388 |
fPath.getFillType() == SkPathFillType::kWinding) { |
| 389 |
// The first winding count will come from the actual triangle we emit. The remaining counts |
| 390 |
// come from the breadcrumb triangle. |
| 391 |
fBreadcrumbList.append(fAlloc, prev->fPoint, curr->fPoint, next->fPoint, abs(winding) - 1); |
| 392 |
} |
| 393 |
return emit_triangle(prev, curr, next, fEmitCoverage, std::move(data)); |
| 394 |
} |
| 395 |
|
| 396 |
GrTriangulator::Poly::Poly(Vertex* v, int winding) |
| 397 |
: fFirstVertex(v) |
| 398 |
, fWinding(winding) |
| 399 |
, fHead(nullptr) |
| 400 |
, fTail(nullptr) |
| 401 |
, fNext(nullptr) |
| 402 |
, fPartner(nullptr) |
| 403 |
, fCount(0) |
| 404 |
{ |
| 405 |
#if TRIANGULATOR_LOGGING |
| 406 |
static int gID = 0; |
| 407 |
fID = gID++; |
| 408 |
TESS_LOG("*** created Poly %d\n", fID); |
| 409 |
#endif |
| 410 |
} |
| 411 |
|
| 412 |
Poly* GrTriangulator::Poly::addEdge(Edge* e, Side side, GrTriangulator* tri) { |
| 413 |
TESS_LOG("addEdge (%g -> %g) to poly %d, %s side\n", |
| 414 |
e->fTop->fID, |
| 415 |
e->fBottom->fID, |
| 416 |
fID, |
| 417 |
side == Side::kLeft ? "left" : "right"); |
| 418 |
Poly* partner = fPartner; |
| 419 |
Poly* poly = this; |
| 420 |
if (side == Side::kRight) { |
| 421 |
if (e->fUsedInRightPoly) { |
| 422 |
return this; |
| 423 |
} |
| 424 |
} else { |
| 425 |
if (e->fUsedInLeftPoly) { |
| 426 |
return this; |
| 427 |
} |
| 428 |
} |
| 429 |
if (partner) { |
| 430 |
fPartner = partner->fPartner = nullptr; |
| 431 |
} |
| 432 |
if (!fTail) { |
| 433 |
fHead = fTail = tri->allocateMonotonePoly(e, side, fWinding); |
| 434 |
fCount += 2; |
| 435 |
} else if (e->fBottom == fTail->fLastEdge->fBottom) { |
| 436 |
return poly; |
| 437 |
} else if (side == fTail->fSide) { |
| 438 |
fTail->addEdge(e); |
| 439 |
fCount++; |
| 440 |
} else { |
| 441 |
e = tri->allocateEdge(fTail->fLastEdge->fBottom, e->fBottom, 1, EdgeType::kInner); |
| 442 |
fTail->addEdge(e); |
| 443 |
fCount++; |
| 444 |
if (partner) { |
| 445 |
partner->addEdge(e, side, tri); |
| 446 |
poly = partner; |
| 447 |
} else { |
| 448 |
MonotonePoly* m = tri->allocateMonotonePoly(e, side, fWinding); |
| 449 |
m->fPrev = fTail; |
| 450 |
fTail->fNext = m; |
| 451 |
fTail = m; |
| 452 |
} |
| 453 |
} |
| 454 |
return poly; |
| 455 |
} |
| 456 |
skgpu::VertexWriter GrTriangulator::emitPoly(const Poly* poly, skgpu::VertexWriter data) const { |
| 457 |
if (poly->fCount < 3) { |
| 458 |
return data; |
| 459 |
} |
| 460 |
TESS_LOG("emit() %d, size %d\n", poly->fID, poly->fCount); |
| 461 |
for (MonotonePoly* m = poly->fHead; m != nullptr; m = m->fNext) { |
| 462 |
data = this->emitMonotonePoly(m, std::move(data)); |
| 463 |
} |
| 464 |
return data; |
| 465 |
} |
| 466 |
|
| 467 |
static bool coincident(const SkPoint& a, const SkPoint& b) { |
| 468 |
return a == b; |
| 469 |
} |
| 470 |
|
| 471 |
Poly* GrTriangulator::makePoly(Poly** head, Vertex* v, int winding) const { |
| 472 |
Poly* poly = fAlloc->make<Poly>(v, winding); |
| 473 |
poly->fNext = *head; |
| 474 |
*head = poly; |
| 475 |
return poly; |
| 476 |
} |
| 477 |
|
| 478 |
void GrTriangulator::appendPointToContour(const SkPoint& p, VertexList* contour) const { |
| 479 |
Vertex* v = fAlloc->make<Vertex>(p, 255); |
| 480 |
#if TRIANGULATOR_LOGGING |
| 481 |
static float gID = 0.0f; |
| 482 |
v->fID = gID++; |
| 483 |
#endif |
| 484 |
contour->append(v); |
| 485 |
} |
| 486 |
|
| 487 |
static SkScalar quad_error_at(const SkPoint pts[3], SkScalar t, SkScalar u) { |
| 488 |
SkQuadCoeff quad(pts); |
| 489 |
SkPoint p0 = to_point(quad.eval(t - 0.5f * u)); |
| 490 |
SkPoint mid = to_point(quad.eval(t)); |
| 491 |
SkPoint p1 = to_point(quad.eval(t + 0.5f * u)); |
| 492 |
if (!p0.isFinite() || !mid.isFinite() || !p1.isFinite()) { |
| 493 |
return 0; |
| 494 |
} |
| 495 |
return SkPointPriv::DistanceToLineSegmentBetweenSqd(mid, p0, p1); |
| 496 |
} |
| 497 |
|
| 498 |
void GrTriangulator::appendQuadraticToContour(const SkPoint pts[3], SkScalar toleranceSqd, |
| 499 |
VertexList* contour) const { |
| 500 |
SkQuadCoeff quad(pts); |
| 501 |
skvx::float2 aa = quad.fA * quad.fA; |
| 502 |
SkScalar denom = 2.0f * (aa[0] + aa[1]); |
| 503 |
skvx::float2 ab = quad.fA * quad.fB; |
| 504 |
SkScalar t = denom ? (-ab[0] - ab[1]) / denom : 0.0f; |
| 505 |
int nPoints = 1; |
| 506 |
SkScalar u = 1.0f; |
| 507 |
// Test possible subdivision values only at the point of maximum curvature. |
| 508 |
// If it passes the flatness metric there, it'll pass everywhere. |
| 509 |
while (nPoints < GrPathUtils::kMaxPointsPerCurve) { |
| 510 |
u = 1.0f / nPoints; |
| 511 |
if (quad_error_at(pts, t, u) < toleranceSqd) { |
| 512 |
break; |
| 513 |
} |
| 514 |
nPoints++; |
| 515 |
} |
| 516 |
for (int j = 1; j <= nPoints; j++) { |
| 517 |
this->appendPointToContour(to_point(quad.eval(j * u)), contour); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
void GrTriangulator::generateCubicPoints(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2, |
| 522 |
const SkPoint& p3, SkScalar tolSqd, VertexList* contour, |
| 523 |
int pointsLeft) const { |
| 524 |
SkScalar d1 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p1, p0, p3); |
| 525 |
SkScalar d2 = SkPointPriv::DistanceToLineSegmentBetweenSqd(p2, p0, p3); |
| 526 |
if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || !SkIsFinite(d1, d2)) { |
| 527 |
this->appendPointToContour(p3, contour); |
| 528 |
return; |
| 529 |
} |
| 530 |
const SkPoint q[] = { |
| 531 |
{ sk_float_midpoint(p0.fX, p1.fX), sk_float_midpoint(p0.fY, p1.fY) }, |
| 532 |
{ sk_float_midpoint(p1.fX, p2.fX), sk_float_midpoint(p1.fY, p2.fY) }, |
| 533 |
{ sk_float_midpoint(p2.fX, p3.fX), sk_float_midpoint(p2.fY, p3.fY) } |
| 534 |
}; |
| 535 |
const SkPoint r[] = { |
| 536 |
{ sk_float_midpoint(q[0].fX, q[1].fX), sk_float_midpoint(q[0].fY, q[1].fY) }, |
| 537 |
{ sk_float_midpoint(q[1].fX, q[2].fX), sk_float_midpoint(q[1].fY, q[2].fY) } |
| 538 |
}; |
| 539 |
const SkPoint s = { sk_float_midpoint(r[0].fX, r[1].fX), sk_float_midpoint(r[0].fY, r[1].fY) }; |
| 540 |
pointsLeft >>= 1; |
| 541 |
this->generateCubicPoints(p0, q[0], r[0], s, tolSqd, contour, pointsLeft); |
| 542 |
this->generateCubicPoints(s, r[1], q[2], p3, tolSqd, contour, pointsLeft); |
| 543 |
} |
| 544 |
|
| 545 |
// Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| 546 |
|
| 547 |
void GrTriangulator::pathToContours(float tolerance, const SkRect& clipBounds, |
| 548 |
VertexList* contours, bool* isLinear) const { |
| 549 |
SkScalar toleranceSqd = tolerance * tolerance; |
| 550 |
*isLinear = true; |
| 551 |
VertexList* contour = contours; |
| 552 |
if (fPath.isInverseFillType()) { |
| 553 |
const std::array<SkPoint, 4> quad = clipBounds.toQuad(); |
| 554 |
for (int i = 3; i >= 0; i--) { |
| 555 |
this->appendPointToContour(quad[i], contours); |
| 556 |
} |
| 557 |
contour++; |
| 558 |
} |
| 559 |
SkAutoConicToQuads converter; |
| 560 |
SkPath::Iter iter(fPath, false); |
| 561 |
while (auto rec = iter.next()) { |
| 562 |
SkSpan<const SkPoint> pts = rec->fPoints; |
| 563 |
switch (rec->fVerb) { |
| 564 |
case SkPathVerb::kConic: { |
| 565 |
*isLinear = false; |
| 566 |
if (toleranceSqd == 0) { |
| 567 |
this->appendPointToContour(pts[2], contour); |
| 568 |
break; |
| 569 |
} |
| 570 |
const SkPoint* quadPts = converter.computeQuads(pts.data(), rec->conicWeight(), |
| 571 |
toleranceSqd); |
| 572 |
for (int i = 0; i < converter.countQuads(); ++i) { |
| 573 |
this->appendQuadraticToContour(quadPts, toleranceSqd, contour); |
| 574 |
quadPts += 2; |
| 575 |
} |
| 576 |
break; |
| 577 |
} |
| 578 |
case SkPathVerb::kMove: |
| 579 |
if (contour->fHead) { |
| 580 |
contour++; |
| 581 |
} |
| 582 |
this->appendPointToContour(pts[0], contour); |
| 583 |
break; |
| 584 |
case SkPathVerb::kLine: { |
| 585 |
this->appendPointToContour(pts[1], contour); |
| 586 |
break; |
| 587 |
} |
| 588 |
case SkPathVerb::kQuad: { |
| 589 |
*isLinear = false; |
| 590 |
if (toleranceSqd == 0) { |
| 591 |
this->appendPointToContour(pts[2], contour); |
| 592 |
break; |
| 593 |
} |
| 594 |
this->appendQuadraticToContour(pts.data(), toleranceSqd, contour); |
| 595 |
break; |
| 596 |
} |
| 597 |
case SkPathVerb::kCubic: { |
| 598 |
*isLinear = false; |
| 599 |
if (toleranceSqd == 0) { |
| 600 |
this->appendPointToContour(pts[3], contour); |
| 601 |
break; |
| 602 |
} |
| 603 |
int pointsLeft = GrPathUtils::cubicPointCount(pts.data(), tolerance); |
| 604 |
this->generateCubicPoints(pts[0], pts[1], pts[2], pts[3], toleranceSqd, contour, |
| 605 |
pointsLeft); |
| 606 |
break; |
| 607 |
} |
| 608 |
case SkPathVerb::kClose: |
| 609 |
break; |
| 610 |
} |
| 611 |
} |
| 612 |
} |
| 613 |
|
| 614 |
static inline bool apply_fill_type(SkPathFillType fillType, int winding) { |
| 615 |
switch (fillType) { |
| 616 |
case SkPathFillType::kWinding: |
| 617 |
return winding != 0; |
| 618 |
case SkPathFillType::kEvenOdd: |
| 619 |
return (winding & 1) != 0; |
| 620 |
case SkPathFillType::kInverseWinding: |
| 621 |
return winding == 1; |
| 622 |
case SkPathFillType::kInverseEvenOdd: |
| 623 |
return (winding & 1) == 1; |
| 624 |
default: |
| 625 |
SkASSERT(false); |
| 626 |
return false; |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
bool GrTriangulator::applyFillType(int winding) const { |
| 631 |
return apply_fill_type(fPath.getFillType(), winding); |
| 632 |
} |
| 633 |
|
| 634 |
static inline bool apply_fill_type(SkPathFillType fillType, Poly* poly) { |
| 635 |
return poly && apply_fill_type(fillType, poly->fWinding); |
| 636 |
} |
| 637 |
|
| 638 |
MonotonePoly* GrTriangulator::allocateMonotonePoly(Edge* edge, Side side, int winding) { |
| 639 |
++fNumMonotonePolys; |
| 640 |
return fAlloc->make<MonotonePoly>(edge, side, winding); |
| 641 |
} |
| 642 |
|
| 643 |
Edge* GrTriangulator::allocateEdge(Vertex* top, Vertex* bottom, int winding, EdgeType type) { |
| 644 |
++fNumEdges; |
| 645 |
return fAlloc->make<Edge>(top, bottom, winding, type); |
| 646 |
} |
| 647 |
|
| 648 |
Edge* GrTriangulator::makeEdge(Vertex* prev, Vertex* next, EdgeType type, |
| 649 |
const Comparator& c) { |
| 650 |
SkASSERT(prev->fPoint != next->fPoint); |
| 651 |
int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
| 652 |
Vertex* top = winding < 0 ? next : prev; |
| 653 |
Vertex* bottom = winding < 0 ? prev : next; |
| 654 |
return this->allocateEdge(top, bottom, winding, type); |
| 655 |
} |
| 656 |
|
| 657 |
bool EdgeList::insert(Edge* edge, Edge* prev) { |
| 658 |
TESS_LOG("inserting edge %g -> %g\n", edge->fTop->fID, edge->fBottom->fID); |
| 659 |
// SkASSERT(!this->contains(edge)); // Leave this here for debugging. |
| 660 |
if (this->contains(edge)) { |
| 661 |
return false; |
| 662 |
} |
| 663 |
Edge* next = prev ? prev->fRight : fHead; |
| 664 |
this->insert(edge, prev, next); |
| 665 |
return true; |
| 666 |
} |
| 667 |
|
| 668 |
void GrTriangulator::FindEnclosingEdges(const Vertex& v, |
| 669 |
const EdgeList& edges, |
| 670 |
Edge** left, Edge**right) { |
| 671 |
if (v.fFirstEdgeAbove && v.fLastEdgeAbove) { |
| 672 |
*left = v.fFirstEdgeAbove->fLeft; |
| 673 |
*right = v.fLastEdgeAbove->fRight; |
| 674 |
return; |
| 675 |
} |
| 676 |
Edge* next = nullptr; |
| 677 |
Edge* prev; |
| 678 |
for (prev = edges.fTail; prev != nullptr; prev = prev->fLeft) { |
| 679 |
if (prev->isLeftOf(v)) { |
| 680 |
break; |
| 681 |
} |
| 682 |
next = prev; |
| 683 |
} |
| 684 |
*left = prev; |
| 685 |
*right = next; |
| 686 |
} |
| 687 |
|
| 688 |
void GrTriangulator::Edge::insertAbove(Vertex* v, const Comparator& c) { |
| 689 |
if (fTop->fPoint == fBottom->fPoint || |
| 690 |
c.sweep_lt(fBottom->fPoint, fTop->fPoint)) { |
| 691 |
return; |
| 692 |
} |
| 693 |
TESS_LOG("insert edge (%g -> %g) above vertex %g\n", fTop->fID, fBottom->fID, v->fID); |
| 694 |
Edge* prev = nullptr; |
| 695 |
Edge* next; |
| 696 |
for (next = v->fFirstEdgeAbove; next; next = next->fNextEdgeAbove) { |
| 697 |
if (next->isRightOf(*fTop)) { |
| 698 |
break; |
| 699 |
} |
| 700 |
prev = next; |
| 701 |
} |
| 702 |
list_insert<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
| 703 |
this, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
| 704 |
} |
| 705 |
|
| 706 |
void GrTriangulator::Edge::insertBelow(Vertex* v, const Comparator& c) { |
| 707 |
if (fTop->fPoint == fBottom->fPoint || |
| 708 |
c.sweep_lt(fBottom->fPoint, fTop->fPoint)) { |
| 709 |
return; |
| 710 |
} |
| 711 |
TESS_LOG("insert edge (%g -> %g) below vertex %g\n", fTop->fID, fBottom->fID, v->fID); |
| 712 |
Edge* prev = nullptr; |
| 713 |
Edge* next; |
| 714 |
for (next = v->fFirstEdgeBelow; next; next = next->fNextEdgeBelow) { |
| 715 |
if (next->isRightOf(*fBottom)) { |
| 716 |
break; |
| 717 |
} |
| 718 |
prev = next; |
| 719 |
} |
| 720 |
list_insert<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
| 721 |
this, prev, next, &v->fFirstEdgeBelow, &v->fLastEdgeBelow); |
| 722 |
} |
| 723 |
|
| 724 |
static void remove_edge_above(Edge* edge) { |
| 725 |
SkASSERT(edge->fTop && edge->fBottom); |
| 726 |
TESS_LOG("removing edge (%g -> %g) above vertex %g\n", edge->fTop->fID, edge->fBottom->fID, |
| 727 |
edge->fBottom->fID); |
| 728 |
list_remove<Edge, &Edge::fPrevEdgeAbove, &Edge::fNextEdgeAbove>( |
| 729 |
edge, &edge->fBottom->fFirstEdgeAbove, &edge->fBottom->fLastEdgeAbove); |
| 730 |
} |
| 731 |
|
| 732 |
static void remove_edge_below(Edge* edge) { |
| 733 |
SkASSERT(edge->fTop && edge->fBottom); |
| 734 |
TESS_LOG("removing edge (%g -> %g) below vertex %g\n", |
| 735 |
edge->fTop->fID, edge->fBottom->fID, edge->fTop->fID); |
| 736 |
list_remove<Edge, &Edge::fPrevEdgeBelow, &Edge::fNextEdgeBelow>( |
| 737 |
edge, &edge->fTop->fFirstEdgeBelow, &edge->fTop->fLastEdgeBelow); |
| 738 |
} |
| 739 |
|
| 740 |
void GrTriangulator::Edge::disconnect() { |
| 741 |
remove_edge_above(this); |
| 742 |
remove_edge_below(this); |
| 743 |
} |
| 744 |
|
| 745 |
static bool rewind(EdgeList* activeEdges, Vertex** current, Vertex* dst, const Comparator& c) { |
| 746 |
if (!current || *current == dst || c.sweep_lt((*current)->fPoint, dst->fPoint)) { |
| 747 |
return true; |
| 748 |
} |
| 749 |
Vertex* v = *current; |
| 750 |
TESS_LOG("rewinding active edges from vertex %g to vertex %g\n", v->fID, dst->fID); |
| 751 |
while (v != dst) { |
| 752 |
v = v->fPrev; |
| 753 |
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 754 |
if (!activeEdges->remove(e)) { |
| 755 |
return false; |
| 756 |
} |
| 757 |
} |
| 758 |
Edge* leftEdge = v->fLeftEnclosingEdge; |
| 759 |
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 760 |
if (!e) { |
| 761 |
return false; |
| 762 |
} |
| 763 |
if (!activeEdges->insert(e, leftEdge)) { |
| 764 |
return false; |
| 765 |
} |
| 766 |
leftEdge = e; |
| 767 |
Vertex* top = e->fTop; |
| 768 |
if (!top || |
| 769 |
(top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->hasTopAndBottom()) || |
| 770 |
(top->fRightEnclosingEdge && !top->fRightEnclosingEdge->hasTopAndBottom())) { |
| 771 |
return false; |
| 772 |
} |
| 773 |
if (c.sweep_lt(top->fPoint, dst->fPoint) && |
| 774 |
((top->fLeftEnclosingEdge && !top->fLeftEnclosingEdge->isLeftOf(*top)) || |
| 775 |
(top->fRightEnclosingEdge && !top->fRightEnclosingEdge->isRightOf(*top)))) { |
| 776 |
dst = top; |
| 777 |
} |
| 778 |
} |
| 779 |
} |
| 780 |
*current = v; |
| 781 |
return true; |
| 782 |
} |
| 783 |
|
| 784 |
static bool rewind_if_necessary(Edge* edge, EdgeList* activeEdges, Vertex** current, |
| 785 |
const Comparator& c) { |
| 786 |
if (!activeEdges || !current) { |
| 787 |
return true; |
| 788 |
} |
| 789 |
if (!edge) { |
| 790 |
return false; |
| 791 |
} |
| 792 |
Vertex* top = edge->fTop; |
| 793 |
Vertex* bottom = edge->fBottom; |
| 794 |
if (edge->fLeft) { |
| 795 |
Vertex* leftTop = edge->fLeft->fTop; |
| 796 |
Vertex* leftBottom = edge->fLeft->fBottom; |
| 797 |
if (leftTop && leftBottom) { |
| 798 |
if (c.sweep_lt(leftTop->fPoint, top->fPoint) && !edge->fLeft->isLeftOf(*top)) { |
| 799 |
if (!rewind(activeEdges, current, leftTop, c)) { |
| 800 |
return false; |
| 801 |
} |
| 802 |
} else if (c.sweep_lt(top->fPoint, leftTop->fPoint) && !edge->isRightOf(*leftTop)) { |
| 803 |
if (!rewind(activeEdges, current, top, c)) { |
| 804 |
return false; |
| 805 |
} |
| 806 |
} else if (c.sweep_lt(bottom->fPoint, leftBottom->fPoint) && |
| 807 |
!edge->fLeft->isLeftOf(*bottom)) { |
| 808 |
if (!rewind(activeEdges, current, leftTop, c)) { |
| 809 |
return false; |
| 810 |
} |
| 811 |
} else if (c.sweep_lt(leftBottom->fPoint, bottom->fPoint) && |
| 812 |
!edge->isRightOf(*leftBottom)) { |
| 813 |
if (!rewind(activeEdges, current, top, c)) { |
| 814 |
return false; |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
} |
| 819 |
if (edge->fRight) { |
| 820 |
Vertex* rightTop = edge->fRight->fTop; |
| 821 |
Vertex* rightBottom = edge->fRight->fBottom; |
| 822 |
if (rightTop && rightBottom) { |
| 823 |
if (c.sweep_lt(rightTop->fPoint, top->fPoint) && !edge->fRight->isRightOf(*top)) { |
| 824 |
if (!rewind(activeEdges, current, rightTop, c)) { |
| 825 |
return false; |
| 826 |
} |
| 827 |
} else if (c.sweep_lt(top->fPoint, rightTop->fPoint) && !edge->isLeftOf(*rightTop)) { |
| 828 |
if (!rewind(activeEdges, current, top, c)) { |
| 829 |
return false; |
| 830 |
} |
| 831 |
} else if (c.sweep_lt(bottom->fPoint, rightBottom->fPoint) && |
| 832 |
!edge->fRight->isRightOf(*bottom)) { |
| 833 |
if (!rewind(activeEdges, current, rightTop, c)) { |
| 834 |
return false; |
| 835 |
} |
| 836 |
} else if (c.sweep_lt(rightBottom->fPoint, bottom->fPoint) && |
| 837 |
!edge->isLeftOf(*rightBottom)) { |
| 838 |
if (!rewind(activeEdges, current, top, c)) { |
| 839 |
return false; |
| 840 |
} |
| 841 |
} |
| 842 |
} |
| 843 |
} |
| 844 |
return true; |
| 845 |
} |
| 846 |
|
| 847 |
bool GrTriangulator::setTop(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, |
| 848 |
const Comparator& c) const { |
| 849 |
remove_edge_below(edge); |
| 850 |
if (fCollectBreadcrumbTriangles) { |
| 851 |
fBreadcrumbList.append(fAlloc, edge->fTop->fPoint, edge->fBottom->fPoint, v->fPoint, |
| 852 |
edge->fWinding); |
| 853 |
} |
| 854 |
edge->fTop = v; |
| 855 |
edge->recompute(); |
| 856 |
edge->insertBelow(v, c); |
| 857 |
if (!rewind_if_necessary(edge, activeEdges, current, c)) { |
| 858 |
return false; |
| 859 |
} |
| 860 |
return this->mergeCollinearEdges(edge, activeEdges, current, c); |
| 861 |
} |
| 862 |
|
| 863 |
bool GrTriangulator::setBottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, |
| 864 |
const Comparator& c) const { |
| 865 |
remove_edge_above(edge); |
| 866 |
if (fCollectBreadcrumbTriangles) { |
| 867 |
fBreadcrumbList.append(fAlloc, edge->fTop->fPoint, edge->fBottom->fPoint, v->fPoint, |
| 868 |
edge->fWinding); |
| 869 |
} |
| 870 |
edge->fBottom = v; |
| 871 |
edge->recompute(); |
| 872 |
edge->insertAbove(v, c); |
| 873 |
if (!rewind_if_necessary(edge, activeEdges, current, c)) { |
| 874 |
return false; |
| 875 |
} |
| 876 |
return this->mergeCollinearEdges(edge, activeEdges, current, c); |
| 877 |
} |
| 878 |
|
| 879 |
/* |
| 880 |
* NOTE: Also used in mergeEdgesBelow() |
| 881 |
* Merges two adjacent, collinear edges. This is a complex operation with two main cases: |
| 882 |
* |
| 883 |
* 1. Coincident endpoints: If the edges share the exact same top/bottom vertex, one edge's winding |
| 884 |
* is absorbed into the other. The now-unused "zombie" edge is fully disconnected and also |
| 885 |
* explicitly removed from the activeEdges list to prevent state corruption that can lead to |
| 886 |
* null-pointer dereferences (b/419397557, b/421959607). |
| 887 |
* |
| 888 |
* 2. Non-coincident (overlapping) endpoints: One edge is shortened to meet the endpoint of the |
| 889 |
* other by modifying it in-place. This is a fragile operation that relies on the stability of |
| 890 |
* the floating point comparisons to succeed. To combat this, the geometric checks (`isLeftOf`, |
| 891 |
* etc.) use an adaptive, per-edge epsilon. This tries to ensure that the decision to merge is |
| 892 |
* mathematically sound, even with pathological floating-point coordinates. |
| 893 |
*/ |
| 894 |
bool GrTriangulator::mergeEdgesAbove(Edge* edge, Edge* other, EdgeList* activeEdges, |
| 895 |
Vertex** current, const Comparator& c) const { |
| 896 |
if (!edge || !other) { |
| 897 |
return false; |
| 898 |
} |
| 899 |
if (coincident(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 900 |
TESS_LOG("merging coincident above edges (%g, %g) -> (%g, %g)\n", |
| 901 |
edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 902 |
edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
| 903 |
if (!rewind(activeEdges, current, edge->fTop, c)) { |
| 904 |
return false; |
| 905 |
} |
| 906 |
other->fWinding += edge->fWinding; |
| 907 |
edge->disconnect(); |
| 908 |
if (activeEdges) { |
| 909 |
activeEdges->remove(edge); |
| 910 |
} |
| 911 |
edge->fTop = edge->fBottom = nullptr; |
| 912 |
} else if (c.sweep_lt(edge->fTop->fPoint, other->fTop->fPoint)) { |
| 913 |
if (!rewind(activeEdges, current, edge->fTop, c)) { |
| 914 |
return false; |
| 915 |
} |
| 916 |
other->fWinding += edge->fWinding; |
| 917 |
if (!this->setBottom(edge, other->fTop, activeEdges, current, c)) { |
| 918 |
return false; |
| 919 |
} |
| 920 |
} else { |
| 921 |
if (!rewind(activeEdges, current, other->fTop, c)) { |
| 922 |
return false; |
| 923 |
} |
| 924 |
edge->fWinding += other->fWinding; |
| 925 |
if (!this->setBottom(other, edge->fTop, activeEdges, current, c)) { |
| 926 |
return false; |
| 927 |
} |
| 928 |
} |
| 929 |
return true; |
| 930 |
} |
| 931 |
|
| 932 |
// NOTE: See mergeEdgesAbove() comment |
| 933 |
bool GrTriangulator::mergeEdgesBelow(Edge* edge, Edge* other, EdgeList* activeEdges, |
| 934 |
Vertex** current, const Comparator& c) const { |
| 935 |
if (!edge || !other) { |
| 936 |
return false; |
| 937 |
} |
| 938 |
if (coincident(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 939 |
TESS_LOG("merging coincident below edges (%g, %g) -> (%g, %g)\n", |
| 940 |
edge->fTop->fPoint.fX, edge->fTop->fPoint.fY, |
| 941 |
edge->fBottom->fPoint.fX, edge->fBottom->fPoint.fY); |
| 942 |
if (!rewind(activeEdges, current, edge->fTop, c)) { |
| 943 |
return false; |
| 944 |
} |
| 945 |
other->fWinding += edge->fWinding; |
| 946 |
edge->disconnect(); |
| 947 |
if (activeEdges) { |
| 948 |
activeEdges->remove(edge); |
| 949 |
} |
| 950 |
edge->fTop = edge->fBottom = nullptr; |
| 951 |
} else if (c.sweep_lt(edge->fBottom->fPoint, other->fBottom->fPoint)) { |
| 952 |
if (!rewind(activeEdges, current, other->fTop, c)) { |
| 953 |
return false; |
| 954 |
} |
| 955 |
edge->fWinding += other->fWinding; |
| 956 |
if (!this->setTop(other, edge->fBottom, activeEdges, current, c)) { |
| 957 |
return false; |
| 958 |
} |
| 959 |
} else { |
| 960 |
if (!rewind(activeEdges, current, edge->fTop, c)) { |
| 961 |
return false; |
| 962 |
} |
| 963 |
other->fWinding += edge->fWinding; |
| 964 |
if (!this->setTop(edge, other->fBottom, activeEdges, current, c)) { |
| 965 |
return false; |
| 966 |
} |
| 967 |
} |
| 968 |
return true; |
| 969 |
} |
| 970 |
|
| 971 |
static bool top_collinear(Edge* left, Edge* right) { |
| 972 |
if (!left || !right) { |
| 973 |
return false; |
| 974 |
} |
| 975 |
return left->fTop->fPoint == right->fTop->fPoint || |
| 976 |
!left->isLeftOf(*right->fTop) || !right->isRightOf(*left->fTop); |
| 977 |
} |
| 978 |
|
| 979 |
static bool bottom_collinear(Edge* left, Edge* right) { |
| 980 |
if (!left || !right) { |
| 981 |
return false; |
| 982 |
} |
| 983 |
return left->fBottom->fPoint == right->fBottom->fPoint || |
| 984 |
!left->isLeftOf(*right->fBottom) || !right->isRightOf(*left->fBottom); |
| 985 |
} |
| 986 |
|
| 987 |
// How deep of a stack of mergeCollinearEdges() we'll accept |
| 988 |
static constexpr int kMaxMergeCollinearCalls = 64; |
| 989 |
|
| 990 |
bool GrTriangulator::mergeCollinearEdges(Edge* edge, EdgeList* activeEdges, Vertex** current, |
| 991 |
const Comparator& c) const { |
| 992 |
// Stack is unreasonably deep |
| 993 |
if (++fMergeCollinearStackCount > kMaxMergeCollinearCalls) { |
| 994 |
return false; |
| 995 |
} |
| 996 |
for (;;) { |
| 997 |
if (top_collinear(edge->fPrevEdgeAbove, edge)) { |
| 998 |
if (!this->mergeEdgesAbove(edge->fPrevEdgeAbove, edge, activeEdges, current, c)) { |
| 999 |
return false; |
| 1000 |
} |
| 1001 |
} else if (top_collinear(edge, edge->fNextEdgeAbove)) { |
| 1002 |
if (!this->mergeEdgesAbove(edge->fNextEdgeAbove, edge, activeEdges, current, c)) { |
| 1003 |
return false; |
| 1004 |
} |
| 1005 |
} else if (bottom_collinear(edge->fPrevEdgeBelow, edge)) { |
| 1006 |
if (!this->mergeEdgesBelow(edge->fPrevEdgeBelow, edge, activeEdges, current, c)) { |
| 1007 |
return false; |
| 1008 |
} |
| 1009 |
} else if (bottom_collinear(edge, edge->fNextEdgeBelow)) { |
| 1010 |
if (!this->mergeEdgesBelow(edge->fNextEdgeBelow, edge, activeEdges, current, c)) { |
| 1011 |
return false; |
| 1012 |
} |
| 1013 |
} else { |
| 1014 |
break; |
| 1015 |
} |
| 1016 |
} |
| 1017 |
SkASSERT(!top_collinear(edge->fPrevEdgeAbove, edge)); |
| 1018 |
SkASSERT(!top_collinear(edge, edge->fNextEdgeAbove)); |
| 1019 |
SkASSERT(!bottom_collinear(edge->fPrevEdgeBelow, edge)); |
| 1020 |
SkASSERT(!bottom_collinear(edge, edge->fNextEdgeBelow)); |
| 1021 |
return true; |
| 1022 |
} |
| 1023 |
|
| 1024 |
GrTriangulator::BoolFail GrTriangulator::splitEdge( |
| 1025 |
Edge* edge, Vertex* v, EdgeList* activeEdges, Vertex** current, const Comparator& c) { |
| 1026 |
if (!edge->fTop || !edge->fBottom || v == edge->fTop || v == edge->fBottom) { |
| 1027 |
return BoolFail::kFalse; |
| 1028 |
} |
| 1029 |
TESS_LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| 1030 |
edge->fTop->fID, edge->fBottom->fID, v->fID, v->fPoint.fX, v->fPoint.fY); |
| 1031 |
Vertex* top; |
| 1032 |
Vertex* bottom; |
| 1033 |
int winding = edge->fWinding; |
| 1034 |
// Theoretically, and ideally, the edge betwee p0 and p1 is being split by v, and v is "between" |
| 1035 |
// the segment end points according to c. This is equivalent to p0 < v < p1. Unfortunately, if |
| 1036 |
// v was clamped/rounded this relation doesn't always hold. |
| 1037 |
if (c.sweep_lt(v->fPoint, edge->fTop->fPoint)) { |
| 1038 |
// Actually "v < p0 < p1": update 'edge' to be v->p1 and add v->p0. We flip the winding on |
| 1039 |
// the new edge so that it winds as if it were p0->v. |
| 1040 |
top = v; |
| 1041 |
bottom = edge->fTop; |
| 1042 |
winding *= -1; |
| 1043 |
if (!this->setTop(edge, v, activeEdges, current, c)) { |
| 1044 |
return BoolFail::kFail; |
| 1045 |
} |
| 1046 |
} else if (c.sweep_lt(edge->fBottom->fPoint, v->fPoint)) { |
| 1047 |
// Actually "p0 < p1 < v": update 'edge' to be p0->v and add p1->v. We flip the winding on |
| 1048 |
// the new edge so that it winds as if it were v->p1. |
| 1049 |
top = edge->fBottom; |
| 1050 |
bottom = v; |
| 1051 |
winding *= -1; |
| 1052 |
if (!this->setBottom(edge, v, activeEdges, current, c)) { |
| 1053 |
return BoolFail::kFail; |
| 1054 |
} |
| 1055 |
} else { |
| 1056 |
// The ideal case, "p0 < v < p1": update 'edge' to be p0->v and add v->p1. Original winding |
| 1057 |
// is valid for both edges. |
| 1058 |
top = v; |
| 1059 |
bottom = edge->fBottom; |
| 1060 |
if (!this->setBottom(edge, v, activeEdges, current, c)) { |
| 1061 |
return BoolFail::kFail; |
| 1062 |
} |
| 1063 |
} |
| 1064 |
Edge* newEdge = this->allocateEdge(top, bottom, winding, edge->fType); |
| 1065 |
newEdge->insertBelow(top, c); |
| 1066 |
newEdge->insertAbove(bottom, c); |
| 1067 |
fMergeCollinearStackCount = 0; |
| 1068 |
if (!this->mergeCollinearEdges(newEdge, activeEdges, current, c)) { |
| 1069 |
return BoolFail::kFail; |
| 1070 |
} |
| 1071 |
return BoolFail::kTrue; |
| 1072 |
} |
| 1073 |
|
| 1074 |
GrTriangulator::BoolFail GrTriangulator::intersectEdgePair( |
| 1075 |
Edge* left, Edge* right, EdgeList* activeEdges, Vertex** current, const Comparator& c) { |
| 1076 |
if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) { |
| 1077 |
return BoolFail::kFalse; |
| 1078 |
} |
| 1079 |
if (left->fTop == right->fTop || left->fBottom == right->fBottom) { |
| 1080 |
return BoolFail::kFalse; |
| 1081 |
} |
| 1082 |
|
| 1083 |
// Check if the lines intersect as determined by isLeftOf and isRightOf, since that is the |
| 1084 |
// source of ground truth. It may suggest an intersection even if Edge::intersect() did not have |
| 1085 |
// the precision to check it. In this case we are explicitly correcting the edge topology to |
| 1086 |
// match the sided-ness checks. |
| 1087 |
Edge* split = nullptr; |
| 1088 |
Vertex* splitAt = nullptr; |
| 1089 |
if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1090 |
if (!left->isLeftOf(*right->fTop)) { |
| 1091 |
split = left; |
| 1092 |
splitAt = right->fTop; |
| 1093 |
} |
| 1094 |
} else { |
| 1095 |
if (!right->isRightOf(*left->fTop)) { |
| 1096 |
split = right; |
| 1097 |
splitAt = left->fTop; |
| 1098 |
} |
| 1099 |
} |
| 1100 |
if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1101 |
if (!left->isLeftOf(*right->fBottom)) { |
| 1102 |
split = left; |
| 1103 |
splitAt = right->fBottom; |
| 1104 |
} |
| 1105 |
} else { |
| 1106 |
if (!right->isRightOf(*left->fBottom)) { |
| 1107 |
split = right; |
| 1108 |
splitAt = left->fBottom; |
| 1109 |
} |
| 1110 |
} |
| 1111 |
|
| 1112 |
if (!split) { |
| 1113 |
return BoolFail::kFalse; |
| 1114 |
} |
| 1115 |
|
| 1116 |
// Rewind to the top of the edge that is "moving" since this topology correction can change the |
| 1117 |
// geometry of the split edge. |
| 1118 |
if (!rewind(activeEdges, current, split->fTop, c)) { |
| 1119 |
return BoolFail::kFail; |
| 1120 |
} |
| 1121 |
return this->splitEdge(split, splitAt, activeEdges, current, c); |
| 1122 |
} |
| 1123 |
|
| 1124 |
Edge* GrTriangulator::makeConnectingEdge(Vertex* prev, Vertex* next, EdgeType type, |
| 1125 |
const Comparator& c, int windingScale) { |
| 1126 |
if (!prev || !next || prev->fPoint == next->fPoint) { |
| 1127 |
return nullptr; |
| 1128 |
} |
| 1129 |
Edge* edge = this->makeEdge(prev, next, type, c); |
| 1130 |
edge->insertBelow(edge->fTop, c); |
| 1131 |
edge->insertAbove(edge->fBottom, c); |
| 1132 |
edge->fWinding *= windingScale; |
| 1133 |
fMergeCollinearStackCount = 0; |
| 1134 |
this->mergeCollinearEdges(edge, nullptr, nullptr, c); |
| 1135 |
return edge; |
| 1136 |
} |
| 1137 |
|
| 1138 |
void GrTriangulator::mergeVertices(Vertex* src, Vertex* dst, VertexList* mesh, |
| 1139 |
const Comparator& c) const { |
| 1140 |
TESS_LOG("found coincident verts at %g, %g; merging %g into %g\n", |
| 1141 |
src->fPoint.fX, src->fPoint.fY, src->fID, dst->fID); |
| 1142 |
dst->fAlpha = std::max(src->fAlpha, dst->fAlpha); |
| 1143 |
if (src->fPartner) { |
| 1144 |
src->fPartner->fPartner = dst; |
| 1145 |
} |
| 1146 |
// setBottom()/setTop() will call mergeCollinearEdges() but this can recurse, |
| 1147 |
// so we need to clear the stack count here. |
| 1148 |
while (Edge* edge = src->fFirstEdgeAbove) { |
| 1149 |
fMergeCollinearStackCount = 0; |
| 1150 |
std::ignore = this->setBottom(edge, dst, nullptr, nullptr, c); |
| 1151 |
} |
| 1152 |
while (Edge* edge = src->fFirstEdgeBelow) { |
| 1153 |
fMergeCollinearStackCount = 0; |
| 1154 |
std::ignore = this->setTop(edge, dst, nullptr, nullptr, c); |
| 1155 |
} |
| 1156 |
mesh->remove(src); |
| 1157 |
dst->fSynthetic = true; |
| 1158 |
} |
| 1159 |
|
| 1160 |
Vertex* GrTriangulator::makeSortedVertex(const SkPoint& p, uint8_t alpha, VertexList* mesh, |
| 1161 |
Vertex* reference, const Comparator& c) const { |
| 1162 |
Vertex* prevV = reference; |
| 1163 |
while (prevV && c.sweep_lt(p, prevV->fPoint)) { |
| 1164 |
prevV = prevV->fPrev; |
| 1165 |
} |
| 1166 |
Vertex* nextV = prevV ? prevV->fNext : mesh->fHead; |
| 1167 |
while (nextV && c.sweep_lt(nextV->fPoint, p)) { |
| 1168 |
prevV = nextV; |
| 1169 |
nextV = nextV->fNext; |
| 1170 |
} |
| 1171 |
Vertex* v; |
| 1172 |
if (prevV && coincident(prevV->fPoint, p)) { |
| 1173 |
v = prevV; |
| 1174 |
} else if (nextV && coincident(nextV->fPoint, p)) { |
| 1175 |
v = nextV; |
| 1176 |
} else { |
| 1177 |
v = fAlloc->make<Vertex>(p, alpha); |
| 1178 |
#if TRIANGULATOR_LOGGING |
| 1179 |
if (!prevV) { |
| 1180 |
v->fID = mesh->fHead->fID - 1.0f; |
| 1181 |
} else if (!nextV) { |
| 1182 |
v->fID = mesh->fTail->fID + 1.0f; |
| 1183 |
} else { |
| 1184 |
v->fID = (prevV->fID + nextV->fID) * 0.5f; |
| 1185 |
} |
| 1186 |
#endif |
| 1187 |
mesh->insert(v, prevV, nextV); |
| 1188 |
} |
| 1189 |
return v; |
| 1190 |
} |
| 1191 |
|
| 1192 |
// Clamps x and y coordinates independently, so the returned point will lie within the bounding |
| 1193 |
// box formed by the corners of 'min' and 'max' (although min/max here refer to the ordering |
| 1194 |
// imposed by 'c'). |
| 1195 |
static SkPoint clamp(SkPoint p, SkPoint min, SkPoint max, const Comparator& c) { |
| 1196 |
if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1197 |
// With horizontal sorting, we know min.x <= max.x, but there's no relation between |
| 1198 |
// Y components unless min.x == max.x. |
| 1199 |
return {SkTPin(p.fX, min.fX, max.fX), |
| 1200 |
min.fY < max.fY ? SkTPin(p.fY, min.fY, max.fY) |
| 1201 |
: SkTPin(p.fY, max.fY, min.fY)}; |
| 1202 |
} else { |
| 1203 |
// And with vertical sorting, we know Y's relation but not necessarily X's. |
| 1204 |
return {min.fX < max.fX ? SkTPin(p.fX, min.fX, max.fX) |
| 1205 |
: SkTPin(p.fX, max.fX, min.fX), |
| 1206 |
SkTPin(p.fY, min.fY, max.fY)}; |
| 1207 |
} |
| 1208 |
} |
| 1209 |
|
| 1210 |
void GrTriangulator::computeBisector(Edge* edge1, Edge* edge2, Vertex* v) const { |
| 1211 |
SkASSERT(fEmitCoverage); // Edge-AA only! |
| 1212 |
Line line1 = edge1->fLine; |
| 1213 |
Line line2 = edge2->fLine; |
| 1214 |
line1.normalize(); |
| 1215 |
line2.normalize(); |
| 1216 |
double cosAngle = line1.fA * line2.fA + line1.fB * line2.fB; |
| 1217 |
if (cosAngle > 0.999) { |
| 1218 |
return; |
| 1219 |
} |
| 1220 |
line1.fC += edge1->fWinding > 0 ? -1 : 1; |
| 1221 |
line2.fC += edge2->fWinding > 0 ? -1 : 1; |
| 1222 |
SkPoint p; |
| 1223 |
if (line1.intersect(line2, &p)) { |
| 1224 |
uint8_t alpha = edge1->fType == EdgeType::kOuter ? 255 : 0; |
| 1225 |
v->fPartner = fAlloc->make<Vertex>(p, alpha); |
| 1226 |
TESS_LOG("computed bisector (%g,%g) alpha %d for vertex %g\n", p.fX, p.fY, alpha, v->fID); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
GrTriangulator::BoolFail GrTriangulator::checkForIntersection( |
| 1231 |
Edge* left, Edge* right, EdgeList* activeEdges, |
| 1232 |
Vertex** current, VertexList* mesh, |
| 1233 |
const Comparator& c) { |
| 1234 |
if (!left || !right) { |
| 1235 |
return BoolFail::kFalse; |
| 1236 |
} |
| 1237 |
SkPoint p; |
| 1238 |
uint8_t alpha; |
| 1239 |
// If we are going to call intersect, then there must be tops and bottoms. |
| 1240 |
if (!left->fTop || !left->fBottom || !right->fTop || !right->fBottom) { |
| 1241 |
return BoolFail::kFail; |
| 1242 |
} |
| 1243 |
if (left->intersect(*right, &p, &alpha) && p.isFinite()) { |
| 1244 |
Vertex* v; |
| 1245 |
TESS_LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); |
| 1246 |
Vertex* top = *current; |
| 1247 |
// If the intersection point is above the current vertex, rewind to the vertex above the |
| 1248 |
// intersection. |
| 1249 |
while (top && c.sweep_lt(p, top->fPoint)) { |
| 1250 |
top = top->fPrev; |
| 1251 |
} |
| 1252 |
|
| 1253 |
// Always clamp the intersection to lie between the vertices of each segment, since |
| 1254 |
// in theory that's where the intersection is, but in reality, floating point error may |
| 1255 |
// have computed an intersection beyond a vertex's component(s). |
| 1256 |
p = clamp(p, left->fTop->fPoint, left->fBottom->fPoint, c); |
| 1257 |
p = clamp(p, right->fTop->fPoint, right->fBottom->fPoint, c); |
| 1258 |
|
| 1259 |
if (coincident(p, left->fTop->fPoint)) { |
| 1260 |
v = left->fTop; |
| 1261 |
} else if (coincident(p, left->fBottom->fPoint)) { |
| 1262 |
v = left->fBottom; |
| 1263 |
} else if (coincident(p, right->fTop->fPoint)) { |
| 1264 |
v = right->fTop; |
| 1265 |
} else if (coincident(p, right->fBottom->fPoint)) { |
| 1266 |
v = right->fBottom; |
| 1267 |
} else { |
| 1268 |
v = this->makeSortedVertex(p, alpha, mesh, top, c); |
| 1269 |
if (left->fTop->fPartner) { |
| 1270 |
SkASSERT(fEmitCoverage); // Edge-AA only! |
| 1271 |
v->fSynthetic = true; |
| 1272 |
this->computeBisector(left, right, v); |
| 1273 |
} |
| 1274 |
} |
| 1275 |
if (!rewind(activeEdges, current, top ? top : v, c)) { |
| 1276 |
return BoolFail::kFail; |
| 1277 |
} |
| 1278 |
if (this->splitEdge(left, v, activeEdges, current, c) == BoolFail::kFail) { |
| 1279 |
return BoolFail::kFail; |
| 1280 |
} |
| 1281 |
if (this->splitEdge(right, v, activeEdges, current, c) == BoolFail::kFail) { |
| 1282 |
return BoolFail::kFail; |
| 1283 |
} |
| 1284 |
v->fAlpha = std::max(v->fAlpha, alpha); |
| 1285 |
return BoolFail::kTrue; |
| 1286 |
} |
| 1287 |
return this->intersectEdgePair(left, right, activeEdges, current, c); |
| 1288 |
} |
| 1289 |
|
| 1290 |
void GrTriangulator::sanitizeContours(VertexList* contours, int contourCnt) const { |
| 1291 |
for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1292 |
SkASSERT(contour->fHead); |
| 1293 |
Vertex* prev = contour->fTail; |
| 1294 |
prev->fPoint.fX = double_to_clamped_scalar((double) prev->fPoint.fX); |
| 1295 |
prev->fPoint.fY = double_to_clamped_scalar((double) prev->fPoint.fY); |
| 1296 |
if (fRoundVerticesToQuarterPixel) { |
| 1297 |
round(&prev->fPoint); |
| 1298 |
} |
| 1299 |
for (Vertex* v = contour->fHead; v;) { |
| 1300 |
v->fPoint.fX = double_to_clamped_scalar((double) v->fPoint.fX); |
| 1301 |
v->fPoint.fY = double_to_clamped_scalar((double) v->fPoint.fY); |
| 1302 |
if (fRoundVerticesToQuarterPixel) { |
| 1303 |
round(&v->fPoint); |
| 1304 |
} |
| 1305 |
Vertex* next = v->fNext; |
| 1306 |
Vertex* nextWrap = next ? next : contour->fHead; |
| 1307 |
if (coincident(prev->fPoint, v->fPoint)) { |
| 1308 |
TESS_LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1309 |
contour->remove(v); |
| 1310 |
} else if (!v->fPoint.isFinite()) { |
| 1311 |
TESS_LOG("vertex %g,%g non-finite; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1312 |
contour->remove(v); |
| 1313 |
} else if (!fPreserveCollinearVertices && |
| 1314 |
Line(prev->fPoint, nextWrap->fPoint).dist(v->fPoint) == 0.0) { |
| 1315 |
TESS_LOG("vertex %g,%g collinear; removing\n", v->fPoint.fX, v->fPoint.fY); |
| 1316 |
contour->remove(v); |
| 1317 |
} else { |
| 1318 |
prev = v; |
| 1319 |
} |
| 1320 |
v = next; |
| 1321 |
} |
| 1322 |
} |
| 1323 |
} |
| 1324 |
|
| 1325 |
bool GrTriangulator::mergeCoincidentVertices(VertexList* mesh, const Comparator& c) const { |
| 1326 |
if (!mesh->fHead) { |
| 1327 |
return false; |
| 1328 |
} |
| 1329 |
bool merged = false; |
| 1330 |
for (Vertex* v = mesh->fHead->fNext; v;) { |
| 1331 |
Vertex* next = v->fNext; |
| 1332 |
if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| 1333 |
v->fPoint = v->fPrev->fPoint; |
| 1334 |
} |
| 1335 |
if (coincident(v->fPrev->fPoint, v->fPoint)) { |
| 1336 |
this->mergeVertices(v, v->fPrev, mesh, c); |
| 1337 |
merged = true; |
| 1338 |
} |
| 1339 |
v = next; |
| 1340 |
} |
| 1341 |
return merged; |
| 1342 |
} |
| 1343 |
|
| 1344 |
// Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| 1345 |
|
| 1346 |
void GrTriangulator::buildEdges(VertexList* contours, int contourCnt, VertexList* mesh, |
| 1347 |
const Comparator& c) { |
| 1348 |
for (VertexList* contour = contours; contourCnt > 0; --contourCnt, ++contour) { |
| 1349 |
Vertex* prev = contour->fTail; |
| 1350 |
for (Vertex* v = contour->fHead; v;) { |
| 1351 |
Vertex* next = v->fNext; |
| 1352 |
this->makeConnectingEdge(prev, v, EdgeType::kInner, c); |
| 1353 |
mesh->append(v); |
| 1354 |
prev = v; |
| 1355 |
v = next; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
template <CompareFunc sweep_lt> |
| 1361 |
static void sorted_merge(VertexList* front, VertexList* back, VertexList* result) { |
| 1362 |
Vertex* a = front->fHead; |
| 1363 |
Vertex* b = back->fHead; |
| 1364 |
while (a && b) { |
| 1365 |
if (sweep_lt(a->fPoint, b->fPoint)) { |
| 1366 |
front->remove(a); |
| 1367 |
result->append(a); |
| 1368 |
a = front->fHead; |
| 1369 |
} else { |
| 1370 |
back->remove(b); |
| 1371 |
result->append(b); |
| 1372 |
b = back->fHead; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
result->append(*front); |
| 1376 |
result->append(*back); |
| 1377 |
} |
| 1378 |
|
| 1379 |
void GrTriangulator::SortedMerge(VertexList* front, VertexList* back, VertexList* result, |
| 1380 |
const Comparator& c) { |
| 1381 |
if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1382 |
sorted_merge<sweep_lt_horiz>(front, back, result); |
| 1383 |
} else { |
| 1384 |
sorted_merge<sweep_lt_vert>(front, back, result); |
| 1385 |
} |
| 1386 |
#if TRIANGULATOR_LOGGING |
| 1387 |
float id = 0.0f; |
| 1388 |
for (Vertex* v = result->fHead; v; v = v->fNext) { |
| 1389 |
v->fID = id++; |
| 1390 |
} |
| 1391 |
#endif |
| 1392 |
} |
| 1393 |
|
| 1394 |
// Stage 3: sort the vertices by increasing sweep direction. |
| 1395 |
|
| 1396 |
template <CompareFunc sweep_lt> |
| 1397 |
static void merge_sort(VertexList* vertices) { |
| 1398 |
Vertex* slow = vertices->fHead; |
| 1399 |
if (!slow) { |
| 1400 |
return; |
| 1401 |
} |
| 1402 |
Vertex* fast = slow->fNext; |
| 1403 |
if (!fast) { |
| 1404 |
return; |
| 1405 |
} |
| 1406 |
do { |
| 1407 |
fast = fast->fNext; |
| 1408 |
if (fast) { |
| 1409 |
fast = fast->fNext; |
| 1410 |
slow = slow->fNext; |
| 1411 |
} |
| 1412 |
} while (fast); |
| 1413 |
VertexList front(vertices->fHead, slow); |
| 1414 |
VertexList back(slow->fNext, vertices->fTail); |
| 1415 |
front.fTail->fNext = back.fHead->fPrev = nullptr; |
| 1416 |
|
| 1417 |
merge_sort<sweep_lt>(&front); |
| 1418 |
merge_sort<sweep_lt>(&back); |
| 1419 |
|
| 1420 |
vertices->fHead = vertices->fTail = nullptr; |
| 1421 |
sorted_merge<sweep_lt>(&front, &back, vertices); |
| 1422 |
} |
| 1423 |
|
| 1424 |
#if TRIANGULATOR_LOGGING |
| 1425 |
void VertexList::dump() const { |
| 1426 |
for (Vertex* v = fHead; v; v = v->fNext) { |
| 1427 |
TESS_LOG("vertex %g (%g, %g) alpha %d", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
| 1428 |
if (Vertex* p = v->fPartner) { |
| 1429 |
TESS_LOG(", partner %g (%g, %g) alpha %d\n", |
| 1430 |
p->fID, p->fPoint.fX, p->fPoint.fY, p->fAlpha); |
| 1431 |
} else { |
| 1432 |
TESS_LOG(", null partner\n"); |
| 1433 |
} |
| 1434 |
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1435 |
TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1436 |
} |
| 1437 |
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1438 |
TESS_LOG(" edge %g -> %g, winding %d\n", e->fTop->fID, e->fBottom->fID, e->fWinding); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
} |
| 1442 |
#endif |
| 1443 |
|
| 1444 |
#ifdef SK_DEBUG |
| 1445 |
static void validate_edge_pair(Edge* left, Edge* right, const Comparator& c) { |
| 1446 |
if (!left || !right) { |
| 1447 |
return; |
| 1448 |
} |
| 1449 |
if (left->fTop == right->fTop) { |
| 1450 |
SkASSERT(left->isLeftOf(*right->fBottom)); |
| 1451 |
SkASSERT(right->isRightOf(*left->fBottom)); |
| 1452 |
} else if (c.sweep_lt(left->fTop->fPoint, right->fTop->fPoint)) { |
| 1453 |
SkASSERT(left->isLeftOf(*right->fTop)); |
| 1454 |
} else { |
| 1455 |
SkASSERT(right->isRightOf(*left->fTop)); |
| 1456 |
} |
| 1457 |
if (left->fBottom == right->fBottom) { |
| 1458 |
SkASSERT(left->isLeftOf(*right->fTop)); |
| 1459 |
SkASSERT(right->isRightOf(*left->fTop)); |
| 1460 |
} else if (c.sweep_lt(right->fBottom->fPoint, left->fBottom->fPoint)) { |
| 1461 |
SkASSERT(left->isLeftOf(*right->fBottom)); |
| 1462 |
} else { |
| 1463 |
SkASSERT(right->isRightOf(*left->fBottom)); |
| 1464 |
} |
| 1465 |
} |
| 1466 |
|
| 1467 |
static void validate_edge_list(EdgeList* edges, const Comparator& c) { |
| 1468 |
Edge* left = edges->fHead; |
| 1469 |
if (!left) { |
| 1470 |
return; |
| 1471 |
} |
| 1472 |
for (Edge* right = left->fRight; right; right = right->fRight) { |
| 1473 |
validate_edge_pair(left, right, c); |
| 1474 |
left = right; |
| 1475 |
} |
| 1476 |
} |
| 1477 |
#endif |
| 1478 |
|
| 1479 |
// Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| 1480 |
|
| 1481 |
GrTriangulator::SimplifyResult GrTriangulator::simplify(VertexList* mesh, |
| 1482 |
const Comparator& c) { |
| 1483 |
TESS_LOG("simplifying complex polygons\n"); |
| 1484 |
|
| 1485 |
int initialNumEdges = fNumEdges; |
| 1486 |
int initialNumVertices = 0; |
| 1487 |
for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
| 1488 |
++initialNumVertices; |
| 1489 |
} |
| 1490 |
int numSelfIntersections = 0; |
| 1491 |
|
| 1492 |
EdgeList activeEdges; |
| 1493 |
auto result = SimplifyResult::kAlreadySimple; |
| 1494 |
int numVisitedVertices = 0; |
| 1495 |
for (Vertex* v = mesh->fHead; v != nullptr; v = v->fNext) { |
| 1496 |
++numVisitedVertices; |
| 1497 |
if (!v->isConnected()) { |
| 1498 |
continue; |
| 1499 |
} |
| 1500 |
|
| 1501 |
// The max increase across all skps, svgs and gms with only the triangulating and SW path |
| 1502 |
// renderers enabled and with the triangulator's maxVerbCount set to the Chrome value is |
| 1503 |
// 17x. |
| 1504 |
if (fNumEdges > 170*initialNumEdges) { |
| 1505 |
return SimplifyResult::kFailed; |
| 1506 |
} |
| 1507 |
|
| 1508 |
if (numVisitedVertices > 170*initialNumVertices) { |
| 1509 |
return SimplifyResult::kFailed; |
| 1510 |
} |
| 1511 |
|
| 1512 |
Edge* leftEnclosingEdge; |
| 1513 |
Edge* rightEnclosingEdge; |
| 1514 |
bool restartChecks; |
| 1515 |
do { |
| 1516 |
TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", |
| 1517 |
v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
| 1518 |
restartChecks = false; |
| 1519 |
FindEnclosingEdges(*v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1520 |
v->fLeftEnclosingEdge = leftEnclosingEdge; |
| 1521 |
v->fRightEnclosingEdge = rightEnclosingEdge; |
| 1522 |
if (v->fFirstEdgeBelow) { |
| 1523 |
for (Edge* edge = v->fFirstEdgeBelow; edge; edge = edge->fNextEdgeBelow) { |
| 1524 |
BoolFail l = this->checkForIntersection( |
| 1525 |
leftEnclosingEdge, edge, &activeEdges, &v, mesh, c); |
| 1526 |
if (l == BoolFail::kFail) { |
| 1527 |
return SimplifyResult::kFailed; |
| 1528 |
} |
| 1529 |
if (l == BoolFail::kFalse) { |
| 1530 |
BoolFail r = this->checkForIntersection( |
| 1531 |
edge, rightEnclosingEdge, &activeEdges, &v, mesh, c); |
| 1532 |
if (r == BoolFail::kFail) { |
| 1533 |
return SimplifyResult::kFailed; |
| 1534 |
} |
| 1535 |
if (r == BoolFail::kFalse) { |
| 1536 |
// Neither l and r are both false. |
| 1537 |
continue; |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
// Either l or r are true. |
| 1542 |
result = SimplifyResult::kFoundSelfIntersection; |
| 1543 |
restartChecks = true; |
| 1544 |
++numSelfIntersections; |
| 1545 |
break; |
| 1546 |
} // for |
| 1547 |
} else { |
| 1548 |
BoolFail bf = this->checkForIntersection( |
| 1549 |
leftEnclosingEdge, rightEnclosingEdge, &activeEdges, &v, mesh, c); |
| 1550 |
if (bf == BoolFail::kFail) { |
| 1551 |
return SimplifyResult::kFailed; |
| 1552 |
} |
| 1553 |
if (bf == BoolFail::kTrue) { |
| 1554 |
result = SimplifyResult::kFoundSelfIntersection; |
| 1555 |
restartChecks = true; |
| 1556 |
++numSelfIntersections; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
// In pathological cases, a path can intersect itself millions of times. After 500,000 |
| 1561 |
// self-intersections are found, reject the path. |
| 1562 |
if (numSelfIntersections > 500000) { |
| 1563 |
return SimplifyResult::kFailed; |
| 1564 |
} |
| 1565 |
} while (restartChecks); |
| 1566 |
#ifdef SK_DEBUG |
| 1567 |
validate_edge_list(&activeEdges, c); |
| 1568 |
#endif |
| 1569 |
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1570 |
if (!activeEdges.remove(e)) { |
| 1571 |
return SimplifyResult::kFailed; |
| 1572 |
} |
| 1573 |
} |
| 1574 |
Edge* leftEdge = leftEnclosingEdge; |
| 1575 |
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1576 |
activeEdges.insert(e, leftEdge); |
| 1577 |
leftEdge = e; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
SkASSERT(!activeEdges.fHead && !activeEdges.fTail); |
| 1581 |
return result; |
| 1582 |
} |
| 1583 |
|
| 1584 |
// Stage 5: Tessellate the simplified mesh into monotone polygons. |
| 1585 |
|
| 1586 |
std::tuple<Poly*, bool> GrTriangulator::tessellate(const VertexList& vertices, const Comparator&) { |
| 1587 |
TESS_LOG("\ntessellating simple polygons\n"); |
| 1588 |
EdgeList activeEdges; |
| 1589 |
Poly* polys = nullptr; |
| 1590 |
for (Vertex* v = vertices.fHead; v != nullptr; v = v->fNext) { |
| 1591 |
if (!v->isConnected()) { |
| 1592 |
continue; |
| 1593 |
} |
| 1594 |
#if TRIANGULATOR_LOGGING |
| 1595 |
TESS_LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha); |
| 1596 |
#endif |
| 1597 |
Edge* leftEnclosingEdge; |
| 1598 |
Edge* rightEnclosingEdge; |
| 1599 |
FindEnclosingEdges(*v, activeEdges, &leftEnclosingEdge, &rightEnclosingEdge); |
| 1600 |
Poly* leftPoly; |
| 1601 |
Poly* rightPoly; |
| 1602 |
if (v->fFirstEdgeAbove) { |
| 1603 |
leftPoly = v->fFirstEdgeAbove->fLeftPoly; |
| 1604 |
rightPoly = v->fLastEdgeAbove->fRightPoly; |
| 1605 |
} else { |
| 1606 |
leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; |
| 1607 |
rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; |
| 1608 |
} |
| 1609 |
#if TRIANGULATOR_LOGGING |
| 1610 |
TESS_LOG("edges above:\n"); |
| 1611 |
for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| 1612 |
TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1613 |
e->fTop->fID, e->fBottom->fID, |
| 1614 |
e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1615 |
e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1616 |
} |
| 1617 |
TESS_LOG("edges below:\n"); |
| 1618 |
for (Edge* e = v->fFirstEdgeBelow; e; e = e->fNextEdgeBelow) { |
| 1619 |
TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1620 |
e->fTop->fID, e->fBottom->fID, |
| 1621 |
e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1622 |
e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1623 |
} |
| 1624 |
#endif |
| 1625 |
if (v->fFirstEdgeAbove) { |
| 1626 |
if (leftPoly) { |
| 1627 |
leftPoly = leftPoly->addEdge(v->fFirstEdgeAbove, Side::kRight, this); |
| 1628 |
} |
| 1629 |
if (rightPoly) { |
| 1630 |
rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Side::kLeft, this); |
| 1631 |
} |
| 1632 |
for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) { |
| 1633 |
Edge* rightEdge = e->fNextEdgeAbove; |
| 1634 |
activeEdges.remove(e); |
| 1635 |
if (e->fRightPoly) { |
| 1636 |
e->fRightPoly->addEdge(e, Side::kLeft, this); |
| 1637 |
} |
| 1638 |
if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) { |
| 1639 |
rightEdge->fLeftPoly->addEdge(e, Side::kRight, this); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
activeEdges.remove(v->fLastEdgeAbove); |
| 1643 |
if (!v->fFirstEdgeBelow) { |
| 1644 |
if (leftPoly && rightPoly && leftPoly != rightPoly) { |
| 1645 |
SkASSERT(leftPoly->fPartner == nullptr && rightPoly->fPartner == nullptr); |
| 1646 |
rightPoly->fPartner = leftPoly; |
| 1647 |
leftPoly->fPartner = rightPoly; |
| 1648 |
} |
| 1649 |
} |
| 1650 |
} |
| 1651 |
if (v->fFirstEdgeBelow) { |
| 1652 |
if (!v->fFirstEdgeAbove) { |
| 1653 |
if (leftPoly && rightPoly) { |
| 1654 |
if (leftPoly == rightPoly) { |
| 1655 |
if (leftPoly->fTail && leftPoly->fTail->fSide == Side::kLeft) { |
| 1656 |
leftPoly = this->makePoly(&polys, leftPoly->lastVertex(), |
| 1657 |
leftPoly->fWinding); |
| 1658 |
leftEnclosingEdge->fRightPoly = leftPoly; |
| 1659 |
} else { |
| 1660 |
rightPoly = this->makePoly(&polys, rightPoly->lastVertex(), |
| 1661 |
rightPoly->fWinding); |
| 1662 |
rightEnclosingEdge->fLeftPoly = rightPoly; |
| 1663 |
} |
| 1664 |
} |
| 1665 |
Edge* join = this->allocateEdge(leftPoly->lastVertex(), v, 1, EdgeType::kInner); |
| 1666 |
leftPoly = leftPoly->addEdge(join, Side::kRight, this); |
| 1667 |
rightPoly = rightPoly->addEdge(join, Side::kLeft, this); |
| 1668 |
} |
| 1669 |
} |
| 1670 |
Edge* leftEdge = v->fFirstEdgeBelow; |
| 1671 |
leftEdge->fLeftPoly = leftPoly; |
| 1672 |
activeEdges.insert(leftEdge, leftEnclosingEdge); |
| 1673 |
for (Edge* rightEdge = leftEdge->fNextEdgeBelow; rightEdge; |
| 1674 |
rightEdge = rightEdge->fNextEdgeBelow) { |
| 1675 |
activeEdges.insert(rightEdge, leftEdge); |
| 1676 |
int winding = leftEdge->fLeftPoly ? leftEdge->fLeftPoly->fWinding : 0; |
| 1677 |
winding += leftEdge->fWinding; |
| 1678 |
if (winding != 0) { |
| 1679 |
Poly* poly = this->makePoly(&polys, v, winding); |
| 1680 |
leftEdge->fRightPoly = rightEdge->fLeftPoly = poly; |
| 1681 |
} |
| 1682 |
leftEdge = rightEdge; |
| 1683 |
} |
| 1684 |
v->fLastEdgeBelow->fRightPoly = rightPoly; |
| 1685 |
} |
| 1686 |
#if TRIANGULATOR_LOGGING |
| 1687 |
TESS_LOG("\nactive edges:\n"); |
| 1688 |
for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { |
| 1689 |
TESS_LOG("%g -> %g, lpoly %d, rpoly %d\n", |
| 1690 |
e->fTop->fID, e->fBottom->fID, |
| 1691 |
e->fLeftPoly ? e->fLeftPoly->fID : -1, |
| 1692 |
e->fRightPoly ? e->fRightPoly->fID : -1); |
| 1693 |
} |
| 1694 |
#endif |
| 1695 |
} |
| 1696 |
return { polys, true }; |
| 1697 |
} |
| 1698 |
|
| 1699 |
// This is a driver function that calls stages 2-5 in turn. |
| 1700 |
|
| 1701 |
void GrTriangulator::contoursToMesh(VertexList* contours, int contourCnt, VertexList* mesh, |
| 1702 |
const Comparator& c) { |
| 1703 |
#if TRIANGULATOR_LOGGING |
| 1704 |
for (int i = 0; i < contourCnt; ++i) { |
| 1705 |
Vertex* v = contours[i].fHead; |
| 1706 |
SkASSERT(v); |
| 1707 |
TESS_LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 1708 |
for (v = v->fNext; v; v = v->fNext) { |
| 1709 |
TESS_LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
#endif |
| 1713 |
this->sanitizeContours(contours, contourCnt); |
| 1714 |
this->buildEdges(contours, contourCnt, mesh, c); |
| 1715 |
} |
| 1716 |
|
| 1717 |
void GrTriangulator::SortMesh(VertexList* vertices, const Comparator& c) { |
| 1718 |
if (!vertices || !vertices->fHead) { |
| 1719 |
return; |
| 1720 |
} |
| 1721 |
|
| 1722 |
// Sort vertices in Y (secondarily in X). |
| 1723 |
if (c.fDirection == Comparator::Direction::kHorizontal) { |
| 1724 |
merge_sort<sweep_lt_horiz>(vertices); |
| 1725 |
} else { |
| 1726 |
merge_sort<sweep_lt_vert>(vertices); |
| 1727 |
} |
| 1728 |
#if TRIANGULATOR_LOGGING |
| 1729 |
for (Vertex* v = vertices->fHead; v != nullptr; v = v->fNext) { |
| 1730 |
static float gID = 0.0f; |
| 1731 |
v->fID = gID++; |
| 1732 |
} |
| 1733 |
#endif |
| 1734 |
} |
| 1735 |
|
| 1736 |
std::tuple<Poly*, bool> GrTriangulator::contoursToPolys(VertexList* contours, int contourCnt) { |
| 1737 |
const SkRect& pathBounds = fPath.getBounds(); |
| 1738 |
Comparator c(pathBounds.width() > pathBounds.height() ? Comparator::Direction::kHorizontal |
| 1739 |
: Comparator::Direction::kVertical); |
| 1740 |
VertexList mesh; |
| 1741 |
this->contoursToMesh(contours, contourCnt, &mesh, c); |
| 1742 |
TESS_LOG("\ninitial mesh:\n"); |
| 1743 |
DUMP_MESH(mesh); |
| 1744 |
SortMesh(&mesh, c); |
| 1745 |
TESS_LOG("\nsorted mesh:\n"); |
| 1746 |
DUMP_MESH(mesh); |
| 1747 |
this->mergeCoincidentVertices(&mesh, c); |
| 1748 |
TESS_LOG("\nsorted+merged mesh:\n"); |
| 1749 |
DUMP_MESH(mesh); |
| 1750 |
auto result = this->simplify(&mesh, c); |
| 1751 |
if (result == SimplifyResult::kFailed) { |
| 1752 |
return { nullptr, false }; |
| 1753 |
} |
| 1754 |
TESS_LOG("\nsimplified mesh:\n"); |
| 1755 |
DUMP_MESH(mesh); |
| 1756 |
return this->tessellate(mesh, c); |
| 1757 |
} |
| 1758 |
|
| 1759 |
// Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 1760 |
skgpu::VertexWriter GrTriangulator::polysToTriangles(Poly* polys, |
| 1761 |
SkPathFillType overrideFillType, |
| 1762 |
skgpu::VertexWriter data) const { |
| 1763 |
for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1764 |
if (apply_fill_type(overrideFillType, poly)) { |
| 1765 |
data = this->emitPoly(poly, std::move(data)); |
| 1766 |
} |
| 1767 |
} |
| 1768 |
return data; |
| 1769 |
} |
| 1770 |
|
| 1771 |
static int get_contour_count(const SkPath& path, SkScalar tolerance) { |
| 1772 |
// We could theoretically be more aggressive about not counting empty contours, but we need to |
| 1773 |
// actually match the exact number of contour linked lists the tessellator will create later on. |
| 1774 |
int contourCnt = 1; |
| 1775 |
bool hasPoints = false; |
| 1776 |
|
| 1777 |
SkPath::Iter iter(path, false); |
| 1778 |
bool first = true; |
| 1779 |
while (auto rec = iter.next()) { |
| 1780 |
switch (rec->fVerb) { |
| 1781 |
case SkPathVerb::kMove: |
| 1782 |
if (!first) { |
| 1783 |
++contourCnt; |
| 1784 |
} |
| 1785 |
[[fallthrough]]; |
| 1786 |
case SkPathVerb::kLine: |
| 1787 |
case SkPathVerb::kConic: |
| 1788 |
case SkPathVerb::kQuad: |
| 1789 |
case SkPathVerb::kCubic: |
| 1790 |
hasPoints = true; |
| 1791 |
break; |
| 1792 |
case SkPathVerb::kClose: |
| 1793 |
break; |
| 1794 |
} |
| 1795 |
first = false; |
| 1796 |
} |
| 1797 |
if (!hasPoints) { |
| 1798 |
return 0; |
| 1799 |
} |
| 1800 |
return contourCnt; |
| 1801 |
} |
| 1802 |
|
| 1803 |
std::tuple<Poly*, bool> GrTriangulator::pathToPolys(float tolerance, const SkRect& clipBounds, bool* isLinear) { |
| 1804 |
int contourCnt = get_contour_count(fPath, tolerance); |
| 1805 |
if (contourCnt <= 0) { |
| 1806 |
*isLinear = true; |
| 1807 |
return { nullptr, true }; |
| 1808 |
} |
| 1809 |
|
| 1810 |
if (SkPathFillType_IsInverse(fPath.getFillType())) { |
| 1811 |
contourCnt++; |
| 1812 |
} |
| 1813 |
std::unique_ptr<VertexList[]> contours(new VertexList[contourCnt]); |
| 1814 |
|
| 1815 |
this->pathToContours(tolerance, clipBounds, contours.get(), isLinear); |
| 1816 |
return this->contoursToPolys(contours.get(), contourCnt); |
| 1817 |
} |
| 1818 |
|
| 1819 |
int64_t GrTriangulator::CountPoints(Poly* polys, SkPathFillType overrideFillType) { |
| 1820 |
int64_t count = 0; |
| 1821 |
for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1822 |
if (apply_fill_type(overrideFillType, poly) && poly->fCount >= 3) { |
| 1823 |
count += (poly->fCount - 2) * (TRIANGULATOR_WIREFRAME ? 6 : 3); |
| 1824 |
} |
| 1825 |
} |
| 1826 |
return count; |
| 1827 |
} |
| 1828 |
|
| 1829 |
// Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| 1830 |
|
| 1831 |
int GrTriangulator::polysToTriangles(Poly* polys, GrEagerVertexAllocator* vertexAllocator) const { |
| 1832 |
int64_t count64 = CountPoints(polys, fPath.getFillType()); |
| 1833 |
if (0 == count64 || count64 > SK_MaxS32) { |
| 1834 |
return 0; |
| 1835 |
} |
| 1836 |
int count = count64; |
| 1837 |
|
| 1838 |
size_t vertexStride = sizeof(SkPoint); |
| 1839 |
if (fEmitCoverage) { |
| 1840 |
vertexStride += sizeof(float); |
| 1841 |
} |
| 1842 |
skgpu::VertexWriter verts = vertexAllocator->lockWriter(vertexStride, count); |
| 1843 |
if (!verts) { |
| 1844 |
SkDebugf("Could not allocate vertices\n"); |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
|
| 1848 |
TESS_LOG("emitting %d verts\n", count); |
| 1849 |
|
| 1850 |
skgpu::BufferWriter::Mark start = verts.mark(); |
| 1851 |
verts = this->polysToTriangles(polys, fPath.getFillType(), std::move(verts)); |
| 1852 |
|
| 1853 |
int actualCount = static_cast<int>((verts.mark() - start) / vertexStride); |
| 1854 |
SkASSERT(actualCount <= count); |
| 1855 |
vertexAllocator->unlock(actualCount); |
| 1856 |
return actualCount; |
| 1857 |
} |
| 1858 |
|
| 1859 |
#endif // SK_ENABLE_OPTIMIZE_SIZE |