blob: d5b0b48e9b4e843f4fcd8dcfbda45a0609da1207 [file] [log] [blame]
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001/*
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
egdaniele61c4112014-06-12 10:24:21 -070010#include "../GrAARectRenderer.h"
11
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000013#include "GrContext.h"
14#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080015#include "GrDefaultGeoProcFactory.h"
egdaniele61c4112014-06-12 10:24:21 -070016#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000017#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080018#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070019#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070020#include "GrStrokeInfo.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000021#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080022#include "gl/GrGLGeometryProcessor.h"
23#include "gl/GrGLProcessor.h"
24#include "gl/GrGLSL.h"
25#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000026
27///////////////////////////////////////////////////////////////////////////////
28
egdaniele61c4112014-06-12 10:24:21 -070029// Returns whether or not the gpu can fast path the dash line effect.
30static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
joshualitt9853cce2014-11-17 14:22:48 -080031 const GrDrawTarget& target, const GrDrawState& ds,
32 const SkMatrix& viewMatrix) {
33 if (ds.getRenderTarget()->isMultisampled()) {
egdaniele61c4112014-06-12 10:24:21 -070034 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
egdanielf767e792014-07-02 06:21:32 -070059 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070060 return false;
61 }
62
63 return true;
64}
65
66namespace {
egdaniele61c4112014-06-12 10:24:21 -070067struct DashLineVertex {
68 SkPoint fPos;
69 SkPoint fDashPos;
70};
egdaniele61c4112014-06-12 10:24:21 -070071};
72
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000073static 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
93static 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
108static 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
egdaniele61c4112014-06-12 10:24:21 -0700117static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
118 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000119 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);
egdaniele61c4112014-06-12 10:24:21 -0700126 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000127 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.orgad883402014-05-19 14:43:45 +0000134 *endingInt -= 0.01f; // make sure we capture the last zero size pnt (used if has caps)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000135 }
136 return *endingInt - info.fIntervals[0];
137 }
138 return 0;
139}
140
egdaniele61c4112014-06-12 10:24:21 -0700141static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
142 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
egdaniele61c4112014-06-12 10:24:21 -0700143 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);
egdaniele61c4112014-06-12 10:24:21 -0700151 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);
egdaniele61c4112014-06-12 10:24:21 -0700155 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
156}
157
joshualitt5478d422014-11-14 16:00:38 -0800158static 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}
egdaniele61c4112014-06-12 10:24:21 -0700166
joshualitt9853cce2014-11-17 14:22:48 -0800167bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target, GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800168 GrColor color, const SkPoint pts[2], const GrPaint& paint,
joshualitt9853cce2014-11-17 14:22:48 -0800169 const GrStrokeInfo& strokeInfo, const SkMatrix& vm) {
egdaniele61c4112014-06-12 10:24:21 -0700170
joshualitt9853cce2014-11-17 14:22:48 -0800171 if (!can_fast_path_dash(pts, strokeInfo, *target, *drawState, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000172 return false;
173 }
174
egdaniele61c4112014-06-12 10:24:21 -0700175 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000176
egdaniele61c4112014-06-12 10:24:21 -0700177 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000178
egdaniele61c4112014-06-12 10:24:21 -0700179 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000180
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
egdaniele61c4112014-06-12 10:24:21 -0700184 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000185
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) {
egdaniele61c4112014-06-12 10:24:21 -0700190 SkMatrix rotMatrix;
191 align_to_x_axis(pts, &rotMatrix, ptsRot);
192 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700193 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000194 return false;
195 }
196 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000197 srcRotInv.reset();
198 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
199 }
200
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000201 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000202
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000203 // Scale corrections of intervals and stroke from view matrix
204 SkScalar parallelScale;
205 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700206 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207
egdanielf767e792014-07-02 06:21:32 -0700208 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000209
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
egdaniele61c4112014-06-12 10:24:21 -0700212 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
egdaniele61c4112014-06-12 10:24:21 -0700214 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000215 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700216 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000217 } else {
egdaniele61c4112014-06-12 10:24:21 -0700218 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219 }
220
egdaniele61c4112014-06-12 10:24:21 -0700221 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.org628ed0b2014-05-19 14:32:49 +0000229 // 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) {
egdaniele61c4112014-06-12 10:24:21 -0700232 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233 SkPoint startPts[2];
234 startPts[0] = ptsRot[0];
235 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700236 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700239 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000240
egdaniele61c4112014-06-12 10:24:21 -0700241 hasStartRect = true;
242 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 }
244 }
245
246 // adjustments for start and end of bounding rect so we only draw dash intervals
247 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700248 startAdj += calc_start_adjustment(info);
249 if (startAdj != 0) {
250 ptsRot[0].fX += startAdj;
251 srcPhase = 0;
252 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000253 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700254 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.org628ed0b2014-05-19 14:32:49 +0000258 }
259
egdaniele61c4112014-06-12 10:24:21 -0700260 SkRect endRect;
261 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000262 // 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
egdaniele61c4112014-06-12 10:24:21 -0700264 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000265 // 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.org628ed0b2014-05-19 14:32:49 +0000269 SkPoint endPts[2];
270 endPts[1] = ptsRot[1];
271 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000272 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700275 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000276
egdaniele61c4112014-06-12 10:24:21 -0700277 hasEndRect = true;
278 endAdj = endingInterval + info.fIntervals[1];
279
280 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000281 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700282 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000283 }
284 }
285 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000286
egdaniele61c4112014-06-12 10:24:21 -0700287 if (startAdj != 0) {
288 srcPhase = 0;
289 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000290
egdaniele61c4112014-06-12 10:24:21 -0700291 // 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.org628ed0b2014-05-19 14:32:49 +0000296 SkScalar strokeWidth = srcStrokeWidth * perpScale;
297
298 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
299 strokeWidth = 1.f;
300 }
301
egdaniele61c4112014-06-12 10:24:21 -0700302 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000303
304 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
305 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700306 devIntervals[0] += strokeWidth;
307 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308 }
egdaniele61c4112014-06-12 10:24:21 -0700309 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310
egdaniele61c4112014-06-12 10:24:21 -0700311 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
egdaniele61c4112014-06-12 10:24:21 -0700316 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 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800337
joshualitt56995b52014-12-11 15:44:02 -0800338 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt5478d422014-11-14 16:00:38 -0800339 bool fullDash = devIntervals[1] > 0.f || useAA;
340 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700341 SkPathEffect::DashInfo devInfo;
342 devInfo.fPhase = devPhase;
343 devInfo.fCount = 2;
344 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700345 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
346 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700347 bool isRoundCap = SkPaint::kRound_Cap == cap;
348 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
349 GrDashingEffect::kNonRound_DashCap;
joshualitt56995b52014-12-11 15:44:02 -0800350 gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType));
joshualitt249af152014-09-15 11:41:13 -0700351 } else {
352 // Set up the vertex data for the line and start/end dashes
joshualitt56995b52014-12-11 15:44:02 -0800353 gp.reset(GrDefaultGeoProcFactory::Create(color, GrDefaultGeoProcFactory::kPosition_GPType));
joshualitt249af152014-09-15 11:41:13 -0700354 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355
egdaniele61c4112014-06-12 10:24:21 -0700356 int totalRectCnt = 0;
357
358 totalRectCnt += !lineDone ? 1 : 0;
359 totalRectCnt += hasStartRect ? 1 : 0;
360 totalRectCnt += hasEndRect ? 1 : 0;
361
joshualitt9853cce2014-11-17 14:22:48 -0800362 GrDrawTarget::AutoReleaseGeometry geo(target,
363 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800364 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700365 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700366 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700367 return false;
368 }
369
egdaniele61c4112014-06-12 10:24:21 -0700370 int curVIdx = 0;
371
egdanielf767e792014-07-02 06:21:32 -0700372 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
egdaniele61c4112014-06-12 10:24:21 -0700377 // 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);
joshualitt5478d422014-11-14 16:00:38 -0800389 if (fullDash) {
390 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800391 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800392 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
393 lineLength, halfDevStroke);
394 } else {
395 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800396 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800397 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
398 }
egdaniele61c4112014-06-12 10:24:21 -0700399 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);
joshualitt5478d422014-11-14 16:00:38 -0800405 if (fullDash) {
406 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800407 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800408 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
409 devIntervals[0], halfDevStroke);
410 } else {
411 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800412 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800413 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
414 }
415
egdaniele61c4112014-06-12 10:24:21 -0700416 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);
joshualitt5478d422014-11-14 16:00:38 -0800422 if (fullDash) {
423 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800424 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800425 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
426 devIntervals[0], halfDevStroke);
427 } else {
428 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800429 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800430 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
431 }
432
egdaniele61c4112014-06-12 10:24:21 -0700433 }
434
435 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
joshualitt56995b52014-12-11 15:44:02 -0800436 target->drawIndexedInstances(drawState, gp, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700437 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000438 return true;
439}
440
441//////////////////////////////////////////////////////////////////////////////
442
egdanielf767e792014-07-02 06:21:32 -0700443class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800444
445struct DashingCircleBatchTracker {
446 GrGPInput fInputColorType;
447 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800448 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800449};
450
egdanielf767e792014-07-02 06:21:32 -0700451/*
452 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
453 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
454 * Both of the previous two parameters are in device space. This effect also requires the setting of
455 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
456 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
457 * transform the line to be horizontal, with the start of line at the origin then shifted to the
458 * right by half the off interval. The line then goes in the positive x direction.
459 */
joshualitt249af152014-09-15 11:41:13 -0700460class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700461public:
462 typedef SkPathEffect::DashInfo DashInfo;
463
joshualitt2e3b3e32014-12-09 13:31:14 -0800464 static GrGeometryProcessor* Create(GrColor,
465 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700466 const DashInfo& info,
467 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700468
469 virtual ~DashingCircleEffect();
470
joshualitteb2a6762014-12-04 11:35:33 -0800471 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700472
joshualitt2dd1ae02014-12-03 06:24:10 -0800473 const GrAttribute* inPosition() const { return fInPosition; }
474
475 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700476
joshualittb0a8a372014-09-23 09:50:21 -0700477 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700478
479 SkScalar getRadius() const { return fRadius; }
480
481 SkScalar getCenterX() const { return fCenterX; }
482
483 SkScalar getIntervalLength() const { return fIntervalLength; }
484
joshualitteb2a6762014-12-04 11:35:33 -0800485 virtual void getGLProcessorKey(const GrBatchTracker&,
486 const GrGLCaps&,
487 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700488
joshualitteb2a6762014-12-04 11:35:33 -0800489 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700490
joshualitt9b989322014-12-15 14:16:27 -0800491 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
492
joshualitt290c09b2014-12-19 13:45:20 -0800493 bool onCanMakeEqual(const GrBatchTracker&,
494 const GrGeometryProcessor&,
495 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800496
egdanielf767e792014-07-02 06:21:32 -0700497private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800498 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
499 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700500
bsalomon0e08fc12014-10-15 08:19:04 -0700501 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700502
joshualitt56995b52014-12-11 15:44:02 -0800503 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700504
joshualitt2dd1ae02014-12-03 06:24:10 -0800505 GrPrimitiveEdgeType fEdgeType;
506 const GrAttribute* fInPosition;
507 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700508 SkScalar fIntervalLength;
509 SkScalar fRadius;
510 SkScalar fCenterX;
511
joshualittb0a8a372014-09-23 09:50:21 -0700512 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700513
joshualitt249af152014-09-15 11:41:13 -0700514 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700515};
516
517//////////////////////////////////////////////////////////////////////////////
518
joshualitt249af152014-09-15 11:41:13 -0700519class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700520public:
joshualitteb2a6762014-12-04 11:35:33 -0800521 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700522
joshualittc369e7c2014-10-22 10:56:26 -0700523 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700524
joshualitt87f48d92014-12-04 10:41:40 -0800525 static inline void GenKey(const GrGeometryProcessor&,
526 const GrBatchTracker&,
527 const GrGLCaps&,
528 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700529
joshualitt87f48d92014-12-04 10:41:40 -0800530 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800531 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800532 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700533
534private:
joshualitt9b989322014-12-15 14:16:27 -0800535 UniformHandle fParamUniform;
536 UniformHandle fColorUniform;
537 GrColor fColor;
538 SkScalar fPrevRadius;
539 SkScalar fPrevCenterX;
540 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700541 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700542};
543
joshualitteb2a6762014-12-04 11:35:33 -0800544GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
545 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800546 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700547 fPrevRadius = SK_ScalarMin;
548 fPrevCenterX = SK_ScalarMin;
549 fPrevIntervalLength = SK_ScalarMax;
550}
551
joshualittc369e7c2014-10-22 10:56:26 -0700552void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
553 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800554 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
555 GrGLGPBuilder* pb = args.fPB;
egdanielf767e792014-07-02 06:21:32 -0700556 const char *paramName;
557 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
558 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700559 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800560 kVec3f_GrSLType, kDefault_GrSLPrecision,
561 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700562
joshualitt2dd1ae02014-12-03 06:24:10 -0800563 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
564
joshualitt74077b92014-10-24 11:26:03 -0700565 GrGLVertToFrag v(kVec2f_GrSLType);
566 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800567 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700568
joshualitt9b989322014-12-15 14:16:27 -0800569 // Setup pass through color
570 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
571
joshualitt2dd1ae02014-12-03 06:24:10 -0800572 // setup coord outputs
573 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
574 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700575
joshualitt4973d9d2014-11-08 09:24:25 -0800576 // setup position varying
577 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800578 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800579
egdanielf767e792014-07-02 06:21:32 -0700580 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700581 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700582 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700583 v.fsIn(), v.fsIn(), paramName, paramName);
584 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700585 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
586 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700587 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700588 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
589 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
590 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700591 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700592 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
593 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700594 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800595 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700596}
597
joshualitt87f48d92014-12-04 10:41:40 -0800598void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800599 const GrPrimitiveProcessor& processor,
600 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700601 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700602 SkScalar radius = dce.getRadius();
603 SkScalar centerX = dce.getCenterX();
604 SkScalar intervalLength = dce.getIntervalLength();
605 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700606 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700607 fPrevRadius = radius;
608 fPrevCenterX = centerX;
609 fPrevIntervalLength = intervalLength;
610 }
joshualitt9b989322014-12-15 14:16:27 -0800611
612 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
613 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
614 GrGLfloat c[4];
615 GrColorToRGBAFloat(local.fColor, c);
616 pdman.set4fv(fColorUniform, 1, c);
617 fColor = local.fColor;
618 }
egdanielf767e792014-07-02 06:21:32 -0700619}
620
joshualitt87f48d92014-12-04 10:41:40 -0800621void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800622 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800623 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700624 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800625 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700626 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800627 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800628 b->add32(dce.getEdgeType() << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700629}
630
631//////////////////////////////////////////////////////////////////////////////
632
joshualitt2e3b3e32014-12-09 13:31:14 -0800633GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
634 GrPrimitiveEdgeType edgeType,
635 const DashInfo& info,
joshualittb0a8a372014-09-23 09:50:21 -0700636 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700637 if (info.fCount != 2 || info.fIntervals[0] != 0) {
638 return NULL;
639 }
640
joshualitt2e3b3e32014-12-09 13:31:14 -0800641 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700642}
643
644DashingCircleEffect::~DashingCircleEffect() {}
645
joshualitt56995b52014-12-11 15:44:02 -0800646void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
647 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700648}
649
joshualitteb2a6762014-12-04 11:35:33 -0800650void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
651 const GrGLCaps& caps,
652 GrProcessorKeyBuilder* b) const {
653 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
654}
655
656GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
657 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700658}
659
joshualitt2e3b3e32014-12-09 13:31:14 -0800660DashingCircleEffect::DashingCircleEffect(GrColor color,
661 GrPrimitiveEdgeType edgeType,
662 const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700663 SkScalar radius)
joshualitt2e3b3e32014-12-09 13:31:14 -0800664 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800665 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800666 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
667 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700668 SkScalar onLen = info.fIntervals[0];
669 SkScalar offLen = info.fIntervals[1];
670 fIntervalLength = onLen + offLen;
671 fRadius = radius;
672 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700673}
674
bsalomon0e08fc12014-10-15 08:19:04 -0700675bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700676 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700677 return (fEdgeType == dce.fEdgeType &&
678 fIntervalLength == dce.fIntervalLength &&
679 fRadius == dce.fRadius &&
680 fCenterX == dce.fCenterX);
681}
682
joshualitt9b989322014-12-15 14:16:27 -0800683void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
684 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
685 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800686 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800687}
688
joshualitt290c09b2014-12-19 13:45:20 -0800689bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
690 const GrGeometryProcessor& that,
691 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800692 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
693 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800694 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
695 that, theirs.fUsesLocalCoords) &&
696 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800697 theirs.fInputColorType, theirs.fColor);
698}
699
joshualittb0a8a372014-09-23 09:50:21 -0700700GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700701
joshualittb0a8a372014-09-23 09:50:21 -0700702GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
703 GrContext*,
704 const GrDrawTargetCaps& caps,
705 GrTexture*[]) {
706 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
707 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700708 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
709 DashInfo info;
710 info.fCount = 2;
711 SkAutoTArray<SkScalar> intervals(info.fCount);
712 info.fIntervals = intervals.get();
713 info.fIntervals[0] = 0;
714 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
715 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
716
joshualitt2e3b3e32014-12-09 13:31:14 -0800717 return DashingCircleEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700718}
719
720//////////////////////////////////////////////////////////////////////////////
721
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000722class GLDashingLineEffect;
723
joshualitt9b989322014-12-15 14:16:27 -0800724struct DashingLineBatchTracker {
725 GrGPInput fInputColorType;
726 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800727 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800728};
729
egdanielf767e792014-07-02 06:21:32 -0700730/*
731 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
732 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
733 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
734 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
735 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
736 * line at the origin then shifted to the right by half the off interval. The line then goes in the
737 * positive x direction.
738 */
joshualitt249af152014-09-15 11:41:13 -0700739class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000740public:
741 typedef SkPathEffect::DashInfo DashInfo;
742
joshualitt2e3b3e32014-12-09 13:31:14 -0800743 static GrGeometryProcessor* Create(GrColor,
744 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700745 const DashInfo& info,
746 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000747
748 virtual ~DashingLineEffect();
749
joshualitteb2a6762014-12-04 11:35:33 -0800750 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000751
joshualitt2dd1ae02014-12-03 06:24:10 -0800752 const GrAttribute* inPosition() const { return fInPosition; }
753
754 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700755
joshualittb0a8a372014-09-23 09:50:21 -0700756 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000757
758 const SkRect& getRect() const { return fRect; }
759
760 SkScalar getIntervalLength() const { return fIntervalLength; }
761
joshualitteb2a6762014-12-04 11:35:33 -0800762 virtual void getGLProcessorKey(const GrBatchTracker& bt,
763 const GrGLCaps& caps,
764 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000765
joshualitteb2a6762014-12-04 11:35:33 -0800766 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000767
joshualitt9b989322014-12-15 14:16:27 -0800768 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
769
joshualitt290c09b2014-12-19 13:45:20 -0800770 bool onCanMakeEqual(const GrBatchTracker&,
771 const GrGeometryProcessor&,
772 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800773
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000774private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800775 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
776 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000777
bsalomon0e08fc12014-10-15 08:19:04 -0700778 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000779
joshualitt56995b52014-12-11 15:44:02 -0800780 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700781
joshualitt2dd1ae02014-12-03 06:24:10 -0800782 GrPrimitiveEdgeType fEdgeType;
783 const GrAttribute* fInPosition;
784 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000785 SkRect fRect;
786 SkScalar fIntervalLength;
787
joshualittb0a8a372014-09-23 09:50:21 -0700788 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000789
joshualitt249af152014-09-15 11:41:13 -0700790 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000791};
792
793//////////////////////////////////////////////////////////////////////////////
794
joshualitt249af152014-09-15 11:41:13 -0700795class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000796public:
joshualitteb2a6762014-12-04 11:35:33 -0800797 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000798
joshualittc369e7c2014-10-22 10:56:26 -0700799 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000800
joshualitt87f48d92014-12-04 10:41:40 -0800801 static inline void GenKey(const GrGeometryProcessor&,
802 const GrBatchTracker&,
803 const GrGLCaps&,
804 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000805
joshualitt87f48d92014-12-04 10:41:40 -0800806 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800807 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800808 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000809
810private:
joshualitt9b989322014-12-15 14:16:27 -0800811 GrColor fColor;
812 UniformHandle fRectUniform;
813 UniformHandle fIntervalUniform;
814 UniformHandle fColorUniform;
815 SkRect fPrevRect;
816 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700817 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000818};
819
joshualitteb2a6762014-12-04 11:35:33 -0800820GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
821 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800822 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000823 fPrevRect.fLeft = SK_ScalarNaN;
824 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000825}
826
joshualittc369e7c2014-10-22 10:56:26 -0700827void GLDashingLineEffect::emitCode(const EmitArgs& args) {
828 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800829 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
830 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000831 const char *rectName;
832 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
833 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700834 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800835 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000836 "rect",
837 &rectName);
838 const char *intervalName;
839 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700840 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800841 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700842 "interval",
843 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700844
joshualitt2dd1ae02014-12-03 06:24:10 -0800845
846 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
847
joshualitt74077b92014-10-24 11:26:03 -0700848 GrGLVertToFrag v(kVec2f_GrSLType);
849 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800850 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
851
joshualitt9b989322014-12-15 14:16:27 -0800852 // Setup pass through color
853 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
854
joshualitt2dd1ae02014-12-03 06:24:10 -0800855 // setup coord outputs
856 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
857 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700858
joshualitt4973d9d2014-11-08 09:24:25 -0800859 // setup position varying
860 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800861 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800862
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000863 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700864 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700865 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700866 v.fsIn(), v.fsIn(), intervalName, intervalName);
867 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700868 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000869 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
870 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700871 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
872 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
873 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
874 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
875 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000876 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
877 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700878 fsBuilder->codeAppendf("\t\tfloat alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000879 } else {
880 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700881 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
882 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
883 fsBuilder->codeAppendf("\t\talpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000884 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800885 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000886}
887
joshualittb0a8a372014-09-23 09:50:21 -0700888void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800889 const GrPrimitiveProcessor& processor,
890 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700891 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000892 const SkRect& rect = de.getRect();
893 SkScalar intervalLength = de.getIntervalLength();
894 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700895 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
896 rect.fRight - 0.5f, rect.fBottom - 0.5f);
897 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000898 fPrevRect = rect;
899 fPrevIntervalLength = intervalLength;
900 }
joshualitt9b989322014-12-15 14:16:27 -0800901
902 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
903 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
904 GrGLfloat c[4];
905 GrColorToRGBAFloat(local.fColor, c);
906 pdman.set4fv(fColorUniform, 1, c);
907 fColor = local.fColor;
908 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000909}
910
joshualitt87f48d92014-12-04 10:41:40 -0800911void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800912 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800913 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700914 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800915 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700916 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800917 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800918 b->add32(de.getEdgeType() << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000919}
920
921//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000922
joshualitt2e3b3e32014-12-09 13:31:14 -0800923GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
924 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700925 const DashInfo& info,
926 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000927 if (info.fCount != 2) {
928 return NULL;
929 }
930
joshualitt2e3b3e32014-12-09 13:31:14 -0800931 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000932}
933
934DashingLineEffect::~DashingLineEffect() {}
935
joshualitt56995b52014-12-11 15:44:02 -0800936void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
937 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000938}
939
joshualitteb2a6762014-12-04 11:35:33 -0800940void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
941 const GrGLCaps& caps,
942 GrProcessorKeyBuilder* b) const {
943 GLDashingLineEffect::GenKey(*this, bt, caps, b);
944}
945
946GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
947 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000948}
949
joshualitt2e3b3e32014-12-09 13:31:14 -0800950DashingLineEffect::DashingLineEffect(GrColor color,
951 GrPrimitiveEdgeType edgeType,
952 const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700953 SkScalar strokeWidth)
joshualitt2e3b3e32014-12-09 13:31:14 -0800954 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800955 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800956 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
957 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000958 SkScalar onLen = info.fIntervals[0];
959 SkScalar offLen = info.fIntervals[1];
960 SkScalar halfOffLen = SkScalarHalf(offLen);
961 SkScalar halfStroke = SkScalarHalf(strokeWidth);
962 fIntervalLength = onLen + offLen;
963 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000964}
965
bsalomon0e08fc12014-10-15 08:19:04 -0700966bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700967 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000968 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000969 fRect == de.fRect &&
970 fIntervalLength == de.fIntervalLength);
971}
972
joshualitt9b989322014-12-15 14:16:27 -0800973void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
974 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
975 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800976 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800977}
978
joshualitt290c09b2014-12-19 13:45:20 -0800979bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
980 const GrGeometryProcessor& that,
981 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800982 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
983 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800984 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
985 that, theirs.fUsesLocalCoords) &&
986 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800987 theirs.fInputColorType, theirs.fColor);
988}
989
joshualittb0a8a372014-09-23 09:50:21 -0700990GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000991
joshualittb0a8a372014-09-23 09:50:21 -0700992GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
993 GrContext*,
994 const GrDrawTargetCaps& caps,
995 GrTexture*[]) {
996 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
997 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000998 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
999 DashInfo info;
1000 info.fCount = 2;
1001 SkAutoTArray<SkScalar> intervals(info.fCount);
1002 info.fIntervals = intervals.get();
1003 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
1004 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
1005 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
1006
joshualitt2e3b3e32014-12-09 13:31:14 -08001007 return DashingLineEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001008}
1009
1010//////////////////////////////////////////////////////////////////////////////
1011
joshualitt2e3b3e32014-12-09 13:31:14 -08001012GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
1013 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -07001014 const SkPathEffect::DashInfo& info,
1015 SkScalar strokeWidth,
1016 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -07001017 switch (cap) {
1018 case GrDashingEffect::kRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -08001019 return DashingCircleEffect::Create(color, edgeType, info, SkScalarHalf(strokeWidth));
egdanielf767e792014-07-02 06:21:32 -07001020 case GrDashingEffect::kNonRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -08001021 return DashingLineEffect::Create(color, edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -07001022 default:
1023 SkFAIL("Unexpected dashed cap.");
1024 }
1025 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001026}