blob: 1641d272fcc1f607009f081a23083b5f27989628 [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;
444/*
445 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
446 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
447 * Both of the previous two parameters are in device space. This effect also requires the setting of
448 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
449 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
450 * transform the line to be horizontal, with the start of line at the origin then shifted to the
451 * right by half the off interval. The line then goes in the positive x direction.
452 */
joshualitt249af152014-09-15 11:41:13 -0700453class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700454public:
455 typedef SkPathEffect::DashInfo DashInfo;
456
joshualitt2e3b3e32014-12-09 13:31:14 -0800457 static GrGeometryProcessor* Create(GrColor,
458 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700459 const DashInfo& info,
460 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700461
462 virtual ~DashingCircleEffect();
463
joshualitteb2a6762014-12-04 11:35:33 -0800464 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700465
joshualitt2dd1ae02014-12-03 06:24:10 -0800466 const GrAttribute* inPosition() const { return fInPosition; }
467
468 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700469
joshualittb0a8a372014-09-23 09:50:21 -0700470 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700471
472 SkScalar getRadius() const { return fRadius; }
473
474 SkScalar getCenterX() const { return fCenterX; }
475
476 SkScalar getIntervalLength() const { return fIntervalLength; }
477
joshualitteb2a6762014-12-04 11:35:33 -0800478 virtual void getGLProcessorKey(const GrBatchTracker&,
479 const GrGLCaps&,
480 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700481
joshualitteb2a6762014-12-04 11:35:33 -0800482 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700483
484private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800485 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
486 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700487
bsalomon0e08fc12014-10-15 08:19:04 -0700488 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700489
joshualitt56995b52014-12-11 15:44:02 -0800490 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700491
joshualitt2dd1ae02014-12-03 06:24:10 -0800492 GrPrimitiveEdgeType fEdgeType;
493 const GrAttribute* fInPosition;
494 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700495 SkScalar fIntervalLength;
496 SkScalar fRadius;
497 SkScalar fCenterX;
498
joshualittb0a8a372014-09-23 09:50:21 -0700499 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700500
joshualitt249af152014-09-15 11:41:13 -0700501 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700502};
503
504//////////////////////////////////////////////////////////////////////////////
505
joshualitt249af152014-09-15 11:41:13 -0700506class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700507public:
joshualitteb2a6762014-12-04 11:35:33 -0800508 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700509
joshualittc369e7c2014-10-22 10:56:26 -0700510 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700511
joshualitt87f48d92014-12-04 10:41:40 -0800512 static inline void GenKey(const GrGeometryProcessor&,
513 const GrBatchTracker&,
514 const GrGLCaps&,
515 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700516
joshualitt87f48d92014-12-04 10:41:40 -0800517 virtual void setData(const GrGLProgramDataManager&,
518 const GrGeometryProcessor&,
519 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700520
521private:
kkinnunen7510b222014-07-30 00:04:16 -0700522 GrGLProgramDataManager::UniformHandle fParamUniform;
523 SkScalar fPrevRadius;
524 SkScalar fPrevCenterX;
525 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700526 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700527};
528
joshualitteb2a6762014-12-04 11:35:33 -0800529GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
530 const GrBatchTracker&) {
egdanielf767e792014-07-02 06:21:32 -0700531 fPrevRadius = SK_ScalarMin;
532 fPrevCenterX = SK_ScalarMin;
533 fPrevIntervalLength = SK_ScalarMax;
534}
535
joshualittc369e7c2014-10-22 10:56:26 -0700536void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
537 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700538 const char *paramName;
539 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
540 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700541 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800542 kVec3f_GrSLType, kDefault_GrSLPrecision,
543 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700544
joshualitt2dd1ae02014-12-03 06:24:10 -0800545 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
546
joshualitt74077b92014-10-24 11:26:03 -0700547 GrGLVertToFrag v(kVec2f_GrSLType);
548 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800549 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700550
joshualitt2dd1ae02014-12-03 06:24:10 -0800551 // setup coord outputs
552 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
553 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700554
joshualitt4973d9d2014-11-08 09:24:25 -0800555 // setup position varying
556 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800557 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800558
egdanielf767e792014-07-02 06:21:32 -0700559 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700560 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700561 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700562 v.fsIn(), v.fsIn(), paramName, paramName);
563 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700564 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
565 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700566 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700567 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
568 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
569 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700570 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700571 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
572 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700573 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800574 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700575}
576
joshualitt87f48d92014-12-04 10:41:40 -0800577void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
578 const GrGeometryProcessor& processor,
579 const GrBatchTracker&) {
joshualittb0a8a372014-09-23 09:50:21 -0700580 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700581 SkScalar radius = dce.getRadius();
582 SkScalar centerX = dce.getCenterX();
583 SkScalar intervalLength = dce.getIntervalLength();
584 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700585 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700586 fPrevRadius = radius;
587 fPrevCenterX = centerX;
588 fPrevIntervalLength = intervalLength;
589 }
590}
591
joshualitt87f48d92014-12-04 10:41:40 -0800592void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
593 const GrBatchTracker&,
594 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700595 GrProcessorKeyBuilder* b) {
596 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700597 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700598}
599
600//////////////////////////////////////////////////////////////////////////////
601
joshualitt2e3b3e32014-12-09 13:31:14 -0800602GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
603 GrPrimitiveEdgeType edgeType,
604 const DashInfo& info,
joshualittb0a8a372014-09-23 09:50:21 -0700605 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700606 if (info.fCount != 2 || info.fIntervals[0] != 0) {
607 return NULL;
608 }
609
joshualitt2e3b3e32014-12-09 13:31:14 -0800610 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700611}
612
613DashingCircleEffect::~DashingCircleEffect() {}
614
joshualitt56995b52014-12-11 15:44:02 -0800615void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
616 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700617}
618
joshualitteb2a6762014-12-04 11:35:33 -0800619void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
620 const GrGLCaps& caps,
621 GrProcessorKeyBuilder* b) const {
622 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
623}
624
625GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
626 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700627}
628
joshualitt2e3b3e32014-12-09 13:31:14 -0800629DashingCircleEffect::DashingCircleEffect(GrColor color,
630 GrPrimitiveEdgeType edgeType,
631 const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700632 SkScalar radius)
joshualitt2e3b3e32014-12-09 13:31:14 -0800633 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800634 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800635 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
636 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700637 SkScalar onLen = info.fIntervals[0];
638 SkScalar offLen = info.fIntervals[1];
639 fIntervalLength = onLen + offLen;
640 fRadius = radius;
641 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700642}
643
bsalomon0e08fc12014-10-15 08:19:04 -0700644bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700645 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700646 return (fEdgeType == dce.fEdgeType &&
647 fIntervalLength == dce.fIntervalLength &&
648 fRadius == dce.fRadius &&
649 fCenterX == dce.fCenterX);
650}
651
joshualittb0a8a372014-09-23 09:50:21 -0700652GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700653
joshualittb0a8a372014-09-23 09:50:21 -0700654GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
655 GrContext*,
656 const GrDrawTargetCaps& caps,
657 GrTexture*[]) {
658 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
659 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700660 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
661 DashInfo info;
662 info.fCount = 2;
663 SkAutoTArray<SkScalar> intervals(info.fCount);
664 info.fIntervals = intervals.get();
665 info.fIntervals[0] = 0;
666 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
667 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
668
joshualitt2e3b3e32014-12-09 13:31:14 -0800669 return DashingCircleEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700670}
671
672//////////////////////////////////////////////////////////////////////////////
673
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000674class GLDashingLineEffect;
675
egdanielf767e792014-07-02 06:21:32 -0700676/*
677 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
678 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
679 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
680 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
681 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
682 * line at the origin then shifted to the right by half the off interval. The line then goes in the
683 * positive x direction.
684 */
joshualitt249af152014-09-15 11:41:13 -0700685class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000686public:
687 typedef SkPathEffect::DashInfo DashInfo;
688
joshualitt2e3b3e32014-12-09 13:31:14 -0800689 static GrGeometryProcessor* Create(GrColor,
690 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700691 const DashInfo& info,
692 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000693
694 virtual ~DashingLineEffect();
695
joshualitteb2a6762014-12-04 11:35:33 -0800696 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000697
joshualitt2dd1ae02014-12-03 06:24:10 -0800698 const GrAttribute* inPosition() const { return fInPosition; }
699
700 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700701
joshualittb0a8a372014-09-23 09:50:21 -0700702 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000703
704 const SkRect& getRect() const { return fRect; }
705
706 SkScalar getIntervalLength() const { return fIntervalLength; }
707
joshualitteb2a6762014-12-04 11:35:33 -0800708 virtual void getGLProcessorKey(const GrBatchTracker& bt,
709 const GrGLCaps& caps,
710 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000711
joshualitteb2a6762014-12-04 11:35:33 -0800712 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000713
714private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800715 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
716 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000717
bsalomon0e08fc12014-10-15 08:19:04 -0700718 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000719
joshualitt56995b52014-12-11 15:44:02 -0800720 virtual void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700721
joshualitt2dd1ae02014-12-03 06:24:10 -0800722 GrPrimitiveEdgeType fEdgeType;
723 const GrAttribute* fInPosition;
724 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000725 SkRect fRect;
726 SkScalar fIntervalLength;
727
joshualittb0a8a372014-09-23 09:50:21 -0700728 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000729
joshualitt249af152014-09-15 11:41:13 -0700730 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000731};
732
733//////////////////////////////////////////////////////////////////////////////
734
joshualitt249af152014-09-15 11:41:13 -0700735class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000736public:
joshualitteb2a6762014-12-04 11:35:33 -0800737 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000738
joshualittc369e7c2014-10-22 10:56:26 -0700739 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000740
joshualitt87f48d92014-12-04 10:41:40 -0800741 static inline void GenKey(const GrGeometryProcessor&,
742 const GrBatchTracker&,
743 const GrGLCaps&,
744 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000745
joshualitt87f48d92014-12-04 10:41:40 -0800746 virtual void setData(const GrGLProgramDataManager&,
747 const GrGeometryProcessor&,
748 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000749
750private:
kkinnunen7510b222014-07-30 00:04:16 -0700751 GrGLProgramDataManager::UniformHandle fRectUniform;
752 GrGLProgramDataManager::UniformHandle fIntervalUniform;
753 SkRect fPrevRect;
754 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700755 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000756};
757
joshualitteb2a6762014-12-04 11:35:33 -0800758GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
759 const GrBatchTracker&) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000760 fPrevRect.fLeft = SK_ScalarNaN;
761 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762}
763
joshualittc369e7c2014-10-22 10:56:26 -0700764void GLDashingLineEffect::emitCode(const EmitArgs& args) {
765 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000766 const char *rectName;
767 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
768 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700769 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800770 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000771 "rect",
772 &rectName);
773 const char *intervalName;
774 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700775 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800776 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700777 "interval",
778 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700779
joshualitt2dd1ae02014-12-03 06:24:10 -0800780
781 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
782
joshualitt74077b92014-10-24 11:26:03 -0700783 GrGLVertToFrag v(kVec2f_GrSLType);
784 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800785 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
786
787 // setup coord outputs
788 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
789 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700790
joshualitt4973d9d2014-11-08 09:24:25 -0800791 // setup position varying
792 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800793 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800794
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000795 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700796 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700797 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700798 v.fsIn(), v.fsIn(), intervalName, intervalName);
799 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700800 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000801 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
802 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700803 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
804 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
805 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
806 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
807 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000808 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
809 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700810 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 +0000811 } else {
812 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700813 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
814 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
815 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 +0000816 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800817 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000818}
819
joshualittb0a8a372014-09-23 09:50:21 -0700820void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt87f48d92014-12-04 10:41:40 -0800821 const GrGeometryProcessor& processor,
822 const GrBatchTracker&) {
joshualittb0a8a372014-09-23 09:50:21 -0700823 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000824 const SkRect& rect = de.getRect();
825 SkScalar intervalLength = de.getIntervalLength();
826 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700827 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
828 rect.fRight - 0.5f, rect.fBottom - 0.5f);
829 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000830 fPrevRect = rect;
831 fPrevIntervalLength = intervalLength;
832 }
833}
834
joshualitt87f48d92014-12-04 10:41:40 -0800835void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
836 const GrBatchTracker&,
837 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700838 GrProcessorKeyBuilder* b) {
839 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700840 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000841}
842
843//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000844
joshualitt2e3b3e32014-12-09 13:31:14 -0800845GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
846 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700847 const DashInfo& info,
848 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000849 if (info.fCount != 2) {
850 return NULL;
851 }
852
joshualitt2e3b3e32014-12-09 13:31:14 -0800853 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000854}
855
856DashingLineEffect::~DashingLineEffect() {}
857
joshualitt56995b52014-12-11 15:44:02 -0800858void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
859 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000860}
861
joshualitteb2a6762014-12-04 11:35:33 -0800862void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
863 const GrGLCaps& caps,
864 GrProcessorKeyBuilder* b) const {
865 GLDashingLineEffect::GenKey(*this, bt, caps, b);
866}
867
868GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
869 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000870}
871
joshualitt2e3b3e32014-12-09 13:31:14 -0800872DashingLineEffect::DashingLineEffect(GrColor color,
873 GrPrimitiveEdgeType edgeType,
874 const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700875 SkScalar strokeWidth)
joshualitt2e3b3e32014-12-09 13:31:14 -0800876 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800877 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800878 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
879 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000880 SkScalar onLen = info.fIntervals[0];
881 SkScalar offLen = info.fIntervals[1];
882 SkScalar halfOffLen = SkScalarHalf(offLen);
883 SkScalar halfStroke = SkScalarHalf(strokeWidth);
884 fIntervalLength = onLen + offLen;
885 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000886}
887
bsalomon0e08fc12014-10-15 08:19:04 -0700888bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700889 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000890 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000891 fRect == de.fRect &&
892 fIntervalLength == de.fIntervalLength);
893}
894
joshualittb0a8a372014-09-23 09:50:21 -0700895GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000896
joshualittb0a8a372014-09-23 09:50:21 -0700897GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
898 GrContext*,
899 const GrDrawTargetCaps& caps,
900 GrTexture*[]) {
901 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
902 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000903 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
904 DashInfo info;
905 info.fCount = 2;
906 SkAutoTArray<SkScalar> intervals(info.fCount);
907 info.fIntervals = intervals.get();
908 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
909 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
910 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
911
joshualitt2e3b3e32014-12-09 13:31:14 -0800912 return DashingLineEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000913}
914
915//////////////////////////////////////////////////////////////////////////////
916
joshualitt2e3b3e32014-12-09 13:31:14 -0800917GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
918 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700919 const SkPathEffect::DashInfo& info,
920 SkScalar strokeWidth,
921 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700922 switch (cap) {
923 case GrDashingEffect::kRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -0800924 return DashingCircleEffect::Create(color, edgeType, info, SkScalarHalf(strokeWidth));
egdanielf767e792014-07-02 06:21:32 -0700925 case GrDashingEffect::kNonRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -0800926 return DashingLineEffect::Create(color, edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700927 default:
928 SkFAIL("Unexpected dashed cap.");
929 }
930 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000931}