blob: af96ef63185acb9ee23426458f3bc8ee6f13abfb [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"
joshualittb0a8a372014-09-23 09:50:21 -070021#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000022#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "gl/GrGLGeometryProcessor.h"
24#include "gl/GrGLProcessor.h"
25#include "gl/GrGLSL.h"
26#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000027
28///////////////////////////////////////////////////////////////////////////////
29
egdaniele61c4112014-06-12 10:24:21 -070030// Returns whether or not the gpu can fast path the dash line effect.
31static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
32 const GrDrawTarget& target, const SkMatrix& viewMatrix) {
33 if (target.getDrawState().getRenderTarget()->isMultisampled()) {
34 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 {
67
68struct DashLineVertex {
69 SkPoint fPos;
70 SkPoint fDashPos;
71};
72
73extern const GrVertexAttrib gDashLineVertexAttribs[] = {
74 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
joshualittb0a8a372014-09-23 09:50:21 -070075 { kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding },
egdaniele61c4112014-06-12 10:24:21 -070076};
77
78};
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000079static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
80 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
81 SkVector vecSrc = pts[1] - pts[0];
82 SkScalar magSrc = vecSrc.length();
83 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
84 vecSrc.scale(invSrc);
85
86 SkVector vecSrcPerp;
87 vecSrc.rotateCW(&vecSrcPerp);
88 viewMatrix.mapVectors(&vecSrc, 1);
89 viewMatrix.mapVectors(&vecSrcPerp, 1);
90
91 // parallelScale tells how much to scale along the line parallel to the dash line
92 // perpScale tells how much to scale in the direction perpendicular to the dash line
93 *parallelScale = vecSrc.length();
94 *perpScale = vecSrcPerp.length();
95}
96
97// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
98// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
99static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
100 SkVector vec = pts[1] - pts[0];
101 SkScalar mag = vec.length();
102 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
103
104 vec.scale(inv);
105 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
106 if (ptsRot) {
107 rotMatrix->mapPoints(ptsRot, pts, 2);
108 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
109 ptsRot[1].fY = pts[0].fY;
110 }
111}
112
113// Assumes phase < sum of all intervals
114static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) {
115 SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
116 if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) {
117 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
118 return srcIntervalLen - info.fPhase;
119 }
120 return 0;
121}
122
egdaniele61c4112014-06-12 10:24:21 -0700123static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
124 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000125 if (pts[1].fX <= pts[0].fX) {
126 return 0;
127 }
128 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
129 SkScalar totalLen = pts[1].fX - pts[0].fX;
130 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
131 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700132 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000133 temp = SkScalarDiv(*endingInt, srcIntervalLen);
134 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
135 if (0 == *endingInt) {
136 *endingInt = srcIntervalLen;
137 }
138 if (*endingInt > info.fIntervals[0]) {
139 if (0 == info.fIntervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000140 *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 +0000141 }
142 return *endingInt - info.fIntervals[0];
143 }
144 return 0;
145}
146
egdaniele61c4112014-06-12 10:24:21 -0700147static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
148 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
egdaniele61c4112014-06-12 10:24:21 -0700149 SkScalar startDashX = offset - bloat;
150 SkScalar endDashX = offset + len + bloat;
151 SkScalar startDashY = -stroke - bloat;
152 SkScalar endDashY = stroke + bloat;
153 verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
154 verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
155 verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
156 verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
egdaniele61c4112014-06-12 10:24:21 -0700157 verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
158 verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
159 verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
160 verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
egdaniele61c4112014-06-12 10:24:21 -0700161 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
162}
163
joshualitt5478d422014-11-14 16:00:38 -0800164static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
165 SkPoint* verts) {
166 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
167 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
168 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
169 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
170 matrix.mapPoints(&verts[idx], 4);
171}
egdaniele61c4112014-06-12 10:24:21 -0700172
173bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint,
174 const GrStrokeInfo& strokeInfo, GrGpu* gpu,
175 GrDrawTarget* target, const SkMatrix& vm) {
176
177 if (!can_fast_path_dash(pts, strokeInfo, *target, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000178 return false;
179 }
180
egdaniele61c4112014-06-12 10:24:21 -0700181 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000182
egdaniele61c4112014-06-12 10:24:21 -0700183 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000184
egdaniele61c4112014-06-12 10:24:21 -0700185 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000186
187 // the phase should be normalized to be [0, sum of all intervals)
188 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
189
egdaniele61c4112014-06-12 10:24:21 -0700190 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000191
192 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
193 SkMatrix srcRotInv;
194 SkPoint ptsRot[2];
195 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700196 SkMatrix rotMatrix;
197 align_to_x_axis(pts, &rotMatrix, ptsRot);
198 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700199 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000200 return false;
201 }
202 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000203 srcRotInv.reset();
204 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
205 }
206
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000208
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000209 // Scale corrections of intervals and stroke from view matrix
210 SkScalar parallelScale;
211 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700212 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
egdanielf767e792014-07-02 06:21:32 -0700214 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000215
216 // We always want to at least stroke out half a pixel on each side in device space
217 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700218 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219
egdaniele61c4112014-06-12 10:24:21 -0700220 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000221 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700222 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000223 } else {
egdaniele61c4112014-06-12 10:24:21 -0700224 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000225 }
226
egdaniele61c4112014-06-12 10:24:21 -0700227 SkScalar startAdj = 0;
228
229 SkMatrix combinedMatrix = srcRotInv;
230 combinedMatrix.postConcat(vm);
231
232 bool lineDone = false;
233 SkRect startRect;
234 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000235 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
236 // draw it separately here and adjust our start point accordingly
237 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700238 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000239 SkPoint startPts[2];
240 startPts[0] = ptsRot[0];
241 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700242 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000244 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700245 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000246
egdaniele61c4112014-06-12 10:24:21 -0700247 hasStartRect = true;
248 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000249 }
250 }
251
252 // adjustments for start and end of bounding rect so we only draw dash intervals
253 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700254 startAdj += calc_start_adjustment(info);
255 if (startAdj != 0) {
256 ptsRot[0].fX += startAdj;
257 srcPhase = 0;
258 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000259 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700260 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
261 ptsRot[1].fX -= endAdj;
262 if (ptsRot[0].fX >= ptsRot[1].fX) {
263 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000264 }
265
egdaniele61c4112014-06-12 10:24:21 -0700266 SkRect endRect;
267 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000268 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
269 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700270 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000271 // If we adjusted the end then we will not be drawing a partial dash at the end.
272 // If we didn't adjust the end point then we just need to make sure the ending
273 // dash isn't a full dash
274 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000275 SkPoint endPts[2];
276 endPts[1] = ptsRot[1];
277 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000278 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000279
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000280 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700281 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000282
egdaniele61c4112014-06-12 10:24:21 -0700283 hasEndRect = true;
284 endAdj = endingInterval + info.fIntervals[1];
285
286 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000287 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700288 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000289 }
290 }
291 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000292
egdaniele61c4112014-06-12 10:24:21 -0700293 if (startAdj != 0) {
294 srcPhase = 0;
295 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000296
egdaniele61c4112014-06-12 10:24:21 -0700297 // Change the dashing info from src space into device space
298 SkScalar devIntervals[2];
299 devIntervals[0] = info.fIntervals[0] * parallelScale;
300 devIntervals[1] = info.fIntervals[1] * parallelScale;
301 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000302 SkScalar strokeWidth = srcStrokeWidth * perpScale;
303
304 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
305 strokeWidth = 1.f;
306 }
307
egdaniele61c4112014-06-12 10:24:21 -0700308 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000309
310 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
311 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700312 devIntervals[0] += strokeWidth;
313 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000314 }
egdaniele61c4112014-06-12 10:24:21 -0700315 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000316
egdaniele61c4112014-06-12 10:24:21 -0700317 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
318 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
319
320 SkScalar devBloat = useAA ? 0.5f : 0.f;
321
322 GrDrawState* drawState = target->drawState();
323 if (devIntervals[1] <= 0.f && useAA) {
324 // Case when we end up drawing a solid AA rect
325 // Reset the start rect to draw this single solid rect
326 // but it requires to upload a new intervals uniform so we can mimic
327 // one giant dash
328 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
329 ptsRot[1].fX += hasEndRect ? endAdj : 0;
330 startRect.set(ptsRot, 2);
331 startRect.outset(strokeAdj, halfSrcStroke);
332 hasStartRect = true;
333 hasEndRect = false;
334 lineDone = true;
335
336 SkPoint devicePts[2];
337 vm.mapPoints(devicePts, ptsRot, 2);
338 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
339 if (hasCap) {
340 lineLength += 2.f * halfDevStroke;
341 }
342 devIntervals[0] = lineLength;
343 }
joshualitt5478d422014-11-14 16:00:38 -0800344 bool fullDash = devIntervals[1] > 0.f || useAA;
345 if (fullDash) {
egdaniele61c4112014-06-12 10:24:21 -0700346 SkPathEffect::DashInfo devInfo;
347 devInfo.fPhase = devPhase;
348 devInfo.fCount = 2;
349 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700350 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
351 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700352 bool isRoundCap = SkPaint::kRound_Cap == cap;
353 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
354 GrDashingEffect::kNonRound_DashCap;
joshualittbd769d02014-09-04 08:56:46 -0700355 drawState->setGeometryProcessor(
joshualittb0a8a372014-09-23 09:50:21 -0700356 GrDashingEffect::Create(edgeType, devInfo, strokeWidth, capType))->unref();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000357
joshualitt249af152014-09-15 11:41:13 -0700358 // Set up the vertex data for the line and start/end dashes
359 drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs),
360 sizeof(DashLineVertex));
361 } else {
362 // Set up the vertex data for the line and start/end dashes
joshualitt5478d422014-11-14 16:00:38 -0800363 drawState->setGeometryProcessor(
364 GrDefaultGeoProcFactory::CreateAndSetAttribs(
365 drawState,
366 GrDefaultGeoProcFactory::kPosition_GPType))->unref();
joshualitt249af152014-09-15 11:41:13 -0700367 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000368
egdaniele61c4112014-06-12 10:24:21 -0700369 int totalRectCnt = 0;
370
371 totalRectCnt += !lineDone ? 1 : 0;
372 totalRectCnt += hasStartRect ? 1 : 0;
373 totalRectCnt += hasEndRect ? 1 : 0;
374
375 GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0);
376 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700377 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700378 return false;
379 }
380
egdaniele61c4112014-06-12 10:24:21 -0700381 int curVIdx = 0;
382
egdanielf767e792014-07-02 06:21:32 -0700383 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
384 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
385 startOffset -= halfDevStroke;
386 }
387
egdaniele61c4112014-06-12 10:24:21 -0700388 // Draw interior part of dashed line
389 if (!lineDone) {
390 SkPoint devicePts[2];
391 vm.mapPoints(devicePts, ptsRot, 2);
392 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
393 if (hasCap) {
394 lineLength += 2.f * halfDevStroke;
395 }
396
397 SkRect bounds;
398 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
399 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
joshualitt5478d422014-11-14 16:00:38 -0800400 if (fullDash) {
401 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
402 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
403 lineLength, halfDevStroke);
404 } else {
405 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
406 setup_dashed_rect_pos(bounds, curVIdx, combinedMatrix, verts);
407 }
egdaniele61c4112014-06-12 10:24:21 -0700408 curVIdx += 4;
409 }
410
411 if (hasStartRect) {
412 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
413 startRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800414 if (fullDash) {
415 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
416 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
417 devIntervals[0], halfDevStroke);
418 } else {
419 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
420 setup_dashed_rect_pos(startRect, curVIdx, combinedMatrix, verts);
421 }
422
egdaniele61c4112014-06-12 10:24:21 -0700423 curVIdx += 4;
424 }
425
426 if (hasEndRect) {
427 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
428 endRect.outset(bloatX, bloatY);
joshualitt5478d422014-11-14 16:00:38 -0800429 if (fullDash) {
430 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
431 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
432 devIntervals[0], halfDevStroke);
433 } else {
434 SkPoint* verts = reinterpret_cast<SkPoint*>(geo.vertices());
435 setup_dashed_rect_pos(endRect, curVIdx, combinedMatrix, verts);
436 }
437
egdaniele61c4112014-06-12 10:24:21 -0700438 }
439
440 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
441 target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
442 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000443 return true;
444}
445
446//////////////////////////////////////////////////////////////////////////////
447
egdanielf767e792014-07-02 06:21:32 -0700448class GLDashingCircleEffect;
449/*
450 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
451 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
452 * Both of the previous two parameters are in device space. This effect also requires the setting of
453 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
454 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
455 * transform the line to be horizontal, with the start of line at the origin then shifted to the
456 * right by half the off interval. The line then goes in the positive x direction.
457 */
joshualitt249af152014-09-15 11:41:13 -0700458class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700459public:
460 typedef SkPathEffect::DashInfo DashInfo;
461
joshualittb0a8a372014-09-23 09:50:21 -0700462 static GrGeometryProcessor* Create(GrPrimitiveEdgeType edgeType,
463 const DashInfo& info,
464 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700465
466 virtual ~DashingCircleEffect();
467
468 static const char* Name() { return "DashingCircleEffect"; }
469
joshualitt249af152014-09-15 11:41:13 -0700470 const GrShaderVar& inCoord() const { return fInCoord; }
471
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
joshualittb0a8a372014-09-23 09:50:21 -0700480 typedef GLDashingCircleEffect GLProcessor;
egdanielf767e792014-07-02 06:21:32 -0700481
joshualittb0a8a372014-09-23 09:50:21 -0700482 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700483
484private:
joshualittb0a8a372014-09-23 09:50:21 -0700485 DashingCircleEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info, SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700486
bsalomon0e08fc12014-10-15 08:19:04 -0700487 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700488
egdaniel605dd0f2014-11-12 08:35:25 -0800489 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700490
joshualittb0a8a372014-09-23 09:50:21 -0700491 GrPrimitiveEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700492 const GrShaderVar& fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700493 SkScalar fIntervalLength;
494 SkScalar fRadius;
495 SkScalar fCenterX;
496
joshualittb0a8a372014-09-23 09:50:21 -0700497 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700498
joshualitt249af152014-09-15 11:41:13 -0700499 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700500};
501
502//////////////////////////////////////////////////////////////////////////////
503
joshualitt249af152014-09-15 11:41:13 -0700504class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700505public:
joshualittb0a8a372014-09-23 09:50:21 -0700506 GLDashingCircleEffect(const GrBackendProcessorFactory&, const GrProcessor&);
egdanielf767e792014-07-02 06:21:32 -0700507
joshualittc369e7c2014-10-22 10:56:26 -0700508 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700509
joshualittb0a8a372014-09-23 09:50:21 -0700510 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700511
joshualittb0a8a372014-09-23 09:50:21 -0700512 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700513
514private:
kkinnunen7510b222014-07-30 00:04:16 -0700515 GrGLProgramDataManager::UniformHandle fParamUniform;
516 SkScalar fPrevRadius;
517 SkScalar fPrevCenterX;
518 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700519 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700520};
521
joshualittb0a8a372014-09-23 09:50:21 -0700522GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendProcessorFactory& factory,
523 const GrProcessor&)
egdanielf767e792014-07-02 06:21:32 -0700524 : INHERITED (factory) {
525 fPrevRadius = SK_ScalarMin;
526 fPrevCenterX = SK_ScalarMin;
527 fPrevIntervalLength = SK_ScalarMax;
528}
529
joshualittc369e7c2014-10-22 10:56:26 -0700530void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
531 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700532 const char *paramName;
533 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
534 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700535 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
536 kVec3f_GrSLType,
537 "params",
538 &paramName);
egdanielf767e792014-07-02 06:21:32 -0700539
joshualitt74077b92014-10-24 11:26:03 -0700540 GrGLVertToFrag v(kVec2f_GrSLType);
541 args.fPB->addVarying("Coord", &v);
joshualitt30ba4362014-08-21 20:18:45 -0700542
joshualittc369e7c2014-10-22 10:56:26 -0700543 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700544 vsBuilder->codeAppendf("\t%s = %s;\n", v.vsOut(), dce.inCoord().c_str());
egdanielf767e792014-07-02 06:21:32 -0700545
joshualitt4973d9d2014-11-08 09:24:25 -0800546 // setup position varying
547 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
548 vsBuilder->inPosition());
549
egdanielf767e792014-07-02 06:21:32 -0700550 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700551 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700552 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700553 v.fsIn(), v.fsIn(), paramName, paramName);
554 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700555 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
556 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700557 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700558 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
559 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
560 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700561 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700562 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
563 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700564 }
joshualittc369e7c2014-10-22 10:56:26 -0700565 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutput,
566 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("alpha")).c_str());
egdanielf767e792014-07-02 06:21:32 -0700567}
568
joshualittb0a8a372014-09-23 09:50:21 -0700569void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman
570 , const GrProcessor& processor) {
571 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700572 SkScalar radius = dce.getRadius();
573 SkScalar centerX = dce.getCenterX();
574 SkScalar intervalLength = dce.getIntervalLength();
575 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700576 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700577 fPrevRadius = radius;
578 fPrevCenterX = centerX;
579 fPrevIntervalLength = intervalLength;
580 }
581}
582
joshualittb0a8a372014-09-23 09:50:21 -0700583void GLDashingCircleEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
584 GrProcessorKeyBuilder* b) {
585 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700586 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700587}
588
589//////////////////////////////////////////////////////////////////////////////
590
joshualittb0a8a372014-09-23 09:50:21 -0700591GrGeometryProcessor* DashingCircleEffect::Create(GrPrimitiveEdgeType edgeType, const DashInfo& info,
592 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700593 if (info.fCount != 2 || info.fIntervals[0] != 0) {
594 return NULL;
595 }
596
bsalomon55fad7a2014-07-08 07:34:20 -0700597 return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700598}
599
600DashingCircleEffect::~DashingCircleEffect() {}
601
egdaniel605dd0f2014-11-12 08:35:25 -0800602void DashingCircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700603 inout->mulByUnknownAlpha();
egdanielf767e792014-07-02 06:21:32 -0700604}
605
joshualittb0a8a372014-09-23 09:50:21 -0700606const GrBackendGeometryProcessorFactory& DashingCircleEffect::getFactory() const {
607 return GrTBackendGeometryProcessorFactory<DashingCircleEffect>::getInstance();
egdanielf767e792014-07-02 06:21:32 -0700608}
609
joshualittb0a8a372014-09-23 09:50:21 -0700610DashingCircleEffect::DashingCircleEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700611 SkScalar radius)
joshualitt249af152014-09-15 11:41:13 -0700612 : fEdgeType(edgeType)
613 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
614 kVec2f_GrSLType,
615 GrShaderVar::kAttribute_TypeModifier))) {
egdanielf767e792014-07-02 06:21:32 -0700616 SkScalar onLen = info.fIntervals[0];
617 SkScalar offLen = info.fIntervals[1];
618 fIntervalLength = onLen + offLen;
619 fRadius = radius;
620 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700621}
622
bsalomon0e08fc12014-10-15 08:19:04 -0700623bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700624 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700625 return (fEdgeType == dce.fEdgeType &&
626 fIntervalLength == dce.fIntervalLength &&
627 fRadius == dce.fRadius &&
628 fCenterX == dce.fCenterX);
629}
630
joshualittb0a8a372014-09-23 09:50:21 -0700631GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700632
joshualittb0a8a372014-09-23 09:50:21 -0700633GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
634 GrContext*,
635 const GrDrawTargetCaps& caps,
636 GrTexture*[]) {
637 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
638 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700639 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
640 DashInfo info;
641 info.fCount = 2;
642 SkAutoTArray<SkScalar> intervals(info.fCount);
643 info.fIntervals = intervals.get();
644 info.fIntervals[0] = 0;
645 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
646 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
647
joshualittb0a8a372014-09-23 09:50:21 -0700648 return DashingCircleEffect::Create(edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700649}
650
651//////////////////////////////////////////////////////////////////////////////
652
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000653class GLDashingLineEffect;
654
egdanielf767e792014-07-02 06:21:32 -0700655/*
656 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
657 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
658 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
659 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
660 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
661 * line at the origin then shifted to the right by half the off interval. The line then goes in the
662 * positive x direction.
663 */
joshualitt249af152014-09-15 11:41:13 -0700664class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000665public:
666 typedef SkPathEffect::DashInfo DashInfo;
667
joshualittb0a8a372014-09-23 09:50:21 -0700668 static GrGeometryProcessor* Create(GrPrimitiveEdgeType edgeType,
669 const DashInfo& info,
670 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000671
672 virtual ~DashingLineEffect();
673
674 static const char* Name() { return "DashingEffect"; }
675
joshualitt249af152014-09-15 11:41:13 -0700676 const GrShaderVar& inCoord() const { return fInCoord; }
677
joshualittb0a8a372014-09-23 09:50:21 -0700678 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000679
680 const SkRect& getRect() const { return fRect; }
681
682 SkScalar getIntervalLength() const { return fIntervalLength; }
683
joshualittb0a8a372014-09-23 09:50:21 -0700684 typedef GLDashingLineEffect GLProcessor;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000685
joshualittb0a8a372014-09-23 09:50:21 -0700686 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000687
688private:
joshualittb0a8a372014-09-23 09:50:21 -0700689 DashingLineEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000690
bsalomon0e08fc12014-10-15 08:19:04 -0700691 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000692
egdaniel605dd0f2014-11-12 08:35:25 -0800693 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700694
joshualittb0a8a372014-09-23 09:50:21 -0700695 GrPrimitiveEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700696 const GrShaderVar& fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000697 SkRect fRect;
698 SkScalar fIntervalLength;
699
joshualittb0a8a372014-09-23 09:50:21 -0700700 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000701
joshualitt249af152014-09-15 11:41:13 -0700702 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000703};
704
705//////////////////////////////////////////////////////////////////////////////
706
joshualitt249af152014-09-15 11:41:13 -0700707class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708public:
joshualittb0a8a372014-09-23 09:50:21 -0700709 GLDashingLineEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000710
joshualittc369e7c2014-10-22 10:56:26 -0700711 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000712
joshualittb0a8a372014-09-23 09:50:21 -0700713 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000714
joshualittb0a8a372014-09-23 09:50:21 -0700715 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000716
717private:
kkinnunen7510b222014-07-30 00:04:16 -0700718 GrGLProgramDataManager::UniformHandle fRectUniform;
719 GrGLProgramDataManager::UniformHandle fIntervalUniform;
720 SkRect fPrevRect;
721 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700722 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000723};
724
joshualittb0a8a372014-09-23 09:50:21 -0700725GLDashingLineEffect::GLDashingLineEffect(const GrBackendProcessorFactory& factory,
726 const GrProcessor&)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000727 : INHERITED (factory) {
728 fPrevRect.fLeft = SK_ScalarNaN;
729 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000730}
731
joshualittc369e7c2014-10-22 10:56:26 -0700732void GLDashingLineEffect::emitCode(const EmitArgs& args) {
733 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000734 const char *rectName;
735 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
736 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700737 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000738 kVec4f_GrSLType,
739 "rect",
740 &rectName);
741 const char *intervalName;
742 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700743 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
744 kFloat_GrSLType,
745 "interval",
746 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700747
joshualitt74077b92014-10-24 11:26:03 -0700748 GrGLVertToFrag v(kVec2f_GrSLType);
749 args.fPB->addVarying("Coord", &v);
joshualittc369e7c2014-10-22 10:56:26 -0700750 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700751 vsBuilder->codeAppendf("\t%s = %s;\n", v.vsOut(), de.inCoord().c_str());
egdaniele61c4112014-06-12 10:24:21 -0700752
joshualitt4973d9d2014-11-08 09:24:25 -0800753 // setup position varying
754 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
755 vsBuilder->inPosition());
756
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000757 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700758 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700759 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700760 v.fsIn(), v.fsIn(), intervalName, intervalName);
761 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700762 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000763 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
764 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700765 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
766 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
767 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
768 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
769 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000770 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
771 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700772 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 +0000773 } else {
774 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700775 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
776 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
777 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 +0000778 }
joshualittc369e7c2014-10-22 10:56:26 -0700779 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutput,
780 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000781}
782
joshualittb0a8a372014-09-23 09:50:21 -0700783void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
784 const GrProcessor& processor) {
785 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000786 const SkRect& rect = de.getRect();
787 SkScalar intervalLength = de.getIntervalLength();
788 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700789 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
790 rect.fRight - 0.5f, rect.fBottom - 0.5f);
791 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000792 fPrevRect = rect;
793 fPrevIntervalLength = intervalLength;
794 }
795}
796
joshualittb0a8a372014-09-23 09:50:21 -0700797void GLDashingLineEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
798 GrProcessorKeyBuilder* b) {
799 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700800 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000801}
802
803//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000804
joshualittb0a8a372014-09-23 09:50:21 -0700805GrGeometryProcessor* DashingLineEffect::Create(GrPrimitiveEdgeType edgeType,
806 const DashInfo& info,
807 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000808 if (info.fCount != 2) {
809 return NULL;
810 }
811
bsalomon55fad7a2014-07-08 07:34:20 -0700812 return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000813}
814
815DashingLineEffect::~DashingLineEffect() {}
816
egdaniel605dd0f2014-11-12 08:35:25 -0800817void DashingLineEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700818 inout->mulByUnknownAlpha();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000819}
820
joshualittb0a8a372014-09-23 09:50:21 -0700821const GrBackendGeometryProcessorFactory& DashingLineEffect::getFactory() const {
822 return GrTBackendGeometryProcessorFactory<DashingLineEffect>::getInstance();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000823}
824
joshualittb0a8a372014-09-23 09:50:21 -0700825DashingLineEffect::DashingLineEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700826 SkScalar strokeWidth)
joshualitt249af152014-09-15 11:41:13 -0700827 : fEdgeType(edgeType)
828 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
829 kVec2f_GrSLType,
830 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000831 SkScalar onLen = info.fIntervals[0];
832 SkScalar offLen = info.fIntervals[1];
833 SkScalar halfOffLen = SkScalarHalf(offLen);
834 SkScalar halfStroke = SkScalarHalf(strokeWidth);
835 fIntervalLength = onLen + offLen;
836 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000837}
838
bsalomon0e08fc12014-10-15 08:19:04 -0700839bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700840 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000841 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000842 fRect == de.fRect &&
843 fIntervalLength == de.fIntervalLength);
844}
845
joshualittb0a8a372014-09-23 09:50:21 -0700846GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000847
joshualittb0a8a372014-09-23 09:50:21 -0700848GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
849 GrContext*,
850 const GrDrawTargetCaps& caps,
851 GrTexture*[]) {
852 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
853 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000854 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
855 DashInfo info;
856 info.fCount = 2;
857 SkAutoTArray<SkScalar> intervals(info.fCount);
858 info.fIntervals = intervals.get();
859 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
860 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
861 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
862
joshualittb0a8a372014-09-23 09:50:21 -0700863 return DashingLineEffect::Create(edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000864}
865
866//////////////////////////////////////////////////////////////////////////////
867
joshualittb0a8a372014-09-23 09:50:21 -0700868GrGeometryProcessor* GrDashingEffect::Create(GrPrimitiveEdgeType edgeType,
869 const SkPathEffect::DashInfo& info,
870 SkScalar strokeWidth,
871 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700872 switch (cap) {
873 case GrDashingEffect::kRound_DashCap:
874 return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth));
875 case GrDashingEffect::kNonRound_DashCap:
876 return DashingLineEffect::Create(edgeType, info, strokeWidth);
877 default:
878 SkFAIL("Unexpected dashed cap.");
879 }
880 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000881}