blob: 38a2d4e4babe48b65f3aab3f3ad2fa7023c331b3 [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
joshualitt249af152014-09-15 11:41:13 -070012#include "effects/GrGeometryProcessor.h"
joshualitt408d6122014-09-17 07:00:35 -070013#include "gl/builders/GrGLFullProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000014#include "gl/GrGLEffect.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"
21#include "GrEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070022#include "GrGpu.h"
23#include "GrStrokeInfo.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000024#include "GrTBackendEffectFactory.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000025#include "SkGr.h"
26
27///////////////////////////////////////////////////////////////////////////////
28
egdaniele61c4112014-06-12 10:24:21 -070029// Returns whether or not the gpu can fast path the dash line effect.
30static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
31 const GrDrawTarget& target, const SkMatrix& viewMatrix) {
32 if (target.getDrawState().getRenderTarget()->isMultisampled()) {
33 return false;
34 }
35
36 // Pts must be either horizontal or vertical in src space
37 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
38 return false;
39 }
40
41 // May be able to relax this to include skew. As of now cannot do perspective
42 // because of the non uniform scaling of bloating a rect
43 if (!viewMatrix.preservesRightAngles()) {
44 return false;
45 }
46
47 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
48 return false;
49 }
50
51 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
52 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
53 return false;
54 }
55
56 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
57 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070058 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070059 return false;
60 }
61
62 return true;
63}
64
65namespace {
66
67struct DashLineVertex {
68 SkPoint fPos;
69 SkPoint fDashPos;
70};
71
joshualitt249af152014-09-15 11:41:13 -070072extern const GrVertexAttrib gDashLineNoAAVertexAttribs[] = {
73 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }
74};
75
egdaniele61c4112014-06-12 10:24:21 -070076extern const GrVertexAttrib gDashLineVertexAttribs[] = {
77 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
78 { kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding },
79};
80
81};
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000082static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
83 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
84 SkVector vecSrc = pts[1] - pts[0];
85 SkScalar magSrc = vecSrc.length();
86 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
87 vecSrc.scale(invSrc);
88
89 SkVector vecSrcPerp;
90 vecSrc.rotateCW(&vecSrcPerp);
91 viewMatrix.mapVectors(&vecSrc, 1);
92 viewMatrix.mapVectors(&vecSrcPerp, 1);
93
94 // parallelScale tells how much to scale along the line parallel to the dash line
95 // perpScale tells how much to scale in the direction perpendicular to the dash line
96 *parallelScale = vecSrc.length();
97 *perpScale = vecSrcPerp.length();
98}
99
100// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
101// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
102static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
103 SkVector vec = pts[1] - pts[0];
104 SkScalar mag = vec.length();
105 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
106
107 vec.scale(inv);
108 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
109 if (ptsRot) {
110 rotMatrix->mapPoints(ptsRot, pts, 2);
111 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
112 ptsRot[1].fY = pts[0].fY;
113 }
114}
115
116// Assumes phase < sum of all intervals
117static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) {
118 SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
119 if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) {
120 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
121 return srcIntervalLen - info.fPhase;
122 }
123 return 0;
124}
125
egdaniele61c4112014-06-12 10:24:21 -0700126static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
127 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000128 if (pts[1].fX <= pts[0].fX) {
129 return 0;
130 }
131 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
132 SkScalar totalLen = pts[1].fX - pts[0].fX;
133 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
134 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700135 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000136 temp = SkScalarDiv(*endingInt, srcIntervalLen);
137 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
138 if (0 == *endingInt) {
139 *endingInt = srcIntervalLen;
140 }
141 if (*endingInt > info.fIntervals[0]) {
142 if (0 == info.fIntervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000143 *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 +0000144 }
145 return *endingInt - info.fIntervals[0];
146 }
147 return 0;
148}
149
egdaniele61c4112014-06-12 10:24:21 -0700150static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
151 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000152
egdaniele61c4112014-06-12 10:24:21 -0700153 SkScalar startDashX = offset - bloat;
154 SkScalar endDashX = offset + len + bloat;
155 SkScalar startDashY = -stroke - bloat;
156 SkScalar endDashY = stroke + bloat;
157 verts[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
158 verts[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
159 verts[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
160 verts[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
161
162 verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
163 verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
164 verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
165 verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
166
167 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
168}
169
170
171bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint,
172 const GrStrokeInfo& strokeInfo, GrGpu* gpu,
173 GrDrawTarget* target, const SkMatrix& vm) {
174
175 if (!can_fast_path_dash(pts, strokeInfo, *target, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000176 return false;
177 }
178
egdaniele61c4112014-06-12 10:24:21 -0700179 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000180
egdaniele61c4112014-06-12 10:24:21 -0700181 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000182
egdaniele61c4112014-06-12 10:24:21 -0700183 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000184
185 // the phase should be normalized to be [0, sum of all intervals)
186 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
187
egdaniele61c4112014-06-12 10:24:21 -0700188 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000189
190 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
191 SkMatrix srcRotInv;
192 SkPoint ptsRot[2];
193 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700194 SkMatrix rotMatrix;
195 align_to_x_axis(pts, &rotMatrix, ptsRot);
196 if(!rotMatrix.invert(&srcRotInv)) {
197 GrPrintf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000198 return false;
199 }
200 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000201 srcRotInv.reset();
202 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
203 }
204
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000205 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000206
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207 // Scale corrections of intervals and stroke from view matrix
208 SkScalar parallelScale;
209 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700210 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000211
egdanielf767e792014-07-02 06:21:32 -0700212 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
214 // We always want to at least stroke out half a pixel on each side in device space
215 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700216 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000217
egdaniele61c4112014-06-12 10:24:21 -0700218 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700220 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000221 } else {
egdaniele61c4112014-06-12 10:24:21 -0700222 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000223 }
224
egdaniele61c4112014-06-12 10:24:21 -0700225 SkScalar startAdj = 0;
226
227 SkMatrix combinedMatrix = srcRotInv;
228 combinedMatrix.postConcat(vm);
229
230 bool lineDone = false;
231 SkRect startRect;
232 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
234 // draw it separately here and adjust our start point accordingly
235 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700236 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 SkPoint startPts[2];
238 startPts[0] = ptsRot[0];
239 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700240 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000241 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000242 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700243 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000244
egdaniele61c4112014-06-12 10:24:21 -0700245 hasStartRect = true;
246 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000247 }
248 }
249
250 // adjustments for start and end of bounding rect so we only draw dash intervals
251 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700252 startAdj += calc_start_adjustment(info);
253 if (startAdj != 0) {
254 ptsRot[0].fX += startAdj;
255 srcPhase = 0;
256 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000257 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700258 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
259 ptsRot[1].fX -= endAdj;
260 if (ptsRot[0].fX >= ptsRot[1].fX) {
261 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000262 }
263
egdaniele61c4112014-06-12 10:24:21 -0700264 SkRect endRect;
265 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000266 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
267 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700268 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000269 // If we adjusted the end then we will not be drawing a partial dash at the end.
270 // If we didn't adjust the end point then we just need to make sure the ending
271 // dash isn't a full dash
272 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273 SkPoint endPts[2];
274 endPts[1] = ptsRot[1];
275 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000276 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000277
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000278 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700279 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000280
egdaniele61c4112014-06-12 10:24:21 -0700281 hasEndRect = true;
282 endAdj = endingInterval + info.fIntervals[1];
283
284 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000285 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700286 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000287 }
288 }
289 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000290
egdaniele61c4112014-06-12 10:24:21 -0700291 if (startAdj != 0) {
292 srcPhase = 0;
293 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000294
egdaniele61c4112014-06-12 10:24:21 -0700295 // Change the dashing info from src space into device space
296 SkScalar devIntervals[2];
297 devIntervals[0] = info.fIntervals[0] * parallelScale;
298 devIntervals[1] = info.fIntervals[1] * parallelScale;
299 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000300 SkScalar strokeWidth = srcStrokeWidth * perpScale;
301
302 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
303 strokeWidth = 1.f;
304 }
305
egdaniele61c4112014-06-12 10:24:21 -0700306 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000307
308 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
309 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700310 devIntervals[0] += strokeWidth;
311 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000312 }
egdaniele61c4112014-06-12 10:24:21 -0700313 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000314
egdaniele61c4112014-06-12 10:24:21 -0700315 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
316 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
317
318 SkScalar devBloat = useAA ? 0.5f : 0.f;
319
320 GrDrawState* drawState = target->drawState();
321 if (devIntervals[1] <= 0.f && useAA) {
322 // Case when we end up drawing a solid AA rect
323 // Reset the start rect to draw this single solid rect
324 // but it requires to upload a new intervals uniform so we can mimic
325 // one giant dash
326 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
327 ptsRot[1].fX += hasEndRect ? endAdj : 0;
328 startRect.set(ptsRot, 2);
329 startRect.outset(strokeAdj, halfSrcStroke);
330 hasStartRect = true;
331 hasEndRect = false;
332 lineDone = true;
333
334 SkPoint devicePts[2];
335 vm.mapPoints(devicePts, ptsRot, 2);
336 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
337 if (hasCap) {
338 lineLength += 2.f * halfDevStroke;
339 }
340 devIntervals[0] = lineLength;
341 }
342 if (devIntervals[1] > 0.f || useAA) {
343 SkPathEffect::DashInfo devInfo;
344 devInfo.fPhase = devPhase;
345 devInfo.fCount = 2;
346 devInfo.fIntervals = devIntervals;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000347 GrEffectEdgeType edgeType= useAA ? kFillAA_GrEffectEdgeType :
348 kFillBW_GrEffectEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700349 bool isRoundCap = SkPaint::kRound_Cap == cap;
350 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
351 GrDashingEffect::kNonRound_DashCap;
joshualittbd769d02014-09-04 08:56:46 -0700352 drawState->setGeometryProcessor(
joshualitt249af152014-09-15 11:41:13 -0700353 GrDashingEffect::Create(edgeType, devInfo, strokeWidth, capType))->unref();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000354
joshualitt249af152014-09-15 11:41:13 -0700355 // Set up the vertex data for the line and start/end dashes
356 drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs),
357 sizeof(DashLineVertex));
358 } else {
359 // Set up the vertex data for the line and start/end dashes
360 drawState->setVertexAttribs<gDashLineNoAAVertexAttribs>(
361 SK_ARRAY_COUNT(gDashLineNoAAVertexAttribs), sizeof(DashLineVertex));
362 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000363
egdaniele61c4112014-06-12 10:24:21 -0700364 int totalRectCnt = 0;
365
366 totalRectCnt += !lineDone ? 1 : 0;
367 totalRectCnt += hasStartRect ? 1 : 0;
368 totalRectCnt += hasEndRect ? 1 : 0;
369
370 GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0);
371 if (!geo.succeeded()) {
372 GrPrintf("Failed to get space for vertices!\n");
373 return false;
374 }
375
376 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
377
378 int curVIdx = 0;
379
egdanielf767e792014-07-02 06:21:32 -0700380 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
381 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
382 startOffset -= halfDevStroke;
383 }
384
egdaniele61c4112014-06-12 10:24:21 -0700385 // Draw interior part of dashed line
386 if (!lineDone) {
387 SkPoint devicePts[2];
388 vm.mapPoints(devicePts, ptsRot, 2);
389 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
390 if (hasCap) {
391 lineLength += 2.f * halfDevStroke;
392 }
393
394 SkRect bounds;
395 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
396 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
397 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
398 lineLength, halfDevStroke);
399 curVIdx += 4;
400 }
401
402 if (hasStartRect) {
403 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
404 startRect.outset(bloatX, bloatY);
405 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
406 devIntervals[0], halfDevStroke);
407 curVIdx += 4;
408 }
409
410 if (hasEndRect) {
411 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
412 endRect.outset(bloatX, bloatY);
413 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
414 devIntervals[0], halfDevStroke);
415 }
416
417 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
418 target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
419 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000420 return true;
421}
422
423//////////////////////////////////////////////////////////////////////////////
424
egdanielf767e792014-07-02 06:21:32 -0700425class GLDashingCircleEffect;
426/*
427 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
428 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
429 * Both of the previous two parameters are in device space. This effect also requires the setting of
430 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
431 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
432 * transform the line to be horizontal, with the start of line at the origin then shifted to the
433 * right by half the off interval. The line then goes in the positive x direction.
434 */
joshualitt249af152014-09-15 11:41:13 -0700435class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700436public:
437 typedef SkPathEffect::DashInfo DashInfo;
438
bsalomon83d081a2014-07-08 09:56:10 -0700439 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700440
441 virtual ~DashingCircleEffect();
442
443 static const char* Name() { return "DashingCircleEffect"; }
444
joshualitt249af152014-09-15 11:41:13 -0700445 const GrShaderVar& inCoord() const { return fInCoord; }
446
egdanielf767e792014-07-02 06:21:32 -0700447 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
448
449 SkScalar getRadius() const { return fRadius; }
450
451 SkScalar getCenterX() const { return fCenterX; }
452
453 SkScalar getIntervalLength() const { return fIntervalLength; }
454
455 typedef GLDashingCircleEffect GLEffect;
456
457 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
458
459 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
460
461private:
462 DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
463
464 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
465
466 GrEffectEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700467 const GrShaderVar& fInCoord;
egdanielf767e792014-07-02 06:21:32 -0700468 SkScalar fIntervalLength;
469 SkScalar fRadius;
470 SkScalar fCenterX;
471
472 GR_DECLARE_EFFECT_TEST;
473
joshualitt249af152014-09-15 11:41:13 -0700474 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700475};
476
477//////////////////////////////////////////////////////////////////////////////
478
joshualitt249af152014-09-15 11:41:13 -0700479class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700480public:
joshualitt49586be2014-09-16 08:21:41 -0700481 GLDashingCircleEffect(const GrBackendEffectFactory&, const GrEffect&);
egdanielf767e792014-07-02 06:21:32 -0700482
joshualitt30ba4362014-08-21 20:18:45 -0700483 virtual void emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700484 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700485 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700486 const char* outputColor,
487 const char* inputColor,
488 const TransformedCoordsArray&,
489 const TextureSamplerArray&) SK_OVERRIDE;
490
joshualitt49586be2014-09-16 08:21:41 -0700491 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700492
joshualitt49586be2014-09-16 08:21:41 -0700493 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700494
495private:
kkinnunen7510b222014-07-30 00:04:16 -0700496 GrGLProgramDataManager::UniformHandle fParamUniform;
497 SkScalar fPrevRadius;
498 SkScalar fPrevCenterX;
499 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700500 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700501};
502
503GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendEffectFactory& factory,
joshualitt49586be2014-09-16 08:21:41 -0700504 const GrEffect& effect)
egdanielf767e792014-07-02 06:21:32 -0700505 : INHERITED (factory) {
506 fPrevRadius = SK_ScalarMin;
507 fPrevCenterX = SK_ScalarMin;
508 fPrevIntervalLength = SK_ScalarMax;
509}
510
joshualitt30ba4362014-08-21 20:18:45 -0700511void GLDashingCircleEffect::emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700512 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700513 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700514 const char* outputColor,
515 const char* inputColor,
516 const TransformedCoordsArray&,
517 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700518 const DashingCircleEffect& dce = effect.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700519 const char *paramName;
520 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
521 // the total interval length of the dash.
joshualitt30ba4362014-08-21 20:18:45 -0700522 fParamUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
egdanielf767e792014-07-02 06:21:32 -0700523 kVec3f_GrSLType,
524 "params",
525 &paramName);
526
527 const char *vsCoordName, *fsCoordName;
528 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700529
530 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700531 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, dce.inCoord().c_str());
egdanielf767e792014-07-02 06:21:32 -0700532
533 // transforms all points so that we can compare them to our test circle
joshualitt30ba4362014-08-21 20:18:45 -0700534 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
535 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
egdanielf767e792014-07-02 06:21:32 -0700536 fsCoordName, fsCoordName, paramName, paramName);
joshualitt30ba4362014-08-21 20:18:45 -0700537 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
538 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
539 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
egdanielf767e792014-07-02 06:21:32 -0700540 if (GrEffectEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700541 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
542 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
543 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700544 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700545 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
546 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700547 }
joshualitt30ba4362014-08-21 20:18:45 -0700548 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
egdanielf767e792014-07-02 06:21:32 -0700549 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
550}
551
joshualitt49586be2014-09-16 08:21:41 -0700552void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrEffect& effect) {
553 const DashingCircleEffect& dce = effect.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700554 SkScalar radius = dce.getRadius();
555 SkScalar centerX = dce.getCenterX();
556 SkScalar intervalLength = dce.getIntervalLength();
557 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700558 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700559 fPrevRadius = radius;
560 fPrevCenterX = centerX;
561 fPrevIntervalLength = intervalLength;
562 }
563}
564
joshualitt49586be2014-09-16 08:21:41 -0700565void GLDashingCircleEffect::GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700566 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700567 const DashingCircleEffect& dce = effect.cast<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700568 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700569}
570
571//////////////////////////////////////////////////////////////////////////////
572
bsalomon83d081a2014-07-08 09:56:10 -0700573GrEffect* DashingCircleEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
574 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700575 if (info.fCount != 2 || info.fIntervals[0] != 0) {
576 return NULL;
577 }
578
bsalomon55fad7a2014-07-08 07:34:20 -0700579 return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700580}
581
582DashingCircleEffect::~DashingCircleEffect() {}
583
584void DashingCircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
585 *validFlags = 0;
586}
587
588const GrBackendEffectFactory& DashingCircleEffect::getFactory() const {
589 return GrTBackendEffectFactory<DashingCircleEffect>::getInstance();
590}
591
592DashingCircleEffect::DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info,
593 SkScalar radius)
joshualitt249af152014-09-15 11:41:13 -0700594 : fEdgeType(edgeType)
595 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
596 kVec2f_GrSLType,
597 GrShaderVar::kAttribute_TypeModifier))) {
egdanielf767e792014-07-02 06:21:32 -0700598 SkScalar onLen = info.fIntervals[0];
599 SkScalar offLen = info.fIntervals[1];
600 fIntervalLength = onLen + offLen;
601 fRadius = radius;
602 fCenterX = SkScalarHalf(offLen);
egdanielf767e792014-07-02 06:21:32 -0700603}
604
605bool DashingCircleEffect::onIsEqual(const GrEffect& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700606 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
egdanielf767e792014-07-02 06:21:32 -0700607 return (fEdgeType == dce.fEdgeType &&
608 fIntervalLength == dce.fIntervalLength &&
609 fRadius == dce.fRadius &&
610 fCenterX == dce.fCenterX);
611}
612
613GR_DEFINE_EFFECT_TEST(DashingCircleEffect);
614
bsalomon83d081a2014-07-08 09:56:10 -0700615GrEffect* DashingCircleEffect::TestCreate(SkRandom* random,
616 GrContext*,
617 const GrDrawTargetCaps& caps,
618 GrTexture*[]) {
619 GrEffect* effect;
egdanielf767e792014-07-02 06:21:32 -0700620 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
621 kGrEffectEdgeTypeCnt));
622 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
623 DashInfo info;
624 info.fCount = 2;
625 SkAutoTArray<SkScalar> intervals(info.fCount);
626 info.fIntervals = intervals.get();
627 info.fIntervals[0] = 0;
628 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
629 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
630
631 effect = DashingCircleEffect::Create(edgeType, info, strokeWidth);
632 return effect;
633}
634
635//////////////////////////////////////////////////////////////////////////////
636
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000637class GLDashingLineEffect;
638
egdanielf767e792014-07-02 06:21:32 -0700639/*
640 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
641 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
642 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
643 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
644 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
645 * line at the origin then shifted to the right by half the off interval. The line then goes in the
646 * positive x direction.
647 */
joshualitt249af152014-09-15 11:41:13 -0700648class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000649public:
650 typedef SkPathEffect::DashInfo DashInfo;
651
bsalomon83d081a2014-07-08 09:56:10 -0700652 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000653
654 virtual ~DashingLineEffect();
655
656 static const char* Name() { return "DashingEffect"; }
657
joshualitt249af152014-09-15 11:41:13 -0700658 const GrShaderVar& inCoord() const { return fInCoord; }
659
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000660 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
661
662 const SkRect& getRect() const { return fRect; }
663
664 SkScalar getIntervalLength() const { return fIntervalLength; }
665
666 typedef GLDashingLineEffect GLEffect;
667
668 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
669
670 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
671
672private:
egdaniele61c4112014-06-12 10:24:21 -0700673 DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000674
675 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
676
677 GrEffectEdgeType fEdgeType;
joshualitt249af152014-09-15 11:41:13 -0700678 const GrShaderVar& fInCoord;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000679 SkRect fRect;
680 SkScalar fIntervalLength;
681
682 GR_DECLARE_EFFECT_TEST;
683
joshualitt249af152014-09-15 11:41:13 -0700684 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000685};
686
687//////////////////////////////////////////////////////////////////////////////
688
joshualitt249af152014-09-15 11:41:13 -0700689class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000690public:
joshualitt49586be2014-09-16 08:21:41 -0700691 GLDashingLineEffect(const GrBackendEffectFactory&, const GrEffect&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000692
joshualitt30ba4362014-08-21 20:18:45 -0700693 virtual void emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700694 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700695 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000696 const char* outputColor,
697 const char* inputColor,
698 const TransformedCoordsArray&,
699 const TextureSamplerArray&) SK_OVERRIDE;
700
joshualitt49586be2014-09-16 08:21:41 -0700701 static inline void GenKey(const GrEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000702
joshualitt49586be2014-09-16 08:21:41 -0700703 virtual void setData(const GrGLProgramDataManager&, const GrEffect&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000704
705private:
kkinnunen7510b222014-07-30 00:04:16 -0700706 GrGLProgramDataManager::UniformHandle fRectUniform;
707 GrGLProgramDataManager::UniformHandle fIntervalUniform;
708 SkRect fPrevRect;
709 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700710 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000711};
712
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000713GLDashingLineEffect::GLDashingLineEffect(const GrBackendEffectFactory& factory,
joshualitt49586be2014-09-16 08:21:41 -0700714 const GrEffect& effect)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000715 : INHERITED (factory) {
716 fPrevRect.fLeft = SK_ScalarNaN;
717 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000718}
719
joshualitt30ba4362014-08-21 20:18:45 -0700720void GLDashingLineEffect::emitCode(GrGLFullProgramBuilder* builder,
joshualitt49586be2014-09-16 08:21:41 -0700721 const GrEffect& effect,
bsalomon63e99f72014-07-21 08:03:14 -0700722 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000723 const char* outputColor,
724 const char* inputColor,
egdaniele61c4112014-06-12 10:24:21 -0700725 const TransformedCoordsArray&,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000726 const TextureSamplerArray& samplers) {
joshualitt49586be2014-09-16 08:21:41 -0700727 const DashingLineEffect& de = effect.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000728 const char *rectName;
729 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
730 // respectively.
joshualitt30ba4362014-08-21 20:18:45 -0700731 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000732 kVec4f_GrSLType,
733 "rect",
734 &rectName);
735 const char *intervalName;
736 // The interval uniform's refers to the total length of the interval (on + off)
joshualitt30ba4362014-08-21 20:18:45 -0700737 fIntervalUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000738 kFloat_GrSLType,
739 "interval",
740 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700741
742 const char *vsCoordName, *fsCoordName;
743 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700744 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
joshualitt249af152014-09-15 11:41:13 -0700745 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, de.inCoord().c_str());
egdaniele61c4112014-06-12 10:24:21 -0700746
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000747 // transforms all points so that we can compare them to our test rect
joshualitt30ba4362014-08-21 20:18:45 -0700748 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
749 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
egdaniele61c4112014-06-12 10:24:21 -0700750 fsCoordName, fsCoordName, intervalName, intervalName);
joshualitt30ba4362014-08-21 20:18:45 -0700751 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000752 if (GrEffectEdgeTypeIsAA(de.getEdgeType())) {
753 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
754 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700755 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
756 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
757 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
758 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
759 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000760 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
761 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700762 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 +0000763 } else {
764 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700765 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
766 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
767 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 +0000768 }
joshualitt30ba4362014-08-21 20:18:45 -0700769 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000770 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
771}
772
joshualitt49586be2014-09-16 08:21:41 -0700773void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, const GrEffect& effect) {
774 const DashingLineEffect& de = effect.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000775 const SkRect& rect = de.getRect();
776 SkScalar intervalLength = de.getIntervalLength();
777 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700778 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
779 rect.fRight - 0.5f, rect.fBottom - 0.5f);
780 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000781 fPrevRect = rect;
782 fPrevIntervalLength = intervalLength;
783 }
784}
785
joshualitt49586be2014-09-16 08:21:41 -0700786void GLDashingLineEffect::GenKey(const GrEffect& effect, const GrGLCaps&,
bsalomon63e99f72014-07-21 08:03:14 -0700787 GrEffectKeyBuilder* b) {
joshualitt49586be2014-09-16 08:21:41 -0700788 const DashingLineEffect& de = effect.cast<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700789 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000790}
791
792//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000793
bsalomon83d081a2014-07-08 09:56:10 -0700794GrEffect* DashingLineEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
795 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000796 if (info.fCount != 2) {
797 return NULL;
798 }
799
bsalomon55fad7a2014-07-08 07:34:20 -0700800 return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000801}
802
803DashingLineEffect::~DashingLineEffect() {}
804
805void DashingLineEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
806 *validFlags = 0;
807}
808
809const GrBackendEffectFactory& DashingLineEffect::getFactory() const {
810 return GrTBackendEffectFactory<DashingLineEffect>::getInstance();
811}
812
813DashingLineEffect::DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700814 SkScalar strokeWidth)
joshualitt249af152014-09-15 11:41:13 -0700815 : fEdgeType(edgeType)
816 , fInCoord(this->addVertexAttrib(GrShaderVar("inCoord",
817 kVec2f_GrSLType,
818 GrShaderVar::kAttribute_TypeModifier))) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000819 SkScalar onLen = info.fIntervals[0];
820 SkScalar offLen = info.fIntervals[1];
821 SkScalar halfOffLen = SkScalarHalf(offLen);
822 SkScalar halfStroke = SkScalarHalf(strokeWidth);
823 fIntervalLength = onLen + offLen;
824 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000825}
826
827bool DashingLineEffect::onIsEqual(const GrEffect& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700828 const DashingLineEffect& de = other.cast<DashingLineEffect>();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000829 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000830 fRect == de.fRect &&
831 fIntervalLength == de.fIntervalLength);
832}
833
834GR_DEFINE_EFFECT_TEST(DashingLineEffect);
835
bsalomon83d081a2014-07-08 09:56:10 -0700836GrEffect* DashingLineEffect::TestCreate(SkRandom* random,
837 GrContext*,
838 const GrDrawTargetCaps& caps,
839 GrTexture*[]) {
840 GrEffect* effect;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000841 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
842 kGrEffectEdgeTypeCnt));
843 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
844 DashInfo info;
845 info.fCount = 2;
846 SkAutoTArray<SkScalar> intervals(info.fCount);
847 info.fIntervals = intervals.get();
848 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
849 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
850 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
851
egdaniele61c4112014-06-12 10:24:21 -0700852 effect = DashingLineEffect::Create(edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000853 return effect;
854}
855
856//////////////////////////////////////////////////////////////////////////////
857
bsalomon83d081a2014-07-08 09:56:10 -0700858GrEffect* GrDashingEffect::Create(GrEffectEdgeType edgeType, const SkPathEffect::DashInfo& info,
859 SkScalar strokeWidth, GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700860 switch (cap) {
861 case GrDashingEffect::kRound_DashCap:
862 return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth));
863 case GrDashingEffect::kNonRound_DashCap:
864 return DashingLineEffect::Create(edgeType, info, strokeWidth);
865 default:
866 SkFAIL("Unexpected dashed cap.");
867 }
868 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000869}