blob: b6dbbd6e0ed19f877ca6c3c5b391e7334fe0923d [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
joshualitt4f569be2015-02-27 11:41:49 -080010#include "GrBatch.h"
11#include "GrBatchTarget.h"
12#include "GrBufferAllocPool.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000014#include "GrContext.h"
15#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080016#include "GrDefaultGeoProcFactory.h"
egdaniele61c4112014-06-12 10:24:21 -070017#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000018#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080019#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070020#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070021#include "GrStrokeInfo.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000022#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "gl/GrGLGeometryProcessor.h"
24#include "gl/GrGLProcessor.h"
25#include "gl/GrGLSL.h"
26#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000027
28///////////////////////////////////////////////////////////////////////////////
29
egdaniele61c4112014-06-12 10:24:21 -070030// Returns whether or not the gpu can fast path the dash line effect.
kkinnunen18996512015-04-26 23:18:49 -070031bool GrDashingEffect::CanDrawDashLine(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
32 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070033 // Pts must be either horizontal or vertical in src space
34 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
35 return false;
36 }
37
38 // May be able to relax this to include skew. As of now cannot do perspective
39 // because of the non uniform scaling of bloating a rect
40 if (!viewMatrix.preservesRightAngles()) {
41 return false;
42 }
43
44 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
45 return false;
46 }
47
48 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
49 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
50 return false;
51 }
52
53 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
54 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070055 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070056 return false;
57 }
58
59 return true;
60}
61
62namespace {
egdaniele61c4112014-06-12 10:24:21 -070063struct DashLineVertex {
64 SkPoint fPos;
65 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080066 SkScalar fIntervalLength;
67 SkRect fRect;
68};
69struct DashCircleVertex {
70 SkPoint fPos;
71 SkPoint fDashPos;
72 SkScalar fIntervalLength;
73 SkScalar fRadius;
74 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070075};
senorblancof3c2c462015-04-20 14:44:26 -070076
77enum DashAAMode {
78 kBW_DashAAMode,
79 kEdgeAA_DashAAMode,
80 kMSAA_DashAAMode,
81
82 kDashAAModeCount,
83};
egdaniele61c4112014-06-12 10:24:21 -070084};
85
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000086static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
87 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
88 SkVector vecSrc = pts[1] - pts[0];
89 SkScalar magSrc = vecSrc.length();
90 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
91 vecSrc.scale(invSrc);
92
93 SkVector vecSrcPerp;
94 vecSrc.rotateCW(&vecSrcPerp);
95 viewMatrix.mapVectors(&vecSrc, 1);
96 viewMatrix.mapVectors(&vecSrcPerp, 1);
97
98 // parallelScale tells how much to scale along the line parallel to the dash line
99 // perpScale tells how much to scale in the direction perpendicular to the dash line
100 *parallelScale = vecSrc.length();
101 *perpScale = vecSrcPerp.length();
102}
103
104// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
105// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
106static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
107 SkVector vec = pts[1] - pts[0];
108 SkScalar mag = vec.length();
109 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
110
111 vec.scale(inv);
112 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
113 if (ptsRot) {
114 rotMatrix->mapPoints(ptsRot, pts, 2);
115 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
116 ptsRot[1].fY = pts[0].fY;
117 }
118}
119
120// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800121static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
122 SkASSERT(phase < intervals[0] + intervals[1]);
123 if (phase >= intervals[0] && phase != 0) {
124 SkScalar srcIntervalLen = intervals[0] + intervals[1];
125 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000126 }
127 return 0;
128}
129
joshualitt4f569be2015-02-27 11:41:49 -0800130static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700131 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000132 if (pts[1].fX <= pts[0].fX) {
133 return 0;
134 }
joshualitt4f569be2015-02-27 11:41:49 -0800135 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000136 SkScalar totalLen = pts[1].fX - pts[0].fX;
137 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
138 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700139 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000140 temp = SkScalarDiv(*endingInt, srcIntervalLen);
141 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
142 if (0 == *endingInt) {
143 *endingInt = srcIntervalLen;
144 }
joshualitt4f569be2015-02-27 11:41:49 -0800145 if (*endingInt > intervals[0]) {
146 if (0 == intervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000147 *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 +0000148 }
joshualitt4f569be2015-02-27 11:41:49 -0800149 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000150 }
151 return 0;
152}
153
joshualitt5224ba72015-02-03 15:07:51 -0800154enum DashCap {
155 kRound_DashCap,
156 kNonRound_DashCap,
157};
158
159static int kDashVertices = 4;
160
161template <typename T>
162void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700163 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
164 SkScalar stroke) {
165 SkScalar startDashX = offset - bloatX;
166 SkScalar endDashX = offset + len + bloatX;
167 SkScalar startDashY = -stroke - bloatY;
168 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800169 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
170 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
171 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
172 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
173
174 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
175 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
176 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
177 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
178
179 matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4);
180}
181
182static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700183 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
184 SkScalar bloatY, SkScalar len, SkScalar stroke,
185 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
186 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800187 SkScalar intervalLength = startInterval + endInterval;
188
189 if (kRound_DashCap == cap) {
190 SkASSERT(vertexStride == sizeof(DashCircleVertex));
191 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
192
senorblancof3c2c462015-04-20 14:44:26 -0700193 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
194 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800195
196 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
197 SkScalar centerX = SkScalarHalf(endInterval);
198
199 for (int i = 0; i < kDashVertices; i++) {
200 verts[idx + i].fIntervalLength = intervalLength;
201 verts[idx + i].fRadius = radius;
202 verts[idx + i].fCenterX = centerX;
203 }
204
205 } else {
206 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
207 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
208
senorblancof3c2c462015-04-20 14:44:26 -0700209 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
210 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800211
212 SkScalar halfOffLen = SkScalarHalf(endInterval);
213 SkScalar halfStroke = SkScalarHalf(strokeWidth);
214 SkRect rectParam;
215 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
216 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
217 for (int i = 0; i < kDashVertices; i++) {
218 verts[idx + i].fIntervalLength = intervalLength;
219 verts[idx + i].fRect = rectParam;
220 }
221 }
egdaniele61c4112014-06-12 10:24:21 -0700222}
223
joshualitt5478d422014-11-14 16:00:38 -0800224static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
225 SkPoint* verts) {
226 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
227 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
228 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
229 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
230 matrix.mapPoints(&verts[idx], 4);
231}
egdaniele61c4112014-06-12 10:24:21 -0700232
joshualitt5224ba72015-02-03 15:07:51 -0800233
234/**
235 * An GrGeometryProcessor that renders a dashed line.
236 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
237 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
238 * position relative to the dashed line.
239 */
240static GrGeometryProcessor* create_dash_gp(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700241 DashAAMode aaMode,
joshualitt5224ba72015-02-03 15:07:51 -0800242 DashCap cap,
243 const SkMatrix& localMatrix);
244
joshualitt4f569be2015-02-27 11:41:49 -0800245class DashBatch : public GrBatch {
246public:
247 struct Geometry {
248 GrColor fColor;
249 SkMatrix fViewMatrix;
250 SkMatrix fSrcRotInv;
251 SkPoint fPtsRot[2];
252 SkScalar fSrcStrokeWidth;
253 SkScalar fPhase;
254 SkScalar fIntervals[2];
255 SkScalar fParallelScale;
256 SkScalar fPerpendicularScale;
257 SkDEBUGCODE(SkRect fDevBounds;)
258 };
259
senorblancof3c2c462015-04-20 14:44:26 -0700260 static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
261 return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800262 }
263
mtklein36352bf2015-03-25 18:17:31 -0700264 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800265
mtklein36352bf2015-03-25 18:17:31 -0700266 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800267 // When this is called on a batch, there is only one geometry bundle
268 out->setKnownFourComponents(fGeoData[0].fColor);
269 }
mtklein36352bf2015-03-25 18:17:31 -0700270 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800271 out->setUnknownSingleComponent();
272 }
273
mtklein36352bf2015-03-25 18:17:31 -0700274 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt4f569be2015-02-27 11:41:49 -0800275 // Handle any color overrides
276 if (init.fColorIgnored) {
277 fGeoData[0].fColor = GrColor_ILLEGAL;
278 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
279 fGeoData[0].fColor = init.fOverrideColor;
280 }
281
282 // setup batch properties
283 fBatch.fColorIgnored = init.fColorIgnored;
284 fBatch.fColor = fGeoData[0].fColor;
285 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
286 fBatch.fCoverageIgnored = init.fCoverageIgnored;
287 }
288
289 struct DashDraw {
290 SkScalar fStartOffset;
291 SkScalar fStrokeWidth;
292 SkScalar fLineLength;
293 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700294 SkScalar fDevBloatX;
295 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800296 bool fLineDone;
297 bool fHasStartRect;
298 bool fHasEndRect;
299 };
300
mtklein36352bf2015-03-25 18:17:31 -0700301 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt4f569be2015-02-27 11:41:49 -0800302 int instanceCount = fGeoData.count();
303
304 SkMatrix invert;
305 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
306 SkDebugf("Failed to invert\n");
307 return;
308 }
309
310 SkPaint::Cap cap = this->cap();
311
312 SkAutoTUnref<const GrGeometryProcessor> gp;
313
314 bool isRoundCap = SkPaint::kRound_Cap == cap;
315 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
316 if (this->fullDash()) {
senorblancof3c2c462015-04-20 14:44:26 -0700317 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert));
joshualitt4f569be2015-02-27 11:41:49 -0800318 } else {
319 // Set up the vertex data for the line and start/end dashes
320 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
321 this->color(),
322 SkMatrix::I(),
323 invert));
324 }
325
326 batchTarget->initDraw(gp, pipeline);
327
328 // TODO remove this when batch is everywhere
329 GrPipelineInfo init;
330 init.fColorIgnored = fBatch.fColorIgnored;
331 init.fOverrideColor = GrColor_ILLEGAL;
332 init.fCoverageIgnored = fBatch.fCoverageIgnored;
333 init.fUsesLocalCoords = this->usesLocalCoords();
334 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
335
senorblancof3c2c462015-04-20 14:44:26 -0700336 // useAA here means Edge AA or MSAA
337 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800338 bool fullDash = this->fullDash();
339
340 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
341 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
342 // iterate again over these decomposed dashes to generate vertices
343 SkSTArray<128, SkRect, true> rects;
344 SkSTArray<128, DashDraw, true> draws;
345
346 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800347 int rectOffset = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800348 for (int i = 0; i < instanceCount; i++) {
349 Geometry& args = fGeoData[i];
350
351 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
352
353 // We always want to at least stroke out half a pixel on each side in device space
354 // so 0.5f / perpScale gives us this min in src space
355 SkScalar halfSrcStroke =
356 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
357
358 SkScalar strokeAdj;
359 if (!hasCap) {
360 strokeAdj = 0.f;
361 } else {
362 strokeAdj = halfSrcStroke;
363 }
364
365 SkScalar startAdj = 0;
366
367 SkMatrix& combinedMatrix = args.fSrcRotInv;
368 combinedMatrix.postConcat(args.fViewMatrix);
369
370 bool lineDone = false;
371
372 // Too simplify the algorithm, we always push back rects for start and end rect.
373 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800374 rects.push_back();
375 rects.push_back();
376 rects.push_back();
377 SkRect& bounds = rects[rectOffset++];
378 SkRect& startRect = rects[rectOffset++];
379 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800380
381 bool hasStartRect = false;
382 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
383 // draw it separately here and adjust our start point accordingly
384 if (useAA) {
385 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
386 SkPoint startPts[2];
387 startPts[0] = args.fPtsRot[0];
388 startPts[1].fY = startPts[0].fY;
389 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
390 args.fPtsRot[1].fX);
391 startRect.set(startPts, 2);
392 startRect.outset(strokeAdj, halfSrcStroke);
393
394 hasStartRect = true;
395 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
396 }
397 }
398
399 // adjustments for start and end of bounding rect so we only draw dash intervals
400 // contained in the original line segment.
401 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
402 if (startAdj != 0) {
403 args.fPtsRot[0].fX += startAdj;
404 args.fPhase = 0;
405 }
406 SkScalar endingInterval = 0;
407 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
408 &endingInterval);
409 args.fPtsRot[1].fX -= endAdj;
410 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
411 lineDone = true;
412 }
413
414 bool hasEndRect = false;
415 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
416 // draw it separately here and adjust our end point accordingly
417 if (useAA && !lineDone) {
418 // If we adjusted the end then we will not be drawing a partial dash at the end.
419 // If we didn't adjust the end point then we just need to make sure the ending
420 // dash isn't a full dash
421 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
422 SkPoint endPts[2];
423 endPts[1] = args.fPtsRot[1];
424 endPts[0].fY = endPts[1].fY;
425 endPts[0].fX = endPts[1].fX - endingInterval;
426
427 endRect.set(endPts, 2);
428 endRect.outset(strokeAdj, halfSrcStroke);
429
430 hasEndRect = true;
431 endAdj = endingInterval + args.fIntervals[1];
432
433 args.fPtsRot[1].fX -= endAdj;
434 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
435 lineDone = true;
436 }
437 }
438 }
439
440 if (startAdj != 0) {
441 args.fPhase = 0;
442 }
443
444 // Change the dashing info from src space into device space
445 SkScalar* devIntervals = args.fIntervals;
446 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
447 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
448 SkScalar devPhase = args.fPhase * args.fParallelScale;
449 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
450
senorblancof3c2c462015-04-20 14:44:26 -0700451 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800452 strokeWidth = 1.f;
453 }
454
455 SkScalar halfDevStroke = strokeWidth * 0.5f;
456
457 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
458 // add cap to on interval and remove from off interval
459 devIntervals[0] += strokeWidth;
460 devIntervals[1] -= strokeWidth;
461 }
462 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
463
senorblancof3c2c462015-04-20 14:44:26 -0700464 // For EdgeAA, we bloat in X & Y for both square and round caps.
465 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
466 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
467 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
468 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800469
senorblancof3c2c462015-04-20 14:44:26 -0700470 SkScalar bloatX = devBloatX / args.fParallelScale;
471 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800472
473 if (devIntervals[1] <= 0.f && useAA) {
474 // Case when we end up drawing a solid AA rect
475 // Reset the start rect to draw this single solid rect
476 // but it requires to upload a new intervals uniform so we can mimic
477 // one giant dash
478 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
479 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
480 startRect.set(args.fPtsRot, 2);
481 startRect.outset(strokeAdj, halfSrcStroke);
482 hasStartRect = true;
483 hasEndRect = false;
484 lineDone = true;
485
486 SkPoint devicePts[2];
487 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
488 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
489 if (hasCap) {
490 lineLength += 2.f * halfDevStroke;
491 }
492 devIntervals[0] = lineLength;
493 }
494
495 totalRectCount += !lineDone ? 1 : 0;
496 totalRectCount += hasStartRect ? 1 : 0;
497 totalRectCount += hasEndRect ? 1 : 0;
498
499 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
500 // need to adjust this for round caps to correctly set the dashPos attrib on
501 // vertices
502 startOffset -= halfDevStroke;
503 }
504
505 DashDraw& draw = draws.push_back();
506 if (!lineDone) {
507 SkPoint devicePts[2];
508 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
509 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
510 if (hasCap) {
511 draw.fLineLength += 2.f * halfDevStroke;
512 }
513
514 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
515 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
516 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
517 }
518
519 if (hasStartRect) {
520 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
521 startRect.outset(bloatX, bloatY);
522 }
523
524 if (hasEndRect) {
525 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
526 endRect.outset(bloatX, bloatY);
527 }
528
529 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700530 draw.fDevBloatX = devBloatX;
531 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800532 draw.fHalfDevStroke = halfDevStroke;
533 draw.fStrokeWidth = strokeWidth;
534 draw.fHasStartRect = hasStartRect;
535 draw.fLineDone = lineDone;
536 draw.fHasEndRect = hasEndRect;
537 }
538
539 const GrVertexBuffer* vertexBuffer;
540 int firstVertex;
541
542 size_t vertexStride = gp->getVertexStride();
543 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
544 totalRectCount * kVertsPerDash,
545 &vertexBuffer,
546 &firstVertex);
547
joshualitt4b31de82015-03-05 14:33:41 -0800548 if (!vertices || !batchTarget->quadIndexBuffer()) {
549 SkDebugf("Could not allocate buffers\n");
550 return;
551 }
552
joshualitt4f569be2015-02-27 11:41:49 -0800553 int curVIdx = 0;
554 int rectIndex = 0;
555 for (int i = 0; i < instanceCount; i++) {
556 Geometry& args = fGeoData[i];
557
558 if (!draws[i].fLineDone) {
559 if (fullDash) {
560 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700561 draws[i].fStartOffset, draws[i].fDevBloatX,
562 draws[i].fDevBloatY, draws[i].fLineLength,
563 draws[i].fHalfDevStroke, args.fIntervals[0],
564 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800565 capType, gp->getVertexStride());
566 } else {
567 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
568 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
569 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
570 }
571 curVIdx += 4;
572 }
573 rectIndex++;
574
575 if (draws[i].fHasStartRect) {
576 if (fullDash) {
577 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700578 draws[i].fStartOffset, draws[i].fDevBloatX,
579 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800580 draws[i].fHalfDevStroke, args.fIntervals[0],
581 args.fIntervals[1], draws[i].fStrokeWidth, capType,
582 gp->getVertexStride());
583 } else {
584 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
585 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
586 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
587 }
588
589 curVIdx += 4;
590 }
591 rectIndex++;
592
593 if (draws[i].fHasEndRect) {
594 if (fullDash) {
595 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700596 draws[i].fStartOffset, draws[i].fDevBloatX,
597 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800598 draws[i].fHalfDevStroke, args.fIntervals[0],
599 args.fIntervals[1], draws[i].fStrokeWidth, capType,
600 gp->getVertexStride());
601 } else {
602 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
603 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
604 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
605 }
606 curVIdx += 4;
607 }
608 rectIndex++;
609 }
610
611 const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer();
612
613 GrDrawTarget::DrawInfo drawInfo;
614 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
615 drawInfo.setStartVertex(0);
616 drawInfo.setStartIndex(0);
617 drawInfo.setVerticesPerInstance(kVertsPerDash);
618 drawInfo.setIndicesPerInstance(kIndicesPerDash);
619 drawInfo.adjustStartVertex(firstVertex);
620 drawInfo.setVertexBuffer(vertexBuffer);
621 drawInfo.setIndexBuffer(dashIndexBuffer);
622
623 int maxInstancesPerDraw = dashIndexBuffer->maxQuads();
624 while (totalRectCount) {
625 drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw));
626 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance());
627 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance());
628
629 batchTarget->draw(drawInfo);
630
631 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount());
632 totalRectCount -= drawInfo.instanceCount();
633 }
634 }
635
636 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
637
638private:
senorblancof3c2c462015-04-20 14:44:26 -0700639 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
joshualitt4f569be2015-02-27 11:41:49 -0800640 this->initClassID<DashBatch>();
641 fGeoData.push_back(geometry);
642
senorblancof3c2c462015-04-20 14:44:26 -0700643 fBatch.fAAMode = aaMode;
joshualitt4f569be2015-02-27 11:41:49 -0800644 fBatch.fCap = cap;
645 fBatch.fFullDash = fullDash;
646 }
647
mtklein36352bf2015-03-25 18:17:31 -0700648 bool onCombineIfPossible(GrBatch* t) override {
joshualitt4f569be2015-02-27 11:41:49 -0800649 DashBatch* that = t->cast<DashBatch>();
650
senorblancof3c2c462015-04-20 14:44:26 -0700651 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800652 return false;
653 }
654
655 if (this->fullDash() != that->fullDash()) {
656 return false;
657 }
658
659 if (this->cap() != that->cap()) {
660 return false;
661 }
662
663 // TODO vertex color
664 if (this->color() != that->color()) {
665 return false;
666 }
667
668 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
669 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
670 return false;
671 }
672
673 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
674 return true;
675 }
676
677 GrColor color() const { return fBatch.fColor; }
678 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
679 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700680 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800681 bool fullDash() const { return fBatch.fFullDash; }
682 SkPaint::Cap cap() const { return fBatch.fCap; }
683
684 struct BatchTracker {
685 GrColor fColor;
686 bool fUsesLocalCoords;
687 bool fColorIgnored;
688 bool fCoverageIgnored;
689 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700690 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800691 bool fFullDash;
692 };
693
694 static const int kVertsPerDash = 4;
695 static const int kIndicesPerDash = 6;
696
697 BatchTracker fBatch;
698 SkSTArray<1, Geometry, true> fGeoData;
699};
700
701
egdaniel8dd688b2015-01-22 10:16:09 -0800702bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
703 GrPipelineBuilder* pipelineBuilder, GrColor color,
704 const SkMatrix& viewMatrix, const SkPoint pts[2],
kkinnunen18996512015-04-26 23:18:49 -0700705 bool useAA, const GrStrokeInfo& strokeInfo) {
egdaniele61c4112014-06-12 10:24:21 -0700706 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000707
egdaniele61c4112014-06-12 10:24:21 -0700708 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000709
joshualitt4f569be2015-02-27 11:41:49 -0800710 DashBatch::Geometry geometry;
711 geometry.fSrcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000712
713 // the phase should be normalized to be [0, sum of all intervals)
714 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
715
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000716 // Rotate the src pts so they are aligned horizontally with pts[0].fX < pts[1].fX
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000717 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700718 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800719 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
720 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700721 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000722 return false;
723 }
724 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800725 geometry.fSrcRotInv.reset();
726 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
727 }
728
729 // Scale corrections of intervals and stroke from view matrix
730 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
731 geometry.fPtsRot);
732
733 SkScalar offInterval = info.fIntervals[1] * geometry.fParallelScale;
734 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
735
736 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
737 // add cap to on interveal and remove from off interval
738 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000739 }
740
senorblancof3c2c462015-04-20 14:44:26 -0700741 DashAAMode aaMode = pipelineBuilder->getRenderTarget()->isMultisampled() ? kMSAA_DashAAMode :
kkinnunen18996512015-04-26 23:18:49 -0700742 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
senorblancof3c2c462015-04-20 14:44:26 -0700743
joshualitt4f569be2015-02-27 11:41:49 -0800744 // TODO we can do a real rect call if not using fulldash(ie no off interval, not using AA)
senorblancof3c2c462015-04-20 14:44:26 -0700745 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000746
joshualitt4f569be2015-02-27 11:41:49 -0800747 geometry.fColor = color;
748 geometry.fViewMatrix = viewMatrix;
749 geometry.fPhase = info.fPhase;
750 geometry.fIntervals[0] = info.fIntervals[0];
751 geometry.fIntervals[1] = info.fIntervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000752
senorblancof3c2c462015-04-20 14:44:26 -0700753 SkAutoTUnref<GrBatch> batch(DashBatch::Create(geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800754 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000755
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000756 return true;
757}
758
759//////////////////////////////////////////////////////////////////////////////
760
egdanielf767e792014-07-02 06:21:32 -0700761class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800762
763struct DashingCircleBatchTracker {
764 GrGPInput fInputColorType;
765 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800766 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800767};
768
egdanielf767e792014-07-02 06:21:32 -0700769/*
770 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
771 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
772 * Both of the previous two parameters are in device space. This effect also requires the setting of
773 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
774 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
775 * transform the line to be horizontal, with the start of line at the origin then shifted to the
776 * right by half the off interval. The line then goes in the positive x direction.
777 */
joshualitt249af152014-09-15 11:41:13 -0700778class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700779public:
780 typedef SkPathEffect::DashInfo DashInfo;
781
joshualitt2e3b3e32014-12-09 13:31:14 -0800782 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700783 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800784 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700785
786 virtual ~DashingCircleEffect();
787
mtklein36352bf2015-03-25 18:17:31 -0700788 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700789
joshualitt71c92602015-01-14 08:12:47 -0800790 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800791
joshualitt5224ba72015-02-03 15:07:51 -0800792 const Attribute* inDashParams() const { return fInDashParams; }
793
794 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700795
senorblancof3c2c462015-04-20 14:44:26 -0700796 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700797
joshualitteb2a6762014-12-04 11:35:33 -0800798 virtual void getGLProcessorKey(const GrBatchTracker&,
799 const GrGLCaps&,
mtklein36352bf2015-03-25 18:17:31 -0700800 GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700801
joshualittabb52a12015-01-13 15:02:10 -0800802 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
mtklein36352bf2015-03-25 18:17:31 -0700803 const GrGLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700804
mtklein36352bf2015-03-25 18:17:31 -0700805 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -0800806
joshualitt290c09b2014-12-19 13:45:20 -0800807 bool onCanMakeEqual(const GrBatchTracker&,
808 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700809 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800810
egdanielf767e792014-07-02 06:21:32 -0700811private:
senorblancof3c2c462015-04-20 14:44:26 -0700812 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700813
mtklein36352bf2015-03-25 18:17:31 -0700814 bool onIsEqual(const GrGeometryProcessor& other) const override;
egdanielf767e792014-07-02 06:21:32 -0700815
mtklein36352bf2015-03-25 18:17:31 -0700816 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700817
senorblancof3c2c462015-04-20 14:44:26 -0700818 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800819 const Attribute* fInPosition;
820 const Attribute* fInDashParams;
821 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700822
joshualittb0a8a372014-09-23 09:50:21 -0700823 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700824
joshualitt249af152014-09-15 11:41:13 -0700825 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700826};
827
828//////////////////////////////////////////////////////////////////////////////
829
joshualitt249af152014-09-15 11:41:13 -0700830class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700831public:
joshualitteb2a6762014-12-04 11:35:33 -0800832 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700833
mtklein36352bf2015-03-25 18:17:31 -0700834 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700835
joshualitt87f48d92014-12-04 10:41:40 -0800836 static inline void GenKey(const GrGeometryProcessor&,
837 const GrBatchTracker&,
838 const GrGLCaps&,
839 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700840
joshualitt87f48d92014-12-04 10:41:40 -0800841 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800842 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700843 const GrBatchTracker&) override;
egdanielf767e792014-07-02 06:21:32 -0700844
845private:
joshualitt9b989322014-12-15 14:16:27 -0800846 UniformHandle fParamUniform;
847 UniformHandle fColorUniform;
848 GrColor fColor;
849 SkScalar fPrevRadius;
850 SkScalar fPrevCenterX;
851 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700852 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700853};
854
joshualitteb2a6762014-12-04 11:35:33 -0800855GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
856 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800857 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700858 fPrevRadius = SK_ScalarMin;
859 fPrevCenterX = SK_ScalarMin;
860 fPrevIntervalLength = SK_ScalarMax;
861}
862
robertphillips46d36f02015-01-18 08:14:14 -0800863void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700864 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800865 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
866 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800867 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
868
joshualittabb52a12015-01-13 15:02:10 -0800869 // emit attributes
870 vsBuilder->emitAttributes(dce);
871
joshualitt5224ba72015-02-03 15:07:51 -0800872 // XY are dashPos, Z is dashInterval
873 GrGLVertToFrag dashParams(kVec3f_GrSLType);
874 args.fPB->addVarying("DashParam", &dashParams);
875 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
876
senorblancof3c2c462015-04-20 14:44:26 -0700877 // x refers to circle radius - 0.5, y refers to cicle's center x coord
joshualitt5224ba72015-02-03 15:07:51 -0800878 GrGLVertToFrag circleParams(kVec2f_GrSLType);
879 args.fPB->addVarying("CircleParams", &circleParams);
880 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700881
joshualitt9b989322014-12-15 14:16:27 -0800882 // Setup pass through color
883 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
884
joshualittabb52a12015-01-13 15:02:10 -0800885 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800886 this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800887
joshualittabb52a12015-01-13 15:02:10 -0800888 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800889 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800890 args.fTransformsIn, args.fTransformsOut);
891
egdanielf767e792014-07-02 06:21:32 -0700892 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700893 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800894 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
895 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
896 dashParams.fsIn());
897 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
898 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
899 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700900 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800901 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
902 fsBuilder->codeAppend("diff = 1.0 - diff;");
903 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700904 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800905 fsBuilder->codeAppendf("float alpha = 1.0;");
906 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700907 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800908 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700909}
910
joshualitt87f48d92014-12-04 10:41:40 -0800911void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800912 const GrPrimitiveProcessor& processor,
913 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800914 this->setUniformViewMatrix(pdman, processor.viewMatrix());
915
joshualitt9b989322014-12-15 14:16:27 -0800916 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
917 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
918 GrGLfloat c[4];
919 GrColorToRGBAFloat(local.fColor, c);
920 pdman.set4fv(fColorUniform, 1, c);
921 fColor = local.fColor;
922 }
egdanielf767e792014-07-02 06:21:32 -0700923}
924
robertphillips46d36f02015-01-18 08:14:14 -0800925void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800926 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800927 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700928 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800929 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800930 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
931 uint32_t key = 0;
932 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
933 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -0700934 key |= dce.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -0800935 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700936}
937
938//////////////////////////////////////////////////////////////////////////////
939
joshualitt2e3b3e32014-12-09 13:31:14 -0800940GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700941 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800942 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -0700943 return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700944}
945
946DashingCircleEffect::~DashingCircleEffect() {}
947
joshualitt56995b52014-12-11 15:44:02 -0800948void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
949 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700950}
951
joshualitteb2a6762014-12-04 11:35:33 -0800952void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
953 const GrGLCaps& caps,
954 GrProcessorKeyBuilder* b) const {
955 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
956}
957
joshualittabb52a12015-01-13 15:02:10 -0800958GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
959 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800960 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700961}
962
joshualitt2e3b3e32014-12-09 13:31:14 -0800963DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700964 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800965 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -0700966 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800967 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800968 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800969 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
970 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
971 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700972}
973
bsalomon0e08fc12014-10-15 08:19:04 -0700974bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700975 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
senorblancof3c2c462015-04-20 14:44:26 -0700976 return fAAMode == dce.fAAMode;
egdanielf767e792014-07-02 06:21:32 -0700977}
978
joshualitt4d8da812015-01-28 12:53:54 -0800979void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800980 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
981 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800982 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800983}
984
joshualitt290c09b2014-12-19 13:45:20 -0800985bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
986 const GrGeometryProcessor& that,
987 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800988 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
989 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800990 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
991 that, theirs.fUsesLocalCoords) &&
992 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800993 theirs.fInputColorType, theirs.fColor);
994}
995
joshualittb0a8a372014-09-23 09:50:21 -0700996GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700997
joshualittb0a8a372014-09-23 09:50:21 -0700998GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
999 GrContext*,
1000 const GrDrawTargetCaps& caps,
1001 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001002 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001003 return DashingCircleEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001004 aaMode, GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -07001005}
1006
1007//////////////////////////////////////////////////////////////////////////////
1008
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001009class GLDashingLineEffect;
1010
joshualitt9b989322014-12-15 14:16:27 -08001011struct DashingLineBatchTracker {
1012 GrGPInput fInputColorType;
1013 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -08001014 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001015};
1016
egdanielf767e792014-07-02 06:21:32 -07001017/*
1018 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1019 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
1020 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
1021 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1022 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1023 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1024 * positive x direction.
1025 */
joshualitt249af152014-09-15 11:41:13 -07001026class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001027public:
1028 typedef SkPathEffect::DashInfo DashInfo;
1029
joshualitt2e3b3e32014-12-09 13:31:14 -08001030 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001031 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001032 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001033
1034 virtual ~DashingLineEffect();
1035
mtklein36352bf2015-03-25 18:17:31 -07001036 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001037
joshualitt71c92602015-01-14 08:12:47 -08001038 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001039
joshualitt5224ba72015-02-03 15:07:51 -08001040 const Attribute* inDashParams() const { return fInDashParams; }
1041
1042 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001043
senorblancof3c2c462015-04-20 14:44:26 -07001044 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001045
joshualitteb2a6762014-12-04 11:35:33 -08001046 virtual void getGLProcessorKey(const GrBatchTracker& bt,
1047 const GrGLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -07001048 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001049
joshualittabb52a12015-01-13 15:02:10 -08001050 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
mtklein36352bf2015-03-25 18:17:31 -07001051 const GrGLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001052
mtklein36352bf2015-03-25 18:17:31 -07001053 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -08001054
joshualitt290c09b2014-12-19 13:45:20 -08001055 bool onCanMakeEqual(const GrBatchTracker&,
1056 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001057 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -08001058
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001059private:
senorblancof3c2c462015-04-20 14:44:26 -07001060 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001061
mtklein36352bf2015-03-25 18:17:31 -07001062 bool onIsEqual(const GrGeometryProcessor& other) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001063
mtklein36352bf2015-03-25 18:17:31 -07001064 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -07001065
senorblancof3c2c462015-04-20 14:44:26 -07001066 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001067 const Attribute* fInPosition;
1068 const Attribute* fInDashParams;
1069 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001070
joshualittb0a8a372014-09-23 09:50:21 -07001071 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001072
joshualitt249af152014-09-15 11:41:13 -07001073 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001074};
1075
1076//////////////////////////////////////////////////////////////////////////////
1077
joshualitt249af152014-09-15 11:41:13 -07001078class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001079public:
joshualitteb2a6762014-12-04 11:35:33 -08001080 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001081
mtklein36352bf2015-03-25 18:17:31 -07001082 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001083
joshualitt87f48d92014-12-04 10:41:40 -08001084 static inline void GenKey(const GrGeometryProcessor&,
1085 const GrBatchTracker&,
1086 const GrGLCaps&,
1087 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001088
joshualitt87f48d92014-12-04 10:41:40 -08001089 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -08001090 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001091 const GrBatchTracker&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001092
1093private:
joshualitt9b989322014-12-15 14:16:27 -08001094 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001095 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001096 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001097};
1098
joshualitteb2a6762014-12-04 11:35:33 -08001099GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
1100 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -08001101 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001102}
1103
robertphillips46d36f02015-01-18 08:14:14 -08001104void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001105 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -08001106 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
1107 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001108
1109 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
1110
joshualittabb52a12015-01-13 15:02:10 -08001111 // emit attributes
1112 vsBuilder->emitAttributes(de);
1113
joshualitt5224ba72015-02-03 15:07:51 -08001114 // XY refers to dashPos, Z is the dash interval length
1115 GrGLVertToFrag inDashParams(kVec3f_GrSLType);
1116 args.fPB->addVarying("DashParams", &inDashParams);
1117 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1118
1119 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1120 // respectively.
1121 GrGLVertToFrag inRectParams(kVec4f_GrSLType);
1122 args.fPB->addVarying("RectParams", &inRectParams);
1123 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001124
joshualitt9b989322014-12-15 14:16:27 -08001125 // Setup pass through color
1126 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
1127
joshualittabb52a12015-01-13 15:02:10 -08001128 // Setup position
joshualittdd219872015-02-12 14:48:42 -08001129 this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -08001130
joshualittabb52a12015-01-13 15:02:10 -08001131 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001132 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001133 args.fTransformsIn, args.fTransformsOut);
1134
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001135 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -07001136 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001137 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1138 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1139 inDashParams.fsIn());
1140 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001141 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001142 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1143 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001144 fsBuilder->codeAppend("float xSub, ySub;");
1145 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1146 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1147 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1148 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001149 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1150 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001151 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001152 } else if (de.aaMode() == kMSAA_DashAAMode) {
1153 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1154 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1155 fsBuilder->codeAppend("float xSub;");
1156 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1157 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1158 // Now compute coverage in x to get the fraction of the pixel covered.
1159 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001160 } else {
1161 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001162 fsBuilder->codeAppendf("float alpha = 1.0;");
1163 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1164 inRectParams.fsIn());
1165 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1166 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001167 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001168 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001169}
1170
joshualittb0a8a372014-09-23 09:50:21 -07001171void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -08001172 const GrPrimitiveProcessor& processor,
1173 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -08001174 this->setUniformViewMatrix(pdman, processor.viewMatrix());
1175
joshualitt9b989322014-12-15 14:16:27 -08001176 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
1177 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
1178 GrGLfloat c[4];
1179 GrColorToRGBAFloat(local.fColor, c);
1180 pdman.set4fv(fColorUniform, 1, c);
1181 fColor = local.fColor;
1182 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001183}
1184
robertphillips46d36f02015-01-18 08:14:14 -08001185void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -08001186 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -08001187 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001188 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -08001189 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -08001190 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1191 uint32_t key = 0;
1192 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
1193 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -07001194 key |= de.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -08001195 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001196}
1197
1198//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001199
joshualitt2e3b3e32014-12-09 13:31:14 -08001200GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001201 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001202 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -07001203 return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001204}
1205
1206DashingLineEffect::~DashingLineEffect() {}
1207
joshualitt56995b52014-12-11 15:44:02 -08001208void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
1209 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001210}
1211
joshualitteb2a6762014-12-04 11:35:33 -08001212void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
1213 const GrGLCaps& caps,
1214 GrProcessorKeyBuilder* b) const {
1215 GLDashingLineEffect::GenKey(*this, bt, caps, b);
1216}
1217
joshualittabb52a12015-01-13 15:02:10 -08001218GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
1219 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -08001220 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001221}
1222
joshualitt2e3b3e32014-12-09 13:31:14 -08001223DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001224 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001225 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -07001226 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001227 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001228 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001229 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1230 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001231}
1232
bsalomon0e08fc12014-10-15 08:19:04 -07001233bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -07001234 const DashingLineEffect& de = other.cast<DashingLineEffect>();
senorblancof3c2c462015-04-20 14:44:26 -07001235 return fAAMode == de.fAAMode;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001236}
1237
joshualitt4d8da812015-01-28 12:53:54 -08001238void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001239 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1240 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001241 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001242}
1243
joshualitt290c09b2014-12-19 13:45:20 -08001244bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1245 const GrGeometryProcessor& that,
1246 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001247 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1248 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001249 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1250 that, theirs.fUsesLocalCoords) &&
1251 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001252 theirs.fInputColorType, theirs.fColor);
1253}
1254
joshualittb0a8a372014-09-23 09:50:21 -07001255GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001256
joshualittb0a8a372014-09-23 09:50:21 -07001257GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1258 GrContext*,
1259 const GrDrawTargetCaps& caps,
1260 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001261 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001262 return DashingLineEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001263 aaMode, GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001264}
1265
1266//////////////////////////////////////////////////////////////////////////////
1267
joshualitt5224ba72015-02-03 15:07:51 -08001268static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001269 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001270 DashCap cap,
1271 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001272 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001273 case kRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001274 return DashingCircleEffect::Create(color, dashAAMode, localMatrix);
joshualitt5224ba72015-02-03 15:07:51 -08001275 case kNonRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001276 return DashingLineEffect::Create(color, dashAAMode, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001277 default:
1278 SkFAIL("Unexpected dashed cap.");
1279 }
1280 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001281}