Chromium Code Reviews| Index: src/gpu/SkGpuDevice.cpp |
| diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp |
| index d43552e0d724892f09c397d6ff765537b59a9ba7..dbef07d80ef3340f7706337493bb1955a3388a62 100644 |
| --- a/src/gpu/SkGpuDevice.cpp |
| +++ b/src/gpu/SkGpuDevice.cpp |
| @@ -8,6 +8,7 @@ |
| #include "SkGpuDevice.h" |
| #include "effects/GrBicubicEffect.h" |
| +#include "effects/GrDashingEffect.h" |
| #include "effects/GrTextureDomain.h" |
| #include "effects/GrSimpleTextureEffect.h" |
| @@ -535,6 +536,12 @@ void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| return; |
| } |
| + if (paint.getPathEffect() && 2 == count) { |
| + if (this->drawDash(pts, paint, mode)) { |
| + return; |
| + } |
| + } |
| + |
| // we only handle hairlines and paints without path effects or mask filters, |
| // else we let the SkDraw call our drawPath() |
| if (width > 0 || paint.getPathEffect() || paint.getMaskFilter()) { |
| @@ -559,6 +566,207 @@ void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| /////////////////////////////////////////////////////////////////////////////// |
| +static void calcDashScaling(SkScalar* parallelScale, SkScalar* perpScale, |
|
bsalomon
2014/05/09 15:02:37
style nit: we've been tending towards naming_stati
egdaniel
2014/05/12 19:21:44
fixed
On 2014/05/09 15:02:37, bsalomon wrote:
|
| + const SkMatrix& viewMatrix, const SkPoint pts[2]) { |
| + SkVector vecSrc = pts[1] - pts[0]; |
| + SkScalar magSrc = vecSrc.length(); |
| + SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0; |
| + vecSrc.scale(invSrc); |
| + |
| + SkVector vecSrcPerp; |
| + vecSrc.rotateCW(&vecSrcPerp); |
| + viewMatrix.mapVectors(&vecSrc, 1); |
| + viewMatrix.mapVectors(&vecSrcPerp, 1); |
| + |
| + // parallelScale tells how much to scale along the line parallel to the dash line |
| + // perpScale tells how much to scale in the direction perpendicular to the dash line |
| + *parallelScale = vecSrc.length(); |
| + *perpScale = vecSrcPerp.length(); |
| +} |
| + |
| +// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1] |
| +// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot |
| +static void alignToXAxis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) { |
| + SkVector vec = pts[1] - pts[0]; |
| + SkScalar mag = vec.length(); |
| + SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| + |
| + vec.scale(inv); |
| + rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| + if (ptsRot) { |
| + rotMatrix->mapPoints(ptsRot, pts, 2); |
| + // correction for numerical issues if map doesn't make ptsRot exactly horizontal |
| + ptsRot[1].fY = pts[0].fY; |
| + } |
| +} |
| + |
| +// Assumes phase < sum of all intervals |
|
bsalomon
2014/05/09 15:02:37
is that a valid assumption? add an assert?
egdaniel
2014/05/12 19:21:44
In the creation of SkDashEffect the phase is norma
|
| +static SkScalar calcStartAdjustment(const SkPathEffect::DashInfo& info) { |
| + SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
|
bsalomon
2014/05/09 15:02:37
don't need this until inside both ifs, right?
egdaniel
2014/05/12 19:21:44
yup
On 2014/05/09 15:02:37, bsalomon wrote:
|
| + if (info.fPhase >= info.fIntervals[0]) { |
| + if (info.fIntervals[0] !=0 || info.fPhase != 0) { |
|
bsalomon
2014/05/09 15:02:37
if intervals[0] is != 0 then phase can't be either
egdaniel
2014/05/12 19:21:44
Ahh you're right. I was just inverting the logic i
|
| + return srcIntervalLen - info.fPhase; |
| + } |
| + } |
| + return 0; |
| +} |
| + |
| +static SkScalar calcEndAdjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2]) { |
| + SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| + SkScalar totalLen = pts[1].fX - pts[0].fY; |
| + SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen); |
| + SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
| + SkScalar endingInt = totalLen - numFullIntervals * srcIntervalLen + info.fPhase; |
| + temp = SkScalarDiv(endingInt, srcIntervalLen); |
| + endingInt = endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen; |
| + if (0 == endingInt) { |
| + endingInt = srcIntervalLen; |
| + } |
| + if (endingInt > info.fIntervals[0]) { |
| + if (0 == info.fIntervals[0]) { |
| + endingInt -= 0.01; // make sure we capture the last zero size pnt (used if has caps) |
| + } |
| + return endingInt - info.fIntervals[0]; |
| + } |
| + return 0; |
| +} |
| + |
| +bool SkGpuDevice::drawDash(const SkPoint pts[2], const SkPaint& paint, SkCanvas::PointMode mode) { |
|
bsalomon
2014/05/09 15:02:37
maybe this should be called drawDashedLine()? It o
egdaniel
2014/05/12 19:21:44
Name changed. Moved the mode check to the caller.
|
| + if (SkCanvas::kLines_PointMode != mode) { |
| + return false; |
| + } |
| + |
| + const SkMatrix& viewMatrix = fContext->getMatrix(); |
| + if (!viewMatrix.preservesRightAngles()) { |
| + return false; |
| + } |
| + |
| + const SkPathEffect* pe = paint.getPathEffect(); |
| + SkPathEffect::DashInfo info; |
| + SkPathEffect::DashType dashType = pe->asADash(&info); |
| + // Must be a dash effect with 2 intervals (1 on and 1 off) |
| + if (SkPathEffect::kDash_DashType != dashType || 2 != info.fCount) { |
| + return false; |
| + } |
| + |
| + GrPaint grPaint; |
|
bsalomon
2014/05/09 15:02:37
maybe hold off on this until after your cap check?
egdaniel
2014/05/12 19:21:44
done
On 2014/05/09 15:02:37, bsalomon wrote:
|
| + if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| + return false; |
| + } |
| + |
| + SkPaint::Cap cap = paint.getStrokeCap(); |
| + // Current we do don't handle Round or Square cap dashes |
| + if (SkPaint::kRound_Cap == cap) { |
| + return false; |
| + } |
| + |
| + SkScalar srcStrokeWidth = paint.getStrokeWidth(); |
| + |
| + // Get all info about the dash effect |
| + SkAutoTArray<SkScalar> intervals(info.fCount); |
| + info.fIntervals = intervals.get(); |
| + pe->asADash(&info); |
| + |
| + SkMatrix coordTrans; |
| + |
| + // If the src pts are not axis aligned, rotate them so that they are horizontal |
| + SkMatrix srcRotInv; |
| + SkPoint ptsRot[2]; |
| + if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
| + alignToXAxis(pts, &coordTrans, ptsRot); |
| + if(!coordTrans.invert(&srcRotInv)) { |
| + return false; |
| + } |
| + } else { |
| + coordTrans.reset(); |
| + srcRotInv.reset(); |
| + ptsRot[0] = pts[0]; |
| + ptsRot[1] = pts[1]; |
| + } |
| + |
| + // adjustments for start and end of bounding rect so we only draw dash intervals |
| + // contained in the original line segment. |
| + // TODO: what happens if adjustments make degenerate line? |
|
bsalomon
2014/05/09 15:02:37
Can these conditions cause us to draw incorrectly
egdaniel
2014/05/12 19:21:44
Changed so that if the first or last dashes in the
|
| + // TODO: since we are using the bounding geometry to clip off bad parts, we now |
| + // loss so AA that happens on end of dash segment. Better to draw first and last |
| + // rects separately and use shader on middle? |
| + SkScalar startAdj = calcStartAdjustment(info); |
| + SkScalar endAdj = calcEndAdjustment(info, ptsRot); |
| + |
| + coordTrans.postConcat(viewMatrix); |
| + |
| + SkPoint devicePts[2]; |
| + viewMatrix.mapPoints(devicePts, ptsRot, 2); |
| + |
| + // Scale corrections of intervals and stroke from view matrix |
| + SkScalar parallelScale; |
| + SkScalar perpScale; |
| + calcDashScaling(¶llelScale, &perpScale, viewMatrix, ptsRot); |
| + info.fIntervals[0] *= parallelScale; |
| + info.fIntervals[1] *= parallelScale; |
| + info.fPhase *= parallelScale; |
| + SkScalar strokeWidth = srcStrokeWidth * perpScale; |
| + |
| + bool useAA = paint.isAntiAlias(); |
| + if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) { |
| + strokeWidth = 1.f; |
| + } |
| + |
| + // Set up coordTransform for device space transforms |
| + // We rotate the dashed line such that it is horizontal with the start point at smaller x |
| + // then we translate the start point to the origin |
| + if (devicePts[0].fY != devicePts[1].fY || devicePts[0].fX > devicePts[1].fX) { |
| + SkMatrix rot; |
| + alignToXAxis(devicePts, &rot); |
| + coordTrans.postConcat(rot); |
| + } |
| + coordTrans.postTranslate(-devicePts[0].fX, -devicePts[0].fY); |
| + coordTrans.postTranslate(info.fIntervals[1] * 0.5f + info.fPhase, 0); |
| + |
| + bool hasCap = false; |
| + if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) { |
| + // add cap to on interveal and remove from off interval |
| + info.fIntervals[0] += strokeWidth; |
| + info.fIntervals[1] -= strokeWidth; |
| + hasCap = true; |
| + } |
| + |
| + if (info.fIntervals[1] > 0.f) { |
| + GrEffectEdgeType edgeType= useAA ? kFillAA_GrEffectEdgeType : |
| + kFillBW_GrEffectEdgeType; |
| + grPaint.addCoverageEffect(GrDashingEffect::Create(edgeType, info, coordTrans, strokeWidth)); |
| + grPaint.setAntiAlias(false); |
| + } |
| + |
| + // We always want to at least stroke out half a pixel on each side in device space |
| + // so 0.5f / perpScale gives us this min in src space |
| + SkScalar halfStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale); |
| + |
| + SkRect rect; |
| + bool bloat = useAA && info.fIntervals[1] > 0.f; |
| + SkScalar bloatX = bloat ? 0.5f / parallelScale : 0.f; |
| + SkScalar bloatY = bloat ? 0.5f / perpScale : 0.f; |
| + SkScalar xStroke; |
| + SkScalar yStroke; |
| + if (!hasCap) { |
| + bool horiz = ptsRot[0].fY == ptsRot[1].fY; |
| + xStroke = horiz ? 0.f : halfStroke; |
| + yStroke = horiz ? halfStroke : 0.f; |
| + } else { |
| + xStroke = halfStroke; |
| + yStroke = halfStroke; |
| + } |
| + ptsRot[0].fX += startAdj; |
| + ptsRot[1].fX -= endAdj; |
| + rect.set(ptsRot, 2); |
| + rect.outset(bloatX + xStroke, bloatY + yStroke); |
| + fContext->drawRect(grPaint, rect, NULL, &srcRotInv); |
| + |
| + return true; |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| const SkPaint& paint) { |
| CHECK_FOR_ANNOTATION(paint); |