blob: 0a9be3c311c6a95814472eff76376842672b60c0 [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"
bsalomon72e3ae42015-04-28 08:08:46 -070022#include "GrVertexBuffer.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000023#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080024#include "gl/GrGLGeometryProcessor.h"
25#include "gl/GrGLProcessor.h"
26#include "gl/GrGLSL.h"
27#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000028
29///////////////////////////////////////////////////////////////////////////////
30
egdaniele61c4112014-06-12 10:24:21 -070031// Returns whether or not the gpu can fast path the dash line effect.
kkinnunen18996512015-04-26 23:18:49 -070032bool GrDashingEffect::CanDrawDashLine(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
33 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070034 // Pts must be either horizontal or vertical in src space
35 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
36 return false;
37 }
38
39 // May be able to relax this to include skew. As of now cannot do perspective
40 // because of the non uniform scaling of bloating a rect
41 if (!viewMatrix.preservesRightAngles()) {
42 return false;
43 }
44
45 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
46 return false;
47 }
48
49 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
50 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
51 return false;
52 }
53
54 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
55 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070056 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070057 return false;
58 }
59
60 return true;
61}
62
63namespace {
egdaniele61c4112014-06-12 10:24:21 -070064struct DashLineVertex {
65 SkPoint fPos;
66 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080067 SkScalar fIntervalLength;
68 SkRect fRect;
69};
70struct DashCircleVertex {
71 SkPoint fPos;
72 SkPoint fDashPos;
73 SkScalar fIntervalLength;
74 SkScalar fRadius;
75 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070076};
senorblancof3c2c462015-04-20 14:44:26 -070077
78enum DashAAMode {
79 kBW_DashAAMode,
80 kEdgeAA_DashAAMode,
81 kMSAA_DashAAMode,
82
83 kDashAAModeCount,
84};
egdaniele61c4112014-06-12 10:24:21 -070085};
86
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000087static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
88 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
89 SkVector vecSrc = pts[1] - pts[0];
90 SkScalar magSrc = vecSrc.length();
91 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
92 vecSrc.scale(invSrc);
93
94 SkVector vecSrcPerp;
95 vecSrc.rotateCW(&vecSrcPerp);
96 viewMatrix.mapVectors(&vecSrc, 1);
97 viewMatrix.mapVectors(&vecSrcPerp, 1);
98
99 // parallelScale tells how much to scale along the line parallel to the dash line
100 // perpScale tells how much to scale in the direction perpendicular to the dash line
101 *parallelScale = vecSrc.length();
102 *perpScale = vecSrcPerp.length();
103}
104
105// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
106// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
107static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
108 SkVector vec = pts[1] - pts[0];
109 SkScalar mag = vec.length();
110 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
111
112 vec.scale(inv);
113 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
114 if (ptsRot) {
115 rotMatrix->mapPoints(ptsRot, pts, 2);
116 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
117 ptsRot[1].fY = pts[0].fY;
118 }
119}
120
121// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800122static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
123 SkASSERT(phase < intervals[0] + intervals[1]);
124 if (phase >= intervals[0] && phase != 0) {
125 SkScalar srcIntervalLen = intervals[0] + intervals[1];
126 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000127 }
128 return 0;
129}
130
joshualitt4f569be2015-02-27 11:41:49 -0800131static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700132 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000133 if (pts[1].fX <= pts[0].fX) {
134 return 0;
135 }
joshualitt4f569be2015-02-27 11:41:49 -0800136 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000137 SkScalar totalLen = pts[1].fX - pts[0].fX;
138 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
139 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700140 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000141 temp = SkScalarDiv(*endingInt, srcIntervalLen);
142 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
143 if (0 == *endingInt) {
144 *endingInt = srcIntervalLen;
145 }
joshualitt4f569be2015-02-27 11:41:49 -0800146 if (*endingInt > intervals[0]) {
147 if (0 == intervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000148 *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 +0000149 }
joshualitt4f569be2015-02-27 11:41:49 -0800150 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000151 }
152 return 0;
153}
154
joshualitt5224ba72015-02-03 15:07:51 -0800155enum DashCap {
156 kRound_DashCap,
157 kNonRound_DashCap,
158};
159
160static int kDashVertices = 4;
161
162template <typename T>
163void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700164 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
165 SkScalar stroke) {
166 SkScalar startDashX = offset - bloatX;
167 SkScalar endDashX = offset + len + bloatX;
168 SkScalar startDashY = -stroke - bloatY;
169 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800170 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
171 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
172 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
173 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
174
175 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
176 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
177 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
178 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
179
180 matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4);
181}
182
183static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700184 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
185 SkScalar bloatY, SkScalar len, SkScalar stroke,
186 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
187 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800188 SkScalar intervalLength = startInterval + endInterval;
189
190 if (kRound_DashCap == cap) {
191 SkASSERT(vertexStride == sizeof(DashCircleVertex));
192 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
193
senorblancof3c2c462015-04-20 14:44:26 -0700194 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
195 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800196
197 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
198 SkScalar centerX = SkScalarHalf(endInterval);
199
200 for (int i = 0; i < kDashVertices; i++) {
201 verts[idx + i].fIntervalLength = intervalLength;
202 verts[idx + i].fRadius = radius;
203 verts[idx + i].fCenterX = centerX;
204 }
205
206 } else {
207 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
208 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
209
senorblancof3c2c462015-04-20 14:44:26 -0700210 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
211 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800212
213 SkScalar halfOffLen = SkScalarHalf(endInterval);
214 SkScalar halfStroke = SkScalarHalf(strokeWidth);
215 SkRect rectParam;
216 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
217 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
218 for (int i = 0; i < kDashVertices; i++) {
219 verts[idx + i].fIntervalLength = intervalLength;
220 verts[idx + i].fRect = rectParam;
221 }
222 }
egdaniele61c4112014-06-12 10:24:21 -0700223}
224
joshualitt5478d422014-11-14 16:00:38 -0800225static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
226 SkPoint* verts) {
227 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
228 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
229 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
230 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
231 matrix.mapPoints(&verts[idx], 4);
232}
egdaniele61c4112014-06-12 10:24:21 -0700233
joshualitt5224ba72015-02-03 15:07:51 -0800234
235/**
236 * An GrGeometryProcessor that renders a dashed line.
237 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
238 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
239 * position relative to the dashed line.
240 */
241static GrGeometryProcessor* create_dash_gp(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700242 DashAAMode aaMode,
joshualitt5224ba72015-02-03 15:07:51 -0800243 DashCap cap,
244 const SkMatrix& localMatrix);
245
joshualitt4f569be2015-02-27 11:41:49 -0800246class DashBatch : public GrBatch {
247public:
248 struct Geometry {
249 GrColor fColor;
250 SkMatrix fViewMatrix;
251 SkMatrix fSrcRotInv;
252 SkPoint fPtsRot[2];
253 SkScalar fSrcStrokeWidth;
254 SkScalar fPhase;
255 SkScalar fIntervals[2];
256 SkScalar fParallelScale;
257 SkScalar fPerpendicularScale;
258 SkDEBUGCODE(SkRect fDevBounds;)
259 };
260
senorblancof3c2c462015-04-20 14:44:26 -0700261 static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
262 return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800263 }
264
mtklein36352bf2015-03-25 18:17:31 -0700265 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800266
mtklein36352bf2015-03-25 18:17:31 -0700267 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800268 // When this is called on a batch, there is only one geometry bundle
269 out->setKnownFourComponents(fGeoData[0].fColor);
270 }
mtklein36352bf2015-03-25 18:17:31 -0700271 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800272 out->setUnknownSingleComponent();
273 }
274
mtklein36352bf2015-03-25 18:17:31 -0700275 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt4f569be2015-02-27 11:41:49 -0800276 // Handle any color overrides
277 if (init.fColorIgnored) {
278 fGeoData[0].fColor = GrColor_ILLEGAL;
279 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
280 fGeoData[0].fColor = init.fOverrideColor;
281 }
282
283 // setup batch properties
284 fBatch.fColorIgnored = init.fColorIgnored;
285 fBatch.fColor = fGeoData[0].fColor;
286 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
287 fBatch.fCoverageIgnored = init.fCoverageIgnored;
288 }
289
290 struct DashDraw {
291 SkScalar fStartOffset;
292 SkScalar fStrokeWidth;
293 SkScalar fLineLength;
294 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700295 SkScalar fDevBloatX;
296 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800297 bool fLineDone;
298 bool fHasStartRect;
299 bool fHasEndRect;
300 };
301
mtklein36352bf2015-03-25 18:17:31 -0700302 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt4f569be2015-02-27 11:41:49 -0800303 int instanceCount = fGeoData.count();
304
305 SkMatrix invert;
306 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
307 SkDebugf("Failed to invert\n");
308 return;
309 }
310
311 SkPaint::Cap cap = this->cap();
312
313 SkAutoTUnref<const GrGeometryProcessor> gp;
314
315 bool isRoundCap = SkPaint::kRound_Cap == cap;
316 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
317 if (this->fullDash()) {
senorblancof3c2c462015-04-20 14:44:26 -0700318 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert));
joshualitt4f569be2015-02-27 11:41:49 -0800319 } else {
320 // Set up the vertex data for the line and start/end dashes
321 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
322 this->color(),
323 SkMatrix::I(),
324 invert));
325 }
326
327 batchTarget->initDraw(gp, pipeline);
328
329 // TODO remove this when batch is everywhere
330 GrPipelineInfo init;
331 init.fColorIgnored = fBatch.fColorIgnored;
332 init.fOverrideColor = GrColor_ILLEGAL;
333 init.fCoverageIgnored = fBatch.fCoverageIgnored;
334 init.fUsesLocalCoords = this->usesLocalCoords();
335 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
336
senorblancof3c2c462015-04-20 14:44:26 -0700337 // useAA here means Edge AA or MSAA
338 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800339 bool fullDash = this->fullDash();
340
341 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
342 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
343 // iterate again over these decomposed dashes to generate vertices
344 SkSTArray<128, SkRect, true> rects;
345 SkSTArray<128, DashDraw, true> draws;
346
347 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800348 int rectOffset = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800349 for (int i = 0; i < instanceCount; i++) {
350 Geometry& args = fGeoData[i];
351
352 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
353
354 // We always want to at least stroke out half a pixel on each side in device space
355 // so 0.5f / perpScale gives us this min in src space
356 SkScalar halfSrcStroke =
357 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
358
359 SkScalar strokeAdj;
360 if (!hasCap) {
361 strokeAdj = 0.f;
362 } else {
363 strokeAdj = halfSrcStroke;
364 }
365
366 SkScalar startAdj = 0;
367
368 SkMatrix& combinedMatrix = args.fSrcRotInv;
369 combinedMatrix.postConcat(args.fViewMatrix);
370
371 bool lineDone = false;
372
373 // Too simplify the algorithm, we always push back rects for start and end rect.
374 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800375 rects.push_back();
376 rects.push_back();
377 rects.push_back();
378 SkRect& bounds = rects[rectOffset++];
379 SkRect& startRect = rects[rectOffset++];
380 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800381
382 bool hasStartRect = false;
383 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
384 // draw it separately here and adjust our start point accordingly
385 if (useAA) {
386 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
387 SkPoint startPts[2];
388 startPts[0] = args.fPtsRot[0];
389 startPts[1].fY = startPts[0].fY;
390 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
391 args.fPtsRot[1].fX);
392 startRect.set(startPts, 2);
393 startRect.outset(strokeAdj, halfSrcStroke);
394
395 hasStartRect = true;
396 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
397 }
398 }
399
400 // adjustments for start and end of bounding rect so we only draw dash intervals
401 // contained in the original line segment.
402 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
403 if (startAdj != 0) {
404 args.fPtsRot[0].fX += startAdj;
405 args.fPhase = 0;
406 }
407 SkScalar endingInterval = 0;
408 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
409 &endingInterval);
410 args.fPtsRot[1].fX -= endAdj;
411 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
412 lineDone = true;
413 }
414
415 bool hasEndRect = false;
416 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
417 // draw it separately here and adjust our end point accordingly
418 if (useAA && !lineDone) {
419 // If we adjusted the end then we will not be drawing a partial dash at the end.
420 // If we didn't adjust the end point then we just need to make sure the ending
421 // dash isn't a full dash
422 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
423 SkPoint endPts[2];
424 endPts[1] = args.fPtsRot[1];
425 endPts[0].fY = endPts[1].fY;
426 endPts[0].fX = endPts[1].fX - endingInterval;
427
428 endRect.set(endPts, 2);
429 endRect.outset(strokeAdj, halfSrcStroke);
430
431 hasEndRect = true;
432 endAdj = endingInterval + args.fIntervals[1];
433
434 args.fPtsRot[1].fX -= endAdj;
435 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
436 lineDone = true;
437 }
438 }
439 }
440
441 if (startAdj != 0) {
442 args.fPhase = 0;
443 }
444
445 // Change the dashing info from src space into device space
446 SkScalar* devIntervals = args.fIntervals;
447 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
448 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
449 SkScalar devPhase = args.fPhase * args.fParallelScale;
450 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
451
senorblancof3c2c462015-04-20 14:44:26 -0700452 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800453 strokeWidth = 1.f;
454 }
455
456 SkScalar halfDevStroke = strokeWidth * 0.5f;
457
458 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
459 // add cap to on interval and remove from off interval
460 devIntervals[0] += strokeWidth;
461 devIntervals[1] -= strokeWidth;
462 }
463 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
464
senorblancof3c2c462015-04-20 14:44:26 -0700465 // For EdgeAA, we bloat in X & Y for both square and round caps.
466 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
467 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
468 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
469 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800470
senorblancof3c2c462015-04-20 14:44:26 -0700471 SkScalar bloatX = devBloatX / args.fParallelScale;
472 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800473
474 if (devIntervals[1] <= 0.f && useAA) {
475 // Case when we end up drawing a solid AA rect
476 // Reset the start rect to draw this single solid rect
477 // but it requires to upload a new intervals uniform so we can mimic
478 // one giant dash
479 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
480 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
481 startRect.set(args.fPtsRot, 2);
482 startRect.outset(strokeAdj, halfSrcStroke);
483 hasStartRect = true;
484 hasEndRect = false;
485 lineDone = true;
486
487 SkPoint devicePts[2];
488 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
489 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
490 if (hasCap) {
491 lineLength += 2.f * halfDevStroke;
492 }
493 devIntervals[0] = lineLength;
494 }
495
496 totalRectCount += !lineDone ? 1 : 0;
497 totalRectCount += hasStartRect ? 1 : 0;
498 totalRectCount += hasEndRect ? 1 : 0;
499
500 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
501 // need to adjust this for round caps to correctly set the dashPos attrib on
502 // vertices
503 startOffset -= halfDevStroke;
504 }
505
506 DashDraw& draw = draws.push_back();
507 if (!lineDone) {
508 SkPoint devicePts[2];
509 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
510 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
511 if (hasCap) {
512 draw.fLineLength += 2.f * halfDevStroke;
513 }
514
515 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
516 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
517 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
518 }
519
520 if (hasStartRect) {
521 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
522 startRect.outset(bloatX, bloatY);
523 }
524
525 if (hasEndRect) {
526 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
527 endRect.outset(bloatX, bloatY);
528 }
529
530 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700531 draw.fDevBloatX = devBloatX;
532 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800533 draw.fHalfDevStroke = halfDevStroke;
534 draw.fStrokeWidth = strokeWidth;
535 draw.fHasStartRect = hasStartRect;
536 draw.fLineDone = lineDone;
537 draw.fHasEndRect = hasEndRect;
538 }
539
540 const GrVertexBuffer* vertexBuffer;
541 int firstVertex;
542
543 size_t vertexStride = gp->getVertexStride();
544 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
545 totalRectCount * kVertsPerDash,
546 &vertexBuffer,
547 &firstVertex);
548
joshualitt4b31de82015-03-05 14:33:41 -0800549 if (!vertices || !batchTarget->quadIndexBuffer()) {
550 SkDebugf("Could not allocate buffers\n");
551 return;
552 }
553
joshualitt4f569be2015-02-27 11:41:49 -0800554 int curVIdx = 0;
555 int rectIndex = 0;
556 for (int i = 0; i < instanceCount; i++) {
557 Geometry& args = fGeoData[i];
558
559 if (!draws[i].fLineDone) {
560 if (fullDash) {
561 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700562 draws[i].fStartOffset, draws[i].fDevBloatX,
563 draws[i].fDevBloatY, draws[i].fLineLength,
564 draws[i].fHalfDevStroke, args.fIntervals[0],
565 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800566 capType, gp->getVertexStride());
567 } else {
568 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
569 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
570 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
571 }
572 curVIdx += 4;
573 }
574 rectIndex++;
575
576 if (draws[i].fHasStartRect) {
577 if (fullDash) {
578 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700579 draws[i].fStartOffset, draws[i].fDevBloatX,
580 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800581 draws[i].fHalfDevStroke, args.fIntervals[0],
582 args.fIntervals[1], draws[i].fStrokeWidth, capType,
583 gp->getVertexStride());
584 } else {
585 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
586 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
587 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
588 }
589
590 curVIdx += 4;
591 }
592 rectIndex++;
593
594 if (draws[i].fHasEndRect) {
595 if (fullDash) {
596 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700597 draws[i].fStartOffset, draws[i].fDevBloatX,
598 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800599 draws[i].fHalfDevStroke, args.fIntervals[0],
600 args.fIntervals[1], draws[i].fStrokeWidth, capType,
601 gp->getVertexStride());
602 } else {
603 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
604 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
605 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
606 }
607 curVIdx += 4;
608 }
609 rectIndex++;
610 }
611
612 const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer();
613
614 GrDrawTarget::DrawInfo drawInfo;
615 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
616 drawInfo.setStartVertex(0);
617 drawInfo.setStartIndex(0);
618 drawInfo.setVerticesPerInstance(kVertsPerDash);
619 drawInfo.setIndicesPerInstance(kIndicesPerDash);
620 drawInfo.adjustStartVertex(firstVertex);
621 drawInfo.setVertexBuffer(vertexBuffer);
622 drawInfo.setIndexBuffer(dashIndexBuffer);
623
624 int maxInstancesPerDraw = dashIndexBuffer->maxQuads();
625 while (totalRectCount) {
626 drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw));
627 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance());
628 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance());
629
630 batchTarget->draw(drawInfo);
631
632 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount());
633 totalRectCount -= drawInfo.instanceCount();
634 }
635 }
636
637 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
638
639private:
senorblancof3c2c462015-04-20 14:44:26 -0700640 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
joshualitt4f569be2015-02-27 11:41:49 -0800641 this->initClassID<DashBatch>();
642 fGeoData.push_back(geometry);
643
senorblancof3c2c462015-04-20 14:44:26 -0700644 fBatch.fAAMode = aaMode;
joshualitt4f569be2015-02-27 11:41:49 -0800645 fBatch.fCap = cap;
646 fBatch.fFullDash = fullDash;
647 }
648
mtklein36352bf2015-03-25 18:17:31 -0700649 bool onCombineIfPossible(GrBatch* t) override {
joshualitt4f569be2015-02-27 11:41:49 -0800650 DashBatch* that = t->cast<DashBatch>();
651
senorblancof3c2c462015-04-20 14:44:26 -0700652 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800653 return false;
654 }
655
656 if (this->fullDash() != that->fullDash()) {
657 return false;
658 }
659
660 if (this->cap() != that->cap()) {
661 return false;
662 }
663
664 // TODO vertex color
665 if (this->color() != that->color()) {
666 return false;
667 }
668
669 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
670 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
671 return false;
672 }
673
674 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
675 return true;
676 }
677
678 GrColor color() const { return fBatch.fColor; }
679 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
680 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700681 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800682 bool fullDash() const { return fBatch.fFullDash; }
683 SkPaint::Cap cap() const { return fBatch.fCap; }
684
685 struct BatchTracker {
686 GrColor fColor;
687 bool fUsesLocalCoords;
688 bool fColorIgnored;
689 bool fCoverageIgnored;
690 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700691 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800692 bool fFullDash;
693 };
694
695 static const int kVertsPerDash = 4;
696 static const int kIndicesPerDash = 6;
697
698 BatchTracker fBatch;
699 SkSTArray<1, Geometry, true> fGeoData;
700};
701
702
egdaniel8dd688b2015-01-22 10:16:09 -0800703bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
704 GrPipelineBuilder* pipelineBuilder, GrColor color,
705 const SkMatrix& viewMatrix, const SkPoint pts[2],
kkinnunen18996512015-04-26 23:18:49 -0700706 bool useAA, const GrStrokeInfo& strokeInfo) {
egdaniele61c4112014-06-12 10:24:21 -0700707 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708
egdaniele61c4112014-06-12 10:24:21 -0700709 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000710
joshualitt4f569be2015-02-27 11:41:49 -0800711 DashBatch::Geometry geometry;
712 geometry.fSrcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000713
714 // the phase should be normalized to be [0, sum of all intervals)
715 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
716
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000717 // 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 +0000718 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700719 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800720 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
721 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700722 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000723 return false;
724 }
725 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800726 geometry.fSrcRotInv.reset();
727 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
728 }
729
730 // Scale corrections of intervals and stroke from view matrix
731 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
732 geometry.fPtsRot);
733
734 SkScalar offInterval = info.fIntervals[1] * geometry.fParallelScale;
735 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
736
737 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
738 // add cap to on interveal and remove from off interval
739 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000740 }
741
senorblancof3c2c462015-04-20 14:44:26 -0700742 DashAAMode aaMode = pipelineBuilder->getRenderTarget()->isMultisampled() ? kMSAA_DashAAMode :
kkinnunen18996512015-04-26 23:18:49 -0700743 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
senorblancof3c2c462015-04-20 14:44:26 -0700744
joshualitt4f569be2015-02-27 11:41:49 -0800745 // 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 -0700746 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000747
joshualitt4f569be2015-02-27 11:41:49 -0800748 geometry.fColor = color;
749 geometry.fViewMatrix = viewMatrix;
750 geometry.fPhase = info.fPhase;
751 geometry.fIntervals[0] = info.fIntervals[0];
752 geometry.fIntervals[1] = info.fIntervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000753
senorblancof3c2c462015-04-20 14:44:26 -0700754 SkAutoTUnref<GrBatch> batch(DashBatch::Create(geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800755 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000756
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000757 return true;
758}
759
760//////////////////////////////////////////////////////////////////////////////
761
egdanielf767e792014-07-02 06:21:32 -0700762class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800763
764struct DashingCircleBatchTracker {
765 GrGPInput fInputColorType;
766 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800767 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800768};
769
egdanielf767e792014-07-02 06:21:32 -0700770/*
771 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
772 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
773 * Both of the previous two parameters are in device space. This effect also requires the setting of
774 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
775 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
776 * transform the line to be horizontal, with the start of line at the origin then shifted to the
777 * right by half the off interval. The line then goes in the positive x direction.
778 */
joshualitt249af152014-09-15 11:41:13 -0700779class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700780public:
781 typedef SkPathEffect::DashInfo DashInfo;
782
joshualitt2e3b3e32014-12-09 13:31:14 -0800783 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700784 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800785 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700786
787 virtual ~DashingCircleEffect();
788
mtklein36352bf2015-03-25 18:17:31 -0700789 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700790
joshualitt71c92602015-01-14 08:12:47 -0800791 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800792
joshualitt5224ba72015-02-03 15:07:51 -0800793 const Attribute* inDashParams() const { return fInDashParams; }
794
795 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700796
senorblancof3c2c462015-04-20 14:44:26 -0700797 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700798
joshualitteb2a6762014-12-04 11:35:33 -0800799 virtual void getGLProcessorKey(const GrBatchTracker&,
800 const GrGLCaps&,
mtklein36352bf2015-03-25 18:17:31 -0700801 GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700802
joshualittabb52a12015-01-13 15:02:10 -0800803 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
mtklein36352bf2015-03-25 18:17:31 -0700804 const GrGLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700805
mtklein36352bf2015-03-25 18:17:31 -0700806 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -0800807
joshualitt290c09b2014-12-19 13:45:20 -0800808 bool onCanMakeEqual(const GrBatchTracker&,
809 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700810 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800811
egdanielf767e792014-07-02 06:21:32 -0700812private:
senorblancof3c2c462015-04-20 14:44:26 -0700813 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700814
mtklein36352bf2015-03-25 18:17:31 -0700815 bool onIsEqual(const GrGeometryProcessor& other) const override;
egdanielf767e792014-07-02 06:21:32 -0700816
mtklein36352bf2015-03-25 18:17:31 -0700817 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700818
senorblancof3c2c462015-04-20 14:44:26 -0700819 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800820 const Attribute* fInPosition;
821 const Attribute* fInDashParams;
822 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700823
joshualittb0a8a372014-09-23 09:50:21 -0700824 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700825
joshualitt249af152014-09-15 11:41:13 -0700826 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700827};
828
829//////////////////////////////////////////////////////////////////////////////
830
joshualitt249af152014-09-15 11:41:13 -0700831class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700832public:
joshualitteb2a6762014-12-04 11:35:33 -0800833 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700834
mtklein36352bf2015-03-25 18:17:31 -0700835 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700836
joshualitt87f48d92014-12-04 10:41:40 -0800837 static inline void GenKey(const GrGeometryProcessor&,
838 const GrBatchTracker&,
839 const GrGLCaps&,
840 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700841
joshualitt87f48d92014-12-04 10:41:40 -0800842 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800843 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700844 const GrBatchTracker&) override;
egdanielf767e792014-07-02 06:21:32 -0700845
846private:
joshualitt9b989322014-12-15 14:16:27 -0800847 UniformHandle fParamUniform;
848 UniformHandle fColorUniform;
849 GrColor fColor;
850 SkScalar fPrevRadius;
851 SkScalar fPrevCenterX;
852 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700853 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700854};
855
joshualitteb2a6762014-12-04 11:35:33 -0800856GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
857 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800858 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700859 fPrevRadius = SK_ScalarMin;
860 fPrevCenterX = SK_ScalarMin;
861 fPrevIntervalLength = SK_ScalarMax;
862}
863
robertphillips46d36f02015-01-18 08:14:14 -0800864void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700865 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800866 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
867 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800868 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
869
joshualittabb52a12015-01-13 15:02:10 -0800870 // emit attributes
871 vsBuilder->emitAttributes(dce);
872
joshualitt5224ba72015-02-03 15:07:51 -0800873 // XY are dashPos, Z is dashInterval
874 GrGLVertToFrag dashParams(kVec3f_GrSLType);
875 args.fPB->addVarying("DashParam", &dashParams);
876 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
877
senorblancof3c2c462015-04-20 14:44:26 -0700878 // x refers to circle radius - 0.5, y refers to cicle's center x coord
joshualitt5224ba72015-02-03 15:07:51 -0800879 GrGLVertToFrag circleParams(kVec2f_GrSLType);
880 args.fPB->addVarying("CircleParams", &circleParams);
881 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700882
joshualitt9b989322014-12-15 14:16:27 -0800883 // Setup pass through color
884 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
885
joshualittabb52a12015-01-13 15:02:10 -0800886 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800887 this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800888
joshualittabb52a12015-01-13 15:02:10 -0800889 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800890 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800891 args.fTransformsIn, args.fTransformsOut);
892
egdanielf767e792014-07-02 06:21:32 -0700893 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700894 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800895 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
896 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
897 dashParams.fsIn());
898 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
899 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
900 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700901 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800902 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
903 fsBuilder->codeAppend("diff = 1.0 - diff;");
904 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700905 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800906 fsBuilder->codeAppendf("float alpha = 1.0;");
907 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700908 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800909 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700910}
911
joshualitt87f48d92014-12-04 10:41:40 -0800912void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800913 const GrPrimitiveProcessor& processor,
914 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800915 this->setUniformViewMatrix(pdman, processor.viewMatrix());
916
joshualitt9b989322014-12-15 14:16:27 -0800917 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
918 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
919 GrGLfloat c[4];
920 GrColorToRGBAFloat(local.fColor, c);
921 pdman.set4fv(fColorUniform, 1, c);
922 fColor = local.fColor;
923 }
egdanielf767e792014-07-02 06:21:32 -0700924}
925
robertphillips46d36f02015-01-18 08:14:14 -0800926void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800927 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800928 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700929 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800930 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800931 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
932 uint32_t key = 0;
933 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
934 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -0700935 key |= dce.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -0800936 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700937}
938
939//////////////////////////////////////////////////////////////////////////////
940
joshualitt2e3b3e32014-12-09 13:31:14 -0800941GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700942 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800943 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -0700944 return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700945}
946
947DashingCircleEffect::~DashingCircleEffect() {}
948
joshualitt56995b52014-12-11 15:44:02 -0800949void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
950 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700951}
952
joshualitteb2a6762014-12-04 11:35:33 -0800953void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
954 const GrGLCaps& caps,
955 GrProcessorKeyBuilder* b) const {
956 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
957}
958
joshualittabb52a12015-01-13 15:02:10 -0800959GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
960 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800961 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700962}
963
joshualitt2e3b3e32014-12-09 13:31:14 -0800964DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700965 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800966 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -0700967 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800968 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800969 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800970 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
971 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
972 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700973}
974
bsalomon0e08fc12014-10-15 08:19:04 -0700975bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700976 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
senorblancof3c2c462015-04-20 14:44:26 -0700977 return fAAMode == dce.fAAMode;
egdanielf767e792014-07-02 06:21:32 -0700978}
979
joshualitt4d8da812015-01-28 12:53:54 -0800980void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800981 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
982 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800983 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800984}
985
joshualitt290c09b2014-12-19 13:45:20 -0800986bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
987 const GrGeometryProcessor& that,
988 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800989 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
990 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800991 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
992 that, theirs.fUsesLocalCoords) &&
993 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800994 theirs.fInputColorType, theirs.fColor);
995}
996
joshualittb0a8a372014-09-23 09:50:21 -0700997GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700998
joshualittb0a8a372014-09-23 09:50:21 -0700999GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
1000 GrContext*,
1001 const GrDrawTargetCaps& caps,
1002 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001003 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001004 return DashingCircleEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001005 aaMode, GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -07001006}
1007
1008//////////////////////////////////////////////////////////////////////////////
1009
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001010class GLDashingLineEffect;
1011
joshualitt9b989322014-12-15 14:16:27 -08001012struct DashingLineBatchTracker {
1013 GrGPInput fInputColorType;
1014 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -08001015 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001016};
1017
egdanielf767e792014-07-02 06:21:32 -07001018/*
1019 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1020 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
1021 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
1022 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1023 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1024 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1025 * positive x direction.
1026 */
joshualitt249af152014-09-15 11:41:13 -07001027class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001028public:
1029 typedef SkPathEffect::DashInfo DashInfo;
1030
joshualitt2e3b3e32014-12-09 13:31:14 -08001031 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001032 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001033 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001034
1035 virtual ~DashingLineEffect();
1036
mtklein36352bf2015-03-25 18:17:31 -07001037 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001038
joshualitt71c92602015-01-14 08:12:47 -08001039 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001040
joshualitt5224ba72015-02-03 15:07:51 -08001041 const Attribute* inDashParams() const { return fInDashParams; }
1042
1043 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001044
senorblancof3c2c462015-04-20 14:44:26 -07001045 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001046
joshualitteb2a6762014-12-04 11:35:33 -08001047 virtual void getGLProcessorKey(const GrBatchTracker& bt,
1048 const GrGLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -07001049 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001050
joshualittabb52a12015-01-13 15:02:10 -08001051 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
mtklein36352bf2015-03-25 18:17:31 -07001052 const GrGLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001053
mtklein36352bf2015-03-25 18:17:31 -07001054 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -08001055
joshualitt290c09b2014-12-19 13:45:20 -08001056 bool onCanMakeEqual(const GrBatchTracker&,
1057 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001058 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -08001059
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001060private:
senorblancof3c2c462015-04-20 14:44:26 -07001061 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001062
mtklein36352bf2015-03-25 18:17:31 -07001063 bool onIsEqual(const GrGeometryProcessor& other) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001064
mtklein36352bf2015-03-25 18:17:31 -07001065 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -07001066
senorblancof3c2c462015-04-20 14:44:26 -07001067 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001068 const Attribute* fInPosition;
1069 const Attribute* fInDashParams;
1070 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001071
joshualittb0a8a372014-09-23 09:50:21 -07001072 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001073
joshualitt249af152014-09-15 11:41:13 -07001074 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001075};
1076
1077//////////////////////////////////////////////////////////////////////////////
1078
joshualitt249af152014-09-15 11:41:13 -07001079class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001080public:
joshualitteb2a6762014-12-04 11:35:33 -08001081 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001082
mtklein36352bf2015-03-25 18:17:31 -07001083 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001084
joshualitt87f48d92014-12-04 10:41:40 -08001085 static inline void GenKey(const GrGeometryProcessor&,
1086 const GrBatchTracker&,
1087 const GrGLCaps&,
1088 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001089
joshualitt87f48d92014-12-04 10:41:40 -08001090 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -08001091 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001092 const GrBatchTracker&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001093
1094private:
joshualitt9b989322014-12-15 14:16:27 -08001095 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001096 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001097 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001098};
1099
joshualitteb2a6762014-12-04 11:35:33 -08001100GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
1101 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -08001102 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001103}
1104
robertphillips46d36f02015-01-18 08:14:14 -08001105void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001106 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -08001107 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
1108 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001109
1110 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
1111
joshualittabb52a12015-01-13 15:02:10 -08001112 // emit attributes
1113 vsBuilder->emitAttributes(de);
1114
joshualitt5224ba72015-02-03 15:07:51 -08001115 // XY refers to dashPos, Z is the dash interval length
1116 GrGLVertToFrag inDashParams(kVec3f_GrSLType);
1117 args.fPB->addVarying("DashParams", &inDashParams);
1118 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1119
1120 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1121 // respectively.
1122 GrGLVertToFrag inRectParams(kVec4f_GrSLType);
1123 args.fPB->addVarying("RectParams", &inRectParams);
1124 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001125
joshualitt9b989322014-12-15 14:16:27 -08001126 // Setup pass through color
1127 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
1128
joshualittabb52a12015-01-13 15:02:10 -08001129 // Setup position
joshualittdd219872015-02-12 14:48:42 -08001130 this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -08001131
joshualittabb52a12015-01-13 15:02:10 -08001132 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001133 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001134 args.fTransformsIn, args.fTransformsOut);
1135
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001136 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -07001137 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001138 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1139 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1140 inDashParams.fsIn());
1141 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001142 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001143 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1144 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001145 fsBuilder->codeAppend("float xSub, ySub;");
1146 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1147 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1148 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1149 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001150 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1151 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001152 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001153 } else if (de.aaMode() == kMSAA_DashAAMode) {
1154 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1155 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1156 fsBuilder->codeAppend("float xSub;");
1157 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1158 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1159 // Now compute coverage in x to get the fraction of the pixel covered.
1160 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001161 } else {
1162 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001163 fsBuilder->codeAppendf("float alpha = 1.0;");
1164 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1165 inRectParams.fsIn());
1166 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1167 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001168 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001169 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001170}
1171
joshualittb0a8a372014-09-23 09:50:21 -07001172void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -08001173 const GrPrimitiveProcessor& processor,
1174 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -08001175 this->setUniformViewMatrix(pdman, processor.viewMatrix());
1176
joshualitt9b989322014-12-15 14:16:27 -08001177 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
1178 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
1179 GrGLfloat c[4];
1180 GrColorToRGBAFloat(local.fColor, c);
1181 pdman.set4fv(fColorUniform, 1, c);
1182 fColor = local.fColor;
1183 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001184}
1185
robertphillips46d36f02015-01-18 08:14:14 -08001186void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -08001187 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -08001188 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001189 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -08001190 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -08001191 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1192 uint32_t key = 0;
1193 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
1194 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -07001195 key |= de.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -08001196 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001197}
1198
1199//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001200
joshualitt2e3b3e32014-12-09 13:31:14 -08001201GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001202 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001203 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -07001204 return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001205}
1206
1207DashingLineEffect::~DashingLineEffect() {}
1208
joshualitt56995b52014-12-11 15:44:02 -08001209void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
1210 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001211}
1212
joshualitteb2a6762014-12-04 11:35:33 -08001213void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
1214 const GrGLCaps& caps,
1215 GrProcessorKeyBuilder* b) const {
1216 GLDashingLineEffect::GenKey(*this, bt, caps, b);
1217}
1218
joshualittabb52a12015-01-13 15:02:10 -08001219GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
1220 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -08001221 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001222}
1223
joshualitt2e3b3e32014-12-09 13:31:14 -08001224DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001225 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001226 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -07001227 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001228 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001229 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001230 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1231 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001232}
1233
bsalomon0e08fc12014-10-15 08:19:04 -07001234bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -07001235 const DashingLineEffect& de = other.cast<DashingLineEffect>();
senorblancof3c2c462015-04-20 14:44:26 -07001236 return fAAMode == de.fAAMode;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001237}
1238
joshualitt4d8da812015-01-28 12:53:54 -08001239void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001240 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1241 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001242 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001243}
1244
joshualitt290c09b2014-12-19 13:45:20 -08001245bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1246 const GrGeometryProcessor& that,
1247 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001248 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1249 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001250 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1251 that, theirs.fUsesLocalCoords) &&
1252 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001253 theirs.fInputColorType, theirs.fColor);
1254}
1255
joshualittb0a8a372014-09-23 09:50:21 -07001256GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001257
joshualittb0a8a372014-09-23 09:50:21 -07001258GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1259 GrContext*,
1260 const GrDrawTargetCaps& caps,
1261 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001262 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001263 return DashingLineEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001264 aaMode, GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001265}
1266
1267//////////////////////////////////////////////////////////////////////////////
1268
joshualitt5224ba72015-02-03 15:07:51 -08001269static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001270 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001271 DashCap cap,
1272 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001273 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001274 case kRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001275 return DashingCircleEffect::Create(color, dashAAMode, localMatrix);
joshualitt5224ba72015-02-03 15:07:51 -08001276 case kNonRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001277 return DashingLineEffect::Create(color, dashAAMode, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001278 default:
1279 SkFAIL("Unexpected dashed cap.");
1280 }
1281 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001282}