blob: 47b8d65a22e629a2174453e293c58a30cce29c75 [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
joshualittee2af952014-12-30 09:04:15 -0800586 // setup uniform viewMatrix
587 this->addUniformViewMatrix(pb);
588
joshualitt4973d9d2014-11-08 09:24:25 -0800589 // setup position varying
joshualittee2af952014-12-30 09:04:15 -0800590 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800591 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800592
egdanielf767e792014-07-02 06:21:32 -0700593 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700594 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700595 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700596 v.fsIn(), v.fsIn(), paramName, paramName);
597 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700598 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
599 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700600 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700601 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
602 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
603 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700604 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700605 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
606 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700607 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800608 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700609}
610
joshualitt87f48d92014-12-04 10:41:40 -0800611void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800612 const GrPrimitiveProcessor& processor,
613 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800614 this->setUniformViewMatrix(pdman, processor.viewMatrix());
615
joshualittb0a8a372014-09-23 09:50:21 -0700616 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700617 SkScalar radius = dce.getRadius();
618 SkScalar centerX = dce.getCenterX();
619 SkScalar intervalLength = dce.getIntervalLength();
620 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700621 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700622 fPrevRadius = radius;
623 fPrevCenterX = centerX;
624 fPrevIntervalLength = intervalLength;
625 }
joshualitt9b989322014-12-15 14:16:27 -0800626
627 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
628 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
629 GrGLfloat c[4];
630 GrColorToRGBAFloat(local.fColor, c);
631 pdman.set4fv(fColorUniform, 1, c);
632 fColor = local.fColor;
633 }
egdanielf767e792014-07-02 06:21:32 -0700634}
635
joshualitt87f48d92014-12-04 10:41:40 -0800636void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800637 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800638 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700639 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800640 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700641 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800642 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800643 b->add32(dce.getEdgeType() << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700644}
645
646//////////////////////////////////////////////////////////////////////////////
647
joshualitt2e3b3e32014-12-09 13:31:14 -0800648GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
649 GrPrimitiveEdgeType edgeType,
650 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800651 SkScalar radius,
652 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -0700653 if (info.fCount != 2 || info.fIntervals[0] != 0) {
654 return NULL;
655 }
656
joshualittd27f73e2014-12-29 07:43:36 -0800657 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700658}
659
660DashingCircleEffect::~DashingCircleEffect() {}
661
joshualitt56995b52014-12-11 15:44:02 -0800662void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
663 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700664}
665
joshualitteb2a6762014-12-04 11:35:33 -0800666void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
667 const GrGLCaps& caps,
668 GrProcessorKeyBuilder* b) const {
669 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
670}
671
672GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
673 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700674}
675
joshualitt2e3b3e32014-12-09 13:31:14 -0800676DashingCircleEffect::DashingCircleEffect(GrColor color,
677 GrPrimitiveEdgeType edgeType,
678 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800679 SkScalar radius,
680 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800681 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800682 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800683 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
684 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700685 SkScalar onLen = info.fIntervals[0];
686 SkScalar offLen = info.fIntervals[1];
687 fIntervalLength = onLen + offLen;
688 fRadius = radius;
689 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700690}
691
bsalomon0e08fc12014-10-15 08:19:04 -0700692bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700693 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700694 return (fEdgeType == dce.fEdgeType &&
695 fIntervalLength == dce.fIntervalLength &&
696 fRadius == dce.fRadius &&
697 fCenterX == dce.fCenterX);
698}
699
joshualitt9b989322014-12-15 14:16:27 -0800700void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
701 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
702 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800703 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800704}
705
joshualitt290c09b2014-12-19 13:45:20 -0800706bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
707 const GrGeometryProcessor& that,
708 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800709 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
710 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800711 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
712 that, theirs.fUsesLocalCoords) &&
713 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800714 theirs.fInputColorType, theirs.fColor);
715}
716
joshualittb0a8a372014-09-23 09:50:21 -0700717GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700718
joshualittb0a8a372014-09-23 09:50:21 -0700719GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
720 GrContext*,
721 const GrDrawTargetCaps& caps,
722 GrTexture*[]) {
723 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
724 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700725 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
726 DashInfo info;
727 info.fCount = 2;
728 SkAutoTArray<SkScalar> intervals(info.fCount);
729 info.fIntervals = intervals.get();
730 info.fIntervals[0] = 0;
731 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
732 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
733
joshualitt8059eb92014-12-29 15:10:07 -0800734 return DashingCircleEffect::Create(GrRandomColor(random),
735 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -0800736 GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -0700737}
738
739//////////////////////////////////////////////////////////////////////////////
740
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000741class GLDashingLineEffect;
742
joshualitt9b989322014-12-15 14:16:27 -0800743struct DashingLineBatchTracker {
744 GrGPInput fInputColorType;
745 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800746 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800747};
748
egdanielf767e792014-07-02 06:21:32 -0700749/*
750 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
751 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
752 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
753 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
754 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
755 * line at the origin then shifted to the right by half the off interval. The line then goes in the
756 * positive x direction.
757 */
joshualitt249af152014-09-15 11:41:13 -0700758class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000759public:
760 typedef SkPathEffect::DashInfo DashInfo;
761
joshualitt2e3b3e32014-12-09 13:31:14 -0800762 static GrGeometryProcessor* Create(GrColor,
763 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700764 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800765 SkScalar strokeWidth,
766 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000767
768 virtual ~DashingLineEffect();
769
joshualitteb2a6762014-12-04 11:35:33 -0800770 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000771
joshualitt2dd1ae02014-12-03 06:24:10 -0800772 const GrAttribute* inPosition() const { return fInPosition; }
773
774 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700775
joshualittb0a8a372014-09-23 09:50:21 -0700776 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000777
778 const SkRect& getRect() const { return fRect; }
779
780 SkScalar getIntervalLength() const { return fIntervalLength; }
781
joshualitteb2a6762014-12-04 11:35:33 -0800782 virtual void getGLProcessorKey(const GrBatchTracker& bt,
783 const GrGLCaps& caps,
784 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000785
joshualitteb2a6762014-12-04 11:35:33 -0800786 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000787
joshualitt9b989322014-12-15 14:16:27 -0800788 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
789
joshualitt290c09b2014-12-19 13:45:20 -0800790 bool onCanMakeEqual(const GrBatchTracker&,
791 const GrGeometryProcessor&,
792 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800793
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000794private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800795 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800796 SkScalar strokeWidth, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000797
bsalomon0e08fc12014-10-15 08:19:04 -0700798 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000799
joshualitt56995b52014-12-11 15:44:02 -0800800 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700801
joshualitt2dd1ae02014-12-03 06:24:10 -0800802 GrPrimitiveEdgeType fEdgeType;
803 const GrAttribute* fInPosition;
804 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000805 SkRect fRect;
806 SkScalar fIntervalLength;
807
joshualittb0a8a372014-09-23 09:50:21 -0700808 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000809
joshualitt249af152014-09-15 11:41:13 -0700810 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000811};
812
813//////////////////////////////////////////////////////////////////////////////
814
joshualitt249af152014-09-15 11:41:13 -0700815class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000816public:
joshualitteb2a6762014-12-04 11:35:33 -0800817 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000818
joshualittc369e7c2014-10-22 10:56:26 -0700819 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000820
joshualitt87f48d92014-12-04 10:41:40 -0800821 static inline void GenKey(const GrGeometryProcessor&,
822 const GrBatchTracker&,
823 const GrGLCaps&,
824 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000825
joshualitt87f48d92014-12-04 10:41:40 -0800826 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800827 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800828 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000829
830private:
joshualitt9b989322014-12-15 14:16:27 -0800831 GrColor fColor;
832 UniformHandle fRectUniform;
833 UniformHandle fIntervalUniform;
834 UniformHandle fColorUniform;
835 SkRect fPrevRect;
836 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700837 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000838};
839
joshualitteb2a6762014-12-04 11:35:33 -0800840GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
841 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800842 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000843 fPrevRect.fLeft = SK_ScalarNaN;
844 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000845}
846
joshualittc369e7c2014-10-22 10:56:26 -0700847void GLDashingLineEffect::emitCode(const EmitArgs& args) {
848 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800849 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
850 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000851 const char *rectName;
852 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
853 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700854 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800855 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000856 "rect",
857 &rectName);
858 const char *intervalName;
859 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700860 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800861 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700862 "interval",
863 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700864
joshualitt2dd1ae02014-12-03 06:24:10 -0800865
866 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
867
joshualitt74077b92014-10-24 11:26:03 -0700868 GrGLVertToFrag v(kVec2f_GrSLType);
869 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800870 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
871
joshualitt9b989322014-12-15 14:16:27 -0800872 // Setup pass through color
873 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
874
joshualitt2dd1ae02014-12-03 06:24:10 -0800875 // setup coord outputs
876 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
877 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700878
joshualittee2af952014-12-30 09:04:15 -0800879 // setup uniform viewMatrix
880 this->addUniformViewMatrix(pb);
881
joshualitt4973d9d2014-11-08 09:24:25 -0800882 // setup position varying
joshualittee2af952014-12-30 09:04:15 -0800883 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), this->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800884 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800885
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000886 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700887 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700888 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700889 v.fsIn(), v.fsIn(), intervalName, intervalName);
890 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700891 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000892 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
893 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700894 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
895 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
896 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
897 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
898 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000899 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
900 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700901 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 +0000902 } else {
903 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700904 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
905 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
906 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 +0000907 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800908 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000909}
910
joshualittb0a8a372014-09-23 09:50:21 -0700911void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800912 const GrPrimitiveProcessor& processor,
913 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800914 this->setUniformViewMatrix(pdman, processor.viewMatrix());
915
joshualittb0a8a372014-09-23 09:50:21 -0700916 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000917 const SkRect& rect = de.getRect();
918 SkScalar intervalLength = de.getIntervalLength();
919 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700920 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
921 rect.fRight - 0.5f, rect.fBottom - 0.5f);
922 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000923 fPrevRect = rect;
924 fPrevIntervalLength = intervalLength;
925 }
joshualitt9b989322014-12-15 14:16:27 -0800926
927 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
928 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
929 GrGLfloat c[4];
930 GrColorToRGBAFloat(local.fColor, c);
931 pdman.set4fv(fColorUniform, 1, c);
932 fColor = local.fColor;
933 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000934}
935
joshualitt87f48d92014-12-04 10:41:40 -0800936void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800937 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800938 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700939 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800940 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700941 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
joshualitt8fc6c2d2014-12-22 15:27:05 -0800942 b->add32(local.fUsesLocalCoords && processor.localMatrix().hasPerspective());
joshualitt9b989322014-12-15 14:16:27 -0800943 b->add32(de.getEdgeType() << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000944}
945
946//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000947
joshualitt2e3b3e32014-12-09 13:31:14 -0800948GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
949 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700950 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800951 SkScalar strokeWidth,
952 const SkMatrix& localMatrix) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000953 if (info.fCount != 2) {
954 return NULL;
955 }
956
joshualittd27f73e2014-12-29 07:43:36 -0800957 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000958}
959
960DashingLineEffect::~DashingLineEffect() {}
961
joshualitt56995b52014-12-11 15:44:02 -0800962void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
963 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000964}
965
joshualitteb2a6762014-12-04 11:35:33 -0800966void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
967 const GrGLCaps& caps,
968 GrProcessorKeyBuilder* b) const {
969 GLDashingLineEffect::GenKey(*this, bt, caps, b);
970}
971
972GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
973 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000974}
975
joshualitt2e3b3e32014-12-09 13:31:14 -0800976DashingLineEffect::DashingLineEffect(GrColor color,
977 GrPrimitiveEdgeType edgeType,
978 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800979 SkScalar strokeWidth,
980 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800981 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800982 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800983 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
984 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000985 SkScalar onLen = info.fIntervals[0];
986 SkScalar offLen = info.fIntervals[1];
987 SkScalar halfOffLen = SkScalarHalf(offLen);
988 SkScalar halfStroke = SkScalarHalf(strokeWidth);
989 fIntervalLength = onLen + offLen;
990 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000991}
992
bsalomon0e08fc12014-10-15 08:19:04 -0700993bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700994 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000995 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000996 fRect == de.fRect &&
997 fIntervalLength == de.fIntervalLength);
998}
999
joshualitt9b989322014-12-15 14:16:27 -08001000void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
1001 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1002 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001003 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001004}
1005
joshualitt290c09b2014-12-19 13:45:20 -08001006bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1007 const GrGeometryProcessor& that,
1008 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001009 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1010 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001011 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1012 that, theirs.fUsesLocalCoords) &&
1013 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001014 theirs.fInputColorType, theirs.fColor);
1015}
1016
joshualittb0a8a372014-09-23 09:50:21 -07001017GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001018
joshualittb0a8a372014-09-23 09:50:21 -07001019GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1020 GrContext*,
1021 const GrDrawTargetCaps& caps,
1022 GrTexture*[]) {
1023 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
1024 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001025 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
1026 DashInfo info;
1027 info.fCount = 2;
1028 SkAutoTArray<SkScalar> intervals(info.fCount);
1029 info.fIntervals = intervals.get();
1030 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
1031 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
1032 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
1033
joshualitt8059eb92014-12-29 15:10:07 -08001034 return DashingLineEffect::Create(GrRandomColor(random),
1035 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001036 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001037}
1038
1039//////////////////////////////////////////////////////////////////////////////
1040
joshualitt2e3b3e32014-12-09 13:31:14 -08001041GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
1042 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -07001043 const SkPathEffect::DashInfo& info,
1044 SkScalar strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001045 GrDashingEffect::DashCap cap,
1046 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001047 switch (cap) {
1048 case GrDashingEffect::kRound_DashCap:
joshualitt8059eb92014-12-29 15:10:07 -08001049 return DashingCircleEffect::Create(color, edgeType, info,
1050 SkScalarHalf(strokeWidth),
joshualittd27f73e2014-12-29 07:43:36 -08001051 localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001052 case GrDashingEffect::kNonRound_DashCap:
joshualittd27f73e2014-12-29 07:43:36 -08001053 return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001054 default:
1055 SkFAIL("Unexpected dashed cap.");
1056 }
1057 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001058}