blob: bee7b87a41c27383bc5df156a7503d8b7693a287 [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"
joshualitt47bb3822014-10-07 16:43:25 -070013#include "gl/builders/GrGLProgramBuilder.h"
joshualittb0a8a372014-09-23 09:50:21 -070014#include "gl/GrGLProcessor.h"
joshualitt249af152014-09-15 11:41:13 -070015#include "gl/GrGLGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000016#include "gl/GrGLSL.h"
17#include "GrContext.h"
18#include "GrCoordTransform.h"
egdaniele61c4112014-06-12 10:24:21 -070019#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000020#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080021#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070022#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070023#include "GrGpu.h"
24#include "GrStrokeInfo.h"
joshualittb0a8a372014-09-23 09:50:21 -070025#include "GrTBackendProcessorFactory.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000026#include "SkGr.h"
27
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
joshualitt249af152014-09-15 11:41:13 -070073extern const GrVertexAttrib gDashLineNoAAVertexAttribs[] = {
74 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }
75};
76
egdaniele61c4112014-06-12 10:24:21 -070077extern const GrVertexAttrib gDashLineVertexAttribs[] = {
78 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
joshualittb0a8a372014-09-23 09:50:21 -070079 { kVec2f_GrVertexAttribType, sizeof(SkPoint), kGeometryProcessor_GrVertexAttribBinding },
egdaniele61c4112014-06-12 10:24:21 -070080};
81
82};
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000083static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
84 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
85 SkVector vecSrc = pts[1] - pts[0];
86 SkScalar magSrc = vecSrc.length();
87 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
88 vecSrc.scale(invSrc);
89
90 SkVector vecSrcPerp;
91 vecSrc.rotateCW(&vecSrcPerp);
92 viewMatrix.mapVectors(&vecSrc, 1);
93 viewMatrix.mapVectors(&vecSrcPerp, 1);
94
95 // parallelScale tells how much to scale along the line parallel to the dash line
96 // perpScale tells how much to scale in the direction perpendicular to the dash line
97 *parallelScale = vecSrc.length();
98 *perpScale = vecSrcPerp.length();
99}
100
101// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
102// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
103static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
104 SkVector vec = pts[1] - pts[0];
105 SkScalar mag = vec.length();
106 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
107
108 vec.scale(inv);
109 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
110 if (ptsRot) {
111 rotMatrix->mapPoints(ptsRot, pts, 2);
112 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
113 ptsRot[1].fY = pts[0].fY;
114 }
115}
116
117// Assumes phase < sum of all intervals
118static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) {
119 SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
120 if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) {
121 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
122 return srcIntervalLen - info.fPhase;
123 }
124 return 0;
125}
126
egdaniele61c4112014-06-12 10:24:21 -0700127static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
128 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000129 if (pts[1].fX <= pts[0].fX) {
130 return 0;
131 }
132 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
133 SkScalar totalLen = pts[1].fX - pts[0].fX;
134 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
135 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700136 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000137 temp = SkScalarDiv(*endingInt, srcIntervalLen);
138 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
139 if (0 == *endingInt) {
140 *endingInt = srcIntervalLen;
141 }
142 if (*endingInt > info.fIntervals[0]) {
143 if (0 == info.fIntervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000144 *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 +0000145 }
146 return *endingInt - info.fIntervals[0];
147 }
148 return 0;
149}
150
egdaniele61c4112014-06-12 10:24:21 -0700151static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
152 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000153
egdaniele61c4112014-06-12 10:24:21 -0700154 SkScalar startDashX = offset - bloat;
155 SkScalar endDashX = offset + len + bloat;
156 SkScalar startDashY = -stroke - bloat;
157 SkScalar endDashY = stroke + bloat;
158 verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
159 verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
160 verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
161 verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
162
163 verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
164 verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
165 verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
166 verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
167
168 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
169}
170
171
172bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint,
173 const GrStrokeInfo& strokeInfo, GrGpu* gpu,
174 GrDrawTarget* target, const SkMatrix& vm) {
175
176 if (!can_fast_path_dash(pts, strokeInfo, *target, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000177 return false;
178 }
179
egdaniele61c4112014-06-12 10:24:21 -0700180 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000181
egdaniele61c4112014-06-12 10:24:21 -0700182 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000183
egdaniele61c4112014-06-12 10:24:21 -0700184 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000185
186 // the phase should be normalized to be [0, sum of all intervals)
187 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
188
egdaniele61c4112014-06-12 10:24:21 -0700189 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000190
191 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
192 SkMatrix srcRotInv;
193 SkPoint ptsRot[2];
194 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700195 SkMatrix rotMatrix;
196 align_to_x_axis(pts, &rotMatrix, ptsRot);
197 if(!rotMatrix.invert(&srcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700198 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000199 return false;
200 }
201 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000202 srcRotInv.reset();
203 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
204 }
205
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000206 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000207
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000208 // Scale corrections of intervals and stroke from view matrix
209 SkScalar parallelScale;
210 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700211 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000212
egdanielf767e792014-07-02 06:21:32 -0700213 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000214
215 // We always want to at least stroke out half a pixel on each side in device space
216 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700217 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000218
egdaniele61c4112014-06-12 10:24:21 -0700219 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000220 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700221 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000222 } else {
egdaniele61c4112014-06-12 10:24:21 -0700223 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000224 }
225
egdaniele61c4112014-06-12 10:24:21 -0700226 SkScalar startAdj = 0;
227
228 SkMatrix combinedMatrix = srcRotInv;
229 combinedMatrix.postConcat(vm);
230
231 bool lineDone = false;
232 SkRect startRect;
233 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000234 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
235 // draw it separately here and adjust our start point accordingly
236 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700237 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 SkPoint startPts[2];
239 startPts[0] = ptsRot[0];
240 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700241 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000242 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700244 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000245
egdaniele61c4112014-06-12 10:24:21 -0700246 hasStartRect = true;
247 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000248 }
249 }
250
251 // adjustments for start and end of bounding rect so we only draw dash intervals
252 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700253 startAdj += calc_start_adjustment(info);
254 if (startAdj != 0) {
255 ptsRot[0].fX += startAdj;
256 srcPhase = 0;
257 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000258 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700259 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
260 ptsRot[1].fX -= endAdj;
261 if (ptsRot[0].fX >= ptsRot[1].fX) {
262 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000263 }
264
egdaniele61c4112014-06-12 10:24:21 -0700265 SkRect endRect;
266 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000267 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
268 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700269 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000270 // If we adjusted the end then we will not be drawing a partial dash at the end.
271 // If we didn't adjust the end point then we just need to make sure the ending
272 // dash isn't a full dash
273 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274 SkPoint endPts[2];
275 endPts[1] = ptsRot[1];
276 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000277 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000278
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000279 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700280 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000281
egdaniele61c4112014-06-12 10:24:21 -0700282 hasEndRect = true;
283 endAdj = endingInterval + info.fIntervals[1];
284
285 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000286 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700287 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000288 }
289 }
290 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000291
egdaniele61c4112014-06-12 10:24:21 -0700292 if (startAdj != 0) {
293 srcPhase = 0;
294 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000295
egdaniele61c4112014-06-12 10:24:21 -0700296 // Change the dashing info from src space into device space
297 SkScalar devIntervals[2];
298 devIntervals[0] = info.fIntervals[0] * parallelScale;
299 devIntervals[1] = info.fIntervals[1] * parallelScale;
300 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000301 SkScalar strokeWidth = srcStrokeWidth * perpScale;
302
303 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
304 strokeWidth = 1.f;
305 }
306
egdaniele61c4112014-06-12 10:24:21 -0700307 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308
309 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
310 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700311 devIntervals[0] += strokeWidth;
312 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000313 }
egdaniele61c4112014-06-12 10:24:21 -0700314 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000315
egdaniele61c4112014-06-12 10:24:21 -0700316 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
317 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
318
319 SkScalar devBloat = useAA ? 0.5f : 0.f;
320
321 GrDrawState* drawState = target->drawState();
322 if (devIntervals[1] <= 0.f && useAA) {
323 // Case when we end up drawing a solid AA rect
324 // Reset the start rect to draw this single solid rect
325 // but it requires to upload a new intervals uniform so we can mimic
326 // one giant dash
327 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
328 ptsRot[1].fX += hasEndRect ? endAdj : 0;
329 startRect.set(ptsRot, 2);
330 startRect.outset(strokeAdj, halfSrcStroke);
331 hasStartRect = true;
332 hasEndRect = false;
333 lineDone = true;
334
335 SkPoint devicePts[2];
336 vm.mapPoints(devicePts, ptsRot, 2);
337 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
338 if (hasCap) {
339 lineLength += 2.f * halfDevStroke;
340 }
341 devIntervals[0] = lineLength;
342 }
343 if (devIntervals[1] > 0.f || useAA) {
344 SkPathEffect::DashInfo devInfo;
345 devInfo.fPhase = devPhase;
346 devInfo.fCount = 2;
347 devInfo.fIntervals = devIntervals;
joshualittb0a8a372014-09-23 09:50:21 -0700348 GrPrimitiveEdgeType edgeType= useAA ? kFillAA_GrProcessorEdgeType :
349 kFillBW_GrProcessorEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700350 bool isRoundCap = SkPaint::kRound_Cap == cap;
351 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
352 GrDashingEffect::kNonRound_DashCap;
joshualittbd769d02014-09-04 08:56:46 -0700353 drawState->setGeometryProcessor(
joshualittb0a8a372014-09-23 09:50:21 -0700354 GrDashingEffect::Create(edgeType, devInfo, strokeWidth, capType))->unref();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355
joshualitt249af152014-09-15 11:41:13 -0700356 // Set up the vertex data for the line and start/end dashes
357 drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs),
358 sizeof(DashLineVertex));
359 } else {
360 // Set up the vertex data for the line and start/end dashes
361 drawState->setVertexAttribs<gDashLineNoAAVertexAttribs>(
362 SK_ARRAY_COUNT(gDashLineNoAAVertexAttribs), sizeof(DashLineVertex));
363 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000364
egdaniele61c4112014-06-12 10:24:21 -0700365 int totalRectCnt = 0;
366
367 totalRectCnt += !lineDone ? 1 : 0;
368 totalRectCnt += hasStartRect ? 1 : 0;
369 totalRectCnt += hasEndRect ? 1 : 0;
370
371 GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0);
372 if (!geo.succeeded()) {
tfarina38406c82014-10-31 07:11:12 -0700373 SkDebugf("Failed to get space for vertices!\n");
egdaniele61c4112014-06-12 10:24:21 -0700374 return false;
375 }
376
377 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
378
379 int curVIdx = 0;
380
egdanielf767e792014-07-02 06:21:32 -0700381 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
382 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
383 startOffset -= halfDevStroke;
384 }
385
egdaniele61c4112014-06-12 10:24:21 -0700386 // Draw interior part of dashed line
387 if (!lineDone) {
388 SkPoint devicePts[2];
389 vm.mapPoints(devicePts, ptsRot, 2);
390 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
391 if (hasCap) {
392 lineLength += 2.f * halfDevStroke;
393 }
394
395 SkRect bounds;
396 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
397 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
398 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
399 lineLength, halfDevStroke);
400 curVIdx += 4;
401 }
402
403 if (hasStartRect) {
404 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
405 startRect.outset(bloatX, bloatY);
406 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
407 devIntervals[0], halfDevStroke);
408 curVIdx += 4;
409 }
410
411 if (hasEndRect) {
412 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
413 endRect.outset(bloatX, bloatY);
414 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
415 devIntervals[0], halfDevStroke);
416 }
417
418 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
419 target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
420 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000421 return true;
422}
423
424//////////////////////////////////////////////////////////////////////////////
425
egdanielf767e792014-07-02 06:21:32 -0700426class GLDashingCircleEffect;
427/*
428 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
429 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
430 * Both of the previous two parameters are in device space. This effect also requires the setting of
431 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
432 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
433 * transform the line to be horizontal, with the start of line at the origin then shifted to the
434 * right by half the off interval. The line then goes in the positive x direction.
435 */
joshualitt249af152014-09-15 11:41:13 -0700436class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700437public:
438 typedef SkPathEffect::DashInfo DashInfo;
439
joshualittb0a8a372014-09-23 09:50:21 -0700440 static GrGeometryProcessor* Create(GrPrimitiveEdgeType edgeType,
441 const DashInfo& info,
442 SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700443
444 virtual ~DashingCircleEffect();
445
446 static const char* Name() { return "DashingCircleEffect"; }
447
joshualitt249af152014-09-15 11:41:13 -0700448 const GrShaderVar& inCoord() const { return fInCoord; }
449
joshualittb0a8a372014-09-23 09:50:21 -0700450 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
egdanielf767e792014-07-02 06:21:32 -0700451
452 SkScalar getRadius() const { return fRadius; }
453
454 SkScalar getCenterX() const { return fCenterX; }
455
456 SkScalar getIntervalLength() const { return fIntervalLength; }
457
joshualittb0a8a372014-09-23 09:50:21 -0700458 typedef GLDashingCircleEffect GLProcessor;
egdanielf767e792014-07-02 06:21:32 -0700459
joshualittb0a8a372014-09-23 09:50:21 -0700460 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700461
462private:
joshualittb0a8a372014-09-23 09:50:21 -0700463 DashingCircleEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info, SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700464
bsalomon0e08fc12014-10-15 08:19:04 -0700465 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700466
egdaniel605dd0f2014-11-12 08:35:25 -0800467 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700468
joshualittb0a8a372014-09-23 09:50:21 -0700469 GrPrimitiveEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700470 const GrShaderVar& fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700471 SkScalar fIntervalLength;
472 SkScalar fRadius;
473 SkScalar fCenterX;
474
joshualittb0a8a372014-09-23 09:50:21 -0700475 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700476
joshualitt249af152014-09-15 11:41:13 -0700477 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700478};
479
480//////////////////////////////////////////////////////////////////////////////
481
joshualitt249af152014-09-15 11:41:13 -0700482class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700483public:
joshualittb0a8a372014-09-23 09:50:21 -0700484 GLDashingCircleEffect(const GrBackendProcessorFactory&, const GrProcessor&);
egdanielf767e792014-07-02 06:21:32 -0700485
joshualittc369e7c2014-10-22 10:56:26 -0700486 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700487
joshualittb0a8a372014-09-23 09:50:21 -0700488 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700489
joshualittb0a8a372014-09-23 09:50:21 -0700490 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700491
492private:
kkinnunen7510b222014-07-30 00:04:16 -0700493 GrGLProgramDataManager::UniformHandle fParamUniform;
494 SkScalar fPrevRadius;
495 SkScalar fPrevCenterX;
496 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700497 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700498};
499
joshualittb0a8a372014-09-23 09:50:21 -0700500GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendProcessorFactory& factory,
501 const GrProcessor&)
egdanielf767e792014-07-02 06:21:32 -0700502 : INHERITED (factory) {
503 fPrevRadius = SK_ScalarMin;
504 fPrevCenterX = SK_ScalarMin;
505 fPrevIntervalLength = SK_ScalarMax;
506}
507
joshualittc369e7c2014-10-22 10:56:26 -0700508void GLDashingCircleEffect::emitCode(const EmitArgs& args) {
509 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700510 const char *paramName;
511 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
512 // the total interval length of the dash.
joshualittc369e7c2014-10-22 10:56:26 -0700513 fParamUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
514 kVec3f_GrSLType,
515 "params",
516 &paramName);
egdanielf767e792014-07-02 06:21:32 -0700517
joshualitt74077b92014-10-24 11:26:03 -0700518 GrGLVertToFrag v(kVec2f_GrSLType);
519 args.fPB->addVarying("Coord", &v);
joshualitt30ba4362014-08-21 20:18:45 -0700520
joshualittc369e7c2014-10-22 10:56:26 -0700521 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700522 vsBuilder->codeAppendf("\t%s = %s;\n", v.vsOut(), dce.inCoord().c_str());
egdanielf767e792014-07-02 06:21:32 -0700523
joshualitt4973d9d2014-11-08 09:24:25 -0800524 // setup position varying
525 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
526 vsBuilder->inPosition());
527
egdanielf767e792014-07-02 06:21:32 -0700528 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700529 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700530 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
joshualitt74077b92014-10-24 11:26:03 -0700531 v.fsIn(), v.fsIn(), paramName, paramName);
532 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualitt30ba4362014-08-21 20:18:45 -0700533 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
534 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
joshualittb0a8a372014-09-23 09:50:21 -0700535 if (GrProcessorEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700536 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
537 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
538 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700539 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700540 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
541 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700542 }
joshualittc369e7c2014-10-22 10:56:26 -0700543 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutput,
544 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("alpha")).c_str());
egdanielf767e792014-07-02 06:21:32 -0700545}
546
joshualittb0a8a372014-09-23 09:50:21 -0700547void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman
548 , const GrProcessor& processor) {
549 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700550 SkScalar radius = dce.getRadius();
551 SkScalar centerX = dce.getCenterX();
552 SkScalar intervalLength = dce.getIntervalLength();
553 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700554 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700555 fPrevRadius = radius;
556 fPrevCenterX = centerX;
557 fPrevIntervalLength = intervalLength;
558 }
559}
560
joshualittb0a8a372014-09-23 09:50:21 -0700561void GLDashingCircleEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
562 GrProcessorKeyBuilder* b) {
563 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700564 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700565}
566
567//////////////////////////////////////////////////////////////////////////////
568
joshualittb0a8a372014-09-23 09:50:21 -0700569GrGeometryProcessor* DashingCircleEffect::Create(GrPrimitiveEdgeType edgeType, const DashInfo& info,
570 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700571 if (info.fCount != 2 || info.fIntervals[0] != 0) {
572 return NULL;
573 }
574
bsalomon55fad7a2014-07-08 07:34:20 -0700575 return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700576}
577
578DashingCircleEffect::~DashingCircleEffect() {}
579
egdaniel605dd0f2014-11-12 08:35:25 -0800580void DashingCircleEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700581 inout->mulByUnknownAlpha();
egdanielf767e792014-07-02 06:21:32 -0700582}
583
joshualittb0a8a372014-09-23 09:50:21 -0700584const GrBackendGeometryProcessorFactory& DashingCircleEffect::getFactory() const {
585 return GrTBackendGeometryProcessorFactory<DashingCircleEffect>::getInstance();
egdanielf767e792014-07-02 06:21:32 -0700586}
587
joshualittb0a8a372014-09-23 09:50:21 -0700588DashingCircleEffect::DashingCircleEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info,
egdanielf767e792014-07-02 06:21:32 -0700589 SkScalar radius)
joshualitt249af152014-09-15 11:41:13 -0700590 : fEdgeType(edgeType)
591 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
592 kVec2f_GrSLType,
593 GrShaderVar::kAttribute_TypeModifier))) {
egdanielf767e792014-07-02 06:21:32 -0700594 SkScalar onLen = info.fIntervals[0];
595 SkScalar offLen = info.fIntervals[1];
596 fIntervalLength = onLen + offLen;
597 fRadius = radius;
598 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700599}
600
bsalomon0e08fc12014-10-15 08:19:04 -0700601bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700602 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700603 return (fEdgeType == dce.fEdgeType &&
604 fIntervalLength == dce.fIntervalLength &&
605 fRadius == dce.fRadius &&
606 fCenterX == dce.fCenterX);
607}
608
joshualittb0a8a372014-09-23 09:50:21 -0700609GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700610
joshualittb0a8a372014-09-23 09:50:21 -0700611GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
612 GrContext*,
613 const GrDrawTargetCaps& caps,
614 GrTexture*[]) {
615 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
616 kGrProcessorEdgeTypeCnt));
egdanielf767e792014-07-02 06:21:32 -0700617 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
618 DashInfo info;
619 info.fCount = 2;
620 SkAutoTArray<SkScalar> intervals(info.fCount);
621 info.fIntervals = intervals.get();
622 info.fIntervals[0] = 0;
623 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
624 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
625
joshualittb0a8a372014-09-23 09:50:21 -0700626 return DashingCircleEffect::Create(edgeType, info, strokeWidth);
egdanielf767e792014-07-02 06:21:32 -0700627}
628
629//////////////////////////////////////////////////////////////////////////////
630
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000631class GLDashingLineEffect;
632
egdanielf767e792014-07-02 06:21:32 -0700633/*
634 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
635 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
636 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
637 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
638 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
639 * line at the origin then shifted to the right by half the off interval. The line then goes in the
640 * positive x direction.
641 */
joshualitt249af152014-09-15 11:41:13 -0700642class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000643public:
644 typedef SkPathEffect::DashInfo DashInfo;
645
joshualittb0a8a372014-09-23 09:50:21 -0700646 static GrGeometryProcessor* Create(GrPrimitiveEdgeType edgeType,
647 const DashInfo& info,
648 SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000649
650 virtual ~DashingLineEffect();
651
652 static const char* Name() { return "DashingEffect"; }
653
joshualitt249af152014-09-15 11:41:13 -0700654 const GrShaderVar& inCoord() const { return fInCoord; }
655
joshualittb0a8a372014-09-23 09:50:21 -0700656 GrPrimitiveEdgeType getEdgeType() const { return fEdgeType; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000657
658 const SkRect& getRect() const { return fRect; }
659
660 SkScalar getIntervalLength() const { return fIntervalLength; }
661
joshualittb0a8a372014-09-23 09:50:21 -0700662 typedef GLDashingLineEffect GLProcessor;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000663
joshualittb0a8a372014-09-23 09:50:21 -0700664 virtual const GrBackendGeometryProcessorFactory& getFactory() const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000665
666private:
joshualittb0a8a372014-09-23 09:50:21 -0700667 DashingLineEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000668
bsalomon0e08fc12014-10-15 08:19:04 -0700669 virtual bool onIsEqual(const GrGeometryProcessor& other) const SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000670
egdaniel605dd0f2014-11-12 08:35:25 -0800671 virtual void onComputeInvariantOutput(GrInvariantOutput* inout) const SK_OVERRIDE;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700672
joshualittb0a8a372014-09-23 09:50:21 -0700673 GrPrimitiveEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700674 const GrShaderVar& fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000675 SkRect fRect;
676 SkScalar fIntervalLength;
677
joshualittb0a8a372014-09-23 09:50:21 -0700678 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000679
joshualitt249af152014-09-15 11:41:13 -0700680 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000681};
682
683//////////////////////////////////////////////////////////////////////////////
684
joshualitt249af152014-09-15 11:41:13 -0700685class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000686public:
joshualittb0a8a372014-09-23 09:50:21 -0700687 GLDashingLineEffect(const GrBackendProcessorFactory&, const GrProcessor&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000688
joshualittc369e7c2014-10-22 10:56:26 -0700689 virtual void emitCode(const EmitArgs&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000690
joshualittb0a8a372014-09-23 09:50:21 -0700691 static inline void GenKey(const GrProcessor&, const GrGLCaps&, GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000692
joshualittb0a8a372014-09-23 09:50:21 -0700693 virtual void setData(const GrGLProgramDataManager&, const GrProcessor&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000694
695private:
kkinnunen7510b222014-07-30 00:04:16 -0700696 GrGLProgramDataManager::UniformHandle fRectUniform;
697 GrGLProgramDataManager::UniformHandle fIntervalUniform;
698 SkRect fPrevRect;
699 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700700 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000701};
702
joshualittb0a8a372014-09-23 09:50:21 -0700703GLDashingLineEffect::GLDashingLineEffect(const GrBackendProcessorFactory& factory,
704 const GrProcessor&)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000705 : INHERITED (factory) {
706 fPrevRect.fLeft = SK_ScalarNaN;
707 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708}
709
joshualittc369e7c2014-10-22 10:56:26 -0700710void GLDashingLineEffect::emitCode(const EmitArgs& args) {
711 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000712 const char *rectName;
713 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
714 // respectively.
joshualittc369e7c2014-10-22 10:56:26 -0700715 fRectUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000716 kVec4f_GrSLType,
717 "rect",
718 &rectName);
719 const char *intervalName;
720 // The interval uniform's refers to the total length of the interval (on + off)
joshualittc369e7c2014-10-22 10:56:26 -0700721 fIntervalUniform = args.fPB->addUniform(GrGLProgramBuilder::kFragment_Visibility,
722 kFloat_GrSLType,
723 "interval",
724 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700725
joshualitt74077b92014-10-24 11:26:03 -0700726 GrGLVertToFrag v(kVec2f_GrSLType);
727 args.fPB->addVarying("Coord", &v);
joshualittc369e7c2014-10-22 10:56:26 -0700728 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt74077b92014-10-24 11:26:03 -0700729 vsBuilder->codeAppendf("\t%s = %s;\n", v.vsOut(), de.inCoord().c_str());
egdaniele61c4112014-06-12 10:24:21 -0700730
joshualitt4973d9d2014-11-08 09:24:25 -0800731 // setup position varying
732 vsBuilder->codeAppendf("%s = %s * vec3(%s, 1);", vsBuilder->glPosition(), vsBuilder->uViewM(),
733 vsBuilder->inPosition());
734
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000735 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -0700736 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt30ba4362014-08-21 20:18:45 -0700737 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
joshualitt74077b92014-10-24 11:26:03 -0700738 v.fsIn(), v.fsIn(), intervalName, intervalName);
739 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", v.fsIn());
joshualittb0a8a372014-09-23 09:50:21 -0700740 if (GrProcessorEdgeTypeIsAA(de.getEdgeType())) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000741 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
742 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700743 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
744 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
745 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
746 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
747 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000748 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
749 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700750 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 +0000751 } else {
752 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700753 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
754 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
755 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 +0000756 }
joshualittc369e7c2014-10-22 10:56:26 -0700757 fsBuilder->codeAppendf("\t\t%s = %s;\n", args.fOutput,
758 (GrGLSLExpr4(args.fInput) * GrGLSLExpr1("alpha")).c_str());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000759}
760
joshualittb0a8a372014-09-23 09:50:21 -0700761void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
762 const GrProcessor& processor) {
763 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000764 const SkRect& rect = de.getRect();
765 SkScalar intervalLength = de.getIntervalLength();
766 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700767 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
768 rect.fRight - 0.5f, rect.fBottom - 0.5f);
769 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000770 fPrevRect = rect;
771 fPrevIntervalLength = intervalLength;
772 }
773}
774
joshualittb0a8a372014-09-23 09:50:21 -0700775void GLDashingLineEffect::GenKey(const GrProcessor& processor, const GrGLCaps&,
776 GrProcessorKeyBuilder* b) {
777 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700778 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000779}
780
781//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000782
joshualittb0a8a372014-09-23 09:50:21 -0700783GrGeometryProcessor* DashingLineEffect::Create(GrPrimitiveEdgeType edgeType,
784 const DashInfo& info,
785 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000786 if (info.fCount != 2) {
787 return NULL;
788 }
789
bsalomon55fad7a2014-07-08 07:34:20 -0700790 return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000791}
792
793DashingLineEffect::~DashingLineEffect() {}
794
egdaniel605dd0f2014-11-12 08:35:25 -0800795void DashingLineEffect::onComputeInvariantOutput(GrInvariantOutput* inout) const {
egdanielccb2e382014-10-13 12:53:46 -0700796 inout->mulByUnknownAlpha();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000797}
798
joshualittb0a8a372014-09-23 09:50:21 -0700799const GrBackendGeometryProcessorFactory& DashingLineEffect::getFactory() const {
800 return GrTBackendGeometryProcessorFactory<DashingLineEffect>::getInstance();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000801}
802
joshualittb0a8a372014-09-23 09:50:21 -0700803DashingLineEffect::DashingLineEffect(GrPrimitiveEdgeType edgeType, const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700804 SkScalar strokeWidth)
joshualitt249af152014-09-15 11:41:13 -0700805 : fEdgeType(edgeType)
806 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
807 kVec2f_GrSLType,
808 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000809 SkScalar onLen = info.fIntervals[0];
810 SkScalar offLen = info.fIntervals[1];
811 SkScalar halfOffLen = SkScalarHalf(offLen);
812 SkScalar halfStroke = SkScalarHalf(strokeWidth);
813 fIntervalLength = onLen + offLen;
814 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000815}
816
bsalomon0e08fc12014-10-15 08:19:04 -0700817bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700818 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000819 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000820 fRect == de.fRect &&
821 fIntervalLength == de.fIntervalLength);
822}
823
joshualittb0a8a372014-09-23 09:50:21 -0700824GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000825
joshualittb0a8a372014-09-23 09:50:21 -0700826GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
827 GrContext*,
828 const GrDrawTargetCaps& caps,
829 GrTexture*[]) {
830 GrPrimitiveEdgeType edgeType = static_cast<GrPrimitiveEdgeType>(random->nextULessThan(
831 kGrProcessorEdgeTypeCnt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000832 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
833 DashInfo info;
834 info.fCount = 2;
835 SkAutoTArray<SkScalar> intervals(info.fCount);
836 info.fIntervals = intervals.get();
837 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
838 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
839 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
840
joshualittb0a8a372014-09-23 09:50:21 -0700841 return DashingLineEffect::Create(edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000842}
843
844//////////////////////////////////////////////////////////////////////////////
845
joshualittb0a8a372014-09-23 09:50:21 -0700846GrGeometryProcessor* GrDashingEffect::Create(GrPrimitiveEdgeType edgeType,
847 const SkPathEffect::DashInfo& info,
848 SkScalar strokeWidth,
849 GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700850 switch (cap) {
851 case GrDashingEffect::kRound_DashCap:
852 return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth));
853 case GrDashingEffect::kNonRound_DashCap:
854 return DashingLineEffect::Create(edgeType, info, strokeWidth);
855 default:
856 SkFAIL("Unexpected dashed cap.");
857 }
858 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000859}