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 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 481 | virtual 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 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 499 | virtual 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 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 511 | virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE; |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 512 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 513 | virtual 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 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 533 | virtual 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 | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 586 | // setup position varying |
| 587 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 588 | dce.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 589 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 590 | // transforms all points so that we can compare them to our test circle |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 591 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 592 | 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] | 593 | v.fsIn(), v.fsIn(), paramName, paramName); |
| 594 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn()); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 595 | fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName); |
| 596 | fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n"); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 597 | if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 598 | fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName); |
| 599 | fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n"); |
| 600 | fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n"); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 601 | } else { |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 602 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 603 | 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] | 604 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 605 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 606 | } |
| 607 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 608 | void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 609 | const GrPrimitiveProcessor& processor, |
| 610 | const GrBatchTracker& bt) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 611 | const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 612 | SkScalar radius = dce.getRadius(); |
| 613 | SkScalar centerX = dce.getCenterX(); |
| 614 | SkScalar intervalLength = dce.getIntervalLength(); |
| 615 | if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 616 | pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 617 | fPrevRadius = radius; |
| 618 | fPrevCenterX = centerX; |
| 619 | fPrevIntervalLength = intervalLength; |
| 620 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 621 | |
| 622 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
| 623 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 624 | GrGLfloat c[4]; |
| 625 | GrColorToRGBAFloat(local.fColor, c); |
| 626 | pdman.set4fv(fColorUniform, 1, c); |
| 627 | fColor = local.fColor; |
| 628 | } |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 629 | } |
| 630 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 631 | void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 632 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 633 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 634 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 635 | const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 636 | const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 637 | b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 638 | b->add32(dce.getEdgeType() << 16 | local.fInputColorType); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | ////////////////////////////////////////////////////////////////////////////// |
| 642 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 643 | GrGeometryProcessor* DashingCircleEffect::Create(GrColor color, |
| 644 | GrPrimitiveEdgeType edgeType, |
| 645 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 646 | SkScalar radius, |
| 647 | const SkMatrix& localMatrix) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 648 | if (info.fCount != 2 || info.fIntervals[0] != 0) { |
| 649 | return NULL; |
| 650 | } |
| 651 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 652 | return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | DashingCircleEffect::~DashingCircleEffect() {} |
| 656 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 657 | void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 658 | out->setUnknownSingleComponent(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 659 | } |
| 660 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 661 | void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 662 | const GrGLCaps& caps, |
| 663 | GrProcessorKeyBuilder* b) const { |
| 664 | GLDashingCircleEffect::GenKey(*this, bt, caps, b); |
| 665 | } |
| 666 | |
| 667 | GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const { |
| 668 | return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 669 | } |
| 670 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 671 | DashingCircleEffect::DashingCircleEffect(GrColor color, |
| 672 | GrPrimitiveEdgeType edgeType, |
| 673 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 674 | SkScalar radius, |
| 675 | const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 676 | : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 677 | this->initClassID<DashingCircleEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 678 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
| 679 | fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 680 | SkScalar onLen = info.fIntervals[0]; |
| 681 | SkScalar offLen = info.fIntervals[1]; |
| 682 | fIntervalLength = onLen + offLen; |
| 683 | fRadius = radius; |
| 684 | fCenterX = SkScalarHalf(offLen); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 685 | } |
| 686 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 687 | bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 688 | const DashingCircleEffect& dce = other.cast<DashingCircleEffect>(); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 689 | return (fEdgeType == dce.fEdgeType && |
| 690 | fIntervalLength == dce.fIntervalLength && |
| 691 | fRadius == dce.fRadius && |
| 692 | fCenterX == dce.fCenterX); |
| 693 | } |
| 694 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 695 | void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
| 696 | DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>(); |
| 697 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 698 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 699 | } |
| 700 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 701 | bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 702 | const GrGeometryProcessor& that, |
| 703 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 704 | const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>(); |
| 705 | const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 706 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 707 | that, theirs.fUsesLocalCoords) && |
| 708 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 709 | theirs.fInputColorType, theirs.fColor); |
| 710 | } |
| 711 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 712 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 713 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 714 | GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random, |
| 715 | GrContext*, |
| 716 | const GrDrawTargetCaps& caps, |
| 717 | GrTexture*[]) { |
| 718 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan( |
| 719 | kGrProcessorEdgeTypeCnt)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 720 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 721 | DashInfo info; |
| 722 | info.fCount = 2; |
| 723 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 724 | info.fIntervals = intervals.get(); |
| 725 | info.fIntervals[0] = 0; |
| 726 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 727 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]); |
| 728 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 729 | return DashingCircleEffect::Create(GrRandomColor(random), |
| 730 | edgeType, info, strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 731 | GrProcessorUnitTest::TestMatrix(random)); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | ////////////////////////////////////////////////////////////////////////////// |
| 735 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 736 | class GLDashingLineEffect; |
| 737 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 738 | struct DashingLineBatchTracker { |
| 739 | GrGPInput fInputColorType; |
| 740 | GrColor fColor; |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 741 | bool fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 742 | }; |
| 743 | |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 744 | /* |
| 745 | * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the |
| 746 | * length and spacing by the DashInfo. Both of the previous two parameters are in device space. |
| 747 | * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the |
| 748 | * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the |
| 749 | * vertex coords (in device space) if we transform the line to be horizontal, with the start of |
| 750 | * line at the origin then shifted to the right by half the off interval. The line then goes in the |
| 751 | * positive x direction. |
| 752 | */ |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 753 | class DashingLineEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 754 | public: |
| 755 | typedef SkPathEffect::DashInfo DashInfo; |
| 756 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 757 | static GrGeometryProcessor* Create(GrColor, |
| 758 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 759 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 760 | SkScalar strokeWidth, |
| 761 | const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 762 | |
| 763 | virtual ~DashingLineEffect(); |
| 764 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 765 | virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 766 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 767 | const GrAttribute* inPosition() const { return fInPosition; } |
| 768 | |
| 769 | const GrAttribute* inCoord() const { return fInCoord; } |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 770 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 771 | GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 772 | |
| 773 | const SkRect& getRect() const { return fRect; } |
| 774 | |
| 775 | SkScalar getIntervalLength() const { return fIntervalLength; } |
| 776 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 777 | virtual void getGLProcessorKey(const GrBatchTracker& bt, |
| 778 | const GrGLCaps& caps, |
| 779 | GrProcessorKeyBuilder* b) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 780 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 781 | virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 782 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 783 | void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE; |
| 784 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 785 | bool onCanMakeEqual(const GrBatchTracker&, |
| 786 | const GrGeometryProcessor&, |
| 787 | const GrBatchTracker&) const SK_OVERRIDE; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 788 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 789 | private: |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 790 | DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 791 | SkScalar strokeWidth, const SkMatrix& localMatrix); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 792 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 793 | virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 794 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 795 | virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE; |
egdaniel | 1a8ecdf | 2014-10-03 06:24:12 -0700 | [diff] [blame] | 796 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 797 | GrPrimitiveEdgeType fEdgeType; |
| 798 | const GrAttribute* fInPosition; |
| 799 | const GrAttribute* fInCoord; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 800 | SkRect fRect; |
| 801 | SkScalar fIntervalLength; |
| 802 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 803 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 804 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 805 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 806 | }; |
| 807 | |
| 808 | ////////////////////////////////////////////////////////////////////////////// |
| 809 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 810 | class GLDashingLineEffect : public GrGLGeometryProcessor { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 811 | public: |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 812 | GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 813 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 814 | virtual void emitCode(const EmitArgs&) SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 815 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 816 | static inline void GenKey(const GrGeometryProcessor&, |
| 817 | const GrBatchTracker&, |
| 818 | const GrGLCaps&, |
| 819 | GrProcessorKeyBuilder*); |
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 | virtual void setData(const GrGLProgramDataManager&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 822 | const GrPrimitiveProcessor&, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 823 | const GrBatchTracker&) SK_OVERRIDE; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 824 | |
| 825 | private: |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 826 | GrColor fColor; |
| 827 | UniformHandle fRectUniform; |
| 828 | UniformHandle fIntervalUniform; |
| 829 | UniformHandle fColorUniform; |
| 830 | SkRect fPrevRect; |
| 831 | SkScalar fPrevIntervalLength; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 832 | typedef GrGLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 833 | }; |
| 834 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 835 | GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&, |
| 836 | const GrBatchTracker&) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 837 | fColor = GrColor_ILLEGAL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 838 | fPrevRect.fLeft = SK_ScalarNaN; |
| 839 | fPrevIntervalLength = SK_ScalarMax; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 840 | } |
| 841 | |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 842 | void GLDashingLineEffect::emitCode(const EmitArgs& args) { |
| 843 | const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>(); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 844 | const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>(); |
| 845 | GrGLGPBuilder* pb = args.fPB; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 846 | const char *rectName; |
| 847 | // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5), |
| 848 | // respectively. |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 849 | fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 850 | kVec4f_GrSLType, kDefault_GrSLPrecision, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 851 | "rect", |
| 852 | &rectName); |
| 853 | const char *intervalName; |
| 854 | // 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] | 855 | fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility, |
bsalomon | 422f56f | 2014-12-09 10:18:12 -0800 | [diff] [blame] | 856 | kFloat_GrSLType, kDefault_GrSLPrecision, |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 857 | "interval", |
| 858 | &intervalName); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 859 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 860 | |
| 861 | GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder(); |
| 862 | |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 863 | GrGLVertToFrag v(kVec2f_GrSLType); |
| 864 | args.fPB->addVarying("Coord", &v); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 865 | vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName); |
| 866 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 867 | // Setup pass through color |
| 868 | this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform); |
| 869 | |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 870 | // setup coord outputs |
| 871 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName); |
| 872 | vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName); |
egdaniel | e61c411 | 2014-06-12 10:24:21 -0700 | [diff] [blame] | 873 | |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 874 | // setup position varying |
| 875 | vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(), |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 876 | de.inPosition()->fName); |
joshualitt | 4973d9d | 2014-11-08 09:24:25 -0800 | [diff] [blame] | 877 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 878 | // transforms all points so that we can compare them to our test rect |
joshualitt | c369e7c | 2014-10-22 10:56:26 -0700 | [diff] [blame] | 879 | GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder(); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 880 | fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n", |
joshualitt | 74077b9 | 2014-10-24 11:26:03 -0700 | [diff] [blame] | 881 | v.fsIn(), v.fsIn(), intervalName, intervalName); |
| 882 | fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn()); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 883 | if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 884 | // The amount of coverage removed in x and y by the edges is computed as a pair of negative |
| 885 | // numbers, xSub and ySub. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 886 | fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n"); |
| 887 | fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName); |
| 888 | fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName); |
| 889 | fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName); |
| 890 | 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] | 891 | // Now compute coverage in x and y and multiply them to get the fraction of the pixel |
| 892 | // covered. |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 893 | 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] | 894 | } else { |
| 895 | // Assuming the bounding geometry is tight so no need to check y values |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 896 | fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n"); |
| 897 | fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName); |
| 898 | 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] | 899 | } |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 900 | fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 901 | } |
| 902 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 903 | void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 904 | const GrPrimitiveProcessor& processor, |
| 905 | const GrBatchTracker& bt) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 906 | const DashingLineEffect& de = processor.cast<DashingLineEffect>(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 907 | const SkRect& rect = de.getRect(); |
| 908 | SkScalar intervalLength = de.getIntervalLength(); |
| 909 | if (rect != fPrevRect || intervalLength != fPrevIntervalLength) { |
kkinnunen | 7510b22 | 2014-07-30 00:04:16 -0700 | [diff] [blame] | 910 | pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f, |
| 911 | rect.fRight - 0.5f, rect.fBottom - 0.5f); |
| 912 | pdman.set1f(fIntervalUniform, intervalLength); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 913 | fPrevRect = rect; |
| 914 | fPrevIntervalLength = intervalLength; |
| 915 | } |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 916 | |
| 917 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
| 918 | if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) { |
| 919 | GrGLfloat c[4]; |
| 920 | GrColorToRGBAFloat(local.fColor, c); |
| 921 | pdman.set4fv(fColorUniform, 1, c); |
| 922 | fColor = local.fColor; |
| 923 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 924 | } |
| 925 | |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 926 | void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 927 | const GrBatchTracker& bt, |
joshualitt | 87f48d9 | 2014-12-04 10:41:40 -0800 | [diff] [blame] | 928 | const GrGLCaps&, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 929 | GrProcessorKeyBuilder* b) { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 930 | const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>(); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 931 | const DashingLineEffect& de = processor.cast<DashingLineEffect>(); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 932 | b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective()); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 933 | b->add32(de.getEdgeType() << 16 | local.fInputColorType); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | ////////////////////////////////////////////////////////////////////////////// |
skia.committer@gmail.com | 3b9e8be | 2014-05-20 03:05:34 +0000 | [diff] [blame] | 937 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 938 | GrGeometryProcessor* DashingLineEffect::Create(GrColor color, |
| 939 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 940 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 941 | SkScalar strokeWidth, |
| 942 | const SkMatrix& localMatrix) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 943 | if (info.fCount != 2) { |
| 944 | return NULL; |
| 945 | } |
| 946 | |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 947 | return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | DashingLineEffect::~DashingLineEffect() {} |
| 951 | |
joshualitt | 56995b5 | 2014-12-11 15:44:02 -0800 | [diff] [blame] | 952 | void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const { |
| 953 | out->setUnknownSingleComponent(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 954 | } |
| 955 | |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 956 | void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt, |
| 957 | const GrGLCaps& caps, |
| 958 | GrProcessorKeyBuilder* b) const { |
| 959 | GLDashingLineEffect::GenKey(*this, bt, caps, b); |
| 960 | } |
| 961 | |
| 962 | GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const { |
| 963 | return SkNEW_ARGS(GLDashingLineEffect, (*this, bt)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 964 | } |
| 965 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 966 | DashingLineEffect::DashingLineEffect(GrColor color, |
| 967 | GrPrimitiveEdgeType edgeType, |
| 968 | const DashInfo& info, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 969 | SkScalar strokeWidth, |
| 970 | const SkMatrix& localMatrix) |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 971 | : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) { |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 972 | this->initClassID<DashingLineEffect>(); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 973 | fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType)); |
| 974 | fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 975 | SkScalar onLen = info.fIntervals[0]; |
| 976 | SkScalar offLen = info.fIntervals[1]; |
| 977 | SkScalar halfOffLen = SkScalarHalf(offLen); |
| 978 | SkScalar halfStroke = SkScalarHalf(strokeWidth); |
| 979 | fIntervalLength = onLen + offLen; |
| 980 | fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 981 | } |
| 982 | |
bsalomon | 0e08fc1 | 2014-10-15 08:19:04 -0700 | [diff] [blame] | 983 | bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const { |
joshualitt | 49586be | 2014-09-16 08:21:41 -0700 | [diff] [blame] | 984 | const DashingLineEffect& de = other.cast<DashingLineEffect>(); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 985 | return (fEdgeType == de.fEdgeType && |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 986 | fRect == de.fRect && |
| 987 | fIntervalLength == de.fIntervalLength); |
| 988 | } |
| 989 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 990 | void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const { |
| 991 | DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>(); |
| 992 | local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 993 | local->fUsesLocalCoords = init.fUsesLocalCoords; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 994 | } |
| 995 | |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 996 | bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m, |
| 997 | const GrGeometryProcessor& that, |
| 998 | const GrBatchTracker& t) const { |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 999 | const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>(); |
| 1000 | const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>(); |
joshualitt | 290c09b | 2014-12-19 13:45:20 -0800 | [diff] [blame] | 1001 | return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords, |
| 1002 | that, theirs.fUsesLocalCoords) && |
| 1003 | CanCombineOutput(mine.fInputColorType, mine.fColor, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 1004 | theirs.fInputColorType, theirs.fColor); |
| 1005 | } |
| 1006 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1007 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1008 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1009 | GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random, |
| 1010 | GrContext*, |
| 1011 | const GrDrawTargetCaps& caps, |
| 1012 | GrTexture*[]) { |
| 1013 | GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan( |
| 1014 | kGrProcessorEdgeTypeCnt)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1015 | SkScalar strokeWidth = random->nextRangeScalar(0, 100.f); |
| 1016 | DashInfo info; |
| 1017 | info.fCount = 2; |
| 1018 | SkAutoTArray<SkScalar> intervals(info.fCount); |
| 1019 | info.fIntervals = intervals.get(); |
| 1020 | info.fIntervals[0] = random->nextRangeScalar(0, 10.f); |
| 1021 | info.fIntervals[1] = random->nextRangeScalar(0, 10.f); |
| 1022 | info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]); |
| 1023 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 1024 | return DashingLineEffect::Create(GrRandomColor(random), |
| 1025 | edgeType, info, strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1026 | GrProcessorUnitTest::TestMatrix(random)); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1027 | } |
| 1028 | |
| 1029 | ////////////////////////////////////////////////////////////////////////////// |
| 1030 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 1031 | GrGeometryProcessor* GrDashingEffect::Create(GrColor color, |
| 1032 | GrPrimitiveEdgeType edgeType, |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 1033 | const SkPathEffect::DashInfo& info, |
| 1034 | SkScalar strokeWidth, |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1035 | GrDashingEffect::DashCap cap, |
| 1036 | const SkMatrix& localMatrix) { |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1037 | switch (cap) { |
| 1038 | case GrDashingEffect::kRound_DashCap: |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame^] | 1039 | return DashingCircleEffect::Create(color, edgeType, info, |
| 1040 | SkScalarHalf(strokeWidth), |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1041 | localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1042 | case GrDashingEffect::kNonRound_DashCap: |
joshualitt | d27f73e | 2014-12-29 07:43:36 -0800 | [diff] [blame] | 1043 | return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix); |
egdaniel | f767e79 | 2014-07-02 06:21:32 -0700 | [diff] [blame] | 1044 | default: |
| 1045 | SkFAIL("Unexpected dashed cap."); |
| 1046 | } |
| 1047 | return NULL; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 1048 | } |