blob: 69e40eb3154d68e8aeb674ad98eeade49cc257db [file] [log] [blame]
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrDashingEffect.h"
9
egdaniele61c4112014-06-12 10:24:21 -070010#include "../GrAARectRenderer.h"
11
joshualittb0a8a372014-09-23 09:50:21 -070012#include "GrGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000013#include "GrContext.h"
14#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080015#include "GrDefaultGeoProcFactory.h"
egdaniele61c4112014-06-12 10:24:21 -070016#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000017#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080018#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070019#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070020#include "GrStrokeInfo.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000021#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080022#include "gl/GrGLGeometryProcessor.h"
23#include "gl/GrGLProcessor.h"
24#include "gl/GrGLSL.h"
25#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000026
27///////////////////////////////////////////////////////////////////////////////
28
egdaniele61c4112014-06-12 10:24:21 -070029// Returns whether or not the gpu can fast path the dash line effect.
30static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
joshualitt9853cce2014-11-17 14:22:48 -080031 const GrDrawTarget& target, const GrDrawState& ds,
32 const SkMatrix& viewMatrix) {
33 if (ds.getRenderTarget()->isMultisampled()) {
egdaniele61c4112014-06-12 10:24:21 -070034 return false;
35 }
36
37 // Pts must be either horizontal or vertical in src space
38 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
39 return false;
40 }
41
42 // May be able to relax this to include skew. As of now cannot do perspective
43 // because of the non uniform scaling of bloating a rect
44 if (!viewMatrix.preservesRightAngles()) {
45 return false;
46 }
47
48 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
49 return false;
50 }
51
52 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
53 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
54 return false;
55 }
56
57 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
58 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070059 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070060 return false;
61 }
62
63 return true;
64}
65
66namespace {
egdaniele61c4112014-06-12 10:24:21 -070067struct DashLineVertex {
68 SkPoint fPos;
69 SkPoint fDashPos;
70};
egdaniele61c4112014-06-12 10:24:21 -070071};
72
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000073static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
74 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
75 SkVector vecSrc = pts[1] - pts[0];
76 SkScalar magSrc = vecSrc.length();
77 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
78 vecSrc.scale(invSrc);
79
80 SkVector vecSrcPerp;
81 vecSrc.rotateCW(&vecSrcPerp);
82 viewMatrix.mapVectors(&vecSrc, 1);
83 viewMatrix.mapVectors(&vecSrcPerp, 1);
84
85 // parallelScale tells how much to scale along the line parallel to the dash line
86 // perpScale tells how much to scale in the direction perpendicular to the dash line
87 *parallelScale = vecSrc.length();
88 *perpScale = vecSrcPerp.length();
89}
90
91// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
92// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
93static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
94 SkVector vec = pts[1] - pts[0];
95 SkScalar mag = vec.length();
96 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
97
98 vec.scale(inv);
99 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
100 if (ptsRot) {
101 rotMatrix->mapPoints(ptsRot, pts, 2);
102 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
103 ptsRot[1].fY = pts[0].fY;
104 }
105}
106
107// Assumes phase < sum of all intervals
108static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) {
109 SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
110 if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) {
111 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
112 return srcIntervalLen - info.fPhase;
113 }
114 return 0;
115}
116
egdaniele61c4112014-06-12 10:24:21 -0700117static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
118 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000119 if (pts[1].fX <= pts[0].fX) {
120 return 0;
121 }
122 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
123 SkScalar totalLen = pts[1].fX - pts[0].fX;
124 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
125 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700126 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000127 temp = SkScalarDiv(*endingInt, srcIntervalLen);
128 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
129 if (0 == *endingInt) {
130 *endingInt = srcIntervalLen;
131 }
132 if (*endingInt > info.fIntervals[0]) {
133 if (0 == info.fIntervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000134 *endingInt -= 0.01f; // make sure we capture the last zero size pnt (used if has caps)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000135 }
136 return *endingInt - info.fIntervals[0];
137 }
138 return 0;
139}
140
egdaniele61c4112014-06-12 10:24:21 -0700141static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
142 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
egdaniele61c4112014-06-12 10:24:21 -0700143 SkScalar startDashX = offset - bloat;
144 SkScalar endDashX = offset + len + bloat;
145 SkScalar startDashY = -stroke - bloat;
146 SkScalar endDashY = stroke + bloat;
147 verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
148 verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
149 verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
150 verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
egdaniele61c4112014-06-12 10:24:21 -0700151 verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
152 verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
153 verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
154 verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
egdaniele61c4112014-06-12 10:24:21 -0700155 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
156}
157
joshualitt5478d422014-11-14 16:00:38 -0800158static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
159 SkPoint* verts) {
160 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
161 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
162 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
163 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
164 matrix.mapPoints(&verts[idx], 4);
165}
egdaniele61c4112014-06-12 10:24:21 -0700166
joshualitt9853cce2014-11-17 14:22:48 -0800167bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target, GrDrawState* drawState,
joshualitt2e3b3e32014-12-09 13:31:14 -0800168 GrColor color, const SkPoint pts[2], const GrPaint& paint,
joshualitt9853cce2014-11-17 14:22:48 -0800169 const GrStrokeInfo& strokeInfo, const SkMatrix& vm) {
egdaniele61c4112014-06-12 10:24:21 -0700170
joshualitt9853cce2014-11-17 14:22:48 -0800171 if (!can_fast_path_dash(pts, strokeInfo, *target, *drawState, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000172 return false;
173 }
174
egdaniele61c4112014-06-12 10:24:21 -0700175 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000176
egdaniele61c4112014-06-12 10:24:21 -0700177 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000178
egdaniele61c4112014-06-12 10:24:21 -0700179 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000180
181 // the phase should be normalized to be [0, sum of all intervals)
182 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
183
egdaniele61c4112014-06-12 10:24:21 -0700184 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000185
186 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
187 SkMatrix srcRotInv;
188 SkPoint ptsRot[2];
189 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700190 SkMatrix rotMatrix;
191 align_to_x_axis(pts, &rotMatrix, ptsRot);
192 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700193 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000194 return false;
195 }
196 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000197 srcRotInv.reset();
198 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
199 }
200
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000201 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000202
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000203 // Scale corrections of intervals and stroke from view matrix
204 SkScalar parallelScale;
205 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700206 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207
egdanielf767e792014-07-02 06:21:32 -0700208 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000209
210 // We always want to at least stroke out half a pixel on each side in device space
211 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700212 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
egdaniele61c4112014-06-12 10:24:21 -0700214 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000215 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700216 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000217 } else {
egdaniele61c4112014-06-12 10:24:21 -0700218 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219 }
220
egdaniele61c4112014-06-12 10:24:21 -0700221 SkScalar startAdj = 0;
222
223 SkMatrix combinedMatrix = srcRotInv;
224 combinedMatrix.postConcat(vm);
225
226 bool lineDone = false;
227 SkRect startRect;
228 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000229 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
230 // draw it separately here and adjust our start point accordingly
231 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700232 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233 SkPoint startPts[2];
234 startPts[0] = ptsRot[0];
235 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700236 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700239 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000240
egdaniele61c4112014-06-12 10:24:21 -0700241 hasStartRect = true;
242 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 }
244 }
245
246 // adjustments for start and end of bounding rect so we only draw dash intervals
247 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700248 startAdj += calc_start_adjustment(info);
249 if (startAdj != 0) {
250 ptsRot[0].fX += startAdj;
251 srcPhase = 0;
252 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000253 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700254 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
255 ptsRot[1].fX -= endAdj;
256 if (ptsRot[0].fX >= ptsRot[1].fX) {
257 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000258 }
259
egdaniele61c4112014-06-12 10:24:21 -0700260 SkRect endRect;
261 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000262 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
263 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700264 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000265 // If we adjusted the end then we will not be drawing a partial dash at the end.
266 // If we didn't adjust the end point then we just need to make sure the ending
267 // dash isn't a full dash
268 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000269 SkPoint endPts[2];
270 endPts[1] = ptsRot[1];
271 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000272 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700275 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000276
egdaniele61c4112014-06-12 10:24:21 -0700277 hasEndRect = true;
278 endAdj = endingInterval + info.fIntervals[1];
279
280 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000281 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700282 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000283 }
284 }
285 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000286
egdaniele61c4112014-06-12 10:24:21 -0700287 if (startAdj != 0) {
288 srcPhase = 0;
289 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000290
egdaniele61c4112014-06-12 10:24:21 -0700291 // Change the dashing info from src space into device space
292 SkScalar devIntervals[2];
293 devIntervals[0] = info.fIntervals[0] * parallelScale;
294 devIntervals[1] = info.fIntervals[1] * parallelScale;
295 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000296 SkScalar strokeWidth = srcStrokeWidth * perpScale;
297
298 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
299 strokeWidth = 1.f;
300 }
301
egdaniele61c4112014-06-12 10:24:21 -0700302 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000303
304 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
305 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700306 devIntervals[0] += strokeWidth;
307 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308 }
egdaniele61c4112014-06-12 10:24:21 -0700309 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310
egdaniele61c4112014-06-12 10:24:21 -0700311 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
312 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
313
314 SkScalar devBloat = useAA ? 0.5f : 0.f;
315
egdaniele61c4112014-06-12 10:24:21 -0700316 if (devIntervals[1] <= 0.f && useAA) {
317 // Case when we end up drawing a solid AA rect
318 // Reset the start rect to draw this single solid rect
319 // but it requires to upload a new intervals uniform so we can mimic
320 // one giant dash
321 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
322 ptsRot[1].fX += hasEndRect ? endAdj : 0;
323 startRect.set(ptsRot, 2);
324 startRect.outset(strokeAdj, halfSrcStroke);
325 hasStartRect = true;
326 hasEndRect = false;
327 lineDone = true;
328
329 SkPoint devicePts[2];
330 vm.mapPoints(devicePts, ptsRot, 2);
331 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
332 if (hasCap) {
333 lineLength += 2.f * halfDevStroke;
334 }
335 devIntervals[0] = lineLength;
336 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800337
joshualitt56995b52014-12-11 15:44:02 -0800338 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt5478d422014-11-14 16:00:38 -0800339 bool fullDash = devIntervals[1] > 0.f || useAA;
340 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700341 SkPathEffect::DashInfo devInfo;
342 devInfo.fPhase = devPhase;
343 devInfo.fCount = 2;
344 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700345 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
346 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700347 bool isRoundCap = SkPaint::kRound_Cap == cap;
348 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
349 GrDashingEffect::kNonRound_DashCap;
joshualitt56995b52014-12-11 15:44:02 -0800350 gp.reset(GrDashingEffect::Create(color, edgeType, devInfo, strokeWidth, capType));
joshualitt249af152014-09-15 11:41:13 -0700351 } else {
352 // Set up the vertex data for the line and start/end dashes
joshualitt56995b52014-12-11 15:44:02 -0800353 gp.reset(GrDefaultGeoProcFactory::Create(color, GrDefaultGeoProcFactory::kPosition_GPType));
joshualitt249af152014-09-15 11:41:13 -0700354 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355
egdaniele61c4112014-06-12 10:24:21 -0700356 int totalRectCnt = 0;
357
358 totalRectCnt += !lineDone ? 1 : 0;
359 totalRectCnt += hasStartRect ? 1 : 0;
360 totalRectCnt += hasEndRect ? 1 : 0;
361
joshualitt9853cce2014-11-17 14:22:48 -0800362 GrDrawTarget::AutoReleaseGeometry geo(target,
363 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800364 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700365 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700366 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700367 return false;
368 }
369
egdaniele61c4112014-06-12 10:24:21 -0700370 int curVIdx = 0;
371
egdanielf767e792014-07-02 06:21:32 -0700372 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
373 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
374 startOffset -= halfDevStroke;
375 }
376
egdaniele61c4112014-06-12 10:24:21 -0700377 // Draw interior part of dashed line
378 if (!lineDone) {
379 SkPoint devicePts[2];
380 vm.mapPoints(devicePts, ptsRot, 2);
381 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
382 if (hasCap) {
383 lineLength += 2.f * halfDevStroke;
384 }
385
386 SkRect bounds;
387 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
388 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800389 if (fullDash) {
390 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800391 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800392 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
393 lineLength, halfDevStroke);
394 } else {
395 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800396 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800397 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
398 }
egdaniele61c4112014-06-12 10:24:21 -0700399 curVIdx += 4;
400 }
401
402 if (hasStartRect) {
403 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
404 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800405 if (fullDash) {
406 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800407 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800408 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
409 devIntervals[0], halfDevStroke);
410 } else {
411 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800412 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800413 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
414 }
415
egdaniele61c4112014-06-12 10:24:21 -0700416 curVIdx += 4;
417 }
418
419 if (hasEndRect) {
420 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
421 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800422 if (fullDash) {
423 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800424 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800425 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
426 devIntervals[0], halfDevStroke);
427 } else {
428 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800429 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800430 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
431 }
432
egdaniele61c4112014-06-12 10:24:21 -0700433 }
434
435 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
joshualitt56995b52014-12-11 15:44:02 -0800436 target->drawIndexedInstances(drawState, gp, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700437 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000438 return true;
439}
440
441//////////////////////////////////////////////////////////////////////////////
442
egdanielf767e792014-07-02 06:21:32 -0700443class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800444
445struct DashingCircleBatchTracker {
446 GrGPInput fInputColorType;
447 GrColor fColor;
448};
449
egdanielf767e792014-07-02 06:21:32 -0700450/*
451 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
452 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
453 * Both of the previous two parameters are in device space. This effect also requires the setting of
454 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
455 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
456 * transform the line to be horizontal, with the start of line at the origin then shifted to the
457 * right by half the off interval. The line then goes in the positive x direction.
458 */
joshualitt249af152014-09-15 11:41:13 -0700459class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700460public:
461 typedef SkPathEffect::DashInfo DashInfo;
462
joshualitt2e3b3e32014-12-09 13:31:14 -0800463 static GrGeometryProcessor* Create(GrColor,
464 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700465 const DashInfo& info,
466 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700467
468 virtual ~DashingCircleEffect();
469
joshualitteb2a6762014-12-04 11:35:33 -0800470 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700471
joshualitt2dd1ae02014-12-03 06:24:10 -0800472 const GrAttribute* inPosition() const { return fInPosition; }
473
474 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700475
joshualittb0a8a372014-09-23 09:50:21 -0700476 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700477
478 SkScalar getRadius() const { return fRadius; }
479
480 SkScalar getCenterX() const { return fCenterX; }
481
482 SkScalar getIntervalLength() const { return fIntervalLength; }
483
joshualitteb2a6762014-12-04 11:35:33 -0800484 virtual void getGLProcessorKey(const GrBatchTracker&,
485 const GrGLCaps&,
486 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700487
joshualitteb2a6762014-12-04 11:35:33 -0800488 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700489
joshualitt9b989322014-12-15 14:16:27 -0800490 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
491
492 bool onCanMakeEqual(const GrBatchTracker&, const GrBatchTracker&) const SK_OVERRIDE;
493
egdanielf767e792014-07-02 06:21:32 -0700494private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800495 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
496 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700497
bsalomon0e08fc12014-10-15 08:19:04 -0700498 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700499
joshualitt56995b52014-12-11 15:44:02 -0800500 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700501
joshualitt2dd1ae02014-12-03 06:24:10 -0800502 GrPrimitiveEdgeType fEdgeType;
503 const GrAttribute* fInPosition;
504 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700505 SkScalar fIntervalLength;
506 SkScalar fRadius;
507 SkScalar fCenterX;
508
joshualittb0a8a372014-09-23 09:50:21 -0700509 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700510
joshualitt249af152014-09-15 11:41:13 -0700511 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700512};
513
514//////////////////////////////////////////////////////////////////////////////
515
joshualitt249af152014-09-15 11:41:13 -0700516class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700517public:
joshualitteb2a6762014-12-04 11:35:33 -0800518 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700519
joshualittc369e7c2014-10-22 10:56:26 -0700520 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700521
joshualitt87f48d92014-12-04 10:41:40 -0800522 static inline void GenKey(const GrGeometryProcessor&,
523 const GrBatchTracker&,
524 const GrGLCaps&,
525 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700526
joshualitt87f48d92014-12-04 10:41:40 -0800527 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800528 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800529 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700530
531private:
joshualitt9b989322014-12-15 14:16:27 -0800532 UniformHandle fParamUniform;
533 UniformHandle fColorUniform;
534 GrColor fColor;
535 SkScalar fPrevRadius;
536 SkScalar fPrevCenterX;
537 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700538 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700539};
540
joshualitteb2a6762014-12-04 11:35:33 -0800541GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
542 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800543 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700544 fPrevRadius = SK_ScalarMin;
545 fPrevCenterX = SK_ScalarMin;
546 fPrevIntervalLength = SK_ScalarMax;
547}
548
joshualittc369e7c2014-10-22 10:56:26 -0700549void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
550 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800551 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
552 GrGLGPBuilder* pb = args.fPB;
egdanielf767e792014-07-02 06:21:32 -0700553 const char *paramName;
554 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
555 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700556 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800557 kVec3f_GrSLType, kDefault_GrSLPrecision,
558 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700559
joshualitt2dd1ae02014-12-03 06:24:10 -0800560 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
561
joshualitt74077b92014-10-24 11:26:03 -0700562 GrGLVertToFrag v(kVec2f_GrSLType);
563 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800564 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700565
joshualitt9b989322014-12-15 14:16:27 -0800566 // Setup pass through color
567 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
568
joshualitt2dd1ae02014-12-03 06:24:10 -0800569 // setup coord outputs
570 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
571 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700572
joshualitt4973d9d2014-11-08 09:24:25 -0800573 // setup position varying
574 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800575 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800576
egdanielf767e792014-07-02 06:21:32 -0700577 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700578 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700579 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700580 v.fsIn(), v.fsIn(), paramName, paramName);
581 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700582 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
583 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700584 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700585 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
586 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
587 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700588 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700589 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
590 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700591 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800592 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700593}
594
joshualitt87f48d92014-12-04 10:41:40 -0800595void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800596 const GrPrimitiveProcessor& processor,
597 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700598 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700599 SkScalar radius = dce.getRadius();
600 SkScalar centerX = dce.getCenterX();
601 SkScalar intervalLength = dce.getIntervalLength();
602 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700603 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700604 fPrevRadius = radius;
605 fPrevCenterX = centerX;
606 fPrevIntervalLength = intervalLength;
607 }
joshualitt9b989322014-12-15 14:16:27 -0800608
609 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
610 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
611 GrGLfloat c[4];
612 GrColorToRGBAFloat(local.fColor, c);
613 pdman.set4fv(fColorUniform, 1, c);
614 fColor = local.fColor;
615 }
egdanielf767e792014-07-02 06:21:32 -0700616}
617
joshualitt87f48d92014-12-04 10:41:40 -0800618void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800619 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800620 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700621 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800622 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700623 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800624 b->add32(dce.getEdgeType() << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700625}
626
627//////////////////////////////////////////////////////////////////////////////
628
joshualitt2e3b3e32014-12-09 13:31:14 -0800629GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
630 GrPrimitiveEdgeType edgeType,
631 const DashInfo& info,
joshualittb0a8a372014-09-23 09:50:21 -0700632 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700633 if (info.fCount != 2 || info.fIntervals[0] != 0) {
634 return NULL;
635 }
636
joshualitt2e3b3e32014-12-09 13:31:14 -0800637 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700638}
639
640DashingCircleEffect::~DashingCircleEffect() {}
641
joshualitt56995b52014-12-11 15:44:02 -0800642void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
643 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700644}
645
joshualitteb2a6762014-12-04 11:35:33 -0800646void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
647 const GrGLCaps& caps,
648 GrProcessorKeyBuilder* b) const {
649 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
650}
651
652GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
653 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700654}
655
joshualitt2e3b3e32014-12-09 13:31:14 -0800656DashingCircleEffect::DashingCircleEffect(GrColor color,
657 GrPrimitiveEdgeType edgeType,
658 const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700659 SkScalar radius)
joshualitt2e3b3e32014-12-09 13:31:14 -0800660 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800661 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800662 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
663 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700664 SkScalar onLen = info.fIntervals[0];
665 SkScalar offLen = info.fIntervals[1];
666 fIntervalLength = onLen + offLen;
667 fRadius = radius;
668 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700669}
670
bsalomon0e08fc12014-10-15 08:19:04 -0700671bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700672 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700673 return (fEdgeType == dce.fEdgeType &&
674 fIntervalLength == dce.fIntervalLength &&
675 fRadius == dce.fRadius &&
676 fCenterX == dce.fCenterX);
677}
678
joshualitt9b989322014-12-15 14:16:27 -0800679void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
680 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
681 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
682}
683
684bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m, const GrBatchTracker& t) const {
685 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
686 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
687 return CanCombineOutput(mine.fInputColorType, mine.fColor,
688 theirs.fInputColorType, theirs.fColor);
689}
690
joshualittb0a8a372014-09-23 09:50:21 -0700691GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700692
joshualittb0a8a372014-09-23 09:50:21 -0700693GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
694 GrContext*,
695 const GrDrawTargetCaps& caps,
696 GrTexture*[]) {
697 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
698 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700699 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
700 DashInfo info;
701 info.fCount = 2;
702 SkAutoTArray<SkScalar> intervals(info.fCount);
703 info.fIntervals = intervals.get();
704 info.fIntervals[0] = 0;
705 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
706 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
707
joshualitt2e3b3e32014-12-09 13:31:14 -0800708 return DashingCircleEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700709}
710
711//////////////////////////////////////////////////////////////////////////////
712
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000713class GLDashingLineEffect;
714
joshualitt9b989322014-12-15 14:16:27 -0800715struct DashingLineBatchTracker {
716 GrGPInput fInputColorType;
717 GrColor fColor;
718};
719
egdanielf767e792014-07-02 06:21:32 -0700720/*
721 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
722 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
723 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
724 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
725 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
726 * line at the origin then shifted to the right by half the off interval. The line then goes in the
727 * positive x direction.
728 */
joshualitt249af152014-09-15 11:41:13 -0700729class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000730public:
731 typedef SkPathEffect::DashInfo DashInfo;
732
joshualitt2e3b3e32014-12-09 13:31:14 -0800733 static GrGeometryProcessor* Create(GrColor,
734 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700735 const DashInfo& info,
736 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000737
738 virtual ~DashingLineEffect();
739
joshualitteb2a6762014-12-04 11:35:33 -0800740 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000741
joshualitt2dd1ae02014-12-03 06:24:10 -0800742 const GrAttribute* inPosition() const { return fInPosition; }
743
744 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700745
joshualittb0a8a372014-09-23 09:50:21 -0700746 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000747
748 const SkRect& getRect() const { return fRect; }
749
750 SkScalar getIntervalLength() const { return fIntervalLength; }
751
joshualitteb2a6762014-12-04 11:35:33 -0800752 virtual void getGLProcessorKey(const GrBatchTracker& bt,
753 const GrGLCaps& caps,
754 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000755
joshualitteb2a6762014-12-04 11:35:33 -0800756 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000757
joshualitt9b989322014-12-15 14:16:27 -0800758 void initBatchTracker(GrBatchTracker* bt, const InitBT& init) const SK_OVERRIDE;
759
760 bool onCanMakeEqual(const GrBatchTracker&, const GrBatchTracker&) const SK_OVERRIDE;
761
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800763 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
764 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000765
bsalomon0e08fc12014-10-15 08:19:04 -0700766 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000767
joshualitt56995b52014-12-11 15:44:02 -0800768 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700769
joshualitt2dd1ae02014-12-03 06:24:10 -0800770 GrPrimitiveEdgeType fEdgeType;
771 const GrAttribute* fInPosition;
772 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000773 SkRect fRect;
774 SkScalar fIntervalLength;
775
joshualittb0a8a372014-09-23 09:50:21 -0700776 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000777
joshualitt249af152014-09-15 11:41:13 -0700778 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000779};
780
781//////////////////////////////////////////////////////////////////////////////
782
joshualitt249af152014-09-15 11:41:13 -0700783class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000784public:
joshualitteb2a6762014-12-04 11:35:33 -0800785 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000786
joshualittc369e7c2014-10-22 10:56:26 -0700787 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000788
joshualitt87f48d92014-12-04 10:41:40 -0800789 static inline void GenKey(const GrGeometryProcessor&,
790 const GrBatchTracker&,
791 const GrGLCaps&,
792 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000793
joshualitt87f48d92014-12-04 10:41:40 -0800794 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800795 const GrPrimitiveProcessor&,
joshualitt87f48d92014-12-04 10:41:40 -0800796 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000797
798private:
joshualitt9b989322014-12-15 14:16:27 -0800799 GrColor fColor;
800 UniformHandle fRectUniform;
801 UniformHandle fIntervalUniform;
802 UniformHandle fColorUniform;
803 SkRect fPrevRect;
804 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700805 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000806};
807
joshualitteb2a6762014-12-04 11:35:33 -0800808GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
809 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800810 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000811 fPrevRect.fLeft = SK_ScalarNaN;
812 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000813}
814
joshualittc369e7c2014-10-22 10:56:26 -0700815void GLDashingLineEffect::emitCode(const EmitArgs& args) {
816 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800817 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
818 GrGLGPBuilder* pb = args.fPB;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000819 const char *rectName;
820 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
821 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700822 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800823 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000824 "rect",
825 &rectName);
826 const char *intervalName;
827 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700828 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800829 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700830 "interval",
831 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700832
joshualitt2dd1ae02014-12-03 06:24:10 -0800833
834 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
835
joshualitt74077b92014-10-24 11:26:03 -0700836 GrGLVertToFrag v(kVec2f_GrSLType);
837 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800838 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
839
joshualitt9b989322014-12-15 14:16:27 -0800840 // Setup pass through color
841 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
842
joshualitt2dd1ae02014-12-03 06:24:10 -0800843 // setup coord outputs
844 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
845 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700846
joshualitt4973d9d2014-11-08 09:24:25 -0800847 // setup position varying
848 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800849 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800850
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000851 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700852 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700853 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700854 v.fsIn(), v.fsIn(), intervalName, intervalName);
855 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700856 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000857 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
858 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700859 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
860 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
861 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
862 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
863 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000864 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
865 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700866 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 +0000867 } else {
868 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700869 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
870 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
871 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 +0000872 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800873 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000874}
875
joshualittb0a8a372014-09-23 09:50:21 -0700876void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800877 const GrPrimitiveProcessor& processor,
878 const GrBatchTracker& bt) {
joshualittb0a8a372014-09-23 09:50:21 -0700879 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000880 const SkRect& rect = de.getRect();
881 SkScalar intervalLength = de.getIntervalLength();
882 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700883 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
884 rect.fRight - 0.5f, rect.fBottom - 0.5f);
885 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000886 fPrevRect = rect;
887 fPrevIntervalLength = intervalLength;
888 }
joshualitt9b989322014-12-15 14:16:27 -0800889
890 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
891 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
892 GrGLfloat c[4];
893 GrColorToRGBAFloat(local.fColor, c);
894 pdman.set4fv(fColorUniform, 1, c);
895 fColor = local.fColor;
896 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000897}
898
joshualitt87f48d92014-12-04 10:41:40 -0800899void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
joshualitt9b989322014-12-15 14:16:27 -0800900 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800901 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700902 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800903 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
joshualittb0a8a372014-09-23 09:50:21 -0700904 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800905 b->add32(de.getEdgeType() << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000906}
907
908//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000909
joshualitt2e3b3e32014-12-09 13:31:14 -0800910GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
911 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700912 const DashInfo& info,
913 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000914 if (info.fCount != 2) {
915 return NULL;
916 }
917
joshualitt2e3b3e32014-12-09 13:31:14 -0800918 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000919}
920
921DashingLineEffect::~DashingLineEffect() {}
922
joshualitt56995b52014-12-11 15:44:02 -0800923void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
924 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000925}
926
joshualitteb2a6762014-12-04 11:35:33 -0800927void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
928 const GrGLCaps& caps,
929 GrProcessorKeyBuilder* b) const {
930 GLDashingLineEffect::GenKey(*this, bt, caps, b);
931}
932
933GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
934 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000935}
936
joshualitt2e3b3e32014-12-09 13:31:14 -0800937DashingLineEffect::DashingLineEffect(GrColor color,
938 GrPrimitiveEdgeType edgeType,
939 const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700940 SkScalar strokeWidth)
joshualitt2e3b3e32014-12-09 13:31:14 -0800941 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800942 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800943 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
944 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000945 SkScalar onLen = info.fIntervals[0];
946 SkScalar offLen = info.fIntervals[1];
947 SkScalar halfOffLen = SkScalarHalf(offLen);
948 SkScalar halfStroke = SkScalarHalf(strokeWidth);
949 fIntervalLength = onLen + offLen;
950 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000951}
952
bsalomon0e08fc12014-10-15 08:19:04 -0700953bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700954 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000955 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000956 fRect == de.fRect &&
957 fIntervalLength == de.fIntervalLength);
958}
959
joshualitt9b989322014-12-15 14:16:27 -0800960void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const InitBT& init) const {
961 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
962 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
963}
964
965bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m, const GrBatchTracker& t) const {
966 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
967 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
968 return CanCombineOutput(mine.fInputColorType, mine.fColor,
969 theirs.fInputColorType, theirs.fColor);
970}
971
joshualittb0a8a372014-09-23 09:50:21 -0700972GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000973
joshualittb0a8a372014-09-23 09:50:21 -0700974GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
975 GrContext*,
976 const GrDrawTargetCaps& caps,
977 GrTexture*[]) {
978 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
979 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000980 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
981 DashInfo info;
982 info.fCount = 2;
983 SkAutoTArray<SkScalar> intervals(info.fCount);
984 info.fIntervals = intervals.get();
985 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
986 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
987 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
988
joshualitt2e3b3e32014-12-09 13:31:14 -0800989 return DashingLineEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000990}
991
992//////////////////////////////////////////////////////////////////////////////
993
joshualitt2e3b3e32014-12-09 13:31:14 -0800994GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
995 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700996 const SkPathEffect::DashInfo& info,
997 SkScalar strokeWidth,
998 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700999 switch (cap) {
1000 case GrDashingEffect::kRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -08001001 return DashingCircleEffect::Create(color, edgeType, info, SkScalarHalf(strokeWidth));
egdanielf767e792014-07-02 06:21:32 -07001002 case GrDashingEffect::kNonRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -08001003 return DashingLineEffect::Create(color, edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -07001004 default:
1005 SkFAIL("Unexpected dashed cap.");
1006 }
1007 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001008}