blob: ed2a847ad8e6d79c9a7691eb63dd7d0b304552da [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
Brian Salomona6aa5902016-12-16 09:32:00 -05008#include "GrDashOp.h"
Brian Salomon98222ac2017-07-12 15:27:54 -04009#include "GrAppliedClip.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070010#include "GrCaps.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000011#include "GrContext.h"
12#include "GrCoordTransform.h"
joshualitt5478d422014-11-14 16:00:38 -080013#include "GrDefaultGeoProcFactory.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050014#include "GrDrawOpTest.h"
Brian Salomondad29232016-12-01 16:40:24 -050015#include "GrGeometryProcessor.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050016#include "GrOpFlushState.h"
joshualittb0a8a372014-09-23 09:50:21 -070017#include "GrProcessor.h"
bsalomon6663acf2016-05-10 09:14:17 -070018#include "GrStyle.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000019#include "SkGr.h"
Brian Salomonfa3783f2018-01-05 13:49:07 -050020#include "SkMatrixPriv.h"
Cary Clarkdf429f32017-11-08 11:44:31 -050021#include "SkPointPriv.h"
egdaniel2d721d32015-11-11 13:06:05 -080022#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080023#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070024#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080025#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080026#include "glsl/GrGLSLVarying.h"
Chris Daltonc17bf322017-10-24 10:59:03 -060027#include "glsl/GrGLSLVertexGeoBuilder.h"
Brian Salomon89527432016-12-16 09:52:16 -050028#include "ops/GrMeshDrawOp.h"
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000029
Brian Salomona6aa5902016-12-16 09:32:00 -050030using AAMode = GrDashOp::AAMode;
bsalomonaf18fb42016-06-07 08:10:46 -070031
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000032///////////////////////////////////////////////////////////////////////////////
33
egdaniele61c4112014-06-12 10:24:21 -070034// Returns whether or not the gpu can fast path the dash line effect.
Brian Salomona6aa5902016-12-16 09:32:00 -050035bool GrDashOp::CanDrawDashLine(const SkPoint pts[2], const GrStyle& style,
36 const SkMatrix& viewMatrix) {
egdaniele61c4112014-06-12 10:24:21 -070037 // Pts must be either horizontal or vertical in src space
38 if (pts[0].fX != pts[1].fX && pts[0].fY != pts[1].fY) {
39 return false;
40 }
41
42 // May be able to relax this to include skew. As of now cannot do perspective
43 // because of the non uniform scaling of bloating a rect
44 if (!viewMatrix.preservesRightAngles()) {
45 return false;
46 }
47
bsalomon6663acf2016-05-10 09:14:17 -070048 if (!style.isDashed() || 2 != style.dashIntervalCnt()) {
egdaniele61c4112014-06-12 10:24:21 -070049 return false;
50 }
51
bsalomon6663acf2016-05-10 09:14:17 -070052 const SkScalar* intervals = style.dashIntervals();
kkinnunen261694c2015-05-05 08:00:10 -070053 if (0 == intervals[0] && 0 == intervals[1]) {
egdaniele61c4112014-06-12 10:24:21 -070054 return false;
55 }
56
bsalomon6663acf2016-05-10 09:14:17 -070057 SkPaint::Cap cap = style.strokeRec().getCap();
Greg Daniel5fb30562017-06-29 12:27:48 -040058 if (SkPaint::kRound_Cap == cap) {
59 // Current we don't support round caps unless the on interval is zero
60 if (intervals[0] != 0.f) {
61 return false;
62 }
63 // If the width of the circle caps in greater than the off interval we will pick up unwanted
64 // segments of circles at the start and end of the dash line.
65 if (style.strokeRec().getWidth() > intervals[1]) {
66 return false;
67 }
egdaniele61c4112014-06-12 10:24:21 -070068 }
69
70 return true;
71}
72
73namespace {
egdaniele61c4112014-06-12 10:24:21 -070074struct DashLineVertex {
75 SkPoint fPos;
76 SkPoint fDashPos;
joshualitt5224ba72015-02-03 15:07:51 -080077 SkScalar fIntervalLength;
78 SkRect fRect;
79};
80struct DashCircleVertex {
81 SkPoint fPos;
82 SkPoint fDashPos;
83 SkScalar fIntervalLength;
84 SkScalar fRadius;
85 SkScalar fCenterX;
egdaniele61c4112014-06-12 10:24:21 -070086};
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];
Greg Danielc96f9b52017-12-07 15:00:06 -050092 if (pts[1] == pts[0]) {
93 vecSrc.set(1.0, 0.0);
94 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000095 SkScalar magSrc = vecSrc.length();
96 SkScalar invSrc = magSrc ? SkScalarInvert(magSrc) : 0;
97 vecSrc.scale(invSrc);
98
99 SkVector vecSrcPerp;
Cary Clarkdf429f32017-11-08 11:44:31 -0500100 SkPointPriv::RotateCW(vecSrc, &vecSrcPerp);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000101 viewMatrix.mapVectors(&vecSrc, 1);
102 viewMatrix.mapVectors(&vecSrcPerp, 1);
103
104 // parallelScale tells how much to scale along the line parallel to the dash line
105 // perpScale tells how much to scale in the direction perpendicular to the dash line
106 *parallelScale = vecSrc.length();
107 *perpScale = vecSrcPerp.length();
108}
109
110// calculates the rotation needed to aligned pts to the x axis with pts[0] < pts[1]
111// Stores the rotation matrix in rotMatrix, and the mapped points in ptsRot
halcanary96fcdcc2015-08-27 07:41:13 -0700112static void align_to_x_axis(const SkPoint pts[2], SkMatrix* rotMatrix, SkPoint ptsRot[2] = nullptr) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000113 SkVector vec = pts[1] - pts[0];
Greg Danielc96f9b52017-12-07 15:00:06 -0500114 if (pts[1] == pts[0]) {
115 vec.set(1.0, 0.0);
116 }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000117 SkScalar mag = vec.length();
118 SkScalar inv = mag ? SkScalarInvert(mag) : 0;
119
120 vec.scale(inv);
121 rotMatrix->setSinCos(-vec.fY, vec.fX, pts[0].fX, pts[0].fY);
122 if (ptsRot) {
123 rotMatrix->mapPoints(ptsRot, pts, 2);
124 // correction for numerical issues if map doesn't make ptsRot exactly horizontal
125 ptsRot[1].fY = pts[0].fY;
126 }
127}
128
129// Assumes phase < sum of all intervals
joshualitt4f569be2015-02-27 11:41:49 -0800130static SkScalar calc_start_adjustment(const SkScalar intervals[2], SkScalar phase) {
131 SkASSERT(phase < intervals[0] + intervals[1]);
132 if (phase >= intervals[0] && phase != 0) {
133 SkScalar srcIntervalLen = intervals[0] + intervals[1];
134 return srcIntervalLen - phase;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000135 }
136 return 0;
137}
138
joshualitt4f569be2015-02-27 11:41:49 -0800139static SkScalar calc_end_adjustment(const SkScalar intervals[2], const SkPoint pts[2],
egdaniele61c4112014-06-12 10:24:21 -0700140 SkScalar phase, SkScalar* endingInt) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000141 if (pts[1].fX <= pts[0].fX) {
142 return 0;
143 }
joshualitt4f569be2015-02-27 11:41:49 -0800144 SkScalar srcIntervalLen = intervals[0] + intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000145 SkScalar totalLen = pts[1].fX - pts[0].fX;
reed80ea19c2015-05-12 10:37:34 -0700146 SkScalar temp = totalLen / srcIntervalLen;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000147 SkScalar numFullIntervals = SkScalarFloorToScalar(temp);
egdaniele61c4112014-06-12 10:24:21 -0700148 *endingInt = totalLen - numFullIntervals * srcIntervalLen + phase;
reed80ea19c2015-05-12 10:37:34 -0700149 temp = *endingInt / srcIntervalLen;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000150 *endingInt = *endingInt - SkScalarFloorToScalar(temp) * srcIntervalLen;
151 if (0 == *endingInt) {
152 *endingInt = srcIntervalLen;
153 }
joshualitt4f569be2015-02-27 11:41:49 -0800154 if (*endingInt > intervals[0]) {
joshualitt4f569be2015-02-27 11:41:49 -0800155 return *endingInt - intervals[0];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000156 }
157 return 0;
158}
159
joshualitt5224ba72015-02-03 15:07:51 -0800160enum DashCap {
161 kRound_DashCap,
162 kNonRound_DashCap,
163};
164
165static int kDashVertices = 4;
166
167template <typename T>
168void setup_dashed_rect_common(const SkRect& rect, const SkMatrix& matrix, T* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700169 SkScalar offset, SkScalar bloatX, SkScalar bloatY, SkScalar len,
170 SkScalar stroke) {
171 SkScalar startDashX = offset - bloatX;
172 SkScalar endDashX = offset + len + bloatX;
173 SkScalar startDashY = -stroke - bloatY;
174 SkScalar endDashY = stroke + bloatY;
joshualitt5224ba72015-02-03 15:07:51 -0800175 vertices[idx].fDashPos = SkPoint::Make(startDashX , startDashY);
176 vertices[idx + 1].fDashPos = SkPoint::Make(startDashX, endDashY);
Brian Salomon57caa662017-10-18 12:21:05 +0000177 vertices[idx + 2].fDashPos = SkPoint::Make(endDashX, startDashY);
178 vertices[idx + 3].fDashPos = SkPoint::Make(endDashX, endDashY);
joshualitt5224ba72015-02-03 15:07:51 -0800179
180 vertices[idx].fPos = SkPoint::Make(rect.fLeft, rect.fTop);
181 vertices[idx + 1].fPos = SkPoint::Make(rect.fLeft, rect.fBottom);
Brian Salomon57caa662017-10-18 12:21:05 +0000182 vertices[idx + 2].fPos = SkPoint::Make(rect.fRight, rect.fTop);
183 vertices[idx + 3].fPos = SkPoint::Make(rect.fRight, rect.fBottom);
joshualitt5224ba72015-02-03 15:07:51 -0800184
Brian Salomonfa3783f2018-01-05 13:49:07 -0500185 SkMatrixPriv::MapPointsWithStride(matrix, &vertices[idx].fPos, sizeof(T), 4);
joshualitt5224ba72015-02-03 15:07:51 -0800186}
187
188static void setup_dashed_rect(const SkRect& rect, void* vertices, int idx,
senorblancof3c2c462015-04-20 14:44:26 -0700189 const SkMatrix& matrix, SkScalar offset, SkScalar bloatX,
190 SkScalar bloatY, SkScalar len, SkScalar stroke,
191 SkScalar startInterval, SkScalar endInterval, SkScalar strokeWidth,
192 DashCap cap, const size_t vertexStride) {
joshualitt5224ba72015-02-03 15:07:51 -0800193 SkScalar intervalLength = startInterval + endInterval;
194
195 if (kRound_DashCap == cap) {
196 SkASSERT(vertexStride == sizeof(DashCircleVertex));
197 DashCircleVertex* verts = reinterpret_cast<DashCircleVertex*>(vertices);
198
senorblancof3c2c462015-04-20 14:44:26 -0700199 setup_dashed_rect_common<DashCircleVertex>(rect, matrix, verts, idx, offset, bloatX,
200 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800201
202 SkScalar radius = SkScalarHalf(strokeWidth) - 0.5f;
203 SkScalar centerX = SkScalarHalf(endInterval);
204
205 for (int i = 0; i < kDashVertices; i++) {
206 verts[idx + i].fIntervalLength = intervalLength;
207 verts[idx + i].fRadius = radius;
208 verts[idx + i].fCenterX = centerX;
209 }
joshualitt5224ba72015-02-03 15:07:51 -0800210 } else {
211 SkASSERT(kNonRound_DashCap == cap && vertexStride == sizeof(DashLineVertex));
212 DashLineVertex* verts = reinterpret_cast<DashLineVertex*>(vertices);
213
senorblancof3c2c462015-04-20 14:44:26 -0700214 setup_dashed_rect_common<DashLineVertex>(rect, matrix, verts, idx, offset, bloatX,
215 bloatY, len, stroke);
joshualitt5224ba72015-02-03 15:07:51 -0800216
217 SkScalar halfOffLen = SkScalarHalf(endInterval);
218 SkScalar halfStroke = SkScalarHalf(strokeWidth);
219 SkRect rectParam;
220 rectParam.set(halfOffLen + 0.5f, -halfStroke + 0.5f,
221 halfOffLen + startInterval - 0.5f, halfStroke - 0.5f);
222 for (int i = 0; i < kDashVertices; i++) {
223 verts[idx + i].fIntervalLength = intervalLength;
224 verts[idx + i].fRect = rectParam;
225 }
226 }
egdaniele61c4112014-06-12 10:24:21 -0700227}
228
joshualitt5478d422014-11-14 16:00:38 -0800229static void setup_dashed_rect_pos(const SkRect& rect, int idx, const SkMatrix& matrix,
230 SkPoint* verts) {
231 verts[idx] = SkPoint::Make(rect.fLeft, rect.fTop);
232 verts[idx + 1] = SkPoint::Make(rect.fLeft, rect.fBottom);
Brian Salomon57caa662017-10-18 12:21:05 +0000233 verts[idx + 2] = SkPoint::Make(rect.fRight, rect.fTop);
234 verts[idx + 3] = SkPoint::Make(rect.fRight, rect.fBottom);
joshualitt5478d422014-11-14 16:00:38 -0800235 matrix.mapPoints(&verts[idx], 4);
236}
egdaniele61c4112014-06-12 10:24:21 -0700237
joshualitt5224ba72015-02-03 15:07:51 -0800238
239/**
240 * An GrGeometryProcessor that renders a dashed line.
241 * This GrGeometryProcessor is meant for dashed lines that only have a single on/off interval pair.
242 * Bounding geometry is rendered and the effect computes coverage based on the fragment's
243 * position relative to the dashed line.
244 */
bungeman06ca8ec2016-06-09 08:01:03 -0700245static sk_sp<GrGeometryProcessor> make_dash_gp(GrColor,
246 AAMode aaMode,
247 DashCap cap,
248 const SkMatrix& localMatrix,
249 bool usesLocalCoords);
joshualitt5224ba72015-02-03 15:07:51 -0800250
Brian Salomon98222ac2017-07-12 15:27:54 -0400251class DashOp final : public GrMeshDrawOp {
joshualitt4f569be2015-02-27 11:41:49 -0800252public:
Brian Salomon25a88092016-12-01 09:36:50 -0500253 DEFINE_OP_CLASS_ID
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400254
Brian Salomona6aa5902016-12-16 09:32:00 -0500255 struct LineData {
joshualitt4f569be2015-02-27 11:41:49 -0800256 SkMatrix fViewMatrix;
257 SkMatrix fSrcRotInv;
258 SkPoint fPtsRot[2];
259 SkScalar fSrcStrokeWidth;
260 SkScalar fPhase;
261 SkScalar fIntervals[2];
262 SkScalar fParallelScale;
263 SkScalar fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800264 };
265
Brian Salomon98222ac2017-07-12 15:27:54 -0400266 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const LineData& geometry,
267 SkPaint::Cap cap, AAMode aaMode, bool fullDash,
268 const GrUserStencilSettings* stencilSettings) {
269 return std::unique_ptr<GrDrawOp>(
270 new DashOp(std::move(paint), geometry, cap, aaMode, fullDash, stencilSettings));
joshualitt4f569be2015-02-27 11:41:49 -0800271 }
272
Brian Salomona6aa5902016-12-16 09:32:00 -0500273 const char* name() const override { return "DashOp"; }
joshualitt4f569be2015-02-27 11:41:49 -0800274
Robert Phillipsf1748f52017-09-14 14:11:24 -0400275 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400276 fProcessorSet.visitProxies(func);
277 }
278
Brian Salomon7c3e7182016-12-01 09:35:30 -0500279 SkString dumpInfo() const override {
280 SkString string;
Brian Salomona6aa5902016-12-16 09:32:00 -0500281 for (const auto& geo : fLines) {
Brian Salomon7c3e7182016-12-01 09:35:30 -0500282 string.appendf("Pt0: [%.2f, %.2f], Pt1: [%.2f, %.2f], Width: %.2f, Ival0: %.2f, "
283 "Ival1 : %.2f, Phase: %.2f\n",
284 geo.fPtsRot[0].fX, geo.fPtsRot[0].fY,
285 geo.fPtsRot[1].fX, geo.fPtsRot[1].fY,
286 geo.fSrcStrokeWidth,
287 geo.fIntervals[0],
288 geo.fIntervals[1],
289 geo.fPhase);
290 }
Brian Salomon98222ac2017-07-12 15:27:54 -0400291 string += fProcessorSet.dumpProcessors();
292 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500293 return string;
294 }
295
Brian Salomon98222ac2017-07-12 15:27:54 -0400296 FixedFunctionFlags fixedFunctionFlags() const override {
297 FixedFunctionFlags flags = FixedFunctionFlags::kNone;
298 if (AAMode::kCoverageWithMSAA == fAAMode) {
299 flags |= FixedFunctionFlags::kUsesHWAA;
300 }
Brian Salomon89717fc2017-07-13 11:25:18 -0400301 if (fStencilSettings != &GrUserStencilSettings::kUnused) {
Brian Salomon98222ac2017-07-12 15:27:54 -0400302 flags |= FixedFunctionFlags::kUsesStencil;
303 }
304 return flags;
305 }
306
Brian Osman9a725dd2017-09-20 09:53:22 -0400307 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip,
308 GrPixelConfigIsClamped dstIsClamped) override {
Brian Salomon98222ac2017-07-12 15:27:54 -0400309 GrProcessorAnalysisCoverage coverage;
Chris Dalton69824002017-10-31 00:37:52 -0600310 if (AAMode::kNone == fAAMode && !clip->numClipCoverageFragmentProcessors()) {
Brian Salomon98222ac2017-07-12 15:27:54 -0400311 coverage = GrProcessorAnalysisCoverage::kNone;
312 } else {
313 coverage = GrProcessorAnalysisCoverage::kSingleChannel;
314 }
Brian Osman9a725dd2017-09-20 09:53:22 -0400315 auto analysis = fProcessorSet.finalize(fColor, coverage, clip, false, caps, dstIsClamped,
316 &fColor);
Brian Salomon98222ac2017-07-12 15:27:54 -0400317 fDisallowCombineOnTouchOrOverlap = analysis.requiresDstTexture() ||
318 (fProcessorSet.xferProcessor() &&
319 fProcessorSet.xferProcessor()->xferBarrierType(caps));
320 fUsesLocalCoords = analysis.usesLocalCoords();
321 return analysis.requiresDstTexture() ? RequiresDstTexture::kYes : RequiresDstTexture::kNo;
322 }
323
bsalomone46f9fe2015-08-18 06:05:14 -0700324private:
Brian Salomon98222ac2017-07-12 15:27:54 -0400325 DashOp(GrPaint&& paint, const LineData& geometry, SkPaint::Cap cap, AAMode aaMode,
326 bool fullDash, const GrUserStencilSettings* stencilSettings)
327 : INHERITED(ClassID())
328 , fColor(paint.getColor())
329 , fAllowsSRGBInputs(paint.getAllowSRGBInputs())
330 , fDisableSRGBOutputConversion(paint.getDisableOutputConversionToSRGB())
Brian Salomon98222ac2017-07-12 15:27:54 -0400331 , fFullDash(fullDash)
Brian Salomon89717fc2017-07-13 11:25:18 -0400332 , fCap(cap)
Brian Salomon98222ac2017-07-12 15:27:54 -0400333 , fAAMode(aaMode)
334 , fProcessorSet(std::move(paint))
335 , fStencilSettings(stencilSettings) {
Brian Salomona6aa5902016-12-16 09:32:00 -0500336 fLines.push_back(geometry);
bsalomone46f9fe2015-08-18 06:05:14 -0700337
338 // compute bounds
339 SkScalar halfStrokeWidth = 0.5f * geometry.fSrcStrokeWidth;
340 SkScalar xBloat = SkPaint::kButt_Cap == cap ? 0 : halfStrokeWidth;
bsalomon88cf17d2016-07-08 06:40:56 -0700341 SkRect bounds;
342 bounds.set(geometry.fPtsRot[0], geometry.fPtsRot[1]);
343 bounds.outset(xBloat, halfStrokeWidth);
bsalomone46f9fe2015-08-18 06:05:14 -0700344
345 // Note, we actually create the combined matrix here, and save the work
Brian Salomona6aa5902016-12-16 09:32:00 -0500346 SkMatrix& combinedMatrix = fLines[0].fSrcRotInv;
bsalomone46f9fe2015-08-18 06:05:14 -0700347 combinedMatrix.postConcat(geometry.fViewMatrix);
bsalomon88cf17d2016-07-08 06:40:56 -0700348
349 IsZeroArea zeroArea = geometry.fSrcStrokeWidth ? IsZeroArea::kNo : IsZeroArea::kYes;
350 HasAABloat aaBloat = (aaMode == AAMode::kNone) ? HasAABloat ::kNo : HasAABloat::kYes;
351 this->setTransformedBounds(bounds, combinedMatrix, aaBloat, zeroArea);
bsalomone46f9fe2015-08-18 06:05:14 -0700352 }
353
joshualitt4f569be2015-02-27 11:41:49 -0800354 struct DashDraw {
Brian Salomona6aa5902016-12-16 09:32:00 -0500355 DashDraw(const LineData& geo) {
joshualitt144c3c82015-11-30 12:30:13 -0800356 memcpy(fPtsRot, geo.fPtsRot, sizeof(geo.fPtsRot));
357 memcpy(fIntervals, geo.fIntervals, sizeof(geo.fIntervals));
358 fPhase = geo.fPhase;
359 }
360 SkPoint fPtsRot[2];
361 SkScalar fIntervals[2];
362 SkScalar fPhase;
joshualitt4f569be2015-02-27 11:41:49 -0800363 SkScalar fStartOffset;
364 SkScalar fStrokeWidth;
365 SkScalar fLineLength;
366 SkScalar fHalfDevStroke;
senorblancof3c2c462015-04-20 14:44:26 -0700367 SkScalar fDevBloatX;
368 SkScalar fDevBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800369 bool fLineDone;
370 bool fHasStartRect;
371 bool fHasEndRect;
372 };
373
Brian Salomon91326c32017-08-09 16:02:19 -0400374 void onPrepareDraws(Target* target) override {
Brian Salomona6aa5902016-12-16 09:32:00 -0500375 int instanceCount = fLines.count();
joshualitt4f569be2015-02-27 11:41:49 -0800376 SkPaint::Cap cap = this->cap();
joshualitte494a582015-08-03 09:32:36 -0700377 bool isRoundCap = SkPaint::kRound_Cap == cap;
378 DashCap capType = isRoundCap ? kRound_DashCap : kNonRound_DashCap;
joshualittdf0c5572015-08-03 11:35:28 -0700379
bungeman06ca8ec2016-06-09 08:01:03 -0700380 sk_sp<GrGeometryProcessor> gp;
joshualitt4f569be2015-02-27 11:41:49 -0800381 if (this->fullDash()) {
bungeman06ca8ec2016-06-09 08:01:03 -0700382 gp = make_dash_gp(this->color(), this->aaMode(), capType, this->viewMatrix(),
Brian Salomon98222ac2017-07-12 15:27:54 -0400383 fUsesLocalCoords);
joshualitt4f569be2015-02-27 11:41:49 -0800384 } else {
385 // Set up the vertex data for the line and start/end dashes
joshualittdf0c5572015-08-03 11:35:28 -0700386 using namespace GrDefaultGeoProcFactory;
387 Color color(this->color());
Brian Salomon98222ac2017-07-12 15:27:54 -0400388 LocalCoords::Type localCoordsType =
389 fUsesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Brian Salomon8c852be2017-01-04 10:44:42 -0500390 gp = MakeForDeviceSpace(color, Coverage::kSolid_Type, localCoordsType,
391 this->viewMatrix());
joshualittdf0c5572015-08-03 11:35:28 -0700392 }
393
394 if (!gp) {
395 SkDebugf("Could not create GrGeometryProcessor\n");
396 return;
joshualitt4f569be2015-02-27 11:41:49 -0800397 }
398
senorblancof3c2c462015-04-20 14:44:26 -0700399 // useAA here means Edge AA or MSAA
bsalomonaf18fb42016-06-07 08:10:46 -0700400 bool useAA = this->aaMode() != AAMode::kNone;
joshualitt4f569be2015-02-27 11:41:49 -0800401 bool fullDash = this->fullDash();
402
403 // We do two passes over all of the dashes. First we setup the start, end, and bounds,
404 // rectangles. We preserve all of this work in the rects / draws arrays below. Then we
405 // iterate again over these decomposed dashes to generate vertices
joshualitt144c3c82015-11-30 12:30:13 -0800406 static const int kNumStackDashes = 128;
407 SkSTArray<kNumStackDashes, SkRect, true> rects;
408 SkSTArray<kNumStackDashes, DashDraw, true> draws;
joshualitt4f569be2015-02-27 11:41:49 -0800409
410 int totalRectCount = 0;
joshualittd0f54572015-03-02 12:00:52 -0800411 int rectOffset = 0;
bsalomonb5238a72015-05-05 07:49:49 -0700412 rects.push_back_n(3 * instanceCount);
joshualitt4f569be2015-02-27 11:41:49 -0800413 for (int i = 0; i < instanceCount; i++) {
Brian Salomona6aa5902016-12-16 09:32:00 -0500414 const LineData& args = fLines[i];
joshualitt144c3c82015-11-30 12:30:13 -0800415
416 DashDraw& draw = draws.push_back(args);
joshualitt4f569be2015-02-27 11:41:49 -0800417
Greg Daniel95581bb2017-06-30 10:36:38 -0400418 bool hasCap = SkPaint::kButt_Cap != cap;
joshualitt4f569be2015-02-27 11:41:49 -0800419
420 // We always want to at least stroke out half a pixel on each side in device space
421 // so 0.5f / perpScale gives us this min in src space
422 SkScalar halfSrcStroke =
423 SkMaxScalar(args.fSrcStrokeWidth * 0.5f, 0.5f / args.fPerpendicularScale);
424
425 SkScalar strokeAdj;
426 if (!hasCap) {
427 strokeAdj = 0.f;
428 } else {
429 strokeAdj = halfSrcStroke;
430 }
431
432 SkScalar startAdj = 0;
433
joshualitt4f569be2015-02-27 11:41:49 -0800434 bool lineDone = false;
435
436 // Too simplify the algorithm, we always push back rects for start and end rect.
437 // Otherwise we'd have to track start / end rects for each individual geometry
joshualittd0f54572015-03-02 12:00:52 -0800438 SkRect& bounds = rects[rectOffset++];
439 SkRect& startRect = rects[rectOffset++];
440 SkRect& endRect = rects[rectOffset++];
joshualitt4f569be2015-02-27 11:41:49 -0800441
442 bool hasStartRect = false;
443 // If we are using AA, check to see if we are drawing a partial dash at the start. If so
444 // draw it separately here and adjust our start point accordingly
445 if (useAA) {
joshualitt144c3c82015-11-30 12:30:13 -0800446 if (draw.fPhase > 0 && draw.fPhase < draw.fIntervals[0]) {
joshualitt4f569be2015-02-27 11:41:49 -0800447 SkPoint startPts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800448 startPts[0] = draw.fPtsRot[0];
joshualitt4f569be2015-02-27 11:41:49 -0800449 startPts[1].fY = startPts[0].fY;
joshualitt144c3c82015-11-30 12:30:13 -0800450 startPts[1].fX = SkMinScalar(startPts[0].fX + draw.fIntervals[0] - draw.fPhase,
451 draw.fPtsRot[1].fX);
joshualitt4f569be2015-02-27 11:41:49 -0800452 startRect.set(startPts, 2);
453 startRect.outset(strokeAdj, halfSrcStroke);
454
455 hasStartRect = true;
joshualitt144c3c82015-11-30 12:30:13 -0800456 startAdj = draw.fIntervals[0] + draw.fIntervals[1] - draw.fPhase;
joshualitt4f569be2015-02-27 11:41:49 -0800457 }
458 }
459
460 // adjustments for start and end of bounding rect so we only draw dash intervals
461 // contained in the original line segment.
joshualitt144c3c82015-11-30 12:30:13 -0800462 startAdj += calc_start_adjustment(draw.fIntervals, draw.fPhase);
joshualitt4f569be2015-02-27 11:41:49 -0800463 if (startAdj != 0) {
joshualitt144c3c82015-11-30 12:30:13 -0800464 draw.fPtsRot[0].fX += startAdj;
465 draw.fPhase = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800466 }
467 SkScalar endingInterval = 0;
joshualitt144c3c82015-11-30 12:30:13 -0800468 SkScalar endAdj = calc_end_adjustment(draw.fIntervals, draw.fPtsRot, draw.fPhase,
joshualitt4f569be2015-02-27 11:41:49 -0800469 &endingInterval);
joshualitt144c3c82015-11-30 12:30:13 -0800470 draw.fPtsRot[1].fX -= endAdj;
471 if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) {
joshualitt4f569be2015-02-27 11:41:49 -0800472 lineDone = true;
473 }
474
475 bool hasEndRect = false;
476 // If we are using AA, check to see if we are drawing a partial dash at then end. If so
477 // draw it separately here and adjust our end point accordingly
478 if (useAA && !lineDone) {
479 // If we adjusted the end then we will not be drawing a partial dash at the end.
480 // If we didn't adjust the end point then we just need to make sure the ending
481 // dash isn't a full dash
joshualitt144c3c82015-11-30 12:30:13 -0800482 if (0 == endAdj && endingInterval != draw.fIntervals[0]) {
joshualitt4f569be2015-02-27 11:41:49 -0800483 SkPoint endPts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800484 endPts[1] = draw.fPtsRot[1];
joshualitt4f569be2015-02-27 11:41:49 -0800485 endPts[0].fY = endPts[1].fY;
486 endPts[0].fX = endPts[1].fX - endingInterval;
487
488 endRect.set(endPts, 2);
489 endRect.outset(strokeAdj, halfSrcStroke);
490
491 hasEndRect = true;
joshualitt144c3c82015-11-30 12:30:13 -0800492 endAdj = endingInterval + draw.fIntervals[1];
joshualitt4f569be2015-02-27 11:41:49 -0800493
joshualitt144c3c82015-11-30 12:30:13 -0800494 draw.fPtsRot[1].fX -= endAdj;
495 if (draw.fPtsRot[0].fX >= draw.fPtsRot[1].fX) {
joshualitt4f569be2015-02-27 11:41:49 -0800496 lineDone = true;
497 }
498 }
499 }
500
Greg Danielc96f9b52017-12-07 15:00:06 -0500501 if (draw.fPtsRot[0].fX == draw.fPtsRot[1].fX &&
502 (0 != endAdj || 0 == startAdj) &&
503 hasCap) {
504 // At this point the fPtsRot[0]/[1] represent the start and end of the inner rect of
505 // dashes that we want to draw. The only way they can be equal is if the on interval
506 // is zero (or an edge case if the end of line ends at a full off interval, but this
507 // is handled as well). Thus if the on interval is zero then we need to draw a cap
508 // at this position if the stroke has caps. The spec says we only draw this point if
509 // point lies between [start of line, end of line). Thus we check if we are at the
510 // end (but not the start), and if so we don't draw the cap.
511 lineDone = false;
512 }
513
joshualitt4f569be2015-02-27 11:41:49 -0800514 if (startAdj != 0) {
joshualitt144c3c82015-11-30 12:30:13 -0800515 draw.fPhase = 0;
joshualitt4f569be2015-02-27 11:41:49 -0800516 }
517
518 // Change the dashing info from src space into device space
joshualitt144c3c82015-11-30 12:30:13 -0800519 SkScalar* devIntervals = draw.fIntervals;
520 devIntervals[0] = draw.fIntervals[0] * args.fParallelScale;
521 devIntervals[1] = draw.fIntervals[1] * args.fParallelScale;
522 SkScalar devPhase = draw.fPhase * args.fParallelScale;
joshualitt4f569be2015-02-27 11:41:49 -0800523 SkScalar strokeWidth = args.fSrcStrokeWidth * args.fPerpendicularScale;
524
senorblancof3c2c462015-04-20 14:44:26 -0700525 if ((strokeWidth < 1.f && useAA) || 0.f == strokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800526 strokeWidth = 1.f;
527 }
528
529 SkScalar halfDevStroke = strokeWidth * 0.5f;
530
Greg Daniel95581bb2017-06-30 10:36:38 -0400531 if (SkPaint::kSquare_Cap == cap) {
joshualitt4f569be2015-02-27 11:41:49 -0800532 // add cap to on interval and remove from off interval
533 devIntervals[0] += strokeWidth;
534 devIntervals[1] -= strokeWidth;
535 }
536 SkScalar startOffset = devIntervals[1] * 0.5f + devPhase;
537
senorblancof3c2c462015-04-20 14:44:26 -0700538 // For EdgeAA, we bloat in X & Y for both square and round caps.
539 // For MSAA, we don't bloat at all for square caps, and bloat in Y only for round caps.
bsalomonaf18fb42016-06-07 08:10:46 -0700540 SkScalar devBloatX = this->aaMode() == AAMode::kCoverage ? 0.5f : 0.0f;
541 SkScalar devBloatY;
542 if (SkPaint::kRound_Cap == cap && this->aaMode() == AAMode::kCoverageWithMSAA) {
543 devBloatY = 0.5f;
544 } else {
545 devBloatY = devBloatX;
546 }
joshualitt4f569be2015-02-27 11:41:49 -0800547
senorblancof3c2c462015-04-20 14:44:26 -0700548 SkScalar bloatX = devBloatX / args.fParallelScale;
549 SkScalar bloatY = devBloatY / args.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800550
551 if (devIntervals[1] <= 0.f && useAA) {
552 // Case when we end up drawing a solid AA rect
553 // Reset the start rect to draw this single solid rect
554 // but it requires to upload a new intervals uniform so we can mimic
555 // one giant dash
joshualitt144c3c82015-11-30 12:30:13 -0800556 draw.fPtsRot[0].fX -= hasStartRect ? startAdj : 0;
557 draw.fPtsRot[1].fX += hasEndRect ? endAdj : 0;
558 startRect.set(draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800559 startRect.outset(strokeAdj, halfSrcStroke);
560 hasStartRect = true;
561 hasEndRect = false;
562 lineDone = true;
563
564 SkPoint devicePts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800565 args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800566 SkScalar lineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
567 if (hasCap) {
568 lineLength += 2.f * halfDevStroke;
569 }
570 devIntervals[0] = lineLength;
571 }
572
573 totalRectCount += !lineDone ? 1 : 0;
574 totalRectCount += hasStartRect ? 1 : 0;
575 totalRectCount += hasEndRect ? 1 : 0;
576
577 if (SkPaint::kRound_Cap == cap && 0 != args.fSrcStrokeWidth) {
578 // need to adjust this for round caps to correctly set the dashPos attrib on
579 // vertices
580 startOffset -= halfDevStroke;
581 }
582
joshualitt4f569be2015-02-27 11:41:49 -0800583 if (!lineDone) {
584 SkPoint devicePts[2];
joshualitt144c3c82015-11-30 12:30:13 -0800585 args.fViewMatrix.mapPoints(devicePts, draw.fPtsRot, 2);
joshualitt4f569be2015-02-27 11:41:49 -0800586 draw.fLineLength = SkPoint::Distance(devicePts[0], devicePts[1]);
587 if (hasCap) {
588 draw.fLineLength += 2.f * halfDevStroke;
589 }
590
joshualitt144c3c82015-11-30 12:30:13 -0800591 bounds.set(draw.fPtsRot[0].fX, draw.fPtsRot[0].fY,
592 draw.fPtsRot[1].fX, draw.fPtsRot[1].fY);
joshualitt4f569be2015-02-27 11:41:49 -0800593 bounds.outset(bloatX + strokeAdj, bloatY + halfSrcStroke);
594 }
595
596 if (hasStartRect) {
597 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
598 startRect.outset(bloatX, bloatY);
599 }
600
601 if (hasEndRect) {
602 SkASSERT(useAA); // so that we know bloatX and bloatY have been set
603 endRect.outset(bloatX, bloatY);
604 }
605
606 draw.fStartOffset = startOffset;
senorblancof3c2c462015-04-20 14:44:26 -0700607 draw.fDevBloatX = devBloatX;
608 draw.fDevBloatY = devBloatY;
joshualitt4f569be2015-02-27 11:41:49 -0800609 draw.fHalfDevStroke = halfDevStroke;
610 draw.fStrokeWidth = strokeWidth;
611 draw.fHasStartRect = hasStartRect;
612 draw.fLineDone = lineDone;
613 draw.fHasEndRect = hasEndRect;
614 }
615
bsalomonb5238a72015-05-05 07:49:49 -0700616 if (!totalRectCount) {
617 return;
618 }
bsalomon8415abe2015-05-04 11:41:41 -0700619
bsalomonb5238a72015-05-05 07:49:49 -0700620 QuadHelper helper;
bsalomon75398562015-08-17 12:55:38 -0700621 void* vertices = helper.init(target, gp->getVertexStride(), totalRectCount);
bsalomonb5238a72015-05-05 07:49:49 -0700622 if (!vertices) {
joshualitt4b31de82015-03-05 14:33:41 -0800623 return;
624 }
625
joshualitt4f569be2015-02-27 11:41:49 -0800626 int curVIdx = 0;
627 int rectIndex = 0;
628 for (int i = 0; i < instanceCount; i++) {
Brian Salomona6aa5902016-12-16 09:32:00 -0500629 const LineData& geom = fLines[i];
joshualitt4f569be2015-02-27 11:41:49 -0800630
631 if (!draws[i].fLineDone) {
632 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700633 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700634 draws[i].fStartOffset, draws[i].fDevBloatX,
635 draws[i].fDevBloatY, draws[i].fLineLength,
joshualitt144c3c82015-11-30 12:30:13 -0800636 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
637 draws[i].fIntervals[1], draws[i].fStrokeWidth,
joshualitt4f569be2015-02-27 11:41:49 -0800638 capType, gp->getVertexStride());
639 } else {
640 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
641 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700642 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800643 }
644 curVIdx += 4;
645 }
646 rectIndex++;
647
648 if (draws[i].fHasStartRect) {
649 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700650 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700651 draws[i].fStartOffset, draws[i].fDevBloatX,
joshualitt144c3c82015-11-30 12:30:13 -0800652 draws[i].fDevBloatY, draws[i].fIntervals[0],
653 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
654 draws[i].fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800655 gp->getVertexStride());
656 } else {
657 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
658 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700659 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800660 }
joshualitt4f569be2015-02-27 11:41:49 -0800661 curVIdx += 4;
662 }
663 rectIndex++;
664
665 if (draws[i].fHasEndRect) {
666 if (fullDash) {
bsalomonb5238a72015-05-05 07:49:49 -0700667 setup_dashed_rect(rects[rectIndex], vertices, curVIdx, geom.fSrcRotInv,
senorblancof3c2c462015-04-20 14:44:26 -0700668 draws[i].fStartOffset, draws[i].fDevBloatX,
joshualitt144c3c82015-11-30 12:30:13 -0800669 draws[i].fDevBloatY, draws[i].fIntervals[0],
670 draws[i].fHalfDevStroke, draws[i].fIntervals[0],
671 draws[i].fIntervals[1], draws[i].fStrokeWidth, capType,
joshualitt4f569be2015-02-27 11:41:49 -0800672 gp->getVertexStride());
673 } else {
674 SkPoint* verts = reinterpret_cast<SkPoint*>(vertices);
675 SkASSERT(gp->getVertexStride() == sizeof(SkPoint));
bsalomonb5238a72015-05-05 07:49:49 -0700676 setup_dashed_rect_pos(rects[rectIndex], curVIdx, geom.fSrcRotInv, verts);
joshualitt4f569be2015-02-27 11:41:49 -0800677 }
678 curVIdx += 4;
679 }
680 rectIndex++;
681 }
bsalomonb5238a72015-05-05 07:49:49 -0700682 SkASSERT(0 == (curVIdx % 4) && (curVIdx / 4) == totalRectCount);
Brian Salomon98222ac2017-07-12 15:27:54 -0400683 uint32_t pipelineFlags = 0;
684 if (AAMode::kCoverageWithMSAA == fAAMode) {
685 pipelineFlags |= GrPipeline::kHWAntialias_Flag;
686 }
687 if (fDisableSRGBOutputConversion) {
688 pipelineFlags |= GrPipeline::kDisableOutputConversionToSRGB_Flag;
689 }
690 if (fAllowsSRGBInputs) {
691 pipelineFlags |= GrPipeline::kAllowSRGBInputs_Flag;
692 }
Brian Salomonbfd18cd2017-08-09 16:27:09 -0400693 const GrPipeline* pipeline = target->makePipeline(pipelineFlags, std::move(fProcessorSet),
694 target->detachAppliedClip());
Brian Salomon98222ac2017-07-12 15:27:54 -0400695 helper.recordDraw(target, gp.get(), pipeline);
joshualitt4f569be2015-02-27 11:41:49 -0800696 }
697
Brian Salomon25a88092016-12-01 09:36:50 -0500698 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomona6aa5902016-12-16 09:32:00 -0500699 DashOp* that = t->cast<DashOp>();
Brian Salomon98222ac2017-07-12 15:27:54 -0400700 if (fProcessorSet != that->fProcessorSet) {
701 return false;
702 }
703 if (fDisallowCombineOnTouchOrOverlap &&
704 GrRectsTouchOrOverlap(this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700705 return false;
706 }
707
senorblancof3c2c462015-04-20 14:44:26 -0700708 if (this->aaMode() != that->aaMode()) {
joshualitt4f569be2015-02-27 11:41:49 -0800709 return false;
710 }
711
712 if (this->fullDash() != that->fullDash()) {
713 return false;
714 }
715
716 if (this->cap() != that->cap()) {
717 return false;
718 }
719
720 // TODO vertex color
721 if (this->color() != that->color()) {
722 return false;
723 }
724
Brian Salomon98222ac2017-07-12 15:27:54 -0400725 if (fUsesLocalCoords && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
joshualitt4f569be2015-02-27 11:41:49 -0800726 return false;
727 }
728
Brian Salomona6aa5902016-12-16 09:32:00 -0500729 fLines.push_back_n(that->fLines.count(), that->fLines.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700730 this->joinBounds(*that);
joshualitt4f569be2015-02-27 11:41:49 -0800731 return true;
732 }
733
Brian Salomona6aa5902016-12-16 09:32:00 -0500734 GrColor color() const { return fColor; }
Brian Salomona6aa5902016-12-16 09:32:00 -0500735 const SkMatrix& viewMatrix() const { return fLines[0].fViewMatrix; }
736 AAMode aaMode() const { return fAAMode; }
737 bool fullDash() const { return fFullDash; }
738 SkPaint::Cap cap() const { return fCap; }
joshualitt4f569be2015-02-27 11:41:49 -0800739
740 static const int kVertsPerDash = 4;
741 static const int kIndicesPerDash = 6;
742
Brian Salomonbeae8a92017-07-12 16:58:54 +0000743 SkSTArray<1, LineData, true> fLines;
Brian Salomon98222ac2017-07-12 15:27:54 -0400744 GrColor fColor;
745 bool fAllowsSRGBInputs : 1;
746 bool fDisableSRGBOutputConversion : 1;
747 bool fDisallowCombineOnTouchOrOverlap : 1;
748 bool fUsesLocalCoords : 1;
Brian Salomon98222ac2017-07-12 15:27:54 -0400749 bool fFullDash : 1;
Brian Salomon89717fc2017-07-13 11:25:18 -0400750 // We use 3 bits for this 3-value enum because MSVS makes the underlying types signed.
751 SkPaint::Cap fCap : 3;
Brian Salomon98222ac2017-07-12 15:27:54 -0400752 AAMode fAAMode;
753 GrProcessorSet fProcessorSet;
754 const GrUserStencilSettings* fStencilSettings;
reed1b55a962015-09-17 20:16:13 -0700755
Brian Salomon98222ac2017-07-12 15:27:54 -0400756 typedef GrMeshDrawOp INHERITED;
joshualitt4f569be2015-02-27 11:41:49 -0800757};
758
Brian Salomon98222ac2017-07-12 15:27:54 -0400759std::unique_ptr<GrDrawOp> GrDashOp::MakeDashLineOp(GrPaint&& paint,
760 const SkMatrix& viewMatrix,
761 const SkPoint pts[2],
762 AAMode aaMode,
763 const GrStyle& style,
764 const GrUserStencilSettings* stencilSettings) {
Brian Salomona6aa5902016-12-16 09:32:00 -0500765 SkASSERT(GrDashOp::CanDrawDashLine(pts, style, viewMatrix));
bsalomon6663acf2016-05-10 09:14:17 -0700766 const SkScalar* intervals = style.dashIntervals();
767 SkScalar phase = style.dashPhase();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000768
bsalomon6663acf2016-05-10 09:14:17 -0700769 SkPaint::Cap cap = style.strokeRec().getCap();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000770
Brian Salomona6aa5902016-12-16 09:32:00 -0500771 DashOp::LineData lineData;
772 lineData.fSrcStrokeWidth = style.strokeRec().getWidth();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000773
774 // the phase should be normalized to be [0, sum of all intervals)
kkinnunen261694c2015-05-05 08:00:10 -0700775 SkASSERT(phase >= 0 && phase < intervals[0] + intervals[1]);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000776
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000777 // 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 +0000778 if (pts[0].fY != pts[1].fY || pts[0].fX > pts[1].fX) {
egdaniele61c4112014-06-12 10:24:21 -0700779 SkMatrix rotMatrix;
Brian Salomona6aa5902016-12-16 09:32:00 -0500780 align_to_x_axis(pts, &rotMatrix, lineData.fPtsRot);
781 if (!rotMatrix.invert(&lineData.fSrcRotInv)) {
tfarina38406c82014-10-31 07:11:12 -0700782 SkDebugf("Failed to create invertible rotation matrix!\n");
halcanary96fcdcc2015-08-27 07:41:13 -0700783 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000784 }
785 } else {
Brian Salomona6aa5902016-12-16 09:32:00 -0500786 lineData.fSrcRotInv.reset();
787 memcpy(lineData.fPtsRot, pts, 2 * sizeof(SkPoint));
joshualitt4f569be2015-02-27 11:41:49 -0800788 }
789
790 // Scale corrections of intervals and stroke from view matrix
Brian Salomona6aa5902016-12-16 09:32:00 -0500791 calc_dash_scaling(&lineData.fParallelScale, &lineData.fPerpendicularScale, viewMatrix,
792 lineData.fPtsRot);
joshualitt4f569be2015-02-27 11:41:49 -0800793
Brian Salomona6aa5902016-12-16 09:32:00 -0500794 SkScalar offInterval = intervals[1] * lineData.fParallelScale;
795 SkScalar strokeWidth = lineData.fSrcStrokeWidth * lineData.fPerpendicularScale;
joshualitt4f569be2015-02-27 11:41:49 -0800796
Brian Salomona6aa5902016-12-16 09:32:00 -0500797 if (SkPaint::kSquare_Cap == cap && 0 != lineData.fSrcStrokeWidth) {
joshualitt4f569be2015-02-27 11:41:49 -0800798 // add cap to on interveal and remove from off interval
799 offInterval -= strokeWidth;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000800 }
801
joshualitt4f569be2015-02-27 11:41:49 -0800802 // TODO we can do a real rect call if not using fulldash(ie no off interval, not using AA)
bsalomonaf18fb42016-06-07 08:10:46 -0700803 bool fullDash = offInterval > 0.f || aaMode != AAMode::kNone;
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +0000804
Brian Salomona6aa5902016-12-16 09:32:00 -0500805 lineData.fViewMatrix = viewMatrix;
806 lineData.fPhase = phase;
807 lineData.fIntervals[0] = intervals[0];
808 lineData.fIntervals[1] = intervals[1];
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000809
Brian Salomon98222ac2017-07-12 15:27:54 -0400810 return DashOp::Make(std::move(paint), lineData, cap, aaMode, fullDash, stencilSettings);
joshualittfa2008f2015-04-29 11:32:05 -0700811}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000812
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000813//////////////////////////////////////////////////////////////////////////////
814
egdanielf767e792014-07-02 06:21:32 -0700815class GLDashingCircleEffect;
joshualitt9b989322014-12-15 14:16:27 -0800816
egdanielf767e792014-07-02 06:21:32 -0700817/*
818 * This effect will draw a dotted line (defined as a dashed lined with round caps and no on
819 * interval). The radius of the dots is given by the strokeWidth and the spacing by the DashInfo.
820 * Both of the previous two parameters are in device space. This effect also requires the setting of
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400821 * a float2 vertex attribute for the the four corners of the bounding rect. This attribute is the
egdanielf767e792014-07-02 06:21:32 -0700822 * "dash position" of each vertex. In other words it is the vertex coords (in device space) if we
823 * transform the line to be horizontal, with the start of line at the origin then shifted to the
824 * right by half the off interval. The line then goes in the positive x direction.
825 */
joshualitt249af152014-09-15 11:41:13 -0700826class DashingCircleEffect : public GrGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700827public:
828 typedef SkPathEffect::DashInfo DashInfo;
829
bungeman06ca8ec2016-06-09 08:01:03 -0700830 static sk_sp<GrGeometryProcessor> Make(GrColor,
831 AAMode aaMode,
832 const SkMatrix& localMatrix,
833 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700834
mtklein36352bf2015-03-25 18:17:31 -0700835 const char* name() const override { return "DashingCircleEffect"; }
egdanielf767e792014-07-02 06:21:32 -0700836
joshualitt71c92602015-01-14 08:12:47 -0800837 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -0800838
joshualitt5224ba72015-02-03 15:07:51 -0800839 const Attribute* inDashParams() const { return fInDashParams; }
840
841 const Attribute* inCircleParams() const { return fInCircleParams; }
joshualitt249af152014-09-15 11:41:13 -0700842
bsalomonaf18fb42016-06-07 08:10:46 -0700843 AAMode aaMode() const { return fAAMode; }
egdanielf767e792014-07-02 06:21:32 -0700844
joshualitt88c23fc2015-05-13 14:18:07 -0700845 GrColor color() const { return fColor; }
846
joshualitte3ababe2015-05-15 07:56:07 -0700847 const SkMatrix& localMatrix() const { return fLocalMatrix; }
848
joshualittb8c241a2015-05-19 08:23:30 -0700849 bool usesLocalCoords() const { return fUsesLocalCoords; }
850
Brian Salomon94efbf52016-11-29 13:43:05 -0500851 void getGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder* b) const override;
egdanielf767e792014-07-02 06:21:32 -0700852
Brian Salomon94efbf52016-11-29 13:43:05 -0500853 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
egdanielf767e792014-07-02 06:21:32 -0700854
855private:
bsalomonaf18fb42016-06-07 08:10:46 -0700856 DashingCircleEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -0700857 bool usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -0700858
joshualitt88c23fc2015-05-13 14:18:07 -0700859 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -0700860 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700861 bool fUsesLocalCoords;
bsalomonaf18fb42016-06-07 08:10:46 -0700862 AAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -0800863 const Attribute* fInPosition;
864 const Attribute* fInDashParams;
865 const Attribute* fInCircleParams;
egdanielf767e792014-07-02 06:21:32 -0700866
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400867 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
egdanielf767e792014-07-02 06:21:32 -0700868
joshualitt249af152014-09-15 11:41:13 -0700869 typedef GrGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700870};
871
872//////////////////////////////////////////////////////////////////////////////
873
egdaniele659a582015-11-13 09:55:43 -0800874class GLDashingCircleEffect : public GrGLSLGeometryProcessor {
egdanielf767e792014-07-02 06:21:32 -0700875public:
joshualitt465283c2015-09-11 08:19:35 -0700876 GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700877
mtklein36352bf2015-03-25 18:17:31 -0700878 void onEmitCode(EmitArgs&, GrGPArgs*) override;
egdanielf767e792014-07-02 06:21:32 -0700879
joshualitt87f48d92014-12-04 10:41:40 -0800880 static inline void GenKey(const GrGeometryProcessor&,
Brian Salomon94efbf52016-11-29 13:43:05 -0500881 const GrShaderCaps&,
joshualitt87f48d92014-12-04 10:41:40 -0800882 GrProcessorKeyBuilder*);
egdanielf767e792014-07-02 06:21:32 -0700883
bsalomona624bf32016-09-20 09:12:47 -0700884 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&,
885 FPCoordTransformIter&& transformIter) override;
egdanielf767e792014-07-02 06:21:32 -0700886private:
joshualitt9b989322014-12-15 14:16:27 -0800887 UniformHandle fParamUniform;
888 UniformHandle fColorUniform;
889 GrColor fColor;
890 SkScalar fPrevRadius;
891 SkScalar fPrevCenterX;
892 SkScalar fPrevIntervalLength;
egdaniele659a582015-11-13 09:55:43 -0800893 typedef GrGLSLGeometryProcessor INHERITED;
egdanielf767e792014-07-02 06:21:32 -0700894};
895
joshualitt465283c2015-09-11 08:19:35 -0700896GLDashingCircleEffect::GLDashingCircleEffect() {
joshualitt9b989322014-12-15 14:16:27 -0800897 fColor = GrColor_ILLEGAL;
egdanielf767e792014-07-02 06:21:32 -0700898 fPrevRadius = SK_ScalarMin;
899 fPrevCenterX = SK_ScalarMin;
900 fPrevIntervalLength = SK_ScalarMax;
901}
902
robertphillips46d36f02015-01-18 08:14:14 -0800903void GLDashingCircleEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -0700904 const DashingCircleEffect& dce = args.fGP.cast<DashingCircleEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800905 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800906 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800907 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800908
joshualittabb52a12015-01-13 15:02:10 -0800909 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800910 varyingHandler->emitAttributes(dce);
joshualittabb52a12015-01-13 15:02:10 -0800911
joshualitt5224ba72015-02-03 15:07:51 -0800912 // XY are dashPos, Z is dashInterval
Chris Dalton27372882017-12-08 13:34:21 -0700913 GrGLSLVarying dashParams(kHalf3_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800914 varyingHandler->addVarying("DashParam", &dashParams);
Brian Salomon70132d02018-05-29 15:33:06 -0400915 vertBuilder->codeAppendf("%s = %s;", dashParams.vsOut(), dce.inDashParams()->name());
joshualitt5224ba72015-02-03 15:07:51 -0800916
senorblancof3c2c462015-04-20 14:44:26 -0700917 // x refers to circle radius - 0.5, y refers to cicle's center x coord
Chris Dalton27372882017-12-08 13:34:21 -0700918 GrGLSLVarying circleParams(kHalf2_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800919 varyingHandler->addVarying("CircleParams", &circleParams);
Brian Salomon70132d02018-05-29 15:33:06 -0400920 vertBuilder->codeAppendf("%s = %s;", circleParams.vsOut(), dce.inCircleParams()->name());
joshualitt30ba4362014-08-21 20:18:45 -0700921
Chris Dalton60283612018-02-14 13:38:14 -0700922 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800923 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -0500924 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualitt9b989322014-12-15 14:16:27 -0800925
joshualittabb52a12015-01-13 15:02:10 -0800926 // Setup position
Brian Salomon70132d02018-05-29 15:33:06 -0400927 this->writeOutputPosition(vertBuilder, gpArgs, dce.inPosition()->name());
joshualitt4973d9d2014-11-08 09:24:25 -0800928
joshualittabb52a12015-01-13 15:02:10 -0800929 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800930 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800931 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800932 uniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -0500933 dce.inPosition()->asShaderVar(),
egdaniel4ca2e602015-11-18 08:01:26 -0800934 dce.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -0700935 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -0800936
egdanielf767e792014-07-02 06:21:32 -0700937 // transforms all points so that we can compare them to our test circle
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400938 fragBuilder->codeAppendf("half xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
egdaniel4ca2e602015-11-18 08:01:26 -0800939 dashParams.fsIn(), dashParams.fsIn(), dashParams.fsIn(),
940 dashParams.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400941 fragBuilder->codeAppendf("half2 fragPosShifted = half2(xShifted, %s.y);", dashParams.fsIn());
942 fragBuilder->codeAppendf("half2 center = half2(%s.y, 0.0);", circleParams.fsIn());
943 fragBuilder->codeAppend("half dist = length(center - fragPosShifted);");
bsalomonaf18fb42016-06-07 08:10:46 -0700944 if (dce.aaMode() != AAMode::kNone) {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400945 fragBuilder->codeAppendf("half diff = dist - %s.x;", circleParams.fsIn());
egdaniel4ca2e602015-11-18 08:01:26 -0800946 fragBuilder->codeAppend("diff = 1.0 - diff;");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400947 fragBuilder->codeAppend("half alpha = clamp(diff, 0.0, 1.0);");
egdanielf767e792014-07-02 06:21:32 -0700948 } else {
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400949 fragBuilder->codeAppendf("half alpha = 1.0;");
egdaniel4ca2e602015-11-18 08:01:26 -0800950 fragBuilder->codeAppendf("alpha *= dist < %s.x + 0.5 ? 1.0 : 0.0;", circleParams.fsIn());
egdanielf767e792014-07-02 06:21:32 -0700951 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400952 fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage);
egdanielf767e792014-07-02 06:21:32 -0700953}
954
egdaniel018fb622015-10-28 07:26:40 -0700955void GLDashingCircleEffect::setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -0700956 const GrPrimitiveProcessor& processor,
957 FPCoordTransformIter&& transformIter) {
joshualittb8c241a2015-05-19 08:23:30 -0700958 const DashingCircleEffect& dce = processor.cast<DashingCircleEffect>();
959 if (dce.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -0700960 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -0700961 GrColorToRGBAFloat(dce.color(), c);
joshualitt9b989322014-12-15 14:16:27 -0800962 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -0700963 fColor = dce.color();
joshualitt9b989322014-12-15 14:16:27 -0800964 }
bsalomona624bf32016-09-20 09:12:47 -0700965 this->setTransformDataHelper(dce.localMatrix(), pdman, &transformIter);
egdanielf767e792014-07-02 06:21:32 -0700966}
967
robertphillips46d36f02015-01-18 08:14:14 -0800968void GLDashingCircleEffect::GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500969 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -0700970 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -0800971 const DashingCircleEffect& dce = gp.cast<DashingCircleEffect>();
972 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -0700973 key |= dce.usesLocalCoords() && dce.localMatrix().hasPerspective() ? 0x1 : 0x0;
Brian Salomonbfd51832017-01-04 13:22:08 -0500974 key |= static_cast<uint32_t>(dce.aaMode()) << 1;
joshualittb8c241a2015-05-19 08:23:30 -0700975 b->add32(key);
egdanielf767e792014-07-02 06:21:32 -0700976}
977
978//////////////////////////////////////////////////////////////////////////////
979
bungeman06ca8ec2016-06-09 08:01:03 -0700980sk_sp<GrGeometryProcessor> DashingCircleEffect::Make(GrColor color,
981 AAMode aaMode,
982 const SkMatrix& localMatrix,
983 bool usesLocalCoords) {
984 return sk_sp<GrGeometryProcessor>(
985 new DashingCircleEffect(color, aaMode, localMatrix, usesLocalCoords));
egdanielf767e792014-07-02 06:21:32 -0700986}
987
Brian Salomon94efbf52016-11-29 13:43:05 -0500988void DashingCircleEffect::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -0800989 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -0700990 GLDashingCircleEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800991}
992
Brian Salomon94efbf52016-11-29 13:43:05 -0500993GrGLSLPrimitiveProcessor* DashingCircleEffect::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -0700994 return new GLDashingCircleEffect();
egdanielf767e792014-07-02 06:21:32 -0700995}
996
joshualitt2e3b3e32014-12-09 13:31:14 -0800997DashingCircleEffect::DashingCircleEffect(GrColor color,
bsalomonaf18fb42016-06-07 08:10:46 -0700998 AAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -0700999 const SkMatrix& localMatrix,
1000 bool usesLocalCoords)
Ethan Nicholasabff9562017-10-09 10:54:08 -04001001 : INHERITED(kDashingCircleEffect_ClassID)
1002 , fColor(color)
joshualitte3ababe2015-05-15 07:56:07 -07001003 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -07001004 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -07001005 , fAAMode(aaMode) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04001006 fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType);
1007 fInDashParams = &this->addVertexAttrib("inDashParams", kHalf3_GrVertexAttribType);
1008 fInCircleParams = &this->addVertexAttrib("inCircleParams", kHalf2_GrVertexAttribType);
egdanielf767e792014-07-02 06:21:32 -07001009}
1010
joshualittb0a8a372014-09-23 09:50:21 -07001011GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingCircleEffect);
egdanielf767e792014-07-02 06:21:32 -07001012
Hal Canary6f6961e2017-01-31 13:50:44 -05001013#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -07001014sk_sp<GrGeometryProcessor> DashingCircleEffect::TestCreate(GrProcessorTestData* d) {
Brian Salomona6aa5902016-12-16 09:32:00 -05001015 AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashOp::kAAModeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -07001016 return DashingCircleEffect::Make(GrRandomColor(d->fRandom),
1017 aaMode, GrTest::TestMatrix(d->fRandom),
1018 d->fRandom->nextBool());
egdanielf767e792014-07-02 06:21:32 -07001019}
Hal Canary6f6961e2017-01-31 13:50:44 -05001020#endif
egdanielf767e792014-07-02 06:21:32 -07001021
1022//////////////////////////////////////////////////////////////////////////////
1023
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001024class GLDashingLineEffect;
1025
egdanielf767e792014-07-02 06:21:32 -07001026/*
1027 * This effect will draw a dashed line. The width of the dash is given by the strokeWidth and the
1028 * length and spacing by the DashInfo. Both of the previous two parameters are in device space.
Ethan Nicholas5af9ea32017-07-28 15:19:46 -04001029 * This effect also requires the setting of a float2 vertex attribute for the the four corners of the
egdanielf767e792014-07-02 06:21:32 -07001030 * bounding rect. This attribute is the "dash position" of each vertex. In other words it is the
1031 * vertex coords (in device space) if we transform the line to be horizontal, with the start of
1032 * line at the origin then shifted to the right by half the off interval. The line then goes in the
1033 * positive x direction.
1034 */
joshualitt249af152014-09-15 11:41:13 -07001035class DashingLineEffect : public GrGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001036public:
1037 typedef SkPathEffect::DashInfo DashInfo;
1038
bungeman06ca8ec2016-06-09 08:01:03 -07001039 static sk_sp<GrGeometryProcessor> Make(GrColor,
1040 AAMode aaMode,
1041 const SkMatrix& localMatrix,
1042 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001043
mtklein36352bf2015-03-25 18:17:31 -07001044 const char* name() const override { return "DashingEffect"; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001045
joshualitt71c92602015-01-14 08:12:47 -08001046 const Attribute* inPosition() const { return fInPosition; }
joshualitt2dd1ae02014-12-03 06:24:10 -08001047
joshualitt5224ba72015-02-03 15:07:51 -08001048 const Attribute* inDashParams() const { return fInDashParams; }
1049
1050 const Attribute* inRectParams() const { return fInRectParams; }
joshualitt249af152014-09-15 11:41:13 -07001051
bsalomonaf18fb42016-06-07 08:10:46 -07001052 AAMode aaMode() const { return fAAMode; }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001053
joshualitt88c23fc2015-05-13 14:18:07 -07001054 GrColor color() const { return fColor; }
1055
Brian Salomonbfd51832017-01-04 13:22:08 -05001056 const SkMatrix& localMatrix() const { return fLocalMatrix; }
joshualitte3ababe2015-05-15 07:56:07 -07001057
joshualittb8c241a2015-05-19 08:23:30 -07001058 bool usesLocalCoords() const { return fUsesLocalCoords; }
1059
Brian Salomon94efbf52016-11-29 13:43:05 -05001060 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001061
Brian Salomon94efbf52016-11-29 13:43:05 -05001062 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001063
1064private:
bsalomonaf18fb42016-06-07 08:10:46 -07001065 DashingLineEffect(GrColor, AAMode aaMode, const SkMatrix& localMatrix,
joshualittb8c241a2015-05-19 08:23:30 -07001066 bool usesLocalCoords);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001067
joshualitt88c23fc2015-05-13 14:18:07 -07001068 GrColor fColor;
joshualitte3ababe2015-05-15 07:56:07 -07001069 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -07001070 bool fUsesLocalCoords;
bsalomonaf18fb42016-06-07 08:10:46 -07001071 AAMode fAAMode;
joshualitt5224ba72015-02-03 15:07:51 -08001072 const Attribute* fInPosition;
1073 const Attribute* fInDashParams;
1074 const Attribute* fInRectParams;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001075
Brian Salomon0c26a9d2017-07-06 10:09:38 -04001076 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001077
joshualitt249af152014-09-15 11:41:13 -07001078 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001079};
1080
1081//////////////////////////////////////////////////////////////////////////////
1082
egdaniele659a582015-11-13 09:55:43 -08001083class GLDashingLineEffect : public GrGLSLGeometryProcessor {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001084public:
joshualitt465283c2015-09-11 08:19:35 -07001085 GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001086
mtklein36352bf2015-03-25 18:17:31 -07001087 void onEmitCode(EmitArgs&, GrGPArgs*) override;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001088
joshualitt87f48d92014-12-04 10:41:40 -08001089 static inline void GenKey(const GrGeometryProcessor&,
Brian Salomon94efbf52016-11-29 13:43:05 -05001090 const GrShaderCaps&,
joshualitt87f48d92014-12-04 10:41:40 -08001091 GrProcessorKeyBuilder*);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001092
bsalomona624bf32016-09-20 09:12:47 -07001093 void setData(const GrGLSLProgramDataManager&, const GrPrimitiveProcessor&,
1094 FPCoordTransformIter&& iter) override;
joshualitte3ababe2015-05-15 07:56:07 -07001095
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001096private:
joshualitt9b989322014-12-15 14:16:27 -08001097 GrColor fColor;
joshualitt9b989322014-12-15 14:16:27 -08001098 UniformHandle fColorUniform;
egdaniele659a582015-11-13 09:55:43 -08001099 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001100};
1101
Brian Salomonbfd51832017-01-04 13:22:08 -05001102GLDashingLineEffect::GLDashingLineEffect() : fColor(GrColor_ILLEGAL) {}
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001103
robertphillips46d36f02015-01-18 08:14:14 -08001104void GLDashingLineEffect::onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) {
joshualittc369e7c2014-10-22 10:56:26 -07001105 const DashingLineEffect& de = args.fGP.cast<DashingLineEffect>();
joshualitt2dd1ae02014-12-03 06:24:10 -08001106
egdaniel4ca2e602015-11-18 08:01:26 -08001107 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -08001108 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -08001109 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -08001110
joshualittabb52a12015-01-13 15:02:10 -08001111 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -08001112 varyingHandler->emitAttributes(de);
joshualittabb52a12015-01-13 15:02:10 -08001113
joshualitt5224ba72015-02-03 15:07:51 -08001114 // XY refers to dashPos, Z is the dash interval length
Chris Dalton27372882017-12-08 13:34:21 -07001115 GrGLSLVarying inDashParams(kFloat3_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -06001116 varyingHandler->addVarying("DashParams", &inDashParams);
Brian Salomon70132d02018-05-29 15:33:06 -04001117 vertBuilder->codeAppendf("%s = %s;", inDashParams.vsOut(), de.inDashParams()->name());
joshualitt5224ba72015-02-03 15:07:51 -08001118
1119 // The rect uniform's xyzw refer to (left + 0.5, top + 0.5, right - 0.5, bottom - 0.5),
1120 // respectively.
Chris Dalton27372882017-12-08 13:34:21 -07001121 GrGLSLVarying inRectParams(kFloat4_GrSLType);
Chris Daltonfdde34e2017-10-16 14:15:26 -06001122 varyingHandler->addVarying("RectParams", &inRectParams);
Brian Salomon70132d02018-05-29 15:33:06 -04001123 vertBuilder->codeAppendf("%s = %s;", inRectParams.vsOut(), de.inRectParams()->name());
joshualitt2dd1ae02014-12-03 06:24:10 -08001124
Chris Dalton60283612018-02-14 13:38:14 -07001125 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -08001126 // Setup pass through color
Brian Salomonbfd51832017-01-04 13:22:08 -05001127 this->setupUniformColor(fragBuilder, uniformHandler, args.fOutputColor, &fColorUniform);
joshualittb8c241a2015-05-19 08:23:30 -07001128
joshualittabb52a12015-01-13 15:02:10 -08001129 // Setup position
Brian Salomon70132d02018-05-29 15:33:06 -04001130 this->writeOutputPosition(vertBuilder, gpArgs, de.inPosition()->name());
joshualitt4973d9d2014-11-08 09:24:25 -08001131
joshualittabb52a12015-01-13 15:02:10 -08001132 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -08001133 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -08001134 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -08001135 uniformHandler,
Brian Salomon04460cc2017-12-06 14:47:42 -05001136 de.inPosition()->asShaderVar(),
egdaniel4ca2e602015-11-18 08:01:26 -08001137 de.localMatrix(),
bsalomona624bf32016-09-20 09:12:47 -07001138 args.fFPCoordTransformHandler);
joshualittabb52a12015-01-13 15:02:10 -08001139
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001140 // transforms all points so that we can compare them to our test rect
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001141 fragBuilder->codeAppendf("half xShifted = %s.x - floor(%s.x / %s.z) * %s.z;",
egdaniel4ca2e602015-11-18 08:01:26 -08001142 inDashParams.fsIn(), inDashParams.fsIn(), inDashParams.fsIn(),
1143 inDashParams.fsIn());
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001144 fragBuilder->codeAppendf("half2 fragPosShifted = half2(xShifted, %s.y);", inDashParams.fsIn());
bsalomonaf18fb42016-06-07 08:10:46 -07001145 if (de.aaMode() == AAMode::kCoverage) {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001146 // The amount of coverage removed in x and y by the edges is computed as a pair of negative
1147 // numbers, xSub and ySub.
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001148 fragBuilder->codeAppend("half xSub, ySub;");
egdaniel4ca2e602015-11-18 08:01:26 -08001149 fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1150 fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
1151 fragBuilder->codeAppendf("ySub = min(fragPosShifted.y - %s.y, 0.0);", inRectParams.fsIn());
1152 fragBuilder->codeAppendf("ySub += min(%s.w - fragPosShifted.y, 0.0);", inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001153 // Now compute coverage in x and y and multiply them to get the fraction of the pixel
1154 // covered.
egdaniel4ca2e602015-11-18 08:01:26 -08001155 fragBuilder->codeAppendf(
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001156 "half alpha = (1.0 + max(xSub, -1.0)) * (1.0 + max(ySub, -1.0));");
bsalomonaf18fb42016-06-07 08:10:46 -07001157 } else if (de.aaMode() == AAMode::kCoverageWithMSAA) {
senorblancof3c2c462015-04-20 14:44:26 -07001158 // For MSAA, we don't modulate the alpha by the Y distance, since MSAA coverage will handle
1159 // AA on the the top and bottom edges. The shader is only responsible for intra-dash alpha.
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001160 fragBuilder->codeAppend("half xSub;");
egdaniel4ca2e602015-11-18 08:01:26 -08001161 fragBuilder->codeAppendf("xSub = min(fragPosShifted.x - %s.x, 0.0);", inRectParams.fsIn());
1162 fragBuilder->codeAppendf("xSub += min(%s.z - fragPosShifted.x, 0.0);", inRectParams.fsIn());
senorblancof3c2c462015-04-20 14:44:26 -07001163 // Now compute coverage in x to get the fraction of the pixel covered.
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001164 fragBuilder->codeAppendf("half alpha = (1.0 + max(xSub, -1.0));");
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001165 } else {
1166 // Assuming the bounding geometry is tight so no need to check y values
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001167 fragBuilder->codeAppendf("half alpha = 1.0;");
egdaniel4ca2e602015-11-18 08:01:26 -08001168 fragBuilder->codeAppendf("alpha *= (fragPosShifted.x - %s.x) > -0.5 ? 1.0 : 0.0;",
1169 inRectParams.fsIn());
1170 fragBuilder->codeAppendf("alpha *= (%s.z - fragPosShifted.x) >= -0.5 ? 1.0 : 0.0;",
1171 inRectParams.fsIn());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001172 }
Ethan Nicholasf7b88202017-09-18 14:10:39 -04001173 fragBuilder->codeAppendf("%s = half4(alpha);", args.fOutputCoverage);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001174}
1175
egdaniel018fb622015-10-28 07:26:40 -07001176void GLDashingLineEffect::setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -07001177 const GrPrimitiveProcessor& processor,
1178 FPCoordTransformIter&& transformIter) {
joshualittb8c241a2015-05-19 08:23:30 -07001179 const DashingLineEffect& de = processor.cast<DashingLineEffect>();
1180 if (de.color() != fColor) {
egdaniel018fb622015-10-28 07:26:40 -07001181 float c[4];
joshualittb8c241a2015-05-19 08:23:30 -07001182 GrColorToRGBAFloat(de.color(), c);
joshualitt9b989322014-12-15 14:16:27 -08001183 pdman.set4fv(fColorUniform, 1, c);
joshualittb8c241a2015-05-19 08:23:30 -07001184 fColor = de.color();
joshualitt9b989322014-12-15 14:16:27 -08001185 }
bsalomona624bf32016-09-20 09:12:47 -07001186 this->setTransformDataHelper(de.localMatrix(), pdman, &transformIter);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001187}
1188
robertphillips46d36f02015-01-18 08:14:14 -08001189void GLDashingLineEffect::GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -05001190 const GrShaderCaps&,
joshualittb0a8a372014-09-23 09:50:21 -07001191 GrProcessorKeyBuilder* b) {
robertphillips46d36f02015-01-18 08:14:14 -08001192 const DashingLineEffect& de = gp.cast<DashingLineEffect>();
1193 uint32_t key = 0;
joshualittb8c241a2015-05-19 08:23:30 -07001194 key |= de.usesLocalCoords() && de.localMatrix().hasPerspective() ? 0x1 : 0x0;
bsalomonaf18fb42016-06-07 08:10:46 -07001195 key |= static_cast<int>(de.aaMode()) << 8;
joshualittb8c241a2015-05-19 08:23:30 -07001196 b->add32(key);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001197}
1198
1199//////////////////////////////////////////////////////////////////////////////
skia.committer@gmail.com3b9e8be2014-05-20 03:05:34 +00001200
bungeman06ca8ec2016-06-09 08:01:03 -07001201sk_sp<GrGeometryProcessor> DashingLineEffect::Make(GrColor color,
1202 AAMode aaMode,
1203 const SkMatrix& localMatrix,
1204 bool usesLocalCoords) {
1205 return sk_sp<GrGeometryProcessor>(
1206 new DashingLineEffect(color, aaMode, localMatrix, usesLocalCoords));
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001207}
1208
Brian Salomon94efbf52016-11-29 13:43:05 -05001209void DashingLineEffect::getGLSLProcessorKey(const GrShaderCaps& caps,
egdaniel57d3b032015-11-13 11:57:27 -08001210 GrProcessorKeyBuilder* b) const {
joshualitt465283c2015-09-11 08:19:35 -07001211 GLDashingLineEffect::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -08001212}
1213
Brian Salomon94efbf52016-11-29 13:43:05 -05001214GrGLSLPrimitiveProcessor* DashingLineEffect::createGLSLInstance(const GrShaderCaps&) const {
joshualitt465283c2015-09-11 08:19:35 -07001215 return new GLDashingLineEffect();
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001216}
1217
joshualitt2e3b3e32014-12-09 13:31:14 -08001218DashingLineEffect::DashingLineEffect(GrColor color,
bsalomonaf18fb42016-06-07 08:10:46 -07001219 AAMode aaMode,
joshualittb8c241a2015-05-19 08:23:30 -07001220 const SkMatrix& localMatrix,
1221 bool usesLocalCoords)
Ethan Nicholasabff9562017-10-09 10:54:08 -04001222 : INHERITED(kDashingLineEffect_ClassID)
1223 , fColor(color)
joshualitte3ababe2015-05-15 07:56:07 -07001224 , fLocalMatrix(localMatrix)
joshualittb8c241a2015-05-19 08:23:30 -07001225 , fUsesLocalCoords(usesLocalCoords)
joshualitt88c23fc2015-05-13 14:18:07 -07001226 , fAAMode(aaMode) {
Ethan Nicholasfa7ee242017-09-25 09:52:04 -04001227 fInPosition = &this->addVertexAttrib("inPosition", kFloat2_GrVertexAttribType);
1228 fInDashParams = &this->addVertexAttrib("inDashParams", kHalf3_GrVertexAttribType);
1229 fInRectParams = &this->addVertexAttrib("inRect", kHalf4_GrVertexAttribType);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001230}
1231
joshualittb0a8a372014-09-23 09:50:21 -07001232GR_DEFINE_GEOMETRY_PROCESSOR_TEST(DashingLineEffect);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001233
Hal Canary6f6961e2017-01-31 13:50:44 -05001234#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -07001235sk_sp<GrGeometryProcessor> DashingLineEffect::TestCreate(GrProcessorTestData* d) {
Brian Salomona6aa5902016-12-16 09:32:00 -05001236 AAMode aaMode = static_cast<AAMode>(d->fRandom->nextULessThan(GrDashOp::kAAModeCnt));
bungeman06ca8ec2016-06-09 08:01:03 -07001237 return DashingLineEffect::Make(GrRandomColor(d->fRandom),
1238 aaMode, GrTest::TestMatrix(d->fRandom),
1239 d->fRandom->nextBool());
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001240}
Hal Canary6f6961e2017-01-31 13:50:44 -05001241#endif
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001242
1243//////////////////////////////////////////////////////////////////////////////
1244
bungeman06ca8ec2016-06-09 08:01:03 -07001245static sk_sp<GrGeometryProcessor> make_dash_gp(GrColor color,
1246 AAMode aaMode,
1247 DashCap cap,
1248 const SkMatrix& viewMatrix,
1249 bool usesLocalCoords) {
joshualittdf0c5572015-08-03 11:35:28 -07001250 SkMatrix invert;
1251 if (usesLocalCoords && !viewMatrix.invert(&invert)) {
1252 SkDebugf("Failed to invert\n");
halcanary96fcdcc2015-08-27 07:41:13 -07001253 return nullptr;
joshualittdf0c5572015-08-03 11:35:28 -07001254 }
1255
egdanielf767e792014-07-02 06:21:32 -07001256 switch (cap) {
joshualitt5224ba72015-02-03 15:07:51 -08001257 case kRound_DashCap:
bungeman06ca8ec2016-06-09 08:01:03 -07001258 return DashingCircleEffect::Make(color, aaMode, invert, usesLocalCoords);
joshualitt5224ba72015-02-03 15:07:51 -08001259 case kNonRound_DashCap:
bungeman06ca8ec2016-06-09 08:01:03 -07001260 return DashingLineEffect::Make(color, aaMode, invert, usesLocalCoords);
egdanielf767e792014-07-02 06:21:32 -07001261 }
halcanary96fcdcc2015-08-27 07:41:13 -07001262 return nullptr;
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +00001263}
joshualittfa2008f2015-04-29 11:32:05 -07001264
1265/////////////////////////////////////////////////////////////////////////////////////////////////
1266
Hal Canary6f6961e2017-01-31 13:50:44 -05001267#if GR_TEST_UTILS
joshualittfa2008f2015-04-29 11:32:05 -07001268
Brian Salomon98222ac2017-07-12 15:27:54 -04001269GR_DRAW_OP_TEST_DEFINE(DashOp) {
joshualittfa2008f2015-04-29 11:32:05 -07001270 SkMatrix viewMatrix = GrTest::TestMatrixPreservesRightAngles(random);
Brian Salomon98222ac2017-07-12 15:27:54 -04001271 AAMode aaMode;
1272 do {
1273 aaMode = static_cast<AAMode>(random->nextULessThan(GrDashOp::kAAModeCnt));
1274 } while (AAMode::kCoverageWithMSAA == aaMode && GrFSAAType::kUnifiedMSAA != fsaaType);
joshualittfa2008f2015-04-29 11:32:05 -07001275
1276 // We can only dash either horizontal or vertical lines
1277 SkPoint pts[2];
1278 if (random->nextBool()) {
1279 // vertical
1280 pts[0].fX = 1.f;
1281 pts[0].fY = random->nextF() * 10.f;
1282 pts[1].fX = 1.f;
1283 pts[1].fY = random->nextF() * 10.f;
1284 } else {
1285 // horizontal
1286 pts[0].fX = random->nextF() * 10.f;
1287 pts[0].fY = 1.f;
1288 pts[1].fX = random->nextF() * 10.f;
1289 pts[1].fY = 1.f;
1290 }
1291
1292 // pick random cap
bsalomona7d85ba2016-07-06 11:54:59 -07001293 SkPaint::Cap cap = SkPaint::Cap(random->nextULessThan(SkPaint::kCapCount));
joshualittfa2008f2015-04-29 11:32:05 -07001294
1295 SkScalar intervals[2];
1296
1297 // We can only dash with the following intervals
1298 enum Intervals {
1299 kOpenOpen_Intervals ,
1300 kOpenClose_Intervals,
1301 kCloseOpen_Intervals,
1302 };
1303
Greg Daniel5d00f002017-06-29 13:44:51 -04001304 Intervals intervalType = SkPaint::kRound_Cap == cap ?
joshualittfa2008f2015-04-29 11:32:05 -07001305 kOpenClose_Intervals :
1306 Intervals(random->nextULessThan(kCloseOpen_Intervals + 1));
1307 static const SkScalar kIntervalMin = 0.1f;
Greg Daniel5d00f002017-06-29 13:44:51 -04001308 static const SkScalar kIntervalMinCircles = 1.f; // Must be >= to stroke width
joshualittfa2008f2015-04-29 11:32:05 -07001309 static const SkScalar kIntervalMax = 10.f;
1310 switch (intervalType) {
1311 case kOpenOpen_Intervals:
1312 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1313 intervals[1] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1314 break;
Greg Daniel5d00f002017-06-29 13:44:51 -04001315 case kOpenClose_Intervals: {
joshualittfa2008f2015-04-29 11:32:05 -07001316 intervals[0] = 0.f;
Greg Daniel5d00f002017-06-29 13:44:51 -04001317 SkScalar min = SkPaint::kRound_Cap == cap ? kIntervalMinCircles : kIntervalMin;
1318 intervals[1] = random->nextRangeScalar(min, kIntervalMax);
joshualittfa2008f2015-04-29 11:32:05 -07001319 break;
Greg Daniel5d00f002017-06-29 13:44:51 -04001320 }
joshualittfa2008f2015-04-29 11:32:05 -07001321 case kCloseOpen_Intervals:
1322 intervals[0] = random->nextRangeScalar(kIntervalMin, kIntervalMax);
1323 intervals[1] = 0.f;
1324 break;
1325
1326 }
1327
1328 // phase is 0 < sum (i0, i1)
1329 SkScalar phase = random->nextRangeScalar(0, intervals[0] + intervals[1]);
1330
1331 SkPaint p;
1332 p.setStyle(SkPaint::kStroke_Style);
1333 p.setStrokeWidth(SkIntToScalar(1));
1334 p.setStrokeCap(cap);
bsalomon6663acf2016-05-10 09:14:17 -07001335 p.setPathEffect(GrTest::TestDashPathEffect::Make(intervals, 2, phase));
joshualittfa2008f2015-04-29 11:32:05 -07001336
bsalomon6663acf2016-05-10 09:14:17 -07001337 GrStyle style(p);
joshualittfa2008f2015-04-29 11:32:05 -07001338
Brian Salomon98222ac2017-07-12 15:27:54 -04001339 return GrDashOp::MakeDashLineOp(std::move(paint), viewMatrix, pts, aaMode, style,
1340 GrGetRandomStencil(random, context));
joshualittfa2008f2015-04-29 11:32:05 -07001341}
1342
1343#endif