blob: 34e03011b2aced46670e3747f87f909164a61fa9 [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,
egdaniel8dd688b2015-01-22 10:16:09 -080031 const GrDrawTarget& target, const GrPipelineBuilder& pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -080032 const SkMatrix& viewMatrix) {
egdaniel8dd688b2015-01-22 10:16:09 -080033 if (pipelineBuilder.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
egdaniel8dd688b2015-01-22 10:16:09 -0800167bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
168 GrPipelineBuilder* pipelineBuilder, GrColor color,
169 const SkMatrix& viewMatrix, const SkPoint pts[2],
joshualitt8059eb92014-12-29 15:10:07 -0800170 const GrPaint& paint, const GrStrokeInfo& strokeInfo) {
egdaniel8dd688b2015-01-22 10:16:09 -0800171 if (!can_fast_path_dash(pts, strokeInfo, *target, *pipelineBuilder, viewMatrix)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000172 return false;
173 }
174
egdaniele61c4112014-06-12 10:24:21 -0700175 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000176
egdaniele61c4112014-06-12 10:24:21 -0700177 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000178
egdaniele61c4112014-06-12 10:24:21 -0700179 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000180
181 // the phase should be normalized to be [0, sum of all intervals)
182 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
183
egdaniele61c4112014-06-12 10:24:21 -0700184 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000185
186 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
187 SkMatrix srcRotInv;
188 SkPoint ptsRot[2];
189 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700190 SkMatrix rotMatrix;
191 align_to_x_axis(pts, &rotMatrix, ptsRot);
192 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700193 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000194 return false;
195 }
196 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000197 srcRotInv.reset();
198 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
199 }
200
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000201 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000202
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000203 // Scale corrections of intervals and stroke from view matrix
204 SkScalar parallelScale;
205 SkScalar perpScale;
joshualitt8059eb92014-12-29 15:10:07 -0800206 calc_dash_scaling(&parallelScale, &perpScale, viewMatrix, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207
egdanielf767e792014-07-02 06:21:32 -0700208 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000209
210 // We always want to at least stroke out half a pixel on each side in device space
211 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700212 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
egdaniele61c4112014-06-12 10:24:21 -0700214 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000215 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700216 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000217 } else {
egdaniele61c4112014-06-12 10:24:21 -0700218 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219 }
220
egdaniele61c4112014-06-12 10:24:21 -0700221 SkScalar startAdj = 0;
222
223 SkMatrix combinedMatrix = srcRotInv;
joshualitt8059eb92014-12-29 15:10:07 -0800224 combinedMatrix.postConcat(viewMatrix);
egdaniele61c4112014-06-12 10:24:21 -0700225
226 bool lineDone = false;
227 SkRect startRect;
228 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000229 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
230 // draw it separately here and adjust our start point accordingly
231 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700232 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233 SkPoint startPts[2];
234 startPts[0] = ptsRot[0];
235 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700236 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700239 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000240
egdaniele61c4112014-06-12 10:24:21 -0700241 hasStartRect = true;
242 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 }
244 }
245
246 // adjustments for start and end of bounding rect so we only draw dash intervals
247 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700248 startAdj += calc_start_adjustment(info);
249 if (startAdj != 0) {
250 ptsRot[0].fX += startAdj;
251 srcPhase = 0;
252 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000253 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700254 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
255 ptsRot[1].fX -= endAdj;
256 if (ptsRot[0].fX >= ptsRot[1].fX) {
257 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000258 }
259
egdaniele61c4112014-06-12 10:24:21 -0700260 SkRect endRect;
261 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000262 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
263 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700264 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000265 // If we adjusted the end then we will not be drawing a partial dash at the end.
266 // If we didn't adjust the end point then we just need to make sure the ending
267 // dash isn't a full dash
268 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000269 SkPoint endPts[2];
270 endPts[1] = ptsRot[1];
271 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000272 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700275 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000276
egdaniele61c4112014-06-12 10:24:21 -0700277 hasEndRect = true;
278 endAdj = endingInterval + info.fIntervals[1];
279
280 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000281 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700282 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000283 }
284 }
285 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000286
egdaniele61c4112014-06-12 10:24:21 -0700287 if (startAdj != 0) {
288 srcPhase = 0;
289 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000290
egdaniele61c4112014-06-12 10:24:21 -0700291 // Change the dashing info from src space into device space
292 SkScalar devIntervals[2];
293 devIntervals[0] = info.fIntervals[0] * parallelScale;
294 devIntervals[1] = info.fIntervals[1] * parallelScale;
295 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000296 SkScalar strokeWidth = srcStrokeWidth * perpScale;
297
298 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
299 strokeWidth = 1.f;
300 }
301
egdaniele61c4112014-06-12 10:24:21 -0700302 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000303
304 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
305 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700306 devIntervals[0] += strokeWidth;
307 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308 }
egdaniele61c4112014-06-12 10:24:21 -0700309 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310
egdaniele61c4112014-06-12 10:24:21 -0700311 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
312 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
313
314 SkScalar devBloat = useAA ? 0.5f : 0.f;
315
egdaniele61c4112014-06-12 10:24:21 -0700316 if (devIntervals[1] <= 0.f && useAA) {
317 // Case when we end up drawing a solid AA rect
318 // Reset the start rect to draw this single solid rect
319 // but it requires to upload a new intervals uniform so we can mimic
320 // one giant dash
321 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
322 ptsRot[1].fX += hasEndRect ? endAdj : 0;
323 startRect.set(ptsRot, 2);
324 startRect.outset(strokeAdj, halfSrcStroke);
325 hasStartRect = true;
326 hasEndRect = false;
327 lineDone = true;
328
329 SkPoint devicePts[2];
joshualitt8059eb92014-12-29 15:10:07 -0800330 viewMatrix.mapPoints(devicePts, ptsRot, 2);
egdaniele61c4112014-06-12 10:24:21 -0700331 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
332 if (hasCap) {
333 lineLength += 2.f * halfDevStroke;
334 }
335 devIntervals[0] = lineLength;
336 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800337
joshualittd27f73e2014-12-29 07:43:36 -0800338 // reset to device coordinates
339 SkMatrix invert;
joshualitt8059eb92014-12-29 15:10:07 -0800340 if (!viewMatrix.invert(&invert)) {
joshualittd27f73e2014-12-29 07:43:36 -0800341 SkDebugf("Failed to invert\n");
342 return false;
343 }
344
joshualitt56995b52014-12-11 15:44:02 -0800345 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt5478d422014-11-14 16:00:38 -0800346 bool fullDash = devIntervals[1] > 0.f || useAA;
347 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700348 SkPathEffect::DashInfo devInfo;
349 devInfo.fPhase = devPhase;
350 devInfo.fCount = 2;
351 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700352 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
353 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700354 bool isRoundCap = SkPaint::kRound_Cap == cap;
355 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
356 GrDashingEffect::kNonRound_DashCap;
joshualittd27f73e2014-12-29 07:43:36 -0800357 gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType, invert));
joshualitt249af152014-09-15 11:41:13 -0700358 } else {
359 // Set up the vertex data for the line and start/end dashes
joshualitt8059eb92014-12-29 15:10:07 -0800360 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
361 color,
362 SkMatrix::I(),
joshualittd27f73e2014-12-29 07:43:36 -0800363 invert));
joshualitt249af152014-09-15 11:41:13 -0700364 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000365
egdaniele61c4112014-06-12 10:24:21 -0700366 int totalRectCnt = 0;
367
368 totalRectCnt += !lineDone ? 1 : 0;
369 totalRectCnt += hasStartRect ? 1 : 0;
370 totalRectCnt += hasEndRect ? 1 : 0;
371
joshualitt9853cce2014-11-17 14:22:48 -0800372 GrDrawTarget::AutoReleaseGeometry geo(target,
373 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800374 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700375 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700376 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700377 return false;
378 }
379
egdaniele61c4112014-06-12 10:24:21 -0700380 int curVIdx = 0;
381
egdanielf767e792014-07-02 06:21:32 -0700382 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
383 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
384 startOffset -= halfDevStroke;
385 }
386
egdaniele61c4112014-06-12 10:24:21 -0700387 // Draw interior part of dashed line
388 if (!lineDone) {
389 SkPoint devicePts[2];
joshualitt8059eb92014-12-29 15:10:07 -0800390 viewMatrix.mapPoints(devicePts, ptsRot, 2);
egdaniele61c4112014-06-12 10:24:21 -0700391 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
392 if (hasCap) {
393 lineLength += 2.f * halfDevStroke;
394 }
395
396 SkRect bounds;
397 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
398 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800399 if (fullDash) {
400 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800401 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800402 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
403 lineLength, halfDevStroke);
404 } else {
405 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800406 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800407 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
408 }
egdaniele61c4112014-06-12 10:24:21 -0700409 curVIdx += 4;
410 }
411
412 if (hasStartRect) {
413 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
414 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800415 if (fullDash) {
416 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800417 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800418 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
419 devIntervals[0], halfDevStroke);
420 } else {
421 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800422 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800423 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
424 }
425
egdaniele61c4112014-06-12 10:24:21 -0700426 curVIdx += 4;
427 }
428
429 if (hasEndRect) {
430 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
431 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800432 if (fullDash) {
433 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800434 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800435 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
436 devIntervals[0], halfDevStroke);
437 } else {
438 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800439 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800440 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
441 }
442
egdaniele61c4112014-06-12 10:24:21 -0700443 }
444
445 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
egdaniel8dd688b2015-01-22 10:16:09 -0800446 target->drawIndexedInstances(pipelineBuilder, gp, kTriangles_GrPrimitiveType,
447 totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700448 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000449 return true;
450}
451
452//////////////////////////////////////////////////////////////////////////////
453
egdanielf767e792014-07-02 06:21:32 -0700454class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800455
456struct DashingCircleBatchTracker {
457 GrGPInput fInputColorType;
458 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800459 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800460};
461
egdanielf767e792014-07-02 06:21:32 -0700462/*
463 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
464 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
465 * Both of the previous two parameters are in device space. This effect also requires the setting of
466 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
467 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
468 * transform the line to be horizontal, with the start of line at the origin then shifted to the
469 * right by half the off interval. The line then goes in the positive x direction.
470 */
joshualitt249af152014-09-15 11:41:13 -0700471class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700472public:
473 typedef SkPathEffect::DashInfo DashInfo;
474
joshualitt2e3b3e32014-12-09 13:31:14 -0800475 static GrGeometryProcessor* Create(GrColor,
476 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700477 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800478 SkScalar radius,
479 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700480
481 virtual ~DashingCircleEffect();
482
mtklein72c9faa2015-01-09 10:06:39 -0800483 const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700484
joshualitt71c92602015-01-14 08:12:47 -0800485 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800486
joshualitt71c92602015-01-14 08:12:47 -0800487 const Attribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700488
joshualittb0a8a372014-09-23 09:50:21 -0700489 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700490
491 SkScalar getRadius() const { return fRadius; }
492
493 SkScalar getCenterX() const { return fCenterX; }
494
495 SkScalar getIntervalLength() const { return fIntervalLength; }
496
joshualitteb2a6762014-12-04 11:35:33 -0800497 virtual void getGLProcessorKey(const GrBatchTracker&,
498 const GrGLCaps&,
499 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700500
joshualittabb52a12015-01-13 15:02:10 -0800501 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
502 const GrGLCaps&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700503
joshualittd5a7db42015-01-27 15:39:06 -0800504 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800505
joshualitt290c09b2014-12-19 13:45:20 -0800506 bool onCanMakeEqual(const GrBatchTracker&,
507 const GrGeometryProcessor&,
508 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800509
egdanielf767e792014-07-02 06:21:32 -0700510private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800511 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800512 SkScalar radius, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700513
mtklein72c9faa2015-01-09 10:06:39 -0800514 bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700515
mtklein72c9faa2015-01-09 10:06:39 -0800516 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700517
joshualitt2dd1ae02014-12-03 06:24:10 -0800518 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800519 const Attribute* fInPosition;
520 const Attribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700521 SkScalar fIntervalLength;
522 SkScalar fRadius;
523 SkScalar fCenterX;
524
joshualittb0a8a372014-09-23 09:50:21 -0700525 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700526
joshualitt249af152014-09-15 11:41:13 -0700527 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700528};
529
530//////////////////////////////////////////////////////////////////////////////
531
joshualitt249af152014-09-15 11:41:13 -0700532class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700533public:
joshualitteb2a6762014-12-04 11:35:33 -0800534 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700535
robertphillips46d36f02015-01-18 08:14:14 -0800536 void onEmitCode(EmitArgs&, GrGPArgs*) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700537
joshualitt87f48d92014-12-04 10:41:40 -0800538 static inline void GenKey(const GrGeometryProcessor&,
539 const GrBatchTracker&,
540 const GrGLCaps&,
541 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700542
joshualitt87f48d92014-12-04 10:41:40 -0800543 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800544 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800545 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700546
547private:
joshualitt9b989322014-12-15 14:16:27 -0800548 UniformHandle fParamUniform;
549 UniformHandle fColorUniform;
550 GrColor fColor;
551 SkScalar fPrevRadius;
552 SkScalar fPrevCenterX;
553 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700554 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700555};
556
joshualitteb2a6762014-12-04 11:35:33 -0800557GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
558 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800559 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700560 fPrevRadius = SK_ScalarMin;
561 fPrevCenterX = SK_ScalarMin;
562 fPrevIntervalLength = SK_ScalarMax;
563}
564
robertphillips46d36f02015-01-18 08:14:14 -0800565void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700566 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800567 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
568 GrGLGPBuilder* pb = args.fPB;
egdanielf767e792014-07-02 06:21:32 -0700569 const char *paramName;
570 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
571 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700572 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800573 kVec3f_GrSLType, kDefault_GrSLPrecision,
574 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700575
joshualitt2dd1ae02014-12-03 06:24:10 -0800576 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
577
joshualittabb52a12015-01-13 15:02:10 -0800578 // emit attributes
579 vsBuilder->emitAttributes(dce);
580
joshualitt74077b92014-10-24 11:26:03 -0700581 GrGLVertToFrag v(kVec2f_GrSLType);
582 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800583 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700584
joshualitt9b989322014-12-15 14:16:27 -0800585 // Setup pass through color
586 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
587
joshualittee2af952014-12-30 09:04:15 -0800588 // setup uniform viewMatrix
589 this->addUniformViewMatrix(pb);
590
joshualittabb52a12015-01-13 15:02:10 -0800591 // Setup position
robertphillips46d36f02015-01-18 08:14:14 -0800592 SetupPosition(vsBuilder, gpArgs, dce.inPosition()->fName, dce.viewMatrix(), this->uViewM());
joshualitt4973d9d2014-11-08 09:24:25 -0800593
joshualittabb52a12015-01-13 15:02:10 -0800594 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800595 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800596 args.fTransformsIn, args.fTransformsOut);
597
egdanielf767e792014-07-02 06:21:32 -0700598 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700599 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700600 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700601 v.fsIn(), v.fsIn(), paramName, paramName);
602 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700603 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
604 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700605 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700606 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
607 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
608 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700609 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700610 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
611 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700612 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800613 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700614}
615
joshualitt87f48d92014-12-04 10:41:40 -0800616void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800617 const GrPrimitiveProcessor& processor,
618 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800619 this->setUniformViewMatrix(pdman, processor.viewMatrix());
620
joshualittb0a8a372014-09-23 09:50:21 -0700621 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700622 SkScalar radius = dce.getRadius();
623 SkScalar centerX = dce.getCenterX();
624 SkScalar intervalLength = dce.getIntervalLength();
625 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700626 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700627 fPrevRadius = radius;
628 fPrevCenterX = centerX;
629 fPrevIntervalLength = intervalLength;
630 }
joshualitt9b989322014-12-15 14:16:27 -0800631
632 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
633 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
634 GrGLfloat c[4];
635 GrColorToRGBAFloat(local.fColor, c);
636 pdman.set4fv(fColorUniform, 1, c);
637 fColor = local.fColor;
638 }
egdanielf767e792014-07-02 06:21:32 -0700639}
640
robertphillips46d36f02015-01-18 08:14:14 -0800641void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800642 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800643 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700644 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800645 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800646 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
647 uint32_t key = 0;
648 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
649 key |= ComputePosKey(gp.viewMatrix()) << 1;
650 key |= dce.getEdgeType() << 8;
651 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700652}
653
654//////////////////////////////////////////////////////////////////////////////
655
joshualitt2e3b3e32014-12-09 13:31:14 -0800656GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
657 GrPrimitiveEdgeType edgeType,
658 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800659 SkScalar radius,
660 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -0700661 if (info.fCount != 2 || info.fIntervals[0] != 0) {
662 return NULL;
663 }
664
joshualittd27f73e2014-12-29 07:43:36 -0800665 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700666}
667
668DashingCircleEffect::~DashingCircleEffect() {}
669
joshualitt56995b52014-12-11 15:44:02 -0800670void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
671 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700672}
673
joshualitteb2a6762014-12-04 11:35:33 -0800674void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
675 const GrGLCaps& caps,
676 GrProcessorKeyBuilder* b) const {
677 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
678}
679
joshualittabb52a12015-01-13 15:02:10 -0800680GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
681 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800682 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700683}
684
joshualitt2e3b3e32014-12-09 13:31:14 -0800685DashingCircleEffect::DashingCircleEffect(GrColor color,
686 GrPrimitiveEdgeType edgeType,
687 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800688 SkScalar radius,
689 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800690 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800691 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800692 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
693 fInCoord = &this->addVertexAttrib(Attribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700694 SkScalar onLen = info.fIntervals[0];
695 SkScalar offLen = info.fIntervals[1];
696 fIntervalLength = onLen + offLen;
697 fRadius = radius;
698 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700699}
700
bsalomon0e08fc12014-10-15 08:19:04 -0700701bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700702 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700703 return (fEdgeType == dce.fEdgeType &&
704 fIntervalLength == dce.fIntervalLength &&
705 fRadius == dce.fRadius &&
706 fCenterX == dce.fCenterX);
707}
708
joshualittd5a7db42015-01-27 15:39:06 -0800709void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800710 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
711 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800712 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800713}
714
joshualitt290c09b2014-12-19 13:45:20 -0800715bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
716 const GrGeometryProcessor& that,
717 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800718 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
719 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800720 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
721 that, theirs.fUsesLocalCoords) &&
722 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800723 theirs.fInputColorType, theirs.fColor);
724}
725
joshualittb0a8a372014-09-23 09:50:21 -0700726GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700727
joshualittb0a8a372014-09-23 09:50:21 -0700728GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
729 GrContext*,
730 const GrDrawTargetCaps& caps,
731 GrTexture*[]) {
732 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
733 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700734 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
735 DashInfo info;
736 info.fCount = 2;
737 SkAutoTArray<SkScalar> intervals(info.fCount);
738 info.fIntervals = intervals.get();
739 info.fIntervals[0] = 0;
740 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
741 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
742
joshualitt8059eb92014-12-29 15:10:07 -0800743 return DashingCircleEffect::Create(GrRandomColor(random),
744 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -0800745 GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -0700746}
747
748//////////////////////////////////////////////////////////////////////////////
749
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000750class GLDashingLineEffect;
751
joshualitt9b989322014-12-15 14:16:27 -0800752struct DashingLineBatchTracker {
753 GrGPInput fInputColorType;
754 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800755 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800756};
757
egdanielf767e792014-07-02 06:21:32 -0700758/*
759 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
760 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
761 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
762 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
763 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
764 * line at the origin then shifted to the right by half the off interval. The line then goes in the
765 * positive x direction.
766 */
joshualitt249af152014-09-15 11:41:13 -0700767class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000768public:
769 typedef SkPathEffect::DashInfo DashInfo;
770
joshualitt2e3b3e32014-12-09 13:31:14 -0800771 static GrGeometryProcessor* Create(GrColor,
772 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700773 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800774 SkScalar strokeWidth,
775 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000776
777 virtual ~DashingLineEffect();
778
mtklein72c9faa2015-01-09 10:06:39 -0800779 const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000780
joshualitt71c92602015-01-14 08:12:47 -0800781 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800782
joshualitt71c92602015-01-14 08:12:47 -0800783 const Attribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700784
joshualittb0a8a372014-09-23 09:50:21 -0700785 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000786
787 const SkRect& getRect() const { return fRect; }
788
789 SkScalar getIntervalLength() const { return fIntervalLength; }
790
joshualitteb2a6762014-12-04 11:35:33 -0800791 virtual void getGLProcessorKey(const GrBatchTracker& bt,
792 const GrGLCaps& caps,
793 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000794
joshualittabb52a12015-01-13 15:02:10 -0800795 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
796 const GrGLCaps&) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000797
joshualittd5a7db42015-01-27 15:39:06 -0800798 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800799
joshualitt290c09b2014-12-19 13:45:20 -0800800 bool onCanMakeEqual(const GrBatchTracker&,
801 const GrGeometryProcessor&,
802 const GrBatchTracker&) const SK_OVERRIDE;
joshualitt9b989322014-12-15 14:16:27 -0800803
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000804private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800805 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800806 SkScalar strokeWidth, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000807
mtklein72c9faa2015-01-09 10:06:39 -0800808 bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000809
mtklein72c9faa2015-01-09 10:06:39 -0800810 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700811
joshualitt2dd1ae02014-12-03 06:24:10 -0800812 GrPrimitiveEdgeType fEdgeType;
joshualitt71c92602015-01-14 08:12:47 -0800813 const Attribute* fInPosition;
814 const Attribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000815 SkRect fRect;
816 SkScalar fIntervalLength;
817
joshualittb0a8a372014-09-23 09:50:21 -0700818 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000819
joshualitt249af152014-09-15 11:41:13 -0700820 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000821};
822
823//////////////////////////////////////////////////////////////////////////////
824
joshualitt249af152014-09-15 11:41:13 -0700825class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000826public:
joshualitteb2a6762014-12-04 11:35:33 -0800827 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000828
robertphillips46d36f02015-01-18 08:14:14 -0800829 void onEmitCode(EmitArgs&, GrGPArgs*) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000830
joshualitt87f48d92014-12-04 10:41:40 -0800831 static inline void GenKey(const GrGeometryProcessor&,
832 const GrBatchTracker&,
833 const GrGLCaps&,
834 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000835
joshualitt87f48d92014-12-04 10:41:40 -0800836 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800837 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800838 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000839
840private:
joshualitt9b989322014-12-15 14:16:27 -0800841 GrColor fColor;
842 UniformHandle fRectUniform;
843 UniformHandle fIntervalUniform;
844 UniformHandle fColorUniform;
845 SkRect fPrevRect;
846 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700847 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000848};
849
joshualitteb2a6762014-12-04 11:35:33 -0800850GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
851 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800852 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000853 fPrevRect.fLeft = SK_ScalarNaN;
854 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000855}
856
robertphillips46d36f02015-01-18 08:14:14 -0800857void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700858 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800859 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
860 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000861 const char *rectName;
862 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
863 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700864 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800865 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000866 "rect",
867 &rectName);
868 const char *intervalName;
869 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700870 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800871 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700872 "interval",
873 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700874
joshualitt2dd1ae02014-12-03 06:24:10 -0800875
876 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
877
joshualittabb52a12015-01-13 15:02:10 -0800878 // emit attributes
879 vsBuilder->emitAttributes(de);
880
joshualitt74077b92014-10-24 11:26:03 -0700881 GrGLVertToFrag v(kVec2f_GrSLType);
882 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800883 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
884
joshualitt9b989322014-12-15 14:16:27 -0800885 // Setup pass through color
886 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
887
joshualittee2af952014-12-30 09:04:15 -0800888 // setup uniform viewMatrix
889 this->addUniformViewMatrix(pb);
890
joshualittabb52a12015-01-13 15:02:10 -0800891 // Setup position
robertphillips46d36f02015-01-18 08:14:14 -0800892 SetupPosition(vsBuilder, gpArgs, de.inPosition()->fName, de.viewMatrix(), this->uViewM());
joshualitt4973d9d2014-11-08 09:24:25 -0800893
joshualittabb52a12015-01-13 15:02:10 -0800894 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800895 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800896 args.fTransformsIn, args.fTransformsOut);
897
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000898 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700899 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700900 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700901 v.fsIn(), v.fsIn(), intervalName, intervalName);
902 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700903 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000904 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
905 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700906 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
907 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
908 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
909 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
910 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000911 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
912 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700913 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 +0000914 } else {
915 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700916 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
917 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
918 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 +0000919 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800920 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000921}
922
joshualittb0a8a372014-09-23 09:50:21 -0700923void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800924 const GrPrimitiveProcessor& processor,
925 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800926 this->setUniformViewMatrix(pdman, processor.viewMatrix());
927
joshualittb0a8a372014-09-23 09:50:21 -0700928 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000929 const SkRect& rect = de.getRect();
930 SkScalar intervalLength = de.getIntervalLength();
931 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700932 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
933 rect.fRight - 0.5f, rect.fBottom - 0.5f);
934 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000935 fPrevRect = rect;
936 fPrevIntervalLength = intervalLength;
937 }
joshualitt9b989322014-12-15 14:16:27 -0800938
939 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
940 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
941 GrGLfloat c[4];
942 GrColorToRGBAFloat(local.fColor, c);
943 pdman.set4fv(fColorUniform, 1, c);
944 fColor = local.fColor;
945 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000946}
947
robertphillips46d36f02015-01-18 08:14:14 -0800948void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800949 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800950 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700951 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800952 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800953 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
954 uint32_t key = 0;
955 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
956 key |= ComputePosKey(gp.viewMatrix()) << 1;
957 key |= de.getEdgeType() << 8;
958 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000959}
960
961//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000962
joshualitt2e3b3e32014-12-09 13:31:14 -0800963GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
964 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700965 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800966 SkScalar strokeWidth,
967 const SkMatrix& localMatrix) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000968 if (info.fCount != 2) {
969 return NULL;
970 }
971
joshualittd27f73e2014-12-29 07:43:36 -0800972 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000973}
974
975DashingLineEffect::~DashingLineEffect() {}
976
joshualitt56995b52014-12-11 15:44:02 -0800977void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
978 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000979}
980
joshualitteb2a6762014-12-04 11:35:33 -0800981void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
982 const GrGLCaps& caps,
983 GrProcessorKeyBuilder* b) const {
984 GLDashingLineEffect::GenKey(*this, bt, caps, b);
985}
986
joshualittabb52a12015-01-13 15:02:10 -0800987GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
988 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800989 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000990}
991
joshualitt2e3b3e32014-12-09 13:31:14 -0800992DashingLineEffect::DashingLineEffect(GrColor color,
993 GrPrimitiveEdgeType edgeType,
994 const DashInfo& info,
joshualittd27f73e2014-12-29 07:43:36 -0800995 SkScalar strokeWidth,
996 const SkMatrix& localMatrix)
joshualitt8059eb92014-12-29 15:10:07 -0800997 : INHERITED(color, SkMatrix::I(), localMatrix), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800998 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800999 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
1000 fInCoord = &this->addVertexAttrib(Attribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001001 SkScalar onLen = info.fIntervals[0];
1002 SkScalar offLen = info.fIntervals[1];
1003 SkScalar halfOffLen = SkScalarHalf(offLen);
1004 SkScalar halfStroke = SkScalarHalf(strokeWidth);
1005 fIntervalLength = onLen + offLen;
1006 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001007}
1008
bsalomon0e08fc12014-10-15 08:19:04 -07001009bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -07001010 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001011 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001012 fRect == de.fRect &&
1013 fIntervalLength == de.fIntervalLength);
1014}
1015
joshualittd5a7db42015-01-27 15:39:06 -08001016void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001017 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1018 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001019 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001020}
1021
joshualitt290c09b2014-12-19 13:45:20 -08001022bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1023 const GrGeometryProcessor& that,
1024 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001025 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1026 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001027 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1028 that, theirs.fUsesLocalCoords) &&
1029 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001030 theirs.fInputColorType, theirs.fColor);
1031}
1032
joshualittb0a8a372014-09-23 09:50:21 -07001033GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001034
joshualittb0a8a372014-09-23 09:50:21 -07001035GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1036 GrContext*,
1037 const GrDrawTargetCaps& caps,
1038 GrTexture*[]) {
1039 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
1040 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001041 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
1042 DashInfo info;
1043 info.fCount = 2;
1044 SkAutoTArray<SkScalar> intervals(info.fCount);
1045 info.fIntervals = intervals.get();
1046 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
1047 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
1048 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
1049
joshualitt8059eb92014-12-29 15:10:07 -08001050 return DashingLineEffect::Create(GrRandomColor(random),
1051 edgeType, info, strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001052 GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001053}
1054
1055//////////////////////////////////////////////////////////////////////////////
1056
joshualitt2e3b3e32014-12-09 13:31:14 -08001057GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
1058 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -07001059 const SkPathEffect::DashInfo& info,
1060 SkScalar strokeWidth,
joshualittd27f73e2014-12-29 07:43:36 -08001061 GrDashingEffect::DashCap cap,
1062 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001063 switch (cap) {
1064 case GrDashingEffect::kRound_DashCap:
joshualitt8059eb92014-12-29 15:10:07 -08001065 return DashingCircleEffect::Create(color, edgeType, info,
1066 SkScalarHalf(strokeWidth),
joshualittd27f73e2014-12-29 07:43:36 -08001067 localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001068 case GrDashingEffect::kNonRound_DashCap:
joshualittd27f73e2014-12-29 07:43:36 -08001069 return DashingLineEffect::Create(color, edgeType, info, strokeWidth, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001070 default:
1071 SkFAIL("Unexpected dashed cap.");
1072 }
1073 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001074}