| 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/GrAAConvexTessellator.h" |
| 9 |
|
| 10 |
#include "include/core/SkMatrix.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPoint.h" |
| 13 |
#include "include/core/SkRect.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "include/private/base/SkFloatingPoint.h" |
| 16 |
#include "include/private/base/SkTPin.h" |
| 17 |
#include "src/core/SkPathPriv.h" |
| 18 |
#include "src/gpu/ganesh/geometry/GrPathUtils.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
|
| 22 |
// Next steps: |
| 23 |
// add an interactive sample app slide |
| 24 |
// add debug check that all points are suitably far apart |
| 25 |
// test more degenerate cases |
| 26 |
|
| 27 |
// The tolerance for fusing vertices and eliminating colinear lines (It is in device space). |
| 28 |
static constexpr SkScalar kClose = (SK_Scalar1 / 16); |
| 29 |
static constexpr SkScalar kCloseSqd = kClose * kClose; |
| 30 |
|
| 31 |
// tesselation tolerance values, in device space pixels |
| 32 |
static constexpr SkScalar kQuadTolerance = 0.2f; |
| 33 |
static constexpr SkScalar kCubicTolerance = 0.2f; |
| 34 |
static constexpr SkScalar kQuadToleranceSqd = kQuadTolerance * kQuadTolerance; |
| 35 |
static constexpr SkScalar kCubicToleranceSqd = kCubicTolerance * kCubicTolerance; |
| 36 |
static constexpr SkScalar kConicTolerance = 0.25f; |
| 37 |
|
| 38 |
// dot product below which we use a round cap between curve segments |
| 39 |
static constexpr SkScalar kRoundCapThreshold = 0.8f; |
| 40 |
|
| 41 |
// dot product above which we consider two adjacent curves to be part of the "same" curve |
| 42 |
static constexpr SkScalar kCurveConnectionThreshold = 0.8f; |
| 43 |
|
| 44 |
static bool intersect(const SkPoint& p0, const SkPoint& n0, |
| 45 |
const SkPoint& p1, const SkPoint& n1, |
| 46 |
SkScalar* t) { |
| 47 |
const SkPoint v = p1 - p0; |
| 48 |
SkScalar perpDot = n0.fX * n1.fY - n0.fY * n1.fX; |
| 49 |
if (SkScalarNearlyZero(perpDot)) { |
| 50 |
return false; |
| 51 |
} |
| 52 |
*t = (v.fX * n1.fY - v.fY * n1.fX) / perpDot; |
| 53 |
return SkIsFinite(*t); |
| 54 |
} |
| 55 |
|
| 56 |
// This is a special case version of intersect where we have the vector |
| 57 |
// perpendicular to the second line rather than the vector parallel to it. |
| 58 |
static bool perp_intersect(const SkPoint& p0, const SkPoint& n0, |
| 59 |
const SkPoint& p1, const SkPoint& perp, |
| 60 |
SkScalar* t) { |
| 61 |
const SkPoint v = p1 - p0; |
| 62 |
SkScalar perpDot = n0.dot(perp); |
| 63 |
if (SkScalarNearlyZero(perpDot)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
*t = v.dot(perp) / perpDot; |
| 67 |
return SkIsFinite(*t); |
| 68 |
} |
| 69 |
|
| 70 |
static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) { |
| 71 |
SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1); |
| 72 |
return distSq < kCloseSqd; |
| 73 |
} |
| 74 |
|
| 75 |
static bool points_are_colinear_and_b_is_middle(const SkPoint& a, const SkPoint& b, |
| 76 |
const SkPoint& c, float* accumError) { |
| 77 |
// First check distance from b to the infinite line through a, c |
| 78 |
SkVector aToC = c - a; |
| 79 |
SkVector n = {aToC.fY, -aToC.fX}; |
| 80 |
n.normalize(); |
| 81 |
|
| 82 |
SkScalar distBToLineAC = SkScalarAbs(n.dot(b) - n.dot(a)); |
| 83 |
if (*accumError + distBToLineAC >= kClose || aToC.dot(b - a) <= 0.f || aToC.dot(c - b) <= 0.f) { |
| 84 |
// Too far from the line or not between the line segment from a to c |
| 85 |
return false; |
| 86 |
} else { |
| 87 |
// Accumulate the distance from b to |ac| that goes "away" when this near-colinear point |
| 88 |
// is removed to simplify the path. |
| 89 |
*accumError += distBToLineAC; |
| 90 |
return true; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
int GrAAConvexTessellator::addPt(const SkPoint& pt, |
| 95 |
SkScalar depth, |
| 96 |
SkScalar coverage, |
| 97 |
bool movable, |
| 98 |
CurveState curve) { |
| 99 |
SkASSERT(pt.isFinite()); |
| 100 |
this->validate(); |
| 101 |
|
| 102 |
int index = fPts.size(); |
| 103 |
*fPts.append() = pt; |
| 104 |
*fCoverages.append() = coverage; |
| 105 |
*fMovable.append() = movable; |
| 106 |
*fCurveState.append() = curve; |
| 107 |
|
| 108 |
this->validate(); |
| 109 |
return index; |
| 110 |
} |
| 111 |
|
| 112 |
void GrAAConvexTessellator::popLastPt() { |
| 113 |
this->validate(); |
| 114 |
|
| 115 |
fPts.pop_back(); |
| 116 |
fCoverages.pop_back(); |
| 117 |
fMovable.pop_back(); |
| 118 |
fCurveState.pop_back(); |
| 119 |
|
| 120 |
this->validate(); |
| 121 |
} |
| 122 |
|
| 123 |
void GrAAConvexTessellator::popFirstPtShuffle() { |
| 124 |
this->validate(); |
| 125 |
|
| 126 |
fPts.removeShuffle(0); |
| 127 |
fCoverages.removeShuffle(0); |
| 128 |
fMovable.removeShuffle(0); |
| 129 |
fCurveState.removeShuffle(0); |
| 130 |
|
| 131 |
this->validate(); |
| 132 |
} |
| 133 |
|
| 134 |
void GrAAConvexTessellator::updatePt(int index, |
| 135 |
const SkPoint& pt, |
| 136 |
SkScalar depth, |
| 137 |
SkScalar coverage) { |
| 138 |
this->validate(); |
| 139 |
SkASSERT(fMovable[index]); |
| 140 |
|
| 141 |
fPts[index] = pt; |
| 142 |
fCoverages[index] = coverage; |
| 143 |
} |
| 144 |
|
| 145 |
void GrAAConvexTessellator::addTri(int i0, int i1, int i2) { |
| 146 |
if (i0 == i1 || i1 == i2 || i2 == i0) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
*fIndices.append() = i0; |
| 151 |
*fIndices.append() = i1; |
| 152 |
*fIndices.append() = i2; |
| 153 |
} |
| 154 |
|
| 155 |
void GrAAConvexTessellator::rewind() { |
| 156 |
fPts.clear(); |
| 157 |
fCoverages.clear(); |
| 158 |
fMovable.clear(); |
| 159 |
fIndices.clear(); |
| 160 |
fNorms.clear(); |
| 161 |
fCurveState.clear(); |
| 162 |
fInitialRing.rewind(); |
| 163 |
fCandidateVerts.rewind(); |
| 164 |
#if GR_AA_CONVEX_TESSELLATOR_VIZ |
| 165 |
fRings.rewind(); // TODO: leak in this case! |
| 166 |
#else |
| 167 |
fRings[0].rewind(); |
| 168 |
fRings[1].rewind(); |
| 169 |
#endif |
| 170 |
} |
| 171 |
|
| 172 |
void GrAAConvexTessellator::computeNormals() { |
| 173 |
auto normalToVector = [this](SkVector v) { |
| 174 |
SkVector n = SkPointPriv::MakeOrthog(v, fSide); |
| 175 |
SkAssertResult(n.normalize()); |
| 176 |
SkASSERT(SkScalarNearlyEqual(1.0f, n.length())); |
| 177 |
return n; |
| 178 |
}; |
| 179 |
|
| 180 |
// Check the cross product of the final trio |
| 181 |
fNorms.append(fPts.size()); |
| 182 |
fNorms[0] = fPts[1] - fPts[0]; |
| 183 |
fNorms.back() = fPts[0] - fPts.back(); |
| 184 |
SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.back()); |
| 185 |
fSide = (cross > 0.0f) ? SkPointPriv::kRight_Side : SkPointPriv::kLeft_Side; |
| 186 |
fNorms[0] = normalToVector(fNorms[0]); |
| 187 |
for (int cur = 1; cur < fNorms.size() - 1; ++cur) { |
| 188 |
fNorms[cur] = normalToVector(fPts[cur + 1] - fPts[cur]); |
| 189 |
} |
| 190 |
fNorms.back() = normalToVector(fNorms.back()); |
| 191 |
} |
| 192 |
|
| 193 |
void GrAAConvexTessellator::computeBisectors() { |
| 194 |
fBisectors.resize(fNorms.size()); |
| 195 |
|
| 196 |
int prev = fBisectors.size() - 1; |
| 197 |
for (int cur = 0; cur < fBisectors.size(); prev = cur, ++cur) { |
| 198 |
fBisectors[cur] = fNorms[cur] + fNorms[prev]; |
| 199 |
if (!fBisectors[cur].normalize()) { |
| 200 |
fBisectors[cur] = SkPointPriv::MakeOrthog(fNorms[cur], (SkPointPriv::Side)-fSide) + |
| 201 |
SkPointPriv::MakeOrthog(fNorms[prev], fSide); |
| 202 |
SkAssertResult(fBisectors[cur].normalize()); |
| 203 |
} else { |
| 204 |
fBisectors[cur].negate(); // make the bisector face in |
| 205 |
} |
| 206 |
if (fCurveState[prev] == kIndeterminate_CurveState) { |
| 207 |
if (fCurveState[cur] == kSharp_CurveState) { |
| 208 |
fCurveState[prev] = kSharp_CurveState; |
| 209 |
} else { |
| 210 |
if (SkScalarAbs(fNorms[cur].dot(fNorms[prev])) > kCurveConnectionThreshold) { |
| 211 |
fCurveState[prev] = kCurve_CurveState; |
| 212 |
fCurveState[cur] = kCurve_CurveState; |
| 213 |
} else { |
| 214 |
fCurveState[prev] = kSharp_CurveState; |
| 215 |
fCurveState[cur] = kSharp_CurveState; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
SkASSERT(SkScalarNearlyEqual(1.0f, fBisectors[cur].length())); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
// Create as many rings as we need to (up to a predefined limit) to reach the specified target |
| 225 |
// depth. If we are in fill mode, the final ring will automatically be fanned. |
| 226 |
bool GrAAConvexTessellator::createInsetRings(Ring& previousRing, SkScalar initialDepth, |
| 227 |
SkScalar initialCoverage, SkScalar targetDepth, |
| 228 |
SkScalar targetCoverage, Ring** finalRing) { |
| 229 |
static const int kMaxNumRings = 8; |
| 230 |
|
| 231 |
if (previousRing.numPts() < 3) { |
| 232 |
return false; |
| 233 |
} |
| 234 |
Ring* currentRing = &previousRing; |
| 235 |
int i; |
| 236 |
for (i = 0; i < kMaxNumRings; ++i) { |
| 237 |
Ring* nextRing = this->getNextRing(currentRing); |
| 238 |
SkASSERT(nextRing != currentRing); |
| 239 |
|
| 240 |
bool done = this->createInsetRing(*currentRing, nextRing, initialDepth, initialCoverage, |
| 241 |
targetDepth, targetCoverage, i == 0); |
| 242 |
currentRing = nextRing; |
| 243 |
if (done) { |
| 244 |
break; |
| 245 |
} |
| 246 |
currentRing->init(*this); |
| 247 |
} |
| 248 |
|
| 249 |
if (kMaxNumRings == i) { |
| 250 |
// Bail if we've exceeded the amount of time we want to throw at this. |
| 251 |
this->terminate(*currentRing); |
| 252 |
return false; |
| 253 |
} |
| 254 |
bool done = currentRing->numPts() >= 3; |
| 255 |
if (done) { |
| 256 |
currentRing->init(*this); |
| 257 |
} |
| 258 |
*finalRing = currentRing; |
| 259 |
return done; |
| 260 |
} |
| 261 |
|
| 262 |
// The general idea here is to, conceptually, start with the original polygon and slide |
| 263 |
// the vertices along the bisectors until the first intersection. At that |
| 264 |
// point two of the edges collapse and the process repeats on the new polygon. |
| 265 |
// The polygon state is captured in the Ring class while the GrAAConvexTessellator |
| 266 |
// controls the iteration. The CandidateVerts holds the formative points for the |
| 267 |
// next ring. |
| 268 |
bool GrAAConvexTessellator::tessellate(const SkMatrix& m, const SkPath& path) { |
| 269 |
if (!this->extractFromPath(m, path)) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
SkScalar coverage = 1.0f; |
| 274 |
SkScalar scaleFactor = 0.0f; |
| 275 |
|
| 276 |
if (SkStrokeRec::kStrokeAndFill_Style == fStyle) { |
| 277 |
SkASSERT(m.isSimilarity()); |
| 278 |
scaleFactor = m.getMaxScale(); // x and y scale are the same |
| 279 |
SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth; |
| 280 |
Ring outerStrokeAndAARing; |
| 281 |
this->createOuterRing(fInitialRing, |
| 282 |
effectiveStrokeWidth / 2 + kAntialiasingRadius, 0.0, |
| 283 |
&outerStrokeAndAARing); |
| 284 |
|
| 285 |
// discard all the triangles added between the originating ring and the new outer ring |
| 286 |
fIndices.clear(); |
| 287 |
|
| 288 |
outerStrokeAndAARing.init(*this); |
| 289 |
|
| 290 |
outerStrokeAndAARing.makeOriginalRing(); |
| 291 |
|
| 292 |
// Add the outer stroke ring's normals to the originating ring's normals |
| 293 |
// so it can also act as an originating ring |
| 294 |
fNorms.resize(fNorms.size() + outerStrokeAndAARing.numPts()); |
| 295 |
for (int i = 0; i < outerStrokeAndAARing.numPts(); ++i) { |
| 296 |
SkASSERT(outerStrokeAndAARing.index(i) < fNorms.size()); |
| 297 |
fNorms[outerStrokeAndAARing.index(i)] = outerStrokeAndAARing.norm(i); |
| 298 |
} |
| 299 |
|
| 300 |
// the bisectors are only needed for the computation of the outer ring |
| 301 |
fBisectors.clear(); |
| 302 |
|
| 303 |
Ring* insetAARing; |
| 304 |
this->createInsetRings(outerStrokeAndAARing, |
| 305 |
0.0f, 0.0f, 2*kAntialiasingRadius, 1.0f, |
| 306 |
&insetAARing); |
| 307 |
|
| 308 |
SkDEBUGCODE(this->validate();) |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
if (SkStrokeRec::kStroke_Style == fStyle) { |
| 313 |
SkASSERT(fStrokeWidth >= 0.0f); |
| 314 |
SkASSERT(m.isSimilarity()); |
| 315 |
scaleFactor = m.getMaxScale(); // x and y scale are the same |
| 316 |
SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth; |
| 317 |
Ring outerStrokeRing; |
| 318 |
this->createOuterRing(fInitialRing, effectiveStrokeWidth / 2 - kAntialiasingRadius, |
| 319 |
coverage, &outerStrokeRing); |
| 320 |
outerStrokeRing.init(*this); |
| 321 |
Ring outerAARing; |
| 322 |
this->createOuterRing(outerStrokeRing, kAntialiasingRadius * 2, 0.0f, &outerAARing); |
| 323 |
} else { |
| 324 |
Ring outerAARing; |
| 325 |
this->createOuterRing(fInitialRing, kAntialiasingRadius, 0.0f, &outerAARing); |
| 326 |
} |
| 327 |
|
| 328 |
// the bisectors are only needed for the computation of the outer ring |
| 329 |
fBisectors.clear(); |
| 330 |
if (SkStrokeRec::kStroke_Style == fStyle && fInitialRing.numPts() > 2) { |
| 331 |
SkASSERT(fStrokeWidth >= 0.0f); |
| 332 |
SkScalar effectiveStrokeWidth = scaleFactor * fStrokeWidth; |
| 333 |
Ring* insetStrokeRing; |
| 334 |
SkScalar strokeDepth = effectiveStrokeWidth / 2 - kAntialiasingRadius; |
| 335 |
if (this->createInsetRings(fInitialRing, 0.0f, coverage, strokeDepth, coverage, |
| 336 |
&insetStrokeRing)) { |
| 337 |
Ring* insetAARing; |
| 338 |
this->createInsetRings(*insetStrokeRing, strokeDepth, coverage, strokeDepth + |
| 339 |
kAntialiasingRadius * 2, 0.0f, &insetAARing); |
| 340 |
} |
| 341 |
} else { |
| 342 |
Ring* insetAARing; |
| 343 |
this->createInsetRings(fInitialRing, 0.0f, 0.5f, kAntialiasingRadius, 1.0f, &insetAARing); |
| 344 |
} |
| 345 |
|
| 346 |
SkDEBUGCODE(this->validate();) |
| 347 |
return true; |
| 348 |
} |
| 349 |
|
| 350 |
SkScalar GrAAConvexTessellator::computeDepthFromEdge(int edgeIdx, const SkPoint& p) const { |
| 351 |
SkASSERT(edgeIdx < fNorms.size()); |
| 352 |
|
| 353 |
SkPoint v = p - fPts[edgeIdx]; |
| 354 |
SkScalar depth = -fNorms[edgeIdx].dot(v); |
| 355 |
return depth; |
| 356 |
} |
| 357 |
|
| 358 |
// Find a point that is 'desiredDepth' away from the 'edgeIdx'-th edge and lies |
| 359 |
// along the 'bisector' from the 'startIdx'-th point. |
| 360 |
bool GrAAConvexTessellator::computePtAlongBisector(int startIdx, |
| 361 |
const SkVector& bisector, |
| 362 |
int edgeIdx, |
| 363 |
SkScalar desiredDepth, |
| 364 |
SkPoint* result) const { |
| 365 |
const SkPoint& norm = fNorms[edgeIdx]; |
| 366 |
|
| 367 |
// First find the point where the edge and the bisector intersect |
| 368 |
SkPoint newP; |
| 369 |
|
| 370 |
SkScalar t; |
| 371 |
if (!perp_intersect(fPts[startIdx], bisector, fPts[edgeIdx], norm, &t)) { |
| 372 |
return false; |
| 373 |
} |
| 374 |
if (SkScalarNearlyEqual(t, 0.0f)) { |
| 375 |
// the start point was one of the original ring points |
| 376 |
SkASSERT(startIdx < fPts.size()); |
| 377 |
newP = fPts[startIdx]; |
| 378 |
} else if (t < 0.0f) { |
| 379 |
newP = bisector; |
| 380 |
newP.scale(t); |
| 381 |
newP += fPts[startIdx]; |
| 382 |
} else { |
| 383 |
return false; |
| 384 |
} |
| 385 |
|
| 386 |
// Then offset along the bisector from that point the correct distance |
| 387 |
SkScalar dot = bisector.dot(norm); |
| 388 |
t = -desiredDepth / dot; |
| 389 |
*result = bisector; |
| 390 |
result->scale(t); |
| 391 |
*result += newP; |
| 392 |
|
| 393 |
return true; |
| 394 |
} |
| 395 |
|
| 396 |
bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& path) { |
| 397 |
SkASSERT(path.isConvex()); |
| 398 |
|
| 399 |
SkRect bounds = path.getBounds(); |
| 400 |
m.mapRect(&bounds); |
| 401 |
if (!bounds.isFinite()) { |
| 402 |
// We could do something smarter here like clip the path based on the bounds of the dst. |
| 403 |
// We'd have to be careful about strokes to ensure we don't draw something wrong. |
| 404 |
return false; |
| 405 |
} |
| 406 |
|
| 407 |
// Outer ring: 3*numPts |
| 408 |
// Middle ring: numPts |
| 409 |
// Presumptive inner ring: numPts |
| 410 |
this->reservePts(5*path.countPoints()); |
| 411 |
// Outer ring: 12*numPts |
| 412 |
// Middle ring: 0 |
| 413 |
// Presumptive inner ring: 6*numPts + 6 |
| 414 |
fIndices.reserve(18*path.countPoints() + 6); |
| 415 |
|
| 416 |
// Reset the accumulated error for all the future lineTo() calls when iterating over the path. |
| 417 |
fAccumLinearError = 0.f; |
| 418 |
// TODO: is there a faster way to extract the points from the path? Perhaps |
| 419 |
// get all the points via a new entry point, transform them all in bulk |
| 420 |
// and then walk them to find duplicates? |
| 421 |
SkPathEdgeIter iter(path); |
| 422 |
while (auto e = iter.next()) { |
| 423 |
switch (e.fEdge) { |
| 424 |
case SkPathEdgeIter::Edge::kLine: |
| 425 |
if (!SkPathPriv::AllPointsEq({e.fPts, 2})) { |
| 426 |
this->lineTo(m, e.fPts[1], kSharp_CurveState); |
| 427 |
} |
| 428 |
break; |
| 429 |
case SkPathEdgeIter::Edge::kQuad: |
| 430 |
if (!SkPathPriv::AllPointsEq({e.fPts, 3})) { |
| 431 |
this->quadTo(m, e.fPts); |
| 432 |
} |
| 433 |
break; |
| 434 |
case SkPathEdgeIter::Edge::kCubic: |
| 435 |
if (!SkPathPriv::AllPointsEq({e.fPts, 4})) { |
| 436 |
this->cubicTo(m, e.fPts); |
| 437 |
} |
| 438 |
break; |
| 439 |
case SkPathEdgeIter::Edge::kConic: |
| 440 |
if (!SkPathPriv::AllPointsEq({e.fPts, 3})) { |
| 441 |
this->conicTo(m, e.fPts, iter.conicWeight()); |
| 442 |
} |
| 443 |
break; |
| 444 |
default: |
| 445 |
SkDEBUGFAIL("Unknown edge type"); |
| 446 |
break; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if (this->numPts() < 2) { |
| 451 |
return false; |
| 452 |
} |
| 453 |
|
| 454 |
// check if last point is a duplicate of the first point. If so, remove it. |
| 455 |
if (duplicate_pt(fPts[this->numPts()-1], fPts[0])) { |
| 456 |
this->popLastPt(); |
| 457 |
} |
| 458 |
|
| 459 |
// Remove any lingering colinear points where the path wraps around |
| 460 |
fAccumLinearError = 0.f; |
| 461 |
bool noRemovalsToDo = false; |
| 462 |
while (!noRemovalsToDo && this->numPts() >= 3) { |
| 463 |
if (points_are_colinear_and_b_is_middle(fPts[fPts.size() - 2], fPts.back(), fPts[0], |
| 464 |
&fAccumLinearError)) { |
| 465 |
this->popLastPt(); |
| 466 |
} else if (points_are_colinear_and_b_is_middle(fPts.back(), fPts[0], fPts[1], |
| 467 |
&fAccumLinearError)) { |
| 468 |
this->popFirstPtShuffle(); |
| 469 |
} else { |
| 470 |
noRemovalsToDo = true; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
// Compute the normals and bisectors. |
| 475 |
SkASSERT(fNorms.empty()); |
| 476 |
if (this->numPts() >= 3) { |
| 477 |
this->computeNormals(); |
| 478 |
this->computeBisectors(); |
| 479 |
} else if (this->numPts() == 2) { |
| 480 |
// We've got two points, so we're degenerate. |
| 481 |
if (fStyle == SkStrokeRec::kFill_Style) { |
| 482 |
// it's a fill, so we don't need to worry about degenerate paths |
| 483 |
return false; |
| 484 |
} |
| 485 |
// For stroking, we still need to process the degenerate path, so fix it up |
| 486 |
fSide = SkPointPriv::kLeft_Side; |
| 487 |
|
| 488 |
fNorms.append(2); |
| 489 |
fNorms[0] = SkPointPriv::MakeOrthog(fPts[1] - fPts[0], fSide); |
| 490 |
fNorms[0].normalize(); |
| 491 |
fNorms[1] = -fNorms[0]; |
| 492 |
SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[0].length())); |
| 493 |
// we won't actually use the bisectors, so just push zeroes |
| 494 |
fBisectors.push_back(SkPoint::Make(0.0, 0.0)); |
| 495 |
fBisectors.push_back(SkPoint::Make(0.0, 0.0)); |
| 496 |
} else { |
| 497 |
return false; |
| 498 |
} |
| 499 |
|
| 500 |
fCandidateVerts.setReserve(this->numPts()); |
| 501 |
fInitialRing.setReserve(this->numPts()); |
| 502 |
for (int i = 0; i < this->numPts(); ++i) { |
| 503 |
fInitialRing.addIdx(i, i); |
| 504 |
} |
| 505 |
fInitialRing.init(fNorms, fBisectors); |
| 506 |
|
| 507 |
this->validate(); |
| 508 |
return true; |
| 509 |
} |
| 510 |
|
| 511 |
GrAAConvexTessellator::Ring* GrAAConvexTessellator::getNextRing(Ring* lastRing) { |
| 512 |
#if GR_AA_CONVEX_TESSELLATOR_VIZ |
| 513 |
Ring* ring = *fRings.push() = new Ring; |
| 514 |
ring->setReserve(fInitialRing.numPts()); |
| 515 |
ring->rewind(); |
| 516 |
return ring; |
| 517 |
#else |
| 518 |
// Flip flop back and forth between fRings[0] & fRings[1] |
| 519 |
int nextRing = (lastRing == &fRings[0]) ? 1 : 0; |
| 520 |
fRings[nextRing].setReserve(fInitialRing.numPts()); |
| 521 |
fRings[nextRing].rewind(); |
| 522 |
return &fRings[nextRing]; |
| 523 |
#endif |
| 524 |
} |
| 525 |
|
| 526 |
void GrAAConvexTessellator::fanRing(const Ring& ring) { |
| 527 |
// fan out from point 0 |
| 528 |
int startIdx = ring.index(0); |
| 529 |
for (int cur = ring.numPts() - 2; cur >= 0; --cur) { |
| 530 |
this->addTri(startIdx, ring.index(cur), ring.index(cur + 1)); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
void GrAAConvexTessellator::createOuterRing(const Ring& previousRing, SkScalar outset, |
| 535 |
SkScalar coverage, Ring* nextRing) { |
| 536 |
const int numPts = previousRing.numPts(); |
| 537 |
if (numPts == 0) { |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
int prev = numPts - 1; |
| 542 |
int lastPerpIdx = -1, firstPerpIdx = -1; |
| 543 |
|
| 544 |
const SkScalar outsetSq = outset * outset; |
| 545 |
SkScalar miterLimitSq = outset * fMiterLimit; |
| 546 |
miterLimitSq = miterLimitSq * miterLimitSq; |
| 547 |
for (int cur = 0; cur < numPts; ++cur) { |
| 548 |
int originalIdx = previousRing.index(cur); |
| 549 |
// For each vertex of the original polygon we add at least two points to the |
| 550 |
// outset polygon - one extending perpendicular to each impinging edge. Connecting these |
| 551 |
// two points yields a bevel join. We need one additional point for a mitered join, and |
| 552 |
// a round join requires one or more points depending upon curvature. |
| 553 |
|
| 554 |
// The perpendicular point for the last edge |
| 555 |
SkPoint normal1 = previousRing.norm(prev); |
| 556 |
SkPoint perp1 = normal1; |
| 557 |
perp1.scale(outset); |
| 558 |
perp1 += this->point(originalIdx); |
| 559 |
|
| 560 |
// The perpendicular point for the next edge. |
| 561 |
SkPoint normal2 = previousRing.norm(cur); |
| 562 |
SkPoint perp2 = normal2; |
| 563 |
perp2.scale(outset); |
| 564 |
perp2 += fPts[originalIdx]; |
| 565 |
|
| 566 |
CurveState curve = fCurveState[originalIdx]; |
| 567 |
|
| 568 |
// We know it isn't a duplicate of the prior point (since it and this |
| 569 |
// one are just perpendicular offsets from the non-merged polygon points) |
| 570 |
int perp1Idx = this->addPt(perp1, -outset, coverage, false, curve); |
| 571 |
nextRing->addIdx(perp1Idx, originalIdx); |
| 572 |
|
| 573 |
int perp2Idx; |
| 574 |
// For very shallow angles all the corner points could fuse. |
| 575 |
if (duplicate_pt(perp2, this->point(perp1Idx))) { |
| 576 |
perp2Idx = perp1Idx; |
| 577 |
} else { |
| 578 |
perp2Idx = this->addPt(perp2, -outset, coverage, false, curve); |
| 579 |
} |
| 580 |
|
| 581 |
if (perp2Idx != perp1Idx) { |
| 582 |
if (curve == kCurve_CurveState) { |
| 583 |
// bevel or round depending upon curvature |
| 584 |
SkScalar dotProd = normal1.dot(normal2); |
| 585 |
if (dotProd < kRoundCapThreshold) { |
| 586 |
// Currently we "round" by creating a single extra point, which produces |
| 587 |
// good results for common cases. For thick strokes with high curvature, we will |
| 588 |
// need to add more points; for the time being we simply fall back to software |
| 589 |
// rendering for thick strokes. |
| 590 |
SkPoint miter = previousRing.bisector(cur); |
| 591 |
miter.setLength(-outset); |
| 592 |
miter += fPts[originalIdx]; |
| 593 |
|
| 594 |
// For very shallow angles all the corner points could fuse |
| 595 |
if (!duplicate_pt(miter, this->point(perp1Idx))) { |
| 596 |
int miterIdx; |
| 597 |
miterIdx = this->addPt(miter, -outset, coverage, false, kSharp_CurveState); |
| 598 |
nextRing->addIdx(miterIdx, originalIdx); |
| 599 |
// The two triangles for the corner |
| 600 |
this->addTri(originalIdx, perp1Idx, miterIdx); |
| 601 |
this->addTri(originalIdx, miterIdx, perp2Idx); |
| 602 |
} |
| 603 |
} else { |
| 604 |
this->addTri(originalIdx, perp1Idx, perp2Idx); |
| 605 |
} |
| 606 |
} else { |
| 607 |
switch (fJoin) { |
| 608 |
case SkPaint::Join::kMiter_Join: { |
| 609 |
// The bisector outset point |
| 610 |
SkPoint miter = previousRing.bisector(cur); |
| 611 |
SkScalar dotProd = normal1.dot(normal2); |
| 612 |
// The max is because this could go slightly negative if precision causes |
| 613 |
// us to become slightly concave. |
| 614 |
SkScalar sinHalfAngleSq = std::max(SkScalarHalf(SK_Scalar1 + dotProd), 0.f); |
| 615 |
SkScalar lengthSq = sk_ieee_float_divide(outsetSq, sinHalfAngleSq); |
| 616 |
if (lengthSq > miterLimitSq) { |
| 617 |
// just bevel it |
| 618 |
this->addTri(originalIdx, perp1Idx, perp2Idx); |
| 619 |
break; |
| 620 |
} |
| 621 |
miter.setLength(-SkScalarSqrt(lengthSq)); |
| 622 |
miter += fPts[originalIdx]; |
| 623 |
|
| 624 |
// For very shallow angles all the corner points could fuse |
| 625 |
if (!duplicate_pt(miter, this->point(perp1Idx))) { |
| 626 |
int miterIdx; |
| 627 |
miterIdx = this->addPt(miter, -outset, coverage, false, |
| 628 |
kSharp_CurveState); |
| 629 |
nextRing->addIdx(miterIdx, originalIdx); |
| 630 |
// The two triangles for the corner |
| 631 |
this->addTri(originalIdx, perp1Idx, miterIdx); |
| 632 |
this->addTri(originalIdx, miterIdx, perp2Idx); |
| 633 |
} else { |
| 634 |
// ignore the miter point as it's so close to perp1/perp2 and simply |
| 635 |
// bevel. |
| 636 |
this->addTri(originalIdx, perp1Idx, perp2Idx); |
| 637 |
} |
| 638 |
break; |
| 639 |
} |
| 640 |
case SkPaint::Join::kBevel_Join: |
| 641 |
this->addTri(originalIdx, perp1Idx, perp2Idx); |
| 642 |
break; |
| 643 |
default: |
| 644 |
// kRound_Join is unsupported for now. AALinearizingConvexPathRenderer is |
| 645 |
// only willing to draw mitered or beveled, so we should never get here. |
| 646 |
SkASSERT(false); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
nextRing->addIdx(perp2Idx, originalIdx); |
| 651 |
} |
| 652 |
|
| 653 |
if (0 == cur) { |
| 654 |
// Store the index of the first perpendicular point to finish up |
| 655 |
firstPerpIdx = perp1Idx; |
| 656 |
SkASSERT(-1 == lastPerpIdx); |
| 657 |
} else { |
| 658 |
// The triangles for the previous edge |
| 659 |
int prevIdx = previousRing.index(prev); |
| 660 |
this->addTri(prevIdx, perp1Idx, originalIdx); |
| 661 |
this->addTri(prevIdx, lastPerpIdx, perp1Idx); |
| 662 |
} |
| 663 |
|
| 664 |
// Track the last perpendicular outset point so we can construct the |
| 665 |
// trailing edge triangles. |
| 666 |
lastPerpIdx = perp2Idx; |
| 667 |
prev = cur; |
| 668 |
} |
| 669 |
|
| 670 |
// pick up the final edge rect |
| 671 |
int lastIdx = previousRing.index(numPts - 1); |
| 672 |
this->addTri(lastIdx, firstPerpIdx, previousRing.index(0)); |
| 673 |
this->addTri(lastIdx, lastPerpIdx, firstPerpIdx); |
| 674 |
|
| 675 |
this->validate(); |
| 676 |
} |
| 677 |
|
| 678 |
// Something went wrong in the creation of the next ring. If we're filling the shape, just go ahead |
| 679 |
// and fan it. |
| 680 |
void GrAAConvexTessellator::terminate(const Ring& ring) { |
| 681 |
if (fStyle != SkStrokeRec::kStroke_Style && ring.numPts() > 0) { |
| 682 |
this->fanRing(ring); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
static SkScalar compute_coverage(SkScalar depth, SkScalar initialDepth, SkScalar initialCoverage, |
| 687 |
SkScalar targetDepth, SkScalar targetCoverage) { |
| 688 |
if (SkScalarNearlyEqual(initialDepth, targetDepth)) { |
| 689 |
return targetCoverage; |
| 690 |
} |
| 691 |
SkScalar result = (depth - initialDepth) / (targetDepth - initialDepth) * |
| 692 |
(targetCoverage - initialCoverage) + initialCoverage; |
| 693 |
return SkTPin(result, 0.0f, 1.0f); |
| 694 |
} |
| 695 |
|
| 696 |
// return true when processing is complete |
| 697 |
bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing, |
| 698 |
SkScalar initialDepth, SkScalar initialCoverage, |
| 699 |
SkScalar targetDepth, SkScalar targetCoverage, |
| 700 |
bool forceNew) { |
| 701 |
bool done = false; |
| 702 |
|
| 703 |
fCandidateVerts.rewind(); |
| 704 |
|
| 705 |
// Loop through all the points in the ring and find the intersection with the smallest depth |
| 706 |
SkScalar minDist = SK_ScalarMax, minT = 0.0f; |
| 707 |
int minEdgeIdx = -1; |
| 708 |
|
| 709 |
for (int cur = 0; cur < lastRing.numPts(); ++cur) { |
| 710 |
int next = (cur + 1) % lastRing.numPts(); |
| 711 |
|
| 712 |
SkScalar t; |
| 713 |
bool result = intersect(this->point(lastRing.index(cur)), lastRing.bisector(cur), |
| 714 |
this->point(lastRing.index(next)), lastRing.bisector(next), |
| 715 |
&t); |
| 716 |
// The bisectors may be parallel (!result) or the previous ring may have become slightly |
| 717 |
// concave due to accumulated error (t <= 0). |
| 718 |
if (!result || t <= 0) { |
| 719 |
continue; |
| 720 |
} |
| 721 |
SkScalar dist = -t * lastRing.norm(cur).dot(lastRing.bisector(cur)); |
| 722 |
|
| 723 |
if (minDist > dist) { |
| 724 |
minDist = dist; |
| 725 |
minT = t; |
| 726 |
minEdgeIdx = cur; |
| 727 |
} |
| 728 |
} |
| 729 |
|
| 730 |
if (minEdgeIdx == -1) { |
| 731 |
return false; |
| 732 |
} |
| 733 |
SkPoint newPt = lastRing.bisector(minEdgeIdx); |
| 734 |
newPt.scale(minT); |
| 735 |
newPt += this->point(lastRing.index(minEdgeIdx)); |
| 736 |
|
| 737 |
SkScalar depth = this->computeDepthFromEdge(lastRing.origEdgeID(minEdgeIdx), newPt); |
| 738 |
if (depth >= targetDepth) { |
| 739 |
// None of the bisectors intersect before reaching the desired depth. |
| 740 |
// Just step them all to the desired depth |
| 741 |
depth = targetDepth; |
| 742 |
done = true; |
| 743 |
} |
| 744 |
|
| 745 |
// 'dst' stores where each point in the last ring maps to/transforms into |
| 746 |
// in the next ring. |
| 747 |
SkTDArray<int> dst; |
| 748 |
dst.resize(lastRing.numPts()); |
| 749 |
|
| 750 |
// Create the first point (who compares with no one) |
| 751 |
if (!this->computePtAlongBisector(lastRing.index(0), |
| 752 |
lastRing.bisector(0), |
| 753 |
lastRing.origEdgeID(0), |
| 754 |
depth, &newPt)) { |
| 755 |
this->terminate(lastRing); |
| 756 |
return true; |
| 757 |
} |
| 758 |
dst[0] = fCandidateVerts.addNewPt(newPt, |
| 759 |
lastRing.index(0), lastRing.origEdgeID(0), |
| 760 |
!this->movable(lastRing.index(0))); |
| 761 |
|
| 762 |
// Handle the middle points (who only compare with the prior point) |
| 763 |
for (int cur = 1; cur < lastRing.numPts()-1; ++cur) { |
| 764 |
if (!this->computePtAlongBisector(lastRing.index(cur), |
| 765 |
lastRing.bisector(cur), |
| 766 |
lastRing.origEdgeID(cur), |
| 767 |
depth, &newPt)) { |
| 768 |
this->terminate(lastRing); |
| 769 |
return true; |
| 770 |
} |
| 771 |
if (!duplicate_pt(newPt, fCandidateVerts.lastPoint())) { |
| 772 |
dst[cur] = fCandidateVerts.addNewPt(newPt, |
| 773 |
lastRing.index(cur), lastRing.origEdgeID(cur), |
| 774 |
!this->movable(lastRing.index(cur))); |
| 775 |
} else { |
| 776 |
dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur)); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
// Check on the last point (handling the wrap around) |
| 781 |
int cur = lastRing.numPts()-1; |
| 782 |
if (!this->computePtAlongBisector(lastRing.index(cur), |
| 783 |
lastRing.bisector(cur), |
| 784 |
lastRing.origEdgeID(cur), |
| 785 |
depth, &newPt)) { |
| 786 |
this->terminate(lastRing); |
| 787 |
return true; |
| 788 |
} |
| 789 |
bool dupPrev = duplicate_pt(newPt, fCandidateVerts.lastPoint()); |
| 790 |
bool dupNext = duplicate_pt(newPt, fCandidateVerts.firstPoint()); |
| 791 |
|
| 792 |
if (!dupPrev && !dupNext) { |
| 793 |
dst[cur] = fCandidateVerts.addNewPt(newPt, |
| 794 |
lastRing.index(cur), lastRing.origEdgeID(cur), |
| 795 |
!this->movable(lastRing.index(cur))); |
| 796 |
} else if (dupPrev && !dupNext) { |
| 797 |
dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur)); |
| 798 |
} else if (!dupPrev && dupNext) { |
| 799 |
dst[cur] = fCandidateVerts.fuseWithNext(); |
| 800 |
} else { |
| 801 |
bool dupPrevVsNext = duplicate_pt(fCandidateVerts.firstPoint(), fCandidateVerts.lastPoint()); |
| 802 |
|
| 803 |
if (!dupPrevVsNext) { |
| 804 |
dst[cur] = fCandidateVerts.fuseWithPrior(lastRing.origEdgeID(cur)); |
| 805 |
} else { |
| 806 |
const int fused = fCandidateVerts.fuseWithBoth(); |
| 807 |
dst[cur] = fused; |
| 808 |
const int targetIdx = dst[cur - 1]; |
| 809 |
for (int i = cur - 1; i >= 0 && dst[i] == targetIdx; i--) { |
| 810 |
dst[i] = fused; |
| 811 |
} |
| 812 |
} |
| 813 |
} |
| 814 |
|
| 815 |
// Fold the new ring's points into the global pool |
| 816 |
for (int i = 0; i < fCandidateVerts.numPts(); ++i) { |
| 817 |
int newIdx; |
| 818 |
if (fCandidateVerts.needsToBeNew(i) || forceNew) { |
| 819 |
// if the originating index is still valid then this point wasn't |
| 820 |
// fused (and is thus movable) |
| 821 |
SkScalar coverage = compute_coverage(depth, initialDepth, initialCoverage, |
| 822 |
targetDepth, targetCoverage); |
| 823 |
newIdx = this->addPt(fCandidateVerts.point(i), depth, coverage, |
| 824 |
fCandidateVerts.originatingIdx(i) != -1, kSharp_CurveState); |
| 825 |
} else { |
| 826 |
SkASSERT(fCandidateVerts.originatingIdx(i) != -1); |
| 827 |
this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth, |
| 828 |
targetCoverage); |
| 829 |
newIdx = fCandidateVerts.originatingIdx(i); |
| 830 |
} |
| 831 |
|
| 832 |
nextRing->addIdx(newIdx, fCandidateVerts.origEdge(i)); |
| 833 |
} |
| 834 |
|
| 835 |
// 'dst' currently has indices into the ring. Remap these to be indices |
| 836 |
// into the global pool since the triangulation operates in that space. |
| 837 |
for (int i = 0; i < dst.size(); ++i) { |
| 838 |
dst[i] = nextRing->index(dst[i]); |
| 839 |
} |
| 840 |
|
| 841 |
for (int i = 0; i < lastRing.numPts(); ++i) { |
| 842 |
int next = (i + 1) % lastRing.numPts(); |
| 843 |
|
| 844 |
this->addTri(lastRing.index(i), lastRing.index(next), dst[next]); |
| 845 |
this->addTri(lastRing.index(i), dst[next], dst[i]); |
| 846 |
} |
| 847 |
|
| 848 |
if (done && fStyle != SkStrokeRec::kStroke_Style) { |
| 849 |
// fill or stroke-and-fill |
| 850 |
this->fanRing(*nextRing); |
| 851 |
} |
| 852 |
|
| 853 |
if (nextRing->numPts() < 3) { |
| 854 |
done = true; |
| 855 |
} |
| 856 |
return done; |
| 857 |
} |
| 858 |
|
| 859 |
void GrAAConvexTessellator::validate() const { |
| 860 |
SkASSERT(fPts.size() == fMovable.size()); |
| 861 |
SkASSERT(fPts.size() == fCoverages.size()); |
| 862 |
SkASSERT(fPts.size() == fCurveState.size()); |
| 863 |
SkASSERT(0 == (fIndices.size() % 3)); |
| 864 |
SkASSERT(fBisectors.empty() || fBisectors.size() == fNorms.size()); |
| 865 |
} |
| 866 |
|
| 867 |
////////////////////////////////////////////////////////////////////////////// |
| 868 |
void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) { |
| 869 |
this->computeNormals(tess); |
| 870 |
this->computeBisectors(tess); |
| 871 |
} |
| 872 |
|
| 873 |
void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms, |
| 874 |
const SkTDArray<SkVector>& bisectors) { |
| 875 |
for (int i = 0; i < fPts.size(); ++i) { |
| 876 |
fPts[i].fNorm = norms[i]; |
| 877 |
fPts[i].fBisector = bisectors[i]; |
| 878 |
} |
| 879 |
} |
| 880 |
|
| 881 |
// Compute the outward facing normal at each vertex. |
| 882 |
void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& tess) { |
| 883 |
for (int cur = 0; cur < fPts.size(); ++cur) { |
| 884 |
int next = (cur + 1) % fPts.size(); |
| 885 |
|
| 886 |
fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex); |
| 887 |
SkPoint::Normalize(&fPts[cur].fNorm); |
| 888 |
fPts[cur].fNorm = SkPointPriv::MakeOrthog(fPts[cur].fNorm, tess.side()); |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& tess) { |
| 893 |
int prev = fPts.size() - 1; |
| 894 |
for (int cur = 0; cur < fPts.size(); prev = cur, ++cur) { |
| 895 |
fPts[cur].fBisector = fPts[cur].fNorm + fPts[prev].fNorm; |
| 896 |
if (!fPts[cur].fBisector.normalize()) { |
| 897 |
fPts[cur].fBisector = |
| 898 |
SkPointPriv::MakeOrthog(fPts[cur].fNorm, (SkPointPriv::Side)-tess.side()) + |
| 899 |
SkPointPriv::MakeOrthog(fPts[prev].fNorm, tess.side()); |
| 900 |
SkAssertResult(fPts[cur].fBisector.normalize()); |
| 901 |
} else { |
| 902 |
fPts[cur].fBisector.negate(); // make the bisector face in |
| 903 |
} |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
////////////////////////////////////////////////////////////////////////////// |
| 908 |
#ifdef SK_DEBUG |
| 909 |
// Is this ring convex? |
| 910 |
bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const { |
| 911 |
if (fPts.size() < 3) { |
| 912 |
return true; |
| 913 |
} |
| 914 |
|
| 915 |
SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.back().fIndex); |
| 916 |
SkPoint cur = tess.point(fPts[1].fIndex) - tess.point(fPts[0].fIndex); |
| 917 |
SkScalar minDot = prev.fX * cur.fY - prev.fY * cur.fX; |
| 918 |
SkScalar maxDot = minDot; |
| 919 |
|
| 920 |
prev = cur; |
| 921 |
for (int i = 1; i < fPts.size(); ++i) { |
| 922 |
int next = (i + 1) % fPts.size(); |
| 923 |
|
| 924 |
cur = tess.point(fPts[next].fIndex) - tess.point(fPts[i].fIndex); |
| 925 |
SkScalar dot = prev.fX * cur.fY - prev.fY * cur.fX; |
| 926 |
|
| 927 |
minDot = std::min(minDot, dot); |
| 928 |
maxDot = std::max(maxDot, dot); |
| 929 |
|
| 930 |
prev = cur; |
| 931 |
} |
| 932 |
|
| 933 |
if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) { |
| 934 |
maxDot = 0; |
| 935 |
} |
| 936 |
if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) { |
| 937 |
minDot = 0; |
| 938 |
} |
| 939 |
return (maxDot >= 0.0f) == (minDot >= 0.0f); |
| 940 |
} |
| 941 |
|
| 942 |
#endif |
| 943 |
|
| 944 |
void GrAAConvexTessellator::lineTo(const SkPoint& p, CurveState curve) { |
| 945 |
if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) { |
| 946 |
return; |
| 947 |
} |
| 948 |
|
| 949 |
if (this->numPts() >= 2 && |
| 950 |
points_are_colinear_and_b_is_middle(fPts[fPts.size() - 2], fPts.back(), p, |
| 951 |
&fAccumLinearError)) { |
| 952 |
// The old last point is on the line from the second to last to the new point |
| 953 |
this->popLastPt(); |
| 954 |
// double-check that the new last point is not a duplicate of the new point. In an ideal |
| 955 |
// world this wouldn't be necessary (since it's only possible for non-convex paths), but |
| 956 |
// floating point precision issues mean it can actually happen on paths that were |
| 957 |
// determined to be convex. |
| 958 |
if (duplicate_pt(p, this->lastPoint())) { |
| 959 |
return; |
| 960 |
} |
| 961 |
} else { |
| 962 |
fAccumLinearError = 0.f; |
| 963 |
} |
| 964 |
SkScalar initialRingCoverage = (SkStrokeRec::kFill_Style == fStyle) ? 0.5f : 1.0f; |
| 965 |
this->addPt(p, 0.0f, initialRingCoverage, false, curve); |
| 966 |
} |
| 967 |
|
| 968 |
void GrAAConvexTessellator::lineTo(const SkMatrix& m, const SkPoint& p, CurveState curve) { |
| 969 |
this->lineTo(m.mapPoint(p), curve); |
| 970 |
} |
| 971 |
|
| 972 |
void GrAAConvexTessellator::quadTo(const SkPoint pts[3]) { |
| 973 |
int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance); |
| 974 |
fPointBuffer.resize(maxCount); |
| 975 |
SkPoint* target = fPointBuffer.begin(); |
| 976 |
int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2], |
| 977 |
kQuadToleranceSqd, &target, maxCount); |
| 978 |
fPointBuffer.resize(count); |
| 979 |
for (int i = 0; i < count - 1; i++) { |
| 980 |
this->lineTo(fPointBuffer[i], kCurve_CurveState); |
| 981 |
} |
| 982 |
this->lineTo(fPointBuffer[count - 1], |
| 983 |
count == 1 ? kSharp_CurveState : kIndeterminate_CurveState); |
| 984 |
} |
| 985 |
|
| 986 |
void GrAAConvexTessellator::quadTo(const SkMatrix& m, const SkPoint srcPts[3]) { |
| 987 |
SkPoint pts[3]; |
| 988 |
m.mapPoints({pts, 3}, {srcPts, 3}); |
| 989 |
this->quadTo(pts); |
| 990 |
} |
| 991 |
|
| 992 |
void GrAAConvexTessellator::cubicTo(const SkMatrix& m, const SkPoint srcPts[4]) { |
| 993 |
SkPoint pts[4]; |
| 994 |
m.mapPoints({pts, 4}, {srcPts, 4}); |
| 995 |
int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance); |
| 996 |
fPointBuffer.resize(maxCount); |
| 997 |
SkPoint* target = fPointBuffer.begin(); |
| 998 |
int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3], |
| 999 |
kCubicToleranceSqd, &target, maxCount); |
| 1000 |
fPointBuffer.resize(count); |
| 1001 |
for (int i = 0; i < count - 1; i++) { |
| 1002 |
this->lineTo(fPointBuffer[i], kCurve_CurveState); |
| 1003 |
} |
| 1004 |
this->lineTo(fPointBuffer[count - 1], |
| 1005 |
count == 1 ? kSharp_CurveState : kIndeterminate_CurveState); |
| 1006 |
} |
| 1007 |
|
| 1008 |
// include down here to avoid compilation errors caused by "-" overload in SkGeometry.h |
| 1009 |
#include "src/core/SkGeometry.h" |
| 1010 |
|
| 1011 |
void GrAAConvexTessellator::conicTo(const SkMatrix& m, const SkPoint srcPts[3], SkScalar w) { |
| 1012 |
SkPoint pts[3]; |
| 1013 |
m.mapPoints({pts, 3}, {srcPts, 3}); |
| 1014 |
SkAutoConicToQuads quadder; |
| 1015 |
const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance); |
| 1016 |
SkPoint lastPoint = *(quads++); |
| 1017 |
int count = quadder.countQuads(); |
| 1018 |
for (int i = 0; i < count; ++i) { |
| 1019 |
SkPoint quadPts[3]; |
| 1020 |
quadPts[0] = lastPoint; |
| 1021 |
quadPts[1] = quads[0]; |
| 1022 |
quadPts[2] = i == count - 1 ? pts[2] : quads[1]; |
| 1023 |
this->quadTo(quadPts); |
| 1024 |
lastPoint = quadPts[2]; |
| 1025 |
quads += 2; |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
////////////////////////////////////////////////////////////////////////////// |
| 1030 |
#if GR_AA_CONVEX_TESSELLATOR_VIZ |
| 1031 |
static const SkScalar kPointRadius = 0.02f; |
| 1032 |
static const SkScalar kArrowStrokeWidth = 0.0f; |
| 1033 |
static const SkScalar kArrowLength = 0.2f; |
| 1034 |
static const SkScalar kEdgeTextSize = 0.1f; |
| 1035 |
static const SkScalar kPointTextSize = 0.02f; |
| 1036 |
|
| 1037 |
static void draw_point(SkCanvas* canvas, const SkPoint& p, SkScalar paramValue, bool stroke) { |
| 1038 |
SkPaint paint; |
| 1039 |
SkASSERT(paramValue <= 1.0f); |
| 1040 |
int gs = int(255*paramValue); |
| 1041 |
paint.setARGB(255, gs, gs, gs); |
| 1042 |
|
| 1043 |
canvas->drawCircle(p.fX, p.fY, kPointRadius, paint); |
| 1044 |
|
| 1045 |
if (stroke) { |
| 1046 |
SkPaint stroke; |
| 1047 |
stroke.setColor(SK_ColorYELLOW); |
| 1048 |
stroke.setStyle(SkPaint::kStroke_Style); |
| 1049 |
stroke.setStrokeWidth(kPointRadius/3.0f); |
| 1050 |
canvas->drawCircle(p.fX, p.fY, kPointRadius, stroke); |
| 1051 |
} |
| 1052 |
} |
| 1053 |
|
| 1054 |
static void draw_line(SkCanvas* canvas, const SkPoint& p0, const SkPoint& p1, SkColor color) { |
| 1055 |
SkPaint p; |
| 1056 |
p.setColor(color); |
| 1057 |
|
| 1058 |
canvas->drawLine(p0.fX, p0.fY, p1.fX, p1.fY, p); |
| 1059 |
} |
| 1060 |
|
| 1061 |
static void draw_arrow(SkCanvas*canvas, const SkPoint& p, const SkPoint &n, |
| 1062 |
SkScalar len, SkColor color) { |
| 1063 |
SkPaint paint; |
| 1064 |
paint.setColor(color); |
| 1065 |
paint.setStrokeWidth(kArrowStrokeWidth); |
| 1066 |
paint.setStyle(SkPaint::kStroke_Style); |
| 1067 |
|
| 1068 |
canvas->drawLine(p.fX, p.fY, |
| 1069 |
p.fX + len * n.fX, p.fY + len * n.fY, |
| 1070 |
paint); |
| 1071 |
} |
| 1072 |
|
| 1073 |
void GrAAConvexTessellator::Ring::draw(SkCanvas* canvas, const GrAAConvexTessellator& tess) const { |
| 1074 |
SkPaint paint; |
| 1075 |
paint.setTextSize(kEdgeTextSize); |
| 1076 |
|
| 1077 |
for (int cur = 0; cur < fPts.count(); ++cur) { |
| 1078 |
int next = (cur + 1) % fPts.count(); |
| 1079 |
|
| 1080 |
draw_line(canvas, |
| 1081 |
tess.point(fPts[cur].fIndex), |
| 1082 |
tess.point(fPts[next].fIndex), |
| 1083 |
SK_ColorGREEN); |
| 1084 |
|
| 1085 |
SkPoint mid = tess.point(fPts[cur].fIndex) + tess.point(fPts[next].fIndex); |
| 1086 |
mid.scale(0.5f); |
| 1087 |
|
| 1088 |
if (fPts.count()) { |
| 1089 |
draw_arrow(canvas, mid, fPts[cur].fNorm, kArrowLength, SK_ColorRED); |
| 1090 |
mid.fX += (kArrowLength/2) * fPts[cur].fNorm.fX; |
| 1091 |
mid.fY += (kArrowLength/2) * fPts[cur].fNorm.fY; |
| 1092 |
} |
| 1093 |
|
| 1094 |
SkString num; |
| 1095 |
num.printf("%d", this->origEdgeID(cur)); |
| 1096 |
canvas->drawString(num, mid.fX, mid.fY, paint); |
| 1097 |
|
| 1098 |
if (fPts.count()) { |
| 1099 |
draw_arrow(canvas, tess.point(fPts[cur].fIndex), fPts[cur].fBisector, |
| 1100 |
kArrowLength, SK_ColorBLUE); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
void GrAAConvexTessellator::draw(SkCanvas* canvas) const { |
| 1106 |
for (int i = 0; i < fIndices.count(); i += 3) { |
| 1107 |
SkASSERT(fIndices[i] < this->numPts()) ; |
| 1108 |
SkASSERT(fIndices[i+1] < this->numPts()) ; |
| 1109 |
SkASSERT(fIndices[i+2] < this->numPts()) ; |
| 1110 |
|
| 1111 |
draw_line(canvas, |
| 1112 |
this->point(this->fIndices[i]), this->point(this->fIndices[i+1]), |
| 1113 |
SK_ColorBLACK); |
| 1114 |
draw_line(canvas, |
| 1115 |
this->point(this->fIndices[i+1]), this->point(this->fIndices[i+2]), |
| 1116 |
SK_ColorBLACK); |
| 1117 |
draw_line(canvas, |
| 1118 |
this->point(this->fIndices[i+2]), this->point(this->fIndices[i]), |
| 1119 |
SK_ColorBLACK); |
| 1120 |
} |
| 1121 |
|
| 1122 |
fInitialRing.draw(canvas, *this); |
| 1123 |
for (int i = 0; i < fRings.count(); ++i) { |
| 1124 |
fRings[i]->draw(canvas, *this); |
| 1125 |
} |
| 1126 |
|
| 1127 |
for (int i = 0; i < this->numPts(); ++i) { |
| 1128 |
draw_point(canvas, |
| 1129 |
this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)), |
| 1130 |
!this->movable(i)); |
| 1131 |
|
| 1132 |
SkPaint paint; |
| 1133 |
paint.setTextSize(kPointTextSize); |
| 1134 |
if (this->depth(i) <= -kAntialiasingRadius) { |
| 1135 |
paint.setColor(SK_ColorWHITE); |
| 1136 |
} |
| 1137 |
|
| 1138 |
SkString num; |
| 1139 |
num.printf("%d", i); |
| 1140 |
canvas->drawString(num, |
| 1141 |
this->point(i).fX, this->point(i).fY+(kPointRadius/2.0f), |
| 1142 |
paint); |
| 1143 |
} |
| 1144 |
} |
| 1145 |
|
| 1146 |
#endif |