blob: 09dc90a529734ee51f9845e6c8976af119c9c074 [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
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualittfa2008f2015-04-29 11:32:05 -070011#include "GrBatchTest.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070012#include "GrCaps.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"
egdaniel605dd0f2014-11-12 08:35:25 -080018#include "GrInvariantOutput.h"
joshualittb0a8a372014-09-23 09:50:21 -070019#include "GrProcessor.h"
egdaniele61c4112014-06-12 10:24:21 -070020#include "GrStrokeInfo.h"
bsalomon72e3ae42015-04-28 08:08:46 -070021#include "GrVertexBuffer.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000022#include "SkGr.h"
bsalomon16b99132015-08-13 14:55:50 -070023#include "batches/GrVertexBatch.h"
egdaniel2d721d32015-11-11 13:06:05 -080024#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080025#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070026#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080027#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080028#include "glsl/GrGLSLVarying.h"
egdaniel2d721d32015-11-11 13:06:05 -080029#include "glsl/GrGLSLVertexShaderBuilder.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
kkinnunen261694c2015-05-05 08:00:10 -070047 if (!strokeInfo.isDashed() || 2 != strokeInfo.getDashCount()) {
egdaniele61c4112014-06-12 10:24:21 -070048 return false;
49 }
50
kkinnunen261694c2015-05-05 08:00:10 -070051 const SkScalar* intervals = strokeInfo.getDashIntervals();
52 if (0 == intervals[0] && 0 == intervals[1]) {
egdaniele61c4112014-06-12 10:24:21 -070053 return false;
54 }
55
kkinnunend156d362015-05-18 22:23:54 -070056 SkPaint::Cap cap = strokeInfo.getCap();
egdaniele61c4112014-06-12 10:24:21 -070057 // Current we do don't handle Round or Square cap dashes
kkinnunen261694c2015-05-05 08:00:10 -070058 if (SkPaint::kRound_Cap == cap && intervals[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
halcanary96fcdcc2015-08-27 07:41:13 -0700109static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = nullptr) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000110 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;
reed80ea19c2015-05-12 10:37:34 -0700140 SkScalar temp = totalLen / srcIntervalLen;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000141 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700142 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
reed80ea19c2015-05-12 10:37:34 -0700143 temp = *endingInt / srcIntervalLen;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000144 *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,
joshualittb8c241a2015-05-19 08:23:30 -0700246 const SkMatrix& localMatrix,
247 bool usesLocalCoords);
joshualitt5224ba72015-02-03 15:07:51 -0800248
bsalomonabd30f52015-08-13 13:34:48 -0700249class DashBatch : public GrVertexBatch {
joshualitt4f569be2015-02-27 11:41:49 -0800250public:
reed1b55a962015-09-17 20:16:13 -0700251 DEFINE_BATCH_CLASS_ID
252
joshualitt4f569be2015-02-27 11:41:49 -0800253 struct Geometry {
joshualitt4f569be2015-02-27 11:41:49 -0800254 SkMatrix fViewMatrix;
255 SkMatrix fSrcRotInv;
256 SkPoint fPtsRot[2];
257 SkScalar fSrcStrokeWidth;
258 SkScalar fPhase;
259 SkScalar fIntervals[2];
260 SkScalar fParallelScale;
261 SkScalar fPerpendicularScale;
reed1b55a962015-09-17 20:16:13 -0700262 GrColor fColor;
joshualitt4f569be2015-02-27 11:41:49 -0800263 };
264
bsalomonabd30f52015-08-13 13:34:48 -0700265 static GrDrawBatch* Create(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode,
266 bool fullDash) {
halcanary385fe4d2015-08-26 13:07:48 -0700267 return new DashBatch(geometry, cap, aaMode, fullDash);
joshualitt4f569be2015-02-27 11:41:49 -0800268 }
269
mtklein36352bf2015-03-25 18:17:31 -0700270 const char* name() const override { return "DashBatch"; }
joshualitt4f569be2015-02-27 11:41:49 -0800271
ethannicholasff210322015-11-24 12:10:10 -0800272 void computePipelineOptimizations(GrInitInvariantOutput* color,
273 GrInitInvariantOutput* coverage,
274 GrBatchToXPOverrides* overrides) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800275 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800276 color->setKnownFourComponents(fGeoData[0].fColor);
277 coverage->setUnknownSingleComponent();
joshualitt4f569be2015-02-27 11:41:49 -0800278 }
279
bsalomone46f9fe2015-08-18 06:05:14 -0700280 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
281
282private:
reed1b55a962015-09-17 20:16:13 -0700283 DashBatch(const Geometry& geometry, SkPaint::Cap cap, DashAAMode aaMode, bool fullDash)
284 : INHERITED(ClassID()) {
bsalomone46f9fe2015-08-18 06:05:14 -0700285 fGeoData.push_back(geometry);
286
287 fBatch.fAAMode = aaMode;
288 fBatch.fCap = cap;
289 fBatch.fFullDash = fullDash;
290
291 // compute bounds
292 SkScalar halfStrokeWidth = 0.5f * geometry.fSrcStrokeWidth;
293 SkScalar xBloat = SkPaint::kButt_Cap == cap ? 0 : halfStrokeWidth;
294 fBounds.set(geometry.fPtsRot[0], geometry.fPtsRot[1]);
295 fBounds.outset(xBloat, halfStrokeWidth);
296
297 // Note, we actually create the combined matrix here, and save the work
298 SkMatrix& combinedMatrix = fGeoData[0].fSrcRotInv;
299 combinedMatrix.postConcat(geometry.fViewMatrix);
300 combinedMatrix.mapRect(&fBounds);
301 }
302
ethannicholasff210322015-11-24 12:10:10 -0800303 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
joshualitt4f569be2015-02-27 11:41:49 -0800304 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800305 if (!overrides.readsColor()) {
joshualitt4f569be2015-02-27 11:41:49 -0800306 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt4f569be2015-02-27 11:41:49 -0800307 }
ethannicholasff210322015-11-24 12:10:10 -0800308 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt4f569be2015-02-27 11:41:49 -0800309
310 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800311 fBatch.fColorIgnored = !overrides.readsColor();
joshualitt4f569be2015-02-27 11:41:49 -0800312 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -0800313 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
314 fBatch.fCoverageIgnored = !overrides.readsCoverage();
joshualitt4f569be2015-02-27 11:41:49 -0800315 }
316
317 struct DashDraw {
joshualitt144c3c82015-11-30 12:30:13 -0800318 DashDraw(const Geometry& geo) {
319 memcpy(fPtsRot, geo.fPtsRot, sizeof(geo.fPtsRot));
320 memcpy(fIntervals, geo.fIntervals, sizeof(geo.fIntervals));
321 fPhase = geo.fPhase;
322 }
323 SkPoint fPtsRot[2];
324 SkScalar fIntervals[2];
325 SkScalar fPhase;
joshualitt4f569be2015-02-27 11:41:49 -0800326 SkScalar fStartOffset;
327 SkScalar fStrokeWidth;
328 SkScalar fLineLength;
329 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700330 SkScalar fDevBloatX;
331 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800332 bool fLineDone;
333 bool fHasStartRect;
334 bool fHasEndRect;
335 };
336
joshualitt144c3c82015-11-30 12:30:13 -0800337 void onPrepareDraws(Target* target) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800338 int instanceCount = fGeoData.count();
joshualitt4f569be2015-02-27 11:41:49 -0800339 SkPaint::Cap cap = this->cap();
joshualitte494a582015-08-03 09:32:36 -0700340 bool isRoundCap = SkPaint::kRound_Cap == cap;
341 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
joshualittdf0c5572015-08-03 11:35:28 -0700342
343 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt4f569be2015-02-27 11:41:49 -0800344 if (this->fullDash()) {
joshualittdf0c5572015-08-03 11:35:28 -0700345 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, this->viewMatrix(),
joshualittb8c241a2015-05-19 08:23:30 -0700346 this->usesLocalCoords()));
joshualitt4f569be2015-02-27 11:41:49 -0800347 } else {
348 // Set up the vertex data for the line and start/end dashes
joshualittdf0c5572015-08-03 11:35:28 -0700349 using namespace GrDefaultGeoProcFactory;
350 Color color(this->color());
351 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type :
352 Coverage::kSolid_Type);
353 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosition_Type :
354 LocalCoords::kUnused_Type);
355 gp.reset(CreateForDeviceSpace(color, coverage, localCoords, this->viewMatrix()));
356 }
357
358 if (!gp) {
359 SkDebugf("Could not create GrGeometryProcessor\n");
360 return;
joshualitt4f569be2015-02-27 11:41:49 -0800361 }
362
bsalomon75398562015-08-17 12:55:38 -0700363 target->initDraw(gp, this->pipeline());
joshualitt4f569be2015-02-27 11:41:49 -0800364
senorblancof3c2c462015-04-20 14:44:26 -0700365 // useAA here means Edge AA or MSAA
366 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800367 bool fullDash = this->fullDash();
368
369 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
370 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
371 // iterate again over these decomposed dashes to generate vertices
joshualitt144c3c82015-11-30 12:30:13 -0800372 static const int kNumStackDashes = 128;
373 SkSTArray<kNumStackDashes, SkRect, true> rects;
374 SkSTArray<kNumStackDashes, DashDraw, true> draws;
joshualitt4f569be2015-02-27 11:41:49 -0800375
376 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800377 int rectOffset = 0;
bsalomonb5238a72015-05-05 07:49:49 -0700378 rects.push_back_n(3 * instanceCount);
joshualitt4f569be2015-02-27 11:41:49 -0800379 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800380 const Geometry& args = fGeoData[i];
381
382 DashDraw& draw = draws.push_back(args);
joshualitt4f569be2015-02-27 11:41:49 -0800383
384 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
385
386 // We always want to at least stroke out half a pixel on each side in device space
387 // so 0.5f / perpScale gives us this min in src space
388 SkScalar halfSrcStroke =
389 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
390
391 SkScalar strokeAdj;
392 if (!hasCap) {
393 strokeAdj = 0.f;
394 } else {
395 strokeAdj = halfSrcStroke;
396 }
397
398 SkScalar startAdj = 0;
399
joshualitt4f569be2015-02-27 11:41:49 -0800400 bool lineDone = false;
401
402 // Too simplify the algorithm, we always push back rects for start and end rect.
403 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800404 SkRect& bounds = rects[rectOffset++];
405 SkRect& startRect = rects[rectOffset++];
406 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800407
408 bool hasStartRect = false;
409 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
410 // draw it separately here and adjust our start point accordingly
411 if (useAA) {
joshualitt144c3c82015-11-30 12:30:13 -0800412 if (draw.fPhase > 0 && draw.fPhase < draw.fIntervals[0]) {
joshualitt4f569be2015-02-27 11:41:49 -0800413 SkPoint startPts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800414 startPts[0] = draw.fPtsRot[0];
joshualitt4f569be2015-02-27 11:41:49 -0800415 startPts[1].fY = startPts[0].fY;
joshualitt144c3c82015-11-30 12:30:13 -0800416 startPts[1].fX = SkMinScalar(startPts[0].fX + draw.fIntervals[0] - draw.fPhase,
417 draw.fPtsRot[1].fX);
joshualitt4f569be2015-02-27 11:41:49 -0800418 startRect.set(startPts, 2);
419 startRect.outset(strokeAdj, halfSrcStroke);
420
421 hasStartRect = true;
joshualitt144c3c82015-11-30 12:30:13 -0800422 startAdj = draw.fIntervals[0] + draw.fIntervals[1] - draw.fPhase;
joshualitt4f569be2015-02-27 11:41:49 -0800423 }
424 }
425
426 // adjustments for start and end of bounding rect so we only draw dash intervals
427 // contained in the original line segment.
joshualitt144c3c82015-11-30 12:30:13 -0800428 startAdj += calc_start_adjustment(draw.fIntervals, draw.fPhase);
joshualitt4f569be2015-02-27 11:41:49 -0800429 if (startAdj != 0) {
joshualitt144c3c82015-11-30 12:30:13 -0800430 draw.fPtsRot[0].fX += startAdj;
431 draw.fPhase = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800432 }
433 SkScalar endingInterval = 0;
joshualitt144c3c82015-11-30 12:30:13 -0800434 SkScalar endAdj = calc_end_adjustment(draw.fIntervals, draw.fPtsRot, draw.fPhase,
joshualitt4f569be2015-02-27 11:41:49 -0800435 &endingInterval);
joshualitt144c3c82015-11-30 12:30:13 -0800436 draw.fPtsRot[1].fX -= endAdj;
437 if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) {
joshualitt4f569be2015-02-27 11:41:49 -0800438 lineDone = true;
439 }
440
441 bool hasEndRect = false;
442 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
443 // draw it separately here and adjust our end point accordingly
444 if (useAA && !lineDone) {
445 // If we adjusted the end then we will not be drawing a partial dash at the end.
446 // If we didn't adjust the end point then we just need to make sure the ending
447 // dash isn't a full dash
joshualitt144c3c82015-11-30 12:30:13 -0800448 if (0 == endAdj && endingInterval != draw.fIntervals[0]) {
joshualitt4f569be2015-02-27 11:41:49 -0800449 SkPoint endPts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800450 endPts[1] = draw.fPtsRot[1];
joshualitt4f569be2015-02-27 11:41:49 -0800451 endPts[0].fY = endPts[1].fY;
452 endPts[0].fX = endPts[1].fX - endingInterval;
453
454 endRect.set(endPts, 2);
455 endRect.outset(strokeAdj, halfSrcStroke);
456
457 hasEndRect = true;
joshualitt144c3c82015-11-30 12:30:13 -0800458 endAdj = endingInterval + draw.fIntervals[1];
joshualitt4f569be2015-02-27 11:41:49 -0800459
joshualitt144c3c82015-11-30 12:30:13 -0800460 draw.fPtsRot[1].fX -= endAdj;
461 if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) {
joshualitt4f569be2015-02-27 11:41:49 -0800462 lineDone = true;
463 }
464 }
465 }
466
467 if (startAdj != 0) {
joshualitt144c3c82015-11-30 12:30:13 -0800468 draw.fPhase = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800469 }
470
471 // Change the dashing info from src space into device space
joshualitt144c3c82015-11-30 12:30:13 -0800472 SkScalar* devIntervals = draw.fIntervals;
473 devIntervals[0] = draw.fIntervals[0] * args.fParallelScale;
474 devIntervals[1] = draw.fIntervals[1] * args.fParallelScale;
475 SkScalar devPhase = draw.fPhase * args.fParallelScale;
joshualitt4f569be2015-02-27 11:41:49 -0800476 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
477
senorblancof3c2c462015-04-20 14:44:26 -0700478 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800479 strokeWidth = 1.f;
480 }
481
482 SkScalar halfDevStroke = strokeWidth * 0.5f;
483
484 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
485 // add cap to on interval and remove from off interval
486 devIntervals[0] += strokeWidth;
487 devIntervals[1] -= strokeWidth;
488 }
489 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
490
senorblancof3c2c462015-04-20 14:44:26 -0700491 // For EdgeAA, we bloat in X & Y for both square and round caps.
492 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
493 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
494 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
495 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800496
senorblancof3c2c462015-04-20 14:44:26 -0700497 SkScalar bloatX = devBloatX / args.fParallelScale;
498 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800499
500 if (devIntervals[1] <= 0.f && useAA) {
501 // Case when we end up drawing a solid AA rect
502 // Reset the start rect to draw this single solid rect
503 // but it requires to upload a new intervals uniform so we can mimic
504 // one giant dash
joshualitt144c3c82015-11-30 12:30:13 -0800505 draw.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
506 draw.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
507 startRect.set(draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800508 startRect.outset(strokeAdj, halfSrcStroke);
509 hasStartRect = true;
510 hasEndRect = false;
511 lineDone = true;
512
513 SkPoint devicePts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800514 args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800515 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
516 if (hasCap) {
517 lineLength += 2.f * halfDevStroke;
518 }
519 devIntervals[0] = lineLength;
520 }
521
522 totalRectCount += !lineDone ? 1 : 0;
523 totalRectCount += hasStartRect ? 1 : 0;
524 totalRectCount += hasEndRect ? 1 : 0;
525
526 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
527 // need to adjust this for round caps to correctly set the dashPos attrib on
528 // vertices
529 startOffset -= halfDevStroke;
530 }
531
joshualitt4f569be2015-02-27 11:41:49 -0800532 if (!lineDone) {
533 SkPoint devicePts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800534 args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800535 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
536 if (hasCap) {
537 draw.fLineLength += 2.f * halfDevStroke;
538 }
539
joshualitt144c3c82015-11-30 12:30:13 -0800540 bounds.set(draw.fPtsRot[0].fX, draw.fPtsRot[0].fY,
541 draw.fPtsRot[1].fX, draw.fPtsRot[1].fY);
joshualitt4f569be2015-02-27 11:41:49 -0800542 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
543 }
544
545 if (hasStartRect) {
546 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
547 startRect.outset(bloatX, bloatY);
548 }
549
550 if (hasEndRect) {
551 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
552 endRect.outset(bloatX, bloatY);
553 }
554
555 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700556 draw.fDevBloatX = devBloatX;
557 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800558 draw.fHalfDevStroke = halfDevStroke;
559 draw.fStrokeWidth = strokeWidth;
560 draw.fHasStartRect = hasStartRect;
561 draw.fLineDone = lineDone;
562 draw.fHasEndRect = hasEndRect;
563 }
564
bsalomonb5238a72015-05-05 07:49:49 -0700565 if (!totalRectCount) {
566 return;
567 }
bsalomon8415abe2015-05-04 11:41:41 -0700568
bsalomonb5238a72015-05-05 07:49:49 -0700569 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700570 void* vertices = helper.init(target, gp->getVertexStride(), totalRectCount);
bsalomonb5238a72015-05-05 07:49:49 -0700571 if (!vertices) {
joshualitt4b31de82015-03-05 14:33:41 -0800572 return;
573 }
574
joshualitt4f569be2015-02-27 11:41:49 -0800575 int curVIdx = 0;
576 int rectIndex = 0;
577 for (int i = 0; i < instanceCount; i++) {
joshualitt144c3c82015-11-30 12:30:13 -0800578 const Geometry& geom = fGeoData[i];
joshualitt4f569be2015-02-27 11:41:49 -0800579
580 if (!draws[i].fLineDone) {
581 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700582 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700583 draws[i].fStartOffset, draws[i].fDevBloatX,
584 draws[i].fDevBloatY, draws[i].fLineLength,
joshualitt144c3c82015-11-30 12:30:13 -0800585 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
586 draws[i].fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800587 capType, gp->getVertexStride());
588 } else {
589 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
590 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700591 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800592 }
593 curVIdx += 4;
594 }
595 rectIndex++;
596
597 if (draws[i].fHasStartRect) {
598 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700599 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700600 draws[i].fStartOffset, draws[i].fDevBloatX,
joshualitt144c3c82015-11-30 12:30:13 -0800601 draws[i].fDevBloatY, draws[i].fIntervals[0],
602 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
603 draws[i].fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800604 gp->getVertexStride());
605 } else {
606 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
607 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700608 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800609 }
joshualitt4f569be2015-02-27 11:41:49 -0800610 curVIdx += 4;
611 }
612 rectIndex++;
613
614 if (draws[i].fHasEndRect) {
615 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700616 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700617 draws[i].fStartOffset, draws[i].fDevBloatX,
joshualitt144c3c82015-11-30 12:30:13 -0800618 draws[i].fDevBloatY, draws[i].fIntervals[0],
619 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
620 draws[i].fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800621 gp->getVertexStride());
622 } else {
623 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
624 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700625 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800626 }
627 curVIdx += 4;
628 }
629 rectIndex++;
630 }
bsalomonb5238a72015-05-05 07:49:49 -0700631 SkASSERT(0 == (curVIdx % 4) && (curVIdx / 4) == totalRectCount);
bsalomon75398562015-08-17 12:55:38 -0700632 helper.recordDraw(target);
joshualitt4f569be2015-02-27 11:41:49 -0800633 }
634
bsalomoncb02b382015-08-12 11:14:50 -0700635 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700636 DashBatch* that = t->cast<DashBatch>();
637 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
638 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700639 return false;
640 }
641
senorblancof3c2c462015-04-20 14:44:26 -0700642 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800643 return false;
644 }
645
646 if (this->fullDash() != that->fullDash()) {
647 return false;
648 }
649
650 if (this->cap() != that->cap()) {
651 return false;
652 }
653
654 // TODO vertex color
655 if (this->color() != that->color()) {
656 return false;
657 }
658
659 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
660 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
661 return false;
662 }
663
664 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700665 this->joinBounds(that->bounds());
joshualitt4f569be2015-02-27 11:41:49 -0800666 return true;
667 }
668
669 GrColor color() const { return fBatch.fColor; }
670 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
671 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700672 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800673 bool fullDash() const { return fBatch.fFullDash; }
674 SkPaint::Cap cap() const { return fBatch.fCap; }
joshualittb8c241a2015-05-19 08:23:30 -0700675 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt4f569be2015-02-27 11:41:49 -0800676
677 struct BatchTracker {
678 GrColor fColor;
679 bool fUsesLocalCoords;
680 bool fColorIgnored;
681 bool fCoverageIgnored;
682 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700683 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800684 bool fFullDash;
685 };
686
687 static const int kVertsPerDash = 4;
688 static const int kIndicesPerDash = 6;
689
690 BatchTracker fBatch;
691 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700692
693 typedef GrVertexBatch INHERITED;
joshualitt4f569be2015-02-27 11:41:49 -0800694};
695
bsalomonabd30f52015-08-13 13:34:48 -0700696static GrDrawBatch* create_batch(GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
697 bool useAA, const GrStrokeInfo& strokeInfo, bool msaaRT) {
kkinnunen261694c2015-05-05 08:00:10 -0700698 const SkScalar* intervals = strokeInfo.getDashIntervals();
699 SkScalar phase = strokeInfo.getDashPhase();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000700
kkinnunend156d362015-05-18 22:23:54 -0700701 SkPaint::Cap cap = strokeInfo.getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000702
joshualitt4f569be2015-02-27 11:41:49 -0800703 DashBatch::Geometry geometry;
kkinnunend156d362015-05-18 22:23:54 -0700704 geometry.fSrcStrokeWidth = strokeInfo.getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000705
706 // the phase should be normalized to be [0, sum of all intervals)
kkinnunen261694c2015-05-05 08:00:10 -0700707 SkASSERT(phase >= 0 && phase < intervals[0] + intervals[1]);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000708
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000709 // 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 +0000710 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700711 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800712 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
713 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700714 SkDebugf("Failed to create invertible rotation matrix!\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700715 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000716 }
717 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800718 geometry.fSrcRotInv.reset();
719 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
720 }
721
722 // Scale corrections of intervals and stroke from view matrix
723 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
724 geometry.fPtsRot);
725
kkinnunen261694c2015-05-05 08:00:10 -0700726 SkScalar offInterval = intervals[1] * geometry.fParallelScale;
joshualitt4f569be2015-02-27 11:41:49 -0800727 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
728
729 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
730 // add cap to on interveal and remove from off interval
731 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000732 }
733
joshualittfa2008f2015-04-29 11:32:05 -0700734 DashAAMode aaMode = msaaRT ? kMSAA_DashAAMode :
735 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
senorblancof3c2c462015-04-20 14:44:26 -0700736
joshualitt4f569be2015-02-27 11:41:49 -0800737 // 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 -0700738 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000739
joshualitt4f569be2015-02-27 11:41:49 -0800740 geometry.fColor = color;
741 geometry.fViewMatrix = viewMatrix;
kkinnunen261694c2015-05-05 08:00:10 -0700742 geometry.fPhase = phase;
743 geometry.fIntervals[0] = intervals[0];
744 geometry.fIntervals[1] = intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000745
joshualittfa2008f2015-04-29 11:32:05 -0700746 return DashBatch::Create(geometry, cap, aaMode, fullDash);
747}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000748
bsalomoned0bcad2015-05-04 10:36:42 -0700749bool GrDashingEffect::DrawDashLine(GrDrawTarget* target,
joshualitt1c735482015-07-13 08:08:25 -0700750 const GrPipelineBuilder& pipelineBuilder, GrColor color,
joshualittfa2008f2015-04-29 11:32:05 -0700751 const SkMatrix& viewMatrix, const SkPoint pts[2],
752 bool useAA, const GrStrokeInfo& strokeInfo) {
bsalomonabd30f52015-08-13 13:34:48 -0700753 SkAutoTUnref<GrDrawBatch> batch(
joshualitt1c735482015-07-13 08:08:25 -0700754 create_batch(color, viewMatrix, pts, useAA, strokeInfo,
755 pipelineBuilder.getRenderTarget()->isUnifiedMultisampled()));
joshualittfa2008f2015-04-29 11:32:05 -0700756 if (!batch) {
757 return false;
758 }
759
760 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000761 return true;
762}
763
764//////////////////////////////////////////////////////////////////////////////
765
egdanielf767e792014-07-02 06:21:32 -0700766class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800767
egdanielf767e792014-07-02 06:21:32 -0700768/*
769 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
770 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
771 * Both of the previous two parameters are in device space. This effect also requires the setting of
772 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
773 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
774 * transform the line to be horizontal, with the start of line at the origin then shifted to the
775 * right by half the off interval. The line then goes in the positive x direction.
776 */
joshualitt249af152014-09-15 11:41:13 -0700777class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700778public:
779 typedef SkPathEffect::DashInfo DashInfo;
780
joshualitt2e3b3e32014-12-09 13:31:14 -0800781 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700782 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700783 const SkMatrix& localMatrix,
784 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700785
mtklein36352bf2015-03-25 18:17:31 -0700786 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700787
joshualitt71c92602015-01-14 08:12:47 -0800788 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800789
joshualitt5224ba72015-02-03 15:07:51 -0800790 const Attribute* inDashParams() const { return fInDashParams; }
791
792 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700793
senorblancof3c2c462015-04-20 14:44:26 -0700794 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700795
joshualitt88c23fc2015-05-13 14:18:07 -0700796 GrColor color() const { return fColor; }
797
joshualittb8c241a2015-05-19 08:23:30 -0700798 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
799
joshualitte3ababe2015-05-15 07:56:07 -0700800 const SkMatrix& localMatrix() const { return fLocalMatrix; }
801
joshualittb8c241a2015-05-19 08:23:30 -0700802 bool usesLocalCoords() const { return fUsesLocalCoords; }
803
egdaniel57d3b032015-11-13 11:57:27 -0800804 void getGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700805
egdaniel57d3b032015-11-13 11:57:27 -0800806 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700807
808private:
joshualittb8c241a2015-05-19 08:23:30 -0700809 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
810 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700811
joshualitt88c23fc2015-05-13 14:18:07 -0700812 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700813 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700814 bool fUsesLocalCoords;
senorblancof3c2c462015-04-20 14:44:26 -0700815 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800816 const Attribute* fInPosition;
817 const Attribute* fInDashParams;
818 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700819
joshualittb0a8a372014-09-23 09:50:21 -0700820 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700821
joshualitt249af152014-09-15 11:41:13 -0700822 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700823};
824
825//////////////////////////////////////////////////////////////////////////////
826
egdaniele659a582015-11-13 09:55:43 -0800827class GLDashingCircleEffect : public GrGLSLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700828public:
joshualitt465283c2015-09-11 08:19:35 -0700829 GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700830
mtklein36352bf2015-03-25 18:17:31 -0700831 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700832
joshualitt87f48d92014-12-04 10:41:40 -0800833 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700834 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800835 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700836
egdaniel018fb622015-10-28 07:26:40 -0700837 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override;
egdanielf767e792014-07-02 06:21:32 -0700838
joshualitte3ababe2015-05-15 07:56:07 -0700839 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700840 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700841 int index,
842 const SkTArray<const GrCoordTransform*, true>& transforms) override {
843 this->setTransformDataHelper<DashingCircleEffect>(primProc, pdman, index, transforms);
844 }
845
egdanielf767e792014-07-02 06:21:32 -0700846private:
joshualitt9b989322014-12-15 14:16:27 -0800847 UniformHandle fParamUniform;
848 UniformHandle fColorUniform;
849 GrColor fColor;
850 SkScalar fPrevRadius;
851 SkScalar fPrevCenterX;
852 SkScalar fPrevIntervalLength;
egdaniele659a582015-11-13 09:55:43 -0800853 typedef GrGLSLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700854};
855
joshualitt465283c2015-09-11 08:19:35 -0700856GLDashingCircleEffect::GLDashingCircleEffect() {
joshualitt9b989322014-12-15 14:16:27 -0800857 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700858 fPrevRadius = SK_ScalarMin;
859 fPrevCenterX = SK_ScalarMin;
860 fPrevIntervalLength = SK_ScalarMax;
861}
862
robertphillips46d36f02015-01-18 08:14:14 -0800863void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700864 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800865 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800866 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800867 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800868
joshualittabb52a12015-01-13 15:02:10 -0800869 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800870 varyingHandler->emitAttributes(dce);
joshualittabb52a12015-01-13 15:02:10 -0800871
joshualitt5224ba72015-02-03 15:07:51 -0800872 // XY are dashPos, Z is dashInterval
egdaniel8dcdedc2015-11-11 06:27:20 -0800873 GrGLSLVertToFrag dashParams(kVec3f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800874 varyingHandler->addVarying("DashParam", &dashParams);
egdaniel4ca2e602015-11-18 08:01:26 -0800875 vertBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
joshualitt5224ba72015-02-03 15:07:51 -0800876
senorblancof3c2c462015-04-20 14:44:26 -0700877 // x refers to circle radius - 0.5, y refers to cicle's center x coord
egdaniel8dcdedc2015-11-11 06:27:20 -0800878 GrGLSLVertToFrag circleParams(kVec2f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800879 varyingHandler->addVarying("CircleParams", &circleParams);
egdaniel4ca2e602015-11-18 08:01:26 -0800880 vertBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700881
egdaniel4ca2e602015-11-18 08:01:26 -0800882 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800883 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700884 if (!dce.colorIgnored()) {
egdaniel7ea439b2015-12-03 09:20:44 -0800885 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -0700886 }
joshualitt9b989322014-12-15 14:16:27 -0800887
joshualittabb52a12015-01-13 15:02:10 -0800888 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -0800889 this->setupPosition(vertBuilder, gpArgs, dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800890
joshualittabb52a12015-01-13 15:02:10 -0800891 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800892 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800893 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800894 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800895 gpArgs->fPositionVar,
896 dce.inPosition()->fName,
897 dce.localMatrix(),
898 args.fTransformsIn,
899 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -0800900
egdanielf767e792014-07-02 06:21:32 -0700901 // transforms all points so that we can compare them to our test circle
egdaniel4ca2e602015-11-18 08:01:26 -0800902 fragBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
903 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
904 dashParams.fsIn());
905 fragBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
906 fragBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
907 fragBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700908 if (dce.aaMode() != kBW_DashAAMode) {
egdaniel4ca2e602015-11-18 08:01:26 -0800909 fragBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
910 fragBuilder->codeAppend("diff = 1.0 - diff;");
911 fragBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700912 } else {
egdaniel4ca2e602015-11-18 08:01:26 -0800913 fragBuilder->codeAppendf("float alpha = 1.0;");
914 fragBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700915 }
egdaniel4ca2e602015-11-18 08:01:26 -0800916 fragBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700917}
918
egdaniel018fb622015-10-28 07:26:40 -0700919void GLDashingCircleEffect::setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700920 const GrPrimitiveProcessor& processor) {
joshualittb8c241a2015-05-19 08:23:30 -0700921 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
922 if (dce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700923 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700924 GrColorToRGBAFloat(dce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800925 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700926 fColor = dce.color();
joshualitt9b989322014-12-15 14:16:27 -0800927 }
egdanielf767e792014-07-02 06:21:32 -0700928}
929
robertphillips46d36f02015-01-18 08:14:14 -0800930void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700931 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700932 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800933 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
934 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700935 key |= dce.usesLocalCoords() && dce.localMatrix().hasPerspective() ? 0x1 : 0x0;
936 key |= dce.colorIgnored() ? 0x2 : 0x0;
senorblancof3c2c462015-04-20 14:44:26 -0700937 key |= dce.aaMode() << 8;
joshualittb8c241a2015-05-19 08:23:30 -0700938 b->add32(key);
egdanielf767e792014-07-02 06:21:32 -0700939}
940
941//////////////////////////////////////////////////////////////////////////////
942
joshualitt2e3b3e32014-12-09 13:31:14 -0800943GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700944 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700945 const SkMatrix& localMatrix,
946 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -0700947 return new DashingCircleEffect(color, aaMode, localMatrix, usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700948}
949
egdaniel57d3b032015-11-13 11:57:27 -0800950void DashingCircleEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
951 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700952 GLDashingCircleEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800953}
954
egdaniel57d3b032015-11-13 11:57:27 -0800955GrGLSLPrimitiveProcessor* DashingCircleEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700956 return new GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700957}
958
joshualitt2e3b3e32014-12-09 13:31:14 -0800959DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700960 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700961 const SkMatrix& localMatrix,
962 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700963 : fColor(color)
964 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700965 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -0700966 , fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800967 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800968 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800969 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
970 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
971 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700972}
973
joshualittb0a8a372014-09-23 09:50:21 -0700974GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700975
bsalomonc21b09e2015-08-28 18:46:56 -0700976const GrGeometryProcessor* DashingCircleEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700977 DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
978 return DashingCircleEffect::Create(GrRandomColor(d->fRandom),
979 aaMode, GrTest::TestMatrix(d->fRandom),
980 d->fRandom->nextBool());
egdanielf767e792014-07-02 06:21:32 -0700981}
982
983//////////////////////////////////////////////////////////////////////////////
984
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000985class GLDashingLineEffect;
986
egdanielf767e792014-07-02 06:21:32 -0700987/*
988 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
989 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
990 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
991 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
992 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
993 * line at the origin then shifted to the right by half the off interval. The line then goes in the
994 * positive x direction.
995 */
joshualitt249af152014-09-15 11:41:13 -0700996class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000997public:
998 typedef SkPathEffect::DashInfo DashInfo;
999
joshualitt2e3b3e32014-12-09 13:31:14 -08001000 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -07001001 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001002 const SkMatrix& localMatrix,
1003 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001004
mtklein36352bf2015-03-25 18:17:31 -07001005 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001006
joshualitt71c92602015-01-14 08:12:47 -08001007 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001008
joshualitt5224ba72015-02-03 15:07:51 -08001009 const Attribute* inDashParams() const { return fInDashParams; }
1010
1011 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001012
senorblancof3c2c462015-04-20 14:44:26 -07001013 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001014
joshualitt88c23fc2015-05-13 14:18:07 -07001015 GrColor color() const { return fColor; }
1016
joshualittb8c241a2015-05-19 08:23:30 -07001017 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
1018
joshualitte3ababe2015-05-15 07:56:07 -07001019 const SkMatrix& localMatrix() const { return fLocalMatrix; }
1020
joshualittb8c241a2015-05-19 08:23:30 -07001021 bool usesLocalCoords() const { return fUsesLocalCoords; }
1022
egdaniel57d3b032015-11-13 11:57:27 -08001023 void getGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001024
egdaniel57d3b032015-11-13 11:57:27 -08001025 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrGLSLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001026
1027private:
joshualittb8c241a2015-05-19 08:23:30 -07001028 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
1029 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001030
joshualitt88c23fc2015-05-13 14:18:07 -07001031 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -07001032 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -07001033 bool fUsesLocalCoords;
senorblancof3c2c462015-04-20 14:44:26 -07001034 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001035 const Attribute* fInPosition;
1036 const Attribute* fInDashParams;
1037 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001038
joshualittb0a8a372014-09-23 09:50:21 -07001039 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001040
joshualitt249af152014-09-15 11:41:13 -07001041 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001042};
1043
1044//////////////////////////////////////////////////////////////////////////////
1045
egdaniele659a582015-11-13 09:55:43 -08001046class GLDashingLineEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001047public:
joshualitt465283c2015-09-11 08:19:35 -07001048 GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001049
mtklein36352bf2015-03-25 18:17:31 -07001050 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001051
joshualitt87f48d92014-12-04 10:41:40 -08001052 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -07001053 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -08001054 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001055
egdaniel018fb622015-10-28 07:26:40 -07001056 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001057
joshualitte3ababe2015-05-15 07:56:07 -07001058 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -07001059 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -07001060 int index,
1061 const SkTArray<const GrCoordTransform*, true>& transforms) override {
1062 this->setTransformDataHelper<DashingLineEffect>(primProc, pdman, index, transforms);
1063 }
1064
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001065private:
joshualitt9b989322014-12-15 14:16:27 -08001066 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001067 UniformHandle fColorUniform;
egdaniele659a582015-11-13 09:55:43 -08001068 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001069};
1070
joshualitt465283c2015-09-11 08:19:35 -07001071GLDashingLineEffect::GLDashingLineEffect() {
joshualitt9b989322014-12-15 14:16:27 -08001072 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001073}
1074
robertphillips46d36f02015-01-18 08:14:14 -08001075void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001076 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -08001077
egdaniel4ca2e602015-11-18 08:01:26 -08001078 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -08001079 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -08001080 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -08001081
joshualittabb52a12015-01-13 15:02:10 -08001082 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -08001083 varyingHandler->emitAttributes(de);
joshualittabb52a12015-01-13 15:02:10 -08001084
joshualitt5224ba72015-02-03 15:07:51 -08001085 // XY refers to dashPos, Z is the dash interval length
egdaniel8dcdedc2015-11-11 06:27:20 -08001086 GrGLSLVertToFrag inDashParams(kVec3f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -08001087 varyingHandler->addVarying("DashParams", &inDashParams, GrSLPrecision::kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -08001088 vertBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
joshualitt5224ba72015-02-03 15:07:51 -08001089
1090 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1091 // respectively.
egdaniel8dcdedc2015-11-11 06:27:20 -08001092 GrGLSLVertToFrag inRectParams(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -08001093 varyingHandler->addVarying("RectParams", &inRectParams, GrSLPrecision::kHigh_GrSLPrecision);
egdaniel4ca2e602015-11-18 08:01:26 -08001094 vertBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001095
egdaniel4ca2e602015-11-18 08:01:26 -08001096 GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -08001097 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -07001098 if (!de.colorIgnored()) {
egdaniel7ea439b2015-12-03 09:20:44 -08001099 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -07001100 }
1101
joshualittabb52a12015-01-13 15:02:10 -08001102 // Setup position
egdaniel7ea439b2015-12-03 09:20:44 -08001103 this->setupPosition(vertBuilder, gpArgs, de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -08001104
joshualittabb52a12015-01-13 15:02:10 -08001105 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -08001106 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -08001107 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -08001108 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -08001109 gpArgs->fPositionVar,
1110 de.inPosition()->fName,
1111 de.localMatrix(),
1112 args.fTransformsIn,
1113 args.fTransformsOut);
joshualittabb52a12015-01-13 15:02:10 -08001114
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001115 // transforms all points so that we can compare them to our test rect
egdaniel4ca2e602015-11-18 08:01:26 -08001116 fragBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1117 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1118 inDashParams.fsIn());
1119 fragBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001120 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001121 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1122 // numbers, xSub and ySub.
egdaniel4ca2e602015-11-18 08:01:26 -08001123 fragBuilder->codeAppend("float xSub, ySub;");
1124 fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1125 fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1126 fragBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1127 fragBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001128 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1129 // covered.
egdaniel4ca2e602015-11-18 08:01:26 -08001130 fragBuilder->codeAppendf(
1131 "float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001132 } else if (de.aaMode() == kMSAA_DashAAMode) {
1133 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1134 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
egdaniel4ca2e602015-11-18 08:01:26 -08001135 fragBuilder->codeAppend("float xSub;");
1136 fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1137 fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001138 // Now compute coverage in x to get the fraction of the pixel covered.
egdaniel4ca2e602015-11-18 08:01:26 -08001139 fragBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001140 } else {
1141 // Assuming the bounding geometry is tight so no need to check y values
egdaniel4ca2e602015-11-18 08:01:26 -08001142 fragBuilder->codeAppendf("float alpha = 1.0;");
1143 fragBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1144 inRectParams.fsIn());
1145 fragBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1146 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001147 }
egdaniel4ca2e602015-11-18 08:01:26 -08001148 fragBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001149}
1150
egdaniel018fb622015-10-28 07:26:40 -07001151void GLDashingLineEffect::setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -07001152 const GrPrimitiveProcessor& processor) {
joshualittb8c241a2015-05-19 08:23:30 -07001153 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
1154 if (de.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -07001155 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -07001156 GrColorToRGBAFloat(de.color(), c);
joshualitt9b989322014-12-15 14:16:27 -08001157 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -07001158 fColor = de.color();
joshualitt9b989322014-12-15 14:16:27 -08001159 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001160}
1161
robertphillips46d36f02015-01-18 08:14:14 -08001162void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -07001163 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001164 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -08001165 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1166 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -07001167 key |= de.usesLocalCoords() && de.localMatrix().hasPerspective() ? 0x1 : 0x0;
1168 key |= de.colorIgnored() ? 0x2 : 0x0;
senorblancof3c2c462015-04-20 14:44:26 -07001169 key |= de.aaMode() << 8;
joshualittb8c241a2015-05-19 08:23:30 -07001170 b->add32(key);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001171}
1172
1173//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001174
joshualitt2e3b3e32014-12-09 13:31:14 -08001175GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001176 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001177 const SkMatrix& localMatrix,
1178 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -07001179 return new DashingLineEffect(color, aaMode, localMatrix, usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001180}
1181
egdaniel57d3b032015-11-13 11:57:27 -08001182void DashingLineEffect::getGLSLProcessorKey(const GrGLSLCaps& caps,
1183 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -07001184 GLDashingLineEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -08001185}
1186
egdaniel57d3b032015-11-13 11:57:27 -08001187GrGLSLPrimitiveProcessor* DashingLineEffect::createGLSLInstance(const GrGLSLCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -07001188 return new GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001189}
1190
joshualitt2e3b3e32014-12-09 13:31:14 -08001191DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001192 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001193 const SkMatrix& localMatrix,
1194 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -07001195 : fColor(color)
1196 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -07001197 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -07001198 , fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001199 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001200 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001201 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1202 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001203}
1204
joshualittb0a8a372014-09-23 09:50:21 -07001205GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001206
bsalomonc21b09e2015-08-28 18:46:56 -07001207const GrGeometryProcessor* DashingLineEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -07001208 DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
1209 return DashingLineEffect::Create(GrRandomColor(d->fRandom),
1210 aaMode, GrTest::TestMatrix(d->fRandom),
1211 d->fRandom->nextBool());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001212}
1213
1214//////////////////////////////////////////////////////////////////////////////
1215
joshualitt5224ba72015-02-03 15:07:51 -08001216static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001217 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001218 DashCap cap,
joshualittdf0c5572015-08-03 11:35:28 -07001219 const SkMatrix& viewMatrix,
joshualittb8c241a2015-05-19 08:23:30 -07001220 bool usesLocalCoords) {
joshualittdf0c5572015-08-03 11:35:28 -07001221 SkMatrix invert;
1222 if (usesLocalCoords && !viewMatrix.invert(&invert)) {
1223 SkDebugf("Failed to invert\n");
halcanary96fcdcc2015-08-27 07:41:13 -07001224 return nullptr;
joshualittdf0c5572015-08-03 11:35:28 -07001225 }
1226
egdanielf767e792014-07-02 06:21:32 -07001227 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001228 case kRound_DashCap:
joshualittdf0c5572015-08-03 11:35:28 -07001229 return DashingCircleEffect::Create(color, dashAAMode, invert, usesLocalCoords);
joshualitt5224ba72015-02-03 15:07:51 -08001230 case kNonRound_DashCap:
joshualittdf0c5572015-08-03 11:35:28 -07001231 return DashingLineEffect::Create(color, dashAAMode, invert, usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -07001232 }
halcanary96fcdcc2015-08-27 07:41:13 -07001233 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001234}
joshualittfa2008f2015-04-29 11:32:05 -07001235
1236/////////////////////////////////////////////////////////////////////////////////////////////////
1237
1238#ifdef GR_TEST_UTILS
1239
bsalomonabd30f52015-08-13 13:34:48 -07001240DRAW_BATCH_TEST_DEFINE(DashBatch) {
joshualittfa2008f2015-04-29 11:32:05 -07001241 GrColor color = GrRandomColor(random);
1242 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1243 bool useAA = random->nextBool();
1244 bool msaaRT = random->nextBool();
1245
1246 // We can only dash either horizontal or vertical lines
1247 SkPoint pts[2];
1248 if (random->nextBool()) {
1249 // vertical
1250 pts[0].fX = 1.f;
1251 pts[0].fY = random->nextF() * 10.f;
1252 pts[1].fX = 1.f;
1253 pts[1].fY = random->nextF() * 10.f;
1254 } else {
1255 // horizontal
1256 pts[0].fX = random->nextF() * 10.f;
1257 pts[0].fY = 1.f;
1258 pts[1].fX = random->nextF() * 10.f;
1259 pts[1].fY = 1.f;
1260 }
1261
1262 // pick random cap
1263 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::Cap::kCapCount));
1264
1265 SkScalar intervals[2];
1266
1267 // We can only dash with the following intervals
1268 enum Intervals {
1269 kOpenOpen_Intervals ,
1270 kOpenClose_Intervals,
1271 kCloseOpen_Intervals,
1272 };
1273
1274 Intervals intervalType = SkPaint::kRound_Cap ?
1275 kOpenClose_Intervals :
1276 Intervals(random->nextULessThan(kCloseOpen_Intervals + 1));
1277 static const SkScalar kIntervalMin = 0.1f;
1278 static const SkScalar kIntervalMax = 10.f;
1279 switch (intervalType) {
1280 case kOpenOpen_Intervals:
1281 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1282 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1283 break;
1284 case kOpenClose_Intervals:
1285 intervals[0] = 0.f;
1286 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1287 break;
1288 case kCloseOpen_Intervals:
1289 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1290 intervals[1] = 0.f;
1291 break;
1292
1293 }
1294
1295 // phase is 0 < sum (i0, i1)
1296 SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]);
1297
1298 SkPaint p;
1299 p.setStyle(SkPaint::kStroke_Style);
1300 p.setStrokeWidth(SkIntToScalar(1));
1301 p.setStrokeCap(cap);
1302
1303 GrStrokeInfo strokeInfo(p);
1304
1305 SkPathEffect::DashInfo info;
1306 info.fIntervals = intervals;
1307 info.fCount = 2;
1308 info.fPhase = phase;
1309 SkDEBUGCODE(bool success = ) strokeInfo.setDashInfo(info);
1310 SkASSERT(success);
1311
1312 return create_batch(color, viewMatrix, pts, useAA, strokeInfo, msaaRT);
1313}
1314
1315#endif