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