Chromium Code Reviews| Index: src/gpu/SkGpuDevice.cpp |
| diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp |
| index 0aee983f7561eaa852059b04d0d21f88d39b043f..01b586e64b57fafc1eac99c91e29ce038edd7ee4 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 && SkCanvas::kLines_PointMode == mode) { |
| + if (this->drawDashLine(pts, paint)) { |
| + 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,262 @@ void SkGpuDevice::drawPoints(const SkDraw& draw, SkCanvas::PointMode mode, |
| /////////////////////////////////////////////////////////////////////////////// |
| +static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale, |
| + 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 align_to_x_axis(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 |
| +static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) { |
| + SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| + if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) { |
| + SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| + return srcIntervalLen - info.fPhase; |
| + } |
| + return 0; |
| +} |
| + |
| +static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2], SkScalar* endingInt) { |
| + if (pts[1].fX <= pts[0].fX) { |
| + return 0; |
| + } |
| + SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| + SkScalar totalLen = pts[1].fX - pts[0].fX; |
| + SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen); |
| + SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
| + *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; |
| +} |
| + |
| + |
|
bsalomon
2014/05/13 09:10:33
This file has gotten huge. It seems like the only
egdaniel
2014/05/13 14:00:11
Agree completely. Will move it out.
On 2014/05/13
|
| +bool SkGpuDevice::drawDashLine(const SkPoint pts[2], const SkPaint& paint) { |
|
bsalomon
2014/05/13 09:10:33
I think this function needs to check if the target
egdaniel
2014/05/13 14:00:11
is that not what the check below this comment is d
bsalomon
2014/05/13 14:14:36
dOn 2014/05/13 14:00:11, egdaniel wrote:
|
| + if (fContext->getRenderTarget()->isMultisampled()) { |
| + 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; |
| + } |
| + |
| + 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); |
| + |
| + // the phase should be normalized to be [0, sum of all intervals) |
| + SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| + |
| + SkMatrix coordTrans; |
| + |
| + // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX |
| + SkMatrix srcRotInv; |
| + SkPoint ptsRot[2]; |
| + if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
| + align_to_x_axis(pts, &coordTrans, ptsRot); |
| + if(!coordTrans.invert(&srcRotInv)) { |
| + return false; |
| + } |
| + } else { |
| + coordTrans.reset(); |
| + srcRotInv.reset(); |
| + memcpy(ptsRot, pts, 2 * sizeof(SkPoint)); |
| + } |
| + |
| + GrPaint grPaint; |
| + if (!skPaint2GrPaintShader(this, paint, true, &grPaint)) { |
| + return false; |
| + } |
| + |
| + bool useAA = paint.isAntiAlias(); |
| + |
| + // Scale corrections of intervals and stroke from view matrix |
| + SkScalar parallelScale; |
| + SkScalar perpScale; |
| + calc_dash_scaling(¶llelScale, &perpScale, viewMatrix, ptsRot); |
| + |
| + bool hasCap =SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth; |
|
bsalomon
2014/05/13 09:10:33
space after =
|
| + |
| + // 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); |
| + |
| + SkScalar xStroke; |
| + if (!hasCap) { |
| + xStroke = 0.f; |
| + } else { |
| + xStroke = halfStroke; |
| + } |
| + |
| + // If we are using AA, check to see if we are drawing a partial dash at the start. If so |
| + // draw it separately here and adjust our start point accordingly |
| + if (useAA) { |
| + if (info.fPhase > 0 && info.fPhase < info.fIntervals[0]) { |
| + SkPoint startPts[2]; |
| + startPts[0] = ptsRot[0]; |
| + startPts[1].fY = startPts[0].fY; |
| + startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - info.fPhase, |
| + ptsRot[1].fX); |
| + SkRect startRect; |
| + startRect.set(startPts, 2); |
| + startRect.outset(xStroke, halfStroke); |
| + fContext->drawRect(grPaint, startRect, NULL, &srcRotInv); |
| + |
| + ptsRot[0].fX += info.fIntervals[0] + info.fIntervals[1] - info.fPhase; |
| + info.fPhase = 0; |
| + } |
| + } |
| + |
| + // adjustments for start and end of bounding rect so we only draw dash intervals |
| + // contained in the original line segment. |
| + SkScalar startAdj = calc_start_adjustment(info); |
| + SkScalar endingInterval = 0; |
| + SkScalar endAdj = calc_end_adjustment(info, ptsRot, &endingInterval); |
| + if (ptsRot[0].fX + startAdj >= ptsRot[1].fX - endAdj) { |
| + // Nothing left to draw so just return |
| + return true; |
| + } |
| + |
| + // If we are using AA, check to see if we are drawing a partial dash at then end. If so |
| + // draw it separately here and adjust our end point accordingly |
| + if (useAA) { |
| + // If we adjusted the end then we will not be drawing a partial dash at the end. |
| + // If we didn't adjust the end point then we just need to make sure the ending |
| + // dash isn't a full dash |
| + if (0 == endAdj && endingInterval != info.fIntervals[0]) { |
| + |
| + SkPoint endPts[2]; |
| + endPts[1] = ptsRot[1]; |
| + endPts[0].fY = endPts[1].fY; |
| + endPts[0].fX = endPts[1].fX - endingInterval; |
| + |
| + SkRect endRect; |
| + endRect.set(endPts, 2); |
| + endRect.outset(xStroke, halfStroke); |
| + fContext->drawRect(grPaint, endRect, NULL, &srcRotInv); |
| + |
| + ptsRot[1].fX -= endingInterval + info.fIntervals[1]; |
| + if (ptsRot[0].fX >= ptsRot[1].fX) { |
| + // Nothing left to draw so just return |
| + return true; |
| + } |
| + } |
| + } |
| + coordTrans.postConcat(viewMatrix); |
| + |
| + SkPoint devicePts[2]; |
| + viewMatrix.mapPoints(devicePts, ptsRot, 2); |
| + |
| + info.fIntervals[0] *= parallelScale; |
| + info.fIntervals[1] *= parallelScale; |
| + info.fPhase *= parallelScale; |
| + SkScalar strokeWidth = srcStrokeWidth * perpScale; |
| + |
| + 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; |
| + align_to_x_axis(devicePts, &rot); |
| + coordTrans.postConcat(rot); |
| + } |
| + coordTrans.postTranslate(-devicePts[0].fX, -devicePts[0].fY); |
| + coordTrans.postTranslate(info.fIntervals[1] * 0.5f + info.fPhase, 0); |
| + |
| + 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; |
| + } |
| + |
| + if (info.fIntervals[1] > 0.f) { |
| + GrEffectEdgeType edgeType= useAA ? kFillAA_GrEffectEdgeType : |
| + kFillBW_GrEffectEdgeType; |
| + grPaint.addCoverageEffect(GrDashingEffect::Create(edgeType, info, coordTrans, strokeWidth))->unref(); |
| + grPaint.setAntiAlias(false); |
| + } |
| + |
| + 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; |
| + ptsRot[0].fX += startAdj; |
| + ptsRot[1].fX -= endAdj; |
| + if (!hasCap) { |
| + xStroke = 0.f; |
| + } else { |
| + xStroke = halfStroke; |
| + } |
| + rect.set(ptsRot, 2); |
| + rect.outset(bloatX + xStroke, bloatY + halfStroke); |
| + fContext->drawRect(grPaint, rect, NULL, &srcRotInv); |
| + |
| + return true; |
| +} |
| + |
| +/////////////////////////////////////////////////////////////////////////////// |
| + |
| void SkGpuDevice::drawRect(const SkDraw& draw, const SkRect& rect, |
| const SkPaint& paint) { |
| CHECK_FOR_ANNOTATION(paint); |