blob: fa8b3a2dcf941bfc6c29b2fa27e39f6bcb1c49f8 [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"
bsalomonab622c72015-05-04 08:09:30 -070022#include "GrResourceProvider.h"
egdaniele61c4112014-06-12 10:24:21 -070023#include "GrStrokeInfo.h"
bsalomon72e3ae42015-04-28 08:08:46 -070024#include "GrVertexBuffer.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000025#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080026#include "gl/GrGLGeometryProcessor.h"
27#include "gl/GrGLProcessor.h"
28#include "gl/GrGLSL.h"
29#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000030
31///////////////////////////////////////////////////////////////////////////////
32
egdaniele61c4112014-06-12 10:24:21 -070033// Returns whether or not the gpu can fast path the dash line effect.
kkinnunen18996512015-04-26 23:18:49 -070034bool GrDashingEffect::CanDrawDashLine(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
35 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070036 // Pts must be either horizontal or vertical in src space
37 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
38 return false;
39 }
40
41 // May be able to relax this to include skew. As of now cannot do perspective
42 // because of the non uniform scaling of bloating a rect
43 if (!viewMatrix.preservesRightAngles()) {
44 return false;
45 }
46
47 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
48 return false;
49 }
50
51 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
52 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
53 return false;
54 }
55
56 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
57 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070058 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070059 return false;
60 }
61
62 return true;
63}
64
65namespace {
egdaniele61c4112014-06-12 10:24:21 -070066struct DashLineVertex {
67 SkPoint fPos;
68 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080069 SkScalar fIntervalLength;
70 SkRect fRect;
71};
72struct DashCircleVertex {
73 SkPoint fPos;
74 SkPoint fDashPos;
75 SkScalar fIntervalLength;
76 SkScalar fRadius;
77 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070078};
senorblancof3c2c462015-04-20 14:44:26 -070079
80enum DashAAMode {
81 kBW_DashAAMode,
82 kEdgeAA_DashAAMode,
83 kMSAA_DashAAMode,
84
85 kDashAAModeCount,
86};
egdaniele61c4112014-06-12 10:24:21 -070087};
88
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000089static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
90 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
91 SkVector vecSrc = pts[1] - pts[0];
92 SkScalar magSrc = vecSrc.length();
93 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
94 vecSrc.scale(invSrc);
95
96 SkVector vecSrcPerp;
97 vecSrc.rotateCW(&vecSrcPerp);
98 viewMatrix.mapVectors(&vecSrc, 1);
99 viewMatrix.mapVectors(&vecSrcPerp, 1);
100
101 // parallelScale tells how much to scale along the line parallel to the dash line
102 // perpScale tells how much to scale in the direction perpendicular to the dash line
103 *parallelScale = vecSrc.length();
104 *perpScale = vecSrcPerp.length();
105}
106
107// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
108// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
109static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
110 SkVector vec = pts[1] - pts[0];
111 SkScalar mag = vec.length();
112 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
113
114 vec.scale(inv);
115 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
116 if (ptsRot) {
117 rotMatrix->mapPoints(ptsRot, pts, 2);
118 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
119 ptsRot[1].fY = pts[0].fY;
120 }
121}
122
123// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800124static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
125 SkASSERT(phase < intervals[0] + intervals[1]);
126 if (phase >= intervals[0] && phase != 0) {
127 SkScalar srcIntervalLen = intervals[0] + intervals[1];
128 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000129 }
130 return 0;
131}
132
joshualitt4f569be2015-02-27 11:41:49 -0800133static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700134 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000135 if (pts[1].fX <= pts[0].fX) {
136 return 0;
137 }
joshualitt4f569be2015-02-27 11:41:49 -0800138 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000139 SkScalar totalLen = pts[1].fX - pts[0].fX;
140 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
141 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700142 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000143 temp = SkScalarDiv(*endingInt, srcIntervalLen);
144 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
145 if (0 == *endingInt) {
146 *endingInt = srcIntervalLen;
147 }
joshualitt4f569be2015-02-27 11:41:49 -0800148 if (*endingInt > intervals[0]) {
149 if (0 == intervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000150 *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 +0000151 }
joshualitt4f569be2015-02-27 11:41:49 -0800152 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000153 }
154 return 0;
155}
156
joshualitt5224ba72015-02-03 15:07:51 -0800157enum DashCap {
158 kRound_DashCap,
159 kNonRound_DashCap,
160};
161
162static int kDashVertices = 4;
163
164template <typename T>
165void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700166 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
167 SkScalar stroke) {
168 SkScalar startDashX = offset - bloatX;
169 SkScalar endDashX = offset + len + bloatX;
170 SkScalar startDashY = -stroke - bloatY;
171 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800172 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
173 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
174 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
175 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
176
177 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
178 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
179 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
180 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
181
182 matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4);
183}
184
185static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700186 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
187 SkScalar bloatY, SkScalar len, SkScalar stroke,
188 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
189 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800190 SkScalar intervalLength = startInterval + endInterval;
191
192 if (kRound_DashCap == cap) {
193 SkASSERT(vertexStride == sizeof(DashCircleVertex));
194 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
195
senorblancof3c2c462015-04-20 14:44:26 -0700196 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
197 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800198
199 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
200 SkScalar centerX = SkScalarHalf(endInterval);
201
202 for (int i = 0; i < kDashVertices; i++) {
203 verts[idx + i].fIntervalLength = intervalLength;
204 verts[idx + i].fRadius = radius;
205 verts[idx + i].fCenterX = centerX;
206 }
207
208 } else {
209 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
210 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
211
senorblancof3c2c462015-04-20 14:44:26 -0700212 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
213 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800214
215 SkScalar halfOffLen = SkScalarHalf(endInterval);
216 SkScalar halfStroke = SkScalarHalf(strokeWidth);
217 SkRect rectParam;
218 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
219 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
220 for (int i = 0; i < kDashVertices; i++) {
221 verts[idx + i].fIntervalLength = intervalLength;
222 verts[idx + i].fRect = rectParam;
223 }
224 }
egdaniele61c4112014-06-12 10:24:21 -0700225}
226
joshualitt5478d422014-11-14 16:00:38 -0800227static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
228 SkPoint* verts) {
229 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
230 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
231 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
232 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
233 matrix.mapPoints(&verts[idx], 4);
234}
egdaniele61c4112014-06-12 10:24:21 -0700235
joshualitt5224ba72015-02-03 15:07:51 -0800236
237/**
238 * An GrGeometryProcessor that renders a dashed line.
239 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
240 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
241 * position relative to the dashed line.
242 */
243static GrGeometryProcessor* create_dash_gp(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700244 DashAAMode aaMode,
joshualitt5224ba72015-02-03 15:07:51 -0800245 DashCap cap,
246 const SkMatrix& localMatrix);
247
joshualitt4f569be2015-02-27 11:41:49 -0800248class DashBatch : public GrBatch {
249public:
250 struct Geometry {
251 GrColor fColor;
252 SkMatrix fViewMatrix;
253 SkMatrix fSrcRotInv;
254 SkPoint fPtsRot[2];
255 SkScalar fSrcStrokeWidth;
256 SkScalar fPhase;
257 SkScalar fIntervals[2];
258 SkScalar fParallelScale;
259 SkScalar fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800260 };
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
joshualitt4f569be2015-02-27 11:41:49 -0800370 bool lineDone = false;
371
372 // Too simplify the algorithm, we always push back rects for start and end rect.
373 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800374 rects.push_back();
375 rects.push_back();
376 rects.push_back();
377 SkRect& bounds = rects[rectOffset++];
378 SkRect& startRect = rects[rectOffset++];
379 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800380
381 bool hasStartRect = false;
382 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
383 // draw it separately here and adjust our start point accordingly
384 if (useAA) {
385 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
386 SkPoint startPts[2];
387 startPts[0] = args.fPtsRot[0];
388 startPts[1].fY = startPts[0].fY;
389 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
390 args.fPtsRot[1].fX);
391 startRect.set(startPts, 2);
392 startRect.outset(strokeAdj, halfSrcStroke);
393
394 hasStartRect = true;
395 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
396 }
397 }
398
399 // adjustments for start and end of bounding rect so we only draw dash intervals
400 // contained in the original line segment.
401 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
402 if (startAdj != 0) {
403 args.fPtsRot[0].fX += startAdj;
404 args.fPhase = 0;
405 }
406 SkScalar endingInterval = 0;
407 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
408 &endingInterval);
409 args.fPtsRot[1].fX -= endAdj;
410 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
411 lineDone = true;
412 }
413
414 bool hasEndRect = false;
415 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
416 // draw it separately here and adjust our end point accordingly
417 if (useAA && !lineDone) {
418 // If we adjusted the end then we will not be drawing a partial dash at the end.
419 // If we didn't adjust the end point then we just need to make sure the ending
420 // dash isn't a full dash
421 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
422 SkPoint endPts[2];
423 endPts[1] = args.fPtsRot[1];
424 endPts[0].fY = endPts[1].fY;
425 endPts[0].fX = endPts[1].fX - endingInterval;
426
427 endRect.set(endPts, 2);
428 endRect.outset(strokeAdj, halfSrcStroke);
429
430 hasEndRect = true;
431 endAdj = endingInterval + args.fIntervals[1];
432
433 args.fPtsRot[1].fX -= endAdj;
434 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
435 lineDone = true;
436 }
437 }
438 }
439
440 if (startAdj != 0) {
441 args.fPhase = 0;
442 }
443
444 // Change the dashing info from src space into device space
445 SkScalar* devIntervals = args.fIntervals;
446 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
447 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
448 SkScalar devPhase = args.fPhase * args.fParallelScale;
449 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
450
senorblancof3c2c462015-04-20 14:44:26 -0700451 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800452 strokeWidth = 1.f;
453 }
454
455 SkScalar halfDevStroke = strokeWidth * 0.5f;
456
457 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
458 // add cap to on interval and remove from off interval
459 devIntervals[0] += strokeWidth;
460 devIntervals[1] -= strokeWidth;
461 }
462 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
463
senorblancof3c2c462015-04-20 14:44:26 -0700464 // For EdgeAA, we bloat in X & Y for both square and round caps.
465 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
466 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
467 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
468 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800469
senorblancof3c2c462015-04-20 14:44:26 -0700470 SkScalar bloatX = devBloatX / args.fParallelScale;
471 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800472
473 if (devIntervals[1] <= 0.f && useAA) {
474 // Case when we end up drawing a solid AA rect
475 // Reset the start rect to draw this single solid rect
476 // but it requires to upload a new intervals uniform so we can mimic
477 // one giant dash
478 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
479 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
480 startRect.set(args.fPtsRot, 2);
481 startRect.outset(strokeAdj, halfSrcStroke);
482 hasStartRect = true;
483 hasEndRect = false;
484 lineDone = true;
485
486 SkPoint devicePts[2];
487 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
488 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
489 if (hasCap) {
490 lineLength += 2.f * halfDevStroke;
491 }
492 devIntervals[0] = lineLength;
493 }
494
495 totalRectCount += !lineDone ? 1 : 0;
496 totalRectCount += hasStartRect ? 1 : 0;
497 totalRectCount += hasEndRect ? 1 : 0;
498
499 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
500 // need to adjust this for round caps to correctly set the dashPos attrib on
501 // vertices
502 startOffset -= halfDevStroke;
503 }
504
505 DashDraw& draw = draws.push_back();
506 if (!lineDone) {
507 SkPoint devicePts[2];
508 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
509 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
510 if (hasCap) {
511 draw.fLineLength += 2.f * halfDevStroke;
512 }
513
514 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
515 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
516 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
517 }
518
519 if (hasStartRect) {
520 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
521 startRect.outset(bloatX, bloatY);
522 }
523
524 if (hasEndRect) {
525 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
526 endRect.outset(bloatX, bloatY);
527 }
528
529 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700530 draw.fDevBloatX = devBloatX;
531 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800532 draw.fHalfDevStroke = halfDevStroke;
533 draw.fStrokeWidth = strokeWidth;
534 draw.fHasStartRect = hasStartRect;
535 draw.fLineDone = lineDone;
536 draw.fHasEndRect = hasEndRect;
537 }
538
bsalomonab622c72015-05-04 08:09:30 -0700539 SkAutoTUnref<const GrIndexBuffer> indexBuffer(
540 batchTarget->resourceProvider()->refQuadIndexBuffer());
541
joshualitt4f569be2015-02-27 11:41:49 -0800542 const GrVertexBuffer* vertexBuffer;
543 int firstVertex;
joshualitt4f569be2015-02-27 11:41:49 -0800544 size_t vertexStride = gp->getVertexStride();
545 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
546 totalRectCount * kVertsPerDash,
547 &vertexBuffer,
548 &firstVertex);
bsalomonab622c72015-05-04 08:09:30 -0700549 if (!vertices || !indexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -0800550 SkDebugf("Could not allocate buffers\n");
551 return;
552 }
553
joshualitt4f569be2015-02-27 11:41:49 -0800554 int curVIdx = 0;
555 int rectIndex = 0;
556 for (int i = 0; i < instanceCount; i++) {
557 Geometry& args = fGeoData[i];
558
559 if (!draws[i].fLineDone) {
560 if (fullDash) {
561 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700562 draws[i].fStartOffset, draws[i].fDevBloatX,
563 draws[i].fDevBloatY, draws[i].fLineLength,
564 draws[i].fHalfDevStroke, args.fIntervals[0],
565 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800566 capType, gp->getVertexStride());
567 } else {
568 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
569 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
570 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
571 }
572 curVIdx += 4;
573 }
574 rectIndex++;
575
576 if (draws[i].fHasStartRect) {
577 if (fullDash) {
578 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700579 draws[i].fStartOffset, draws[i].fDevBloatX,
580 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800581 draws[i].fHalfDevStroke, args.fIntervals[0],
582 args.fIntervals[1], draws[i].fStrokeWidth, capType,
583 gp->getVertexStride());
584 } else {
585 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
586 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
587 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
588 }
589
590 curVIdx += 4;
591 }
592 rectIndex++;
593
594 if (draws[i].fHasEndRect) {
595 if (fullDash) {
596 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700597 draws[i].fStartOffset, draws[i].fDevBloatX,
598 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800599 draws[i].fHalfDevStroke, args.fIntervals[0],
600 args.fIntervals[1], draws[i].fStrokeWidth, capType,
601 gp->getVertexStride());
602 } else {
603 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
604 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
605 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
606 }
607 curVIdx += 4;
608 }
609 rectIndex++;
610 }
611
joshualitt4f569be2015-02-27 11:41:49 -0800612 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);
bsalomonab622c72015-05-04 08:09:30 -0700620 drawInfo.setIndexBuffer(indexBuffer);
joshualitt4f569be2015-02-27 11:41:49 -0800621
bsalomonab622c72015-05-04 08:09:30 -0700622 int maxInstancesPerDraw = indexBuffer->maxQuads();
joshualitt4f569be2015-02-27 11:41:49 -0800623 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
bsalomonab622c72015-05-04 08:09:30 -0700764bool GrDashingEffect::DrawDashLine(GrDrawTarget* target,
joshualittfa2008f2015-04-29 11:32:05 -0700765 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