blob: 3fa3014a9d5d88ee2915cf6de07ba059df9d1a9c [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"
joshualitt5478d422014-11-14 16:00:38 -080024#include "gl/GrGLGeometryProcessor.h"
wangyix6af0c932015-07-22 10:21:17 -070025#include "gl/GrGLFragmentProcessor.h"
egdaniel2d721d32015-11-11 13:06:05 -080026#include "glsl/GrGLSLFragmentShaderBuilder.h"
27#include "glsl/GrGLSLProgramBuilder.h"
egdaniel018fb622015-10-28 07:26:40 -070028#include "glsl/GrGLSLProgramDataManager.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
mtklein36352bf2015-03-25 18:17:31 -0700272 void getInvariantOutputColor(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800273 // When this is called on a batch, there is only one geometry bundle
274 out->setKnownFourComponents(fGeoData[0].fColor);
275 }
mtklein36352bf2015-03-25 18:17:31 -0700276 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const override {
joshualitt4f569be2015-02-27 11:41:49 -0800277 out->setUnknownSingleComponent();
278 }
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
bsalomon91d844d2015-08-10 10:47:29 -0700303 void initBatchTracker(const GrPipelineOptimizations& opt) override {
joshualitt4f569be2015-02-27 11:41:49 -0800304 // Handle any color overrides
bsalomon91d844d2015-08-10 10:47:29 -0700305 if (!opt.readsColor()) {
joshualitt4f569be2015-02-27 11:41:49 -0800306 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt4f569be2015-02-27 11:41:49 -0800307 }
bsalomon91d844d2015-08-10 10:47:29 -0700308 opt.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt4f569be2015-02-27 11:41:49 -0800309
310 // setup batch properties
bsalomon91d844d2015-08-10 10:47:29 -0700311 fBatch.fColorIgnored = !opt.readsColor();
joshualitt4f569be2015-02-27 11:41:49 -0800312 fBatch.fColor = fGeoData[0].fColor;
bsalomon91d844d2015-08-10 10:47:29 -0700313 fBatch.fUsesLocalCoords = opt.readsLocalCoords();
314 fBatch.fCoverageIgnored = !opt.readsCoverage();
joshualitt4f569be2015-02-27 11:41:49 -0800315 }
316
317 struct DashDraw {
318 SkScalar fStartOffset;
319 SkScalar fStrokeWidth;
320 SkScalar fLineLength;
321 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700322 SkScalar fDevBloatX;
323 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800324 bool fLineDone;
325 bool fHasStartRect;
326 bool fHasEndRect;
327 };
328
bsalomon75398562015-08-17 12:55:38 -0700329 void onPrepareDraws(Target* target) override {
joshualitt4f569be2015-02-27 11:41:49 -0800330 int instanceCount = fGeoData.count();
joshualitt4f569be2015-02-27 11:41:49 -0800331 SkPaint::Cap cap = this->cap();
joshualitte494a582015-08-03 09:32:36 -0700332 bool isRoundCap = SkPaint::kRound_Cap == cap;
333 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
joshualittdf0c5572015-08-03 11:35:28 -0700334
335 SkAutoTUnref<const GrGeometryProcessor> gp;
joshualitt4f569be2015-02-27 11:41:49 -0800336 if (this->fullDash()) {
joshualittdf0c5572015-08-03 11:35:28 -0700337 gp.reset(create_dash_gp(this->color(), this->aaMode(), capType, this->viewMatrix(),
joshualittb8c241a2015-05-19 08:23:30 -0700338 this->usesLocalCoords()));
joshualitt4f569be2015-02-27 11:41:49 -0800339 } else {
340 // Set up the vertex data for the line and start/end dashes
joshualittdf0c5572015-08-03 11:35:28 -0700341 using namespace GrDefaultGeoProcFactory;
342 Color color(this->color());
343 Coverage coverage(this->coverageIgnored() ? Coverage::kNone_Type :
344 Coverage::kSolid_Type);
345 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosition_Type :
346 LocalCoords::kUnused_Type);
347 gp.reset(CreateForDeviceSpace(color, coverage, localCoords, this->viewMatrix()));
348 }
349
350 if (!gp) {
351 SkDebugf("Could not create GrGeometryProcessor\n");
352 return;
joshualitt4f569be2015-02-27 11:41:49 -0800353 }
354
bsalomon75398562015-08-17 12:55:38 -0700355 target->initDraw(gp, this->pipeline());
joshualitt4f569be2015-02-27 11:41:49 -0800356
senorblancof3c2c462015-04-20 14:44:26 -0700357 // useAA here means Edge AA or MSAA
358 bool useAA = this->aaMode() != kBW_DashAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800359 bool fullDash = this->fullDash();
360
361 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
362 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
363 // iterate again over these decomposed dashes to generate vertices
364 SkSTArray<128, SkRect, true> rects;
365 SkSTArray<128, DashDraw, true> draws;
366
367 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800368 int rectOffset = 0;
bsalomonb5238a72015-05-05 07:49:49 -0700369 rects.push_back_n(3 * instanceCount);
joshualitt4f569be2015-02-27 11:41:49 -0800370 for (int i = 0; i < instanceCount; i++) {
371 Geometry& args = fGeoData[i];
372
373 bool hasCap = SkPaint::kButt_Cap != cap && 0 != args.fSrcStrokeWidth;
374
375 // We always want to at least stroke out half a pixel on each side in device space
376 // so 0.5f / perpScale gives us this min in src space
377 SkScalar halfSrcStroke =
378 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
379
380 SkScalar strokeAdj;
381 if (!hasCap) {
382 strokeAdj = 0.f;
383 } else {
384 strokeAdj = halfSrcStroke;
385 }
386
387 SkScalar startAdj = 0;
388
joshualitt4f569be2015-02-27 11:41:49 -0800389 bool lineDone = false;
390
391 // Too simplify the algorithm, we always push back rects for start and end rect.
392 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800393 SkRect& bounds = rects[rectOffset++];
394 SkRect& startRect = rects[rectOffset++];
395 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800396
397 bool hasStartRect = false;
398 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
399 // draw it separately here and adjust our start point accordingly
400 if (useAA) {
401 if (args.fPhase > 0 && args.fPhase < args.fIntervals[0]) {
402 SkPoint startPts[2];
403 startPts[0] = args.fPtsRot[0];
404 startPts[1].fY = startPts[0].fY;
405 startPts[1].fX = SkMinScalar(startPts[0].fX + args.fIntervals[0] - args.fPhase,
406 args.fPtsRot[1].fX);
407 startRect.set(startPts, 2);
408 startRect.outset(strokeAdj, halfSrcStroke);
409
410 hasStartRect = true;
411 startAdj = args.fIntervals[0] + args.fIntervals[1] - args.fPhase;
412 }
413 }
414
415 // adjustments for start and end of bounding rect so we only draw dash intervals
416 // contained in the original line segment.
417 startAdj += calc_start_adjustment(args.fIntervals, args.fPhase);
418 if (startAdj != 0) {
419 args.fPtsRot[0].fX += startAdj;
420 args.fPhase = 0;
421 }
422 SkScalar endingInterval = 0;
423 SkScalar endAdj = calc_end_adjustment(args.fIntervals, args.fPtsRot, args.fPhase,
424 &endingInterval);
425 args.fPtsRot[1].fX -= endAdj;
426 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
427 lineDone = true;
428 }
429
430 bool hasEndRect = false;
431 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
432 // draw it separately here and adjust our end point accordingly
433 if (useAA && !lineDone) {
434 // If we adjusted the end then we will not be drawing a partial dash at the end.
435 // If we didn't adjust the end point then we just need to make sure the ending
436 // dash isn't a full dash
437 if (0 == endAdj && endingInterval != args.fIntervals[0]) {
438 SkPoint endPts[2];
439 endPts[1] = args.fPtsRot[1];
440 endPts[0].fY = endPts[1].fY;
441 endPts[0].fX = endPts[1].fX - endingInterval;
442
443 endRect.set(endPts, 2);
444 endRect.outset(strokeAdj, halfSrcStroke);
445
446 hasEndRect = true;
447 endAdj = endingInterval + args.fIntervals[1];
448
449 args.fPtsRot[1].fX -= endAdj;
450 if (args.fPtsRot[0].fX >= args.fPtsRot[1].fX) {
451 lineDone = true;
452 }
453 }
454 }
455
456 if (startAdj != 0) {
457 args.fPhase = 0;
458 }
459
460 // Change the dashing info from src space into device space
461 SkScalar* devIntervals = args.fIntervals;
462 devIntervals[0] = args.fIntervals[0] * args.fParallelScale;
463 devIntervals[1] = args.fIntervals[1] * args.fParallelScale;
464 SkScalar devPhase = args.fPhase * args.fParallelScale;
465 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
466
senorblancof3c2c462015-04-20 14:44:26 -0700467 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800468 strokeWidth = 1.f;
469 }
470
471 SkScalar halfDevStroke = strokeWidth * 0.5f;
472
473 if (SkPaint::kSquare_Cap == cap && 0 != args.fSrcStrokeWidth) {
474 // add cap to on interval and remove from off interval
475 devIntervals[0] += strokeWidth;
476 devIntervals[1] -= strokeWidth;
477 }
478 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
479
senorblancof3c2c462015-04-20 14:44:26 -0700480 // For EdgeAA, we bloat in X & Y for both square and round caps.
481 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
482 SkScalar devBloatX = this->aaMode() == kEdgeAA_DashAAMode ? 0.5f : 0.0f;
483 SkScalar devBloatY = (SkPaint::kRound_Cap == cap && this->aaMode() == kMSAA_DashAAMode)
484 ? 0.5f : devBloatX;
joshualitt4f569be2015-02-27 11:41:49 -0800485
senorblancof3c2c462015-04-20 14:44:26 -0700486 SkScalar bloatX = devBloatX / args.fParallelScale;
487 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800488
489 if (devIntervals[1] <= 0.f && useAA) {
490 // Case when we end up drawing a solid AA rect
491 // Reset the start rect to draw this single solid rect
492 // but it requires to upload a new intervals uniform so we can mimic
493 // one giant dash
494 args.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
495 args.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
496 startRect.set(args.fPtsRot, 2);
497 startRect.outset(strokeAdj, halfSrcStroke);
498 hasStartRect = true;
499 hasEndRect = false;
500 lineDone = true;
501
502 SkPoint devicePts[2];
503 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
504 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
505 if (hasCap) {
506 lineLength += 2.f * halfDevStroke;
507 }
508 devIntervals[0] = lineLength;
509 }
510
511 totalRectCount += !lineDone ? 1 : 0;
512 totalRectCount += hasStartRect ? 1 : 0;
513 totalRectCount += hasEndRect ? 1 : 0;
514
515 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
516 // need to adjust this for round caps to correctly set the dashPos attrib on
517 // vertices
518 startOffset -= halfDevStroke;
519 }
520
521 DashDraw& draw = draws.push_back();
522 if (!lineDone) {
523 SkPoint devicePts[2];
524 args.fViewMatrix.mapPoints(devicePts, args.fPtsRot, 2);
525 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
526 if (hasCap) {
527 draw.fLineLength += 2.f * halfDevStroke;
528 }
529
530 bounds.set(args.fPtsRot[0].fX, args.fPtsRot[0].fY,
531 args.fPtsRot[1].fX, args.fPtsRot[1].fY);
532 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
533 }
534
535 if (hasStartRect) {
536 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
537 startRect.outset(bloatX, bloatY);
538 }
539
540 if (hasEndRect) {
541 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
542 endRect.outset(bloatX, bloatY);
543 }
544
545 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700546 draw.fDevBloatX = devBloatX;
547 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800548 draw.fHalfDevStroke = halfDevStroke;
549 draw.fStrokeWidth = strokeWidth;
550 draw.fHasStartRect = hasStartRect;
551 draw.fLineDone = lineDone;
552 draw.fHasEndRect = hasEndRect;
553 }
554
bsalomonb5238a72015-05-05 07:49:49 -0700555 if (!totalRectCount) {
556 return;
557 }
bsalomon8415abe2015-05-04 11:41:41 -0700558
bsalomonb5238a72015-05-05 07:49:49 -0700559 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700560 void* vertices = helper.init(target, gp->getVertexStride(), totalRectCount);
bsalomonb5238a72015-05-05 07:49:49 -0700561 if (!vertices) {
joshualitt4b31de82015-03-05 14:33:41 -0800562 return;
563 }
564
joshualitt4f569be2015-02-27 11:41:49 -0800565 int curVIdx = 0;
566 int rectIndex = 0;
567 for (int i = 0; i < instanceCount; i++) {
bsalomonb5238a72015-05-05 07:49:49 -0700568 Geometry& geom = fGeoData[i];
joshualitt4f569be2015-02-27 11:41:49 -0800569
570 if (!draws[i].fLineDone) {
571 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700572 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700573 draws[i].fStartOffset, draws[i].fDevBloatX,
574 draws[i].fDevBloatY, draws[i].fLineLength,
bsalomonb5238a72015-05-05 07:49:49 -0700575 draws[i].fHalfDevStroke, geom.fIntervals[0],
576 geom.fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800577 capType, gp->getVertexStride());
578 } else {
579 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
580 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700581 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800582 }
583 curVIdx += 4;
584 }
585 rectIndex++;
586
587 if (draws[i].fHasStartRect) {
588 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700589 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700590 draws[i].fStartOffset, draws[i].fDevBloatX,
bsalomonb5238a72015-05-05 07:49:49 -0700591 draws[i].fDevBloatY, geom.fIntervals[0],
592 draws[i].fHalfDevStroke, geom.fIntervals[0],
593 geom.fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800594 gp->getVertexStride());
595 } else {
596 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
597 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700598 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800599 }
joshualitt4f569be2015-02-27 11:41:49 -0800600 curVIdx += 4;
601 }
602 rectIndex++;
603
604 if (draws[i].fHasEndRect) {
605 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700606 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700607 draws[i].fStartOffset, draws[i].fDevBloatX,
bsalomonb5238a72015-05-05 07:49:49 -0700608 draws[i].fDevBloatY, geom.fIntervals[0],
609 draws[i].fHalfDevStroke, geom.fIntervals[0],
610 geom.fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800611 gp->getVertexStride());
612 } else {
613 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
614 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700615 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800616 }
617 curVIdx += 4;
618 }
619 rectIndex++;
620 }
bsalomonb5238a72015-05-05 07:49:49 -0700621 SkASSERT(0 == (curVIdx % 4) && (curVIdx / 4) == totalRectCount);
bsalomon75398562015-08-17 12:55:38 -0700622 helper.recordDraw(target);
joshualitt4f569be2015-02-27 11:41:49 -0800623 }
624
bsalomoncb02b382015-08-12 11:14:50 -0700625 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700626 DashBatch* that = t->cast<DashBatch>();
627 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
628 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700629 return false;
630 }
631
senorblancof3c2c462015-04-20 14:44:26 -0700632 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800633 return false;
634 }
635
636 if (this->fullDash() != that->fullDash()) {
637 return false;
638 }
639
640 if (this->cap() != that->cap()) {
641 return false;
642 }
643
644 // TODO vertex color
645 if (this->color() != that->color()) {
646 return false;
647 }
648
649 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
650 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
651 return false;
652 }
653
654 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700655 this->joinBounds(that->bounds());
joshualitt4f569be2015-02-27 11:41:49 -0800656 return true;
657 }
658
659 GrColor color() const { return fBatch.fColor; }
660 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
661 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
senorblancof3c2c462015-04-20 14:44:26 -0700662 DashAAMode aaMode() const { return fBatch.fAAMode; }
joshualitt4f569be2015-02-27 11:41:49 -0800663 bool fullDash() const { return fBatch.fFullDash; }
664 SkPaint::Cap cap() const { return fBatch.fCap; }
joshualittb8c241a2015-05-19 08:23:30 -0700665 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt4f569be2015-02-27 11:41:49 -0800666
667 struct BatchTracker {
668 GrColor fColor;
669 bool fUsesLocalCoords;
670 bool fColorIgnored;
671 bool fCoverageIgnored;
672 SkPaint::Cap fCap;
senorblancof3c2c462015-04-20 14:44:26 -0700673 DashAAMode fAAMode;
joshualitt4f569be2015-02-27 11:41:49 -0800674 bool fFullDash;
675 };
676
677 static const int kVertsPerDash = 4;
678 static const int kIndicesPerDash = 6;
679
680 BatchTracker fBatch;
681 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700682
683 typedef GrVertexBatch INHERITED;
joshualitt4f569be2015-02-27 11:41:49 -0800684};
685
bsalomonabd30f52015-08-13 13:34:48 -0700686static GrDrawBatch* create_batch(GrColor color, const SkMatrix& viewMatrix, const SkPoint pts[2],
687 bool useAA, const GrStrokeInfo& strokeInfo, bool msaaRT) {
kkinnunen261694c2015-05-05 08:00:10 -0700688 const SkScalar* intervals = strokeInfo.getDashIntervals();
689 SkScalar phase = strokeInfo.getDashPhase();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000690
kkinnunend156d362015-05-18 22:23:54 -0700691 SkPaint::Cap cap = strokeInfo.getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000692
joshualitt4f569be2015-02-27 11:41:49 -0800693 DashBatch::Geometry geometry;
kkinnunend156d362015-05-18 22:23:54 -0700694 geometry.fSrcStrokeWidth = strokeInfo.getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000695
696 // the phase should be normalized to be [0, sum of all intervals)
kkinnunen261694c2015-05-05 08:00:10 -0700697 SkASSERT(phase >= 0 && phase < intervals[0] + intervals[1]);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000698
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000699 // 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 +0000700 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700701 SkMatrix rotMatrix;
joshualitt4f569be2015-02-27 11:41:49 -0800702 align_to_x_axis(pts, &rotMatrix, geometry.fPtsRot);
703 if(!rotMatrix.invert(&geometry.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700704 SkDebugf("Failed to create invertible rotation matrix!\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700705 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000706 }
707 } else {
joshualitt4f569be2015-02-27 11:41:49 -0800708 geometry.fSrcRotInv.reset();
709 memcpy(geometry.fPtsRot, pts, 2 * sizeof(SkPoint));
710 }
711
712 // Scale corrections of intervals and stroke from view matrix
713 calc_dash_scaling(&geometry.fParallelScale, &geometry.fPerpendicularScale, viewMatrix,
714 geometry.fPtsRot);
715
kkinnunen261694c2015-05-05 08:00:10 -0700716 SkScalar offInterval = intervals[1] * geometry.fParallelScale;
joshualitt4f569be2015-02-27 11:41:49 -0800717 SkScalar strokeWidth = geometry.fSrcStrokeWidth * geometry.fPerpendicularScale;
718
719 if (SkPaint::kSquare_Cap == cap && 0 != geometry.fSrcStrokeWidth) {
720 // add cap to on interveal and remove from off interval
721 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000722 }
723
joshualittfa2008f2015-04-29 11:32:05 -0700724 DashAAMode aaMode = msaaRT ? kMSAA_DashAAMode :
725 useAA ? kEdgeAA_DashAAMode : kBW_DashAAMode;
senorblancof3c2c462015-04-20 14:44:26 -0700726
joshualitt4f569be2015-02-27 11:41:49 -0800727 // 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 -0700728 bool fullDash = offInterval > 0.f || aaMode != kBW_DashAAMode;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000729
joshualitt4f569be2015-02-27 11:41:49 -0800730 geometry.fColor = color;
731 geometry.fViewMatrix = viewMatrix;
kkinnunen261694c2015-05-05 08:00:10 -0700732 geometry.fPhase = phase;
733 geometry.fIntervals[0] = intervals[0];
734 geometry.fIntervals[1] = intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000735
joshualittfa2008f2015-04-29 11:32:05 -0700736 return DashBatch::Create(geometry, cap, aaMode, fullDash);
737}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000738
bsalomoned0bcad2015-05-04 10:36:42 -0700739bool GrDashingEffect::DrawDashLine(GrDrawTarget* target,
joshualitt1c735482015-07-13 08:08:25 -0700740 const GrPipelineBuilder& pipelineBuilder, GrColor color,
joshualittfa2008f2015-04-29 11:32:05 -0700741 const SkMatrix& viewMatrix, const SkPoint pts[2],
742 bool useAA, const GrStrokeInfo& strokeInfo) {
bsalomonabd30f52015-08-13 13:34:48 -0700743 SkAutoTUnref<GrDrawBatch> batch(
joshualitt1c735482015-07-13 08:08:25 -0700744 create_batch(color, viewMatrix, pts, useAA, strokeInfo,
745 pipelineBuilder.getRenderTarget()->isUnifiedMultisampled()));
joshualittfa2008f2015-04-29 11:32:05 -0700746 if (!batch) {
747 return false;
748 }
749
750 target->drawBatch(pipelineBuilder, batch);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000751 return true;
752}
753
754//////////////////////////////////////////////////////////////////////////////
755
egdanielf767e792014-07-02 06:21:32 -0700756class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800757
egdanielf767e792014-07-02 06:21:32 -0700758/*
759 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
760 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
761 * Both of the previous two parameters are in device space. This effect also requires the setting of
762 * a vec2 vertex attribute for the the four corners of the bounding rect. This attribute is the
763 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
764 * transform the line to be horizontal, with the start of line at the origin then shifted to the
765 * right by half the off interval. The line then goes in the positive x direction.
766 */
joshualitt249af152014-09-15 11:41:13 -0700767class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700768public:
769 typedef SkPathEffect::DashInfo DashInfo;
770
joshualitt2e3b3e32014-12-09 13:31:14 -0800771 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700772 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700773 const SkMatrix& localMatrix,
774 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700775
mtklein36352bf2015-03-25 18:17:31 -0700776 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700777
joshualitt71c92602015-01-14 08:12:47 -0800778 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800779
joshualitt5224ba72015-02-03 15:07:51 -0800780 const Attribute* inDashParams() const { return fInDashParams; }
781
782 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700783
senorblancof3c2c462015-04-20 14:44:26 -0700784 DashAAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700785
joshualitt88c23fc2015-05-13 14:18:07 -0700786 GrColor color() const { return fColor; }
787
joshualittb8c241a2015-05-19 08:23:30 -0700788 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
789
joshualitte3ababe2015-05-15 07:56:07 -0700790 const SkMatrix& localMatrix() const { return fLocalMatrix; }
791
joshualittb8c241a2015-05-19 08:23:30 -0700792 bool usesLocalCoords() const { return fUsesLocalCoords; }
793
joshualitt465283c2015-09-11 08:19:35 -0700794 void getGLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700795
joshualitt465283c2015-09-11 08:19:35 -0700796 GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700797
798private:
joshualittb8c241a2015-05-19 08:23:30 -0700799 DashingCircleEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
800 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700801
joshualitt88c23fc2015-05-13 14:18:07 -0700802 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700803 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700804 bool fUsesLocalCoords;
senorblancof3c2c462015-04-20 14:44:26 -0700805 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800806 const Attribute* fInPosition;
807 const Attribute* fInDashParams;
808 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700809
joshualittb0a8a372014-09-23 09:50:21 -0700810 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
egdanielf767e792014-07-02 06:21:32 -0700811
joshualitt249af152014-09-15 11:41:13 -0700812 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700813};
814
815//////////////////////////////////////////////////////////////////////////////
816
joshualitt249af152014-09-15 11:41:13 -0700817class GLDashingCircleEffect : public GrGLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700818public:
joshualitt465283c2015-09-11 08:19:35 -0700819 GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700820
mtklein36352bf2015-03-25 18:17:31 -0700821 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700822
joshualitt87f48d92014-12-04 10:41:40 -0800823 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -0700824 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800825 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700826
egdaniel018fb622015-10-28 07:26:40 -0700827 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override;
egdanielf767e792014-07-02 06:21:32 -0700828
joshualitte3ababe2015-05-15 07:56:07 -0700829 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -0700830 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -0700831 int index,
832 const SkTArray<const GrCoordTransform*, true>& transforms) override {
833 this->setTransformDataHelper<DashingCircleEffect>(primProc, pdman, index, transforms);
834 }
835
egdanielf767e792014-07-02 06:21:32 -0700836private:
joshualitt9b989322014-12-15 14:16:27 -0800837 UniformHandle fParamUniform;
838 UniformHandle fColorUniform;
839 GrColor fColor;
840 SkScalar fPrevRadius;
841 SkScalar fPrevCenterX;
842 SkScalar fPrevIntervalLength;
joshualitt249af152014-09-15 11:41:13 -0700843 typedef GrGLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700844};
845
joshualitt465283c2015-09-11 08:19:35 -0700846GLDashingCircleEffect::GLDashingCircleEffect() {
joshualitt9b989322014-12-15 14:16:27 -0800847 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700848 fPrevRadius = SK_ScalarMin;
849 fPrevCenterX = SK_ScalarMin;
850 fPrevIntervalLength = SK_ScalarMax;
851}
852
robertphillips46d36f02015-01-18 08:14:14 -0800853void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700854 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdaniel8dcdedc2015-11-11 06:27:20 -0800855 GrGLSLGPBuilder* pb = args.fPB;
egdaniel2d721d32015-11-11 13:06:05 -0800856 GrGLSLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -0800857
joshualittabb52a12015-01-13 15:02:10 -0800858 // emit attributes
859 vsBuilder->emitAttributes(dce);
860
joshualitt5224ba72015-02-03 15:07:51 -0800861 // XY are dashPos, Z is dashInterval
egdaniel8dcdedc2015-11-11 06:27:20 -0800862 GrGLSLVertToFrag dashParams(kVec3f_GrSLType);
joshualitt5224ba72015-02-03 15:07:51 -0800863 args.fPB->addVarying("DashParam", &dashParams);
864 vsBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->fName);
865
senorblancof3c2c462015-04-20 14:44:26 -0700866 // x refers to circle radius - 0.5, y refers to cicle's center x coord
egdaniel8dcdedc2015-11-11 06:27:20 -0800867 GrGLSLVertToFrag circleParams(kVec2f_GrSLType);
joshualitt5224ba72015-02-03 15:07:51 -0800868 args.fPB->addVarying("CircleParams", &circleParams);
869 vsBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->fName);
joshualitt30ba4362014-08-21 20:18:45 -0700870
joshualitt9b989322014-12-15 14:16:27 -0800871 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -0700872 if (!dce.colorIgnored()) {
873 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
874 }
joshualitt9b989322014-12-15 14:16:27 -0800875
joshualittabb52a12015-01-13 15:02:10 -0800876 // Setup position
joshualitte578a952015-05-14 10:09:13 -0700877 this->setupPosition(pb, gpArgs, dce.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -0800878
joshualittabb52a12015-01-13 15:02:10 -0800879 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -0800880 this->emitTransforms(args.fPB, gpArgs->fPositionVar, dce.inPosition()->fName, dce.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -0800881 args.fTransformsIn, args.fTransformsOut);
882
egdanielf767e792014-07-02 06:21:32 -0700883 // transforms all points so that we can compare them to our test circle
egdaniel2d721d32015-11-11 13:06:05 -0800884 GrGLSLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -0800885 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
886 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
887 dashParams.fsIn());
888 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", dashParams.fsIn());
889 fsBuilder->codeAppendf("vec2 center = vec2(%s.y, 0.0);", circleParams.fsIn());
890 fsBuilder->codeAppend("float dist = length(center - fragPosShifted);");
senorblancof3c2c462015-04-20 14:44:26 -0700891 if (dce.aaMode() != kBW_DashAAMode) {
joshualitt5224ba72015-02-03 15:07:51 -0800892 fsBuilder->codeAppendf("float diff = dist - %s.x;", circleParams.fsIn());
893 fsBuilder->codeAppend("diff = 1.0 - diff;");
894 fsBuilder->codeAppend("float alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700895 } else {
joshualitt5224ba72015-02-03 15:07:51 -0800896 fsBuilder->codeAppendf("float alpha = 1.0;");
897 fsBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700898 }
joshualitt2dd1ae02014-12-03 06:24:10 -0800899 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700900}
901
egdaniel018fb622015-10-28 07:26:40 -0700902void GLDashingCircleEffect::setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -0700903 const GrPrimitiveProcessor& processor) {
joshualittb8c241a2015-05-19 08:23:30 -0700904 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
905 if (dce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700906 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700907 GrColorToRGBAFloat(dce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800908 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700909 fColor = dce.color();
joshualitt9b989322014-12-15 14:16:27 -0800910 }
egdanielf767e792014-07-02 06:21:32 -0700911}
912
robertphillips46d36f02015-01-18 08:14:14 -0800913void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -0700914 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700915 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800916 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
917 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700918 key |= dce.usesLocalCoords() && dce.localMatrix().hasPerspective() ? 0x1 : 0x0;
919 key |= dce.colorIgnored() ? 0x2 : 0x0;
senorblancof3c2c462015-04-20 14:44:26 -0700920 key |= dce.aaMode() << 8;
joshualittb8c241a2015-05-19 08:23:30 -0700921 b->add32(key);
egdanielf767e792014-07-02 06:21:32 -0700922}
923
924//////////////////////////////////////////////////////////////////////////////
925
joshualitt2e3b3e32014-12-09 13:31:14 -0800926GrGeometryProcessor* DashingCircleEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700927 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700928 const SkMatrix& localMatrix,
929 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -0700930 return new DashingCircleEffect(color, aaMode, localMatrix, usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700931}
932
joshualitt465283c2015-09-11 08:19:35 -0700933void DashingCircleEffect::getGLProcessorKey(const GrGLSLCaps& caps,GrProcessorKeyBuilder* b) const {
934 GLDashingCircleEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800935}
936
joshualitt465283c2015-09-11 08:19:35 -0700937GrGLPrimitiveProcessor* DashingCircleEffect::createGLInstance(const GrGLSLCaps&) const {
938 return new GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700939}
940
joshualitt2e3b3e32014-12-09 13:31:14 -0800941DashingCircleEffect::DashingCircleEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -0700942 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700943 const SkMatrix& localMatrix,
944 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -0700945 : fColor(color)
946 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -0700947 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -0700948 , fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -0800949 this->initClassID<DashingCircleEffect>();
joshualitt71c92602015-01-14 08:12:47 -0800950 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -0800951 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
952 fInCircleParams = &this->addVertexAttrib(Attribute("inCircleParams",
953 kVec2f_GrVertexAttribType));
egdanielf767e792014-07-02 06:21:32 -0700954}
955
joshualittb0a8a372014-09-23 09:50:21 -0700956GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -0700957
bsalomonc21b09e2015-08-28 18:46:56 -0700958const GrGeometryProcessor* DashingCircleEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -0700959 DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
960 return DashingCircleEffect::Create(GrRandomColor(d->fRandom),
961 aaMode, GrTest::TestMatrix(d->fRandom),
962 d->fRandom->nextBool());
egdanielf767e792014-07-02 06:21:32 -0700963}
964
965//////////////////////////////////////////////////////////////////////////////
966
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000967class GLDashingLineEffect;
968
egdanielf767e792014-07-02 06:21:32 -0700969/*
970 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
971 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
972 * This effect also requires the setting of a vec2 vertex attribute for the the four corners of the
973 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
974 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
975 * line at the origin then shifted to the right by half the off interval. The line then goes in the
976 * positive x direction.
977 */
joshualitt249af152014-09-15 11:41:13 -0700978class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000979public:
980 typedef SkPathEffect::DashInfo DashInfo;
981
joshualitt2e3b3e32014-12-09 13:31:14 -0800982 static GrGeometryProcessor* Create(GrColor,
senorblancof3c2c462015-04-20 14:44:26 -0700983 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700984 const SkMatrix& localMatrix,
985 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000986
mtklein36352bf2015-03-25 18:17:31 -0700987 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000988
joshualitt71c92602015-01-14 08:12:47 -0800989 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800990
joshualitt5224ba72015-02-03 15:07:51 -0800991 const Attribute* inDashParams() const { return fInDashParams; }
992
993 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -0700994
senorblancof3c2c462015-04-20 14:44:26 -0700995 DashAAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000996
joshualitt88c23fc2015-05-13 14:18:07 -0700997 GrColor color() const { return fColor; }
998
joshualittb8c241a2015-05-19 08:23:30 -0700999 bool colorIgnored() const { return GrColor_ILLEGAL == fColor; }
1000
joshualitte3ababe2015-05-15 07:56:07 -07001001 const SkMatrix& localMatrix() const { return fLocalMatrix; }
1002
joshualittb8c241a2015-05-19 08:23:30 -07001003 bool usesLocalCoords() const { return fUsesLocalCoords; }
1004
joshualitt465283c2015-09-11 08:19:35 -07001005 void getGLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001006
joshualitt465283c2015-09-11 08:19:35 -07001007 GrGLPrimitiveProcessor* createGLInstance(const GrGLSLCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001008
1009private:
joshualittb8c241a2015-05-19 08:23:30 -07001010 DashingLineEffect(GrColor, DashAAMode aaMode, const SkMatrix& localMatrix,
1011 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001012
joshualitt88c23fc2015-05-13 14:18:07 -07001013 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -07001014 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -07001015 bool fUsesLocalCoords;
senorblancof3c2c462015-04-20 14:44:26 -07001016 DashAAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001017 const Attribute* fInPosition;
1018 const Attribute* fInDashParams;
1019 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001020
joshualittb0a8a372014-09-23 09:50:21 -07001021 GR_DECLARE_GEOMETRY_PROCESSOR_TEST;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001022
joshualitt249af152014-09-15 11:41:13 -07001023 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001024};
1025
1026//////////////////////////////////////////////////////////////////////////////
1027
joshualitt249af152014-09-15 11:41:13 -07001028class GLDashingLineEffect : public GrGLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001029public:
joshualitt465283c2015-09-11 08:19:35 -07001030 GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001031
mtklein36352bf2015-03-25 18:17:31 -07001032 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001033
joshualitt87f48d92014-12-04 10:41:40 -08001034 static inline void GenKey(const GrGeometryProcessor&,
jvanverthcfc18862015-04-28 08:48:20 -07001035 const GrGLSLCaps&,
joshualitt87f48d92014-12-04 10:41:40 -08001036 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001037
egdaniel018fb622015-10-28 07:26:40 -07001038 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001039
joshualitte3ababe2015-05-15 07:56:07 -07001040 void setTransformData(const GrPrimitiveProcessor& primProc,
egdaniel018fb622015-10-28 07:26:40 -07001041 const GrGLSLProgramDataManager& pdman,
joshualitte3ababe2015-05-15 07:56:07 -07001042 int index,
1043 const SkTArray<const GrCoordTransform*, true>& transforms) override {
1044 this->setTransformDataHelper<DashingLineEffect>(primProc, pdman, index, transforms);
1045 }
1046
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001047private:
joshualitt9b989322014-12-15 14:16:27 -08001048 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001049 UniformHandle fColorUniform;
joshualitt249af152014-09-15 11:41:13 -07001050 typedef GrGLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001051};
1052
joshualitt465283c2015-09-11 08:19:35 -07001053GLDashingLineEffect::GLDashingLineEffect() {
joshualitt9b989322014-12-15 14:16:27 -08001054 fColor = GrColor_ILLEGAL;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001055}
1056
robertphillips46d36f02015-01-18 08:14:14 -08001057void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001058 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
egdaniel8dcdedc2015-11-11 06:27:20 -08001059 GrGLSLGPBuilder* pb = args.fPB;
joshualitt2dd1ae02014-12-03 06:24:10 -08001060
egdaniel2d721d32015-11-11 13:06:05 -08001061 GrGLSLVertexBuilder* vsBuilder = args.fPB->getVertexShaderBuilder();
joshualitt2dd1ae02014-12-03 06:24:10 -08001062
joshualittabb52a12015-01-13 15:02:10 -08001063 // emit attributes
1064 vsBuilder->emitAttributes(de);
1065
joshualitt5224ba72015-02-03 15:07:51 -08001066 // XY refers to dashPos, Z is the dash interval length
egdaniel8dcdedc2015-11-11 06:27:20 -08001067 GrGLSLVertToFrag inDashParams(kVec3f_GrSLType);
senorblanco1422ec82015-06-11 11:51:29 -07001068 args.fPB->addVarying("DashParams", &inDashParams, GrSLPrecision::kHigh_GrSLPrecision);
joshualitt5224ba72015-02-03 15:07:51 -08001069 vsBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->fName);
1070
1071 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1072 // respectively.
egdaniel8dcdedc2015-11-11 06:27:20 -08001073 GrGLSLVertToFrag inRectParams(kVec4f_GrSLType);
senorblanco1422ec82015-06-11 11:51:29 -07001074 args.fPB->addVarying("RectParams", &inRectParams, GrSLPrecision::kHigh_GrSLPrecision);
joshualitt5224ba72015-02-03 15:07:51 -08001075 vsBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->fName);
joshualitt2dd1ae02014-12-03 06:24:10 -08001076
joshualitt9b989322014-12-15 14:16:27 -08001077 // Setup pass through color
joshualittb8c241a2015-05-19 08:23:30 -07001078 if (!de.colorIgnored()) {
1079 this->setupUniformColor(pb, args.fOutputColor, &fColorUniform);
1080 }
1081
joshualitt9b989322014-12-15 14:16:27 -08001082
joshualittabb52a12015-01-13 15:02:10 -08001083 // Setup position
joshualitte578a952015-05-14 10:09:13 -07001084 this->setupPosition(pb, gpArgs, de.inPosition()->fName);
joshualitt4973d9d2014-11-08 09:24:25 -08001085
joshualittabb52a12015-01-13 15:02:10 -08001086 // emit transforms
robertphillips46d36f02015-01-18 08:14:14 -08001087 this->emitTransforms(args.fPB, gpArgs->fPositionVar, de.inPosition()->fName, de.localMatrix(),
joshualittabb52a12015-01-13 15:02:10 -08001088 args.fTransformsIn, args.fTransformsOut);
1089
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001090 // transforms all points so that we can compare them to our test rect
egdaniel2d721d32015-11-11 13:06:05 -08001091 GrGLSLFragmentBuilder* fsBuilder = args.fPB->getFragmentShaderBuilder();
joshualitt5224ba72015-02-03 15:07:51 -08001092 fsBuilder->codeAppendf("float xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
1093 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1094 inDashParams.fsIn());
1095 fsBuilder->codeAppendf("vec2 fragPosShifted = vec2(xShifted, %s.y);", inDashParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001096 if (de.aaMode() == kEdgeAA_DashAAMode) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001097 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1098 // numbers, xSub and ySub.
joshualitt5224ba72015-02-03 15:07:51 -08001099 fsBuilder->codeAppend("float xSub, ySub;");
1100 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1101 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1102 fsBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1103 fsBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001104 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1105 // covered.
joshualitt5224ba72015-02-03 15:07:51 -08001106 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
senorblancof3c2c462015-04-20 14:44:26 -07001107 } else if (de.aaMode() == kMSAA_DashAAMode) {
1108 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1109 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
1110 fsBuilder->codeAppend("float xSub;");
1111 fsBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1112 fsBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1113 // Now compute coverage in x to get the fraction of the pixel covered.
1114 fsBuilder->codeAppendf("float alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001115 } else {
1116 // Assuming the bounding geometry is tight so no need to check y values
joshualitt5224ba72015-02-03 15:07:51 -08001117 fsBuilder->codeAppendf("float alpha = 1.0;");
1118 fsBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1119 inRectParams.fsIn());
1120 fsBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1121 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001122 }
joshualitt2dd1ae02014-12-03 06:24:10 -08001123 fsBuilder->codeAppendf("%s = vec4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001124}
1125
egdaniel018fb622015-10-28 07:26:40 -07001126void GLDashingLineEffect::setData(const GrGLSLProgramDataManager& pdman,
joshualitt465283c2015-09-11 08:19:35 -07001127 const GrPrimitiveProcessor& processor) {
joshualittb8c241a2015-05-19 08:23:30 -07001128 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
1129 if (de.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -07001130 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -07001131 GrColorToRGBAFloat(de.color(), c);
joshualitt9b989322014-12-15 14:16:27 -08001132 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -07001133 fColor = de.color();
joshualitt9b989322014-12-15 14:16:27 -08001134 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001135}
1136
robertphillips46d36f02015-01-18 08:14:14 -08001137void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
jvanverthcfc18862015-04-28 08:48:20 -07001138 const GrGLSLCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001139 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -08001140 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1141 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -07001142 key |= de.usesLocalCoords() && de.localMatrix().hasPerspective() ? 0x1 : 0x0;
1143 key |= de.colorIgnored() ? 0x2 : 0x0;
senorblancof3c2c462015-04-20 14:44:26 -07001144 key |= de.aaMode() << 8;
joshualittb8c241a2015-05-19 08:23:30 -07001145 b->add32(key);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001146}
1147
1148//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001149
joshualitt2e3b3e32014-12-09 13:31:14 -08001150GrGeometryProcessor* DashingLineEffect::Create(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001151 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001152 const SkMatrix& localMatrix,
1153 bool usesLocalCoords) {
halcanary385fe4d2015-08-26 13:07:48 -07001154 return new DashingLineEffect(color, aaMode, localMatrix, usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001155}
1156
joshualitt465283c2015-09-11 08:19:35 -07001157void DashingLineEffect::getGLProcessorKey(const GrGLSLCaps& caps,
joshualitteb2a6762014-12-04 11:35:33 -08001158 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -07001159 GLDashingLineEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -08001160}
1161
joshualitt465283c2015-09-11 08:19:35 -07001162GrGLPrimitiveProcessor* DashingLineEffect::createGLInstance(const GrGLSLCaps&) const {
1163 return new GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001164}
1165
joshualitt2e3b3e32014-12-09 13:31:14 -08001166DashingLineEffect::DashingLineEffect(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001167 DashAAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001168 const SkMatrix& localMatrix,
1169 bool usesLocalCoords)
joshualitte3ababe2015-05-15 07:56:07 -07001170 : fColor(color)
1171 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -07001172 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -07001173 , fAAMode(aaMode) {
joshualitteb2a6762014-12-04 11:35:33 -08001174 this->initClassID<DashingLineEffect>();
joshualitt71c92602015-01-14 08:12:47 -08001175 fInPosition = &this->addVertexAttrib(Attribute("inPosition", kVec2f_GrVertexAttribType));
joshualitt5224ba72015-02-03 15:07:51 -08001176 fInDashParams = &this->addVertexAttrib(Attribute("inDashParams", kVec3f_GrVertexAttribType));
1177 fInRectParams = &this->addVertexAttrib(Attribute("inRect", kVec4f_GrVertexAttribType));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001178}
1179
joshualittb0a8a372014-09-23 09:50:21 -07001180GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001181
bsalomonc21b09e2015-08-28 18:46:56 -07001182const GrGeometryProcessor* DashingLineEffect::TestCreate(GrProcessorTestData* d) {
joshualitt0067ff52015-07-08 14:26:19 -07001183 DashAAMode aaMode = static_cast<DashAAMode>(d->fRandom->nextULessThan(kDashAAModeCount));
1184 return DashingLineEffect::Create(GrRandomColor(d->fRandom),
1185 aaMode, GrTest::TestMatrix(d->fRandom),
1186 d->fRandom->nextBool());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001187}
1188
1189//////////////////////////////////////////////////////////////////////////////
1190
joshualitt5224ba72015-02-03 15:07:51 -08001191static GrGeometryProcessor* create_dash_gp(GrColor color,
senorblancof3c2c462015-04-20 14:44:26 -07001192 DashAAMode dashAAMode,
joshualitt5224ba72015-02-03 15:07:51 -08001193 DashCap cap,
joshualittdf0c5572015-08-03 11:35:28 -07001194 const SkMatrix& viewMatrix,
joshualittb8c241a2015-05-19 08:23:30 -07001195 bool usesLocalCoords) {
joshualittdf0c5572015-08-03 11:35:28 -07001196 SkMatrix invert;
1197 if (usesLocalCoords && !viewMatrix.invert(&invert)) {
1198 SkDebugf("Failed to invert\n");
halcanary96fcdcc2015-08-27 07:41:13 -07001199 return nullptr;
joshualittdf0c5572015-08-03 11:35:28 -07001200 }
1201
egdanielf767e792014-07-02 06:21:32 -07001202 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001203 case kRound_DashCap:
joshualittdf0c5572015-08-03 11:35:28 -07001204 return DashingCircleEffect::Create(color, dashAAMode, invert, usesLocalCoords);
joshualitt5224ba72015-02-03 15:07:51 -08001205 case kNonRound_DashCap:
joshualittdf0c5572015-08-03 11:35:28 -07001206 return DashingLineEffect::Create(color, dashAAMode, invert, usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -07001207 }
halcanary96fcdcc2015-08-27 07:41:13 -07001208 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001209}
joshualittfa2008f2015-04-29 11:32:05 -07001210
1211/////////////////////////////////////////////////////////////////////////////////////////////////
1212
1213#ifdef GR_TEST_UTILS
1214
bsalomonabd30f52015-08-13 13:34:48 -07001215DRAW_BATCH_TEST_DEFINE(DashBatch) {
joshualittfa2008f2015-04-29 11:32:05 -07001216 GrColor color = GrRandomColor(random);
1217 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
1218 bool useAA = random->nextBool();
1219 bool msaaRT = random->nextBool();
1220
1221 // We can only dash either horizontal or vertical lines
1222 SkPoint pts[2];
1223 if (random->nextBool()) {
1224 // vertical
1225 pts[0].fX = 1.f;
1226 pts[0].fY = random->nextF() * 10.f;
1227 pts[1].fX = 1.f;
1228 pts[1].fY = random->nextF() * 10.f;
1229 } else {
1230 // horizontal
1231 pts[0].fX = random->nextF() * 10.f;
1232 pts[0].fY = 1.f;
1233 pts[1].fX = random->nextF() * 10.f;
1234 pts[1].fY = 1.f;
1235 }
1236
1237 // pick random cap
1238 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::Cap::kCapCount));
1239
1240 SkScalar intervals[2];
1241
1242 // We can only dash with the following intervals
1243 enum Intervals {
1244 kOpenOpen_Intervals ,
1245 kOpenClose_Intervals,
1246 kCloseOpen_Intervals,
1247 };
1248
1249 Intervals intervalType = SkPaint::kRound_Cap ?
1250 kOpenClose_Intervals :
1251 Intervals(random->nextULessThan(kCloseOpen_Intervals + 1));
1252 static const SkScalar kIntervalMin = 0.1f;
1253 static const SkScalar kIntervalMax = 10.f;
1254 switch (intervalType) {
1255 case kOpenOpen_Intervals:
1256 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1257 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1258 break;
1259 case kOpenClose_Intervals:
1260 intervals[0] = 0.f;
1261 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1262 break;
1263 case kCloseOpen_Intervals:
1264 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1265 intervals[1] = 0.f;
1266 break;
1267
1268 }
1269
1270 // phase is 0 < sum (i0, i1)
1271 SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]);
1272
1273 SkPaint p;
1274 p.setStyle(SkPaint::kStroke_Style);
1275 p.setStrokeWidth(SkIntToScalar(1));
1276 p.setStrokeCap(cap);
1277
1278 GrStrokeInfo strokeInfo(p);
1279
1280 SkPathEffect::DashInfo info;
1281 info.fIntervals = intervals;
1282 info.fCount = 2;
1283 info.fPhase = phase;
1284 SkDEBUGCODE(bool success = ) strokeInfo.setDashInfo(info);
1285 SkASSERT(success);
1286
1287 return create_batch(color, viewMatrix, pts, useAA, strokeInfo, msaaRT);
1288}
1289
1290#endif