blob: b59b2a6e5cecba57c2625c0633eeaad01df22852 [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
joshualitt8c0f6152014-12-10 14:12:22 -0800338 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;
joshualitt8c0f6152014-12-10 14:12:22 -0800350 gp = 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
joshualitt8c0f6152014-12-10 14:12:22 -0800353 gp = GrDefaultGeoProcFactory::Create(color, GrDefaultGeoProcFactory::kPosition_GPType);
joshualitt249af152014-09-15 11:41:13 -0700354 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355
joshualitt8c0f6152014-12-10 14:12:22 -0800356 drawState->setGeometryProcessor(gp)->unref();
357
egdaniele61c4112014-06-12 10:24:21 -0700358 int totalRectCnt = 0;
359
360 totalRectCnt += !lineDone ? 1 : 0;
361 totalRectCnt += hasStartRect ? 1 : 0;
362 totalRectCnt += hasEndRect ? 1 : 0;
363
joshualitt9853cce2014-11-17 14:22:48 -0800364 GrDrawTarget::AutoReleaseGeometry geo(target,
365 totalRectCnt * 4,
joshualitt2dd1ae02014-12-03 06:24:10 -0800366 gp->getVertexStride(), 0);
egdaniele61c4112014-06-12 10:24:21 -0700367 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700368 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700369 return false;
370 }
371
egdaniele61c4112014-06-12 10:24:21 -0700372 int curVIdx = 0;
373
egdanielf767e792014-07-02 06:21:32 -0700374 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
375 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
376 startOffset -= halfDevStroke;
377 }
378
egdaniele61c4112014-06-12 10:24:21 -0700379 // Draw interior part of dashed line
380 if (!lineDone) {
381 SkPoint devicePts[2];
382 vm.mapPoints(devicePts, ptsRot, 2);
383 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
384 if (hasCap) {
385 lineLength += 2.f * halfDevStroke;
386 }
387
388 SkRect bounds;
389 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
390 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800391 if (fullDash) {
392 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800393 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800394 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
395 lineLength, halfDevStroke);
396 } else {
397 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800398 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800399 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
400 }
egdaniele61c4112014-06-12 10:24:21 -0700401 curVIdx += 4;
402 }
403
404 if (hasStartRect) {
405 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
406 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800407 if (fullDash) {
408 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800409 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800410 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
411 devIntervals[0], halfDevStroke);
412 } else {
413 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800414 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800415 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
416 }
417
egdaniele61c4112014-06-12 10:24:21 -0700418 curVIdx += 4;
419 }
420
421 if (hasEndRect) {
422 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
423 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800424 if (fullDash) {
425 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800426 SkASSERT(gp->getVertexStride() == sizeof(DashLineVertex));
joshualitt5478d422014-11-14 16:00:38 -0800427 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
428 devIntervals[0], halfDevStroke);
429 } else {
430 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
joshualitt2dd1ae02014-12-03 06:24:10 -0800431 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
joshualitt5478d422014-11-14 16:00:38 -0800432 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
433 }
434
egdaniele61c4112014-06-12 10:24:21 -0700435 }
436
437 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
joshualitt8c0f6152014-12-10 14:12:22 -0800438 target->drawIndexedInstances(drawState, kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
egdaniele61c4112014-06-12 10:24:21 -0700439 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000440 return true;
441}
442
443//////////////////////////////////////////////////////////////////////////////
444
egdanielf767e792014-07-02 06:21:32 -0700445class GLDashingCircleEffect;
446/*
447 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
448 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
449 * Both of the previous two parameters are in device space. This effect also requires the setting of
450 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
451 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
452 * transform the line to be horizontal, with the start of line at the origin then shifted to the
453 * right by half the off interval. The line then goes in the positive x direction.
454 */
joshualitt249af152014-09-15 11:41:13 -0700455class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700456public:
457 typedef SkPathEffect::DashInfo DashInfo;
458
joshualitt2e3b3e32014-12-09 13:31:14 -0800459 static GrGeometryProcessor* Create(GrColor,
460 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700461 const DashInfo& info,
462 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700463
464 virtual ~DashingCircleEffect();
465
joshualitteb2a6762014-12-04 11:35:33 -0800466 virtual const char* name() const SK_OVERRIDE { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700467
joshualitt2dd1ae02014-12-03 06:24:10 -0800468 const GrAttribute* inPosition() const { return fInPosition; }
469
470 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700471
joshualittb0a8a372014-09-23 09:50:21 -0700472 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700473
474 SkScalar getRadius() const { return fRadius; }
475
476 SkScalar getCenterX() const { return fCenterX; }
477
478 SkScalar getIntervalLength() const { return fIntervalLength; }
479
joshualitteb2a6762014-12-04 11:35:33 -0800480 virtual void getGLProcessorKey(const GrBatchTracker&,
481 const GrGLCaps&,
482 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700483
joshualitteb2a6762014-12-04 11:35:33 -0800484 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker&) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700485
486private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800487 DashingCircleEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
488 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700489
bsalomon0e08fc12014-10-15 08:19:04 -0700490 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700491
joshualitt8c0f6152014-12-10 14:12:22 -0800492 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700493
joshualitt2dd1ae02014-12-03 06:24:10 -0800494 GrPrimitiveEdgeType fEdgeType;
495 const GrAttribute* fInPosition;
496 const GrAttribute* fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700497 SkScalar fIntervalLength;
498 SkScalar fRadius;
499 SkScalar fCenterX;
500
joshualittb0a8a372014-09-23 09:50:21 -0700501 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700502
joshualitt249af152014-09-15 11:41:13 -0700503 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700504};
505
506//////////////////////////////////////////////////////////////////////////////
507
joshualitt249af152014-09-15 11:41:13 -0700508class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700509public:
joshualitteb2a6762014-12-04 11:35:33 -0800510 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700511
joshualittc369e7c2014-10-22 10:56:26 -0700512 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700513
joshualitt87f48d92014-12-04 10:41:40 -0800514 static inline void GenKey(const GrGeometryProcessor&,
515 const GrBatchTracker&,
516 const GrGLCaps&,
517 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700518
joshualitt87f48d92014-12-04 10:41:40 -0800519 virtual void setData(const GrGLProgramDataManager&,
520 const GrGeometryProcessor&,
521 const GrBatchTracker&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700522
523private:
kkinnunen7510b222014-07-30 00:04:16 -0700524 GrGLProgramDataManager::UniformHandle fParamUniform;
525 SkScalar fPrevRadius;
526 SkScalar fPrevCenterX;
527 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700528 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700529};
530
joshualitteb2a6762014-12-04 11:35:33 -0800531GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
532 const GrBatchTracker&) {
egdanielf767e792014-07-02 06:21:32 -0700533 fPrevRadius = SK_ScalarMin;
534 fPrevCenterX = SK_ScalarMin;
535 fPrevIntervalLength = SK_ScalarMax;
536}
537
joshualittc369e7c2014-10-22 10:56:26 -0700538void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
539 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700540 const char *paramName;
541 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
542 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700543 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800544 kVec3f_GrSLType, kDefault_GrSLPrecision,
545 "params", &paramName);
egdanielf767e792014-07-02 06:21:32 -0700546
joshualitt2dd1ae02014-12-03 06:24:10 -0800547 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
548
joshualitt74077b92014-10-24 11:26:03 -0700549 GrGLVertToFrag v(kVec2f_GrSLType);
550 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800551 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), dce.inCoord()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700552
joshualitt2dd1ae02014-12-03 06:24:10 -0800553 // setup coord outputs
554 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), dce.inPosition()->fName);
555 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), dce.inPosition()->fName);
egdanielf767e792014-07-02 06:21:32 -0700556
joshualitt4973d9d2014-11-08 09:24:25 -0800557 // setup position varying
558 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800559 dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800560
egdanielf767e792014-07-02 06:21:32 -0700561 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700562 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700563 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700564 v.fsIn(), v.fsIn(), paramName, paramName);
565 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700566 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
567 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700568 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700569 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
570 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
571 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700572 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700573 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
574 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700575 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800576 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700577}
578
joshualitt87f48d92014-12-04 10:41:40 -0800579void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
580 const GrGeometryProcessor& processor,
581 const GrBatchTracker&) {
joshualittb0a8a372014-09-23 09:50:21 -0700582 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700583 SkScalar radius = dce.getRadius();
584 SkScalar centerX = dce.getCenterX();
585 SkScalar intervalLength = dce.getIntervalLength();
586 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700587 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700588 fPrevRadius = radius;
589 fPrevCenterX = centerX;
590 fPrevIntervalLength = intervalLength;
591 }
592}
593
joshualitt87f48d92014-12-04 10:41:40 -0800594void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& processor,
595 const GrBatchTracker&,
596 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700597 GrProcessorKeyBuilder* b) {
598 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700599 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700600}
601
602//////////////////////////////////////////////////////////////////////////////
603
joshualitt2e3b3e32014-12-09 13:31:14 -0800604GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
605 GrPrimitiveEdgeType edgeType,
606 const DashInfo& info,
joshualittb0a8a372014-09-23 09:50:21 -0700607 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700608 if (info.fCount != 2 || info.fIntervals[0] != 0) {
609 return NULL;
610 }
611
joshualitt2e3b3e32014-12-09 13:31:14 -0800612 return SkNEW_ARGS(DashingCircleEffect, (color, edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700613}
614
615DashingCircleEffect::~DashingCircleEffect() {}
616
joshualitt8c0f6152014-12-10 14:12:22 -0800617void DashingCircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
618 inout->mulByUnknownAlpha();
egdanielf767e792014-07-02 06:21:32 -0700619}
620
joshualitteb2a6762014-12-04 11:35:33 -0800621void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
622 const GrGLCaps& caps,
623 GrProcessorKeyBuilder* b) const {
624 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
625}
626
627GrGLGeometryProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt) const {
628 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700629}
630
joshualitt2e3b3e32014-12-09 13:31:14 -0800631DashingCircleEffect::DashingCircleEffect(GrColor color,
632 GrPrimitiveEdgeType edgeType,
633 const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700634 SkScalar radius)
joshualitt2e3b3e32014-12-09 13:31:14 -0800635 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800636 this->initClassID<DashingCircleEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800637 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
638 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700639 SkScalar onLen = info.fIntervals[0];
640 SkScalar offLen = info.fIntervals[1];
641 fIntervalLength = onLen + offLen;
642 fRadius = radius;
643 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700644}
645
bsalomon0e08fc12014-10-15 08:19:04 -0700646bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700647 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700648 return (fEdgeType == dce.fEdgeType &&
649 fIntervalLength == dce.fIntervalLength &&
650 fRadius == dce.fRadius &&
651 fCenterX == dce.fCenterX);
652}
653
joshualittb0a8a372014-09-23 09:50:21 -0700654GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700655
joshualittb0a8a372014-09-23 09:50:21 -0700656GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
657 GrContext*,
658 const GrDrawTargetCaps& caps,
659 GrTexture*[]) {
660 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
661 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700662 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
663 DashInfo info;
664 info.fCount = 2;
665 SkAutoTArray<SkScalar> intervals(info.fCount);
666 info.fIntervals = intervals.get();
667 info.fIntervals[0] = 0;
668 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
669 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
670
joshualitt2e3b3e32014-12-09 13:31:14 -0800671 return DashingCircleEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700672}
673
674//////////////////////////////////////////////////////////////////////////////
675
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000676class GLDashingLineEffect;
677
egdanielf767e792014-07-02 06:21:32 -0700678/*
679 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
680 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
681 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
682 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
683 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
684 * line at the origin then shifted to the right by half the off interval. The line then goes in the
685 * positive x direction.
686 */
joshualitt249af152014-09-15 11:41:13 -0700687class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000688public:
689 typedef SkPathEffect::DashInfo DashInfo;
690
joshualitt2e3b3e32014-12-09 13:31:14 -0800691 static GrGeometryProcessor* Create(GrColor,
692 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700693 const DashInfo& info,
694 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000695
696 virtual ~DashingLineEffect();
697
joshualitteb2a6762014-12-04 11:35:33 -0800698 virtual const char* name() const SK_OVERRIDE { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000699
joshualitt2dd1ae02014-12-03 06:24:10 -0800700 const GrAttribute* inPosition() const { return fInPosition; }
701
702 const GrAttribute* inCoord() const { return fInCoord; }
joshualitt249af152014-09-15 11:41:13 -0700703
joshualittb0a8a372014-09-23 09:50:21 -0700704 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000705
706 const SkRect& getRect() const { return fRect; }
707
708 SkScalar getIntervalLength() const { return fIntervalLength; }
709
joshualitteb2a6762014-12-04 11:35:33 -0800710 virtual void getGLProcessorKey(const GrBatchTracker& bt,
711 const GrGLCaps& caps,
712 GrProcessorKeyBuilder* b) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000713
joshualitteb2a6762014-12-04 11:35:33 -0800714 virtual GrGLGeometryProcessor* createGLInstance(const GrBatchTracker& bt) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000715
716private:
joshualitt2e3b3e32014-12-09 13:31:14 -0800717 DashingLineEffect(GrColor, GrPrimitiveEdgeType edgeType, const DashInfo& info,
718 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000719
bsalomon0e08fc12014-10-15 08:19:04 -0700720 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000721
joshualitt8c0f6152014-12-10 14:12:22 -0800722 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700723
joshualitt2dd1ae02014-12-03 06:24:10 -0800724 GrPrimitiveEdgeType fEdgeType;
725 const GrAttribute* fInPosition;
726 const GrAttribute* fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000727 SkRect fRect;
728 SkScalar fIntervalLength;
729
joshualittb0a8a372014-09-23 09:50:21 -0700730 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000731
joshualitt249af152014-09-15 11:41:13 -0700732 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000733};
734
735//////////////////////////////////////////////////////////////////////////////
736
joshualitt249af152014-09-15 11:41:13 -0700737class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000738public:
joshualitteb2a6762014-12-04 11:35:33 -0800739 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000740
joshualittc369e7c2014-10-22 10:56:26 -0700741 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000742
joshualitt87f48d92014-12-04 10:41:40 -0800743 static inline void GenKey(const GrGeometryProcessor&,
744 const GrBatchTracker&,
745 const GrGLCaps&,
746 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000747
joshualitt87f48d92014-12-04 10:41:40 -0800748 virtual void setData(const GrGLProgramDataManager&,
749 const GrGeometryProcessor&,
750 const GrBatchTracker&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000751
752private:
kkinnunen7510b222014-07-30 00:04:16 -0700753 GrGLProgramDataManager::UniformHandle fRectUniform;
754 GrGLProgramDataManager::UniformHandle fIntervalUniform;
755 SkRect fPrevRect;
756 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700757 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000758};
759
joshualitteb2a6762014-12-04 11:35:33 -0800760GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
761 const GrBatchTracker&) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762 fPrevRect.fLeft = SK_ScalarNaN;
763 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000764}
765
joshualittc369e7c2014-10-22 10:56:26 -0700766void GLDashingLineEffect::emitCode(const EmitArgs& args) {
767 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000768 const char *rectName;
769 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
770 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700771 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800772 kVec4f_GrSLType, kDefault_GrSLPrecision,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000773 "rect",
774 &rectName);
775 const char *intervalName;
776 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700777 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
bsalomon422f56f2014-12-09 10:18:12 -0800778 kFloat_GrSLType, kDefault_GrSLPrecision,
joshualittc369e7c2014-10-22 10:56:26 -0700779 "interval",
780 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700781
joshualitt2dd1ae02014-12-03 06:24:10 -0800782
783 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
784
joshualitt74077b92014-10-24 11:26:03 -0700785 GrGLVertToFrag v(kVec2f_GrSLType);
786 args.fPB->addVarying("Coord", &v);
joshualitt2dd1ae02014-12-03 06:24:10 -0800787 vsBuilder->codeAppendf("%s = %s;", v.vsOut(), de.inCoord()->fName);
788
789 // setup coord outputs
790 vsBuilder->codeAppendf("%s = %s;", vsBuilder->positionCoords(), de.inPosition()->fName);
791 vsBuilder->codeAppendf("%s = %s;", vsBuilder->localCoords(), de.inPosition()->fName);
egdaniele61c4112014-06-12 10:24:21 -0700792
joshualitt4973d9d2014-11-08 09:24:25 -0800793 // setup position varying
794 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
joshualitt2dd1ae02014-12-03 06:24:10 -0800795 de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800796
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000797 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700798 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700799 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700800 v.fsIn(), v.fsIn(), intervalName, intervalName);
801 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700802 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000803 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
804 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700805 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
806 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
807 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
808 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
809 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000810 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
811 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700812 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 +0000813 } else {
814 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700815 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
816 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
817 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 +0000818 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800819 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000820}
821
joshualittb0a8a372014-09-23 09:50:21 -0700822void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt87f48d92014-12-04 10:41:40 -0800823 const GrGeometryProcessor& processor,
824 const GrBatchTracker&) {
joshualittb0a8a372014-09-23 09:50:21 -0700825 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000826 const SkRect& rect = de.getRect();
827 SkScalar intervalLength = de.getIntervalLength();
828 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700829 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
830 rect.fRight - 0.5f, rect.fBottom - 0.5f);
831 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000832 fPrevRect = rect;
833 fPrevIntervalLength = intervalLength;
834 }
835}
836
joshualitt87f48d92014-12-04 10:41:40 -0800837void GLDashingLineEffect::GenKey(const GrGeometryProcessor& processor,
838 const GrBatchTracker&,
839 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700840 GrProcessorKeyBuilder* b) {
841 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700842 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000843}
844
845//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000846
joshualitt2e3b3e32014-12-09 13:31:14 -0800847GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
848 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700849 const DashInfo& info,
850 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000851 if (info.fCount != 2) {
852 return NULL;
853 }
854
joshualitt2e3b3e32014-12-09 13:31:14 -0800855 return SkNEW_ARGS(DashingLineEffect, (color, edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000856}
857
858DashingLineEffect::~DashingLineEffect() {}
859
joshualitt8c0f6152014-12-10 14:12:22 -0800860void DashingLineEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
861 inout->mulByUnknownAlpha();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000862}
863
joshualitteb2a6762014-12-04 11:35:33 -0800864void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
865 const GrGLCaps& caps,
866 GrProcessorKeyBuilder* b) const {
867 GLDashingLineEffect::GenKey(*this, bt, caps, b);
868}
869
870GrGLGeometryProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt) const {
871 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000872}
873
joshualitt2e3b3e32014-12-09 13:31:14 -0800874DashingLineEffect::DashingLineEffect(GrColor color,
875 GrPrimitiveEdgeType edgeType,
876 const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700877 SkScalar strokeWidth)
joshualitt2e3b3e32014-12-09 13:31:14 -0800878 : INHERITED(color), fEdgeType(edgeType) {
joshualitteb2a6762014-12-04 11:35:33 -0800879 this->initClassID<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -0800880 fInPosition = &this->addVertexAttrib(GrAttribute("inPosition", kVec2f_GrVertexAttribType));
881 fInCoord = &this->addVertexAttrib(GrAttribute("inCoord", kVec2f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000882 SkScalar onLen = info.fIntervals[0];
883 SkScalar offLen = info.fIntervals[1];
884 SkScalar halfOffLen = SkScalarHalf(offLen);
885 SkScalar halfStroke = SkScalarHalf(strokeWidth);
886 fIntervalLength = onLen + offLen;
887 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000888}
889
bsalomon0e08fc12014-10-15 08:19:04 -0700890bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700891 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000892 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000893 fRect == de.fRect &&
894 fIntervalLength == de.fIntervalLength);
895}
896
joshualittb0a8a372014-09-23 09:50:21 -0700897GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000898
joshualittb0a8a372014-09-23 09:50:21 -0700899GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
900 GrContext*,
901 const GrDrawTargetCaps& caps,
902 GrTexture*[]) {
903 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
904 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000905 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
906 DashInfo info;
907 info.fCount = 2;
908 SkAutoTArray<SkScalar> intervals(info.fCount);
909 info.fIntervals = intervals.get();
910 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
911 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
912 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
913
joshualitt2e3b3e32014-12-09 13:31:14 -0800914 return DashingLineEffect::Create(GrRandomColor(random), edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000915}
916
917//////////////////////////////////////////////////////////////////////////////
918
joshualitt2e3b3e32014-12-09 13:31:14 -0800919GrGeometryProcessor* GrDashingEffect::Create(GrColor color,
920 GrPrimitiveEdgeType edgeType,
joshualittb0a8a372014-09-23 09:50:21 -0700921 const SkPathEffect::DashInfo& info,
922 SkScalar strokeWidth,
923 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700924 switch (cap) {
925 case GrDashingEffect::kRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -0800926 return DashingCircleEffect::Create(color, edgeType, info, SkScalarHalf(strokeWidth));
egdanielf767e792014-07-02 06:21:32 -0700927 case GrDashingEffect::kNonRound_DashCap:
joshualitt2e3b3e32014-12-09 13:31:14 -0800928 return DashingLineEffect::Create(color, edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700929 default:
930 SkFAIL("Unexpected dashed cap.");
931 }
932 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000933}