Chromium Code Reviews| Index: src/gpu/GrAAConvexTessellator.cpp |
| diff --git a/src/gpu/GrAAConvexTessellator.cpp b/src/gpu/GrAAConvexTessellator.cpp |
| index 56a408d644d468c56fffe98e1b65e05018354385..d56e47c817a997d290d21f694237391129c67d73 100644 |
| --- a/src/gpu/GrAAConvexTessellator.cpp |
| +++ b/src/gpu/GrAAConvexTessellator.cpp |
| @@ -13,7 +13,6 @@ |
| #include "GrPathUtils.h" |
| // Next steps: |
| -// use in AAConvexPathRenderer |
| // add an interactive sample app slide |
| // add debug check that all points are suitably far apart |
| // test more degenerate cases |
| @@ -22,10 +21,17 @@ |
| static const SkScalar kClose = (SK_Scalar1 / 16); |
| static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose); |
|
robertphillips
2015/06/16 13:13:01
Expand this comment a bit - are they in device spa
ethannicholas
2015/06/16 14:53:29
Done.
|
| +// tesselation tolerance values |
| +static const SkScalar kQuadTolerance = 0.2; |
| +static const SkScalar kCubicTolerance = 0.2; |
| +static const SkScalar kConicTolerance = 0.5; |
| + |
| +// dot product below which we use a round cap between curve segments |
| +static const SkScalar kRoundCapThreshold = 0.8; |
| + |
| static SkScalar intersect(const SkPoint& p0, const SkPoint& n0, |
| const SkPoint& p1, const SkPoint& n1) { |
| const SkPoint v = p1 - p0; |
| - |
| SkScalar perpDot = n0.fX * n1.fY - n0.fY * n1.fX; |
| return (v.fX * n1.fY - v.fY * n1.fX) / perpDot; |
| } |
| @@ -52,13 +58,14 @@ static SkScalar abs_dist_from_line(const SkPoint& p0, const SkVector& v, const S |
| int GrAAConvexTessellator::addPt(const SkPoint& pt, |
| SkScalar depth, |
| + SkScalar coverage, |
| bool movable, |
| bool isCurve) { |
| this->validate(); |
| int index = fPts.count(); |
| *fPts.push() = pt; |
| - *fDepths.push() = depth; |
| + *fCoverages.push() = coverage; |
| *fMovable.push() = movable; |
| *fIsCurve.push() = isCurve; |
| @@ -70,7 +77,7 @@ void GrAAConvexTessellator::popLastPt() { |
| this->validate(); |
| fPts.pop(); |
| - fDepths.pop(); |
| + fCoverages.pop(); |
| fMovable.pop(); |
| this->validate(); |
| @@ -80,7 +87,7 @@ void GrAAConvexTessellator::popFirstPtShuffle() { |
| this->validate(); |
| fPts.removeShuffle(0); |
| - fDepths.removeShuffle(0); |
| + fCoverages.removeShuffle(0); |
| fMovable.removeShuffle(0); |
| this->validate(); |
| @@ -88,12 +95,13 @@ void GrAAConvexTessellator::popFirstPtShuffle() { |
| void GrAAConvexTessellator::updatePt(int index, |
| const SkPoint& pt, |
| - SkScalar depth) { |
| + SkScalar depth, |
| + SkScalar coverage) { |
| this->validate(); |
| SkASSERT(fMovable[index]); |
| fPts[index] = pt; |
| - fDepths[index] = depth; |
| + fCoverages[index] = coverage; |
| } |
| void GrAAConvexTessellator::addTri(int i0, int i1, int i2) { |
| @@ -108,7 +116,7 @@ void GrAAConvexTessellator::addTri(int i0, int i1, int i2) { |
| void GrAAConvexTessellator::rewind() { |
| fPts.rewind(); |
| - fDepths.rewind(); |
| + fCoverages.rewind(); |
| fMovable.rewind(); |
| fIndices.rewind(); |
| fNorms.rewind(); |
| @@ -143,6 +151,43 @@ void GrAAConvexTessellator::computeBisectors() { |
| } |
| } |
| +// Create as many rings as we need to (up to a predefined limit) to reach the specified target |
| +// depth. If we are in fill mode, the final ring will automatically be fanned. |
| +bool GrAAConvexTessellator::createInsetRings(Ring& previousRing, SkScalar initialDepth, |
|
robertphillips
2015/06/16 13:13:01
tab this line over ?
ethannicholas
2015/06/16 14:53:29
Sorry, been using "two tabs for continuations" for
|
| + SkScalar initialCoverage, SkScalar targetDepth, SkScalar targetCoverage, Ring** finalRing) { |
| + static const int kMaxNumRings = 8; |
| + |
| + if (previousRing.numPts() < 3) { |
| + return false; |
| + } |
| + Ring* currentRing = &previousRing; |
| + int i; |
| + for (i = 0; i < kMaxNumRings; ++i) { |
| + Ring* nextRing = this->getNextRing(currentRing); |
| + SkASSERT(nextRing != currentRing); |
| + |
| + bool done = this->createInsetRing(*currentRing, nextRing, initialDepth, initialCoverage, |
|
robertphillips
2015/06/16 13:13:01
tab this line over ?
|
| + targetDepth, targetCoverage, i == 0); |
| + currentRing = nextRing; |
| + if (done) { |
| + break; |
| + } |
| + currentRing->init(*this); |
| + } |
| + |
| + if (kMaxNumRings == i) { |
| + // Bail if we've exceeded the amount of time we want to throw at this. |
| + this->terminate(*currentRing); |
| + return false; |
| + } |
| + bool done = currentRing->numPts() >= 3; |
| + if (done) { |
| + currentRing->init(*this); |
| + } |
| + *finalRing = currentRing; |
| + return done; |
| +} |
| + |
| // The general idea here is to, conceptually, start with the original polygon and slide |
| // the vertices along the bisectors until the first intersection. At that |
| // point two of the edges collapse and the process repeats on the new polygon. |
| @@ -150,46 +195,40 @@ void GrAAConvexTessellator::computeBisectors() { |
| // controls the iteration. The CandidateVerts holds the formative points for the |
| // next ring. |
| bool GrAAConvexTessellator::tessellate(const SkMatrix& m, const SkPath& path) { |
| - static const int kMaxNumRings = 8; |
| - |
| - SkDEBUGCODE(fShouldCheckDepths = true;) |
| - |
| if (!this->extractFromPath(m, path)) { |
| return false; |
| } |
| - this->createOuterRing(); |
| + SkScalar coverage = 1.0f; |
| + if (fStrokeWidth >= 0.0f) { |
| + Ring outerStrokeRing; |
|
robertphillips
2015/06/16 13:13:01
What happens when fStrokeWidth is 1.00001 ?
ethannicholas
2015/06/16 14:53:28
I tested it out, and it doesn't appear to cause an
|
| + this->createOuterRing(fInitialRing, fStrokeWidth / 2 - kAntialiasingRadius, coverage, |
|
robertphillips
2015/06/16 13:13:00
tab this line over ?
|
| + &outerStrokeRing); |
| + outerStrokeRing.init(*this); |
| + Ring outerAARing; |
| + this->createOuterRing(outerStrokeRing, kAntialiasingRadius * 2, 0.0f, &outerAARing); |
| + } else { |
| + Ring outerAARing; |
| + this->createOuterRing(fInitialRing, kAntialiasingRadius, 0.0f, &outerAARing); |
| + } |
| // the bisectors are only needed for the computation of the outer ring |
| fBisectors.rewind(); |
| - |
| - Ring* lastRing = &fInitialRing; |
| - int i; |
| - for (i = 0; i < kMaxNumRings; ++i) { |
| - Ring* nextRing = this->getNextRing(lastRing); |
| - |
| - if (this->createInsetRing(*lastRing, nextRing)) { |
| - break; |
| + if (fStrokeWidth >= 0.0f && fInitialRing.numPts() > 2) { |
|
robertphillips
2015/06/16 13:13:01
Do we actually need to get the Ring* back from cre
ethannicholas
2015/06/16 14:53:29
When stroking, we call createInsetRings twice. The
|
| + Ring* insetStrokeRing; |
| + SkScalar strokeDepth = fStrokeWidth / 2 - kAntialiasingRadius; |
|
robertphillips
2015/06/16 13:13:01
this->createInsetRings ?
|
| + if (createInsetRings(fInitialRing, 0.0f, coverage, strokeDepth, coverage, |
| + &insetStrokeRing)) { |
| + Ring* insetAARing; |
| + createInsetRings(*insetStrokeRing, strokeDepth, coverage, strokeDepth + |
| + kAntialiasingRadius * 2, 0.0f, &insetAARing); |
| } |
| - |
| - nextRing->init(*this); |
| - lastRing = nextRing; |
| - } |
| - |
| - if (kMaxNumRings == i) { |
| - // If we've exceeded the amount of time we want to throw at this, set |
| - // the depth of all points in the final ring to 'fTargetDepth' and |
| - // create a fan. |
| - this->terminate(*lastRing); |
| - SkDEBUGCODE(fShouldCheckDepths = false;) |
| + } else { |
| + Ring* insetAARing; |
| + createInsetRings(fInitialRing, 0.0f, 0.5f, kAntialiasingRadius, 1.0f, &insetAARing); |
| } |
| -#ifdef SK_DEBUG |
| - this->validate(); |
| - if (fShouldCheckDepths) { |
| - SkDEBUGCODE(this->checkAllDepths();) |
| - } |
| -#endif |
| + SkDEBUGCODE(this->validate();) |
| return true; |
| } |
| @@ -198,7 +237,6 @@ SkScalar GrAAConvexTessellator::computeDepthFromEdge(int edgeIdx, const SkPoint& |
| SkPoint v = p - fPts[edgeIdx]; |
| SkScalar depth = -fNorms[edgeIdx].dot(v); |
| - SkASSERT(depth >= 0.0f); |
| return depth; |
| } |
| @@ -213,13 +251,13 @@ bool GrAAConvexTessellator::computePtAlongBisector(int startIdx, |
| // First find the point where the edge and the bisector intersect |
| SkPoint newP; |
| + |
| SkScalar t = perp_intersect(fPts[startIdx], bisector, fPts[edgeIdx], norm); |
| if (SkScalarNearlyEqual(t, 0.0f)) { |
| // the start point was one of the original ring points |
| - SkASSERT(startIdx < fNorms.count()); |
| + SkASSERT(startIdx < fPts.count()); |
| newP = fPts[startIdx]; |
| - } else if (t > 0.0f) { |
| - SkASSERT(t < 0.0f); |
| + } else if (t < 0.0f) { |
| newP = bisector; |
| newP.scale(t); |
| newP += fPts[startIdx]; |
| @@ -228,13 +266,12 @@ bool GrAAConvexTessellator::computePtAlongBisector(int startIdx, |
| } |
| // Then offset along the bisector from that point the correct distance |
| - t = -desiredDepth / bisector.dot(norm); |
| - SkASSERT(t > 0.0f); |
| + SkScalar dot = bisector.dot(norm); |
| + t = -desiredDepth / dot; |
| *result = bisector; |
| result->scale(t); |
| *result += newP; |
| - |
| return true; |
| } |
| @@ -252,9 +289,6 @@ bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& pat |
| fNorms.setReserve(path.countPoints()); |
| - SkDEBUGCODE(fMinCross = SK_ScalarMax;) |
| - SkDEBUGCODE(fMaxCross = -SK_ScalarMax;) |
| - |
| // TODO: is there a faster way to extract the points from the path? Perhaps |
| // get all the points via a new entry point, transform them all in bulk |
| // and then walk them to find duplicates? |
| @@ -282,7 +316,7 @@ bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& pat |
| } |
| } |
| - if (this->numPts() < 3) { |
| + if (this->numPts() < 2) { |
| return false; |
| } |
| @@ -293,23 +327,20 @@ bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& pat |
| } |
| SkASSERT(fPts.count() == fNorms.count()+1); |
| - if (this->numPts() >= 3 && |
| - abs_dist_from_line(fPts.top(), fNorms.top(), fPts[0]) < kClose) { |
| - // The last point is on the line from the second to last to the first point. |
| - this->popLastPt(); |
| - fNorms.pop(); |
| - } |
| - |
| - if (this->numPts() < 3) { |
| - return false; |
| + if (this->numPts() >= 3) { |
|
robertphillips
2015/06/16 13:13:00
Don't we still want to remove the last point if it
ethannicholas
2015/06/16 14:53:29
Oops, hadn't meant to leave that commented out.
|
| +/* if (abs_dist_from_line(fPts.top(), fNorms.top(), fPts[0]) < kClose) { |
| + // The last point is on the line from the second to last to the first point. |
| + this->popLastPt(); |
| + fNorms.pop(); |
| + }*/ |
| + |
| + *fNorms.push() = fPts[0] - fPts.top(); |
| + SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top()); |
| + SkASSERT(len > 0.0f); |
| + SkASSERT(fPts.count() == fNorms.count()); |
| } |
| - *fNorms.push() = fPts[0] - fPts.top(); |
| - SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top()); |
| - SkASSERT(len > 0.0f); |
| - SkASSERT(fPts.count() == fNorms.count()); |
| - |
| - if (abs_dist_from_line(fPts[0], fNorms.top(), fPts[1]) < kClose) { |
| + if (this->numPts() >= 3 && abs_dist_from_line(fPts[0], fNorms.top(), fPts[1]) < kClose) { |
| // The first point is on the line from the last to the second. |
| this->popFirstPtShuffle(); |
| fNorms.removeShuffle(0); |
| @@ -319,28 +350,44 @@ bool GrAAConvexTessellator::extractFromPath(const SkMatrix& m, const SkPath& pat |
| SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[0].length())); |
| } |
| - if (this->numPts() < 3) { |
| - return false; |
| - } |
| + if (this->numPts() >= 3) { |
| + // Check the cross product of the final trio |
| + SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.top()); |
| + if (cross > 0.0f) { |
| + fSide = SkPoint::kRight_Side; |
| + } else { |
| + fSide = SkPoint::kLeft_Side; |
| + } |
| - // Check the cross product of the final trio |
| - SkScalar cross = SkPoint::CrossProduct(fNorms[0], fNorms.top()); |
| - SkDEBUGCODE(fMaxCross = SkTMax(fMaxCross, cross)); |
| - SkDEBUGCODE(fMinCross = SkTMin(fMinCross, cross)); |
| - SkASSERT((fMaxCross >= 0.0f) == (fMinCross >= 0.0f)); |
| - if (cross > 0.0f) { |
| - fSide = SkPoint::kRight_Side; |
| - } else { |
| + // Make all the normals face outwards rather than along the edge |
| + for (int cur = 0; cur < fNorms.count(); ++cur) { |
| + fNorms[cur].setOrthog(fNorms[cur], fSide); |
| + SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length())); |
| + } |
| + |
| + this->computeBisectors(); |
| + } else if (this->numPts() == 2) { |
| + // We've got two points, so we're degenerate. |
| + if (fStrokeWidth < 0.0f) { |
| + // it's a fill, so we don't need to worry about degenerate paths |
| + return false; |
| + } |
| + // For stroking, we still need to process the degenerate path, so fix it up |
| fSide = SkPoint::kLeft_Side; |
| - } |
| - // Make all the normals face outwards rather than along the edge |
| - for (int cur = 0; cur < fNorms.count(); ++cur) { |
| - fNorms[cur].setOrthog(fNorms[cur], fSide); |
| - SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length())); |
| - } |
| + // Make all the normals face outwards rather than along the edge |
| + for (int cur = 0; cur < fNorms.count(); ++cur) { |
| + fNorms[cur].setOrthog(fNorms[cur], fSide); |
| + SkASSERT(SkScalarNearlyEqual(1.0f, fNorms[cur].length())); |
| + } |
|
robertphillips
2015/06/16 13:13:01
SkPoint::Make ?
|
| - this->computeBisectors(); |
| + fNorms.push({ -fNorms[0].fX, -fNorms[0].fY }); |
| + // we won't actually use the bisectors, so just push zeroes |
| + fBisectors.push({ 0, 0 }); |
| + fBisectors.push({ 0, 0 }); |
| + } else { |
| + return false; |
| + } |
| fCandidateVerts.setReserve(this->numPts()); |
| fInitialRing.setReserve(this->numPts()); |
| @@ -370,138 +417,171 @@ GrAAConvexTessellator::Ring* GrAAConvexTessellator::getNextRing(Ring* lastRing) |
| void GrAAConvexTessellator::fanRing(const Ring& ring) { |
| // fan out from point 0 |
| - for (int cur = 1; cur < ring.numPts()-1; ++cur) { |
| - this->addTri(ring.index(0), ring.index(cur), ring.index(cur+1)); |
| + int startIdx = ring.index(0); |
| + for (int cur = ring.numPts() - 2; cur >= 0; --cur) { |
| + this->addTri(startIdx, ring.index(cur), ring.index(cur + 1)); |
| } |
| } |
| -void GrAAConvexTessellator::createOuterRing() { |
| - // For now, we're only generating one outer ring (at the start). This |
| - // could be relaxed for stroking use cases. |
| - SkASSERT(0 == fIndices.count()); |
| - SkASSERT(fPts.count() == fNorms.count()); |
| - |
| - const int numPts = fPts.count(); |
| +void GrAAConvexTessellator::createOuterRing(const Ring& previousRing, SkScalar outset, |
| + SkScalar coverage, Ring* nextRing) { |
| + const int numPts = previousRing.numPts(); |
| + if (numPts == 0) { |
| + return; |
| + } |
| int prev = numPts - 1; |
| - int lastPerpIdx = -1, firstPerpIdx = -1, newIdx0, newIdx1, newIdx2; |
| - for (int cur = 0; cur < numPts; ++cur) { |
| - if (fIsCurve[cur]) { |
| - // Inside a curve, we assume that the curvature is shallow enough (due to tesselation) |
| - // that we only need one corner point. Mathematically, the distance the corner point |
| - // gets shifted out should depend on the angle between the two line segments (as in |
| - // mitering), but again due to tesselation we assume that this angle is small and |
| - // therefore the correction factor is negligible and we do not bother with it. |
| - |
| - // The bisector outset point |
| - SkPoint temp = fBisectors[cur]; |
| - temp.scale(-fTargetDepth); // the bisectors point in |
| - temp += fPts[cur]; |
| - |
| - // double-check our "sufficiently flat" assumption; we want the bisector point to be |
| - // close to the normal point. |
| - #define kFlatnessTolerance 1.0f |
| - SkDEBUGCODE(SkPoint prevNormal = fNorms[prev];) |
| - SkDEBUGCODE(prevNormal.scale(fTargetDepth);) |
| - SkDEBUGCODE(prevNormal += fPts[cur];) |
| - SkASSERT((temp - prevNormal).length() < kFlatnessTolerance); |
| - |
| - newIdx1 = this->addPt(temp, -fTargetDepth, false, true); |
| - |
| - if (0 == cur) { |
| - // Store the index of the first perpendicular point to finish up |
| - firstPerpIdx = newIdx1; |
| - SkASSERT(-1 == lastPerpIdx); |
| - } else { |
| - // The triangles for the previous edge |
| - this->addTri(prev, newIdx1, cur); |
| - this->addTri(prev, lastPerpIdx, newIdx1); |
| - } |
| + int lastPerpIdx = -1, firstPerpIdx = -1; |
|
robertphillips
2015/06/16 13:13:01
Make outsetSq const ?
|
| - prev = cur; |
| - // Track the last perpendicular outset point so we can construct the |
| - // trailing edge triangles. |
| - lastPerpIdx = newIdx1; |
| + SkScalar outsetSq = SkScalarMul(outset, outset); |
| + SkScalar miterLimitSq = SkScalarMul(outset, fMiterLimit); |
| + miterLimitSq = SkScalarMul(miterLimitSq, miterLimitSq); |
| + for (int cur = 0; cur < numPts; ++cur) { |
| + int originalIdx = previousRing.index(cur); |
| + // For each vertex of the original polygon we add at least two points to the |
| + // outset polygon - one extending perpendicular to each impinging edge. Connecting these |
| + // two points yields a bevel join. We need one additional point for a mitered join, and |
| + // a round join requires one or more points depending upon curvature. |
| + |
| + // The perpendicular point for the last edge |
| + SkPoint normal1 = previousRing.norm(prev); |
| + SkPoint perp1 = normal1; |
| + perp1.scale(outset); |
| + perp1 += this->point(originalIdx); |
| + |
| + // The perpendicular point for the next edge. |
| + SkPoint normal2 = previousRing.norm(cur); |
| + SkPoint perp2 = normal2; |
| + perp2.scale(outset); |
| + perp2 += fPts[originalIdx]; |
| + |
| + bool isCurve = fIsCurve[originalIdx]; |
| + |
| + // We know it isn't a duplicate of the prior point (since it and this |
| + // one are just perpendicular offsets from the non-merged polygon points) |
| + int perp1Idx = this->addPt(perp1, -outset, coverage, false, isCurve); |
| + nextRing->addIdx(perp1Idx, originalIdx); |
| + |
| + int perp2Idx; |
| + // For very shallow angles all the corner points could fuse. |
| + if (duplicate_pt(perp2, this->point(perp1Idx))) { |
| + perp2Idx = perp1Idx; |
| + } else { |
| + perp2Idx = this->addPt(perp2, -outset, coverage, false, isCurve); |
| } |
| - else { |
| - // For each vertex of the original polygon we add three points to the |
| - // outset polygon - one extending perpendicular to each impinging edge |
| - // and one along the bisector. Two triangles are added for each corner |
| - // and two are added along each edge. |
| - |
| - // The perpendicular point for the last edge |
| - SkPoint temp = fNorms[prev]; |
| - temp.scale(fTargetDepth); |
| - temp += fPts[cur]; |
| - |
| - // We know it isn't a duplicate of the prior point (since it and this |
| - // one are just perpendicular offsets from the non-merged polygon points) |
| - newIdx0 = this->addPt(temp, -fTargetDepth, false, false); |
| - |
| - // The bisector outset point |
| - temp = fBisectors[cur]; |
| - temp.scale(-fTargetDepth); // the bisectors point in |
| - temp += fPts[cur]; |
| - |
| - // For very shallow angles all the corner points could fuse |
| - if (duplicate_pt(temp, this->point(newIdx0))) { |
| - newIdx1 = newIdx0; |
| - } else { |
| - newIdx1 = this->addPt(temp, -fTargetDepth, false, false); |
| - } |
| - |
| - // The perpendicular point for the next edge. |
| - temp = fNorms[cur]; |
| - temp.scale(fTargetDepth); |
| - temp += fPts[cur]; |
| - |
| - // For very shallow angles all the corner points could fuse. |
| - if (duplicate_pt(temp, this->point(newIdx1))) { |
| - newIdx2 = newIdx1; |
| - } else { |
| - newIdx2 = this->addPt(temp, -fTargetDepth, false, false); |
| - } |
| - if (0 == cur) { |
| - // Store the index of the first perpendicular point to finish up |
| - firstPerpIdx = newIdx0; |
| - SkASSERT(-1 == lastPerpIdx); |
| + if (perp2Idx != perp1Idx) { |
| + if (isCurve) { |
| + // bevel or round depending upon curvature |
| + SkScalar dotProd = normal1.dot(normal2); |
| + if (dotProd < kRoundCapThreshold) { |
| + // Currently we "round" by creating a single extra point, which produces |
| + // good results for common cases. For thick strokes with high curvature, we will |
| + // need to add more points; for the time being we simply fall back to software |
| + // rendering for thick strokes. |
| + SkPoint miter = previousRing.bisector(cur); |
| + miter.setLength(-outset); |
| + miter += fPts[originalIdx]; |
| + |
| + // For very shallow angles all the corner points could fuse |
| + if (!duplicate_pt(miter, this->point(perp1Idx))) { |
| + int miterIdx; |
| + miterIdx = this->addPt(miter, -outset, coverage, false, false); |
| + nextRing->addIdx(miterIdx, originalIdx); |
| + // The two triangles for the corner |
| + this->addTri(originalIdx, perp1Idx, miterIdx); |
| + this->addTri(originalIdx, miterIdx, perp2Idx); |
| + } |
| + } else { |
| + this->addTri(originalIdx, perp1Idx, perp2Idx); |
| + } |
| } else { |
| - // The triangles for the previous edge |
| - this->addTri(prev, newIdx0, cur); |
| - this->addTri(prev, lastPerpIdx, newIdx0); |
| + switch (fJoin) { |
| + case SkPaint::Join::kMiter_Join: { |
| + // The bisector outset point |
| + SkPoint miter = previousRing.bisector(cur); |
| + SkScalar dotProd = normal1.dot(normal2); |
| + SkScalar sinHalfAngleSq = SkScalarHalf(SK_Scalar1 + dotProd); |
| + SkScalar lengthSq = outsetSq / sinHalfAngleSq; |
| + if (lengthSq > miterLimitSq) { |
|
robertphillips
2015/06/16 13:13:01
Shouldn't we just cap the miter in this case - not
ethannicholas
2015/06/16 14:53:29
As far as I can tell, what I am doing is visually
|
| + goto bevel; |
| + } |
| + miter.setLength(-SkScalarSqrt(lengthSq)); |
| + miter += fPts[originalIdx]; |
| + |
| + // For very shallow angles all the corner points could fuse |
| + if (!duplicate_pt(miter, this->point(perp1Idx))) { |
| + int miterIdx; |
| + miterIdx = this->addPt(miter, -outset, coverage, false, false); |
| + nextRing->addIdx(miterIdx, originalIdx); |
| + // The two triangles for the corner |
| + this->addTri(originalIdx, perp1Idx, miterIdx); |
| + this->addTri(originalIdx, miterIdx, perp2Idx); |
| + } |
| + break; |
| + } |
| + case SkPaint::Join::kBevel_Join: |
| + bevel: |
| + this->addTri(originalIdx, perp1Idx, perp2Idx); |
| + break; |
| + default: |
| + // kRound_Join is unsupported for now. GrAALinearizingConvexPathRenderer is |
| + // only willing to draw mitered or beveled, so we should never get here. |
| + SkASSERT(false); |
| + } |
| } |
| - // The two triangles for the corner |
| - this->addTri(cur, newIdx0, newIdx1); |
| - this->addTri(cur, newIdx1, newIdx2); |
| + nextRing->addIdx(perp2Idx, originalIdx); |
| + } |
| - prev = cur; |
| - // Track the last perpendicular outset point so we can construct the |
| - // trailing edge triangles. |
| - lastPerpIdx = newIdx2; |
| + if (0 == cur) { |
| + // Store the index of the first perpendicular point to finish up |
| + firstPerpIdx = perp1Idx; |
| + SkASSERT(-1 == lastPerpIdx); |
| + } else { |
| + // The triangles for the previous edge |
| + int prevIdx = previousRing.index(prev); |
| + this->addTri(prevIdx, perp1Idx, originalIdx); |
| + this->addTri(prevIdx, lastPerpIdx, perp1Idx); |
| } |
| + |
| + // Track the last perpendicular outset point so we can construct the |
| + // trailing edge triangles. |
| + lastPerpIdx = perp2Idx; |
| + prev = cur; |
| } |
| // pick up the final edge rect |
| - this->addTri(numPts - 1, firstPerpIdx, 0); |
| - this->addTri(numPts - 1, lastPerpIdx, firstPerpIdx); |
| + int lastIdx = previousRing.index(numPts - 1); |
| + this->addTri(lastIdx, firstPerpIdx, previousRing.index(0)); |
| + this->addTri(lastIdx, lastPerpIdx, firstPerpIdx); |
| this->validate(); |
| } |
| -// Something went wrong in the creation of the next ring. Mark the last good |
| -// ring as being at the desired depth and fan it. |
| +// Something went wrong in the creation of the next ring. If we're filling the shape, just go ahead |
| +// and fan it. |
| void GrAAConvexTessellator::terminate(const Ring& ring) { |
| - for (int i = 0; i < ring.numPts(); ++i) { |
| - fDepths[ring.index(i)] = fTargetDepth; |
| + if (fStrokeWidth < 0.0f) { |
| + this->fanRing(ring); |
| } |
| +} |
|
robertphillips
2015/06/16 13:13:01
compute_coverage since static ?
ethannicholas
2015/06/16 14:53:29
Done.
|
| - this->fanRing(ring); |
| +static SkScalar computeCoverage(SkScalar depth, SkScalar initialDepth, SkScalar initialCoverage, |
|
robertphillips
2015/06/16 13:13:01
tab this over ?
|
| + SkScalar targetDepth, SkScalar targetCoverage) { |
| + if (SkScalarNearlyEqual(initialDepth, targetDepth)) { |
| + return targetCoverage; |
| + } |
| + SkScalar result = (depth - initialDepth) / (targetDepth - initialDepth) * |
| + (targetCoverage - initialCoverage) + initialCoverage; |
| + SkASSERT(result >= 0.0f && result <= 1.0f); |
| + return result; |
| } |
| // return true when processing is complete |
| -bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing) { |
| +bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing, |
|
robertphillips
2015/06/16 13:13:01
tab these lines over ?
|
| + SkScalar initialDepth, SkScalar initialCoverage, SkScalar targetDepth, |
| + SkScalar targetCoverage, bool forceNew) { |
| bool done = false; |
| fCandidateVerts.rewind(); |
| @@ -512,7 +592,6 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| for (int cur = 0; cur < lastRing.numPts(); ++cur) { |
| int next = (cur + 1) % lastRing.numPts(); |
| - |
| SkScalar t = intersect(this->point(lastRing.index(cur)), lastRing.bisector(cur), |
| this->point(lastRing.index(next)), lastRing.bisector(next)); |
| SkScalar dist = -t * lastRing.norm(cur).dot(lastRing.bisector(cur)); |
| @@ -524,15 +603,18 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| } |
| } |
| + if (minEdgeIdx == -1) { |
| + return false; |
| + } |
| SkPoint newPt = lastRing.bisector(minEdgeIdx); |
| newPt.scale(minT); |
| newPt += this->point(lastRing.index(minEdgeIdx)); |
| SkScalar depth = this->computeDepthFromEdge(lastRing.origEdgeID(minEdgeIdx), newPt); |
| - if (depth >= fTargetDepth) { |
| + if (depth >= targetDepth) { |
| // None of the bisectors intersect before reaching the desired depth. |
| // Just step them all to the desired depth |
| - depth = fTargetDepth; |
| + depth = targetDepth; |
| done = true; |
| } |
| @@ -547,7 +629,6 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| lastRing.origEdgeID(0), |
| depth, &newPt)) { |
| this->terminate(lastRing); |
| - SkDEBUGCODE(fShouldCheckDepths = false;) |
| return true; |
| } |
| dst[0] = fCandidateVerts.addNewPt(newPt, |
| @@ -561,7 +642,6 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| lastRing.origEdgeID(cur), |
| depth, &newPt)) { |
| this->terminate(lastRing); |
| - SkDEBUGCODE(fShouldCheckDepths = false;) |
| return true; |
| } |
| if (!duplicate_pt(newPt, fCandidateVerts.lastPoint())) { |
| @@ -580,7 +660,6 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| lastRing.origEdgeID(cur), |
| depth, &newPt)) { |
| this->terminate(lastRing); |
| - SkDEBUGCODE(fShouldCheckDepths = false;) |
| return true; |
| } |
| bool dupPrev = duplicate_pt(newPt, fCandidateVerts.lastPoint()); |
| @@ -607,14 +686,17 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| // Fold the new ring's points into the global pool |
| for (int i = 0; i < fCandidateVerts.numPts(); ++i) { |
| int newIdx; |
| - if (fCandidateVerts.needsToBeNew(i)) { |
| + if (fCandidateVerts.needsToBeNew(i) || forceNew) { |
| // if the originating index is still valid then this point wasn't |
| // fused (and is thus movable) |
|
robertphillips
2015/06/16 13:13:01
Is there any downside to splitting this into 2 sta
|
| - newIdx = this->addPt(fCandidateVerts.point(i), depth, |
| + newIdx = this->addPt(fCandidateVerts.point(i), depth, |
|
robertphillips
2015/06/16 13:13:01
this->computeCoverage
|
| + computeCoverage(depth, initialDepth, initialCoverage, |
|
robertphillips
2015/06/16 13:13:01
tab this line over ?
|
| + targetDepth, targetCoverage), |
| fCandidateVerts.originatingIdx(i) != -1, false); |
| } else { |
| SkASSERT(fCandidateVerts.originatingIdx(i) != -1); |
| - this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth); |
| + this->updatePt(fCandidateVerts.originatingIdx(i), fCandidateVerts.point(i), depth, |
|
robertphillips
2015/06/16 13:13:01
tab this over ?
|
| + targetCoverage); |
| newIdx = fCandidateVerts.originatingIdx(i); |
| } |
| @@ -634,19 +716,18 @@ bool GrAAConvexTessellator::createInsetRing(const Ring& lastRing, Ring* nextRing |
| this->addTri(lastRing.index(cur), dst[next], dst[cur]); |
| } |
| - if (done) { |
| + if (done && fStrokeWidth < 0.0f) { |
| + // fill |
| this->fanRing(*nextRing); |
| } |
| if (nextRing->numPts() < 3) { |
| done = true; |
| } |
| - |
| return done; |
| } |
| void GrAAConvexTessellator::validate() const { |
| - SkASSERT(fPts.count() == fDepths.count()); |
| SkASSERT(fPts.count() == fMovable.count()); |
| SkASSERT(0 == (fIndices.count() % 3)); |
| } |
| @@ -655,7 +736,6 @@ void GrAAConvexTessellator::validate() const { |
| void GrAAConvexTessellator::Ring::init(const GrAAConvexTessellator& tess) { |
| this->computeNormals(tess); |
| this->computeBisectors(tess); |
| - SkASSERT(this->isConvex(tess)); |
| } |
| void GrAAConvexTessellator::Ring::init(const SkTDArray<SkVector>& norms, |
| @@ -672,11 +752,8 @@ void GrAAConvexTessellator::Ring::computeNormals(const GrAAConvexTessellator& te |
| int next = (cur + 1) % fPts.count(); |
| fPts[cur].fNorm = tess.point(fPts[next].fIndex) - tess.point(fPts[cur].fIndex); |
| - SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fPts[cur].fNorm); |
| - SkASSERT(len > 0.0f); |
| + SkPoint::Normalize(&fPts[cur].fNorm); |
| fPts[cur].fNorm.setOrthog(fPts[cur].fNorm, tess.side()); |
| - |
| - SkASSERT(SkScalarNearlyEqual(1.0f, fPts[cur].fNorm.length())); |
| } |
| } |
| @@ -694,9 +771,7 @@ void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& |
| } else { |
| fPts[cur].fBisector.negate(); // make the bisector face in |
| } |
| - |
| - SkASSERT(SkScalarNearlyEqual(1.0f, fPts[cur].fBisector.length())); |
| - } |
| + } |
| } |
| ////////////////////////////////////////////////////////////////////////////// |
| @@ -704,7 +779,7 @@ void GrAAConvexTessellator::Ring::computeBisectors(const GrAAConvexTessellator& |
| // Is this ring convex? |
| bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) const { |
| if (fPts.count() < 3) { |
| - return false; |
| + return true; |
| } |
| SkPoint prev = tess.point(fPts[0].fIndex) - tess.point(fPts.top().fIndex); |
| @@ -725,7 +800,13 @@ bool GrAAConvexTessellator::Ring::isConvex(const GrAAConvexTessellator& tess) co |
| prev = cur; |
| } |
| - return (maxDot > 0.0f) == (minDot >= 0.0f); |
| + if (SkScalarNearlyEqual(maxDot, 0.0f, 0.005f)) { |
| + maxDot = 0; |
| + } |
| + if (SkScalarNearlyEqual(minDot, 0.0f, 0.005f)) { |
| + minDot = 0; |
| + } |
| + return (maxDot >= 0.0f) == (minDot >= 0.0f); |
| } |
| static SkScalar capsule_depth(const SkPoint& p0, const SkPoint& p1, |
| @@ -776,21 +857,8 @@ SkScalar GrAAConvexTessellator::computeRealDepth(const SkPoint& p) const { |
| return closestSign * minDist; |
| } |
| - |
| -// Verify that the incrementally computed depths are close to the actual depths. |
| -void GrAAConvexTessellator::checkAllDepths() const { |
| - for (int cur = 0; cur < this->numPts(); ++cur) { |
|
robertphillips
2015/06/16 13:13:01
So, can computeRealDepth go too ?
ethannicholas
2015/06/16 14:53:29
Looks like it.
|
| - SkScalar realDepth = this->computeRealDepth(this->point(cur)); |
| - SkScalar computedDepth = this->depth(cur); |
| - SkASSERT(SkScalarNearlyEqual(realDepth, computedDepth, 0.01f)); |
| - } |
| -} |
| #endif |
| -#define kQuadTolerance 0.2f |
| -#define kCubicTolerance 0.2f |
| -#define kConicTolerance 0.5f |
| - |
| void GrAAConvexTessellator::lineTo(const SkMatrix& m, SkPoint p, bool isCurve) { |
| m.mapPoints(&p, 1); |
| if (this->numPts() > 0 && duplicate_pt(p, this->lastPoint())) { |
| @@ -805,21 +873,14 @@ void GrAAConvexTessellator::lineTo(const SkMatrix& m, SkPoint p, bool isCurve) { |
| fNorms.pop(); |
| fIsCurve.pop(); |
| } |
| - this->addPt(p, 0.0f, false, isCurve); |
| + SkScalar initialRingCoverage = fStrokeWidth < 0.0f ? 0.5f : 1.0f; |
| + this->addPt(p, 0.0f, initialRingCoverage, false, isCurve); |
| if (this->numPts() > 1) { |
| *fNorms.push() = fPts.top() - fPts[fPts.count()-2]; |
| SkDEBUGCODE(SkScalar len =) SkPoint::Normalize(&fNorms.top()); |
| SkASSERT(len > 0.0f); |
| SkASSERT(SkScalarNearlyEqual(1.0f, fNorms.top().length())); |
| } |
| - SkDEBUGCODE( |
| - if (this->numPts() >= 3) { |
| - int cur = this->numPts()-1; |
| - SkScalar cross = SkPoint::CrossProduct(fNorms[cur-1], fNorms[cur-2]); |
| - fMaxCross = SkTMax(fMaxCross, cross); |
| - fMinCross = SkTMin(fMinCross, cross); |
| - } |
| - ) |
| } |
| void GrAAConvexTessellator::quadTo(const SkMatrix& m, SkPoint pts[3]) { |
| @@ -965,13 +1026,13 @@ void GrAAConvexTessellator::draw(SkCanvas* canvas) const { |
| for (int i = 0; i < this->numPts(); ++i) { |
| draw_point(canvas, |
| - this->point(i), 0.5f + (this->depth(i)/(2*fTargetDepth)), |
| + this->point(i), 0.5f + (this->depth(i)/(2 * kAntialiasingRadius)), |
| !this->movable(i)); |
| SkPaint paint; |
| paint.setTextSize(kPointTextSize); |
| paint.setTextAlign(SkPaint::kCenter_Align); |
| - if (this->depth(i) <= -fTargetDepth) { |
| + if (this->depth(i) <= -kAntialiasingRadius) { |
| paint.setColor(SK_ColorWHITE); |
| } |