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