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