blob: a284b272db239bea4d31ac991a2b81aca6615a7e [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"
joshualittfa2008f2015-04-29 11:32:05 -070012#include "GrBatchTest.h"
joshualitt4f569be2015-02-27 11:41:49 -080013#include "GrBufferAllocPool.h"
joshualittb0a8a372014-09-23 09:50:21 -070014#include "GrGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000015#include "GrContext.h"
16#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrDefaultGeoProcFactory.h"
egdaniele61c4112014-06-12 10:24:21 -070018#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000019#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080020#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070021#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070022#include "GrStrokeInfo.h"
bsalomon72e3ae42015-04-28 08:08:46 -070023#include "GrVertexBuffer.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000024#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080025#include "gl/GrGLGeometryProcessor.h"
26#include "gl/GrGLProcessor.h"
27#include "gl/GrGLSL.h"
28#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000029
30///////////////////////////////////////////////////////////////////////////////
31
egdaniele61c4112014-06-12 10:24:21 -070032// Returns whether or not the gpu can fast path the dash line effect.
kkinnunen18996512015-04-26 23:18:49 -070033bool GrDashingEffect::CanDrawDashLine(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
34 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070035 // Pts must be either horizontal or vertical in src space
36 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
37 return false;
38 }
39
40 // May be able to relax this to include skew. As of now cannot do perspective
41 // because of the non uniform scaling of bloating a rect
42 if (!viewMatrix.preservesRightAngles()) {
43 return false;
44 }
45
46 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
47 return false;
48 }
49
50 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
51 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
52 return false;
53 }
54
55 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
56 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070057 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070058 return false;
59 }
60
61 return true;
62}
63
64namespace {
egdaniele61c4112014-06-12 10:24:21 -070065struct DashLineVertex {
66 SkPoint fPos;
67 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080068 SkScalar fIntervalLength;
69 SkRect fRect;
70};
71struct DashCircleVertex {
72 SkPoint fPos;
73 SkPoint fDashPos;
74 SkScalar fIntervalLength;
75 SkScalar fRadius;
76 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070077};
senorblancof3c2c462015-04-20 14:44:26 -070078
79enum DashAAMode {
80 kBW_DashAAMode,
81 kEdgeAA_DashAAMode,
82 kMSAA_DashAAMode,
83
84 kDashAAModeCount,
85};
egdaniele61c4112014-06-12 10:24:21 -070086};
87
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000088static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
89 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
90 SkVector vecSrc = pts[1] - pts[0];
91 SkScalar magSrc = vecSrc.length();
92 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
93 vecSrc.scale(invSrc);
94
95 SkVector vecSrcPerp;
96 vecSrc.rotateCW(&vecSrcPerp);
97 viewMatrix.mapVectors(&vecSrc, 1);
98 viewMatrix.mapVectors(&vecSrcPerp, 1);
99
100 // parallelScale tells how much to scale along the line parallel to the dash line
101 // perpScale tells how much to scale in the direction perpendicular to the dash line
102 *parallelScale = vecSrc.length();
103 *perpScale = vecSrcPerp.length();
104}
105
106// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
107// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
108static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
109 SkVector vec = pts[1] - pts[0];
110 SkScalar mag = vec.length();
111 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
112
113 vec.scale(inv);
114 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
115 if (ptsRot) {
116 rotMatrix->mapPoints(ptsRot, pts, 2);
117 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
118 ptsRot[1].fY = pts[0].fY;
119 }
120}
121
122// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800123static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
124 SkASSERT(phase < intervals[0] + intervals[1]);
125 if (phase >= intervals[0] && phase != 0) {
126 SkScalar srcIntervalLen = intervals[0] + intervals[1];
127 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000128 }
129 return 0;
130}
131
joshualitt4f569be2015-02-27 11:41:49 -0800132static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700133 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000134 if (pts[1].fX <= pts[0].fX) {
135 return 0;
136 }
joshualitt4f569be2015-02-27 11:41:49 -0800137 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000138 SkScalar totalLen = pts[1].fX - pts[0].fX;
139 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
140 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700141 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000142 temp = SkScalarDiv(*endingInt, srcIntervalLen);
143 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
144 if (0 == *endingInt) {
145 *endingInt = srcIntervalLen;
146 }
joshualitt4f569be2015-02-27 11:41:49 -0800147 if (*endingInt > intervals[0]) {
148 if (0 == intervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000149 *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 +0000150 }
joshualitt4f569be2015-02-27 11:41:49 -0800151 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000152 }
153 return 0;
154}
155
joshualitt5224ba72015-02-03 15:07:51 -0800156enum DashCap {
157 kRound_DashCap,
158 kNonRound_DashCap,
159};
160
161static int kDashVertices = 4;
162
163template <typename T>
164void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700165 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
166 SkScalar stroke) {
167 SkScalar startDashX = offset - bloatX;
168 SkScalar endDashX = offset + len + bloatX;
169 SkScalar startDashY = -stroke - bloatY;
170 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800171 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
172 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
173 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
174 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
175
176 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
177 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
178 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
179 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
180
181 matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4);
182}
183
184static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700185 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
186 SkScalar bloatY, SkScalar len, SkScalar stroke,
187 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
188 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800189 SkScalar intervalLength = startInterval + endInterval;
190
191 if (kRound_DashCap == cap) {
192 SkASSERT(vertexStride == sizeof(DashCircleVertex));
193 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
194
senorblancof3c2c462015-04-20 14:44:26 -0700195 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
196 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800197
198 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
199 SkScalar centerX = SkScalarHalf(endInterval);
200
201 for (int i = 0; i < kDashVertices; i++) {
202 verts[idx + i].fIntervalLength = intervalLength;
203 verts[idx + i].fRadius = radius;
204 verts[idx + i].fCenterX = centerX;
205 }
206
207 } else {
208 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
209 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
210
senorblancof3c2c462015-04-20 14:44:26 -0700211 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
212 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800213
214 SkScalar halfOffLen = SkScalarHalf(endInterval);
215 SkScalar halfStroke = SkScalarHalf(strokeWidth);
216 SkRect rectParam;
217 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
218 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
219 for (int i = 0; i < kDashVertices; i++) {
220 verts[idx + i].fIntervalLength = intervalLength;
221 verts[idx + i].fRect = rectParam;
222 }
223 }
egdaniele61c4112014-06-12 10:24:21 -0700224}
225
joshualitt5478d422014-11-14 16:00:38 -0800226static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
227 SkPoint* verts) {
228 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
229 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
230 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
231 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
232 matrix.mapPoints(&verts[idx], 4);
233}
egdaniele61c4112014-06-12 10:24:21 -0700234
joshualitt5224ba72015-02-03 15:07:51 -0800235
236/**
237 * An GrGeometryProcessor that renders a dashed line.
238 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
239 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
240 * position relative to the dashed line.
241 */
242static GrGeometryProcessor* create_dash_gp(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700243 DashAAMode aaMode,
joshualitt5224ba72015-02-03 15:07:51 -0800244 DashCap cap,
245 const SkMatrix& localMatrix);
246
joshualitt4f569be2015-02-27 11:41:49 -0800247class DashBatch : public GrBatch {
248public:
249 struct Geometry {
250 GrColor fColor;
251 SkMatrix fViewMatrix;
252 SkMatrix fSrcRotInv;
253 SkPoint fPtsRot[2];
254 SkScalar fSrcStrokeWidth;
255 SkScalar fPhase;
256 SkScalar fIntervals[2];
257 SkScalar fParallelScale;
258 SkScalar fPerpendicularScale;
259 SkDEBUGCODE(SkRect fDevBounds;)
260 };
261
joshualittfa2008f2015-04-29 11:32:05 -0700262 static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode,
263 bool fullDash) {
senorblancof3c2c462015-04-20 14:44:26 -0700264 return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800265 }
266
mtklein36352bf2015-03-25 18:17:31 -0700267 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800268
mtklein36352bf2015-03-25 18:17:31 -0700269 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800270 // When this is called on a batch, there is only one geometry bundle
271 out->setKnownFourComponents(fGeoData[0].fColor);
272 }
mtklein36352bf2015-03-25 18:17:31 -0700273 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800274 out->setUnknownSingleComponent();
275 }
276
mtklein36352bf2015-03-25 18:17:31 -0700277 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt4f569be2015-02-27 11:41:49 -0800278 // Handle any color overrides
279 if (init.fColorIgnored) {
280 fGeoData[0].fColor = GrColor_ILLEGAL;
281 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
282 fGeoData[0].fColor = init.fOverrideColor;
283 }
284
285 // setup batch properties
286 fBatch.fColorIgnored = init.fColorIgnored;
287 fBatch.fColor = fGeoData[0].fColor;
288 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
289 fBatch.fCoverageIgnored = init.fCoverageIgnored;
290 }
291
292 struct DashDraw {
293 SkScalar fStartOffset;
294 SkScalar fStrokeWidth;
295 SkScalar fLineLength;
296 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700297 SkScalar fDevBloatX;
298 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800299 bool fLineDone;
300 bool fHasStartRect;
301 bool fHasEndRect;
302 };
303
mtklein36352bf2015-03-25 18:17:31 -0700304 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt4f569be2015-02-27 11:41:49 -0800305 int instanceCount = fGeoData.count();
306
307 SkMatrix invert;
308 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
309 SkDebugf("Failed to invert\n");
310 return;
311 }
312
313 SkPaint::Cap cap = this->cap();
314
315 SkAutoTUnref<const GrGeometryProcessor> gp;
316
317 bool isRoundCap = SkPaint::kRound_Cap == cap;
318 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
319 if (this->fullDash()) {
senorblancof3c2c462015-04-20 14:44:26 -0700320 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert));
joshualitt4f569be2015-02-27 11:41:49 -0800321 } else {
322 // Set up the vertex data for the line and start/end dashes
323 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
324 this->color(),
325 SkMatrix::I(),
326 invert));
327 }
328
329 batchTarget->initDraw(gp, pipeline);
330
331 // TODO remove this when batch is everywhere
332 GrPipelineInfo init;
333 init.fColorIgnored = fBatch.fColorIgnored;
334 init.fOverrideColor = GrColor_ILLEGAL;
335 init.fCoverageIgnored = fBatch.fCoverageIgnored;
336 init.fUsesLocalCoords = this->usesLocalCoords();
337 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
338
senorblancof3c2c462015-04-20 14:44:26 -0700339 // useAA here means Edge AA or MSAA
340 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800341 bool fullDash = this->fullDash();
342
343 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
344 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
345 // iterate again over these decomposed dashes to generate vertices
346 SkSTArray<128, SkRect, true> rects;
347 SkSTArray<128, DashDraw, true> draws;
348
349 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800350 int rectOffset = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800351 for (int i = 0; i < instanceCount; i++) {
352 Geometry& args = fGeoData[i];
353
354 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
355
356 // We always want to at least stroke out half a pixel on each side in device space
357 // so 0.5f / perpScale gives us this min in src space
358 SkScalar halfSrcStroke =
359 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
360
361 SkScalar strokeAdj;
362 if (!hasCap) {
363 strokeAdj = 0.f;
364 } else {
365 strokeAdj = halfSrcStroke;
366 }
367
368 SkScalar startAdj = 0;
369
370 SkMatrix& combinedMatrix = args.fSrcRotInv;
371 combinedMatrix.postConcat(args.fViewMatrix);
372
373 bool lineDone = false;
374
375 // Too simplify the algorithm, we always push back rects for start and end rect.
376 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800377 rects.push_back();
378 rects.push_back();
379 rects.push_back();
380 SkRect& bounds = rects[rectOffset++];
381 SkRect& startRect = rects[rectOffset++];
382 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800383
384 bool hasStartRect = false;
385 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
386 // draw it separately here and adjust our start point accordingly
387 if (useAA) {
388 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
389 SkPoint startPts[2];
390 startPts[0] = args.fPtsRot[0];
391 startPts[1].fY = startPts[0].fY;
392 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
393 args.fPtsRot[1].fX);
394 startRect.set(startPts, 2);
395 startRect.outset(strokeAdj, halfSrcStroke);
396
397 hasStartRect = true;
398 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
399 }
400 }
401
402 // adjustments for start and end of bounding rect so we only draw dash intervals
403 // contained in the original line segment.
404 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
405 if (startAdj != 0) {
406 args.fPtsRot[0].fX += startAdj;
407 args.fPhase = 0;
408 }
409 SkScalar endingInterval = 0;
410 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
411 &endingInterval);
412 args.fPtsRot[1].fX -= endAdj;
413 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
414 lineDone = true;
415 }
416
417 bool hasEndRect = false;
418 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
419 // draw it separately here and adjust our end point accordingly
420 if (useAA && !lineDone) {
421 // If we adjusted the end then we will not be drawing a partial dash at the end.
422 // If we didn't adjust the end point then we just need to make sure the ending
423 // dash isn't a full dash
424 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
425 SkPoint endPts[2];
426 endPts[1] = args.fPtsRot[1];
427 endPts[0].fY = endPts[1].fY;
428 endPts[0].fX = endPts[1].fX - endingInterval;
429
430 endRect.set(endPts, 2);
431 endRect.outset(strokeAdj, halfSrcStroke);
432
433 hasEndRect = true;
434 endAdj = endingInterval + args.fIntervals[1];
435
436 args.fPtsRot[1].fX -= endAdj;
437 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
438 lineDone = true;
439 }
440 }
441 }
442
443 if (startAdj != 0) {
444 args.fPhase = 0;
445 }
446
447 // Change the dashing info from src space into device space
448 SkScalar* devIntervals = args.fIntervals;
449 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
450 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
451 SkScalar devPhase = args.fPhase * args.fParallelScale;
452 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
453
senorblancof3c2c462015-04-20 14:44:26 -0700454 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800455 strokeWidth = 1.f;
456 }
457
458 SkScalar halfDevStroke = strokeWidth * 0.5f;
459
460 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
461 // add cap to on interval and remove from off interval
462 devIntervals[0] += strokeWidth;
463 devIntervals[1] -= strokeWidth;
464 }
465 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
466
senorblancof3c2c462015-04-20 14:44:26 -0700467 // For EdgeAA, we bloat in X & Y for both square and round caps.
468 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
469 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
470 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
471 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800472
senorblancof3c2c462015-04-20 14:44:26 -0700473 SkScalar bloatX = devBloatX / args.fParallelScale;
474 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800475
476 if (devIntervals[1] <= 0.f && useAA) {
477 // Case when we end up drawing a solid AA rect
478 // Reset the start rect to draw this single solid rect
479 // but it requires to upload a new intervals uniform so we can mimic
480 // one giant dash
481 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
482 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
483 startRect.set(args.fPtsRot, 2);
484 startRect.outset(strokeAdj, halfSrcStroke);
485 hasStartRect = true;
486 hasEndRect = false;
487 lineDone = true;
488
489 SkPoint devicePts[2];
490 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
491 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
492 if (hasCap) {
493 lineLength += 2.f * halfDevStroke;
494 }
495 devIntervals[0] = lineLength;
496 }
497
498 totalRectCount += !lineDone ? 1 : 0;
499 totalRectCount += hasStartRect ? 1 : 0;
500 totalRectCount += hasEndRect ? 1 : 0;
501
502 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
503 // need to adjust this for round caps to correctly set the dashPos attrib on
504 // vertices
505 startOffset -= halfDevStroke;
506 }
507
508 DashDraw& draw = draws.push_back();
509 if (!lineDone) {
510 SkPoint devicePts[2];
511 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
512 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
513 if (hasCap) {
514 draw.fLineLength += 2.f * halfDevStroke;
515 }
516
517 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
518 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
519 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
520 }
521
522 if (hasStartRect) {
523 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
524 startRect.outset(bloatX, bloatY);
525 }
526
527 if (hasEndRect) {
528 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
529 endRect.outset(bloatX, bloatY);
530 }
531
532 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700533 draw.fDevBloatX = devBloatX;
534 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800535 draw.fHalfDevStroke = halfDevStroke;
536 draw.fStrokeWidth = strokeWidth;
537 draw.fHasStartRect = hasStartRect;
538 draw.fLineDone = lineDone;
539 draw.fHasEndRect = hasEndRect;
540 }
541
542 const GrVertexBuffer* vertexBuffer;
543 int firstVertex;
544
545 size_t vertexStride = gp->getVertexStride();
546 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
547 totalRectCount * kVertsPerDash,
548 &vertexBuffer,
549 &firstVertex);
550
joshualitt4b31de82015-03-05 14:33:41 -0800551 if (!vertices || !batchTarget->quadIndexBuffer()) {
552 SkDebugf("Could not allocate buffers\n");
553 return;
554 }
555
joshualitt4f569be2015-02-27 11:41:49 -0800556 int curVIdx = 0;
557 int rectIndex = 0;
558 for (int i = 0; i < instanceCount; i++) {
559 Geometry& args = fGeoData[i];
560
561 if (!draws[i].fLineDone) {
562 if (fullDash) {
563 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700564 draws[i].fStartOffset, draws[i].fDevBloatX,
565 draws[i].fDevBloatY, draws[i].fLineLength,
566 draws[i].fHalfDevStroke, args.fIntervals[0],
567 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800568 capType, gp->getVertexStride());
569 } else {
570 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
571 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
572 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
573 }
574 curVIdx += 4;
575 }
576 rectIndex++;
577
578 if (draws[i].fHasStartRect) {
579 if (fullDash) {
580 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700581 draws[i].fStartOffset, draws[i].fDevBloatX,
582 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800583 draws[i].fHalfDevStroke, args.fIntervals[0],
584 args.fIntervals[1], draws[i].fStrokeWidth, capType,
585 gp->getVertexStride());
586 } else {
587 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
588 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
589 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
590 }
591
592 curVIdx += 4;
593 }
594 rectIndex++;
595
596 if (draws[i].fHasEndRect) {
597 if (fullDash) {
598 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700599 draws[i].fStartOffset, draws[i].fDevBloatX,
600 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800601 draws[i].fHalfDevStroke, args.fIntervals[0],
602 args.fIntervals[1], draws[i].fStrokeWidth, capType,
603 gp->getVertexStride());
604 } else {
605 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
606 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
607 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
608 }
609 curVIdx += 4;
610 }
611 rectIndex++;
612 }
613
614 const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer();
615
616 GrDrawTarget::DrawInfo drawInfo;
617 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
618 drawInfo.setStartVertex(0);
619 drawInfo.setStartIndex(0);
620 drawInfo.setVerticesPerInstance(kVertsPerDash);
621 drawInfo.setIndicesPerInstance(kIndicesPerDash);
622 drawInfo.adjustStartVertex(firstVertex);
623 drawInfo.setVertexBuffer(vertexBuffer);
624 drawInfo.setIndexBuffer(dashIndexBuffer);
625
626 int maxInstancesPerDraw = dashIndexBuffer->maxQuads();
627 while (totalRectCount) {
628 drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw));
629 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance());
630 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance());
631
632 batchTarget->draw(drawInfo);
633
634 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount());
635 totalRectCount -= drawInfo.instanceCount();
636 }
637 }
638
639 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
640
641private:
senorblancof3c2c462015-04-20 14:44:26 -0700642 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
joshualitt4f569be2015-02-27 11:41:49 -0800643 this->initClassID<DashBatch>();
644 fGeoData.push_back(geometry);
645
senorblancof3c2c462015-04-20 14:44:26 -0700646 fBatch.fAAMode = aaMode;
joshualitt4f569be2015-02-27 11:41:49 -0800647 fBatch.fCap = cap;
648 fBatch.fFullDash = fullDash;
649 }
650
mtklein36352bf2015-03-25 18:17:31 -0700651 bool onCombineIfPossible(GrBatch* t) override {
joshualitt4f569be2015-02-27 11:41:49 -0800652 DashBatch* that = t->cast<DashBatch>();
653
senorblancof3c2c462015-04-20 14:44:26 -0700654 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800655 return false;
656 }
657
658 if (this->fullDash() != that->fullDash()) {
659 return false;
660 }
661
662 if (this->cap() != that->cap()) {
663 return false;
664 }
665
666 // TODO vertex color
667 if (this->color() != that->color()) {
668 return false;
669 }
670
671 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
672 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
673 return false;
674 }
675
676 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
677 return true;
678 }
679
680 GrColor color() const { return fBatch.fColor; }
681 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
682 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700683 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800684 bool fullDash() const { return fBatch.fFullDash; }
685 SkPaint::Cap cap() const { return fBatch.fCap; }
686
687 struct BatchTracker {
688 GrColor fColor;
689 bool fUsesLocalCoords;
690 bool fColorIgnored;
691 bool fCoverageIgnored;
692 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700693 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800694 bool fFullDash;
695 };
696
697 static const int kVertsPerDash = 4;
698 static const int kIndicesPerDash = 6;
699
700 BatchTracker fBatch;
701 SkSTArray<1, Geometry, true> fGeoData;
702};
703
joshualittfa2008f2015-04-29 11:32:05 -0700704static GrBatch* create_batch(GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
705 bool useAA, const GrStrokeInfo& strokeInfo, bool msaaRT) {
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");
joshualittfa2008f2015-04-29 11:32:05 -0700722 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000723 }
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
joshualittfa2008f2015-04-29 11:32:05 -0700741 DashAAMode aaMode = msaaRT ? kMSAA_DashAAMode :
742 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
joshualittfa2008f2015-04-29 11:32:05 -0700753 return DashBatch::Create(geometry, cap, aaMode, fullDash);
754}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000755
joshualittfa2008f2015-04-29 11:32:05 -0700756bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
757 GrPipelineBuilder* pipelineBuilder, GrColor color,
758 const SkMatrix& viewMatrix, const SkPoint pts[2],
759 bool useAA, const GrStrokeInfo& strokeInfo) {
760 SkAutoTUnref<GrBatch> batch(create_batch(color, viewMatrix, pts, useAA, strokeInfo,
761 pipelineBuilder->getRenderTarget()->isMultisampled()));
762 if (!batch) {
763 return false;
764 }
765
766 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000767 return true;
768}
769
770//////////////////////////////////////////////////////////////////////////////
771
egdanielf767e792014-07-02 06:21:32 -0700772class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800773
774struct DashingCircleBatchTracker {
775 GrGPInput fInputColorType;
776 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800777 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800778};
779
egdanielf767e792014-07-02 06:21:32 -0700780/*
781 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
782 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
783 * Both of the previous two parameters are in device space. This effect also requires the setting of
784 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
785 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
786 * transform the line to be horizontal, with the start of line at the origin then shifted to the
787 * right by half the off interval. The line then goes in the positive x direction.
788 */
joshualitt249af152014-09-15 11:41:13 -0700789class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700790public:
791 typedef SkPathEffect::DashInfo DashInfo;
792
joshualitt2e3b3e32014-12-09 13:31:14 -0800793 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700794 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800795 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700796
joshualitt50cb76b2015-04-28 09:17:05 -0700797 virtual ~DashingCircleEffect();
798
mtklein36352bf2015-03-25 18:17:31 -0700799 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700800
joshualitt71c92602015-01-14 08:12:47 -0800801 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800802
joshualitt5224ba72015-02-03 15:07:51 -0800803 const Attribute* inDashParams() const { return fInDashParams; }
804
805 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700806
senorblancof3c2c462015-04-20 14:44:26 -0700807 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700808
joshualitteb2a6762014-12-04 11:35:33 -0800809 virtual void getGLProcessorKey(const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700810 const GrGLSLCaps&,
mtklein36352bf2015-03-25 18:17:31 -0700811 GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700812
joshualittabb52a12015-01-13 15:02:10 -0800813 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700814 const GrGLSLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700815
mtklein36352bf2015-03-25 18:17:31 -0700816 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -0800817
joshualitt50cb76b2015-04-28 09:17:05 -0700818 bool onCanMakeEqual(const GrBatchTracker&,
819 const GrGeometryProcessor&,
820 const GrBatchTracker&) const override;
821
egdanielf767e792014-07-02 06:21:32 -0700822private:
senorblancof3c2c462015-04-20 14:44:26 -0700823 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700824
joshualitt50cb76b2015-04-28 09:17:05 -0700825 bool onIsEqual(const GrGeometryProcessor& other) const override;
826
827 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
828
senorblancof3c2c462015-04-20 14:44:26 -0700829 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800830 const Attribute* fInPosition;
831 const Attribute* fInDashParams;
832 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700833
joshualittb0a8a372014-09-23 09:50:21 -0700834 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700835
joshualitt249af152014-09-15 11:41:13 -0700836 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700837};
838
839//////////////////////////////////////////////////////////////////////////////
840
joshualitt249af152014-09-15 11:41:13 -0700841class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700842public:
joshualitteb2a6762014-12-04 11:35:33 -0800843 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700844
mtklein36352bf2015-03-25 18:17:31 -0700845 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700846
joshualitt87f48d92014-12-04 10:41:40 -0800847 static inline void GenKey(const GrGeometryProcessor&,
848 const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700849 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800850 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700851
joshualitt87f48d92014-12-04 10:41:40 -0800852 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800853 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700854 const GrBatchTracker&) override;
egdanielf767e792014-07-02 06:21:32 -0700855
856private:
joshualitt9b989322014-12-15 14:16:27 -0800857 UniformHandle fParamUniform;
858 UniformHandle fColorUniform;
859 GrColor fColor;
860 SkScalar fPrevRadius;
861 SkScalar fPrevCenterX;
862 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700863 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700864};
865
joshualitteb2a6762014-12-04 11:35:33 -0800866GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
867 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800868 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700869 fPrevRadius = SK_ScalarMin;
870 fPrevCenterX = SK_ScalarMin;
871 fPrevIntervalLength = SK_ScalarMax;
872}
873
robertphillips46d36f02015-01-18 08:14:14 -0800874void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700875 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800876 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
877 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800878 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
879
joshualittabb52a12015-01-13 15:02:10 -0800880 // emit attributes
881 vsBuilder->emitAttributes(dce);
882
joshualitt5224ba72015-02-03 15:07:51 -0800883 // XY are dashPos, Z is dashInterval
884 GrGLVertToFrag dashParams(kVec3f_GrSLType);
885 args.fPB->addVarying("DashParam", &dashParams);
886 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
887
senorblancof3c2c462015-04-20 14:44:26 -0700888 // x refers to circle radius - 0.5, y refers to cicle's center x coord
joshualitt5224ba72015-02-03 15:07:51 -0800889 GrGLVertToFrag circleParams(kVec2f_GrSLType);
890 args.fPB->addVarying("CircleParams", &circleParams);
891 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700892
joshualitt9b989322014-12-15 14:16:27 -0800893 // Setup pass through color
894 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
895
joshualittabb52a12015-01-13 15:02:10 -0800896 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800897 this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800898
joshualittabb52a12015-01-13 15:02:10 -0800899 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800900 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800901 args.fTransformsIn, args.fTransformsOut);
902
egdanielf767e792014-07-02 06:21:32 -0700903 // transforms all points so that we can compare them to our test circle
egdaniel29bee0f2015-04-29 11:54:42 -0700904 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800905 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
906 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
907 dashParams.fsIn());
908 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
909 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
910 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700911 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800912 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
913 fsBuilder->codeAppend("diff = 1.0 - diff;");
914 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700915 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800916 fsBuilder->codeAppendf("float alpha = 1.0;");
917 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700918 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800919 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700920}
921
joshualitt87f48d92014-12-04 10:41:40 -0800922void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800923 const GrPrimitiveProcessor& processor,
924 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800925 this->setUniformViewMatrix(pdman, processor.viewMatrix());
926
joshualitt9b989322014-12-15 14:16:27 -0800927 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
928 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
929 GrGLfloat c[4];
930 GrColorToRGBAFloat(local.fColor, c);
931 pdman.set4fv(fColorUniform, 1, c);
932 fColor = local.fColor;
933 }
egdanielf767e792014-07-02 06:21:32 -0700934}
935
robertphillips46d36f02015-01-18 08:14:14 -0800936void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800937 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700938 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700939 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800940 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800941 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
942 uint32_t key = 0;
943 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
944 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -0700945 key |= dce.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -0800946 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700947}
948
949//////////////////////////////////////////////////////////////////////////////
950
joshualitt2e3b3e32014-12-09 13:31:14 -0800951GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700952 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800953 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -0700954 return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700955}
956
joshualitt50cb76b2015-04-28 09:17:05 -0700957DashingCircleEffect::~DashingCircleEffect() {}
958
959void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
960 out->setUnknownSingleComponent();
961}
962
joshualitteb2a6762014-12-04 11:35:33 -0800963void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700964 const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800965 GrProcessorKeyBuilder* b) const {
966 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
967}
968
joshualittabb52a12015-01-13 15:02:10 -0800969GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700970 const GrGLSLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800971 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700972}
973
joshualitt2e3b3e32014-12-09 13:31:14 -0800974DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700975 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800976 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -0700977 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800978 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800979 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800980 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
981 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
982 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700983}
984
joshualitt50cb76b2015-04-28 09:17:05 -0700985bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
986 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
987 return fAAMode == dce.fAAMode;
988}
989
joshualitt4d8da812015-01-28 12:53:54 -0800990void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800991 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
992 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800993 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800994}
995
joshualitt50cb76b2015-04-28 09:17:05 -0700996bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
997 const GrGeometryProcessor& that,
998 const GrBatchTracker& t) const {
999 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
1000 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
1001 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1002 that, theirs.fUsesLocalCoords) &&
1003 CanCombineOutput(mine.fInputColorType, mine.fColor,
1004 theirs.fInputColorType, theirs.fColor);
1005}
1006
joshualittb0a8a372014-09-23 09:50:21 -07001007GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -07001008
joshualittb0a8a372014-09-23 09:50:21 -07001009GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
1010 GrContext*,
1011 const GrDrawTargetCaps& caps,
1012 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001013 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001014 return DashingCircleEffect::Create(GrRandomColor(random),
joshualitt4eaf9ce2015-04-28 13:31:18 -07001015 aaMode, GrTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -07001016}
1017
1018//////////////////////////////////////////////////////////////////////////////
1019
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001020class GLDashingLineEffect;
1021
joshualitt9b989322014-12-15 14:16:27 -08001022struct DashingLineBatchTracker {
1023 GrGPInput fInputColorType;
1024 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -08001025 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001026};
1027
egdanielf767e792014-07-02 06:21:32 -07001028/*
1029 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1030 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
1031 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
1032 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1033 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1034 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1035 * positive x direction.
1036 */
joshualitt249af152014-09-15 11:41:13 -07001037class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001038public:
1039 typedef SkPathEffect::DashInfo DashInfo;
1040
joshualitt2e3b3e32014-12-09 13:31:14 -08001041 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001042 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001043 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001044
joshualitt50cb76b2015-04-28 09:17:05 -07001045 virtual ~DashingLineEffect();
1046
mtklein36352bf2015-03-25 18:17:31 -07001047 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001048
joshualitt71c92602015-01-14 08:12:47 -08001049 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001050
joshualitt5224ba72015-02-03 15:07:51 -08001051 const Attribute* inDashParams() const { return fInDashParams; }
1052
1053 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001054
senorblancof3c2c462015-04-20 14:44:26 -07001055 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001056
joshualitteb2a6762014-12-04 11:35:33 -08001057 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001058 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -07001059 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001060
joshualittabb52a12015-01-13 15:02:10 -08001061 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001062 const GrGLSLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001063
mtklein36352bf2015-03-25 18:17:31 -07001064 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -08001065
joshualitt50cb76b2015-04-28 09:17:05 -07001066 bool onCanMakeEqual(const GrBatchTracker&,
1067 const GrGeometryProcessor&,
1068 const GrBatchTracker&) const override;
1069
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001070private:
senorblancof3c2c462015-04-20 14:44:26 -07001071 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001072
joshualitt50cb76b2015-04-28 09:17:05 -07001073 bool onIsEqual(const GrGeometryProcessor& other) const override;
1074
1075 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
1076
senorblancof3c2c462015-04-20 14:44:26 -07001077 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001078 const Attribute* fInPosition;
1079 const Attribute* fInDashParams;
1080 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001081
joshualittb0a8a372014-09-23 09:50:21 -07001082 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001083
joshualitt249af152014-09-15 11:41:13 -07001084 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001085};
1086
1087//////////////////////////////////////////////////////////////////////////////
1088
joshualitt249af152014-09-15 11:41:13 -07001089class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001090public:
joshualitteb2a6762014-12-04 11:35:33 -08001091 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001092
mtklein36352bf2015-03-25 18:17:31 -07001093 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001094
joshualitt87f48d92014-12-04 10:41:40 -08001095 static inline void GenKey(const GrGeometryProcessor&,
1096 const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -07001097 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -08001098 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001099
joshualitt87f48d92014-12-04 10:41:40 -08001100 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -08001101 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001102 const GrBatchTracker&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001103
1104private:
joshualitt9b989322014-12-15 14:16:27 -08001105 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001106 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001107 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001108};
1109
joshualitteb2a6762014-12-04 11:35:33 -08001110GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
1111 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -08001112 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001113}
1114
robertphillips46d36f02015-01-18 08:14:14 -08001115void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001116 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -08001117 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
1118 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001119
1120 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
1121
joshualittabb52a12015-01-13 15:02:10 -08001122 // emit attributes
1123 vsBuilder->emitAttributes(de);
1124
joshualitt5224ba72015-02-03 15:07:51 -08001125 // XY refers to dashPos, Z is the dash interval length
1126 GrGLVertToFrag inDashParams(kVec3f_GrSLType);
1127 args.fPB->addVarying("DashParams", &inDashParams);
1128 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1129
1130 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1131 // respectively.
1132 GrGLVertToFrag inRectParams(kVec4f_GrSLType);
1133 args.fPB->addVarying("RectParams", &inRectParams);
1134 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001135
joshualitt9b989322014-12-15 14:16:27 -08001136 // Setup pass through color
1137 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
1138
joshualittabb52a12015-01-13 15:02:10 -08001139 // Setup position
joshualittdd219872015-02-12 14:48:42 -08001140 this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -08001141
joshualittabb52a12015-01-13 15:02:10 -08001142 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001143 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001144 args.fTransformsIn, args.fTransformsOut);
1145
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001146 // transforms all points so that we can compare them to our test rect
egdaniel29bee0f2015-04-29 11:54:42 -07001147 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001148 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1149 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1150 inDashParams.fsIn());
1151 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001152 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001153 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1154 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001155 fsBuilder->codeAppend("float xSub, ySub;");
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 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1159 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001160 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1161 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001162 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001163 } else if (de.aaMode() == kMSAA_DashAAMode) {
1164 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1165 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1166 fsBuilder->codeAppend("float xSub;");
1167 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1168 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1169 // Now compute coverage in x to get the fraction of the pixel covered.
1170 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001171 } else {
1172 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001173 fsBuilder->codeAppendf("float alpha = 1.0;");
1174 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1175 inRectParams.fsIn());
1176 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1177 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001178 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001179 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001180}
1181
joshualittb0a8a372014-09-23 09:50:21 -07001182void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -08001183 const GrPrimitiveProcessor& processor,
1184 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -08001185 this->setUniformViewMatrix(pdman, processor.viewMatrix());
1186
joshualitt9b989322014-12-15 14:16:27 -08001187 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
1188 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
1189 GrGLfloat c[4];
1190 GrColorToRGBAFloat(local.fColor, c);
1191 pdman.set4fv(fColorUniform, 1, c);
1192 fColor = local.fColor;
1193 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001194}
1195
robertphillips46d36f02015-01-18 08:14:14 -08001196void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -08001197 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001198 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001199 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -08001200 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -08001201 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1202 uint32_t key = 0;
1203 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
1204 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -07001205 key |= de.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -08001206 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001207}
1208
1209//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001210
joshualitt2e3b3e32014-12-09 13:31:14 -08001211GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001212 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001213 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -07001214 return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001215}
1216
joshualitt50cb76b2015-04-28 09:17:05 -07001217DashingLineEffect::~DashingLineEffect() {}
1218
1219void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
1220 out->setUnknownSingleComponent();
1221}
1222
joshualitteb2a6762014-12-04 11:35:33 -08001223void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001224 const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -08001225 GrProcessorKeyBuilder* b) const {
1226 GLDashingLineEffect::GenKey(*this, bt, caps, b);
1227}
1228
joshualittabb52a12015-01-13 15:02:10 -08001229GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001230 const GrGLSLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -08001231 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001232}
1233
joshualitt2e3b3e32014-12-09 13:31:14 -08001234DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001235 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001236 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -07001237 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001238 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001239 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001240 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1241 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001242}
1243
joshualitt50cb76b2015-04-28 09:17:05 -07001244bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
1245 const DashingLineEffect& de = other.cast<DashingLineEffect>();
1246 return fAAMode == de.fAAMode;
1247}
1248
joshualitt4d8da812015-01-28 12:53:54 -08001249void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001250 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1251 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001252 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001253}
1254
joshualitt50cb76b2015-04-28 09:17:05 -07001255bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1256 const GrGeometryProcessor& that,
1257 const GrBatchTracker& t) const {
1258 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1259 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
1260 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1261 that, theirs.fUsesLocalCoords) &&
1262 CanCombineOutput(mine.fInputColorType, mine.fColor,
1263 theirs.fInputColorType, theirs.fColor);
1264}
1265
joshualittb0a8a372014-09-23 09:50:21 -07001266GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001267
joshualittb0a8a372014-09-23 09:50:21 -07001268GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1269 GrContext*,
1270 const GrDrawTargetCaps& caps,
1271 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001272 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001273 return DashingLineEffect::Create(GrRandomColor(random),
joshualitt4eaf9ce2015-04-28 13:31:18 -07001274 aaMode, GrTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001275}
1276
1277//////////////////////////////////////////////////////////////////////////////
1278
joshualitt5224ba72015-02-03 15:07:51 -08001279static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001280 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001281 DashCap cap,
1282 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001283 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001284 case kRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001285 return DashingCircleEffect::Create(color, dashAAMode, localMatrix);
joshualitt5224ba72015-02-03 15:07:51 -08001286 case kNonRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001287 return DashingLineEffect::Create(color, dashAAMode, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001288 default:
1289 SkFAIL("Unexpected dashed cap.");
1290 }
1291 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001292}
joshualittfa2008f2015-04-29 11:32:05 -07001293
1294/////////////////////////////////////////////////////////////////////////////////////////////////
1295
1296#ifdef GR_TEST_UTILS
1297
1298BATCH_TEST_DEFINE(DashBatch) {
1299 GrColor color = GrRandomColor(random);
1300 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1301 bool useAA = random->nextBool();
1302 bool msaaRT = random->nextBool();
1303
1304 // We can only dash either horizontal or vertical lines
1305 SkPoint pts[2];
1306 if (random->nextBool()) {
1307 // vertical
1308 pts[0].fX = 1.f;
1309 pts[0].fY = random->nextF() * 10.f;
1310 pts[1].fX = 1.f;
1311 pts[1].fY = random->nextF() * 10.f;
1312 } else {
1313 // horizontal
1314 pts[0].fX = random->nextF() * 10.f;
1315 pts[0].fY = 1.f;
1316 pts[1].fX = random->nextF() * 10.f;
1317 pts[1].fY = 1.f;
1318 }
1319
1320 // pick random cap
1321 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::Cap::kCapCount));
1322
1323 SkScalar intervals[2];
1324
1325 // We can only dash with the following intervals
1326 enum Intervals {
1327 kOpenOpen_Intervals ,
1328 kOpenClose_Intervals,
1329 kCloseOpen_Intervals,
1330 };
1331
1332 Intervals intervalType = SkPaint::kRound_Cap ?
1333 kOpenClose_Intervals :
1334 Intervals(random->nextULessThan(kCloseOpen_Intervals + 1));
1335 static const SkScalar kIntervalMin = 0.1f;
1336 static const SkScalar kIntervalMax = 10.f;
1337 switch (intervalType) {
1338 case kOpenOpen_Intervals:
1339 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1340 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1341 break;
1342 case kOpenClose_Intervals:
1343 intervals[0] = 0.f;
1344 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1345 break;
1346 case kCloseOpen_Intervals:
1347 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1348 intervals[1] = 0.f;
1349 break;
1350
1351 }
1352
1353 // phase is 0 < sum (i0, i1)
1354 SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]);
1355
1356 SkPaint p;
1357 p.setStyle(SkPaint::kStroke_Style);
1358 p.setStrokeWidth(SkIntToScalar(1));
1359 p.setStrokeCap(cap);
1360
1361 GrStrokeInfo strokeInfo(p);
1362
1363 SkPathEffect::DashInfo info;
1364 info.fIntervals = intervals;
1365 info.fCount = 2;
1366 info.fPhase = phase;
1367 SkDEBUGCODE(bool success = ) strokeInfo.setDashInfo(info);
1368 SkASSERT(success);
1369
1370 return create_batch(color, viewMatrix, pts, useAA, strokeInfo, msaaRT);
1371}
1372
1373#endif