blob: 4b2bafefdbbb14af4f62ff29ae9d9f18390b9b46 [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
djsollenea81ced2014-08-27 13:07:34 -0700353 drawState->setVertexAttribs<gDashLineVertexAttribs>(SK_ARRAY_COUNT(gDashLineVertexAttribs));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000354
egdaniele61c4112014-06-12 10:24:21 -0700355 int totalRectCnt = 0;
356
357 totalRectCnt += !lineDone ? 1 : 0;
358 totalRectCnt += hasStartRect ? 1 : 0;
359 totalRectCnt += hasEndRect ? 1 : 0;
360
361 GrDrawTarget::AutoReleaseGeometry geo(target, totalRectCnt * 4, 0);
362 if (!geo.succeeded()) {
363 GrPrintf("Failed to get space for vertices!\n");
364 return false;
365 }
366
367 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(geo.vertices());
368
369 int curVIdx = 0;
370
egdanielf767e792014-07-02 06:21:32 -0700371 if (SkPaint::kRound_Cap == cap && 0 != srcStrokeWidth) {
372 // need to adjust this for round caps to correctly set the dashPos attrib on vertices
373 startOffset -= halfDevStroke;
374 }
375
egdaniele61c4112014-06-12 10:24:21 -0700376 // Draw interior part of dashed line
377 if (!lineDone) {
378 SkPoint devicePts[2];
379 vm.mapPoints(devicePts, ptsRot, 2);
380 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
381 if (hasCap) {
382 lineLength += 2.f * halfDevStroke;
383 }
384
385 SkRect bounds;
386 bounds.set(ptsRot[0].fX, ptsRot[0].fY, ptsRot[1].fX, ptsRot[1].fY);
387 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
388 setup_dashed_rect(bounds, verts, curVIdx, combinedMatrix, startOffset, devBloat,
389 lineLength, halfDevStroke);
390 curVIdx += 4;
391 }
392
393 if (hasStartRect) {
394 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
395 startRect.outset(bloatX, bloatY);
396 setup_dashed_rect(startRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
397 devIntervals[0], halfDevStroke);
398 curVIdx += 4;
399 }
400
401 if (hasEndRect) {
402 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
403 endRect.outset(bloatX, bloatY);
404 setup_dashed_rect(endRect, verts, curVIdx, combinedMatrix, startOffset, devBloat,
405 devIntervals[0], halfDevStroke);
406 }
407
408 target->setIndexSourceToBuffer(gpu->getContext()->getQuadIndexBuffer());
409 target->drawIndexedInstances(kTriangles_GrPrimitiveType, totalRectCnt, 4, 6);
410 target->resetIndexSource();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000411 return true;
412}
413
414//////////////////////////////////////////////////////////////////////////////
415
egdanielf767e792014-07-02 06:21:32 -0700416class GLDashingCircleEffect;
417/*
418 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
419 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
420 * Both of the previous two parameters are in device space. This effect also requires the setting of
421 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
422 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
423 * transform the line to be horizontal, with the start of line at the origin then shifted to the
424 * right by half the off interval. The line then goes in the positive x direction.
425 */
426class DashingCircleEffect : public GrVertexEffect {
427public:
428 typedef SkPathEffect::DashInfo DashInfo;
429
bsalomon83d081a2014-07-08 09:56:10 -0700430 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
egdanielf767e792014-07-02 06:21:32 -0700431
432 virtual ~DashingCircleEffect();
433
434 static const char* Name() { return "DashingCircleEffect"; }
435
436 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
437
438 SkScalar getRadius() const { return fRadius; }
439
440 SkScalar getCenterX() const { return fCenterX; }
441
442 SkScalar getIntervalLength() const { return fIntervalLength; }
443
444 typedef GLDashingCircleEffect GLEffect;
445
446 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
447
448 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
449
450private:
451 DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar radius);
452
453 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
454
455 GrEffectEdgeType fEdgeType;
456 SkScalar fIntervalLength;
457 SkScalar fRadius;
458 SkScalar fCenterX;
459
460 GR_DECLARE_EFFECT_TEST;
461
462 typedef GrVertexEffect INHERITED;
463};
464
465//////////////////////////////////////////////////////////////////////////////
466
467class GLDashingCircleEffect : public GrGLVertexEffect {
468public:
469 GLDashingCircleEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
470
joshualitt30ba4362014-08-21 20:18:45 -0700471 virtual void emitCode(GrGLFullProgramBuilder* builder,
egdanielf767e792014-07-02 06:21:32 -0700472 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700473 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700474 const char* outputColor,
475 const char* inputColor,
476 const TransformedCoordsArray&,
477 const TextureSamplerArray&) SK_OVERRIDE;
478
bsalomon63e99f72014-07-21 08:03:14 -0700479 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700480
kkinnunen7510b222014-07-30 00:04:16 -0700481 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
egdanielf767e792014-07-02 06:21:32 -0700482
483private:
kkinnunen7510b222014-07-30 00:04:16 -0700484 GrGLProgramDataManager::UniformHandle fParamUniform;
485 SkScalar fPrevRadius;
486 SkScalar fPrevCenterX;
487 SkScalar fPrevIntervalLength;
egdanielf767e792014-07-02 06:21:32 -0700488 typedef GrGLVertexEffect INHERITED;
489};
490
491GLDashingCircleEffect::GLDashingCircleEffect(const GrBackendEffectFactory& factory,
492 const GrDrawEffect& drawEffect)
493 : INHERITED (factory) {
494 fPrevRadius = SK_ScalarMin;
495 fPrevCenterX = SK_ScalarMin;
496 fPrevIntervalLength = SK_ScalarMax;
497}
498
joshualitt30ba4362014-08-21 20:18:45 -0700499void GLDashingCircleEffect::emitCode(GrGLFullProgramBuilder* builder,
egdanielf767e792014-07-02 06:21:32 -0700500 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700501 const GrEffectKey& key,
egdanielf767e792014-07-02 06:21:32 -0700502 const char* outputColor,
503 const char* inputColor,
504 const TransformedCoordsArray&,
505 const TextureSamplerArray& samplers) {
506 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
507 const char *paramName;
508 // The param uniforms, xyz, refer to circle radius - 0.5, cicles center x coord, and
509 // the total interval length of the dash.
joshualitt30ba4362014-08-21 20:18:45 -0700510 fParamUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
egdanielf767e792014-07-02 06:21:32 -0700511 kVec3f_GrSLType,
512 "params",
513 &paramName);
514
515 const char *vsCoordName, *fsCoordName;
516 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700517
518 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
egdanielf767e792014-07-02 06:21:32 -0700519 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700520 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
521 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str());
egdanielf767e792014-07-02 06:21:32 -0700522
523 // transforms all points so that we can compare them to our test circle
joshualitt30ba4362014-08-21 20:18:45 -0700524 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
525 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s.z) * %s.z;\n",
egdanielf767e792014-07-02 06:21:32 -0700526 fsCoordName, fsCoordName, paramName, paramName);
joshualitt30ba4362014-08-21 20:18:45 -0700527 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
528 fsBuilder->codeAppendf("\t\tvec2 center = vec2(%s.y, 0.0);\n", paramName);
529 fsBuilder->codeAppend("\t\tfloat dist = length(center - fragPosShifted);\n");
egdanielf767e792014-07-02 06:21:32 -0700530 if (GrEffectEdgeTypeIsAA(dce.getEdgeType())) {
joshualitt30ba4362014-08-21 20:18:45 -0700531 fsBuilder->codeAppendf("\t\tfloat diff = dist - %s.x;\n", paramName);
532 fsBuilder->codeAppend("\t\tdiff = 1.0 - diff;\n");
533 fsBuilder->codeAppend("\t\tfloat alpha = clamp(diff, 0.0, 1.0);\n");
egdanielf767e792014-07-02 06:21:32 -0700534 } else {
joshualitt30ba4362014-08-21 20:18:45 -0700535 fsBuilder->codeAppendf("\t\tfloat alpha = 1.0;\n");
536 fsBuilder->codeAppendf("\t\talpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;\n", paramName);
egdanielf767e792014-07-02 06:21:32 -0700537 }
joshualitt30ba4362014-08-21 20:18:45 -0700538 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
egdanielf767e792014-07-02 06:21:32 -0700539 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
540}
541
kkinnunen7510b222014-07-30 00:04:16 -0700542void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) {
egdanielf767e792014-07-02 06:21:32 -0700543 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
544 SkScalar radius = dce.getRadius();
545 SkScalar centerX = dce.getCenterX();
546 SkScalar intervalLength = dce.getIntervalLength();
547 if (radius != fPrevRadius || centerX != fPrevCenterX || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700548 pdman.set3f(fParamUniform, radius - 0.5f, centerX, intervalLength);
egdanielf767e792014-07-02 06:21:32 -0700549 fPrevRadius = radius;
550 fPrevCenterX = centerX;
551 fPrevIntervalLength = intervalLength;
552 }
553}
554
bsalomon63e99f72014-07-21 08:03:14 -0700555void GLDashingCircleEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
556 GrEffectKeyBuilder* b) {
egdanielf767e792014-07-02 06:21:32 -0700557 const DashingCircleEffect& dce = drawEffect.castEffect<DashingCircleEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700558 b->add32(dce.getEdgeType());
egdanielf767e792014-07-02 06:21:32 -0700559}
560
561//////////////////////////////////////////////////////////////////////////////
562
bsalomon83d081a2014-07-08 09:56:10 -0700563GrEffect* DashingCircleEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
564 SkScalar radius) {
egdanielf767e792014-07-02 06:21:32 -0700565 if (info.fCount != 2 || info.fIntervals[0] != 0) {
566 return NULL;
567 }
568
bsalomon55fad7a2014-07-08 07:34:20 -0700569 return SkNEW_ARGS(DashingCircleEffect, (edgeType, info, radius));
egdanielf767e792014-07-02 06:21:32 -0700570}
571
572DashingCircleEffect::~DashingCircleEffect() {}
573
574void DashingCircleEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
575 *validFlags = 0;
576}
577
578const GrBackendEffectFactory& DashingCircleEffect::getFactory() const {
579 return GrTBackendEffectFactory<DashingCircleEffect>::getInstance();
580}
581
582DashingCircleEffect::DashingCircleEffect(GrEffectEdgeType edgeType, const DashInfo& info,
583 SkScalar radius)
584 : fEdgeType(edgeType) {
585 SkScalar onLen = info.fIntervals[0];
586 SkScalar offLen = info.fIntervals[1];
587 fIntervalLength = onLen + offLen;
588 fRadius = radius;
589 fCenterX = SkScalarHalf(offLen);
590
591 this->addVertexAttrib(kVec2f_GrSLType);
592}
593
594bool DashingCircleEffect::onIsEqual(const GrEffect& other) const {
595 const DashingCircleEffect& dce = CastEffect<DashingCircleEffect>(other);
596 return (fEdgeType == dce.fEdgeType &&
597 fIntervalLength == dce.fIntervalLength &&
598 fRadius == dce.fRadius &&
599 fCenterX == dce.fCenterX);
600}
601
602GR_DEFINE_EFFECT_TEST(DashingCircleEffect);
603
bsalomon83d081a2014-07-08 09:56:10 -0700604GrEffect* DashingCircleEffect::TestCreate(SkRandom* random,
605 GrContext*,
606 const GrDrawTargetCaps& caps,
607 GrTexture*[]) {
608 GrEffect* effect;
egdanielf767e792014-07-02 06:21:32 -0700609 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
610 kGrEffectEdgeTypeCnt));
611 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
612 DashInfo info;
613 info.fCount = 2;
614 SkAutoTArray<SkScalar> intervals(info.fCount);
615 info.fIntervals = intervals.get();
616 info.fIntervals[0] = 0;
617 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
618 info.fPhase = random->nextRangeScalar(0, info.fIntervals[1]);
619
620 effect = DashingCircleEffect::Create(edgeType, info, strokeWidth);
621 return effect;
622}
623
624//////////////////////////////////////////////////////////////////////////////
625
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000626class GLDashingLineEffect;
627
egdanielf767e792014-07-02 06:21:32 -0700628/*
629 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
630 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
631 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
632 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
633 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
634 * line at the origin then shifted to the right by half the off interval. The line then goes in the
635 * positive x direction.
636 */
egdaniele61c4112014-06-12 10:24:21 -0700637class DashingLineEffect : public GrVertexEffect {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000638public:
639 typedef SkPathEffect::DashInfo DashInfo;
640
bsalomon83d081a2014-07-08 09:56:10 -0700641 static GrEffect* Create(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000642
643 virtual ~DashingLineEffect();
644
645 static const char* Name() { return "DashingEffect"; }
646
647 GrEffectEdgeType getEdgeType() const { return fEdgeType; }
648
649 const SkRect& getRect() const { return fRect; }
650
651 SkScalar getIntervalLength() const { return fIntervalLength; }
652
653 typedef GLDashingLineEffect GLEffect;
654
655 virtual void getConstantColorComponents(GrColor* color, uint32_t* validFlags) const SK_OVERRIDE;
656
657 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE;
658
659private:
egdaniele61c4112014-06-12 10:24:21 -0700660 DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info, SkScalar strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000661
662 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE;
663
664 GrEffectEdgeType fEdgeType;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000665 SkRect fRect;
666 SkScalar fIntervalLength;
667
668 GR_DECLARE_EFFECT_TEST;
669
egdanielf767e792014-07-02 06:21:32 -0700670 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000671};
672
673//////////////////////////////////////////////////////////////////////////////
674
egdaniele61c4112014-06-12 10:24:21 -0700675class GLDashingLineEffect : public GrGLVertexEffect {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000676public:
677 GLDashingLineEffect(const GrBackendEffectFactory&, const GrDrawEffect&);
678
joshualitt30ba4362014-08-21 20:18:45 -0700679 virtual void emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000680 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700681 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000682 const char* outputColor,
683 const char* inputColor,
684 const TransformedCoordsArray&,
685 const TextureSamplerArray&) SK_OVERRIDE;
686
bsalomon63e99f72014-07-21 08:03:14 -0700687 static inline void GenKey(const GrDrawEffect&, const GrGLCaps&, GrEffectKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000688
kkinnunen7510b222014-07-30 00:04:16 -0700689 virtual void setData(const GrGLProgramDataManager&, const GrDrawEffect&) SK_OVERRIDE;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000690
691private:
kkinnunen7510b222014-07-30 00:04:16 -0700692 GrGLProgramDataManager::UniformHandle fRectUniform;
693 GrGLProgramDataManager::UniformHandle fIntervalUniform;
694 SkRect fPrevRect;
695 SkScalar fPrevIntervalLength;
egdaniele61c4112014-06-12 10:24:21 -0700696 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000697};
698
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000699GLDashingLineEffect::GLDashingLineEffect(const GrBackendEffectFactory& factory,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000700 const GrDrawEffect& drawEffect)
701 : INHERITED (factory) {
702 fPrevRect.fLeft = SK_ScalarNaN;
703 fPrevIntervalLength = SK_ScalarMax;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000704}
705
joshualitt30ba4362014-08-21 20:18:45 -0700706void GLDashingLineEffect::emitCode(GrGLFullProgramBuilder* builder,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000707 const GrDrawEffect& drawEffect,
bsalomon63e99f72014-07-21 08:03:14 -0700708 const GrEffectKey& key,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000709 const char* outputColor,
710 const char* inputColor,
egdaniele61c4112014-06-12 10:24:21 -0700711 const TransformedCoordsArray&,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000712 const TextureSamplerArray& samplers) {
713 const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>();
714 const char *rectName;
715 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
716 // respectively.
joshualitt30ba4362014-08-21 20:18:45 -0700717 fRectUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000718 kVec4f_GrSLType,
719 "rect",
720 &rectName);
721 const char *intervalName;
722 // The interval uniform's refers to the total length of the interval (on + off)
joshualitt30ba4362014-08-21 20:18:45 -0700723 fIntervalUniform = builder->addUniform(GrGLProgramBuilder::kFragment_Visibility,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000724 kFloat_GrSLType,
725 "interval",
726 &intervalName);
egdaniele61c4112014-06-12 10:24:21 -0700727
728 const char *vsCoordName, *fsCoordName;
729 builder->addVarying(kVec2f_GrSLType, "Coord", &vsCoordName, &fsCoordName);
joshualitt30ba4362014-08-21 20:18:45 -0700730 GrGLVertexShaderBuilder* vsBuilder = builder->getVertexShaderBuilder();
egdaniele61c4112014-06-12 10:24:21 -0700731 const SkString* attr0Name =
joshualitt30ba4362014-08-21 20:18:45 -0700732 vsBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
733 vsBuilder->codeAppendf("\t%s = %s;\n", vsCoordName, attr0Name->c_str());
egdaniele61c4112014-06-12 10:24:21 -0700734
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000735 // transforms all points so that we can compare them to our test rect
joshualitt30ba4362014-08-21 20:18:45 -0700736 GrGLFragmentShaderBuilder* fsBuilder = builder->getFragmentShaderBuilder();
737 fsBuilder->codeAppendf("\t\tfloat xShifted = %s.x - floor(%s.x / %s) * %s;\n",
egdaniele61c4112014-06-12 10:24:21 -0700738 fsCoordName, fsCoordName, intervalName, intervalName);
joshualitt30ba4362014-08-21 20:18:45 -0700739 fsBuilder->codeAppendf("\t\tvec2 fragPosShifted = vec2(xShifted, %s.y);\n", fsCoordName);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000740 if (GrEffectEdgeTypeIsAA(de.getEdgeType())) {
741 // 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 }
joshualitt30ba4362014-08-21 20:18:45 -0700757 fsBuilder->codeAppendf("\t\t%s = %s;\n", outputColor,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000758 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("alpha")).c_str());
759}
760
kkinnunen7510b222014-07-30 00:04:16 -0700761void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman, const GrDrawEffect& drawEffect) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762 const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>();
763 const SkRect& rect = de.getRect();
764 SkScalar intervalLength = de.getIntervalLength();
765 if (rect != fPrevRect || intervalLength != fPrevIntervalLength) {
kkinnunen7510b222014-07-30 00:04:16 -0700766 pdman.set4f(fRectUniform, rect.fLeft + 0.5f, rect.fTop + 0.5f,
767 rect.fRight - 0.5f, rect.fBottom - 0.5f);
768 pdman.set1f(fIntervalUniform, intervalLength);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000769 fPrevRect = rect;
770 fPrevIntervalLength = intervalLength;
771 }
772}
773
bsalomon63e99f72014-07-21 08:03:14 -0700774void GLDashingLineEffect::GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&,
775 GrEffectKeyBuilder* b) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000776 const DashingLineEffect& de = drawEffect.castEffect<DashingLineEffect>();
bsalomon63e99f72014-07-21 08:03:14 -0700777 b->add32(de.getEdgeType());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000778}
779
780//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000781
bsalomon83d081a2014-07-08 09:56:10 -0700782GrEffect* DashingLineEffect::Create(GrEffectEdgeType edgeType, const DashInfo& info,
783 SkScalar strokeWidth) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000784 if (info.fCount != 2) {
785 return NULL;
786 }
787
bsalomon55fad7a2014-07-08 07:34:20 -0700788 return SkNEW_ARGS(DashingLineEffect, (edgeType, info, strokeWidth));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000789}
790
791DashingLineEffect::~DashingLineEffect() {}
792
793void DashingLineEffect::getConstantColorComponents(GrColor* color, uint32_t* validFlags) const {
794 *validFlags = 0;
795}
796
797const GrBackendEffectFactory& DashingLineEffect::getFactory() const {
798 return GrTBackendEffectFactory<DashingLineEffect>::getInstance();
799}
800
801DashingLineEffect::DashingLineEffect(GrEffectEdgeType edgeType, const DashInfo& info,
egdaniele61c4112014-06-12 10:24:21 -0700802 SkScalar strokeWidth)
803 : fEdgeType(edgeType) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000804 SkScalar onLen = info.fIntervals[0];
805 SkScalar offLen = info.fIntervals[1];
806 SkScalar halfOffLen = SkScalarHalf(offLen);
807 SkScalar halfStroke = SkScalarHalf(strokeWidth);
808 fIntervalLength = onLen + offLen;
809 fRect.set(halfOffLen, -halfStroke, halfOffLen + onLen, halfStroke);
810
egdaniele61c4112014-06-12 10:24:21 -0700811 this->addVertexAttrib(kVec2f_GrSLType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000812}
813
814bool DashingLineEffect::onIsEqual(const GrEffect& other) const {
815 const DashingLineEffect& de = CastEffect<DashingLineEffect>(other);
816 return (fEdgeType == de.fEdgeType &&
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000817 fRect == de.fRect &&
818 fIntervalLength == de.fIntervalLength);
819}
820
821GR_DEFINE_EFFECT_TEST(DashingLineEffect);
822
bsalomon83d081a2014-07-08 09:56:10 -0700823GrEffect* DashingLineEffect::TestCreate(SkRandom* random,
824 GrContext*,
825 const GrDrawTargetCaps& caps,
826 GrTexture*[]) {
827 GrEffect* effect;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000828 GrEffectEdgeType edgeType = static_cast<GrEffectEdgeType>(random->nextULessThan(
829 kGrEffectEdgeTypeCnt));
830 SkScalar strokeWidth = random->nextRangeScalar(0, 100.f);
831 DashInfo info;
832 info.fCount = 2;
833 SkAutoTArray<SkScalar> intervals(info.fCount);
834 info.fIntervals = intervals.get();
835 info.fIntervals[0] = random->nextRangeScalar(0, 10.f);
836 info.fIntervals[1] = random->nextRangeScalar(0, 10.f);
837 info.fPhase = random->nextRangeScalar(0, info.fIntervals[0] + info.fIntervals[1]);
838
egdaniele61c4112014-06-12 10:24:21 -0700839 effect = DashingLineEffect::Create(edgeType, info, strokeWidth);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000840 return effect;
841}
842
843//////////////////////////////////////////////////////////////////////////////
844
bsalomon83d081a2014-07-08 09:56:10 -0700845GrEffect* GrDashingEffect::Create(GrEffectEdgeType edgeType, const SkPathEffect::DashInfo& info,
846 SkScalar strokeWidth, GrDashingEffect::DashCap cap) {
egdanielf767e792014-07-02 06:21:32 -0700847 switch (cap) {
848 case GrDashingEffect::kRound_DashCap:
849 return DashingCircleEffect::Create(edgeType, info, SkScalarHalf(strokeWidth));
850 case GrDashingEffect::kNonRound_DashCap:
851 return DashingLineEffect::Create(edgeType, info, strokeWidth);
852 default:
853 SkFAIL("Unexpected dashed cap.");
854 }
855 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000856}