blob: 3f0df6737796a42a0ccbba098e23855843908d50 [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;
joshualitt4f569be2015-02-27 11:41:49 -0800259 };
260
joshualittfa2008f2015-04-29 11:32:05 -0700261 static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode,
262 bool fullDash) {
senorblancof3c2c462015-04-20 14:44:26 -0700263 return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800264 }
265
mtklein36352bf2015-03-25 18:17:31 -0700266 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800267
mtklein36352bf2015-03-25 18:17:31 -0700268 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800269 // When this is called on a batch, there is only one geometry bundle
270 out->setKnownFourComponents(fGeoData[0].fColor);
271 }
mtklein36352bf2015-03-25 18:17:31 -0700272 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800273 out->setUnknownSingleComponent();
274 }
275
mtklein36352bf2015-03-25 18:17:31 -0700276 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt4f569be2015-02-27 11:41:49 -0800277 // Handle any color overrides
278 if (init.fColorIgnored) {
279 fGeoData[0].fColor = GrColor_ILLEGAL;
280 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
281 fGeoData[0].fColor = init.fOverrideColor;
282 }
283
284 // setup batch properties
285 fBatch.fColorIgnored = init.fColorIgnored;
286 fBatch.fColor = fGeoData[0].fColor;
287 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
288 fBatch.fCoverageIgnored = init.fCoverageIgnored;
289 }
290
291 struct DashDraw {
292 SkScalar fStartOffset;
293 SkScalar fStrokeWidth;
294 SkScalar fLineLength;
295 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700296 SkScalar fDevBloatX;
297 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800298 bool fLineDone;
299 bool fHasStartRect;
300 bool fHasEndRect;
301 };
302
mtklein36352bf2015-03-25 18:17:31 -0700303 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt4f569be2015-02-27 11:41:49 -0800304 int instanceCount = fGeoData.count();
305
306 SkMatrix invert;
307 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
308 SkDebugf("Failed to invert\n");
309 return;
310 }
311
312 SkPaint::Cap cap = this->cap();
313
314 SkAutoTUnref<const GrGeometryProcessor> gp;
315
316 bool isRoundCap = SkPaint::kRound_Cap == cap;
317 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
318 if (this->fullDash()) {
senorblancof3c2c462015-04-20 14:44:26 -0700319 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert));
joshualitt4f569be2015-02-27 11:41:49 -0800320 } else {
321 // Set up the vertex data for the line and start/end dashes
322 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
323 this->color(),
324 SkMatrix::I(),
325 invert));
326 }
327
328 batchTarget->initDraw(gp, pipeline);
329
330 // TODO remove this when batch is everywhere
331 GrPipelineInfo init;
332 init.fColorIgnored = fBatch.fColorIgnored;
333 init.fOverrideColor = GrColor_ILLEGAL;
334 init.fCoverageIgnored = fBatch.fCoverageIgnored;
335 init.fUsesLocalCoords = this->usesLocalCoords();
336 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
337
senorblancof3c2c462015-04-20 14:44:26 -0700338 // useAA here means Edge AA or MSAA
339 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800340 bool fullDash = this->fullDash();
341
342 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
343 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
344 // iterate again over these decomposed dashes to generate vertices
345 SkSTArray<128, SkRect, true> rects;
346 SkSTArray<128, DashDraw, true> draws;
347
348 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800349 int rectOffset = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800350 for (int i = 0; i < instanceCount; i++) {
351 Geometry& args = fGeoData[i];
352
353 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
354
355 // We always want to at least stroke out half a pixel on each side in device space
356 // so 0.5f / perpScale gives us this min in src space
357 SkScalar halfSrcStroke =
358 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
359
360 SkScalar strokeAdj;
361 if (!hasCap) {
362 strokeAdj = 0.f;
363 } else {
364 strokeAdj = halfSrcStroke;
365 }
366
367 SkScalar startAdj = 0;
368
joshualitt4f569be2015-02-27 11:41:49 -0800369 bool lineDone = false;
370
371 // Too simplify the algorithm, we always push back rects for start and end rect.
372 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800373 rects.push_back();
374 rects.push_back();
375 rects.push_back();
376 SkRect& bounds = rects[rectOffset++];
377 SkRect& startRect = rects[rectOffset++];
378 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800379
380 bool hasStartRect = false;
381 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
382 // draw it separately here and adjust our start point accordingly
383 if (useAA) {
384 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
385 SkPoint startPts[2];
386 startPts[0] = args.fPtsRot[0];
387 startPts[1].fY = startPts[0].fY;
388 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
389 args.fPtsRot[1].fX);
390 startRect.set(startPts, 2);
391 startRect.outset(strokeAdj, halfSrcStroke);
392
393 hasStartRect = true;
394 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
395 }
396 }
397
398 // adjustments for start and end of bounding rect so we only draw dash intervals
399 // contained in the original line segment.
400 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
401 if (startAdj != 0) {
402 args.fPtsRot[0].fX += startAdj;
403 args.fPhase = 0;
404 }
405 SkScalar endingInterval = 0;
406 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
407 &endingInterval);
408 args.fPtsRot[1].fX -= endAdj;
409 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
410 lineDone = true;
411 }
412
413 bool hasEndRect = false;
414 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
415 // draw it separately here and adjust our end point accordingly
416 if (useAA && !lineDone) {
417 // If we adjusted the end then we will not be drawing a partial dash at the end.
418 // If we didn't adjust the end point then we just need to make sure the ending
419 // dash isn't a full dash
420 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
421 SkPoint endPts[2];
422 endPts[1] = args.fPtsRot[1];
423 endPts[0].fY = endPts[1].fY;
424 endPts[0].fX = endPts[1].fX - endingInterval;
425
426 endRect.set(endPts, 2);
427 endRect.outset(strokeAdj, halfSrcStroke);
428
429 hasEndRect = true;
430 endAdj = endingInterval + args.fIntervals[1];
431
432 args.fPtsRot[1].fX -= endAdj;
433 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
434 lineDone = true;
435 }
436 }
437 }
438
439 if (startAdj != 0) {
440 args.fPhase = 0;
441 }
442
443 // Change the dashing info from src space into device space
444 SkScalar* devIntervals = args.fIntervals;
445 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
446 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
447 SkScalar devPhase = args.fPhase * args.fParallelScale;
448 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
449
senorblancof3c2c462015-04-20 14:44:26 -0700450 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800451 strokeWidth = 1.f;
452 }
453
454 SkScalar halfDevStroke = strokeWidth * 0.5f;
455
456 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
457 // add cap to on interval and remove from off interval
458 devIntervals[0] += strokeWidth;
459 devIntervals[1] -= strokeWidth;
460 }
461 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
462
senorblancof3c2c462015-04-20 14:44:26 -0700463 // For EdgeAA, we bloat in X & Y for both square and round caps.
464 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
465 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
466 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
467 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800468
senorblancof3c2c462015-04-20 14:44:26 -0700469 SkScalar bloatX = devBloatX / args.fParallelScale;
470 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800471
472 if (devIntervals[1] <= 0.f && useAA) {
473 // Case when we end up drawing a solid AA rect
474 // Reset the start rect to draw this single solid rect
475 // but it requires to upload a new intervals uniform so we can mimic
476 // one giant dash
477 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
478 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
479 startRect.set(args.fPtsRot, 2);
480 startRect.outset(strokeAdj, halfSrcStroke);
481 hasStartRect = true;
482 hasEndRect = false;
483 lineDone = true;
484
485 SkPoint devicePts[2];
486 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
487 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
488 if (hasCap) {
489 lineLength += 2.f * halfDevStroke;
490 }
491 devIntervals[0] = lineLength;
492 }
493
494 totalRectCount += !lineDone ? 1 : 0;
495 totalRectCount += hasStartRect ? 1 : 0;
496 totalRectCount += hasEndRect ? 1 : 0;
497
498 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
499 // need to adjust this for round caps to correctly set the dashPos attrib on
500 // vertices
501 startOffset -= halfDevStroke;
502 }
503
504 DashDraw& draw = draws.push_back();
505 if (!lineDone) {
506 SkPoint devicePts[2];
507 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
508 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
509 if (hasCap) {
510 draw.fLineLength += 2.f * halfDevStroke;
511 }
512
513 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
514 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
515 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
516 }
517
518 if (hasStartRect) {
519 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
520 startRect.outset(bloatX, bloatY);
521 }
522
523 if (hasEndRect) {
524 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
525 endRect.outset(bloatX, bloatY);
526 }
527
528 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700529 draw.fDevBloatX = devBloatX;
530 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800531 draw.fHalfDevStroke = halfDevStroke;
532 draw.fStrokeWidth = strokeWidth;
533 draw.fHasStartRect = hasStartRect;
534 draw.fLineDone = lineDone;
535 draw.fHasEndRect = hasEndRect;
536 }
537
538 const GrVertexBuffer* vertexBuffer;
539 int firstVertex;
540
541 size_t vertexStride = gp->getVertexStride();
542 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
543 totalRectCount * kVertsPerDash,
544 &vertexBuffer,
545 &firstVertex);
546
joshualitt4b31de82015-03-05 14:33:41 -0800547 if (!vertices || !batchTarget->quadIndexBuffer()) {
548 SkDebugf("Could not allocate buffers\n");
549 return;
550 }
551
joshualitt4f569be2015-02-27 11:41:49 -0800552 int curVIdx = 0;
553 int rectIndex = 0;
554 for (int i = 0; i < instanceCount; i++) {
555 Geometry& args = fGeoData[i];
556
557 if (!draws[i].fLineDone) {
558 if (fullDash) {
559 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700560 draws[i].fStartOffset, draws[i].fDevBloatX,
561 draws[i].fDevBloatY, draws[i].fLineLength,
562 draws[i].fHalfDevStroke, args.fIntervals[0],
563 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800564 capType, gp->getVertexStride());
565 } else {
566 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
567 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
568 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
569 }
570 curVIdx += 4;
571 }
572 rectIndex++;
573
574 if (draws[i].fHasStartRect) {
575 if (fullDash) {
576 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700577 draws[i].fStartOffset, draws[i].fDevBloatX,
578 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800579 draws[i].fHalfDevStroke, args.fIntervals[0],
580 args.fIntervals[1], draws[i].fStrokeWidth, capType,
581 gp->getVertexStride());
582 } else {
583 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
584 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
585 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
586 }
587
588 curVIdx += 4;
589 }
590 rectIndex++;
591
592 if (draws[i].fHasEndRect) {
593 if (fullDash) {
594 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700595 draws[i].fStartOffset, draws[i].fDevBloatX,
596 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800597 draws[i].fHalfDevStroke, args.fIntervals[0],
598 args.fIntervals[1], draws[i].fStrokeWidth, capType,
599 gp->getVertexStride());
600 } else {
601 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
602 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
603 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
604 }
605 curVIdx += 4;
606 }
607 rectIndex++;
608 }
609
610 const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer();
611
612 GrDrawTarget::DrawInfo drawInfo;
613 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
614 drawInfo.setStartVertex(0);
615 drawInfo.setStartIndex(0);
616 drawInfo.setVerticesPerInstance(kVertsPerDash);
617 drawInfo.setIndicesPerInstance(kIndicesPerDash);
618 drawInfo.adjustStartVertex(firstVertex);
619 drawInfo.setVertexBuffer(vertexBuffer);
620 drawInfo.setIndexBuffer(dashIndexBuffer);
621
622 int maxInstancesPerDraw = dashIndexBuffer->maxQuads();
623 while (totalRectCount) {
624 drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw));
625 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance());
626 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance());
627
628 batchTarget->draw(drawInfo);
629
630 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount());
631 totalRectCount -= drawInfo.instanceCount();
632 }
633 }
634
635 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
636
637private:
senorblancof3c2c462015-04-20 14:44:26 -0700638 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
joshualitt4f569be2015-02-27 11:41:49 -0800639 this->initClassID<DashBatch>();
640 fGeoData.push_back(geometry);
641
senorblancof3c2c462015-04-20 14:44:26 -0700642 fBatch.fAAMode = aaMode;
joshualitt4f569be2015-02-27 11:41:49 -0800643 fBatch.fCap = cap;
644 fBatch.fFullDash = fullDash;
joshualitt99c7c072015-05-01 13:43:30 -0700645
646 // compute bounds
647 SkScalar halfStrokeWidth = 0.5f * geometry.fSrcStrokeWidth;
648 SkScalar xBloat = SkPaint::kButt_Cap == cap ? 0 : halfStrokeWidth;
649 fBounds.set(geometry.fPtsRot[0], geometry.fPtsRot[1]);
650 fBounds.outset(xBloat, halfStrokeWidth);
651
652 // Note, we actually create the combined matrix here, and save the work
653 SkMatrix& combinedMatrix = fGeoData[0].fSrcRotInv;
654 combinedMatrix.postConcat(geometry.fViewMatrix);
655 combinedMatrix.mapRect(&fBounds);
joshualitt4f569be2015-02-27 11:41:49 -0800656 }
657
mtklein36352bf2015-03-25 18:17:31 -0700658 bool onCombineIfPossible(GrBatch* t) override {
joshualitt4f569be2015-02-27 11:41:49 -0800659 DashBatch* that = t->cast<DashBatch>();
660
senorblancof3c2c462015-04-20 14:44:26 -0700661 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800662 return false;
663 }
664
665 if (this->fullDash() != that->fullDash()) {
666 return false;
667 }
668
669 if (this->cap() != that->cap()) {
670 return false;
671 }
672
673 // TODO vertex color
674 if (this->color() != that->color()) {
675 return false;
676 }
677
678 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
679 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
680 return false;
681 }
682
683 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700684 this->joinBounds(that->bounds());
joshualitt4f569be2015-02-27 11:41:49 -0800685 return true;
686 }
687
688 GrColor color() const { return fBatch.fColor; }
689 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
690 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700691 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800692 bool fullDash() const { return fBatch.fFullDash; }
693 SkPaint::Cap cap() const { return fBatch.fCap; }
694
695 struct BatchTracker {
696 GrColor fColor;
697 bool fUsesLocalCoords;
698 bool fColorIgnored;
699 bool fCoverageIgnored;
700 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700701 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800702 bool fFullDash;
703 };
704
705 static const int kVertsPerDash = 4;
706 static const int kIndicesPerDash = 6;
707
708 BatchTracker fBatch;
709 SkSTArray<1, Geometry, true> fGeoData;
710};
711
joshualittfa2008f2015-04-29 11:32:05 -0700712static GrBatch* create_batch(GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
713 bool useAA, const GrStrokeInfo& strokeInfo, bool msaaRT) {
egdaniele61c4112014-06-12 10:24:21 -0700714 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000715
egdaniele61c4112014-06-12 10:24:21 -0700716 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000717
joshualitt4f569be2015-02-27 11:41:49 -0800718 DashBatch::Geometry geometry;
719 geometry.fSrcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000720
721 // the phase should be normalized to be [0, sum of all intervals)
722 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
723
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000724 // 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 +0000725 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700726 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800727 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
728 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700729 SkDebugf("Failed to create invertible rotation matrix!\n");
joshualittfa2008f2015-04-29 11:32:05 -0700730 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000731 }
732 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800733 geometry.fSrcRotInv.reset();
734 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
735 }
736
737 // Scale corrections of intervals and stroke from view matrix
738 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
739 geometry.fPtsRot);
740
741 SkScalar offInterval = info.fIntervals[1] * geometry.fParallelScale;
742 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
743
744 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
745 // add cap to on interveal and remove from off interval
746 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000747 }
748
joshualittfa2008f2015-04-29 11:32:05 -0700749 DashAAMode aaMode = msaaRT ? kMSAA_DashAAMode :
750 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
senorblancof3c2c462015-04-20 14:44:26 -0700751
joshualitt4f569be2015-02-27 11:41:49 -0800752 // 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 -0700753 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000754
joshualitt4f569be2015-02-27 11:41:49 -0800755 geometry.fColor = color;
756 geometry.fViewMatrix = viewMatrix;
757 geometry.fPhase = info.fPhase;
758 geometry.fIntervals[0] = info.fIntervals[0];
759 geometry.fIntervals[1] = info.fIntervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000760
joshualittfa2008f2015-04-29 11:32:05 -0700761 return DashBatch::Create(geometry, cap, aaMode, fullDash);
762}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000763
joshualittfa2008f2015-04-29 11:32:05 -0700764bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
765 GrPipelineBuilder* pipelineBuilder, GrColor color,
766 const SkMatrix& viewMatrix, const SkPoint pts[2],
767 bool useAA, const GrStrokeInfo& strokeInfo) {
768 SkAutoTUnref<GrBatch> batch(create_batch(color, viewMatrix, pts, useAA, strokeInfo,
769 pipelineBuilder->getRenderTarget()->isMultisampled()));
770 if (!batch) {
771 return false;
772 }
773
774 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000775 return true;
776}
777
778//////////////////////////////////////////////////////////////////////////////
779
egdanielf767e792014-07-02 06:21:32 -0700780class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800781
782struct DashingCircleBatchTracker {
783 GrGPInput fInputColorType;
784 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800785 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800786};
787
egdanielf767e792014-07-02 06:21:32 -0700788/*
789 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
790 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
791 * Both of the previous two parameters are in device space. This effect also requires the setting of
792 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
793 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
794 * transform the line to be horizontal, with the start of line at the origin then shifted to the
795 * right by half the off interval. The line then goes in the positive x direction.
796 */
joshualitt249af152014-09-15 11:41:13 -0700797class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700798public:
799 typedef SkPathEffect::DashInfo DashInfo;
800
joshualitt2e3b3e32014-12-09 13:31:14 -0800801 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700802 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800803 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700804
joshualitt50cb76b2015-04-28 09:17:05 -0700805 virtual ~DashingCircleEffect();
806
mtklein36352bf2015-03-25 18:17:31 -0700807 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700808
joshualitt71c92602015-01-14 08:12:47 -0800809 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800810
joshualitt5224ba72015-02-03 15:07:51 -0800811 const Attribute* inDashParams() const { return fInDashParams; }
812
813 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700814
senorblancof3c2c462015-04-20 14:44:26 -0700815 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700816
joshualitteb2a6762014-12-04 11:35:33 -0800817 virtual void getGLProcessorKey(const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700818 const GrGLSLCaps&,
mtklein36352bf2015-03-25 18:17:31 -0700819 GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700820
joshualittabb52a12015-01-13 15:02:10 -0800821 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700822 const GrGLSLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700823
mtklein36352bf2015-03-25 18:17:31 -0700824 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -0800825
joshualitt50cb76b2015-04-28 09:17:05 -0700826 bool onCanMakeEqual(const GrBatchTracker&,
827 const GrGeometryProcessor&,
828 const GrBatchTracker&) const override;
829
egdanielf767e792014-07-02 06:21:32 -0700830private:
senorblancof3c2c462015-04-20 14:44:26 -0700831 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700832
joshualitt50cb76b2015-04-28 09:17:05 -0700833 bool onIsEqual(const GrGeometryProcessor& other) const override;
834
835 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
836
senorblancof3c2c462015-04-20 14:44:26 -0700837 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800838 const Attribute* fInPosition;
839 const Attribute* fInDashParams;
840 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700841
joshualittb0a8a372014-09-23 09:50:21 -0700842 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700843
joshualitt249af152014-09-15 11:41:13 -0700844 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700845};
846
847//////////////////////////////////////////////////////////////////////////////
848
joshualitt249af152014-09-15 11:41:13 -0700849class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700850public:
joshualitteb2a6762014-12-04 11:35:33 -0800851 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700852
mtklein36352bf2015-03-25 18:17:31 -0700853 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700854
joshualitt87f48d92014-12-04 10:41:40 -0800855 static inline void GenKey(const GrGeometryProcessor&,
856 const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -0700857 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800858 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700859
joshualitt87f48d92014-12-04 10:41:40 -0800860 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800861 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700862 const GrBatchTracker&) override;
egdanielf767e792014-07-02 06:21:32 -0700863
864private:
joshualitt9b989322014-12-15 14:16:27 -0800865 UniformHandle fParamUniform;
866 UniformHandle fColorUniform;
867 GrColor fColor;
868 SkScalar fPrevRadius;
869 SkScalar fPrevCenterX;
870 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700871 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700872};
873
joshualitteb2a6762014-12-04 11:35:33 -0800874GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
875 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800876 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700877 fPrevRadius = SK_ScalarMin;
878 fPrevCenterX = SK_ScalarMin;
879 fPrevIntervalLength = SK_ScalarMax;
880}
881
robertphillips46d36f02015-01-18 08:14:14 -0800882void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700883 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800884 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
885 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800886 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
887
joshualittabb52a12015-01-13 15:02:10 -0800888 // emit attributes
889 vsBuilder->emitAttributes(dce);
890
joshualitt5224ba72015-02-03 15:07:51 -0800891 // XY are dashPos, Z is dashInterval
892 GrGLVertToFrag dashParams(kVec3f_GrSLType);
893 args.fPB->addVarying("DashParam", &dashParams);
894 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
895
senorblancof3c2c462015-04-20 14:44:26 -0700896 // x refers to circle radius - 0.5, y refers to cicle's center x coord
joshualitt5224ba72015-02-03 15:07:51 -0800897 GrGLVertToFrag circleParams(kVec2f_GrSLType);
898 args.fPB->addVarying("CircleParams", &circleParams);
899 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700900
joshualitt9b989322014-12-15 14:16:27 -0800901 // Setup pass through color
902 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
903
joshualittabb52a12015-01-13 15:02:10 -0800904 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800905 this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800906
joshualittabb52a12015-01-13 15:02:10 -0800907 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800908 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800909 args.fTransformsIn, args.fTransformsOut);
910
egdanielf767e792014-07-02 06:21:32 -0700911 // transforms all points so that we can compare them to our test circle
egdaniel29bee0f2015-04-29 11:54:42 -0700912 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800913 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
914 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
915 dashParams.fsIn());
916 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
917 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
918 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700919 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800920 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
921 fsBuilder->codeAppend("diff = 1.0 - diff;");
922 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700923 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800924 fsBuilder->codeAppendf("float alpha = 1.0;");
925 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700926 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800927 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700928}
929
joshualitt87f48d92014-12-04 10:41:40 -0800930void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800931 const GrPrimitiveProcessor& processor,
932 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800933 this->setUniformViewMatrix(pdman, processor.viewMatrix());
934
joshualitt9b989322014-12-15 14:16:27 -0800935 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
936 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
937 GrGLfloat c[4];
938 GrColorToRGBAFloat(local.fColor, c);
939 pdman.set4fv(fColorUniform, 1, c);
940 fColor = local.fColor;
941 }
egdanielf767e792014-07-02 06:21:32 -0700942}
943
robertphillips46d36f02015-01-18 08:14:14 -0800944void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800945 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700946 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700947 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800948 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800949 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
950 uint32_t key = 0;
951 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
952 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -0700953 key |= dce.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -0800954 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700955}
956
957//////////////////////////////////////////////////////////////////////////////
958
joshualitt2e3b3e32014-12-09 13:31:14 -0800959GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700960 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800961 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -0700962 return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700963}
964
joshualitt50cb76b2015-04-28 09:17:05 -0700965DashingCircleEffect::~DashingCircleEffect() {}
966
967void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
968 out->setUnknownSingleComponent();
969}
970
joshualitteb2a6762014-12-04 11:35:33 -0800971void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700972 const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -0800973 GrProcessorKeyBuilder* b) const {
974 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
975}
976
joshualittabb52a12015-01-13 15:02:10 -0800977GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -0700978 const GrGLSLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800979 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700980}
981
joshualitt2e3b3e32014-12-09 13:31:14 -0800982DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700983 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800984 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -0700985 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800986 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800987 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800988 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
989 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
990 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700991}
992
joshualitt50cb76b2015-04-28 09:17:05 -0700993bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
994 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
995 return fAAMode == dce.fAAMode;
996}
997
joshualitt4d8da812015-01-28 12:53:54 -0800998void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800999 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
1000 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001001 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001002}
1003
joshualitt50cb76b2015-04-28 09:17:05 -07001004bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
1005 const GrGeometryProcessor& that,
1006 const GrBatchTracker& t) const {
1007 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
1008 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
1009 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1010 that, theirs.fUsesLocalCoords) &&
1011 CanCombineOutput(mine.fInputColorType, mine.fColor,
1012 theirs.fInputColorType, theirs.fColor);
1013}
1014
joshualittb0a8a372014-09-23 09:50:21 -07001015GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -07001016
joshualittb0a8a372014-09-23 09:50:21 -07001017GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
1018 GrContext*,
1019 const GrDrawTargetCaps& caps,
1020 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001021 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001022 return DashingCircleEffect::Create(GrRandomColor(random),
joshualitt4eaf9ce2015-04-28 13:31:18 -07001023 aaMode, GrTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -07001024}
1025
1026//////////////////////////////////////////////////////////////////////////////
1027
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001028class GLDashingLineEffect;
1029
joshualitt9b989322014-12-15 14:16:27 -08001030struct DashingLineBatchTracker {
1031 GrGPInput fInputColorType;
1032 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -08001033 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001034};
1035
egdanielf767e792014-07-02 06:21:32 -07001036/*
1037 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1038 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
1039 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
1040 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1041 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1042 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1043 * positive x direction.
1044 */
joshualitt249af152014-09-15 11:41:13 -07001045class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001046public:
1047 typedef SkPathEffect::DashInfo DashInfo;
1048
joshualitt2e3b3e32014-12-09 13:31:14 -08001049 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001050 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001051 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001052
joshualitt50cb76b2015-04-28 09:17:05 -07001053 virtual ~DashingLineEffect();
1054
mtklein36352bf2015-03-25 18:17:31 -07001055 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001056
joshualitt71c92602015-01-14 08:12:47 -08001057 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001058
joshualitt5224ba72015-02-03 15:07:51 -08001059 const Attribute* inDashParams() const { return fInDashParams; }
1060
1061 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001062
senorblancof3c2c462015-04-20 14:44:26 -07001063 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001064
joshualitteb2a6762014-12-04 11:35:33 -08001065 virtual void getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001066 const GrGLSLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -07001067 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001068
joshualittabb52a12015-01-13 15:02:10 -08001069 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001070 const GrGLSLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001071
mtklein36352bf2015-03-25 18:17:31 -07001072 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -08001073
joshualitt50cb76b2015-04-28 09:17:05 -07001074 bool onCanMakeEqual(const GrBatchTracker&,
1075 const GrGeometryProcessor&,
1076 const GrBatchTracker&) const override;
1077
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001078private:
senorblancof3c2c462015-04-20 14:44:26 -07001079 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001080
joshualitt50cb76b2015-04-28 09:17:05 -07001081 bool onIsEqual(const GrGeometryProcessor& other) const override;
1082
1083 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
1084
senorblancof3c2c462015-04-20 14:44:26 -07001085 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001086 const Attribute* fInPosition;
1087 const Attribute* fInDashParams;
1088 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001089
joshualittb0a8a372014-09-23 09:50:21 -07001090 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001091
joshualitt249af152014-09-15 11:41:13 -07001092 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001093};
1094
1095//////////////////////////////////////////////////////////////////////////////
1096
joshualitt249af152014-09-15 11:41:13 -07001097class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001098public:
joshualitteb2a6762014-12-04 11:35:33 -08001099 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001100
mtklein36352bf2015-03-25 18:17:31 -07001101 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001102
joshualitt87f48d92014-12-04 10:41:40 -08001103 static inline void GenKey(const GrGeometryProcessor&,
1104 const GrBatchTracker&,
jvanverthcfc18862015-04-28 08:48:20 -07001105 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -08001106 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001107
joshualitt87f48d92014-12-04 10:41:40 -08001108 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -08001109 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001110 const GrBatchTracker&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001111
1112private:
joshualitt9b989322014-12-15 14:16:27 -08001113 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001114 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001115 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001116};
1117
joshualitteb2a6762014-12-04 11:35:33 -08001118GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
1119 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -08001120 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001121}
1122
robertphillips46d36f02015-01-18 08:14:14 -08001123void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001124 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -08001125 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
1126 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001127
1128 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
1129
joshualittabb52a12015-01-13 15:02:10 -08001130 // emit attributes
1131 vsBuilder->emitAttributes(de);
1132
joshualitt5224ba72015-02-03 15:07:51 -08001133 // XY refers to dashPos, Z is the dash interval length
1134 GrGLVertToFrag inDashParams(kVec3f_GrSLType);
1135 args.fPB->addVarying("DashParams", &inDashParams);
1136 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1137
1138 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1139 // respectively.
1140 GrGLVertToFrag inRectParams(kVec4f_GrSLType);
1141 args.fPB->addVarying("RectParams", &inRectParams);
1142 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001143
joshualitt9b989322014-12-15 14:16:27 -08001144 // Setup pass through color
1145 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
1146
joshualittabb52a12015-01-13 15:02:10 -08001147 // Setup position
joshualittdd219872015-02-12 14:48:42 -08001148 this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -08001149
joshualittabb52a12015-01-13 15:02:10 -08001150 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001151 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001152 args.fTransformsIn, args.fTransformsOut);
1153
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001154 // transforms all points so that we can compare them to our test rect
egdaniel29bee0f2015-04-29 11:54:42 -07001155 GrGLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001156 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1157 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1158 inDashParams.fsIn());
1159 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001160 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001161 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1162 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001163 fsBuilder->codeAppend("float xSub, ySub;");
1164 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1165 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1166 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1167 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001168 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1169 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001170 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001171 } else if (de.aaMode() == kMSAA_DashAAMode) {
1172 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1173 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1174 fsBuilder->codeAppend("float xSub;");
1175 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1176 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1177 // Now compute coverage in x to get the fraction of the pixel covered.
1178 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001179 } else {
1180 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001181 fsBuilder->codeAppendf("float alpha = 1.0;");
1182 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1183 inRectParams.fsIn());
1184 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1185 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001186 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001187 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001188}
1189
joshualittb0a8a372014-09-23 09:50:21 -07001190void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -08001191 const GrPrimitiveProcessor& processor,
1192 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -08001193 this->setUniformViewMatrix(pdman, processor.viewMatrix());
1194
joshualitt9b989322014-12-15 14:16:27 -08001195 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
1196 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
1197 GrGLfloat c[4];
1198 GrColorToRGBAFloat(local.fColor, c);
1199 pdman.set4fv(fColorUniform, 1, c);
1200 fColor = local.fColor;
1201 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001202}
1203
robertphillips46d36f02015-01-18 08:14:14 -08001204void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -08001205 const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001206 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001207 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -08001208 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -08001209 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1210 uint32_t key = 0;
1211 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
1212 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -07001213 key |= de.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -08001214 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001215}
1216
1217//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001218
joshualitt2e3b3e32014-12-09 13:31:14 -08001219GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001220 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001221 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -07001222 return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001223}
1224
joshualitt50cb76b2015-04-28 09:17:05 -07001225DashingLineEffect::~DashingLineEffect() {}
1226
1227void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
1228 out->setUnknownSingleComponent();
1229}
1230
joshualitteb2a6762014-12-04 11:35:33 -08001231void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001232 const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -08001233 GrProcessorKeyBuilder* b) const {
1234 GLDashingLineEffect::GenKey(*this, bt, caps, b);
1235}
1236
joshualittabb52a12015-01-13 15:02:10 -08001237GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
jvanverthcfc18862015-04-28 08:48:20 -07001238 const GrGLSLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -08001239 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001240}
1241
joshualitt2e3b3e32014-12-09 13:31:14 -08001242DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001243 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001244 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -07001245 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001246 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001247 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001248 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1249 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001250}
1251
joshualitt50cb76b2015-04-28 09:17:05 -07001252bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
1253 const DashingLineEffect& de = other.cast<DashingLineEffect>();
1254 return fAAMode == de.fAAMode;
1255}
1256
joshualitt4d8da812015-01-28 12:53:54 -08001257void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001258 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1259 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001260 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001261}
1262
joshualitt50cb76b2015-04-28 09:17:05 -07001263bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1264 const GrGeometryProcessor& that,
1265 const GrBatchTracker& t) const {
1266 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1267 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
1268 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1269 that, theirs.fUsesLocalCoords) &&
1270 CanCombineOutput(mine.fInputColorType, mine.fColor,
1271 theirs.fInputColorType, theirs.fColor);
1272}
1273
joshualittb0a8a372014-09-23 09:50:21 -07001274GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001275
joshualittb0a8a372014-09-23 09:50:21 -07001276GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1277 GrContext*,
1278 const GrDrawTargetCaps& caps,
1279 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001280 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001281 return DashingLineEffect::Create(GrRandomColor(random),
joshualitt4eaf9ce2015-04-28 13:31:18 -07001282 aaMode, GrTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001283}
1284
1285//////////////////////////////////////////////////////////////////////////////
1286
joshualitt5224ba72015-02-03 15:07:51 -08001287static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001288 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001289 DashCap cap,
1290 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001291 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001292 case kRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001293 return DashingCircleEffect::Create(color, dashAAMode, localMatrix);
joshualitt5224ba72015-02-03 15:07:51 -08001294 case kNonRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001295 return DashingLineEffect::Create(color, dashAAMode, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001296 default:
1297 SkFAIL("Unexpected dashed cap.");
1298 }
1299 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001300}
joshualittfa2008f2015-04-29 11:32:05 -07001301
1302/////////////////////////////////////////////////////////////////////////////////////////////////
1303
1304#ifdef GR_TEST_UTILS
1305
1306BATCH_TEST_DEFINE(DashBatch) {
1307 GrColor color = GrRandomColor(random);
1308 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1309 bool useAA = random->nextBool();
1310 bool msaaRT = random->nextBool();
1311
1312 // We can only dash either horizontal or vertical lines
1313 SkPoint pts[2];
1314 if (random->nextBool()) {
1315 // vertical
1316 pts[0].fX = 1.f;
1317 pts[0].fY = random->nextF() * 10.f;
1318 pts[1].fX = 1.f;
1319 pts[1].fY = random->nextF() * 10.f;
1320 } else {
1321 // horizontal
1322 pts[0].fX = random->nextF() * 10.f;
1323 pts[0].fY = 1.f;
1324 pts[1].fX = random->nextF() * 10.f;
1325 pts[1].fY = 1.f;
1326 }
1327
1328 // pick random cap
1329 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::Cap::kCapCount));
1330
1331 SkScalar intervals[2];
1332
1333 // We can only dash with the following intervals
1334 enum Intervals {
1335 kOpenOpen_Intervals ,
1336 kOpenClose_Intervals,
1337 kCloseOpen_Intervals,
1338 };
1339
1340 Intervals intervalType = SkPaint::kRound_Cap ?
1341 kOpenClose_Intervals :
1342 Intervals(random->nextULessThan(kCloseOpen_Intervals + 1));
1343 static const SkScalar kIntervalMin = 0.1f;
1344 static const SkScalar kIntervalMax = 10.f;
1345 switch (intervalType) {
1346 case kOpenOpen_Intervals:
1347 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1348 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1349 break;
1350 case kOpenClose_Intervals:
1351 intervals[0] = 0.f;
1352 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1353 break;
1354 case kCloseOpen_Intervals:
1355 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1356 intervals[1] = 0.f;
1357 break;
1358
1359 }
1360
1361 // phase is 0 < sum (i0, i1)
1362 SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]);
1363
1364 SkPaint p;
1365 p.setStyle(SkPaint::kStroke_Style);
1366 p.setStrokeWidth(SkIntToScalar(1));
1367 p.setStrokeCap(cap);
1368
1369 GrStrokeInfo strokeInfo(p);
1370
1371 SkPathEffect::DashInfo info;
1372 info.fIntervals = intervals;
1373 info.fCount = 2;
1374 info.fPhase = phase;
1375 SkDEBUGCODE(bool success = ) strokeInfo.setDashInfo(info);
1376 SkASSERT(success);
1377
1378 return create_batch(color, viewMatrix, pts, useAA, strokeInfo, msaaRT);
1379}
1380
1381#endif