blob: 339e44a83c352a6a63668c26ac5b0089276b27ee [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,
joshualittd27f73e2014-12-29 07:43:36 -0800169 const GrStrokeInfo& strokeInfo) {
170 const SkMatrix& vm = drawState->getViewMatrix();
egdaniele61c4112014-06-12 10:24:21 -0700171
joshualitt9853cce2014-11-17 14:22:48 -0800172 if (!can_fast_path_dash(pts, strokeInfo, *target, *drawState, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000173 return false;
174 }
175
egdaniele61c4112014-06-12 10:24:21 -0700176 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000177
egdaniele61c4112014-06-12 10:24:21 -0700178 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000179
egdaniele61c4112014-06-12 10:24:21 -0700180 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000181
182 // the phase should be normalized to be [0, sum of all intervals)
183 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
184
egdaniele61c4112014-06-12 10:24:21 -0700185 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000186
187 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
188 SkMatrix srcRotInv;
189 SkPoint ptsRot[2];
190 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700191 SkMatrix rotMatrix;
192 align_to_x_axis(pts, &rotMatrix, ptsRot);
193 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700194 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000195 return false;
196 }
197 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000198 srcRotInv.reset();
199 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
200 }
201
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000202 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000203
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000204 // Scale corrections of intervals and stroke from view matrix
205 SkScalar parallelScale;
206 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700207 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000208
egdanielf767e792014-07-02 06:21:32 -0700209 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000210
211 // We always want to at least stroke out half a pixel on each side in device space
212 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700213 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000214
egdaniele61c4112014-06-12 10:24:21 -0700215 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000216 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700217 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000218 } else {
egdaniele61c4112014-06-12 10:24:21 -0700219 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000220 }
221
egdaniele61c4112014-06-12 10:24:21 -0700222 SkScalar startAdj = 0;
223
224 SkMatrix combinedMatrix = srcRotInv;
225 combinedMatrix.postConcat(vm);
226
227 bool lineDone = false;
228 SkRect startRect;
229 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000230 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
231 // draw it separately here and adjust our start point accordingly
232 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700233 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000234 SkPoint startPts[2];
235 startPts[0] = ptsRot[0];
236 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700237 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000239 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700240 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000241
egdaniele61c4112014-06-12 10:24:21 -0700242 hasStartRect = true;
243 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000244 }
245 }
246
247 // adjustments for start and end of bounding rect so we only draw dash intervals
248 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700249 startAdj += calc_start_adjustment(info);
250 if (startAdj != 0) {
251 ptsRot[0].fX += startAdj;
252 srcPhase = 0;
253 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000254 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700255 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
256 ptsRot[1].fX -= endAdj;
257 if (ptsRot[0].fX >= ptsRot[1].fX) {
258 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000259 }
260
egdaniele61c4112014-06-12 10:24:21 -0700261 SkRect endRect;
262 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000263 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
264 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700265 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000266 // If we adjusted the end then we will not be drawing a partial dash at the end.
267 // If we didn't adjust the end point then we just need to make sure the ending
268 // dash isn't a full dash
269 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000270 SkPoint endPts[2];
271 endPts[1] = ptsRot[1];
272 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000273 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000275 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700276 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000277
egdaniele61c4112014-06-12 10:24:21 -0700278 hasEndRect = true;
279 endAdj = endingInterval + info.fIntervals[1];
280
281 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000282 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700283 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000284 }
285 }
286 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000287
egdaniele61c4112014-06-12 10:24:21 -0700288 if (startAdj != 0) {
289 srcPhase = 0;
290 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000291
egdaniele61c4112014-06-12 10:24:21 -0700292 // Change the dashing info from src space into device space
293 SkScalar devIntervals[2];
294 devIntervals[0] = info.fIntervals[0] * parallelScale;
295 devIntervals[1] = info.fIntervals[1] * parallelScale;
296 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000297 SkScalar strokeWidth = srcStrokeWidth * perpScale;
298
299 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
300 strokeWidth = 1.f;
301 }
302
egdaniele61c4112014-06-12 10:24:21 -0700303 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000304
305 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
306 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700307 devIntervals[0] += strokeWidth;
308 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000309 }
egdaniele61c4112014-06-12 10:24:21 -0700310 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000311
egdaniele61c4112014-06-12 10:24:21 -0700312 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
313 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
314
315 SkScalar devBloat = useAA ? 0.5f : 0.f;
316
egdaniele61c4112014-06-12 10:24:21 -0700317 if (devIntervals[1] <= 0.f && useAA) {
318 // Case when we end up drawing a solid AA rect
319 // Reset the start rect to draw this single solid rect
320 // but it requires to upload a new intervals uniform so we can mimic
321 // one giant dash
322 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
323 ptsRot[1].fX += hasEndRect ? endAdj : 0;
324 startRect.set(ptsRot, 2);
325 startRect.outset(strokeAdj, halfSrcStroke);
326 hasStartRect = true;
327 hasEndRect = false;
328 lineDone = true;
329
330 SkPoint devicePts[2];
331 vm.mapPoints(devicePts, ptsRot, 2);
332 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
333 if (hasCap) {
334 lineLength += 2.f * halfDevStroke;
335 }
336 devIntervals[0] = lineLength;
337 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800338
joshualittd27f73e2014-12-29 07:43:36 -0800339 // reset to device coordinates
340 SkMatrix invert;
341 if (!vm.invert(&invert)) {
342 SkDebugf("Failed to invert\n");
343 return false;
344 }
345
346 GrDrawState::AutoViewMatrixRestore avmr(drawState);
347
joshualitt56995b52014-12-11 15:44:02 -0800348 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt5478d422014-11-14 16:00:38 -0800349 bool fullDash = devIntervals[1] > 0.f || useAA;
350 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700351 SkPathEffect::DashInfo devInfo;
352 devInfo.fPhase = devPhase;
353 devInfo.fCount = 2;
354 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700355 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
356 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700357 bool isRoundCap = SkPaint::kRound_Cap == cap;
358 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
359 GrDashingEffect::kNonRound_DashCap;
joshualittd27f73e2014-12-29 07:43:36 -0800360 gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType, invert));
joshualitt249af152014-09-15 11:41:13 -0700361 } else {
362 // Set up the vertex data for the line and start/end dashes
joshualittd27f73e2014-12-29 07:43:36 -0800363 gp.reset(GrDefaultGeoProcFactory::Create(color, GrDefaultGeoProcFactory::kPosition_GPType,
364 invert));
joshualitt249af152014-09-15 11:41:13 -0700365 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000366
egdaniele61c4112014-06-12 10:24:21 -0700367 int totalRectCnt = 0;
368
369 totalRectCnt += !lineDone ? 1 : 0;
370 totalRectCnt += hasStartRect ? 1 : 0;
371 totalRectCnt += hasEndRect ? 1 : 0;
372
joshualitt9853cce2014-11-17 14:22:48 -0800373 GrDrawTarget::AutoReleaseGeometry geo(target,
374 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800375 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700376 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700377 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700378 return false;
379 }
380
egdaniele61c4112014-06-12 10:24:21 -0700381 int curVIdx = 0;
382
egdanielf767e792014-07-02 06:21:32 -0700383 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
384 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
385 startOffset -= halfDevStroke;
386 }
387
egdaniele61c4112014-06-12 10:24:21 -0700388 // Draw interior part of dashed line
389 if (!lineDone) {
390 SkPoint devicePts[2];
391 vm.mapPoints(devicePts, ptsRot, 2);
392 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
393 if (hasCap) {
394 lineLength += 2.f * halfDevStroke;
395 }
396
397 SkRect bounds;
398 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
399 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800400 if (fullDash) {
401 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800402 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800403 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
404 lineLength, halfDevStroke);
405 } else {
406 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800407 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800408 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
409 }
egdaniele61c4112014-06-12 10:24:21 -0700410 curVIdx += 4;
411 }
412
413 if (hasStartRect) {
414 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
415 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800416 if (fullDash) {
417 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800418 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800419 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
420 devIntervals[0], halfDevStroke);
421 } else {
422 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800423 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800424 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
425 }
426
egdaniele61c4112014-06-12 10:24:21 -0700427 curVIdx += 4;
428 }
429
430 if (hasEndRect) {
431 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
432 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800433 if (fullDash) {
434 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800435 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800436 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
437 devIntervals[0], halfDevStroke);
438 } else {
439 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800440 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800441 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
442 }
443
egdaniele61c4112014-06-12 10:24:21 -0700444 }
445
446 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
joshualitt56995b52014-12-11 15:44:02 -0800447 target->drawIndexedInstances(drawState, gp, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700448 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000449 return true;
450}
451
452//////////////////////////////////////////////////////////////////////////////
453
egdanielf767e792014-07-02 06:21:32 -0700454class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800455
456struct DashingCircleBatchTracker {
457 GrGPInput fInputColorType;
458 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800459 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800460};
461
egdanielf767e792014-07-02 06:21:32 -0700462/*
463 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
464 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
465 * Both of the previous two parameters are in device space. This effect also requires the setting of
466 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
467 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
468 * transform the line to be horizontal, with the start of line at the origin then shifted to the
469 * right by half the off interval. The line then goes in the positive x direction.
470 */
joshualitt249af152014-09-15 11:41:13 -0700471class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700472public:
473 typedef SkPathEffect::DashInfo DashInfo;
474
joshualitt2e3b3e32014-12-09 13:31:14 -0800475 static GrGeometryProcessor* Create(GrColor,
476 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700477 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800478 SkScalar radius,
479 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700480
481 virtual ~DashingCircleEffect();
482
joshualitteb2a6762014-12-04 11:35:33 -0800483 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700484
joshualitt2dd1ae02014-12-03 06:24:10 -0800485 const GrAttribute* inPosition() const { return fInPosition; }
486
487 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700488
joshualittb0a8a372014-09-23 09:50:21 -0700489 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700490
491 SkScalar getRadius() const { return fRadius; }
492
493 SkScalar getCenterX() const { return fCenterX; }
494
495 SkScalar getIntervalLength() const { return fIntervalLength; }
496
joshualitteb2a6762014-12-04 11:35:33 -0800497 virtual void getGLProcessorKey(const GrBatchTracker&,
498 const GrGLCaps&,
499 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700500
joshualitteb2a6762014-12-04 11:35:33 -0800501 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700502
joshualitt9b989322014-12-15 14:16:27 -0800503 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
504
joshualitt290c09b2014-12-19 13:45:20 -0800505 bool onCanMakeEqual(const GrBatchTracker&,
506 const GrGeometryProcessor&,
507 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800508
egdanielf767e792014-07-02 06:21:32 -0700509private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800510 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800511 SkScalar radius, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700512
bsalomon0e08fc12014-10-15 08:19:04 -0700513 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700514
joshualitt56995b52014-12-11 15:44:02 -0800515 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700516
joshualitt2dd1ae02014-12-03 06:24:10 -0800517 GrPrimitiveEdgeType fEdgeType;
518 const GrAttribute* fInPosition;
519 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700520 SkScalar fIntervalLength;
521 SkScalar fRadius;
522 SkScalar fCenterX;
523
joshualittb0a8a372014-09-23 09:50:21 -0700524 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700525
joshualitt249af152014-09-15 11:41:13 -0700526 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700527};
528
529//////////////////////////////////////////////////////////////////////////////
530
joshualitt249af152014-09-15 11:41:13 -0700531class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700532public:
joshualitteb2a6762014-12-04 11:35:33 -0800533 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700534
joshualittc369e7c2014-10-22 10:56:26 -0700535 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700536
joshualitt87f48d92014-12-04 10:41:40 -0800537 static inline void GenKey(const GrGeometryProcessor&,
538 const GrBatchTracker&,
539 const GrGLCaps&,
540 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700541
joshualitt87f48d92014-12-04 10:41:40 -0800542 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800543 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800544 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700545
546private:
joshualitt9b989322014-12-15 14:16:27 -0800547 UniformHandle fParamUniform;
548 UniformHandle fColorUniform;
549 GrColor fColor;
550 SkScalar fPrevRadius;
551 SkScalar fPrevCenterX;
552 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700553 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700554};
555
joshualitteb2a6762014-12-04 11:35:33 -0800556GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
557 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800558 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700559 fPrevRadius = SK_ScalarMin;
560 fPrevCenterX = SK_ScalarMin;
561 fPrevIntervalLength = SK_ScalarMax;
562}
563
joshualittc369e7c2014-10-22 10:56:26 -0700564void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
565 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800566 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
567 GrGLGPBuilder* pb = args.fPB;
egdanielf767e792014-07-02 06:21:32 -0700568 const char *paramName;
569 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
570 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700571 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800572 kVec3f_GrSLType, kDefault_GrSLPrecision,
573 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700574
joshualitt2dd1ae02014-12-03 06:24:10 -0800575 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
576
joshualitt74077b92014-10-24 11:26:03 -0700577 GrGLVertToFrag v(kVec2f_GrSLType);
578 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800579 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700580
joshualitt9b989322014-12-15 14:16:27 -0800581 // Setup pass through color
582 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
583
joshualitt2dd1ae02014-12-03 06:24:10 -0800584 // setup coord outputs
585 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
586 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700587
joshualitt4973d9d2014-11-08 09:24:25 -0800588 // setup position varying
589 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800590 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800591
egdanielf767e792014-07-02 06:21:32 -0700592 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700593 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700594 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700595 v.fsIn(), v.fsIn(), paramName, paramName);
596 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700597 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
598 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700599 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700600 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
601 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
602 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700603 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700604 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
605 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700606 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800607 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700608}
609
joshualitt87f48d92014-12-04 10:41:40 -0800610void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800611 const GrPrimitiveProcessor& processor,
612 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700613 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700614 SkScalar radius = dce.getRadius();
615 SkScalar centerX = dce.getCenterX();
616 SkScalar intervalLength = dce.getIntervalLength();
617 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700618 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700619 fPrevRadius = radius;
620 fPrevCenterX = centerX;
621 fPrevIntervalLength = intervalLength;
622 }
joshualitt9b989322014-12-15 14:16:27 -0800623
624 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
625 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
626 GrGLfloat c[4];
627 GrColorToRGBAFloat(local.fColor, c);
628 pdman.set4fv(fColorUniform, 1, c);
629 fColor = local.fColor;
630 }
egdanielf767e792014-07-02 06:21:32 -0700631}
632
joshualitt87f48d92014-12-04 10:41:40 -0800633void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800634 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800635 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700636 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800637 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700638 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800639 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800640 b->add32(dce.getEdgeType() << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700641}
642
643//////////////////////////////////////////////////////////////////////////////
644
joshualitt2e3b3e32014-12-09 13:31:14 -0800645GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
646 GrPrimitiveEdgeType edgeType,
647 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800648 SkScalar radius,
649 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -0700650 if (info.fCount != 2 || info.fIntervals[0] != 0) {
651 return NULL;
652 }
653
joshualittd27f73e2014-12-29 07:43:36 -0800654 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700655}
656
657DashingCircleEffect::~DashingCircleEffect() {}
658
joshualitt56995b52014-12-11 15:44:02 -0800659void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
660 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700661}
662
joshualitteb2a6762014-12-04 11:35:33 -0800663void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
664 const GrGLCaps& caps,
665 GrProcessorKeyBuilder* b) const {
666 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
667}
668
669GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
670 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700671}
672
joshualitt2e3b3e32014-12-09 13:31:14 -0800673DashingCircleEffect::DashingCircleEffect(GrColor color,
674 GrPrimitiveEdgeType edgeType,
675 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800676 SkScalar radius,
677 const SkMatrix& localMatrix)
678 : INHERITED(color, false, localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800679 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800680 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
681 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700682 SkScalar onLen = info.fIntervals[0];
683 SkScalar offLen = info.fIntervals[1];
684 fIntervalLength = onLen + offLen;
685 fRadius = radius;
686 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700687}
688
bsalomon0e08fc12014-10-15 08:19:04 -0700689bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700690 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700691 return (fEdgeType == dce.fEdgeType &&
692 fIntervalLength == dce.fIntervalLength &&
693 fRadius == dce.fRadius &&
694 fCenterX == dce.fCenterX);
695}
696
joshualitt9b989322014-12-15 14:16:27 -0800697void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
698 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
699 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800700 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800701}
702
joshualitt290c09b2014-12-19 13:45:20 -0800703bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
704 const GrGeometryProcessor& that,
705 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800706 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
707 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800708 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
709 that, theirs.fUsesLocalCoords) &&
710 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800711 theirs.fInputColorType, theirs.fColor);
712}
713
joshualittb0a8a372014-09-23 09:50:21 -0700714GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700715
joshualittb0a8a372014-09-23 09:50:21 -0700716GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
717 GrContext*,
718 const GrDrawTargetCaps& caps,
719 GrTexture*[]) {
720 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
721 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700722 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
723 DashInfo info;
724 info.fCount = 2;
725 SkAutoTArray<SkScalar> intervals(info.fCount);
726 info.fIntervals = intervals.get();
727 info.fIntervals[0] = 0;
728 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
729 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
730
joshualittd27f73e2014-12-29 07:43:36 -0800731 return DashingCircleEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth,
732 GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -0700733}
734
735//////////////////////////////////////////////////////////////////////////////
736
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000737class GLDashingLineEffect;
738
joshualitt9b989322014-12-15 14:16:27 -0800739struct DashingLineBatchTracker {
740 GrGPInput fInputColorType;
741 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800742 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800743};
744
egdanielf767e792014-07-02 06:21:32 -0700745/*
746 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
747 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
748 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
749 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
750 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
751 * line at the origin then shifted to the right by half the off interval. The line then goes in the
752 * positive x direction.
753 */
joshualitt249af152014-09-15 11:41:13 -0700754class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000755public:
756 typedef SkPathEffect::DashInfo DashInfo;
757
joshualitt2e3b3e32014-12-09 13:31:14 -0800758 static GrGeometryProcessor* Create(GrColor,
759 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700760 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800761 SkScalar strokeWidth,
762 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000763
764 virtual ~DashingLineEffect();
765
joshualitteb2a6762014-12-04 11:35:33 -0800766 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000767
joshualitt2dd1ae02014-12-03 06:24:10 -0800768 const GrAttribute* inPosition() const { return fInPosition; }
769
770 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700771
joshualittb0a8a372014-09-23 09:50:21 -0700772 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000773
774 const SkRect& getRect() const { return fRect; }
775
776 SkScalar getIntervalLength() const { return fIntervalLength; }
777
joshualitteb2a6762014-12-04 11:35:33 -0800778 virtual void getGLProcessorKey(const GrBatchTracker& bt,
779 const GrGLCaps& caps,
780 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000781
joshualitteb2a6762014-12-04 11:35:33 -0800782 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000783
joshualitt9b989322014-12-15 14:16:27 -0800784 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
785
joshualitt290c09b2014-12-19 13:45:20 -0800786 bool onCanMakeEqual(const GrBatchTracker&,
787 const GrGeometryProcessor&,
788 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800789
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000790private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800791 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800792 SkScalar strokeWidth, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000793
bsalomon0e08fc12014-10-15 08:19:04 -0700794 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000795
joshualitt56995b52014-12-11 15:44:02 -0800796 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700797
joshualitt2dd1ae02014-12-03 06:24:10 -0800798 GrPrimitiveEdgeType fEdgeType;
799 const GrAttribute* fInPosition;
800 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000801 SkRect fRect;
802 SkScalar fIntervalLength;
803
joshualittb0a8a372014-09-23 09:50:21 -0700804 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000805
joshualitt249af152014-09-15 11:41:13 -0700806 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000807};
808
809//////////////////////////////////////////////////////////////////////////////
810
joshualitt249af152014-09-15 11:41:13 -0700811class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000812public:
joshualitteb2a6762014-12-04 11:35:33 -0800813 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000814
joshualittc369e7c2014-10-22 10:56:26 -0700815 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000816
joshualitt87f48d92014-12-04 10:41:40 -0800817 static inline void GenKey(const GrGeometryProcessor&,
818 const GrBatchTracker&,
819 const GrGLCaps&,
820 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000821
joshualitt87f48d92014-12-04 10:41:40 -0800822 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800823 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800824 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000825
826private:
joshualitt9b989322014-12-15 14:16:27 -0800827 GrColor fColor;
828 UniformHandle fRectUniform;
829 UniformHandle fIntervalUniform;
830 UniformHandle fColorUniform;
831 SkRect fPrevRect;
832 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700833 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000834};
835
joshualitteb2a6762014-12-04 11:35:33 -0800836GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
837 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800838 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000839 fPrevRect.fLeft = SK_ScalarNaN;
840 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000841}
842
joshualittc369e7c2014-10-22 10:56:26 -0700843void GLDashingLineEffect::emitCode(const EmitArgs& args) {
844 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800845 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
846 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000847 const char *rectName;
848 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
849 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700850 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800851 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000852 "rect",
853 &rectName);
854 const char *intervalName;
855 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700856 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800857 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700858 "interval",
859 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700860
joshualitt2dd1ae02014-12-03 06:24:10 -0800861
862 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
863
joshualitt74077b92014-10-24 11:26:03 -0700864 GrGLVertToFrag v(kVec2f_GrSLType);
865 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800866 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
867
joshualitt9b989322014-12-15 14:16:27 -0800868 // Setup pass through color
869 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
870
joshualitt2dd1ae02014-12-03 06:24:10 -0800871 // setup coord outputs
872 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
873 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700874
joshualitt4973d9d2014-11-08 09:24:25 -0800875 // setup position varying
876 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800877 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800878
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000879 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700880 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700881 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700882 v.fsIn(), v.fsIn(), intervalName, intervalName);
883 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700884 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000885 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
886 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700887 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
888 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
889 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
890 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
891 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000892 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
893 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700894 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 +0000895 } else {
896 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700897 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
898 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
899 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 +0000900 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800901 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000902}
903
joshualittb0a8a372014-09-23 09:50:21 -0700904void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800905 const GrPrimitiveProcessor& processor,
906 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700907 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000908 const SkRect& rect = de.getRect();
909 SkScalar intervalLength = de.getIntervalLength();
910 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700911 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
912 rect.fRight - 0.5f, rect.fBottom - 0.5f);
913 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000914 fPrevRect = rect;
915 fPrevIntervalLength = intervalLength;
916 }
joshualitt9b989322014-12-15 14:16:27 -0800917
918 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
919 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
920 GrGLfloat c[4];
921 GrColorToRGBAFloat(local.fColor, c);
922 pdman.set4fv(fColorUniform, 1, c);
923 fColor = local.fColor;
924 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000925}
926
joshualitt87f48d92014-12-04 10:41:40 -0800927void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800928 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800929 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700930 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800931 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700932 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800933 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800934 b->add32(de.getEdgeType() << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000935}
936
937//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000938
joshualitt2e3b3e32014-12-09 13:31:14 -0800939GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
940 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700941 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800942 SkScalar strokeWidth,
943 const SkMatrix& localMatrix) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000944 if (info.fCount != 2) {
945 return NULL;
946 }
947
joshualittd27f73e2014-12-29 07:43:36 -0800948 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000949}
950
951DashingLineEffect::~DashingLineEffect() {}
952
joshualitt56995b52014-12-11 15:44:02 -0800953void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
954 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000955}
956
joshualitteb2a6762014-12-04 11:35:33 -0800957void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
958 const GrGLCaps& caps,
959 GrProcessorKeyBuilder* b) const {
960 GLDashingLineEffect::GenKey(*this, bt, caps, b);
961}
962
963GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
964 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000965}
966
joshualitt2e3b3e32014-12-09 13:31:14 -0800967DashingLineEffect::DashingLineEffect(GrColor color,
968 GrPrimitiveEdgeType edgeType,
969 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800970 SkScalar strokeWidth,
971 const SkMatrix& localMatrix)
972 : INHERITED(color, false, localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800973 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800974 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
975 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000976 SkScalar onLen = info.fIntervals[0];
977 SkScalar offLen = info.fIntervals[1];
978 SkScalar halfOffLen = SkScalarHalf(offLen);
979 SkScalar halfStroke = SkScalarHalf(strokeWidth);
980 fIntervalLength = onLen + offLen;
981 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000982}
983
bsalomon0e08fc12014-10-15 08:19:04 -0700984bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700985 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000986 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000987 fRect == de.fRect &&
988 fIntervalLength == de.fIntervalLength);
989}
990
joshualitt9b989322014-12-15 14:16:27 -0800991void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
992 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
993 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800994 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800995}
996
joshualitt290c09b2014-12-19 13:45:20 -0800997bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
998 const GrGeometryProcessor& that,
999 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001000 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1001 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001002 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1003 that, theirs.fUsesLocalCoords) &&
1004 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001005 theirs.fInputColorType, theirs.fColor);
1006}
1007
joshualittb0a8a372014-09-23 09:50:21 -07001008GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001009
joshualittb0a8a372014-09-23 09:50:21 -07001010GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1011 GrContext*,
1012 const GrDrawTargetCaps& caps,
1013 GrTexture*[]) {
1014 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
1015 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001016 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
1017 DashInfo info;
1018 info.fCount = 2;
1019 SkAutoTArray<SkScalar> intervals(info.fCount);
1020 info.fIntervals = intervals.get();
1021 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
1022 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
1023 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
1024
joshualittd27f73e2014-12-29 07:43:36 -08001025 return DashingLineEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth,
1026 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001027}
1028
1029//////////////////////////////////////////////////////////////////////////////
1030
joshualitt2e3b3e32014-12-09 13:31:14 -08001031GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
1032 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -07001033 const SkPathEffect::DashInfo& info,
1034 SkScalar strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001035 GrDashingEffect::DashCap cap,
1036 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001037 switch (cap) {
1038 case GrDashingEffect::kRound_DashCap:
joshualittd27f73e2014-12-29 07:43:36 -08001039 return DashingCircleEffect::Create(color, edgeType, info, SkScalarHalf(strokeWidth),
1040 localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001041 case GrDashingEffect::kNonRound_DashCap:
joshualittd27f73e2014-12-29 07:43:36 -08001042 return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001043 default:
1044 SkFAIL("Unexpected dashed cap.");
1045 }
1046 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001047}