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" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 21 | #include "SkGr.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 22 | #include "gl/GrGLGeometryProcessor.h" |
| 23 | #include "gl/GrGLProcessor.h" |
| 24 | #include "gl/GrGLSL.h" |
| 25 | #include "gl/builders/GrGLProgramBuilder.h" |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 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, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 31 | const GrDrawTarget& target, const GrDrawState& ds, |
| 32 | const SkMatrix& viewMatrix) { |
| 33 | if (ds.getRenderTarget()->isMultisampled()) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 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 { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 67 | struct DashLineVertex { |
| 68 | SkPoint fPos; |
| 69 | SkPoint fDashPos; |
| 70 | }; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 73 | static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale, |
| 74 | const SkMatrix& viewMatrix, const SkPoint pts[2]) { |
| 75 | SkVector vecSrc = pts[1] - pts[0]; |
| 76 | SkScalar magSrc = vecSrc.length(); |
| 77 | SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0; |
| 78 | vecSrc.scale(invSrc); |
| 79 | |
| 80 | SkVector vecSrcPerp; |
| 81 | vecSrc.rotateCW(&vecSrcPerp); |
| 82 | viewMatrix.mapVectors(&vecSrc, 1); |
| 83 | viewMatrix.mapVectors(&vecSrcPerp, 1); |
| 84 | |
| 85 | // parallelScale tells how much to scale along the line parallel to the dash line |
| 86 | // perpScale tells how much to scale in the direction perpendicular to the dash line |
| 87 | *parallelScale = vecSrc.length(); |
| 88 | *perpScale = vecSrcPerp.length(); |
| 89 | } |
| 90 | |
| 91 | // calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1] |
| 92 | // Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot |
| 93 | static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) { |
| 94 | SkVector vec = pts[1] - pts[0]; |
| 95 | SkScalar mag = vec.length(); |
| 96 | SkScalar inv = mag ? SkScalarInvert(mag) : 0; |
| 97 | |
| 98 | vec.scale(inv); |
| 99 | rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY); |
| 100 | if (ptsRot) { |
| 101 | rotMatrix->mapPoints(ptsRot, pts, 2); |
| 102 | // correction for numerical issues if map doesn't make ptsRot exactly horizontal |
| 103 | ptsRot[1].fY = pts[0].fY; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Assumes phase < sum of all intervals |
| 108 | static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) { |
| 109 | SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| 110 | if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) { |
| 111 | SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| 112 | return srcIntervalLen - info.fPhase; |
| 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 117 | static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2], |
| 118 | SkScalar phase, SkScalar* endingInt) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 119 | if (pts[1].fX <= pts[0].fX) { |
| 120 | return 0; |
| 121 | } |
| 122 | SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1]; |
| 123 | SkScalar totalLen = pts[1].fX - pts[0].fX; |
| 124 | SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen); |
| 125 | SkScalar numFullIntervals = SkScalarFloorToScalar(temp); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 126 | *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 127 | temp = SkScalarDiv(*endingInt, srcIntervalLen); |
| 128 | *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen; |
| 129 | if (0 == *endingInt) { |
| 130 | *endingInt = srcIntervalLen; |
| 131 | } |
| 132 | if (*endingInt > info.fIntervals[0]) { |
| 133 | if (0 == info.fIntervals[0]) { |
commit-bot@chromium.org | ad88340 | 2014-05-19 14:43:45 +0000 | [diff] [blame] | 134 | *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] | 135 | } |
| 136 | return *endingInt - info.fIntervals[0]; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 141 | static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix, |
| 142 | SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 143 | SkScalar startDashX = offset - bloat; |
| 144 | SkScalar endDashX = offset + len + bloat; |
| 145 | SkScalar startDashY = -stroke - bloat; |
| 146 | SkScalar endDashY = stroke + bloat; |
| 147 | verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY); |
| 148 | verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY); |
| 149 | verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY); |
| 150 | verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 151 | verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop); |
| 152 | verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom); |
| 153 | verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom); |
| 154 | verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 155 | matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4); |
| 156 | } |
| 157 | |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 158 | static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix, |
| 159 | SkPoint* verts) { |
| 160 | verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop); |
| 161 | verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom); |
| 162 | verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom); |
| 163 | verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop); |
| 164 | matrix.mapPoints(&verts[idx], 4); |
| 165 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 166 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 167 | bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target, GrDrawState* drawState, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 168 | GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2], |
| 169 | const GrPaint& paint, const GrStrokeInfo& strokeInfo) { |
| 170 | if (!can_fast_path_dash(pts, strokeInfo, *target, *drawState, viewMatrix)) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 171 | return false; |
| 172 | } |
| 173 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 174 | const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 175 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 176 | SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 177 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 178 | SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 179 | |
| 180 | // the phase should be normalized to be [0, sum of all intervals) |
| 181 | SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]); |
| 182 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 183 | SkScalar srcPhase = info.fPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 184 | |
| 185 | // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX |
| 186 | SkMatrix srcRotInv; |
| 187 | SkPoint ptsRot[2]; |
| 188 | if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 189 | SkMatrix rotMatrix; |
| 190 | align_to_x_axis(pts, &rotMatrix, ptsRot); |
| 191 | if(!rotMatrix.invert(&srcRotInv)) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 192 | SkDebugf("Failed to create invertible rotation matrix!\n"); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 193 | return false; |
| 194 | } |
| 195 | } else { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 196 | srcRotInv.reset(); |
| 197 | memcpy(ptsRot, pts, 2 * sizeof(SkPoint)); |
| 198 | } |
| 199 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 200 | bool useAA = paint.isAntiAlias(); |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 201 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 202 | // Scale corrections of intervals and stroke from view matrix |
| 203 | SkScalar parallelScale; |
| 204 | SkScalar perpScale; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 205 | calc_dash_scaling(¶llelScale, &perpScale, viewMatrix, ptsRot); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 206 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 207 | bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 208 | |
| 209 | // We always want to at least stroke out half a pixel on each side in device space |
| 210 | // so 0.5f / perpScale gives us this min in src space |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 211 | SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 212 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 213 | SkScalar strokeAdj; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 214 | if (!hasCap) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 215 | strokeAdj = 0.f; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 216 | } else { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 217 | strokeAdj = halfSrcStroke; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 218 | } |
| 219 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 220 | SkScalar startAdj = 0; |
| 221 | |
| 222 | SkMatrix combinedMatrix = srcRotInv; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 223 | combinedMatrix.postConcat(viewMatrix); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 224 | |
| 225 | bool lineDone = false; |
| 226 | SkRect startRect; |
| 227 | bool hasStartRect = false; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 228 | // If we are using AA, check to see if we are drawing a partial dash at the start. If so |
| 229 | // draw it separately here and adjust our start point accordingly |
| 230 | if (useAA) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 231 | if (srcPhase > 0 && srcPhase < info.fIntervals[0]) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 232 | SkPoint startPts[2]; |
| 233 | startPts[0] = ptsRot[0]; |
| 234 | startPts[1].fY = startPts[0].fY; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 235 | 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] | 236 | ptsRot[1].fX); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 237 | startRect.set(startPts, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 238 | startRect.outset(strokeAdj, halfSrcStroke); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 239 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 240 | hasStartRect = true; |
| 241 | startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | // adjustments for start and end of bounding rect so we only draw dash intervals |
| 246 | // contained in the original line segment. |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 247 | startAdj += calc_start_adjustment(info); |
| 248 | if (startAdj != 0) { |
| 249 | ptsRot[0].fX += startAdj; |
| 250 | srcPhase = 0; |
| 251 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 252 | SkScalar endingInterval = 0; |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 253 | SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval); |
| 254 | ptsRot[1].fX -= endAdj; |
| 255 | if (ptsRot[0].fX >= ptsRot[1].fX) { |
| 256 | lineDone = true; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 257 | } |
| 258 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 259 | SkRect endRect; |
| 260 | bool hasEndRect = false; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 261 | // If we are using AA, check to see if we are drawing a partial dash at then end. If so |
| 262 | // draw it separately here and adjust our end point accordingly |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 263 | if (useAA && !lineDone) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 264 | // If we adjusted the end then we will not be drawing a partial dash at the end. |
| 265 | // If we didn't adjust the end point then we just need to make sure the ending |
| 266 | // dash isn't a full dash |
| 267 | if (0 == endAdj && endingInterval != info.fIntervals[0]) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 268 | SkPoint endPts[2]; |
| 269 | endPts[1] = ptsRot[1]; |
| 270 | endPts[0].fY = endPts[1].fY; |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 271 | endPts[0].fX = endPts[1].fX - endingInterval; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 272 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 273 | endRect.set(endPts, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 274 | endRect.outset(strokeAdj, halfSrcStroke); |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 275 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 276 | hasEndRect = true; |
| 277 | endAdj = endingInterval + info.fIntervals[1]; |
| 278 | |
| 279 | ptsRot[1].fX -= endAdj; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 280 | if (ptsRot[0].fX >= ptsRot[1].fX) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 281 | lineDone = true; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 282 | } |
| 283 | } |
| 284 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 285 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 286 | if (startAdj != 0) { |
| 287 | srcPhase = 0; |
| 288 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 289 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 290 | // Change the dashing info from src space into device space |
| 291 | SkScalar devIntervals[2]; |
| 292 | devIntervals[0] = info.fIntervals[0] * parallelScale; |
| 293 | devIntervals[1] = info.fIntervals[1] * parallelScale; |
| 294 | SkScalar devPhase = srcPhase * parallelScale; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 295 | SkScalar strokeWidth = srcStrokeWidth * perpScale; |
| 296 | |
| 297 | if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) { |
| 298 | strokeWidth = 1.f; |
| 299 | } |
| 300 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 301 | SkScalar halfDevStroke = strokeWidth * 0.5f; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 302 | |
| 303 | if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) { |
| 304 | // add cap to on interveal and remove from off interval |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 305 | devIntervals[0] += strokeWidth; |
| 306 | devIntervals[1] -= strokeWidth; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 307 | } |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 308 | SkScalar startOffset = devIntervals[1] * 0.5f + devPhase; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 309 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 310 | SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f; |
| 311 | SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f; |
| 312 | |
| 313 | SkScalar devBloat = useAA ? 0.5f : 0.f; |
| 314 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 315 | if (devIntervals[1] <= 0.f && useAA) { |
| 316 | // Case when we end up drawing a solid AA rect |
| 317 | // Reset the start rect to draw this single solid rect |
| 318 | // but it requires to upload a new intervals uniform so we can mimic |
| 319 | // one giant dash |
| 320 | ptsRot[0].fX -= hasStartRect ? startAdj : 0; |
| 321 | ptsRot[1].fX += hasEndRect ? endAdj : 0; |
| 322 | startRect.set(ptsRot, 2); |
| 323 | startRect.outset(strokeAdj, halfSrcStroke); |
| 324 | hasStartRect = true; |
| 325 | hasEndRect = false; |
| 326 | lineDone = true; |
| 327 | |
| 328 | SkPoint devicePts[2]; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 329 | viewMatrix.mapPoints(devicePts, ptsRot, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 330 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 331 | if (hasCap) { |
| 332 | lineLength += 2.f * halfDevStroke; |
| 333 | } |
| 334 | devIntervals[0] = lineLength; |
| 335 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 336 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 337 | // reset to device coordinates |
| 338 | SkMatrix invert; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 339 | if (!viewMatrix.invert(&invert)) { |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 340 | SkDebugf("Failed to invert\n"); |
| 341 | return false; |
| 342 | } |
| 343 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 344 | SkAutoTUnref<const GrGeometryProcessor> gp; |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 345 | bool fullDash = devIntervals[1] > 0.f || useAA; |
| 346 | if (fullDash) { |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 347 | SkPathEffect::DashInfo devInfo; |
| 348 | devInfo.fPhase = devPhase; |
| 349 | devInfo.fCount = 2; |
| 350 | devInfo.fIntervals = devIntervals; |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 351 | GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType : |
| 352 | kFillBW_GrProcessorEdgeType; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 353 | bool isRoundCap = SkPaint::kRound_Cap == cap; |
| 354 | GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap : |
| 355 | GrDashingEffect::kNonRound_DashCap; |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 356 | gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType, invert)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 357 | } else { |
| 358 | // Set up the vertex data for the line and start/end dashes |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 359 | gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType, |
| 360 | color, |
| 361 | SkMatrix::I(), |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 362 | invert)); |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 363 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 364 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 365 | int totalRectCnt = 0; |
| 366 | |
| 367 | totalRectCnt += !lineDone ? 1 : 0; |
| 368 | totalRectCnt += hasStartRect ? 1 : 0; |
| 369 | totalRectCnt += hasEndRect ? 1 : 0; |
| 370 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 371 | GrDrawTarget::AutoReleaseGeometry geo(target, |
| 372 | totalRectCnt * 4, |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 373 | gp->getVertexStride(), 0); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 374 | if (!geo.succeeded()) { |
tfarina | 38406c8 | 2014-10-31 07:11:12 -0700 | [diff] [blame] | 375 | SkDebugf("Failed to get space for vertices!\n"); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 376 | return false; |
| 377 | } |
| 378 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 379 | int curVIdx = 0; |
| 380 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 381 | if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) { |
| 382 | // need to adjust this for round caps to correctly set the dashPos attrib on vertices |
| 383 | startOffset -= halfDevStroke; |
| 384 | } |
| 385 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 386 | // Draw interior part of dashed line |
| 387 | if (!lineDone) { |
| 388 | SkPoint devicePts[2]; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 389 | viewMatrix.mapPoints(devicePts, ptsRot, 2); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 390 | SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]); |
| 391 | if (hasCap) { |
| 392 | lineLength += 2.f * halfDevStroke; |
| 393 | } |
| 394 | |
| 395 | SkRect bounds; |
| 396 | bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY); |
| 397 | bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 398 | if (fullDash) { |
| 399 | DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 400 | SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 401 | setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 402 | lineLength, halfDevStroke); |
| 403 | } else { |
| 404 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 405 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 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()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 416 | SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 417 | setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 418 | devIntervals[0], halfDevStroke); |
| 419 | } else { |
| 420 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 421 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 422 | setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts); |
| 423 | } |
| 424 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 425 | curVIdx += 4; |
| 426 | } |
| 427 | |
| 428 | if (hasEndRect) { |
| 429 | SkASSERT(useAA); // so that we know bloatX and bloatY have been set |
| 430 | endRect.outset(bloatX, bloatY); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 431 | if (fullDash) { |
| 432 | DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 433 | SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 434 | setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat, |
| 435 | devIntervals[0], halfDevStroke); |
| 436 | } else { |
| 437 | SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices()); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 438 | SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 439 | setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts); |
| 440 | } |
| 441 | |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer()); |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 445 | target->drawIndexedInstances(drawState, gp, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 446 | target->resetIndexSource(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 447 | return true; |
| 448 | } |
| 449 | |
| 450 | ////////////////////////////////////////////////////////////////////////////// |
| 451 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 452 | class GLDashingCircleEffect; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 453 | |
| 454 | struct DashingCircleBatchTracker { |
| 455 | GrGPInput fInputColorType; |
| 456 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 457 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 458 | }; |
| 459 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 460 | /* |
| 461 | * This effect will draw a dotted line (defined as a dashed lined with round caps and no on |
| 462 | * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo. |
| 463 | * Both of the previous two parameters are in device space. This effect also requires the setting of |
| 464 | * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the |
| 465 | * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we |
| 466 | * transform the line to be horizontal, with the start of line at the origin then shifted to the |
| 467 | * right by half the off interval. The line then goes in the positive x direction. |
| 468 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 469 | class DashingCircleEffect : public GrGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 470 | public: |
| 471 | typedef SkPathEffect::DashInfo DashInfo; |
| 472 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 473 | static GrGeometryProcessor* Create(GrColor, |
| 474 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 475 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 476 | SkScalar radius, |
| 477 | const SkMatrix& localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 478 | |
| 479 | virtual ~DashingCircleEffect(); |
| 480 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 481 | const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 482 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 483 | const GrAttribute* inPosition() const { return fInPosition; } |
| 484 | |
| 485 | const GrAttribute* inCoord() const { return fInCoord; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 486 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 487 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 488 | |
| 489 | SkScalar getRadius() const { return fRadius; } |
| 490 | |
| 491 | SkScalar getCenterX() const { return fCenterX; } |
| 492 | |
| 493 | SkScalar getIntervalLength() const { return fIntervalLength; } |
| 494 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 495 | virtual void getGLProcessorKey(const GrBatchTracker&, |
| 496 | const GrGLCaps&, |
| 497 | GrProcessorKeyBuilder* b) const SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 498 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 499 | GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 500 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 501 | void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE; |
| 502 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 503 | bool onCanMakeEqual(const GrBatchTracker&, |
| 504 | const GrGeometryProcessor&, |
| 505 | const GrBatchTracker&) const SK_OVERRIDE; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 506 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 507 | private: |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 508 | DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 509 | SkScalar radius, const SkMatrix& localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 510 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 511 | bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 512 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 513 | void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 514 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 515 | GrPrimitiveEdgeType fEdgeType; |
| 516 | const GrAttribute* fInPosition; |
| 517 | const GrAttribute* fInCoord; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 518 | SkScalar fIntervalLength; |
| 519 | SkScalar fRadius; |
| 520 | SkScalar fCenterX; |
| 521 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 522 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 523 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 524 | typedef GrGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 525 | }; |
| 526 | |
| 527 | ////////////////////////////////////////////////////////////////////////////// |
| 528 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 529 | class GLDashingCircleEffect : public GrGLGeometryProcessor { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 530 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 531 | GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 532 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 533 | void emitCode(const EmitArgs&) SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 534 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 535 | static inline void GenKey(const GrGeometryProcessor&, |
| 536 | const GrBatchTracker&, |
| 537 | const GrGLCaps&, |
| 538 | GrProcessorKeyBuilder*); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 539 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 540 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 541 | const GrPrimitiveProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 542 | const GrBatchTracker&) SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 543 | |
| 544 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 545 | UniformHandle fParamUniform; |
| 546 | UniformHandle fColorUniform; |
| 547 | GrColor fColor; |
| 548 | SkScalar fPrevRadius; |
| 549 | SkScalar fPrevCenterX; |
| 550 | SkScalar fPrevIntervalLength; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 551 | typedef GrGLGeometryProcessor INHERITED; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 552 | }; |
| 553 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 554 | GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&, |
| 555 | const GrBatchTracker&) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 556 | fColor = GrColor_ILLEGAL; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 557 | fPrevRadius = SK_ScalarMin; |
| 558 | fPrevCenterX = SK_ScalarMin; |
| 559 | fPrevIntervalLength = SK_ScalarMax; |
| 560 | } |
| 561 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 562 | void GLDashingCircleEffect::emitCode(const EmitArgs& args) { |
| 563 | const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 564 | const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>(); |
| 565 | GrGLGPBuilder* pb = args.fPB; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 566 | const char *paramName; |
| 567 | // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and |
| 568 | // the total interval length of the dash. |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 569 | fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 570 | kVec3f_GrSLType, kDefault_GrSLPrecision, |
| 571 | "params", ¶mName); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 572 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 573 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 574 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 575 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 576 | args.fPB->addVarying("Coord", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 577 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 578 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 579 | // Setup pass through color |
| 580 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform); |
| 581 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 582 | // setup coord outputs |
| 583 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName); |
| 584 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 585 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 586 | // setup uniform viewMatrix |
| 587 | this->addUniformViewMatrix(pb); |
| 588 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 589 | // setup position varying |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 590 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 591 | dce.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 592 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 593 | // transforms all points so that we can compare them to our test circle |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 594 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 595 | 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] | 596 | v.fsIn(), v.fsIn(), paramName, paramName); |
| 597 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 598 | fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName); |
| 599 | fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n"); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 600 | if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 601 | fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName); |
| 602 | fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n"); |
| 603 | fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 604 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 605 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 606 | 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] | 607 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 608 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 609 | } |
| 610 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 611 | void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 612 | const GrPrimitiveProcessor& processor, |
| 613 | const GrBatchTracker& bt) { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 614 | this->setUniformViewMatrix(pdman, processor.viewMatrix()); |
| 615 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 616 | const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 617 | SkScalar radius = dce.getRadius(); |
| 618 | SkScalar centerX = dce.getCenterX(); |
| 619 | SkScalar intervalLength = dce.getIntervalLength(); |
| 620 | if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 621 | pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 622 | fPrevRadius = radius; |
| 623 | fPrevCenterX = centerX; |
| 624 | fPrevIntervalLength = intervalLength; |
| 625 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 626 | |
| 627 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
| 628 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 629 | GrGLfloat c[4]; |
| 630 | GrColorToRGBAFloat(local.fColor, c); |
| 631 | pdman.set4fv(fColorUniform, 1, c); |
| 632 | fColor = local.fColor; |
| 633 | } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 634 | } |
| 635 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 636 | void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 637 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 638 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 639 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 640 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 641 | const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 642 | b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 643 | b->add32(dce.getEdgeType() << 16 | local.fInputColorType); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | ////////////////////////////////////////////////////////////////////////////// |
| 647 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 648 | GrGeometryProcessor* DashingCircleEffect::Create(GrColor color, |
| 649 | GrPrimitiveEdgeType edgeType, |
| 650 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 651 | SkScalar radius, |
| 652 | const SkMatrix& localMatrix) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 653 | if (info.fCount != 2 || info.fIntervals[0] != 0) { |
| 654 | return NULL; |
| 655 | } |
| 656 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 657 | return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | DashingCircleEffect::~DashingCircleEffect() {} |
| 661 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 662 | void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 663 | out->setUnknownSingleComponent(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 664 | } |
| 665 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 666 | void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 667 | const GrGLCaps& caps, |
| 668 | GrProcessorKeyBuilder* b) const { |
| 669 | GLDashingCircleEffect::GenKey(*this, bt, caps, b); |
| 670 | } |
| 671 | |
| 672 | GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const { |
| 673 | return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 674 | } |
| 675 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 676 | DashingCircleEffect::DashingCircleEffect(GrColor color, |
| 677 | GrPrimitiveEdgeType edgeType, |
| 678 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 679 | SkScalar radius, |
| 680 | const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 681 | : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 682 | this->initClassID<DashingCircleEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 683 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
| 684 | fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 685 | SkScalar onLen = info.fIntervals[0]; |
| 686 | SkScalar offLen = info.fIntervals[1]; |
| 687 | fIntervalLength = onLen + offLen; |
| 688 | fRadius = radius; |
| 689 | fCenterX = SkScalarHalf(offLen); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 690 | } |
| 691 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 692 | bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 693 | const DashingCircleEffect& dce = other.cast<DashingCircleEffect>(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 694 | return (fEdgeType == dce.fEdgeType && |
| 695 | fIntervalLength == dce.fIntervalLength && |
| 696 | fRadius == dce.fRadius && |
| 697 | fCenterX == dce.fCenterX); |
| 698 | } |
| 699 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 700 | void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
| 701 | DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>(); |
| 702 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 703 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 704 | } |
| 705 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 706 | bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 707 | const GrGeometryProcessor& that, |
| 708 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 709 | const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>(); |
| 710 | const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 711 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 712 | that, theirs.fUsesLocalCoords) && |
| 713 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 714 | theirs.fInputColorType, theirs.fColor); |
| 715 | } |
| 716 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 717 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 718 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 719 | GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random, |
| 720 | GrContext*, |
| 721 | const GrDrawTargetCaps& caps, |
| 722 | GrTexture*[]) { |
| 723 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan( |
| 724 | kGrProcessorEdgeTypeCnt)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 725 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 726 | DashInfo info; |
| 727 | info.fCount = 2; |
| 728 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 729 | info.fIntervals = intervals.get(); |
| 730 | info.fIntervals[0] = 0; |
| 731 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 732 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]); |
| 733 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 734 | return DashingCircleEffect::Create(GrRandomColor(random), |
| 735 | edgeType, info, strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 736 | GrProcessorUnitTest::TestMatrix(random)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | ////////////////////////////////////////////////////////////////////////////// |
| 740 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 741 | class GLDashingLineEffect; |
| 742 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 743 | struct DashingLineBatchTracker { |
| 744 | GrGPInput fInputColorType; |
| 745 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 746 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 747 | }; |
| 748 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 749 | /* |
| 750 | * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the |
| 751 | * length and spacing by the DashInfo. Both of the previous two parameters are in device space. |
| 752 | * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the |
| 753 | * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the |
| 754 | * vertex coords (in device space) if we transform the line to be horizontal, with the start of |
| 755 | * line at the origin then shifted to the right by half the off interval. The line then goes in the |
| 756 | * positive x direction. |
| 757 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 758 | class DashingLineEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 759 | public: |
| 760 | typedef SkPathEffect::DashInfo DashInfo; |
| 761 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 762 | static GrGeometryProcessor* Create(GrColor, |
| 763 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 764 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 765 | SkScalar strokeWidth, |
| 766 | const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 767 | |
| 768 | virtual ~DashingLineEffect(); |
| 769 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 770 | const char* name() const SK_OVERRIDE { return "DashingEffect"; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 771 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 772 | const GrAttribute* inPosition() const { return fInPosition; } |
| 773 | |
| 774 | const GrAttribute* inCoord() const { return fInCoord; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 775 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 776 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 777 | |
| 778 | const SkRect& getRect() const { return fRect; } |
| 779 | |
| 780 | SkScalar getIntervalLength() const { return fIntervalLength; } |
| 781 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 782 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 783 | const GrGLCaps& caps, |
| 784 | GrProcessorKeyBuilder* b) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 785 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 786 | GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 787 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 788 | void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE; |
| 789 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 790 | bool onCanMakeEqual(const GrBatchTracker&, |
| 791 | const GrGeometryProcessor&, |
| 792 | const GrBatchTracker&) const SK_OVERRIDE; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 793 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 794 | private: |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 795 | DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 796 | SkScalar strokeWidth, const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 797 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 798 | bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 799 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 800 | void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 801 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 802 | GrPrimitiveEdgeType fEdgeType; |
| 803 | const GrAttribute* fInPosition; |
| 804 | const GrAttribute* fInCoord; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 805 | SkRect fRect; |
| 806 | SkScalar fIntervalLength; |
| 807 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 808 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 809 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 810 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 811 | }; |
| 812 | |
| 813 | ////////////////////////////////////////////////////////////////////////////// |
| 814 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 815 | class GLDashingLineEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 816 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 817 | GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 818 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame^] | 819 | void emitCode(const EmitArgs&) SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 820 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 821 | static inline void GenKey(const GrGeometryProcessor&, |
| 822 | const GrBatchTracker&, |
| 823 | const GrGLCaps&, |
| 824 | GrProcessorKeyBuilder*); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 825 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 826 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 827 | const GrPrimitiveProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 828 | const GrBatchTracker&) SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 829 | |
| 830 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 831 | GrColor fColor; |
| 832 | UniformHandle fRectUniform; |
| 833 | UniformHandle fIntervalUniform; |
| 834 | UniformHandle fColorUniform; |
| 835 | SkRect fPrevRect; |
| 836 | SkScalar fPrevIntervalLength; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 837 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 838 | }; |
| 839 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 840 | GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&, |
| 841 | const GrBatchTracker&) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 842 | fColor = GrColor_ILLEGAL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 843 | fPrevRect.fLeft = SK_ScalarNaN; |
| 844 | fPrevIntervalLength = SK_ScalarMax; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 845 | } |
| 846 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 847 | void GLDashingLineEffect::emitCode(const EmitArgs& args) { |
| 848 | const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 849 | const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>(); |
| 850 | GrGLGPBuilder* pb = args.fPB; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 851 | const char *rectName; |
| 852 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 853 | // respectively. |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 854 | fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 855 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 856 | "rect", |
| 857 | &rectName); |
| 858 | const char *intervalName; |
| 859 | // 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] | 860 | fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 861 | kFloat_GrSLType, kDefault_GrSLPrecision, |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 862 | "interval", |
| 863 | &intervalName); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 864 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 865 | |
| 866 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 867 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 868 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 869 | args.fPB->addVarying("Coord", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 870 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName); |
| 871 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 872 | // Setup pass through color |
| 873 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform); |
| 874 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 875 | // setup coord outputs |
| 876 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName); |
| 877 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 878 | |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 879 | // setup uniform viewMatrix |
| 880 | this->addUniformViewMatrix(pb); |
| 881 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 882 | // setup position varying |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 883 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 884 | de.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 885 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 886 | // transforms all points so that we can compare them to our test rect |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 887 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 888 | fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n", |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 889 | v.fsIn(), v.fsIn(), intervalName, intervalName); |
| 890 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 891 | if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 892 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 893 | // numbers, xSub and ySub. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 894 | fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
| 895 | fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName); |
| 896 | fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName); |
| 897 | fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName); |
| 898 | 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] | 899 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 900 | // covered. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 901 | 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] | 902 | } else { |
| 903 | // Assuming the bounding geometry is tight so no need to check y values |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 904 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 905 | fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName); |
| 906 | 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] | 907 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 908 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 909 | } |
| 910 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 911 | void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 912 | const GrPrimitiveProcessor& processor, |
| 913 | const GrBatchTracker& bt) { |
joshualitt | ee2af95 | 2014-12-30 09:04:15 -0800 | [diff] [blame] | 914 | this->setUniformViewMatrix(pdman, processor.viewMatrix()); |
| 915 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 916 | const DashingLineEffect& de = processor.cast<DashingLineEffect>(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 917 | const SkRect& rect = de.getRect(); |
| 918 | SkScalar intervalLength = de.getIntervalLength(); |
| 919 | if (rect != fPrevRect || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 920 | pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 921 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
| 922 | pdman.set1f(fIntervalUniform, intervalLength); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 923 | fPrevRect = rect; |
| 924 | fPrevIntervalLength = intervalLength; |
| 925 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 926 | |
| 927 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
| 928 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 929 | GrGLfloat c[4]; |
| 930 | GrColorToRGBAFloat(local.fColor, c); |
| 931 | pdman.set4fv(fColorUniform, 1, c); |
| 932 | fColor = local.fColor; |
| 933 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 934 | } |
| 935 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 936 | void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 937 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 938 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 939 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 940 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 941 | const DashingLineEffect& de = processor.cast<DashingLineEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 942 | b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 943 | b->add32(de.getEdgeType() << 16 | local.fInputColorType); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | ////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 947 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 948 | GrGeometryProcessor* DashingLineEffect::Create(GrColor color, |
| 949 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 950 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 951 | SkScalar strokeWidth, |
| 952 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 953 | if (info.fCount != 2) { |
| 954 | return NULL; |
| 955 | } |
| 956 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 957 | return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | DashingLineEffect::~DashingLineEffect() {} |
| 961 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 962 | void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 963 | out->setUnknownSingleComponent(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 964 | } |
| 965 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 966 | void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 967 | const GrGLCaps& caps, |
| 968 | GrProcessorKeyBuilder* b) const { |
| 969 | GLDashingLineEffect::GenKey(*this, bt, caps, b); |
| 970 | } |
| 971 | |
| 972 | GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const { |
| 973 | return SkNEW_ARGS(GLDashingLineEffect, (*this, bt)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 974 | } |
| 975 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 976 | DashingLineEffect::DashingLineEffect(GrColor color, |
| 977 | GrPrimitiveEdgeType edgeType, |
| 978 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 979 | SkScalar strokeWidth, |
| 980 | const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 981 | : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 982 | this->initClassID<DashingLineEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 983 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
| 984 | fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 985 | SkScalar onLen = info.fIntervals[0]; |
| 986 | SkScalar offLen = info.fIntervals[1]; |
| 987 | SkScalar halfOffLen = SkScalarHalf(offLen); |
| 988 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 989 | fIntervalLength = onLen + offLen; |
| 990 | fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 991 | } |
| 992 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 993 | bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 994 | const DashingLineEffect& de = other.cast<DashingLineEffect>(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 995 | return (fEdgeType == de.fEdgeType && |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 996 | fRect == de.fRect && |
| 997 | fIntervalLength == de.fIntervalLength); |
| 998 | } |
| 999 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1000 | void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
| 1001 | DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>(); |
| 1002 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1003 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1006 | bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 1007 | const GrGeometryProcessor& that, |
| 1008 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1009 | const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>(); |
| 1010 | const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1011 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 1012 | that, theirs.fUsesLocalCoords) && |
| 1013 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1014 | theirs.fInputColorType, theirs.fColor); |
| 1015 | } |
| 1016 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1017 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1018 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1019 | GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random, |
| 1020 | GrContext*, |
| 1021 | const GrDrawTargetCaps& caps, |
| 1022 | GrTexture*[]) { |
| 1023 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan( |
| 1024 | kGrProcessorEdgeTypeCnt)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1025 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 1026 | DashInfo info; |
| 1027 | info.fCount = 2; |
| 1028 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 1029 | info.fIntervals = intervals.get(); |
| 1030 | info.fIntervals[0] = random->nextRangeScalar(0, 10.f); |
| 1031 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 1032 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]); |
| 1033 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1034 | return DashingLineEffect::Create(GrRandomColor(random), |
| 1035 | edgeType, info, strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1036 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | ////////////////////////////////////////////////////////////////////////////// |
| 1040 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1041 | GrGeometryProcessor* GrDashingEffect::Create(GrColor color, |
| 1042 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1043 | const SkPathEffect::DashInfo& info, |
| 1044 | SkScalar strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1045 | GrDashingEffect::DashCap cap, |
| 1046 | const SkMatrix& localMatrix) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1047 | switch (cap) { |
| 1048 | case GrDashingEffect::kRound_DashCap: |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1049 | return DashingCircleEffect::Create(color, edgeType, info, |
| 1050 | SkScalarHalf(strokeWidth), |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1051 | localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1052 | case GrDashingEffect::kNonRound_DashCap: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1053 | return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1054 | default: |
| 1055 | SkFAIL("Unexpected dashed cap."); |
| 1056 | } |
| 1057 | return NULL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1058 | } |