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