blob: adee4aec966bdd7acd12b2f994190b7188b19b3e [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
joshualitt30ba4362014-08-21 20:18:45 -07008#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00009#include "GrDashingEffect.h"
10
egdaniele61c4112014-06-12 10:24:21 -070011#include "../GrAARectRenderer.h"
12
13#include "effects/GrVertexEffect.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000014#include "gl/GrGLEffect.h"
egdaniele61c4112014-06-12 10:24:21 -070015#include "gl/GrGLVertexEffect.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
72extern const GrVertexAttrib gDashLineVertexAttribs[] = {
73 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
74 { kVec2f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding },
75};
76
77};
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000078static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
79 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
80 SkVector vecSrc = pts[1] - pts[0];
81 SkScalar magSrc = vecSrc.length();
82 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
83 vecSrc.scale(invSrc);
84
85 SkVector vecSrcPerp;
86 vecSrc.rotateCW(&vecSrcPerp);
87 viewMatrix.mapVectors(&vecSrc, 1);
88 viewMatrix.mapVectors(&vecSrcPerp, 1);
89
90 // parallelScale tells how much to scale along the line parallel to the dash line
91 // perpScale tells how much to scale in the direction perpendicular to the dash line
92 *parallelScale = vecSrc.length();
93 *perpScale = vecSrcPerp.length();
94}
95
96// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
97// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
98static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
99 SkVector vec = pts[1] - pts[0];
100 SkScalar mag = vec.length();
101 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
102
103 vec.scale(inv);
104 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
105 if (ptsRot) {
106 rotMatrix->mapPoints(ptsRot, pts, 2);
107 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
108 ptsRot[1].fY = pts[0].fY;
109 }
110}
111
112// Assumes phase < sum of all intervals
113static SkScalar calc_start_adjustment(const SkPathEffect::DashInfo& info) {
114 SkASSERT(info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
115 if (info.fPhase >= info.fIntervals[0] && info.fPhase != 0) {
116 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
117 return srcIntervalLen - info.fPhase;
118 }
119 return 0;
120}
121
egdaniele61c4112014-06-12 10:24:21 -0700122static SkScalar calc_end_adjustment(const SkPathEffect::DashInfo& info, const SkPoint pts[2],
123 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000124 if (pts[1].fX <= pts[0].fX) {
125 return 0;
126 }
127 SkScalar srcIntervalLen = info.fIntervals[0] + info.fIntervals[1];
128 SkScalar totalLen = pts[1].fX - pts[0].fX;
129 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
130 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700131 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000132 temp = SkScalarDiv(*endingInt, srcIntervalLen);
133 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
134 if (0 == *endingInt) {
135 *endingInt = srcIntervalLen;
136 }
137 if (*endingInt > info.fIntervals[0]) {
138 if (0 == info.fIntervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000139 *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 +0000140 }
141 return *endingInt - info.fIntervals[0];
142 }
143 return 0;
144}
145
egdaniele61c4112014-06-12 10:24:21 -0700146static void setup_dashed_rect(const SkRect& rect, DashLineVertex* verts, int idx, const SkMatrix& matrix,
147 SkScalar offset, SkScalar bloat, SkScalar len, SkScalar stroke) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000148
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);
157
158 verts[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
159 verts[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
160 verts[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
161 verts[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
162
163 matrix.mapPointsWithStride(&verts[idx].fPos, sizeof(DashLineVertex), 4);
164}
165
166
167bool GrDashingEffect::DrawDashLine(const SkPoint pts[2], const GrPaint& paint,
168 const GrStrokeInfo& strokeInfo, GrGpu* gpu,
169 GrDrawTarget* target, const SkMatrix& vm) {
170
171 if (!can_fast_path_dash(pts, strokeInfo, *target, vm)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000172 return false;
173 }
174
egdaniele61c4112014-06-12 10:24:21 -0700175 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000176
egdaniele61c4112014-06-12 10:24:21 -0700177 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000178
egdaniele61c4112014-06-12 10:24:21 -0700179 SkScalar srcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000180
181 // the phase should be normalized to be [0, sum of all intervals)
182 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
183
egdaniele61c4112014-06-12 10:24:21 -0700184 SkScalar srcPhase = info.fPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000185
186 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
187 SkMatrix srcRotInv;
188 SkPoint ptsRot[2];
189 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700190 SkMatrix rotMatrix;
191 align_to_x_axis(pts, &rotMatrix, ptsRot);
192 if(!rotMatrix.invert(&srcRotInv)) {
193 GrPrintf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000194 return false;
195 }
196 } else {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000197 srcRotInv.reset();
198 memcpy(ptsRot, pts, 2 * sizeof(SkPoint));
199 }
200
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000201 bool useAA = paint.isAntiAlias();
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000202
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000203 // Scale corrections of intervals and stroke from view matrix
204 SkScalar parallelScale;
205 SkScalar perpScale;
egdaniele61c4112014-06-12 10:24:21 -0700206 calc_dash_scaling(&parallelScale, &perpScale, vm, ptsRot);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000207
egdanielf767e792014-07-02 06:21:32 -0700208 bool hasCap = SkPaint::kButt_Cap != cap && 0 != srcStrokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000209
210 // We always want to at least stroke out half a pixel on each side in device space
211 // so 0.5f / perpScale gives us this min in src space
egdaniele61c4112014-06-12 10:24:21 -0700212 SkScalar halfSrcStroke = SkMaxScalar(srcStrokeWidth * 0.5f, 0.5f / perpScale);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000213
egdaniele61c4112014-06-12 10:24:21 -0700214 SkScalar strokeAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000215 if (!hasCap) {
egdaniele61c4112014-06-12 10:24:21 -0700216 strokeAdj = 0.f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000217 } else {
egdaniele61c4112014-06-12 10:24:21 -0700218 strokeAdj = halfSrcStroke;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000219 }
220
egdaniele61c4112014-06-12 10:24:21 -0700221 SkScalar startAdj = 0;
222
223 SkMatrix combinedMatrix = srcRotInv;
224 combinedMatrix.postConcat(vm);
225
226 bool lineDone = false;
227 SkRect startRect;
228 bool hasStartRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000229 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
230 // draw it separately here and adjust our start point accordingly
231 if (useAA) {
egdaniele61c4112014-06-12 10:24:21 -0700232 if (srcPhase > 0 && srcPhase < info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000233 SkPoint startPts[2];
234 startPts[0] = ptsRot[0];
235 startPts[1].fY = startPts[0].fY;
egdaniele61c4112014-06-12 10:24:21 -0700236 startPts[1].fX = SkMinScalar(startPts[0].fX + info.fIntervals[0] - srcPhase,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000237 ptsRot[1].fX);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000238 startRect.set(startPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700239 startRect.outset(strokeAdj, halfSrcStroke);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000240
egdaniele61c4112014-06-12 10:24:21 -0700241 hasStartRect = true;
242 startAdj = info.fIntervals[0] + info.fIntervals[1] - srcPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000243 }
244 }
245
246 // adjustments for start and end of bounding rect so we only draw dash intervals
247 // contained in the original line segment.
egdaniele61c4112014-06-12 10:24:21 -0700248 startAdj += calc_start_adjustment(info);
249 if (startAdj != 0) {
250 ptsRot[0].fX += startAdj;
251 srcPhase = 0;
252 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000253 SkScalar endingInterval = 0;
egdaniele61c4112014-06-12 10:24:21 -0700254 SkScalar endAdj = calc_end_adjustment(info, ptsRot, srcPhase, &endingInterval);
255 ptsRot[1].fX -= endAdj;
256 if (ptsRot[0].fX >= ptsRot[1].fX) {
257 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000258 }
259
egdaniele61c4112014-06-12 10:24:21 -0700260 SkRect endRect;
261 bool hasEndRect = false;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000262 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
263 // draw it separately here and adjust our end point accordingly
egdaniele61c4112014-06-12 10:24:21 -0700264 if (useAA && !lineDone) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000265 // If we adjusted the end then we will not be drawing a partial dash at the end.
266 // If we didn't adjust the end point then we just need to make sure the ending
267 // dash isn't a full dash
268 if (0 == endAdj && endingInterval != info.fIntervals[0]) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000269 SkPoint endPts[2];
270 endPts[1] = ptsRot[1];
271 endPts[0].fY = endPts[1].fY;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000272 endPts[0].fX = endPts[1].fX - endingInterval;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000273
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000274 endRect.set(endPts, 2);
egdaniele61c4112014-06-12 10:24:21 -0700275 endRect.outset(strokeAdj, halfSrcStroke);
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000276
egdaniele61c4112014-06-12 10:24:21 -0700277 hasEndRect = true;
278 endAdj = endingInterval + info.fIntervals[1];
279
280 ptsRot[1].fX -= endAdj;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000281 if (ptsRot[0].fX >= ptsRot[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700282 lineDone = true;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000283 }
284 }
285 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000286
egdaniele61c4112014-06-12 10:24:21 -0700287 if (startAdj != 0) {
288 srcPhase = 0;
289 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000290
egdaniele61c4112014-06-12 10:24:21 -0700291 // Change the dashing info from src space into device space
292 SkScalar devIntervals[2];
293 devIntervals[0] = info.fIntervals[0] * parallelScale;
294 devIntervals[1] = info.fIntervals[1] * parallelScale;
295 SkScalar devPhase = srcPhase * parallelScale;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000296 SkScalar strokeWidth = srcStrokeWidth * perpScale;
297
298 if ((strokeWidth < 1.f && !useAA) || 0.f == strokeWidth) {
299 strokeWidth = 1.f;
300 }
301
egdaniele61c4112014-06-12 10:24:21 -0700302 SkScalar halfDevStroke = strokeWidth * 0.5f;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000303
304 if (SkPaint::kSquare_Cap == cap && 0 != srcStrokeWidth) {
305 // add cap to on interveal and remove from off interval
egdaniele61c4112014-06-12 10:24:21 -0700306 devIntervals[0] += strokeWidth;
307 devIntervals[1] -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000308 }
egdaniele61c4112014-06-12 10:24:21 -0700309 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000310
egdaniele61c4112014-06-12 10:24:21 -0700311 SkScalar bloatX = useAA ? 0.5f / parallelScale : 0.f;
312 SkScalar bloatY = useAA ? 0.5f / perpScale : 0.f;
313
314 SkScalar devBloat = useAA ? 0.5f : 0.f;
315
316 GrDrawState* drawState = target->drawState();
317 if (devIntervals[1] <= 0.f && useAA) {
318 // Case when we end up drawing a solid AA rect
319 // Reset the start rect to draw this single solid rect
320 // but it requires to upload a new intervals uniform so we can mimic
321 // one giant dash
322 ptsRot[0].fX -= hasStartRect ? startAdj : 0;
323 ptsRot[1].fX += hasEndRect ? endAdj : 0;
324 startRect.set(ptsRot, 2);
325 startRect.outset(strokeAdj, halfSrcStroke);
326 hasStartRect = true;
327 hasEndRect = false;
328 lineDone = true;
329
330 SkPoint devicePts[2];
331 vm.mapPoints(devicePts, ptsRot, 2);
332 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
333 if (hasCap) {
334 lineLength += 2.f * halfDevStroke;
335 }
336 devIntervals[0] = lineLength;
337 }
338 if (devIntervals[1] > 0.f || useAA) {
339 SkPathEffect::DashInfo devInfo;
340 devInfo.fPhase = devPhase;
341 devInfo.fCount = 2;
342 devInfo.fIntervals = devIntervals;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000343 GrEffectEdgeType edgeType= useAA ? kFillAA_GrEffectEdgeType :
344 kFillBW_GrEffectEdgeType;
egdanielf767e792014-07-02 06:21:32 -0700345 bool isRoundCap = SkPaint::kRound_Cap == cap;
346 GrDashingEffect::DashCap capType = isRoundCap ? GrDashingEffect::kRound_DashCap :
347 GrDashingEffect::kNonRound_DashCap;
egdaniele61c4112014-06-12 10:24:21 -0700348 drawState->addCoverageEffect(
egdanielf767e792014-07-02 06:21:32 -0700349 GrDashingEffect::Create(edgeType, devInfo, strokeWidth, capType), 1)->unref();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000350 }
351
egdaniele61c4112014-06-12 10:24:21 -0700352 // Set up the vertex data for the line and start/end dashes
egdaniel7b3d5ee2014-08-28 05:41:14 -0700353 drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs),
354 sizeof(DashLineVertex));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000355
egdaniele61c4112014-06-12 10:24:21 -0700356 int totalRectCnt = 0;
357
358 totalRectCnt += !lineDone ? 1 : 0;
359 totalRectCnt += hasStartRect ? 1 : 0;
360 totalRectCnt += hasEndRect ? 1 : 0;
361
362 GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0);
363 if (!geo.succeeded()) {
364 GrPrintf("Failed to get space for vertices!\n");
365 return false;
366 }
367
368 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
369
370 int curVIdx = 0;
371
egdanielf767e792014-07-02 06:21:32 -0700372 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
373 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
374 startOffset -= halfDevStroke;
375 }
376
egdaniele61c4112014-06-12 10:24:21 -0700377 // Draw interior part of dashed line
378 if (!lineDone) {
379 SkPoint devicePts[2];
380 vm.mapPoints(devicePts, ptsRot, 2);
381 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
382 if (hasCap) {
383 lineLength += 2.f * halfDevStroke;
384 }
385
386 SkRect bounds;
387 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
388 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
389 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
390 lineLength, halfDevStroke);
391 curVIdx += 4;
392 }
393
394 if (hasStartRect) {
395 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
396 startRect.outset(bloatX, bloatY);
397 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
398 devIntervals[0], halfDevStroke);
399 curVIdx += 4;
400 }
401
402 if (hasEndRect) {
403 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
404 endRect.outset(bloatX, bloatY);
405 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
406 devIntervals[0], halfDevStroke);
407 }
408
409 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
410 target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
411 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000412 return true;
413}
414
415//////////////////////////////////////////////////////////////////////////////
416
egdanielf767e792014-07-02 06:21:32 -0700417class GLDashingCircleEffect;
418/*
419 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
420 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
421 * Both of the previous two parameters are in device space. This effect also requires the setting of
422 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
423 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
424 * transform the line to be horizontal, with the start of line at the origin then shifted to the
425 * right by half the off interval. The line then goes in the positive x direction.
426 */
427class DashingCircleEffect : public GrVertexEffect {
428public:
429 typedef SkPathEffect::DashInfo DashInfo;
430
bsalomon83d081a2014-07-08 09:56:10 -0700431 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700432
433 virtual ~DashingCircleEffect();
434
435 static const char* Name() { return "DashingCircleEffect"; }
436
437 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
438
439 SkScalar getRadius() const { return fRadius; }
440
441 SkScalar getCenterX() const { return fCenterX; }
442
443 SkScalar getIntervalLength() const { return fIntervalLength; }
444
445 typedef GLDashingCircleEffect GLEffect;
446
447 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
448
449 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
450
451private:
452 DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
453
454 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
455
456 GrEffectEdgeType fEdgeType;
457 SkScalar fIntervalLength;
458 SkScalar fRadius;
459 SkScalar fCenterX;
460
461 GR_DECLARE_EFFECT_TEST;
462
463 typedef GrVertexEffect INHERITED;
464};
465
466//////////////////////////////////////////////////////////////////////////////
467
468class GLDashingCircleEffect : public GrGLVertexEffect {
469public:
470 GLDashingCircleEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
471
joshualitt30ba4362014-08-21 20:18:45 -0700472 virtual void emitCode(GrGLFullProgramBuilder* builder,
egdanielf767e792014-07-02 06:21:32 -0700473 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700474 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700475 const char* outputColor,
476 const char* inputColor,
477 const TransformedCoordsArray&,
478 const TextureSamplerArray&) SK_OVERRIDE;
479
bsalomon63e99f72014-07-21 08:03:14 -0700480 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700481
kkinnunen7510b222014-07-30 00:04:16 -0700482 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700483
484private:
kkinnunen7510b222014-07-30 00:04:16 -0700485 GrGLProgramDataManager::UniformHandle fParamUniform;
486 SkScalar fPrevRadius;
487 SkScalar fPrevCenterX;
488 SkScalar fPrevIntervalLength;
egdanielf767e792014-07-02 06:21:32 -0700489 typedef GrGLVertexEffect INHERITED;
490};
491
492GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendEffectFactory& factory,
493 const GrDrawEffect& drawEffect)
494 : INHERITED (factory) {
495 fPrevRadius = SK_ScalarMin;
496 fPrevCenterX = SK_ScalarMin;
497 fPrevIntervalLength = SK_ScalarMax;
498}
499
joshualitt30ba4362014-08-21 20:18:45 -0700500void GLDashingCircleEffect::emitCode(GrGLFullProgramBuilder* builder,
egdanielf767e792014-07-02 06:21:32 -0700501 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700502 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700503 const char* outputColor,
504 const char* inputColor,
505 const TransformedCoordsArray&,
506 const TextureSamplerArray& samplers) {
507 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
508 const char *paramName;
509 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
510 // the total interval length of the dash.
joshualitt30ba4362014-08-21 20:18:45 -0700511 fParamUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
egdanielf767e792014-07-02 06:21:32 -0700512 kVec3f_GrSLType,
513 "params",
514 &paramName);
515
516 const char *vsCoordName, *fsCoordName;
517 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700518
519 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
egdanielf767e792014-07-02 06:21:32 -0700520 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700521 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
522 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str());
egdanielf767e792014-07-02 06:21:32 -0700523
524 // transforms all points so that we can compare them to our test circle
joshualitt30ba4362014-08-21 20:18:45 -0700525 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
526 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
egdanielf767e792014-07-02 06:21:32 -0700527 fsCoordName, fsCoordName, paramName, paramName);
joshualitt30ba4362014-08-21 20:18:45 -0700528 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
529 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
530 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
egdanielf767e792014-07-02 06:21:32 -0700531 if (GrEffectEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700532 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
533 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
534 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700535 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700536 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
537 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700538 }
joshualitt30ba4362014-08-21 20:18:45 -0700539 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
egdanielf767e792014-07-02 06:21:32 -0700540 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
541}
542
kkinnunen7510b222014-07-30 00:04:16 -0700543void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) {
egdanielf767e792014-07-02 06:21:32 -0700544 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
545 SkScalar radius = dce.getRadius();
546 SkScalar centerX = dce.getCenterX();
547 SkScalar intervalLength = dce.getIntervalLength();
548 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700549 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700550 fPrevRadius = radius;
551 fPrevCenterX = centerX;
552 fPrevIntervalLength = intervalLength;
553 }
554}
555
bsalomon63e99f72014-07-21 08:03:14 -0700556void GLDashingCircleEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
557 GrEffectKeyBuilder* b) {
egdanielf767e792014-07-02 06:21:32 -0700558 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700559 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700560}
561
562//////////////////////////////////////////////////////////////////////////////
563
bsalomon83d081a2014-07-08 09:56:10 -0700564GrEffect* DashingCircleEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
565 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700566 if (info.fCount != 2 || info.fIntervals[0] != 0) {
567 return NULL;
568 }
569
bsalomon55fad7a2014-07-08 07:34:20 -0700570 return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700571}
572
573DashingCircleEffect::~DashingCircleEffect() {}
574
575void DashingCircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
576 *validFlags = 0;
577}
578
579const GrBackendEffectFactory& DashingCircleEffect::getFactory() const {
580 return GrTBackendEffectFactory<DashingCircleEffect>::getInstance();
581}
582
583DashingCircleEffect::DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info,
584 SkScalar radius)
585 : fEdgeType(edgeType) {
586 SkScalar onLen = info.fIntervals[0];
587 SkScalar offLen = info.fIntervals[1];
588 fIntervalLength = onLen + offLen;
589 fRadius = radius;
590 fCenterX = SkScalarHalf(offLen);
591
592 this->addVertexAttrib(kVec2f_GrSLType);
593}
594
595bool DashingCircleEffect::onIsEqual(const GrEffect& other) const {
596 const DashingCircleEffect& dce = CastEffect<DashingCircleEffect>(other);
597 return (fEdgeType == dce.fEdgeType &&
598 fIntervalLength == dce.fIntervalLength &&
599 fRadius == dce.fRadius &&
600 fCenterX == dce.fCenterX);
601}
602
603GR_DEFINE_EFFECT_TEST(DashingCircleEffect);
604
bsalomon83d081a2014-07-08 09:56:10 -0700605GrEffect* DashingCircleEffect::TestCreate(SkRandom* random,
606 GrContext*,
607 const GrDrawTargetCaps& caps,
608 GrTexture*[]) {
609 GrEffect* effect;
egdanielf767e792014-07-02 06:21:32 -0700610 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
611 kGrEffectEdgeTypeCnt));
612 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
613 DashInfo info;
614 info.fCount = 2;
615 SkAutoTArray<SkScalar> intervals(info.fCount);
616 info.fIntervals = intervals.get();
617 info.fIntervals[0] = 0;
618 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
619 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
620
621 effect = DashingCircleEffect::Create(edgeType, info, strokeWidth);
622 return effect;
623}
624
625//////////////////////////////////////////////////////////////////////////////
626
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000627class GLDashingLineEffect;
628
egdanielf767e792014-07-02 06:21:32 -0700629/*
630 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
631 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
632 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
633 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
634 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
635 * line at the origin then shifted to the right by half the off interval. The line then goes in the
636 * positive x direction.
637 */
egdaniele61c4112014-06-12 10:24:21 -0700638class DashingLineEffect : public GrVertexEffect {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000639public:
640 typedef SkPathEffect::DashInfo DashInfo;
641
bsalomon83d081a2014-07-08 09:56:10 -0700642 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000643
644 virtual ~DashingLineEffect();
645
646 static const char* Name() { return "DashingEffect"; }
647
648 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
649
650 const SkRect& getRect() const { return fRect; }
651
652 SkScalar getIntervalLength() const { return fIntervalLength; }
653
654 typedef GLDashingLineEffect GLEffect;
655
656 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
657
658 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
659
660private:
egdaniele61c4112014-06-12 10:24:21 -0700661 DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000662
663 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
664
665 GrEffectEdgeType fEdgeType;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000666 SkRect fRect;
667 SkScalar fIntervalLength;
668
669 GR_DECLARE_EFFECT_TEST;
670
egdanielf767e792014-07-02 06:21:32 -0700671 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000672};
673
674//////////////////////////////////////////////////////////////////////////////
675
egdaniele61c4112014-06-12 10:24:21 -0700676class GLDashingLineEffect : public GrGLVertexEffect {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000677public:
678 GLDashingLineEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
679
joshualitt30ba4362014-08-21 20:18:45 -0700680 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000681 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700682 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000683 const char* outputColor,
684 const char* inputColor,
685 const TransformedCoordsArray&,
686 const TextureSamplerArray&) SK_OVERRIDE;
687
bsalomon63e99f72014-07-21 08:03:14 -0700688 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000689
kkinnunen7510b222014-07-30 00:04:16 -0700690 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000691
692private:
kkinnunen7510b222014-07-30 00:04:16 -0700693 GrGLProgramDataManager::UniformHandle fRectUniform;
694 GrGLProgramDataManager::UniformHandle fIntervalUniform;
695 SkRect fPrevRect;
696 SkScalar fPrevIntervalLength;
egdaniele61c4112014-06-12 10:24:21 -0700697 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000698};
699
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000700GLDashingLineEffect::GLDashingLineEffect(const GrBackendEffectFactory& factory,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000701 const GrDrawEffect& drawEffect)
702 : INHERITED (factory) {
703 fPrevRect.fLeft = SK_ScalarNaN;
704 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000705}
706
joshualitt30ba4362014-08-21 20:18:45 -0700707void GLDashingLineEffect::emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700709 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000710 const char* outputColor,
711 const char* inputColor,
egdaniele61c4112014-06-12 10:24:21 -0700712 const TransformedCoordsArray&,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000713 const TextureSamplerArray& samplers) {
714 const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>();
715 const char *rectName;
716 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
717 // respectively.
joshualitt30ba4362014-08-21 20:18:45 -0700718 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000719 kVec4f_GrSLType,
720 "rect",
721 &rectName);
722 const char *intervalName;
723 // The interval uniform's refers to the total length of the interval (on + off)
joshualitt30ba4362014-08-21 20:18:45 -0700724 fIntervalUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000725 kFloat_GrSLType,
726 "interval",
727 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700728
729 const char *vsCoordName, *fsCoordName;
730 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700731 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
egdaniele61c4112014-06-12 10:24:21 -0700732 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700733 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
734 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str());
egdaniele61c4112014-06-12 10:24:21 -0700735
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000736 // transforms all points so that we can compare them to our test rect
joshualitt30ba4362014-08-21 20:18:45 -0700737 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
738 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
egdaniele61c4112014-06-12 10:24:21 -0700739 fsCoordName, fsCoordName, intervalName, intervalName);
joshualitt30ba4362014-08-21 20:18:45 -0700740 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000741 if (GrEffectEdgeTypeIsAA(de.getEdgeType())) {
742 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
743 // numbers, xSub and ySub.
joshualitt30ba4362014-08-21 20:18:45 -0700744 fsBuilder->codeAppend("\t\tfloat xSub, ySub;\n");
745 fsBuilder->codeAppendf("\t\txSub = min(fragPosShifted.x - %s.x, 0.0);\n", rectName);
746 fsBuilder->codeAppendf("\t\txSub += min(%s.z - fragPosShifted.x, 0.0);\n", rectName);
747 fsBuilder->codeAppendf("\t\tySub = min(fragPosShifted.y - %s.y, 0.0);\n", rectName);
748 fsBuilder->codeAppendf("\t\tySub += min(%s.w - fragPosShifted.y, 0.0);\n", rectName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000749 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
750 // covered.
joshualitt30ba4362014-08-21 20:18:45 -0700751 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 +0000752 } else {
753 // Assuming the bounding geometry is tight so no need to check y values
joshualitt30ba4362014-08-21 20:18:45 -0700754 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
755 fsBuilder->codeAppendf("\t\talpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;\n", rectName);
756 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 +0000757 }
joshualitt30ba4362014-08-21 20:18:45 -0700758 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000759 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
760}
761
kkinnunen7510b222014-07-30 00:04:16 -0700762void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000763 const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>();
764 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
bsalomon63e99f72014-07-21 08:03:14 -0700775void GLDashingLineEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
776 GrEffectKeyBuilder* b) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000777 const DashingLineEffect& de = drawEffect.castEffect<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
bsalomon83d081a2014-07-08 09:56:10 -0700783GrEffect* DashingLineEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
784 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000785 if (info.fCount != 2) {
786 return NULL;
787 }
788
bsalomon55fad7a2014-07-08 07:34:20 -0700789 return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000790}
791
792DashingLineEffect::~DashingLineEffect() {}
793
794void DashingLineEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
795 *validFlags = 0;
796}
797
798const GrBackendEffectFactory& DashingLineEffect::getFactory() const {
799 return GrTBackendEffectFactory<DashingLineEffect>::getInstance();
800}
801
802DashingLineEffect::DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700803 SkScalar strokeWidth)
804 : fEdgeType(edgeType) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000805 SkScalar onLen = info.fIntervals[0];
806 SkScalar offLen = info.fIntervals[1];
807 SkScalar halfOffLen = SkScalarHalf(offLen);
808 SkScalar halfStroke = SkScalarHalf(strokeWidth);
809 fIntervalLength = onLen + offLen;
810 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
811
egdaniele61c4112014-06-12 10:24:21 -0700812 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000813}
814
815bool DashingLineEffect::onIsEqual(const GrEffect& other) const {
816 const DashingLineEffect& de = CastEffect<DashingLineEffect>(other);
817 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000818 fRect == de.fRect &&
819 fIntervalLength == de.fIntervalLength);
820}
821
822GR_DEFINE_EFFECT_TEST(DashingLineEffect);
823
bsalomon83d081a2014-07-08 09:56:10 -0700824GrEffect* DashingLineEffect::TestCreate(SkRandom* random,
825 GrContext*,
826 const GrDrawTargetCaps& caps,
827 GrTexture*[]) {
828 GrEffect* effect;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000829 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
830 kGrEffectEdgeTypeCnt));
831 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
832 DashInfo info;
833 info.fCount = 2;
834 SkAutoTArray<SkScalar> intervals(info.fCount);
835 info.fIntervals = intervals.get();
836 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
837 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
838 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
839
egdaniele61c4112014-06-12 10:24:21 -0700840 effect = DashingLineEffect::Create(edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000841 return effect;
842}
843
844//////////////////////////////////////////////////////////////////////////////
845
bsalomon83d081a2014-07-08 09:56:10 -0700846GrEffect* GrDashingEffect::Create(GrEffectEdgeType edgeType, const SkPathEffect::DashInfo& info,
847 SkScalar strokeWidth, GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700848 switch (cap) {
849 case GrDashingEffect::kRound_DashCap:
850 return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth));
851 case GrDashingEffect::kNonRound_DashCap:
852 return DashingLineEffect::Create(edgeType, info, strokeWidth);
853 default:
854 SkFAIL("Unexpected dashed cap.");
855 }
856 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000857}