blob: e23c24ed3b0d0cb3f8da46d3f0433f262ac463fd [file] [log] [blame]
reed@google.com35a81df2012-05-04 21:49:27 +00001/*
2 * Copyright 2012 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 "gm.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkDashPathEffect.h"
12
reed@google.comde1837b2012-05-21 16:47:43 +000013static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000014 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
halcanary9d524f22016-03-29 09:03:52 -070015 SkScalar phase = SkIntToScalar(0),
robertphillips9f2251c2014-11-04 13:33:50 -080016 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
reed@google.com35a81df2012-05-04 21:49:27 +000017 SkPaint p(paint);
18
19 const SkScalar intervals[] = {
20 SkIntToScalar(on),
21 SkIntToScalar(off),
22 };
23
reeda4393342016-03-18 11:22:57 -070024 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips9f2251c2014-11-04 13:33:50 -080025 canvas->drawLine(startX, startY, finalX, finalY, p);
reed@google.comde1837b2012-05-21 16:47:43 +000026}
27
28// earlier bug stopped us from drawing very long single-segment dashes, because
29// SkPathMeasure was skipping very small delta-T values (nearlyzero). This is
30// now fixes, so this giant dash should appear.
31static void show_giant_dash(SkCanvas* canvas) {
32 SkPaint paint;
33
34 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
reed@google.com35a81df2012-05-04 21:49:27 +000035}
36
egdaniel21402e32014-11-05 05:02:27 -080037static void show_zero_len_dash(SkCanvas* canvas) {
38 SkPaint paint;
39
40 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
41 paint.setStyle(SkPaint::kStroke_Style);
42 paint.setStrokeWidth(SkIntToScalar(2));
43 canvas->translate(0, SkIntToScalar(20));
44 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
45}
46
reed@google.com21384df2012-05-18 17:59:08 +000047class DashingGM : public skiagm::GM {
reed@google.com35a81df2012-05-04 21:49:27 +000048public:
49 DashingGM() {}
50
51protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000052
reed@google.com35a81df2012-05-04 21:49:27 +000053 SkString onShortName() {
54 return SkString("dashing");
55 }
56
egdanielc00389e2015-10-05 08:11:49 -070057 SkISize onISize() { return SkISize::Make(640, 340); }
reed@google.com35a81df2012-05-04 21:49:27 +000058
59 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -070060 constexpr struct {
reed@google.com35a81df2012-05-04 21:49:27 +000061 int fOnInterval;
62 int fOffInterval;
63 } gData[] = {
64 { 1, 1 },
65 { 4, 1 },
66 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000067
reed@google.com35a81df2012-05-04 21:49:27 +000068 SkPaint paint;
69 paint.setStyle(SkPaint::kStroke_Style);
70
71 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
72 canvas->translate(0, SK_ScalarHalf);
reed@google.com35a81df2012-05-04 21:49:27 +000073 for (int width = 0; width <= 2; ++width) {
74 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
75 for (int aa = 0; aa <= 1; ++aa) {
76 int w = width * width * width;
77 paint.setAntiAlias(SkToBool(aa));
78 paint.setStrokeWidth(SkIntToScalar(w));
rmistry@google.comae933ce2012-08-23 18:19:56 +000079
reed@google.com35a81df2012-05-04 21:49:27 +000080 int scale = w ? w : 1;
81
82 drawline(canvas, gData[data].fOnInterval * scale,
83 gData[data].fOffInterval * scale,
84 paint);
85 canvas->translate(0, SkIntToScalar(20));
86 }
87 }
88 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000089
reed@google.comde1837b2012-05-21 16:47:43 +000090 show_giant_dash(canvas);
egdaniel21402e32014-11-05 05:02:27 -080091 canvas->translate(0, SkIntToScalar(20));
92 show_zero_len_dash(canvas);
egdanielc00389e2015-10-05 08:11:49 -070093 canvas->translate(0, SkIntToScalar(20));
94 // Draw 0 on, 0 off dashed line
95 paint.setStrokeWidth(SkIntToScalar(8));
96 drawline(canvas, 0, 0, paint);
reed@google.com35a81df2012-05-04 21:49:27 +000097 }
reed@google.com21384df2012-05-18 17:59:08 +000098};
reed@google.com35a81df2012-05-04 21:49:27 +000099
reed@google.com21384df2012-05-18 17:59:08 +0000100///////////////////////////////////////////////////////////////////////////////
101
102static void make_unit_star(SkPath* path, int n) {
103 SkScalar rad = -SK_ScalarPI / 2;
104 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@google.com21384df2012-05-18 17:59:08 +0000106 path->moveTo(0, -SK_Scalar1);
107 for (int i = 1; i < n; i++) {
108 rad += drad;
109 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
110 path->lineTo(cosV, sinV);
111 }
112 path->close();
113}
114
115static void make_path_line(SkPath* path, const SkRect& bounds) {
116 path->moveTo(bounds.left(), bounds.top());
117 path->lineTo(bounds.right(), bounds.bottom());
118}
119
120static void make_path_rect(SkPath* path, const SkRect& bounds) {
121 path->addRect(bounds);
122}
123
124static void make_path_oval(SkPath* path, const SkRect& bounds) {
125 path->addOval(bounds);
126}
127
128static void make_path_star(SkPath* path, const SkRect& bounds) {
129 make_unit_star(path, 5);
130 SkMatrix matrix;
131 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
132 path->transform(matrix);
133}
134
135class Dashing2GM : public skiagm::GM {
136public:
137 Dashing2GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@google.com21384df2012-05-18 17:59:08 +0000139protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000140
reed@google.com21384df2012-05-18 17:59:08 +0000141 SkString onShortName() {
142 return SkString("dashing2");
143 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
tfarinaf5393182014-06-09 23:59:03 -0700145 SkISize onISize() { return SkISize::Make(640, 480); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000146
reed@google.com21384df2012-05-18 17:59:08 +0000147 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700148 constexpr int gIntervals[] = {
reed@google.com21384df2012-05-18 17:59:08 +0000149 3, // 3 dashes: each count [0] followed by intervals [1..count]
150 2, 10, 10,
151 4, 20, 5, 5, 5,
152 2, 2, 2
153 };
154
155 void (*gProc[])(SkPath*, const SkRect&) = {
156 make_path_line, make_path_rect, make_path_oval, make_path_star,
157 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000158
reed@google.com21384df2012-05-18 17:59:08 +0000159 SkPaint paint;
160 paint.setAntiAlias(true);
161 paint.setStyle(SkPaint::kStroke_Style);
162 paint.setStrokeWidth(SkIntToScalar(6));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000163
reed@google.com21384df2012-05-18 17:59:08 +0000164 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
165 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
166 SkScalar dx = bounds.width() * 4 / 3;
167 SkScalar dy = bounds.height() * 4 / 3;
168
169 const int* intervals = &gIntervals[1];
170 for (int y = 0; y < gIntervals[0]; ++y) {
171 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
172 int count = *intervals++;
173 for (int i = 0; i < count; ++i) {
174 vals[i] = SkIntToScalar(*intervals++);
175 }
176 SkScalar phase = vals[0] / 2;
reeda4393342016-03-18 11:22:57 -0700177 paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178
reed@google.com21384df2012-05-18 17:59:08 +0000179 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
180 SkPath path;
181 SkRect r = bounds;
182 r.offset(x * dx, y * dy);
183 gProc[x](&path, r);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184
reed@google.com21384df2012-05-18 17:59:08 +0000185 canvas->drawPath(path, paint);
186 }
187 }
188 }
reed@google.com35a81df2012-05-04 21:49:27 +0000189};
190
191//////////////////////////////////////////////////////////////////////////////
192
robertphillips@google.com629ab542012-11-28 17:18:11 +0000193// Test out the on/off line dashing Chrome if fond of
194class Dashing3GM : public skiagm::GM {
195public:
196 Dashing3GM() {}
197
198protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000199
robertphillips@google.com629ab542012-11-28 17:18:11 +0000200 SkString onShortName() {
201 return SkString("dashing3");
202 }
203
tfarinaf5393182014-06-09 23:59:03 -0700204 SkISize onISize() { return SkISize::Make(640, 480); }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000205
206 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
207 // while the vertical ones are AA.
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000208 void drawDashedLines(SkCanvas* canvas,
209 SkScalar lineLength,
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000210 SkScalar phase,
211 SkScalar dashLength,
212 int strokeWidth,
213 bool circles) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000214 SkPaint p;
215 p.setColor(SK_ColorBLACK);
216 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000217 p.setStrokeWidth(SkIntToScalar(strokeWidth));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000218
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000219 if (circles) {
220 p.setStrokeCap(SkPaint::kRound_Cap);
221 }
222
223 SkScalar intervals[2] = { dashLength, dashLength };
robertphillips@google.com629ab542012-11-28 17:18:11 +0000224
reeda4393342016-03-18 11:22:57 -0700225 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000226
227 SkPoint pts[2];
228
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000229 for (int y = 0; y < 100; y += 10*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000230 pts[0].set(0, SkIntToScalar(y));
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000231 pts[1].set(lineLength, SkIntToScalar(y));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000232
233 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
234 }
235
236 p.setAntiAlias(true);
237
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000238 for (int x = 0; x < 100; x += 14*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000239 pts[0].set(SkIntToScalar(x), 0);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000240 pts[1].set(SkIntToScalar(x), lineLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000241
242 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
243 }
244 }
245
246 virtual void onDraw(SkCanvas* canvas) {
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000247 // 1on/1off 1x1 squares with phase of 0 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000248 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000249 canvas->translate(2, 0);
250 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000251 canvas->restore();
252
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000253 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
robertphillips@google.com629ab542012-11-28 17:18:11 +0000254 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000255 canvas->translate(112, 0);
256 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000257 canvas->restore();
258
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000259 // 1on/1off 1x1 squares with phase of 1 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000260 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000261 canvas->translate(222, 0);
262 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000263 canvas->restore();
264
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000265 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000266 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000267 canvas->translate(332, 0);
reed@google.com140d7282013-01-07 20:25:04 +0000268 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000269 canvas->restore();
270
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000271 // 255on/255off 1x1 squares with phase of 0 - rects fast path
272 canvas->save();
273 canvas->translate(446, 0);
274 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
275 canvas->restore();
276
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000277 // 1on/1off 3x3 squares with phase of 0 - points fast path
278 canvas->save();
279 canvas->translate(2, 110);
280 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
281 canvas->restore();
282
283 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
284 canvas->save();
285 canvas->translate(112, 110);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000286 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000287 canvas->restore();
288
289 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
290 canvas->save();
291 canvas->translate(2, 220);
292 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
293 canvas->restore();
294
295 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
296 canvas->save();
297 canvas->translate(112, 220);
298 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
299 canvas->restore();
300
301 // 1on/1off 1x1 squares with rotation - should break fast path
302 canvas->save();
303 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000304 canvas->rotate(45);
305 canvas->translate(-50, -50);
306
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000307 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000308 canvas->restore();
309
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000310 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
311 for (int phase = 0; phase <= 3; ++phase) {
312 canvas->save();
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000313 canvas->translate(SkIntToScalar(phase*110+2),
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000314 SkIntToScalar(330));
315 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
316 canvas->restore();
317 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000318 }
319
320};
321
322//////////////////////////////////////////////////////////////////////////////
323
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000324class Dashing4GM : public skiagm::GM {
325public:
326 Dashing4GM() {}
327
328protected:
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000329
330 SkString onShortName() {
331 return SkString("dashing4");
332 }
333
tfarinaf5393182014-06-09 23:59:03 -0700334 SkISize onISize() { return SkISize::Make(640, 950); }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000335
336 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700337 constexpr struct {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000338 int fOnInterval;
339 int fOffInterval;
340 } gData[] = {
341 { 1, 1 },
342 { 4, 2 },
343 { 0, 4 }, // test for zero length on interval
344 };
345
346 SkPaint paint;
347 paint.setStyle(SkPaint::kStroke_Style);
348
349 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
350 canvas->translate(0, SK_ScalarHalf);
351
352 for (int width = 0; width <= 2; ++width) {
353 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
354 for (int aa = 0; aa <= 1; ++aa) {
355 for (int cap = 0; cap <= 1; ++cap) {
356 int w = width * width * width;
357 paint.setAntiAlias(SkToBool(aa));
358 paint.setStrokeWidth(SkIntToScalar(w));
359
360 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap)
361 : paint.setStrokeCap(SkPaint::kRound_Cap);
362
363 int scale = w ? w : 1;
364
365 drawline(canvas, gData[data].fOnInterval * scale,
366 gData[data].fOffInterval * scale,
367 paint);
368 canvas->translate(0, SkIntToScalar(20));
369 }
370 }
371 }
372 }
373
374 for (int aa = 0; aa <= 1; ++aa) {
375 paint.setAntiAlias(SkToBool(aa));
376 paint.setStrokeWidth(8.f);
377 paint.setStrokeCap(SkPaint::kSquare_Cap);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000378 // Single dash element that is cut off at start and end
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000379 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000380 canvas->translate(0, SkIntToScalar(20));
381
382 // Two dash elements where each one is cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000383 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000384 canvas->translate(0, SkIntToScalar(20));
385
386 // Many dash elements where first and last are cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000387 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000388 canvas->translate(0, SkIntToScalar(20));
389
390 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
391 // a canvas rotation)
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000392 drawline(canvas, 32, 16, paint, 600.f, 30.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000393 canvas->translate(0, SkIntToScalar(20));
394
395 // Case where only the off interval exists on the line. Thus nothing should be drawn
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000396 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000397 canvas->translate(0, SkIntToScalar(20));
398 }
399 }
400};
401
402//////////////////////////////////////////////////////////////////////////////
403
robertphillips9f2251c2014-11-04 13:33:50 -0800404class Dashing5GM : public skiagm::GM {
405public:
406 Dashing5GM(bool doAA) : fDoAA(doAA) {}
reed@google.com35a81df2012-05-04 21:49:27 +0000407
robertphillips9f2251c2014-11-04 13:33:50 -0800408protected:
robertphillips9f2251c2014-11-04 13:33:50 -0800409
mtklein36352bf2015-03-25 18:17:31 -0700410 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800411
mtklein36352bf2015-03-25 18:17:31 -0700412 SkString onShortName() override {
robertphillips9f2251c2014-11-04 13:33:50 -0800413 if (fDoAA) {
414 return SkString("dashing5_aa");
415 } else {
416 return SkString("dashing5_bw");
417 }
418 }
419
mtklein36352bf2015-03-25 18:17:31 -0700420 SkISize onISize() override { return SkISize::Make(400, 200); }
robertphillips9f2251c2014-11-04 13:33:50 -0800421
mtklein36352bf2015-03-25 18:17:31 -0700422 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700423 constexpr int kOn = 4;
424 constexpr int kOff = 4;
425 constexpr int kIntervalLength = kOn + kOff;
robertphillips9f2251c2014-11-04 13:33:50 -0800426
mtkleindbfd7ab2016-09-01 11:24:54 -0700427 constexpr SkColor gColors[kIntervalLength] = {
robertphillips9f2251c2014-11-04 13:33:50 -0800428 SK_ColorRED,
429 SK_ColorGREEN,
430 SK_ColorBLUE,
431 SK_ColorCYAN,
432 SK_ColorMAGENTA,
433 SK_ColorYELLOW,
434 SK_ColorGRAY,
435 SK_ColorDKGRAY
436 };
437
438 SkPaint paint;
439 paint.setStyle(SkPaint::kStroke_Style);
440
441 paint.setAntiAlias(fDoAA);
442
443 SkMatrix rot;
444 rot.setRotate(90);
445 SkASSERT(rot.rectStaysRect());
446
447 canvas->concat(rot);
448
449 int sign; // used to toggle the direction of the lines
450 int phase = 0;
451
452 for (int x = 0; x < 200; x += 10) {
453 paint.setStrokeWidth(SkIntToScalar(phase+1));
454 paint.setColor(gColors[phase]);
455 sign = (x % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700456 drawline(canvas, kOn, kOff, paint,
457 SkIntToScalar(x), -sign * SkIntToScalar(10003),
robertphillips9f2251c2014-11-04 13:33:50 -0800458 SkIntToScalar(phase),
459 SkIntToScalar(x), sign * SkIntToScalar(10003));
460 phase = (phase + 1) % kIntervalLength;
461 }
462
463 for (int y = -400; y < 0; y += 10) {
464 paint.setStrokeWidth(SkIntToScalar(phase+1));
465 paint.setColor(gColors[phase]);
466 sign = (y % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700467 drawline(canvas, kOn, kOff, paint,
468 -sign * SkIntToScalar(10003), SkIntToScalar(y),
robertphillips9f2251c2014-11-04 13:33:50 -0800469 SkIntToScalar(phase),
470 sign * SkIntToScalar(10003), SkIntToScalar(y));
471 phase = (phase + 1) % kIntervalLength;
472 }
473 }
474
475private:
476 bool fDoAA;
477};
478
reed6dc14aa2016-04-11 07:46:38 -0700479DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) {
caryclarkf97aa742015-12-18 07:03:13 -0800480 SkPath lines;
481 for (int x = 32; x < 256; x += 16) {
482 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
483 SkPoint pts[2] = {
484 { 256 + (float) sin(a) * x,
485 256 + (float) cos(a) * x },
486 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
487 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
488 };
489 lines.moveTo(pts[0]);
490 for (SkScalar i = 0; i < 1; i += 0.05f) {
491 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
492 pts[0].fY * (1 - i) + pts[1].fY * i);
493 }
494 }
495 }
496 SkPaint p;
497 p.setAntiAlias(true);
498 p.setStyle(SkPaint::kStroke_Style);
499 p.setStrokeWidth(1);
500 const SkScalar intervals[] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -0700501 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
reed6dc14aa2016-04-11 07:46:38 -0700502
503 canvas->translate(50, 50);
caryclarkf97aa742015-12-18 07:03:13 -0800504 canvas->drawPath(lines, p);
505}
506
caryclark70e6d602016-01-30 10:11:21 -0800507DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) {
508 SkPaint p;
509 p.setAntiAlias(true);
510 p.setStyle(SkPaint::kStroke_Style);
511 p.setStrokeWidth(80);
512
513 const SkScalar intervals[] = { 2, 2 };
reeda4393342016-03-18 11:22:57 -0700514 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
caryclark70e6d602016-01-30 10:11:21 -0800515 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
516}
517
518DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) {
519 SkPaint p;
520 p.setAntiAlias(true);
521 p.setStyle(SkPaint::kStroke_Style);
522 p.setStrokeWidth(2);
523
524 SkPath wavy;
525 wavy.moveTo(-10000, 100);
526 for (SkScalar i = -10000; i < 10000; i += 20) {
527 wavy.quadTo(i + 5, 95, i + 10, 100);
528 wavy.quadTo(i + 15, 105, i + 20, 100);
529 }
530 canvas->drawPath(wavy, p);
531}
532
caryclarkd7ea92f2016-03-16 07:34:02 -0700533DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
534 SkPaint p;
535 p.setAntiAlias(true);
536 p.setStyle(SkPaint::kStroke_Style);
537 p.setStrokeWidth(10);
538 p.setStrokeCap(SkPaint::kRound_Cap);
caryclark1aaadbd2016-03-17 07:01:49 -0700539 p.setStrokeJoin(SkPaint::kRound_Join);
caryclarkd7ea92f2016-03-16 07:34:02 -0700540 p.setTextSize(100);
541 p.setARGB(0xff, 0xbb, 0x00, 0x00);
caryclark1aaadbd2016-03-17 07:01:49 -0700542 sk_tool_utils::set_portable_typeface(&p);
caryclarkd7ea92f2016-03-16 07:34:02 -0700543 const SkScalar intervals[] = { 12, 12 };
reeda4393342016-03-18 11:22:57 -0700544 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
caryclarkd7ea92f2016-03-16 07:34:02 -0700545 canvas->drawText("Sausages", 8, 10, 90, p);
546 canvas->drawLine(8, 120, 456, 120, p);
547}
548
robertphillips9f2251c2014-11-04 13:33:50 -0800549//////////////////////////////////////////////////////////////////////////////
550
halcanary385fe4d2015-08-26 13:07:48 -0700551DEF_GM(return new DashingGM;)
552DEF_GM(return new Dashing2GM;)
553DEF_GM(return new Dashing3GM;)
554DEF_GM(return new Dashing4GM;)
555DEF_GM(return new Dashing5GM(true);)
556DEF_GM(return new Dashing5GM(false);)