commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 8 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 9 | #include "GrDashingEffect.h" |
| 10 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 11 | #include "../GrAARectRenderer.h" |
| 12 | |
| 13 | #include "effects/GrVertexEffect.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 14 | #include "gl/GrGLEffect.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 15 | #include "gl/GrGLVertexEffect.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 16 | #include "gl/GrGLSL.h" |
| 17 | #include "GrContext.h" |
| 18 | #include "GrCoordTransform.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 19 | #include "GrDrawTarget.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 20 | #include "GrDrawTargetCaps.h" |
| 21 | #include "GrEffect.h" |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 22 | #include "GrGpu.h" |
| 23 | #include "GrStrokeInfo.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 24 | #include "GrTBackendEffectFactory.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 25 | #include "SkGr.h" |
| 26 | |
| 27 | /////////////////////////////////////////////////////////////////////////////// |
| 28 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 29 | // Returns whether or not the gpu can fast path the dash line effect. |
| 30 | static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo, |
| 31 | const GrDrawTarget& target, const SkMatrix& viewMatrix) { |
| 32 | if (target.getDrawState().getRenderTarget()->isMultisampled()) { |
| 33 | return false; |
| 34 | } |
| 35 | |
| 36 | // Pts must be either horizontal or vertical in src space |
| 37 | if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) { |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | // May be able to relax this to include skew. As of now cannot do perspective |
| 42 | // because of the non uniform scaling of bloating a rect |
| 43 | if (!viewMatrix.preservesRightAngles()) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) { |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
| 52 | if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap(); |
| 57 | // Current we do don't handle Round or Square cap dashes |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 58 | if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 59 | return false; |
| 60 | } |
| 61 | |
| 62 | return true; |
| 63 | } |
| 64 | |
| 65 | namespace { |
| 66 | |
| 67 | struct DashLineVertex { |
| 68 | SkPoint fPos; |
| 69 | SkPoint fDashPos; |
| 70 | }; |
| 71 | |
| 72 | extern const GrVertexAttrib gDashLineVertexAttribs[] = { |
| 73 | { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
| 74 | { kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding }, |
| 75 | }; |
| 76 | |
| 77 | }; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 78 | static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale, |
| 79 | const SkMatrix& viewMatrix, const SkPoint pts[2]) { |
| 80 | SkVector vecSrc = pts[1] - pts[0]; |
| 81 | SkScalar magSrc = vecSrc.length(); |
| 82 | SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0; |
| 83 | vecSrc.scale(invSrc); |
| 84 | |
| 85 | SkVector vecSrcPerp; |
| 86 | vecSrc.rotateCW(&vecSrcPerp); |
| 87 | viewMatrix.mapVectors(&vecSrc, 1); |
| 88 | viewMatrix.mapVectors(&vecSrcPerp, 1); |
| 89 | |
| 90 | // parallelScale tells how much to scale along the line parallel to the dash line |
| 91 | // perpScale tells how much to scale in the direction perpendicular to the dash line |
| 92 | *parallelScale = vecSrc.length(); |
| 93 | *perpScale = vecSrcPerp.length(); |
| 94 | } |
| 95 | |
| 96 | // calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1] |
| 97 | // Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot |
| 98 | static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) { |
| 99 | SkVector vec = pts[1] - pts[0]; |
| 100 | SkScalar mag = vec.length(); |
| 101 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 102 | |
| 103 | vec.scale(inv); |
| 104 | rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 105 | if (ptsRot) { |
| 106 | rotMatrix->mapPoints(ptsRot, pts, 2); |
| 107 | // correction for numerical issues if map doesn't make ptsRot exactly horizontal |
| 108 | ptsRot[1].fY = pts[0].fY; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Assumes phase < sum of all intervals |
| 113 | static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) { |
| 114 | SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| 115 | if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) { |
| 116 | SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| 117 | return srcIntervalLen - info.fPhase; |
| 118 | } |
| 119 | return 0; |
| 120 | } |
| 121 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 122 | static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2], |
| 123 | SkScalar phase, SkScalar* endingInt) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 124 | if (pts[1].fX <= pts[0].fX) { |
| 125 | return 0; |
| 126 | } |
| 127 | SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| 128 | SkScalar totalLen = pts[1].fX - pts[0].fX; |
| 129 | SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen); |
| 130 | SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 131 | *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 132 | temp = SkScalarDiv(*endingInt, srcIntervalLen); |
| 133 | *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen; |
| 134 | if (0 == *endingInt) { |
| 135 | *endingInt = srcIntervalLen; |
| 136 | } |
| 137 | if (*endingInt > info.fIntervals[0]) { |
| 138 | if (0 == info.fIntervals[0]) { |
commit-bot@chromium.org | ad88340 | 2014-05-19 14:43:45 +0000 | [diff] [blame] | 139 | *endingInt -= 0.01f; // make sure we capture the last zero size pnt (used if has caps) |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 140 | } |
| 141 | return *endingInt - info.fIntervals[0]; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 146 | static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix, |
| 147 | SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 148 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 149 | SkScalar startDashX = offset - bloat; |
| 150 | SkScalar endDashX = offset + len + bloat; |
| 151 | SkScalar startDashY = -stroke - bloat; |
| 152 | SkScalar endDashY = stroke + bloat; |
| 153 | verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY); |
| 154 | verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY); |
| 155 | verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY); |
| 156 | verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY); |
| 157 | |
| 158 | verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop); |
| 159 | verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom); |
| 160 | verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom); |
| 161 | verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop); |
| 162 | |
| 163 | matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint, |
| 168 | const GrStrokeInfo& strokeInfo, GrGpu* gpu, |
| 169 | GrDrawTarget* target, const SkMatrix& vm) { |
| 170 | |
| 171 | if (!can_fast_path_dash(pts, strokeInfo, *target, vm)) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 175 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 176 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 177 | SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 178 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 179 | SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 180 | |
| 181 | // the phase should be normalized to be [0, sum of all intervals) |
| 182 | SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| 183 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 184 | SkScalar srcPhase = info.fPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 185 | |
| 186 | // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX |
| 187 | SkMatrix srcRotInv; |
| 188 | SkPoint ptsRot[2]; |
| 189 | if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 190 | SkMatrix rotMatrix; |
| 191 | align_to_x_axis(pts, &rotMatrix, ptsRot); |
| 192 | if(!rotMatrix.invert(&srcRotInv)) { |
| 193 | GrPrintf("Failed to create invertible rotation matrix!\n"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 194 | return false; |
| 195 | } |
| 196 | } else { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 197 | srcRotInv.reset(); |
| 198 | memcpy(ptsRot, pts, 2 * sizeof(SkPoint)); |
| 199 | } |
| 200 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 201 | bool useAA = paint.isAntiAlias(); |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 202 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 203 | // Scale corrections of intervals and stroke from view matrix |
| 204 | SkScalar parallelScale; |
| 205 | SkScalar perpScale; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 206 | calc_dash_scaling(¶llelScale, &perpScale, vm, ptsRot); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 207 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 208 | bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 209 | |
| 210 | // We always want to at least stroke out half a pixel on each side in device space |
| 211 | // so 0.5f / perpScale gives us this min in src space |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 212 | SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 213 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 214 | SkScalar strokeAdj; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 215 | if (!hasCap) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 216 | strokeAdj = 0.f; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 217 | } else { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 218 | strokeAdj = halfSrcStroke; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 219 | } |
| 220 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 221 | SkScalar startAdj = 0; |
| 222 | |
| 223 | SkMatrix combinedMatrix = srcRotInv; |
| 224 | combinedMatrix.postConcat(vm); |
| 225 | |
| 226 | bool lineDone = false; |
| 227 | SkRect startRect; |
| 228 | bool hasStartRect = false; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 229 | // If we are using AA, check to see if we are drawing a partial dash at the start. If so |
| 230 | // draw it separately here and adjust our start point accordingly |
| 231 | if (useAA) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 232 | if (srcPhase > 0 && srcPhase < info.fIntervals[0]) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 233 | SkPoint startPts[2]; |
| 234 | startPts[0] = ptsRot[0]; |
| 235 | startPts[1].fY = startPts[0].fY; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 236 | startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 237 | ptsRot[1].fX); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 238 | startRect.set(startPts, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 239 | startRect.outset(strokeAdj, halfSrcStroke); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 240 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 241 | hasStartRect = true; |
| 242 | startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
| 246 | // adjustments for start and end of bounding rect so we only draw dash intervals |
| 247 | // contained in the original line segment. |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 248 | startAdj += calc_start_adjustment(info); |
| 249 | if (startAdj != 0) { |
| 250 | ptsRot[0].fX += startAdj; |
| 251 | srcPhase = 0; |
| 252 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 253 | SkScalar endingInterval = 0; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 254 | SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval); |
| 255 | ptsRot[1].fX -= endAdj; |
| 256 | if (ptsRot[0].fX >= ptsRot[1].fX) { |
| 257 | lineDone = true; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 258 | } |
| 259 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 260 | SkRect endRect; |
| 261 | bool hasEndRect = false; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 262 | // If we are using AA, check to see if we are drawing a partial dash at then end. If so |
| 263 | // draw it separately here and adjust our end point accordingly |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 264 | if (useAA && !lineDone) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 265 | // If we adjusted the end then we will not be drawing a partial dash at the end. |
| 266 | // If we didn't adjust the end point then we just need to make sure the ending |
| 267 | // dash isn't a full dash |
| 268 | if (0 == endAdj && endingInterval != info.fIntervals[0]) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 269 | SkPoint endPts[2]; |
| 270 | endPts[1] = ptsRot[1]; |
| 271 | endPts[0].fY = endPts[1].fY; |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 272 | endPts[0].fX = endPts[1].fX - endingInterval; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 273 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 274 | endRect.set(endPts, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 275 | endRect.outset(strokeAdj, halfSrcStroke); |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 276 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 277 | hasEndRect = true; |
| 278 | endAdj = endingInterval + info.fIntervals[1]; |
| 279 | |
| 280 | ptsRot[1].fX -= endAdj; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 281 | if (ptsRot[0].fX >= ptsRot[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 282 | lineDone = true; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 286 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 287 | if (startAdj != 0) { |
| 288 | srcPhase = 0; |
| 289 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 290 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 291 | // Change the dashing info from src space into device space |
| 292 | SkScalar devIntervals[2]; |
| 293 | devIntervals[0] = info.fIntervals[0] * parallelScale; |
| 294 | devIntervals[1] = info.fIntervals[1] * parallelScale; |
| 295 | SkScalar devPhase = srcPhase * parallelScale; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 296 | SkScalar strokeWidth = srcStrokeWidth * perpScale; |
| 297 | |
| 298 | if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) { |
| 299 | strokeWidth = 1.f; |
| 300 | } |
| 301 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 302 | SkScalar halfDevStroke = strokeWidth * 0.5f; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 303 | |
| 304 | if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) { |
| 305 | // add cap to on interveal and remove from off interval |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 306 | devIntervals[0] += strokeWidth; |
| 307 | devIntervals[1] -= strokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 308 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 309 | SkScalar startOffset = devIntervals[1] * 0.5f + devPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 310 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 311 | SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f; |
| 312 | SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f; |
| 313 | |
| 314 | SkScalar devBloat = useAA ? 0.5f : 0.f; |
| 315 | |
| 316 | GrDrawState* drawState = target->drawState(); |
| 317 | if (devIntervals[1] <= 0.f && useAA) { |
| 318 | // Case when we end up drawing a solid AA rect |
| 319 | // Reset the start rect to draw this single solid rect |
| 320 | // but it requires to upload a new intervals uniform so we can mimic |
| 321 | // one giant dash |
| 322 | ptsRot[0].fX -= hasStartRect ? startAdj : 0; |
| 323 | ptsRot[1].fX += hasEndRect ? endAdj : 0; |
| 324 | startRect.set(ptsRot, 2); |
| 325 | startRect.outset(strokeAdj, halfSrcStroke); |
| 326 | hasStartRect = true; |
| 327 | hasEndRect = false; |
| 328 | lineDone = true; |
| 329 | |
| 330 | SkPoint devicePts[2]; |
| 331 | vm.mapPoints(devicePts, ptsRot, 2); |
| 332 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 333 | if (hasCap) { |
| 334 | lineLength += 2.f * halfDevStroke; |
| 335 | } |
| 336 | devIntervals[0] = lineLength; |
| 337 | } |
| 338 | if (devIntervals[1] > 0.f || useAA) { |
| 339 | SkPathEffect::DashInfo devInfo; |
| 340 | devInfo.fPhase = devPhase; |
| 341 | devInfo.fCount = 2; |
| 342 | devInfo.fIntervals = devIntervals; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 343 | GrEffectEdgeType edgeType= useAA ? kFillAA_GrEffectEdgeType : |
| 344 | kFillBW_GrEffectEdgeType; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 345 | bool isRoundCap = SkPaint::kRound_Cap == cap; |
| 346 | GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap : |
| 347 | GrDashingEffect::kNonRound_DashCap; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 348 | drawState->addCoverageEffect( |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 349 | GrDashingEffect::Create(edgeType, devInfo, strokeWidth, capType), 1)->unref(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 350 | } |
| 351 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 352 | // Set up the vertex data for the line and start/end dashes |
djsollen | ea81ced | 2014-08-27 13:07:34 -0700 | [diff] [blame^] | 353 | drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 354 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 355 | int totalRectCnt = 0; |
| 356 | |
| 357 | totalRectCnt += !lineDone ? 1 : 0; |
| 358 | totalRectCnt += hasStartRect ? 1 : 0; |
| 359 | totalRectCnt += hasEndRect ? 1 : 0; |
| 360 | |
| 361 | GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0); |
| 362 | if (!geo.succeeded()) { |
| 363 | GrPrintf("Failed to get space for vertices!\n"); |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices()); |
| 368 | |
| 369 | int curVIdx = 0; |
| 370 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 371 | if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) { |
| 372 | // need to adjust this for round caps to correctly set the dashPos attrib on vertices |
| 373 | startOffset -= halfDevStroke; |
| 374 | } |
| 375 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 376 | // Draw interior part of dashed line |
| 377 | if (!lineDone) { |
| 378 | SkPoint devicePts[2]; |
| 379 | vm.mapPoints(devicePts, ptsRot, 2); |
| 380 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 381 | if (hasCap) { |
| 382 | lineLength += 2.f * halfDevStroke; |
| 383 | } |
| 384 | |
| 385 | SkRect bounds; |
| 386 | bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY); |
| 387 | bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke); |
| 388 | setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 389 | lineLength, halfDevStroke); |
| 390 | curVIdx += 4; |
| 391 | } |
| 392 | |
| 393 | if (hasStartRect) { |
| 394 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 395 | startRect.outset(bloatX, bloatY); |
| 396 | setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 397 | devIntervals[0], halfDevStroke); |
| 398 | curVIdx += 4; |
| 399 | } |
| 400 | |
| 401 | if (hasEndRect) { |
| 402 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 403 | endRect.outset(bloatX, bloatY); |
| 404 | setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 405 | devIntervals[0], halfDevStroke); |
| 406 | } |
| 407 | |
| 408 | target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer()); |
| 409 | target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6); |
| 410 | target->resetIndexSource(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 411 | return true; |
| 412 | } |
| 413 | |
| 414 | ////////////////////////////////////////////////////////////////////////////// |
| 415 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 416 | class GLDashingCircleEffect; |
| 417 | /* |
| 418 | * This effect will draw a dotted line (defined as a dashed lined with round caps and no on |
| 419 | * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo. |
| 420 | * Both of the previous two parameters are in device space. This effect also requires the setting of |
| 421 | * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the |
| 422 | * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we |
| 423 | * transform the line to be horizontal, with the start of line at the origin then shifted to the |
| 424 | * right by half the off interval. The line then goes in the positive x direction. |
| 425 | */ |
| 426 | class DashingCircleEffect : public GrVertexEffect { |
| 427 | public: |
| 428 | typedef SkPathEffect::DashInfo DashInfo; |
| 429 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 430 | static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 431 | |
| 432 | virtual ~DashingCircleEffect(); |
| 433 | |
| 434 | static const char* Name() { return "DashingCircleEffect"; } |
| 435 | |
| 436 | GrEffectEdgeType getEdgeType() const { return fEdgeType; } |
| 437 | |
| 438 | SkScalar getRadius() const { return fRadius; } |
| 439 | |
| 440 | SkScalar getCenterX() const { return fCenterX; } |
| 441 | |
| 442 | SkScalar getIntervalLength() const { return fIntervalLength; } |
| 443 | |
| 444 | typedef GLDashingCircleEffect GLEffect; |
| 445 | |
| 446 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 447 | |
| 448 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 449 | |
| 450 | private: |
| 451 | DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius); |
| 452 | |
| 453 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 454 | |
| 455 | GrEffectEdgeType fEdgeType; |
| 456 | SkScalar fIntervalLength; |
| 457 | SkScalar fRadius; |
| 458 | SkScalar fCenterX; |
| 459 | |
| 460 | GR_DECLARE_EFFECT_TEST; |
| 461 | |
| 462 | typedef GrVertexEffect INHERITED; |
| 463 | }; |
| 464 | |
| 465 | ////////////////////////////////////////////////////////////////////////////// |
| 466 | |
| 467 | class GLDashingCircleEffect : public GrGLVertexEffect { |
| 468 | public: |
| 469 | GLDashingCircleEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 470 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 471 | virtual void emitCode(GrGLFullProgramBuilder* builder, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 472 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 473 | const GrEffectKey& key, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 474 | const char* outputColor, |
| 475 | const char* inputColor, |
| 476 | const TransformedCoordsArray&, |
| 477 | const TextureSamplerArray&) SK_OVERRIDE; |
| 478 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 479 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 480 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 481 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 482 | |
| 483 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 484 | GrGLProgramDataManager::UniformHandle fParamUniform; |
| 485 | SkScalar fPrevRadius; |
| 486 | SkScalar fPrevCenterX; |
| 487 | SkScalar fPrevIntervalLength; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 488 | typedef GrGLVertexEffect INHERITED; |
| 489 | }; |
| 490 | |
| 491 | GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendEffectFactory& factory, |
| 492 | const GrDrawEffect& drawEffect) |
| 493 | : INHERITED (factory) { |
| 494 | fPrevRadius = SK_ScalarMin; |
| 495 | fPrevCenterX = SK_ScalarMin; |
| 496 | fPrevIntervalLength = SK_ScalarMax; |
| 497 | } |
| 498 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 499 | void GLDashingCircleEffect::emitCode(GrGLFullProgramBuilder* builder, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 500 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 501 | const GrEffectKey& key, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 502 | const char* outputColor, |
| 503 | const char* inputColor, |
| 504 | const TransformedCoordsArray&, |
| 505 | const TextureSamplerArray& samplers) { |
| 506 | const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>(); |
| 507 | const char *paramName; |
| 508 | // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and |
| 509 | // the total interval length of the dash. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 510 | fParamUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 511 | kVec3f_GrSLType, |
| 512 | "params", |
| 513 | ¶mName); |
| 514 | |
| 515 | const char *vsCoordName, *fsCoordName; |
| 516 | builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 517 | |
| 518 | GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 519 | const SkString* attr0Name = |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 520 | vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 521 | vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str()); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 522 | |
| 523 | // transforms all points so that we can compare them to our test circle |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 524 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 525 | fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n", |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 526 | fsCoordName, fsCoordName, paramName, paramName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 527 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName); |
| 528 | fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName); |
| 529 | fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 530 | if (GrEffectEdgeTypeIsAA(dce.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 531 | fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName); |
| 532 | fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n"); |
| 533 | fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 534 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 535 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 536 | fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 537 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 538 | fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 539 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 540 | } |
| 541 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 542 | void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 543 | const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>(); |
| 544 | SkScalar radius = dce.getRadius(); |
| 545 | SkScalar centerX = dce.getCenterX(); |
| 546 | SkScalar intervalLength = dce.getIntervalLength(); |
| 547 | if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 548 | pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 549 | fPrevRadius = radius; |
| 550 | fPrevCenterX = centerX; |
| 551 | fPrevIntervalLength = intervalLength; |
| 552 | } |
| 553 | } |
| 554 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 555 | void GLDashingCircleEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
| 556 | GrEffectKeyBuilder* b) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 557 | const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 558 | b->add32(dce.getEdgeType()); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | ////////////////////////////////////////////////////////////////////////////// |
| 562 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 563 | GrEffect* DashingCircleEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info, |
| 564 | SkScalar radius) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 565 | if (info.fCount != 2 || info.fIntervals[0] != 0) { |
| 566 | return NULL; |
| 567 | } |
| 568 | |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 569 | return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | DashingCircleEffect::~DashingCircleEffect() {} |
| 573 | |
| 574 | void DashingCircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 575 | *validFlags = 0; |
| 576 | } |
| 577 | |
| 578 | const GrBackendEffectFactory& DashingCircleEffect::getFactory() const { |
| 579 | return GrTBackendEffectFactory<DashingCircleEffect>::getInstance(); |
| 580 | } |
| 581 | |
| 582 | DashingCircleEffect::DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info, |
| 583 | SkScalar radius) |
| 584 | : fEdgeType(edgeType) { |
| 585 | SkScalar onLen = info.fIntervals[0]; |
| 586 | SkScalar offLen = info.fIntervals[1]; |
| 587 | fIntervalLength = onLen + offLen; |
| 588 | fRadius = radius; |
| 589 | fCenterX = SkScalarHalf(offLen); |
| 590 | |
| 591 | this->addVertexAttrib(kVec2f_GrSLType); |
| 592 | } |
| 593 | |
| 594 | bool DashingCircleEffect::onIsEqual(const GrEffect& other) const { |
| 595 | const DashingCircleEffect& dce = CastEffect<DashingCircleEffect>(other); |
| 596 | return (fEdgeType == dce.fEdgeType && |
| 597 | fIntervalLength == dce.fIntervalLength && |
| 598 | fRadius == dce.fRadius && |
| 599 | fCenterX == dce.fCenterX); |
| 600 | } |
| 601 | |
| 602 | GR_DEFINE_EFFECT_TEST(DashingCircleEffect); |
| 603 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 604 | GrEffect* DashingCircleEffect::TestCreate(SkRandom* random, |
| 605 | GrContext*, |
| 606 | const GrDrawTargetCaps& caps, |
| 607 | GrTexture*[]) { |
| 608 | GrEffect* effect; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 609 | GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan( |
| 610 | kGrEffectEdgeTypeCnt)); |
| 611 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 612 | DashInfo info; |
| 613 | info.fCount = 2; |
| 614 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 615 | info.fIntervals = intervals.get(); |
| 616 | info.fIntervals[0] = 0; |
| 617 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 618 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]); |
| 619 | |
| 620 | effect = DashingCircleEffect::Create(edgeType, info, strokeWidth); |
| 621 | return effect; |
| 622 | } |
| 623 | |
| 624 | ////////////////////////////////////////////////////////////////////////////// |
| 625 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 626 | class GLDashingLineEffect; |
| 627 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 628 | /* |
| 629 | * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the |
| 630 | * length and spacing by the DashInfo. Both of the previous two parameters are in device space. |
| 631 | * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the |
| 632 | * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the |
| 633 | * vertex coords (in device space) if we transform the line to be horizontal, with the start of |
| 634 | * line at the origin then shifted to the right by half the off interval. The line then goes in the |
| 635 | * positive x direction. |
| 636 | */ |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 637 | class DashingLineEffect : public GrVertexEffect { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 638 | public: |
| 639 | typedef SkPathEffect::DashInfo DashInfo; |
| 640 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 641 | static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 642 | |
| 643 | virtual ~DashingLineEffect(); |
| 644 | |
| 645 | static const char* Name() { return "DashingEffect"; } |
| 646 | |
| 647 | GrEffectEdgeType getEdgeType() const { return fEdgeType; } |
| 648 | |
| 649 | const SkRect& getRect() const { return fRect; } |
| 650 | |
| 651 | SkScalar getIntervalLength() const { return fIntervalLength; } |
| 652 | |
| 653 | typedef GLDashingLineEffect GLEffect; |
| 654 | |
| 655 | virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE; |
| 656 | |
| 657 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE; |
| 658 | |
| 659 | private: |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 660 | DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 661 | |
| 662 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE; |
| 663 | |
| 664 | GrEffectEdgeType fEdgeType; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 665 | SkRect fRect; |
| 666 | SkScalar fIntervalLength; |
| 667 | |
| 668 | GR_DECLARE_EFFECT_TEST; |
| 669 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 670 | typedef GrVertexEffect INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 671 | }; |
| 672 | |
| 673 | ////////////////////////////////////////////////////////////////////////////// |
| 674 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 675 | class GLDashingLineEffect : public GrGLVertexEffect { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 676 | public: |
| 677 | GLDashingLineEffect(const GrBackendEffectFactory&, const GrDrawEffect&); |
| 678 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 679 | virtual void emitCode(GrGLFullProgramBuilder* builder, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 680 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 681 | const GrEffectKey& key, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 682 | const char* outputColor, |
| 683 | const char* inputColor, |
| 684 | const TransformedCoordsArray&, |
| 685 | const TextureSamplerArray&) SK_OVERRIDE; |
| 686 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 687 | static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 688 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 689 | virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 690 | |
| 691 | private: |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 692 | GrGLProgramDataManager::UniformHandle fRectUniform; |
| 693 | GrGLProgramDataManager::UniformHandle fIntervalUniform; |
| 694 | SkRect fPrevRect; |
| 695 | SkScalar fPrevIntervalLength; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 696 | typedef GrGLVertexEffect INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 697 | }; |
| 698 | |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 699 | GLDashingLineEffect::GLDashingLineEffect(const GrBackendEffectFactory& factory, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 700 | const GrDrawEffect& drawEffect) |
| 701 | : INHERITED (factory) { |
| 702 | fPrevRect.fLeft = SK_ScalarNaN; |
| 703 | fPrevIntervalLength = SK_ScalarMax; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 704 | } |
| 705 | |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 706 | void GLDashingLineEffect::emitCode(GrGLFullProgramBuilder* builder, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 707 | const GrDrawEffect& drawEffect, |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 708 | const GrEffectKey& key, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 709 | const char* outputColor, |
| 710 | const char* inputColor, |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 711 | const TransformedCoordsArray&, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 712 | const TextureSamplerArray& samplers) { |
| 713 | const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>(); |
| 714 | const char *rectName; |
| 715 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 716 | // respectively. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 717 | fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 718 | kVec4f_GrSLType, |
| 719 | "rect", |
| 720 | &rectName); |
| 721 | const char *intervalName; |
| 722 | // The interval uniform's refers to the total length of the interval (on + off) |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 723 | fIntervalUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 724 | kFloat_GrSLType, |
| 725 | "interval", |
| 726 | &intervalName); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 727 | |
| 728 | const char *vsCoordName, *fsCoordName; |
| 729 | builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 730 | GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder(); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 731 | const SkString* attr0Name = |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 732 | vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 733 | vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str()); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 734 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 735 | // transforms all points so that we can compare them to our test rect |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 736 | GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder(); |
| 737 | fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n", |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 738 | fsCoordName, fsCoordName, intervalName, intervalName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 739 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 740 | if (GrEffectEdgeTypeIsAA(de.getEdgeType())) { |
| 741 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 742 | // numbers, xSub and ySub. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 743 | fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
| 744 | fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName); |
| 745 | fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName); |
| 746 | fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName); |
| 747 | fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 748 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 749 | // covered. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 750 | fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 751 | } else { |
| 752 | // Assuming the bounding geometry is tight so no need to check y values |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 753 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 754 | fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName); |
| 755 | fsBuilder->codeAppendf("\t\talpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;\n", rectName); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 756 | } |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 757 | fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 758 | (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str()); |
| 759 | } |
| 760 | |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 761 | void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 762 | const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>(); |
| 763 | const SkRect& rect = de.getRect(); |
| 764 | SkScalar intervalLength = de.getIntervalLength(); |
| 765 | if (rect != fPrevRect || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 766 | pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 767 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
| 768 | pdman.set1f(fIntervalUniform, intervalLength); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 769 | fPrevRect = rect; |
| 770 | fPrevIntervalLength = intervalLength; |
| 771 | } |
| 772 | } |
| 773 | |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 774 | void GLDashingLineEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&, |
| 775 | GrEffectKeyBuilder* b) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 776 | const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>(); |
bsalomon | 63e99f7 | 2014-07-21 08:03:14 -0700 | [diff] [blame] | 777 | b->add32(de.getEdgeType()); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | ////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 781 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 782 | GrEffect* DashingLineEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info, |
| 783 | SkScalar strokeWidth) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 784 | if (info.fCount != 2) { |
| 785 | return NULL; |
| 786 | } |
| 787 | |
bsalomon | 55fad7a | 2014-07-08 07:34:20 -0700 | [diff] [blame] | 788 | return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 789 | } |
| 790 | |
| 791 | DashingLineEffect::~DashingLineEffect() {} |
| 792 | |
| 793 | void DashingLineEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const { |
| 794 | *validFlags = 0; |
| 795 | } |
| 796 | |
| 797 | const GrBackendEffectFactory& DashingLineEffect::getFactory() const { |
| 798 | return GrTBackendEffectFactory<DashingLineEffect>::getInstance(); |
| 799 | } |
| 800 | |
| 801 | DashingLineEffect::DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info, |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 802 | SkScalar strokeWidth) |
| 803 | : fEdgeType(edgeType) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 804 | SkScalar onLen = info.fIntervals[0]; |
| 805 | SkScalar offLen = info.fIntervals[1]; |
| 806 | SkScalar halfOffLen = SkScalarHalf(offLen); |
| 807 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 808 | fIntervalLength = onLen + offLen; |
| 809 | fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke); |
| 810 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 811 | this->addVertexAttrib(kVec2f_GrSLType); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | bool DashingLineEffect::onIsEqual(const GrEffect& other) const { |
| 815 | const DashingLineEffect& de = CastEffect<DashingLineEffect>(other); |
| 816 | return (fEdgeType == de.fEdgeType && |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 817 | fRect == de.fRect && |
| 818 | fIntervalLength == de.fIntervalLength); |
| 819 | } |
| 820 | |
| 821 | GR_DEFINE_EFFECT_TEST(DashingLineEffect); |
| 822 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 823 | GrEffect* DashingLineEffect::TestCreate(SkRandom* random, |
| 824 | GrContext*, |
| 825 | const GrDrawTargetCaps& caps, |
| 826 | GrTexture*[]) { |
| 827 | GrEffect* effect; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 828 | GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan( |
| 829 | kGrEffectEdgeTypeCnt)); |
| 830 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 831 | DashInfo info; |
| 832 | info.fCount = 2; |
| 833 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 834 | info.fIntervals = intervals.get(); |
| 835 | info.fIntervals[0] = random->nextRangeScalar(0, 10.f); |
| 836 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 837 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]); |
| 838 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 839 | effect = DashingLineEffect::Create(edgeType, info, strokeWidth); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 840 | return effect; |
| 841 | } |
| 842 | |
| 843 | ////////////////////////////////////////////////////////////////////////////// |
| 844 | |
bsalomon | 83d081a | 2014-07-08 09:56:10 -0700 | [diff] [blame] | 845 | GrEffect* GrDashingEffect::Create(GrEffectEdgeType edgeType, const SkPathEffect::DashInfo& info, |
| 846 | SkScalar strokeWidth, GrDashingEffect::DashCap cap) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 847 | switch (cap) { |
| 848 | case GrDashingEffect::kRound_DashCap: |
| 849 | return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth)); |
| 850 | case GrDashingEffect::kNonRound_DashCap: |
| 851 | return DashingLineEffect::Create(edgeType, info, strokeWidth); |
| 852 | default: |
| 853 | SkFAIL("Unexpected dashed cap."); |
| 854 | } |
| 855 | return NULL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 856 | } |