blob: cf729c177e02344209854fd8a5ac1b7264270b3a [file] [log] [blame]
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrDashingEffect.h"
9
joshualitt4f569be2015-02-27 11:41:49 -080010#include "GrBatch.h"
11#include "GrBatchTarget.h"
12#include "GrBufferAllocPool.h"
joshualittb0a8a372014-09-23 09:50:21 -070013#include "GrGeometryProcessor.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000014#include "GrContext.h"
15#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080016#include "GrDefaultGeoProcFactory.h"
egdaniele61c4112014-06-12 10:24:21 -070017#include "GrDrawTarget.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000018#include "GrDrawTargetCaps.h"
egdaniel605dd0f2014-11-12 08:35:25 -080019#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070020#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070021#include "GrStrokeInfo.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000022#include "SkGr.h"
joshualitt5478d422014-11-14 16:00:38 -080023#include "gl/GrGLGeometryProcessor.h"
24#include "gl/GrGLProcessor.h"
25#include "gl/GrGLSL.h"
26#include "gl/builders/GrGLProgramBuilder.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000027
28///////////////////////////////////////////////////////////////////////////////
29
egdaniele61c4112014-06-12 10:24:21 -070030// Returns whether or not the gpu can fast path the dash line effect.
31static bool can_fast_path_dash(const SkPoint pts[2], const GrStrokeInfo& strokeInfo,
egdaniel8dd688b2015-01-22 10:16:09 -080032 const GrDrawTarget& target, const GrPipelineBuilder& pipelineBuilder,
joshualitt9853cce2014-11-17 14:22:48 -080033 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070034 // Pts must be either horizontal or vertical in src space
35 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
36 return false;
37 }
38
39 // May be able to relax this to include skew. As of now cannot do perspective
40 // because of the non uniform scaling of bloating a rect
41 if (!viewMatrix.preservesRightAngles()) {
42 return false;
43 }
44
45 if (!strokeInfo.isDashed() || 2 != strokeInfo.dashCount()) {
46 return false;
47 }
48
49 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
50 if (0 == info.fIntervals[0] && 0 == info.fIntervals[1]) {
51 return false;
52 }
53
54 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
55 // Current we do don't handle Round or Square cap dashes
egdanielf767e792014-07-02 06:21:32 -070056 if (SkPaint::kRound_Cap == cap && info.fIntervals[0] != 0.f) {
egdaniele61c4112014-06-12 10:24:21 -070057 return false;
58 }
59
60 return true;
61}
62
63namespace {
egdaniele61c4112014-06-12 10:24:21 -070064struct DashLineVertex {
65 SkPoint fPos;
66 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080067 SkScalar fIntervalLength;
68 SkRect fRect;
69};
70struct DashCircleVertex {
71 SkPoint fPos;
72 SkPoint fDashPos;
73 SkScalar fIntervalLength;
74 SkScalar fRadius;
75 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070076};
senorblancof3c2c462015-04-20 14:44:26 -070077
78enum DashAAMode {
79 kBW_DashAAMode,
80 kEdgeAA_DashAAMode,
81 kMSAA_DashAAMode,
82
83 kDashAAModeCount,
84};
egdaniele61c4112014-06-12 10:24:21 -070085};
86
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000087static void calc_dash_scaling(SkScalar* parallelScale, SkScalar* perpScale,
88 const SkMatrix& viewMatrix, const SkPoint pts[2]) {
89 SkVector vecSrc = pts[1] - pts[0];
90 SkScalar magSrc = vecSrc.length();
91 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
92 vecSrc.scale(invSrc);
93
94 SkVector vecSrcPerp;
95 vecSrc.rotateCW(&vecSrcPerp);
96 viewMatrix.mapVectors(&vecSrc, 1);
97 viewMatrix.mapVectors(&vecSrcPerp, 1);
98
99 // parallelScale tells how much to scale along the line parallel to the dash line
100 // perpScale tells how much to scale in the direction perpendicular to the dash line
101 *parallelScale = vecSrc.length();
102 *perpScale = vecSrcPerp.length();
103}
104
105// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
106// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
107static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = NULL) {
108 SkVector vec = pts[1] - pts[0];
109 SkScalar mag = vec.length();
110 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
111
112 vec.scale(inv);
113 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
114 if (ptsRot) {
115 rotMatrix->mapPoints(ptsRot, pts, 2);
116 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
117 ptsRot[1].fY = pts[0].fY;
118 }
119}
120
121// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800122static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
123 SkASSERT(phase < intervals[0] + intervals[1]);
124 if (phase >= intervals[0] && phase != 0) {
125 SkScalar srcIntervalLen = intervals[0] + intervals[1];
126 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000127 }
128 return 0;
129}
130
joshualitt4f569be2015-02-27 11:41:49 -0800131static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700132 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000133 if (pts[1].fX <= pts[0].fX) {
134 return 0;
135 }
joshualitt4f569be2015-02-27 11:41:49 -0800136 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000137 SkScalar totalLen = pts[1].fX - pts[0].fX;
138 SkScalar temp = SkScalarDiv(totalLen, srcIntervalLen);
139 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700140 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000141 temp = SkScalarDiv(*endingInt, srcIntervalLen);
142 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
143 if (0 == *endingInt) {
144 *endingInt = srcIntervalLen;
145 }
joshualitt4f569be2015-02-27 11:41:49 -0800146 if (*endingInt > intervals[0]) {
147 if (0 == intervals[0]) {
commit-bot@chromium.orgad883402014-05-19 14:43:45 +0000148 *endingInt -= 0.01f; // make sure we capture the last zero size pnt (used if has caps)
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000149 }
joshualitt4f569be2015-02-27 11:41:49 -0800150 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000151 }
152 return 0;
153}
154
joshualitt5224ba72015-02-03 15:07:51 -0800155enum DashCap {
156 kRound_DashCap,
157 kNonRound_DashCap,
158};
159
160static int kDashVertices = 4;
161
162template <typename T>
163void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700164 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
165 SkScalar stroke) {
166 SkScalar startDashX = offset - bloatX;
167 SkScalar endDashX = offset + len + bloatX;
168 SkScalar startDashY = -stroke - bloatY;
169 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800170 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
171 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
172 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, endDashY);
173 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, startDashY);
174
175 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
176 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
177 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
178 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fTop);
179
180 matrix.mapPointsWithStride(&vertices[idx].fPos, sizeof(T), 4);
181}
182
183static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700184 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
185 SkScalar bloatY, SkScalar len, SkScalar stroke,
186 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
187 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800188 SkScalar intervalLength = startInterval + endInterval;
189
190 if (kRound_DashCap == cap) {
191 SkASSERT(vertexStride == sizeof(DashCircleVertex));
192 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
193
senorblancof3c2c462015-04-20 14:44:26 -0700194 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
195 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800196
197 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
198 SkScalar centerX = SkScalarHalf(endInterval);
199
200 for (int i = 0; i < kDashVertices; i++) {
201 verts[idx + i].fIntervalLength = intervalLength;
202 verts[idx + i].fRadius = radius;
203 verts[idx + i].fCenterX = centerX;
204 }
205
206 } else {
207 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
208 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
209
senorblancof3c2c462015-04-20 14:44:26 -0700210 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
211 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800212
213 SkScalar halfOffLen = SkScalarHalf(endInterval);
214 SkScalar halfStroke = SkScalarHalf(strokeWidth);
215 SkRect rectParam;
216 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
217 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
218 for (int i = 0; i < kDashVertices; i++) {
219 verts[idx + i].fIntervalLength = intervalLength;
220 verts[idx + i].fRect = rectParam;
221 }
222 }
egdaniele61c4112014-06-12 10:24:21 -0700223}
224
joshualitt5478d422014-11-14 16:00:38 -0800225static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
226 SkPoint* verts) {
227 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
228 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
229 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fBottom);
230 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fTop);
231 matrix.mapPoints(&verts[idx], 4);
232}
egdaniele61c4112014-06-12 10:24:21 -0700233
joshualitt5224ba72015-02-03 15:07:51 -0800234
235/**
236 * An GrGeometryProcessor that renders a dashed line.
237 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
238 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
239 * position relative to the dashed line.
240 */
241static GrGeometryProcessor* create_dash_gp(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700242 DashAAMode aaMode,
joshualitt5224ba72015-02-03 15:07:51 -0800243 DashCap cap,
244 const SkMatrix& localMatrix);
245
joshualitt4f569be2015-02-27 11:41:49 -0800246class DashBatch : public GrBatch {
247public:
248 struct Geometry {
249 GrColor fColor;
250 SkMatrix fViewMatrix;
251 SkMatrix fSrcRotInv;
252 SkPoint fPtsRot[2];
253 SkScalar fSrcStrokeWidth;
254 SkScalar fPhase;
255 SkScalar fIntervals[2];
256 SkScalar fParallelScale;
257 SkScalar fPerpendicularScale;
258 SkDEBUGCODE(SkRect fDevBounds;)
259 };
260
senorblancof3c2c462015-04-20 14:44:26 -0700261 static GrBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
262 return SkNEW_ARGS(DashBatch, (geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800263 }
264
mtklein36352bf2015-03-25 18:17:31 -0700265 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800266
mtklein36352bf2015-03-25 18:17:31 -0700267 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800268 // When this is called on a batch, there is only one geometry bundle
269 out->setKnownFourComponents(fGeoData[0].fColor);
270 }
mtklein36352bf2015-03-25 18:17:31 -0700271 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800272 out->setUnknownSingleComponent();
273 }
274
mtklein36352bf2015-03-25 18:17:31 -0700275 void initBatchTracker(const GrPipelineInfo& init) override {
joshualitt4f569be2015-02-27 11:41:49 -0800276 // Handle any color overrides
277 if (init.fColorIgnored) {
278 fGeoData[0].fColor = GrColor_ILLEGAL;
279 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
280 fGeoData[0].fColor = init.fOverrideColor;
281 }
282
283 // setup batch properties
284 fBatch.fColorIgnored = init.fColorIgnored;
285 fBatch.fColor = fGeoData[0].fColor;
286 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
287 fBatch.fCoverageIgnored = init.fCoverageIgnored;
288 }
289
290 struct DashDraw {
291 SkScalar fStartOffset;
292 SkScalar fStrokeWidth;
293 SkScalar fLineLength;
294 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700295 SkScalar fDevBloatX;
296 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800297 bool fLineDone;
298 bool fHasStartRect;
299 bool fHasEndRect;
300 };
301
mtklein36352bf2015-03-25 18:17:31 -0700302 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) override {
joshualitt4f569be2015-02-27 11:41:49 -0800303 int instanceCount = fGeoData.count();
304
305 SkMatrix invert;
306 if (this->usesLocalCoords() && !this->viewMatrix().invert(&invert)) {
307 SkDebugf("Failed to invert\n");
308 return;
309 }
310
311 SkPaint::Cap cap = this->cap();
312
313 SkAutoTUnref<const GrGeometryProcessor> gp;
314
315 bool isRoundCap = SkPaint::kRound_Cap == cap;
316 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
317 if (this->fullDash()) {
senorblancof3c2c462015-04-20 14:44:26 -0700318 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, invert));
joshualitt4f569be2015-02-27 11:41:49 -0800319 } else {
320 // Set up the vertex data for the line and start/end dashes
321 gp.reset(GrDefaultGeoProcFactory::Create(GrDefaultGeoProcFactory::kPosition_GPType,
322 this->color(),
323 SkMatrix::I(),
324 invert));
325 }
326
327 batchTarget->initDraw(gp, pipeline);
328
329 // TODO remove this when batch is everywhere
330 GrPipelineInfo init;
331 init.fColorIgnored = fBatch.fColorIgnored;
332 init.fOverrideColor = GrColor_ILLEGAL;
333 init.fCoverageIgnored = fBatch.fCoverageIgnored;
334 init.fUsesLocalCoords = this->usesLocalCoords();
335 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
336
senorblancof3c2c462015-04-20 14:44:26 -0700337 // useAA here means Edge AA or MSAA
338 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800339 bool fullDash = this->fullDash();
340
341 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
342 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
343 // iterate again over these decomposed dashes to generate vertices
344 SkSTArray<128, SkRect, true> rects;
345 SkSTArray<128, DashDraw, true> draws;
346
347 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800348 int rectOffset = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800349 for (int i = 0; i < instanceCount; i++) {
350 Geometry& args = fGeoData[i];
351
352 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
353
354 // We always want to at least stroke out half a pixel on each side in device space
355 // so 0.5f / perpScale gives us this min in src space
356 SkScalar halfSrcStroke =
357 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
358
359 SkScalar strokeAdj;
360 if (!hasCap) {
361 strokeAdj = 0.f;
362 } else {
363 strokeAdj = halfSrcStroke;
364 }
365
366 SkScalar startAdj = 0;
367
368 SkMatrix& combinedMatrix = args.fSrcRotInv;
369 combinedMatrix.postConcat(args.fViewMatrix);
370
371 bool lineDone = false;
372
373 // Too simplify the algorithm, we always push back rects for start and end rect.
374 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800375 rects.push_back();
376 rects.push_back();
377 rects.push_back();
378 SkRect& bounds = rects[rectOffset++];
379 SkRect& startRect = rects[rectOffset++];
380 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800381
382 bool hasStartRect = false;
383 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
384 // draw it separately here and adjust our start point accordingly
385 if (useAA) {
386 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
387 SkPoint startPts[2];
388 startPts[0] = args.fPtsRot[0];
389 startPts[1].fY = startPts[0].fY;
390 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
391 args.fPtsRot[1].fX);
392 startRect.set(startPts, 2);
393 startRect.outset(strokeAdj, halfSrcStroke);
394
395 hasStartRect = true;
396 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
397 }
398 }
399
400 // adjustments for start and end of bounding rect so we only draw dash intervals
401 // contained in the original line segment.
402 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
403 if (startAdj != 0) {
404 args.fPtsRot[0].fX += startAdj;
405 args.fPhase = 0;
406 }
407 SkScalar endingInterval = 0;
408 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
409 &endingInterval);
410 args.fPtsRot[1].fX -= endAdj;
411 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
412 lineDone = true;
413 }
414
415 bool hasEndRect = false;
416 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
417 // draw it separately here and adjust our end point accordingly
418 if (useAA && !lineDone) {
419 // If we adjusted the end then we will not be drawing a partial dash at the end.
420 // If we didn't adjust the end point then we just need to make sure the ending
421 // dash isn't a full dash
422 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
423 SkPoint endPts[2];
424 endPts[1] = args.fPtsRot[1];
425 endPts[0].fY = endPts[1].fY;
426 endPts[0].fX = endPts[1].fX - endingInterval;
427
428 endRect.set(endPts, 2);
429 endRect.outset(strokeAdj, halfSrcStroke);
430
431 hasEndRect = true;
432 endAdj = endingInterval + args.fIntervals[1];
433
434 args.fPtsRot[1].fX -= endAdj;
435 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
436 lineDone = true;
437 }
438 }
439 }
440
441 if (startAdj != 0) {
442 args.fPhase = 0;
443 }
444
445 // Change the dashing info from src space into device space
446 SkScalar* devIntervals = args.fIntervals;
447 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
448 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
449 SkScalar devPhase = args.fPhase * args.fParallelScale;
450 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
451
senorblancof3c2c462015-04-20 14:44:26 -0700452 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800453 strokeWidth = 1.f;
454 }
455
456 SkScalar halfDevStroke = strokeWidth * 0.5f;
457
458 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
459 // add cap to on interval and remove from off interval
460 devIntervals[0] += strokeWidth;
461 devIntervals[1] -= strokeWidth;
462 }
463 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
464
senorblancof3c2c462015-04-20 14:44:26 -0700465 // For EdgeAA, we bloat in X & Y for both square and round caps.
466 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
467 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
468 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
469 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800470
senorblancof3c2c462015-04-20 14:44:26 -0700471 SkScalar bloatX = devBloatX / args.fParallelScale;
472 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800473
474 if (devIntervals[1] <= 0.f && useAA) {
475 // Case when we end up drawing a solid AA rect
476 // Reset the start rect to draw this single solid rect
477 // but it requires to upload a new intervals uniform so we can mimic
478 // one giant dash
479 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
480 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
481 startRect.set(args.fPtsRot, 2);
482 startRect.outset(strokeAdj, halfSrcStroke);
483 hasStartRect = true;
484 hasEndRect = false;
485 lineDone = true;
486
487 SkPoint devicePts[2];
488 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
489 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
490 if (hasCap) {
491 lineLength += 2.f * halfDevStroke;
492 }
493 devIntervals[0] = lineLength;
494 }
495
496 totalRectCount += !lineDone ? 1 : 0;
497 totalRectCount += hasStartRect ? 1 : 0;
498 totalRectCount += hasEndRect ? 1 : 0;
499
500 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
501 // need to adjust this for round caps to correctly set the dashPos attrib on
502 // vertices
503 startOffset -= halfDevStroke;
504 }
505
506 DashDraw& draw = draws.push_back();
507 if (!lineDone) {
508 SkPoint devicePts[2];
509 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
510 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
511 if (hasCap) {
512 draw.fLineLength += 2.f * halfDevStroke;
513 }
514
515 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
516 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
517 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
518 }
519
520 if (hasStartRect) {
521 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
522 startRect.outset(bloatX, bloatY);
523 }
524
525 if (hasEndRect) {
526 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
527 endRect.outset(bloatX, bloatY);
528 }
529
530 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700531 draw.fDevBloatX = devBloatX;
532 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800533 draw.fHalfDevStroke = halfDevStroke;
534 draw.fStrokeWidth = strokeWidth;
535 draw.fHasStartRect = hasStartRect;
536 draw.fLineDone = lineDone;
537 draw.fHasEndRect = hasEndRect;
538 }
539
540 const GrVertexBuffer* vertexBuffer;
541 int firstVertex;
542
543 size_t vertexStride = gp->getVertexStride();
544 void* vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
545 totalRectCount * kVertsPerDash,
546 &vertexBuffer,
547 &firstVertex);
548
joshualitt4b31de82015-03-05 14:33:41 -0800549 if (!vertices || !batchTarget->quadIndexBuffer()) {
550 SkDebugf("Could not allocate buffers\n");
551 return;
552 }
553
joshualitt4f569be2015-02-27 11:41:49 -0800554 int curVIdx = 0;
555 int rectIndex = 0;
556 for (int i = 0; i < instanceCount; i++) {
557 Geometry& args = fGeoData[i];
558
559 if (!draws[i].fLineDone) {
560 if (fullDash) {
561 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700562 draws[i].fStartOffset, draws[i].fDevBloatX,
563 draws[i].fDevBloatY, draws[i].fLineLength,
564 draws[i].fHalfDevStroke, args.fIntervals[0],
565 args.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800566 capType, gp->getVertexStride());
567 } else {
568 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
569 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
570 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
571 }
572 curVIdx += 4;
573 }
574 rectIndex++;
575
576 if (draws[i].fHasStartRect) {
577 if (fullDash) {
578 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700579 draws[i].fStartOffset, draws[i].fDevBloatX,
580 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800581 draws[i].fHalfDevStroke, args.fIntervals[0],
582 args.fIntervals[1], draws[i].fStrokeWidth, capType,
583 gp->getVertexStride());
584 } else {
585 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
586 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
587 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
588 }
589
590 curVIdx += 4;
591 }
592 rectIndex++;
593
594 if (draws[i].fHasEndRect) {
595 if (fullDash) {
596 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, args.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700597 draws[i].fStartOffset, draws[i].fDevBloatX,
598 draws[i].fDevBloatY, args.fIntervals[0],
joshualitt4f569be2015-02-27 11:41:49 -0800599 draws[i].fHalfDevStroke, args.fIntervals[0],
600 args.fIntervals[1], draws[i].fStrokeWidth, capType,
601 gp->getVertexStride());
602 } else {
603 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
604 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
605 setup_dashed_rect_pos(rects[rectIndex], curVIdx, args.fSrcRotInv, verts);
606 }
607 curVIdx += 4;
608 }
609 rectIndex++;
610 }
611
612 const GrIndexBuffer* dashIndexBuffer = batchTarget->quadIndexBuffer();
613
614 GrDrawTarget::DrawInfo drawInfo;
615 drawInfo.setPrimitiveType(kTriangles_GrPrimitiveType);
616 drawInfo.setStartVertex(0);
617 drawInfo.setStartIndex(0);
618 drawInfo.setVerticesPerInstance(kVertsPerDash);
619 drawInfo.setIndicesPerInstance(kIndicesPerDash);
620 drawInfo.adjustStartVertex(firstVertex);
621 drawInfo.setVertexBuffer(vertexBuffer);
622 drawInfo.setIndexBuffer(dashIndexBuffer);
623
624 int maxInstancesPerDraw = dashIndexBuffer->maxQuads();
625 while (totalRectCount) {
626 drawInfo.setInstanceCount(SkTMin(totalRectCount, maxInstancesPerDraw));
627 drawInfo.setVertexCount(drawInfo.instanceCount() * drawInfo.verticesPerInstance());
628 drawInfo.setIndexCount(drawInfo.instanceCount() * drawInfo.indicesPerInstance());
629
630 batchTarget->draw(drawInfo);
631
632 drawInfo.setStartVertex(drawInfo.startVertex() + drawInfo.vertexCount());
633 totalRectCount -= drawInfo.instanceCount();
634 }
635 }
636
637 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
638
639private:
senorblancof3c2c462015-04-20 14:44:26 -0700640 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash) {
joshualitt4f569be2015-02-27 11:41:49 -0800641 this->initClassID<DashBatch>();
642 fGeoData.push_back(geometry);
643
senorblancof3c2c462015-04-20 14:44:26 -0700644 fBatch.fAAMode = aaMode;
joshualitt4f569be2015-02-27 11:41:49 -0800645 fBatch.fCap = cap;
646 fBatch.fFullDash = fullDash;
647 }
648
mtklein36352bf2015-03-25 18:17:31 -0700649 bool onCombineIfPossible(GrBatch* t) override {
joshualitt4f569be2015-02-27 11:41:49 -0800650 DashBatch* that = t->cast<DashBatch>();
651
senorblancof3c2c462015-04-20 14:44:26 -0700652 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800653 return false;
654 }
655
656 if (this->fullDash() != that->fullDash()) {
657 return false;
658 }
659
660 if (this->cap() != that->cap()) {
661 return false;
662 }
663
664 // TODO vertex color
665 if (this->color() != that->color()) {
666 return false;
667 }
668
669 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
670 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
671 return false;
672 }
673
674 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
675 return true;
676 }
677
678 GrColor color() const { return fBatch.fColor; }
679 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
680 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700681 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800682 bool fullDash() const { return fBatch.fFullDash; }
683 SkPaint::Cap cap() const { return fBatch.fCap; }
684
685 struct BatchTracker {
686 GrColor fColor;
687 bool fUsesLocalCoords;
688 bool fColorIgnored;
689 bool fCoverageIgnored;
690 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700691 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800692 bool fFullDash;
693 };
694
695 static const int kVertsPerDash = 4;
696 static const int kIndicesPerDash = 6;
697
698 BatchTracker fBatch;
699 SkSTArray<1, Geometry, true> fGeoData;
700};
701
702
egdaniel8dd688b2015-01-22 10:16:09 -0800703bool GrDashingEffect::DrawDashLine(GrGpu* gpu, GrDrawTarget* target,
704 GrPipelineBuilder* pipelineBuilder, GrColor color,
705 const SkMatrix& viewMatrix, const SkPoint pts[2],
joshualitt8059eb92014-12-29 15:10:07 -0800706 const GrPaint& paint, const GrStrokeInfo& strokeInfo) {
egdaniel8dd688b2015-01-22 10:16:09 -0800707 if (!can_fast_path_dash(pts, strokeInfo, *target, *pipelineBuilder, viewMatrix)) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708 return false;
709 }
710
egdaniele61c4112014-06-12 10:24:21 -0700711 const SkPathEffect::DashInfo& info = strokeInfo.getDashInfo();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000712
egdaniele61c4112014-06-12 10:24:21 -0700713 SkPaint::Cap cap = strokeInfo.getStrokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000714
joshualitt4f569be2015-02-27 11:41:49 -0800715 DashBatch::Geometry geometry;
716 geometry.fSrcStrokeWidth = strokeInfo.getStrokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000717
718 // the phase should be normalized to be [0, sum of all intervals)
719 SkASSERT(info.fPhase >= 0 && info.fPhase < info.fIntervals[0] + info.fIntervals[1]);
720
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000721 // 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 +0000722 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700723 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800724 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
725 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700726 SkDebugf("Failed to create invertible rotation matrix!\n");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000727 return false;
728 }
729 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800730 geometry.fSrcRotInv.reset();
731 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
732 }
733
734 // Scale corrections of intervals and stroke from view matrix
735 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
736 geometry.fPtsRot);
737
738 SkScalar offInterval = info.fIntervals[1] * geometry.fParallelScale;
739 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
740
741 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
742 // add cap to on interveal and remove from off interval
743 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000744 }
745
senorblancof3c2c462015-04-20 14:44:26 -0700746 DashAAMode aaMode = pipelineBuilder->getRenderTarget()->isMultisampled() ? kMSAA_DashAAMode :
747 paint.isAntiAlias() ? kEdgeAA_DashAAMode :
748 kBW_DashAAMode;
749
joshualitt4f569be2015-02-27 11:41:49 -0800750 // 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 -0700751 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000752
joshualitt4f569be2015-02-27 11:41:49 -0800753 geometry.fColor = color;
754 geometry.fViewMatrix = viewMatrix;
755 geometry.fPhase = info.fPhase;
756 geometry.fIntervals[0] = info.fIntervals[0];
757 geometry.fIntervals[1] = info.fIntervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000758
senorblancof3c2c462015-04-20 14:44:26 -0700759 SkAutoTUnref<GrBatch> batch(DashBatch::Create(geometry, cap, aaMode, fullDash));
joshualitt4f569be2015-02-27 11:41:49 -0800760 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000761
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000762 return true;
763}
764
765//////////////////////////////////////////////////////////////////////////////
766
egdanielf767e792014-07-02 06:21:32 -0700767class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800768
769struct DashingCircleBatchTracker {
770 GrGPInput fInputColorType;
771 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -0800772 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800773};
774
egdanielf767e792014-07-02 06:21:32 -0700775/*
776 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
777 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
778 * Both of the previous two parameters are in device space. This effect also requires the setting of
779 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
780 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
781 * transform the line to be horizontal, with the start of line at the origin then shifted to the
782 * right by half the off interval. The line then goes in the positive x direction.
783 */
joshualitt249af152014-09-15 11:41:13 -0700784class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700785public:
786 typedef SkPathEffect::DashInfo DashInfo;
787
joshualitt2e3b3e32014-12-09 13:31:14 -0800788 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700789 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800790 const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700791
792 virtual ~DashingCircleEffect();
793
mtklein36352bf2015-03-25 18:17:31 -0700794 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700795
joshualitt71c92602015-01-14 08:12:47 -0800796 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800797
joshualitt5224ba72015-02-03 15:07:51 -0800798 const Attribute* inDashParams() const { return fInDashParams; }
799
800 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700801
senorblancof3c2c462015-04-20 14:44:26 -0700802 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700803
joshualitteb2a6762014-12-04 11:35:33 -0800804 virtual void getGLProcessorKey(const GrBatchTracker&,
805 const GrGLCaps&,
mtklein36352bf2015-03-25 18:17:31 -0700806 GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700807
joshualittabb52a12015-01-13 15:02:10 -0800808 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker&,
mtklein36352bf2015-03-25 18:17:31 -0700809 const GrGLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700810
mtklein36352bf2015-03-25 18:17:31 -0700811 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -0800812
joshualitt290c09b2014-12-19 13:45:20 -0800813 bool onCanMakeEqual(const GrBatchTracker&,
814 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700815 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -0800816
egdanielf767e792014-07-02 06:21:32 -0700817private:
senorblancof3c2c462015-04-20 14:44:26 -0700818 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
egdanielf767e792014-07-02 06:21:32 -0700819
mtklein36352bf2015-03-25 18:17:31 -0700820 bool onIsEqual(const GrGeometryProcessor& other) const override;
egdanielf767e792014-07-02 06:21:32 -0700821
mtklein36352bf2015-03-25 18:17:31 -0700822 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -0700823
senorblancof3c2c462015-04-20 14:44:26 -0700824 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800825 const Attribute* fInPosition;
826 const Attribute* fInDashParams;
827 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700828
joshualittb0a8a372014-09-23 09:50:21 -0700829 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700830
joshualitt249af152014-09-15 11:41:13 -0700831 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700832};
833
834//////////////////////////////////////////////////////////////////////////////
835
joshualitt249af152014-09-15 11:41:13 -0700836class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700837public:
joshualitteb2a6762014-12-04 11:35:33 -0800838 GLDashingCircleEffect(const GrGeometryProcessor&, const GrBatchTracker&);
egdanielf767e792014-07-02 06:21:32 -0700839
mtklein36352bf2015-03-25 18:17:31 -0700840 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700841
joshualitt87f48d92014-12-04 10:41:40 -0800842 static inline void GenKey(const GrGeometryProcessor&,
843 const GrBatchTracker&,
844 const GrGLCaps&,
845 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700846
joshualitt87f48d92014-12-04 10:41:40 -0800847 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -0800848 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -0700849 const GrBatchTracker&) override;
egdanielf767e792014-07-02 06:21:32 -0700850
851private:
joshualitt9b989322014-12-15 14:16:27 -0800852 UniformHandle fParamUniform;
853 UniformHandle fColorUniform;
854 GrColor fColor;
855 SkScalar fPrevRadius;
856 SkScalar fPrevCenterX;
857 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700858 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700859};
860
joshualitteb2a6762014-12-04 11:35:33 -0800861GLDashingCircleEffect::GLDashingCircleEffect(const GrGeometryProcessor&,
862 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -0800863 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700864 fPrevRadius = SK_ScalarMin;
865 fPrevCenterX = SK_ScalarMin;
866 fPrevIntervalLength = SK_ScalarMax;
867}
868
robertphillips46d36f02015-01-18 08:14:14 -0800869void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700870 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
joshualitt9b989322014-12-15 14:16:27 -0800871 const DashingCircleBatchTracker local = args.fBT.cast<DashingCircleBatchTracker>();
872 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -0800873 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
874
joshualittabb52a12015-01-13 15:02:10 -0800875 // emit attributes
876 vsBuilder->emitAttributes(dce);
877
joshualitt5224ba72015-02-03 15:07:51 -0800878 // XY are dashPos, Z is dashInterval
879 GrGLVertToFrag dashParams(kVec3f_GrSLType);
880 args.fPB->addVarying("DashParam", &dashParams);
881 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
882
senorblancof3c2c462015-04-20 14:44:26 -0700883 // x refers to circle radius - 0.5, y refers to cicle's center x coord
joshualitt5224ba72015-02-03 15:07:51 -0800884 GrGLVertToFrag circleParams(kVec2f_GrSLType);
885 args.fPB->addVarying("CircleParams", &circleParams);
886 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700887
joshualitt9b989322014-12-15 14:16:27 -0800888 // Setup pass through color
889 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
890
joshualittabb52a12015-01-13 15:02:10 -0800891 // Setup position
joshualittdd219872015-02-12 14:48:42 -0800892 this->setupPosition(pb, gpArgs, dce.inPosition()->fName, dce.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -0800893
joshualittabb52a12015-01-13 15:02:10 -0800894 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800895 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800896 args.fTransformsIn, args.fTransformsOut);
897
egdanielf767e792014-07-02 06:21:32 -0700898 // transforms all points so that we can compare them to our test circle
joshualittc369e7c2014-10-22 10:56:26 -0700899 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800900 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
901 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
902 dashParams.fsIn());
903 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
904 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
905 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700906 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800907 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
908 fsBuilder->codeAppend("diff = 1.0 - diff;");
909 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700910 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800911 fsBuilder->codeAppendf("float alpha = 1.0;");
912 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700913 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800914 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700915}
916
joshualitt87f48d92014-12-04 10:41:40 -0800917void GLDashingCircleEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -0800918 const GrPrimitiveProcessor& processor,
919 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -0800920 this->setUniformViewMatrix(pdman, processor.viewMatrix());
921
joshualitt9b989322014-12-15 14:16:27 -0800922 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
923 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
924 GrGLfloat c[4];
925 GrColorToRGBAFloat(local.fColor, c);
926 pdman.set4fv(fColorUniform, 1, c);
927 fColor = local.fColor;
928 }
egdanielf767e792014-07-02 06:21:32 -0700929}
930
robertphillips46d36f02015-01-18 08:14:14 -0800931void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -0800932 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -0800933 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700934 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -0800935 const DashingCircleBatchTracker& local = bt.cast<DashingCircleBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -0800936 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
937 uint32_t key = 0;
938 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
939 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -0700940 key |= dce.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -0800941 b->add32(key << 16 | local.fInputColorType);
egdanielf767e792014-07-02 06:21:32 -0700942}
943
944//////////////////////////////////////////////////////////////////////////////
945
joshualitt2e3b3e32014-12-09 13:31:14 -0800946GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700947 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800948 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -0700949 return SkNEW_ARGS(DashingCircleEffect, (color, aaMode, localMatrix));
egdanielf767e792014-07-02 06:21:32 -0700950}
951
952DashingCircleEffect::~DashingCircleEffect() {}
953
joshualitt56995b52014-12-11 15:44:02 -0800954void DashingCircleEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
955 out->setUnknownSingleComponent();
egdanielf767e792014-07-02 06:21:32 -0700956}
957
joshualitteb2a6762014-12-04 11:35:33 -0800958void DashingCircleEffect::getGLProcessorKey(const GrBatchTracker& bt,
959 const GrGLCaps& caps,
960 GrProcessorKeyBuilder* b) const {
961 GLDashingCircleEffect::GenKey(*this, bt, caps, b);
962}
963
joshualittabb52a12015-01-13 15:02:10 -0800964GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrBatchTracker& bt,
965 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -0800966 return SkNEW_ARGS(GLDashingCircleEffect, (*this, bt));
egdanielf767e792014-07-02 06:21:32 -0700967}
968
joshualitt2e3b3e32014-12-09 13:31:14 -0800969DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700970 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -0800971 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -0700972 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800973 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800974 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800975 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
976 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
977 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700978}
979
bsalomon0e08fc12014-10-15 08:19:04 -0700980bool DashingCircleEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -0700981 const DashingCircleEffect& dce = other.cast<DashingCircleEffect>();
senorblancof3c2c462015-04-20 14:44:26 -0700982 return fAAMode == dce.fAAMode;
egdanielf767e792014-07-02 06:21:32 -0700983}
984
joshualitt4d8da812015-01-28 12:53:54 -0800985void DashingCircleEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -0800986 DashingCircleBatchTracker* local = bt->cast<DashingCircleBatchTracker>();
987 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -0800988 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -0800989}
990
joshualitt290c09b2014-12-19 13:45:20 -0800991bool DashingCircleEffect::onCanMakeEqual(const GrBatchTracker& m,
992 const GrGeometryProcessor& that,
993 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -0800994 const DashingCircleBatchTracker& mine = m.cast<DashingCircleBatchTracker>();
995 const DashingCircleBatchTracker& theirs = t.cast<DashingCircleBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -0800996 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
997 that, theirs.fUsesLocalCoords) &&
998 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -0800999 theirs.fInputColorType, theirs.fColor);
1000}
1001
joshualittb0a8a372014-09-23 09:50:21 -07001002GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -07001003
joshualittb0a8a372014-09-23 09:50:21 -07001004GrGeometryProcessor* DashingCircleEffect::TestCreate(SkRandom* random,
1005 GrContext*,
1006 const GrDrawTargetCaps& caps,
1007 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001008 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001009 return DashingCircleEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001010 aaMode, GrProcessorUnitTest::TestMatrix(random));
egdanielf767e792014-07-02 06:21:32 -07001011}
1012
1013//////////////////////////////////////////////////////////////////////////////
1014
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001015class GLDashingLineEffect;
1016
joshualitt9b989322014-12-15 14:16:27 -08001017struct DashingLineBatchTracker {
1018 GrGPInput fInputColorType;
1019 GrColor fColor;
joshualitt290c09b2014-12-19 13:45:20 -08001020 bool fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001021};
1022
egdanielf767e792014-07-02 06:21:32 -07001023/*
1024 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1025 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
1026 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
1027 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1028 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1029 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1030 * positive x direction.
1031 */
joshualitt249af152014-09-15 11:41:13 -07001032class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001033public:
1034 typedef SkPathEffect::DashInfo DashInfo;
1035
joshualitt2e3b3e32014-12-09 13:31:14 -08001036 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001037 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001038 const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001039
1040 virtual ~DashingLineEffect();
1041
mtklein36352bf2015-03-25 18:17:31 -07001042 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001043
joshualitt71c92602015-01-14 08:12:47 -08001044 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001045
joshualitt5224ba72015-02-03 15:07:51 -08001046 const Attribute* inDashParams() const { return fInDashParams; }
1047
1048 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001049
senorblancof3c2c462015-04-20 14:44:26 -07001050 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001051
joshualitteb2a6762014-12-04 11:35:33 -08001052 virtual void getGLProcessorKey(const GrBatchTracker& bt,
1053 const GrGLCaps& caps,
mtklein36352bf2015-03-25 18:17:31 -07001054 GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001055
joshualittabb52a12015-01-13 15:02:10 -08001056 virtual GrGLPrimitiveProcessor* createGLInstance(const GrBatchTracker& bt,
mtklein36352bf2015-03-25 18:17:31 -07001057 const GrGLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001058
mtklein36352bf2015-03-25 18:17:31 -07001059 void initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const override;
joshualitt9b989322014-12-15 14:16:27 -08001060
joshualitt290c09b2014-12-19 13:45:20 -08001061 bool onCanMakeEqual(const GrBatchTracker&,
1062 const GrGeometryProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001063 const GrBatchTracker&) const override;
joshualitt9b989322014-12-15 14:16:27 -08001064
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001065private:
senorblancof3c2c462015-04-20 14:44:26 -07001066 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001067
mtklein36352bf2015-03-25 18:17:31 -07001068 bool onIsEqual(const GrGeometryProcessor& other) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001069
mtklein36352bf2015-03-25 18:17:31 -07001070 void onGetInvariantOutputCoverage(GrInitInvariantOutput*) const override;
egdaniel1a8ecdf2014-10-03 06:24:12 -07001071
senorblancof3c2c462015-04-20 14:44:26 -07001072 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001073 const Attribute* fInPosition;
1074 const Attribute* fInDashParams;
1075 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001076
joshualittb0a8a372014-09-23 09:50:21 -07001077 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001078
joshualitt249af152014-09-15 11:41:13 -07001079 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001080};
1081
1082//////////////////////////////////////////////////////////////////////////////
1083
joshualitt249af152014-09-15 11:41:13 -07001084class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001085public:
joshualitteb2a6762014-12-04 11:35:33 -08001086 GLDashingLineEffect(const GrGeometryProcessor&, const GrBatchTracker&);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001087
mtklein36352bf2015-03-25 18:17:31 -07001088 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001089
joshualitt87f48d92014-12-04 10:41:40 -08001090 static inline void GenKey(const GrGeometryProcessor&,
1091 const GrBatchTracker&,
1092 const GrGLCaps&,
1093 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001094
joshualitt87f48d92014-12-04 10:41:40 -08001095 virtual void setData(const GrGLProgramDataManager&,
joshualitt9b989322014-12-15 14:16:27 -08001096 const GrPrimitiveProcessor&,
mtklein36352bf2015-03-25 18:17:31 -07001097 const GrBatchTracker&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001098
1099private:
joshualitt9b989322014-12-15 14:16:27 -08001100 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001101 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001102 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001103};
1104
joshualitteb2a6762014-12-04 11:35:33 -08001105GLDashingLineEffect::GLDashingLineEffect(const GrGeometryProcessor&,
1106 const GrBatchTracker&) {
joshualitt9b989322014-12-15 14:16:27 -08001107 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001108}
1109
robertphillips46d36f02015-01-18 08:14:14 -08001110void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001111 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt9b989322014-12-15 14:16:27 -08001112 const DashingLineBatchTracker& local = args.fBT.cast<DashingLineBatchTracker>();
1113 GrGLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001114
1115 GrGLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
1116
joshualittabb52a12015-01-13 15:02:10 -08001117 // emit attributes
1118 vsBuilder->emitAttributes(de);
1119
joshualitt5224ba72015-02-03 15:07:51 -08001120 // XY refers to dashPos, Z is the dash interval length
1121 GrGLVertToFrag inDashParams(kVec3f_GrSLType);
1122 args.fPB->addVarying("DashParams", &inDashParams);
1123 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1124
1125 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1126 // respectively.
1127 GrGLVertToFrag inRectParams(kVec4f_GrSLType);
1128 args.fPB->addVarying("RectParams", &inRectParams);
1129 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001130
joshualitt9b989322014-12-15 14:16:27 -08001131 // Setup pass through color
1132 this->setupColorPassThrough(pb, local.fInputColorType, args.fOutputColor, NULL, &fColorUniform);
1133
joshualittabb52a12015-01-13 15:02:10 -08001134 // Setup position
joshualittdd219872015-02-12 14:48:42 -08001135 this->setupPosition(pb, gpArgs, de.inPosition()->fName, de.viewMatrix());
joshualitt4973d9d2014-11-08 09:24:25 -08001136
joshualittabb52a12015-01-13 15:02:10 -08001137 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001138 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001139 args.fTransformsIn, args.fTransformsOut);
1140
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001141 // transforms all points so that we can compare them to our test rect
joshualittc369e7c2014-10-22 10:56:26 -07001142 GrGLGPFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001143 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1144 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1145 inDashParams.fsIn());
1146 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001147 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001148 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1149 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001150 fsBuilder->codeAppend("float xSub, ySub;");
1151 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1152 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1153 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1154 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001155 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1156 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001157 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001158 } else if (de.aaMode() == kMSAA_DashAAMode) {
1159 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1160 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1161 fsBuilder->codeAppend("float xSub;");
1162 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1163 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1164 // Now compute coverage in x to get the fraction of the pixel covered.
1165 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001166 } else {
1167 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001168 fsBuilder->codeAppendf("float alpha = 1.0;");
1169 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1170 inRectParams.fsIn());
1171 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1172 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001173 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001174 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001175}
1176
joshualittb0a8a372014-09-23 09:50:21 -07001177void GLDashingLineEffect::setData(const GrGLProgramDataManager& pdman,
joshualitt9b989322014-12-15 14:16:27 -08001178 const GrPrimitiveProcessor& processor,
1179 const GrBatchTracker& bt) {
joshualittee2af952014-12-30 09:04:15 -08001180 this->setUniformViewMatrix(pdman, processor.viewMatrix());
1181
joshualitt9b989322014-12-15 14:16:27 -08001182 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
1183 if (kUniform_GrGPInput == local.fInputColorType && local.fColor != fColor) {
1184 GrGLfloat c[4];
1185 GrColorToRGBAFloat(local.fColor, c);
1186 pdman.set4fv(fColorUniform, 1, c);
1187 fColor = local.fColor;
1188 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001189}
1190
robertphillips46d36f02015-01-18 08:14:14 -08001191void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
joshualitt9b989322014-12-15 14:16:27 -08001192 const GrBatchTracker& bt,
joshualitt87f48d92014-12-04 10:41:40 -08001193 const GrGLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001194 GrProcessorKeyBuilder* b) {
joshualitt9b989322014-12-15 14:16:27 -08001195 const DashingLineBatchTracker& local = bt.cast<DashingLineBatchTracker>();
robertphillips46d36f02015-01-18 08:14:14 -08001196 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1197 uint32_t key = 0;
1198 key |= local.fUsesLocalCoords && gp.localMatrix().hasPerspective() ? 0x1 : 0x0;
1199 key |= ComputePosKey(gp.viewMatrix()) << 1;
senorblancof3c2c462015-04-20 14:44:26 -07001200 key |= de.aaMode() << 8;
robertphillips46d36f02015-01-18 08:14:14 -08001201 b->add32(key << 16 | local.fInputColorType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001202}
1203
1204//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001205
joshualitt2e3b3e32014-12-09 13:31:14 -08001206GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001207 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001208 const SkMatrix& localMatrix) {
senorblancof3c2c462015-04-20 14:44:26 -07001209 return SkNEW_ARGS(DashingLineEffect, (color, aaMode, localMatrix));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001210}
1211
1212DashingLineEffect::~DashingLineEffect() {}
1213
joshualitt56995b52014-12-11 15:44:02 -08001214void DashingLineEffect::onGetInvariantOutputCoverage(GrInitInvariantOutput* out) const {
1215 out->setUnknownSingleComponent();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001216}
1217
joshualitteb2a6762014-12-04 11:35:33 -08001218void DashingLineEffect::getGLProcessorKey(const GrBatchTracker& bt,
1219 const GrGLCaps& caps,
1220 GrProcessorKeyBuilder* b) const {
1221 GLDashingLineEffect::GenKey(*this, bt, caps, b);
1222}
1223
joshualittabb52a12015-01-13 15:02:10 -08001224GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrBatchTracker& bt,
1225 const GrGLCaps&) const {
joshualitteb2a6762014-12-04 11:35:33 -08001226 return SkNEW_ARGS(GLDashingLineEffect, (*this, bt));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001227}
1228
joshualitt2e3b3e32014-12-09 13:31:14 -08001229DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001230 DashAAMode aaMode,
joshualittd27f73e2014-12-29 07:43:36 -08001231 const SkMatrix& localMatrix)
senorblancof3c2c462015-04-20 14:44:26 -07001232 : INHERITED(color, SkMatrix::I(), localMatrix), fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001233 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001234 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001235 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1236 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001237}
1238
bsalomon0e08fc12014-10-15 08:19:04 -07001239bool DashingLineEffect::onIsEqual(const GrGeometryProcessor& other) const {
joshualitt49586be2014-09-16 08:21:41 -07001240 const DashingLineEffect& de = other.cast<DashingLineEffect>();
senorblancof3c2c462015-04-20 14:44:26 -07001241 return fAAMode == de.fAAMode;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001242}
1243
joshualitt4d8da812015-01-28 12:53:54 -08001244void DashingLineEffect::initBatchTracker(GrBatchTracker* bt, const GrPipelineInfo& init) const {
joshualitt9b989322014-12-15 14:16:27 -08001245 DashingLineBatchTracker* local = bt->cast<DashingLineBatchTracker>();
1246 local->fInputColorType = GetColorInputType(&local->fColor, this->color(), init, false);
joshualitt290c09b2014-12-19 13:45:20 -08001247 local->fUsesLocalCoords = init.fUsesLocalCoords;
joshualitt9b989322014-12-15 14:16:27 -08001248}
1249
joshualitt290c09b2014-12-19 13:45:20 -08001250bool DashingLineEffect::onCanMakeEqual(const GrBatchTracker& m,
1251 const GrGeometryProcessor& that,
1252 const GrBatchTracker& t) const {
joshualitt9b989322014-12-15 14:16:27 -08001253 const DashingLineBatchTracker& mine = m.cast<DashingLineBatchTracker>();
1254 const DashingLineBatchTracker& theirs = t.cast<DashingLineBatchTracker>();
joshualitt290c09b2014-12-19 13:45:20 -08001255 return CanCombineLocalMatrices(*this, mine.fUsesLocalCoords,
1256 that, theirs.fUsesLocalCoords) &&
1257 CanCombineOutput(mine.fInputColorType, mine.fColor,
joshualitt9b989322014-12-15 14:16:27 -08001258 theirs.fInputColorType, theirs.fColor);
1259}
1260
joshualittb0a8a372014-09-23 09:50:21 -07001261GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001262
joshualittb0a8a372014-09-23 09:50:21 -07001263GrGeometryProcessor* DashingLineEffect::TestCreate(SkRandom* random,
1264 GrContext*,
1265 const GrDrawTargetCaps& caps,
1266 GrTexture*[]) {
senorblancof3c2c462015-04-20 14:44:26 -07001267 DashAAMode aaMode = static_cast<DashAAMode>(random->nextULessThan(kDashAAModeCount));
joshualitt8059eb92014-12-29 15:10:07 -08001268 return DashingLineEffect::Create(GrRandomColor(random),
senorblancof3c2c462015-04-20 14:44:26 -07001269 aaMode, GrProcessorUnitTest::TestMatrix(random));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001270}
1271
1272//////////////////////////////////////////////////////////////////////////////
1273
joshualitt5224ba72015-02-03 15:07:51 -08001274static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001275 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001276 DashCap cap,
1277 const SkMatrix& localMatrix) {
egdanielf767e792014-07-02 06:21:32 -07001278 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001279 case kRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001280 return DashingCircleEffect::Create(color, dashAAMode, localMatrix);
joshualitt5224ba72015-02-03 15:07:51 -08001281 case kNonRound_DashCap:
senorblancof3c2c462015-04-20 14:44:26 -07001282 return DashingLineEffect::Create(color, dashAAMode, localMatrix);
egdanielf767e792014-07-02 06:21:32 -07001283 default:
1284 SkFAIL("Unexpected dashed cap.");
1285 }
1286 return NULL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001287}