blob: 4788b3a5b1af519a4e166ac07b2ac28479f87ba8 [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,
joshualitt8059eb92014-12-29 15:10:07 -0800168 GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
169 const GrPaint& paint, const GrStrokeInfo& strokeInfo) {
170 if (!can_fast_path_dash(pts, strokeInfo, *target, *drawState, viewMatrix)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000171 return false;
172 }
173
egdaniele61c4112014-06-12 10:24:21 -0700174 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000175
egdaniele61c4112014-06-12 10:24:21 -0700176 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000177
egdaniele61c4112014-06-12 10:24:21 -0700178 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000179
180 // the phase should be normalized to be [0, sum of all intervals)
181 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
182
egdaniele61c4112014-06-12 10:24:21 -0700183 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000184
185 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
186 SkMatrix srcRotInv;
187 SkPoint ptsRot[2];
188 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700189 SkMatrix rotMatrix;
190 align_to_x_axis(pts, &rotMatrix, ptsRot);
191 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700192 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000193 return false;
194 }
195 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000196 srcRotInv.reset();
197 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
198 }
199
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000200 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000201
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000202 // Scale corrections of intervals and stroke from view matrix
203 SkScalar parallelScale;
204 SkScalar perpScale;
joshualitt8059eb92014-12-29 15:10:07 -0800205 calc_dash_scaling(&parallelScale, &perpScale, viewMatrix, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000206
egdanielf767e792014-07-02 06:21:32 -0700207 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000208
209 // We always want to at least stroke out half a pixel on each side in device space
210 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700211 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000212
egdaniele61c4112014-06-12 10:24:21 -0700213 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000214 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700215 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000216 } else {
egdaniele61c4112014-06-12 10:24:21 -0700217 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000218 }
219
egdaniele61c4112014-06-12 10:24:21 -0700220 SkScalar startAdj = 0;
221
222 SkMatrix combinedMatrix = srcRotInv;
joshualitt8059eb92014-12-29 15:10:07 -0800223 combinedMatrix.postConcat(viewMatrix);
egdaniele61c4112014-06-12 10:24:21 -0700224
225 bool lineDone = false;
226 SkRect startRect;
227 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000228 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
229 // draw it separately here and adjust our start point accordingly
230 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700231 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000232 SkPoint startPts[2];
233 startPts[0] = ptsRot[0];
234 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700235 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000236 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700238 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000239
egdaniele61c4112014-06-12 10:24:21 -0700240 hasStartRect = true;
241 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000242 }
243 }
244
245 // adjustments for start and end of bounding rect so we only draw dash intervals
246 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700247 startAdj += calc_start_adjustment(info);
248 if (startAdj != 0) {
249 ptsRot[0].fX += startAdj;
250 srcPhase = 0;
251 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000252 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700253 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
254 ptsRot[1].fX -= endAdj;
255 if (ptsRot[0].fX >= ptsRot[1].fX) {
256 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000257 }
258
egdaniele61c4112014-06-12 10:24:21 -0700259 SkRect endRect;
260 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000261 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
262 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700263 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000264 // If we adjusted the end then we will not be drawing a partial dash at the end.
265 // If we didn't adjust the end point then we just need to make sure the ending
266 // dash isn't a full dash
267 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000268 SkPoint endPts[2];
269 endPts[1] = ptsRot[1];
270 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000271 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000272
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700274 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000275
egdaniele61c4112014-06-12 10:24:21 -0700276 hasEndRect = true;
277 endAdj = endingInterval + info.fIntervals[1];
278
279 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000280 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700281 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000282 }
283 }
284 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000285
egdaniele61c4112014-06-12 10:24:21 -0700286 if (startAdj != 0) {
287 srcPhase = 0;
288 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000289
egdaniele61c4112014-06-12 10:24:21 -0700290 // Change the dashing info from src space into device space
291 SkScalar devIntervals[2];
292 devIntervals[0] = info.fIntervals[0] * parallelScale;
293 devIntervals[1] = info.fIntervals[1] * parallelScale;
294 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000295 SkScalar strokeWidth = srcStrokeWidth * perpScale;
296
297 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
298 strokeWidth = 1.f;
299 }
300
egdaniele61c4112014-06-12 10:24:21 -0700301 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000302
303 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
304 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700305 devIntervals[0] += strokeWidth;
306 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000307 }
egdaniele61c4112014-06-12 10:24:21 -0700308 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000309
egdaniele61c4112014-06-12 10:24:21 -0700310 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
311 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
312
313 SkScalar devBloat = useAA ? 0.5f : 0.f;
314
egdaniele61c4112014-06-12 10:24:21 -0700315 if (devIntervals[1] <= 0.f && useAA) {
316 // Case when we end up drawing a solid AA rect
317 // Reset the start rect to draw this single solid rect
318 // but it requires to upload a new intervals uniform so we can mimic
319 // one giant dash
320 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
321 ptsRot[1].fX += hasEndRect ? endAdj : 0;
322 startRect.set(ptsRot, 2);
323 startRect.outset(strokeAdj, halfSrcStroke);
324 hasStartRect = true;
325 hasEndRect = false;
326 lineDone = true;
327
328 SkPoint devicePts[2];
joshualitt8059eb92014-12-29 15:10:07 -0800329 viewMatrix.mapPoints(devicePts, ptsRot, 2);
egdaniele61c4112014-06-12 10:24:21 -0700330 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
331 if (hasCap) {
332 lineLength += 2.f * halfDevStroke;
333 }
334 devIntervals[0] = lineLength;
335 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800336
joshualittd27f73e2014-12-29 07:43:36 -0800337 // reset to device coordinates
338 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800339 if (!viewMatrix.invert(&invert)) {
joshualittd27f73e2014-12-29 07:43:36 -0800340 SkDebugf("Failed to invert\n");
341 return false;
342 }
343
joshualitt56995b52014-12-11 15:44:02 -0800344 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt5478d422014-11-14 16:00:38 -0800345 bool fullDash = devIntervals[1] > 0.f || useAA;
346 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700347 SkPathEffect::DashInfo devInfo;
348 devInfo.fPhase = devPhase;
349 devInfo.fCount = 2;
350 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700351 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
352 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700353 bool isRoundCap = SkPaint::kRound_Cap == cap;
354 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
355 GrDashingEffect::kNonRound_DashCap;
joshualittd27f73e2014-12-29 07:43:36 -0800356 gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType, invert));
joshualitt249af152014-09-15 11:41:13 -0700357 } else {
358 // Set up the vertex data for the line and start/end dashes
joshualitt8059eb92014-12-29 15:10:07 -0800359 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
360 color,
361 SkMatrix::I(),
joshualittd27f73e2014-12-29 07:43:36 -0800362 invert));
joshualitt249af152014-09-15 11:41:13 -0700363 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000364
egdaniele61c4112014-06-12 10:24:21 -0700365 int totalRectCnt = 0;
366
367 totalRectCnt += !lineDone ? 1 : 0;
368 totalRectCnt += hasStartRect ? 1 : 0;
369 totalRectCnt += hasEndRect ? 1 : 0;
370
joshualitt9853cce2014-11-17 14:22:48 -0800371 GrDrawTarget::AutoReleaseGeometry geo(target,
372 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800373 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700374 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700375 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700376 return false;
377 }
378
egdaniele61c4112014-06-12 10:24:21 -0700379 int curVIdx = 0;
380
egdanielf767e792014-07-02 06:21:32 -0700381 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
382 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
383 startOffset -= halfDevStroke;
384 }
385
egdaniele61c4112014-06-12 10:24:21 -0700386 // Draw interior part of dashed line
387 if (!lineDone) {
388 SkPoint devicePts[2];
joshualitt8059eb92014-12-29 15:10:07 -0800389 viewMatrix.mapPoints(devicePts, ptsRot, 2);
egdaniele61c4112014-06-12 10:24:21 -0700390 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
391 if (hasCap) {
392 lineLength += 2.f * halfDevStroke;
393 }
394
395 SkRect bounds;
396 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
397 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800398 if (fullDash) {
399 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800400 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800401 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
402 lineLength, halfDevStroke);
403 } else {
404 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800405 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800406 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
407 }
egdaniele61c4112014-06-12 10:24:21 -0700408 curVIdx += 4;
409 }
410
411 if (hasStartRect) {
412 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
413 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800414 if (fullDash) {
415 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800416 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800417 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
418 devIntervals[0], halfDevStroke);
419 } else {
420 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800421 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800422 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
423 }
424
egdaniele61c4112014-06-12 10:24:21 -0700425 curVIdx += 4;
426 }
427
428 if (hasEndRect) {
429 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
430 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800431 if (fullDash) {
432 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800433 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800434 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
435 devIntervals[0], halfDevStroke);
436 } else {
437 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800438 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800439 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
440 }
441
egdaniele61c4112014-06-12 10:24:21 -0700442 }
443
444 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
joshualitt56995b52014-12-11 15:44:02 -0800445 target->drawIndexedInstances(drawState, gp, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700446 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000447 return true;
448}
449
450//////////////////////////////////////////////////////////////////////////////
451
egdanielf767e792014-07-02 06:21:32 -0700452class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800453
454struct DashingCircleBatchTracker {
455 GrGPInput fInputColorType;
456 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800457 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800458};
459
egdanielf767e792014-07-02 06:21:32 -0700460/*
461 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
462 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
463 * Both of the previous two parameters are in device space. This effect also requires the setting of
464 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
465 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
466 * transform the line to be horizontal, with the start of line at the origin then shifted to the
467 * right by half the off interval. The line then goes in the positive x direction.
468 */
joshualitt249af152014-09-15 11:41:13 -0700469class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700470public:
471 typedef SkPathEffect::DashInfo DashInfo;
472
joshualitt2e3b3e32014-12-09 13:31:14 -0800473 static GrGeometryProcessor* Create(GrColor,
474 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700475 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800476 SkScalar radius,
477 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700478
479 virtual ~DashingCircleEffect();
480
joshualitteb2a6762014-12-04 11:35:33 -0800481 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700482
joshualitt2dd1ae02014-12-03 06:24:10 -0800483 const GrAttribute* inPosition() const { return fInPosition; }
484
485 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700486
joshualittb0a8a372014-09-23 09:50:21 -0700487 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700488
489 SkScalar getRadius() const { return fRadius; }
490
491 SkScalar getCenterX() const { return fCenterX; }
492
493 SkScalar getIntervalLength() const { return fIntervalLength; }
494
joshualitteb2a6762014-12-04 11:35:33 -0800495 virtual void getGLProcessorKey(const GrBatchTracker&,
496 const GrGLCaps&,
497 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700498
joshualitteb2a6762014-12-04 11:35:33 -0800499 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700500
joshualitt9b989322014-12-15 14:16:27 -0800501 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
502
joshualitt290c09b2014-12-19 13:45:20 -0800503 bool onCanMakeEqual(const GrBatchTracker&,
504 const GrGeometryProcessor&,
505 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800506
egdanielf767e792014-07-02 06:21:32 -0700507private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800508 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800509 SkScalar radius, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700510
bsalomon0e08fc12014-10-15 08:19:04 -0700511 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700512
joshualitt56995b52014-12-11 15:44:02 -0800513 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700514
joshualitt2dd1ae02014-12-03 06:24:10 -0800515 GrPrimitiveEdgeType fEdgeType;
516 const GrAttribute* fInPosition;
517 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700518 SkScalar fIntervalLength;
519 SkScalar fRadius;
520 SkScalar fCenterX;
521
joshualittb0a8a372014-09-23 09:50:21 -0700522 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700523
joshualitt249af152014-09-15 11:41:13 -0700524 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700525};
526
527//////////////////////////////////////////////////////////////////////////////
528
joshualitt249af152014-09-15 11:41:13 -0700529class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700530public:
joshualitteb2a6762014-12-04 11:35:33 -0800531 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700532
joshualittc369e7c2014-10-22 10:56:26 -0700533 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700534
joshualitt87f48d92014-12-04 10:41:40 -0800535 static inline void GenKey(const GrGeometryProcessor&,
536 const GrBatchTracker&,
537 const GrGLCaps&,
538 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700539
joshualitt87f48d92014-12-04 10:41:40 -0800540 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800541 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800542 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700543
544private:
joshualitt9b989322014-12-15 14:16:27 -0800545 UniformHandle fParamUniform;
546 UniformHandle fColorUniform;
547 GrColor fColor;
548 SkScalar fPrevRadius;
549 SkScalar fPrevCenterX;
550 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700551 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700552};
553
joshualitteb2a6762014-12-04 11:35:33 -0800554GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
555 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800556 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700557 fPrevRadius = SK_ScalarMin;
558 fPrevCenterX = SK_ScalarMin;
559 fPrevIntervalLength = SK_ScalarMax;
560}
561
joshualittc369e7c2014-10-22 10:56:26 -0700562void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
563 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800564 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
565 GrGLGPBuilder* pb = args.fPB;
egdanielf767e792014-07-02 06:21:32 -0700566 const char *paramName;
567 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
568 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700569 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800570 kVec3f_GrSLType, kDefault_GrSLPrecision,
571 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700572
joshualitt2dd1ae02014-12-03 06:24:10 -0800573 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
574
joshualitt74077b92014-10-24 11:26:03 -0700575 GrGLVertToFrag v(kVec2f_GrSLType);
576 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800577 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700578
joshualitt9b989322014-12-15 14:16:27 -0800579 // Setup pass through color
580 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
581
joshualitt2dd1ae02014-12-03 06:24:10 -0800582 // setup coord outputs
583 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
584 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700585
joshualitt4973d9d2014-11-08 09:24:25 -0800586 // setup position varying
587 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800588 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800589
egdanielf767e792014-07-02 06:21:32 -0700590 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700591 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700592 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700593 v.fsIn(), v.fsIn(), paramName, paramName);
594 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700595 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
596 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700597 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700598 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
599 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
600 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700601 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700602 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
603 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700604 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800605 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700606}
607
joshualitt87f48d92014-12-04 10:41:40 -0800608void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800609 const GrPrimitiveProcessor& processor,
610 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700611 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700612 SkScalar radius = dce.getRadius();
613 SkScalar centerX = dce.getCenterX();
614 SkScalar intervalLength = dce.getIntervalLength();
615 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700616 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700617 fPrevRadius = radius;
618 fPrevCenterX = centerX;
619 fPrevIntervalLength = intervalLength;
620 }
joshualitt9b989322014-12-15 14:16:27 -0800621
622 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
623 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
624 GrGLfloat c[4];
625 GrColorToRGBAFloat(local.fColor, c);
626 pdman.set4fv(fColorUniform, 1, c);
627 fColor = local.fColor;
628 }
egdanielf767e792014-07-02 06:21:32 -0700629}
630
joshualitt87f48d92014-12-04 10:41:40 -0800631void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800632 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800633 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700634 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800635 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700636 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800637 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800638 b->add32(dce.getEdgeType() << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700639}
640
641//////////////////////////////////////////////////////////////////////////////
642
joshualitt2e3b3e32014-12-09 13:31:14 -0800643GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
644 GrPrimitiveEdgeType edgeType,
645 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800646 SkScalar radius,
647 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -0700648 if (info.fCount != 2 || info.fIntervals[0] != 0) {
649 return NULL;
650 }
651
joshualittd27f73e2014-12-29 07:43:36 -0800652 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700653}
654
655DashingCircleEffect::~DashingCircleEffect() {}
656
joshualitt56995b52014-12-11 15:44:02 -0800657void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
658 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700659}
660
joshualitteb2a6762014-12-04 11:35:33 -0800661void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
662 const GrGLCaps& caps,
663 GrProcessorKeyBuilder* b) const {
664 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
665}
666
667GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
668 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700669}
670
joshualitt2e3b3e32014-12-09 13:31:14 -0800671DashingCircleEffect::DashingCircleEffect(GrColor color,
672 GrPrimitiveEdgeType edgeType,
673 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800674 SkScalar radius,
675 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800676 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800677 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800678 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
679 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700680 SkScalar onLen = info.fIntervals[0];
681 SkScalar offLen = info.fIntervals[1];
682 fIntervalLength = onLen + offLen;
683 fRadius = radius;
684 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700685}
686
bsalomon0e08fc12014-10-15 08:19:04 -0700687bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700688 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700689 return (fEdgeType == dce.fEdgeType &&
690 fIntervalLength == dce.fIntervalLength &&
691 fRadius == dce.fRadius &&
692 fCenterX == dce.fCenterX);
693}
694
joshualitt9b989322014-12-15 14:16:27 -0800695void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
696 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
697 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800698 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800699}
700
joshualitt290c09b2014-12-19 13:45:20 -0800701bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
702 const GrGeometryProcessor& that,
703 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800704 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
705 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800706 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
707 that, theirs.fUsesLocalCoords) &&
708 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800709 theirs.fInputColorType, theirs.fColor);
710}
711
joshualittb0a8a372014-09-23 09:50:21 -0700712GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700713
joshualittb0a8a372014-09-23 09:50:21 -0700714GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
715 GrContext*,
716 const GrDrawTargetCaps& caps,
717 GrTexture*[]) {
718 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
719 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700720 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
721 DashInfo info;
722 info.fCount = 2;
723 SkAutoTArray<SkScalar> intervals(info.fCount);
724 info.fIntervals = intervals.get();
725 info.fIntervals[0] = 0;
726 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
727 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
728
joshualitt8059eb92014-12-29 15:10:07 -0800729 return DashingCircleEffect::Create(GrRandomColor(random),
730 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -0800731 GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -0700732}
733
734//////////////////////////////////////////////////////////////////////////////
735
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000736class GLDashingLineEffect;
737
joshualitt9b989322014-12-15 14:16:27 -0800738struct DashingLineBatchTracker {
739 GrGPInput fInputColorType;
740 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800741 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800742};
743
egdanielf767e792014-07-02 06:21:32 -0700744/*
745 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
746 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
747 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
748 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
749 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
750 * line at the origin then shifted to the right by half the off interval. The line then goes in the
751 * positive x direction.
752 */
joshualitt249af152014-09-15 11:41:13 -0700753class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000754public:
755 typedef SkPathEffect::DashInfo DashInfo;
756
joshualitt2e3b3e32014-12-09 13:31:14 -0800757 static GrGeometryProcessor* Create(GrColor,
758 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700759 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800760 SkScalar strokeWidth,
761 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762
763 virtual ~DashingLineEffect();
764
joshualitteb2a6762014-12-04 11:35:33 -0800765 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000766
joshualitt2dd1ae02014-12-03 06:24:10 -0800767 const GrAttribute* inPosition() const { return fInPosition; }
768
769 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700770
joshualittb0a8a372014-09-23 09:50:21 -0700771 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000772
773 const SkRect& getRect() const { return fRect; }
774
775 SkScalar getIntervalLength() const { return fIntervalLength; }
776
joshualitteb2a6762014-12-04 11:35:33 -0800777 virtual void getGLProcessorKey(const GrBatchTracker& bt,
778 const GrGLCaps& caps,
779 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000780
joshualitteb2a6762014-12-04 11:35:33 -0800781 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000782
joshualitt9b989322014-12-15 14:16:27 -0800783 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
784
joshualitt290c09b2014-12-19 13:45:20 -0800785 bool onCanMakeEqual(const GrBatchTracker&,
786 const GrGeometryProcessor&,
787 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800788
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000789private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800790 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800791 SkScalar strokeWidth, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000792
bsalomon0e08fc12014-10-15 08:19:04 -0700793 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000794
joshualitt56995b52014-12-11 15:44:02 -0800795 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700796
joshualitt2dd1ae02014-12-03 06:24:10 -0800797 GrPrimitiveEdgeType fEdgeType;
798 const GrAttribute* fInPosition;
799 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000800 SkRect fRect;
801 SkScalar fIntervalLength;
802
joshualittb0a8a372014-09-23 09:50:21 -0700803 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000804
joshualitt249af152014-09-15 11:41:13 -0700805 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000806};
807
808//////////////////////////////////////////////////////////////////////////////
809
joshualitt249af152014-09-15 11:41:13 -0700810class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000811public:
joshualitteb2a6762014-12-04 11:35:33 -0800812 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000813
joshualittc369e7c2014-10-22 10:56:26 -0700814 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000815
joshualitt87f48d92014-12-04 10:41:40 -0800816 static inline void GenKey(const GrGeometryProcessor&,
817 const GrBatchTracker&,
818 const GrGLCaps&,
819 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000820
joshualitt87f48d92014-12-04 10:41:40 -0800821 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800822 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800823 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000824
825private:
joshualitt9b989322014-12-15 14:16:27 -0800826 GrColor fColor;
827 UniformHandle fRectUniform;
828 UniformHandle fIntervalUniform;
829 UniformHandle fColorUniform;
830 SkRect fPrevRect;
831 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700832 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000833};
834
joshualitteb2a6762014-12-04 11:35:33 -0800835GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
836 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800837 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000838 fPrevRect.fLeft = SK_ScalarNaN;
839 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000840}
841
joshualittc369e7c2014-10-22 10:56:26 -0700842void GLDashingLineEffect::emitCode(const EmitArgs& args) {
843 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800844 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
845 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000846 const char *rectName;
847 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
848 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700849 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800850 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000851 "rect",
852 &rectName);
853 const char *intervalName;
854 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700855 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800856 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700857 "interval",
858 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700859
joshualitt2dd1ae02014-12-03 06:24:10 -0800860
861 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
862
joshualitt74077b92014-10-24 11:26:03 -0700863 GrGLVertToFrag v(kVec2f_GrSLType);
864 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800865 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
866
joshualitt9b989322014-12-15 14:16:27 -0800867 // Setup pass through color
868 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
869
joshualitt2dd1ae02014-12-03 06:24:10 -0800870 // setup coord outputs
871 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
872 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700873
joshualitt4973d9d2014-11-08 09:24:25 -0800874 // setup position varying
875 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800876 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800877
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000878 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700879 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700880 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700881 v.fsIn(), v.fsIn(), intervalName, intervalName);
882 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700883 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000884 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
885 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700886 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
887 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
888 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
889 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
890 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000891 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
892 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700893 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 +0000894 } else {
895 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700896 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
897 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
898 fsBuilder->codeAppendf("\t\talpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000899 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800900 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000901}
902
joshualittb0a8a372014-09-23 09:50:21 -0700903void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800904 const GrPrimitiveProcessor& processor,
905 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700906 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000907 const SkRect& rect = de.getRect();
908 SkScalar intervalLength = de.getIntervalLength();
909 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700910 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
911 rect.fRight - 0.5f, rect.fBottom - 0.5f);
912 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000913 fPrevRect = rect;
914 fPrevIntervalLength = intervalLength;
915 }
joshualitt9b989322014-12-15 14:16:27 -0800916
917 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
918 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
919 GrGLfloat c[4];
920 GrColorToRGBAFloat(local.fColor, c);
921 pdman.set4fv(fColorUniform, 1, c);
922 fColor = local.fColor;
923 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000924}
925
joshualitt87f48d92014-12-04 10:41:40 -0800926void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800927 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800928 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700929 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800930 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700931 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800932 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800933 b->add32(de.getEdgeType() << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000934}
935
936//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000937
joshualitt2e3b3e32014-12-09 13:31:14 -0800938GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
939 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700940 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800941 SkScalar strokeWidth,
942 const SkMatrix& localMatrix) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000943 if (info.fCount != 2) {
944 return NULL;
945 }
946
joshualittd27f73e2014-12-29 07:43:36 -0800947 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000948}
949
950DashingLineEffect::~DashingLineEffect() {}
951
joshualitt56995b52014-12-11 15:44:02 -0800952void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
953 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000954}
955
joshualitteb2a6762014-12-04 11:35:33 -0800956void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
957 const GrGLCaps& caps,
958 GrProcessorKeyBuilder* b) const {
959 GLDashingLineEffect::GenKey(*this, bt, caps, b);
960}
961
962GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
963 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000964}
965
joshualitt2e3b3e32014-12-09 13:31:14 -0800966DashingLineEffect::DashingLineEffect(GrColor color,
967 GrPrimitiveEdgeType edgeType,
968 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800969 SkScalar strokeWidth,
970 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800971 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800972 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800973 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
974 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000975 SkScalar onLen = info.fIntervals[0];
976 SkScalar offLen = info.fIntervals[1];
977 SkScalar halfOffLen = SkScalarHalf(offLen);
978 SkScalar halfStroke = SkScalarHalf(strokeWidth);
979 fIntervalLength = onLen + offLen;
980 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000981}
982
bsalomon0e08fc12014-10-15 08:19:04 -0700983bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700984 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000985 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000986 fRect == de.fRect &&
987 fIntervalLength == de.fIntervalLength);
988}
989
joshualitt9b989322014-12-15 14:16:27 -0800990void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
991 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
992 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800993 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800994}
995
joshualitt290c09b2014-12-19 13:45:20 -0800996bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
997 const GrGeometryProcessor& that,
998 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800999 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1000 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001001 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1002 that, theirs.fUsesLocalCoords) &&
1003 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001004 theirs.fInputColorType, theirs.fColor);
1005}
1006
joshualittb0a8a372014-09-23 09:50:21 -07001007GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001008
joshualittb0a8a372014-09-23 09:50:21 -07001009GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1010 GrContext*,
1011 const GrDrawTargetCaps& caps,
1012 GrTexture*[]) {
1013 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
1014 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001015 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
1016 DashInfo info;
1017 info.fCount = 2;
1018 SkAutoTArray<SkScalar> intervals(info.fCount);
1019 info.fIntervals = intervals.get();
1020 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
1021 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
1022 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
1023
joshualitt8059eb92014-12-29 15:10:07 -08001024 return DashingLineEffect::Create(GrRandomColor(random),
1025 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001026 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:
joshualitt8059eb92014-12-29 15:10:07 -08001039 return DashingCircleEffect::Create(color, edgeType, info,
1040 SkScalarHalf(strokeWidth),
joshualittd27f73e2014-12-29 07:43:36 -08001041 localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001042 case GrDashingEffect::kNonRound_DashCap:
joshualittd27f73e2014-12-29 07:43:36 -08001043 return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001044 default:
1045 SkFAIL("Unexpected dashed cap.");
1046 }
1047 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001048}