blob: 9a5268949f528c52f92a0b44b8d92bc3a11a5fab [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@google.com35a81df2012-05-04 21:49:27 +000010#include "SkCanvas.h"
11#include "SkPaint.h"
12#include "SkDashPathEffect.h"
13
reed@google.comde1837b2012-05-21 16:47:43 +000014static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +000015 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
halcanary9d524f22016-03-29 09:03:52 -070016 SkScalar phase = SkIntToScalar(0),
robertphillips9f2251c2014-11-04 13:33:50 -080017 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
reed@google.com35a81df2012-05-04 21:49:27 +000018 SkPaint p(paint);
19
20 const SkScalar intervals[] = {
21 SkIntToScalar(on),
22 SkIntToScalar(off),
23 };
24
reeda4393342016-03-18 11:22:57 -070025 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips9f2251c2014-11-04 13:33:50 -080026 canvas->drawLine(startX, startY, finalX, finalY, p);
reed@google.comde1837b2012-05-21 16:47:43 +000027}
28
29// earlier bug stopped us from drawing very long single-segment dashes, because
30// SkPathMeasure was skipping very small delta-T values (nearlyzero). This is
31// now fixes, so this giant dash should appear.
32static void show_giant_dash(SkCanvas* canvas) {
33 SkPaint paint;
34
35 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
reed@google.com35a81df2012-05-04 21:49:27 +000036}
37
egdaniel21402e32014-11-05 05:02:27 -080038static void show_zero_len_dash(SkCanvas* canvas) {
39 SkPaint paint;
40
41 drawline(canvas, 2, 2, paint, SkIntToScalar(0));
42 paint.setStyle(SkPaint::kStroke_Style);
43 paint.setStrokeWidth(SkIntToScalar(2));
44 canvas->translate(0, SkIntToScalar(20));
45 drawline(canvas, 4, 4, paint, SkIntToScalar(0));
46}
47
reed@google.com21384df2012-05-18 17:59:08 +000048class DashingGM : public skiagm::GM {
reed@google.com35a81df2012-05-04 21:49:27 +000049public:
50 DashingGM() {}
51
52protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000053
reed@google.com35a81df2012-05-04 21:49:27 +000054 SkString onShortName() {
55 return SkString("dashing");
56 }
57
egdanielc00389e2015-10-05 08:11:49 -070058 SkISize onISize() { return SkISize::Make(640, 340); }
reed@google.com35a81df2012-05-04 21:49:27 +000059
60 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -070061 constexpr struct {
reed@google.com35a81df2012-05-04 21:49:27 +000062 int fOnInterval;
63 int fOffInterval;
64 } gData[] = {
65 { 1, 1 },
66 { 4, 1 },
67 };
rmistry@google.comae933ce2012-08-23 18:19:56 +000068
reed@google.com35a81df2012-05-04 21:49:27 +000069 SkPaint paint;
70 paint.setStyle(SkPaint::kStroke_Style);
71
72 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
73 canvas->translate(0, SK_ScalarHalf);
reed@google.com35a81df2012-05-04 21:49:27 +000074 for (int width = 0; width <= 2; ++width) {
75 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
76 for (int aa = 0; aa <= 1; ++aa) {
77 int w = width * width * width;
78 paint.setAntiAlias(SkToBool(aa));
79 paint.setStrokeWidth(SkIntToScalar(w));
rmistry@google.comae933ce2012-08-23 18:19:56 +000080
reed@google.com35a81df2012-05-04 21:49:27 +000081 int scale = w ? w : 1;
82
83 drawline(canvas, gData[data].fOnInterval * scale,
84 gData[data].fOffInterval * scale,
85 paint);
86 canvas->translate(0, SkIntToScalar(20));
87 }
88 }
89 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000090
reed@google.comde1837b2012-05-21 16:47:43 +000091 show_giant_dash(canvas);
egdaniel21402e32014-11-05 05:02:27 -080092 canvas->translate(0, SkIntToScalar(20));
93 show_zero_len_dash(canvas);
egdanielc00389e2015-10-05 08:11:49 -070094 canvas->translate(0, SkIntToScalar(20));
95 // Draw 0 on, 0 off dashed line
96 paint.setStrokeWidth(SkIntToScalar(8));
97 drawline(canvas, 0, 0, paint);
reed@google.com35a81df2012-05-04 21:49:27 +000098 }
reed@google.com21384df2012-05-18 17:59:08 +000099};
reed@google.com35a81df2012-05-04 21:49:27 +0000100
reed@google.com21384df2012-05-18 17:59:08 +0000101///////////////////////////////////////////////////////////////////////////////
102
103static void make_unit_star(SkPath* path, int n) {
104 SkScalar rad = -SK_ScalarPI / 2;
105 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@google.com21384df2012-05-18 17:59:08 +0000107 path->moveTo(0, -SK_Scalar1);
108 for (int i = 1; i < n; i++) {
109 rad += drad;
110 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
111 path->lineTo(cosV, sinV);
112 }
113 path->close();
114}
115
116static void make_path_line(SkPath* path, const SkRect& bounds) {
117 path->moveTo(bounds.left(), bounds.top());
118 path->lineTo(bounds.right(), bounds.bottom());
119}
120
121static void make_path_rect(SkPath* path, const SkRect& bounds) {
122 path->addRect(bounds);
123}
124
125static void make_path_oval(SkPath* path, const SkRect& bounds) {
126 path->addOval(bounds);
127}
128
129static void make_path_star(SkPath* path, const SkRect& bounds) {
130 make_unit_star(path, 5);
131 SkMatrix matrix;
132 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
133 path->transform(matrix);
134}
135
136class Dashing2GM : public skiagm::GM {
137public:
138 Dashing2GM() {}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000139
reed@google.com21384df2012-05-18 17:59:08 +0000140protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000141
reed@google.com21384df2012-05-18 17:59:08 +0000142 SkString onShortName() {
143 return SkString("dashing2");
144 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000145
tfarinaf5393182014-06-09 23:59:03 -0700146 SkISize onISize() { return SkISize::Make(640, 480); }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000147
reed@google.com21384df2012-05-18 17:59:08 +0000148 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700149 constexpr int gIntervals[] = {
reed@google.com21384df2012-05-18 17:59:08 +0000150 3, // 3 dashes: each count [0] followed by intervals [1..count]
151 2, 10, 10,
152 4, 20, 5, 5, 5,
153 2, 2, 2
154 };
155
156 void (*gProc[])(SkPath*, const SkRect&) = {
157 make_path_line, make_path_rect, make_path_oval, make_path_star,
158 };
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159
reed@google.com21384df2012-05-18 17:59:08 +0000160 SkPaint paint;
161 paint.setAntiAlias(true);
162 paint.setStyle(SkPaint::kStroke_Style);
163 paint.setStrokeWidth(SkIntToScalar(6));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000164
reed@google.com21384df2012-05-18 17:59:08 +0000165 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
166 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
167 SkScalar dx = bounds.width() * 4 / 3;
168 SkScalar dy = bounds.height() * 4 / 3;
169
170 const int* intervals = &gIntervals[1];
171 for (int y = 0; y < gIntervals[0]; ++y) {
172 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
173 int count = *intervals++;
174 for (int i = 0; i < count; ++i) {
175 vals[i] = SkIntToScalar(*intervals++);
176 }
177 SkScalar phase = vals[0] / 2;
reeda4393342016-03-18 11:22:57 -0700178 paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase));
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179
reed@google.com21384df2012-05-18 17:59:08 +0000180 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
181 SkPath path;
182 SkRect r = bounds;
183 r.offset(x * dx, y * dy);
184 gProc[x](&path, r);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185
reed@google.com21384df2012-05-18 17:59:08 +0000186 canvas->drawPath(path, paint);
187 }
188 }
189 }
reed@google.com35a81df2012-05-04 21:49:27 +0000190};
191
192//////////////////////////////////////////////////////////////////////////////
193
robertphillips@google.com629ab542012-11-28 17:18:11 +0000194// Test out the on/off line dashing Chrome if fond of
195class Dashing3GM : public skiagm::GM {
196public:
197 Dashing3GM() {}
198
199protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +0000200
robertphillips@google.com629ab542012-11-28 17:18:11 +0000201 SkString onShortName() {
202 return SkString("dashing3");
203 }
204
tfarinaf5393182014-06-09 23:59:03 -0700205 SkISize onISize() { return SkISize::Make(640, 480); }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000206
207 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
208 // while the vertical ones are AA.
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000209 void drawDashedLines(SkCanvas* canvas,
210 SkScalar lineLength,
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000211 SkScalar phase,
212 SkScalar dashLength,
213 int strokeWidth,
214 bool circles) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000215 SkPaint p;
216 p.setColor(SK_ColorBLACK);
217 p.setStyle(SkPaint::kStroke_Style);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000218 p.setStrokeWidth(SkIntToScalar(strokeWidth));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000219
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000220 if (circles) {
221 p.setStrokeCap(SkPaint::kRound_Cap);
222 }
223
224 SkScalar intervals[2] = { dashLength, dashLength };
robertphillips@google.com629ab542012-11-28 17:18:11 +0000225
reeda4393342016-03-18 11:22:57 -0700226 p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000227
228 SkPoint pts[2];
229
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000230 for (int y = 0; y < 100; y += 10*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000231 pts[0].set(0, SkIntToScalar(y));
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000232 pts[1].set(lineLength, SkIntToScalar(y));
robertphillips@google.com629ab542012-11-28 17:18:11 +0000233
234 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
235 }
236
237 p.setAntiAlias(true);
238
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000239 for (int x = 0; x < 100; x += 14*strokeWidth) {
robertphillips@google.com629ab542012-11-28 17:18:11 +0000240 pts[0].set(SkIntToScalar(x), 0);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000241 pts[1].set(SkIntToScalar(x), lineLength);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000242
243 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
244 }
245 }
246
247 virtual void onDraw(SkCanvas* canvas) {
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000248 // 1on/1off 1x1 squares with phase of 0 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000249 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000250 canvas->translate(2, 0);
251 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000252 canvas->restore();
253
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000254 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
robertphillips@google.com629ab542012-11-28 17:18:11 +0000255 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000256 canvas->translate(112, 0);
257 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000258 canvas->restore();
259
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000260 // 1on/1off 1x1 squares with phase of 1 - points fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000261 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000262 canvas->translate(222, 0);
263 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000264 canvas->restore();
265
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000266 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
robertphillips@google.com629ab542012-11-28 17:18:11 +0000267 canvas->save();
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000268 canvas->translate(332, 0);
reed@google.com140d7282013-01-07 20:25:04 +0000269 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000270 canvas->restore();
271
robertphillips@google.com5c4d5582013-01-15 12:53:31 +0000272 // 255on/255off 1x1 squares with phase of 0 - rects fast path
273 canvas->save();
274 canvas->translate(446, 0);
275 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
276 canvas->restore();
277
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000278 // 1on/1off 3x3 squares with phase of 0 - points fast path
279 canvas->save();
280 canvas->translate(2, 110);
281 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
282 canvas->restore();
283
284 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
285 canvas->save();
286 canvas->translate(112, 110);
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000287 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000288 canvas->restore();
289
290 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
291 canvas->save();
292 canvas->translate(2, 220);
293 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
294 canvas->restore();
295
296 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
297 canvas->save();
298 canvas->translate(112, 220);
299 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
300 canvas->restore();
301
302 // 1on/1off 1x1 squares with rotation - should break fast path
303 canvas->save();
304 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000305 canvas->rotate(45);
306 canvas->translate(-50, -50);
307
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000308 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
robertphillips@google.com629ab542012-11-28 17:18:11 +0000309 canvas->restore();
310
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000311 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
312 for (int phase = 0; phase <= 3; ++phase) {
313 canvas->save();
skia.committer@gmail.com73b140a2012-12-05 02:01:21 +0000314 canvas->translate(SkIntToScalar(phase*110+2),
robertphillips@google.com8c685c52012-12-04 20:34:11 +0000315 SkIntToScalar(330));
316 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
317 canvas->restore();
318 }
robertphillips@google.com629ab542012-11-28 17:18:11 +0000319 }
320
321};
322
323//////////////////////////////////////////////////////////////////////////////
324
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000325class Dashing4GM : public skiagm::GM {
326public:
327 Dashing4GM() {}
328
329protected:
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000330
331 SkString onShortName() {
332 return SkString("dashing4");
333 }
334
Greg Danielc96f9b52017-12-07 15:00:06 -0500335 SkISize onISize() { return SkISize::Make(640, 1100); }
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000336
337 virtual void onDraw(SkCanvas* canvas) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700338 constexpr struct {
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000339 int fOnInterval;
340 int fOffInterval;
341 } gData[] = {
342 { 1, 1 },
343 { 4, 2 },
344 { 0, 4 }, // test for zero length on interval
345 };
346
347 SkPaint paint;
348 paint.setStyle(SkPaint::kStroke_Style);
349
350 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
Greg Daniel95581bb2017-06-30 10:36:38 -0400351 canvas->translate(SK_ScalarHalf, SK_ScalarHalf);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000352
353 for (int width = 0; width <= 2; ++width) {
354 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
355 for (int aa = 0; aa <= 1; ++aa) {
356 for (int cap = 0; cap <= 1; ++cap) {
357 int w = width * width * width;
358 paint.setAntiAlias(SkToBool(aa));
359 paint.setStrokeWidth(SkIntToScalar(w));
360
361 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap)
362 : paint.setStrokeCap(SkPaint::kRound_Cap);
363
364 int scale = w ? w : 1;
365
366 drawline(canvas, gData[data].fOnInterval * scale,
367 gData[data].fOffInterval * scale,
368 paint);
369 canvas->translate(0, SkIntToScalar(20));
370 }
371 }
372 }
373 }
374
375 for (int aa = 0; aa <= 1; ++aa) {
376 paint.setAntiAlias(SkToBool(aa));
377 paint.setStrokeWidth(8.f);
378 paint.setStrokeCap(SkPaint::kSquare_Cap);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000379 // Single dash element that is cut off at start and end
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000380 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000381 canvas->translate(0, SkIntToScalar(20));
382
383 // Two dash elements where each one is cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000384 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000385 canvas->translate(0, SkIntToScalar(20));
386
387 // Many dash elements where first and last are cut off at beginning and end respectively
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000388 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000389 canvas->translate(0, SkIntToScalar(20));
390
391 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
392 // a canvas rotation)
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000393 drawline(canvas, 32, 16, paint, 600.f, 30.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000394 canvas->translate(0, SkIntToScalar(20));
395
396 // Case where only the off interval exists on the line. Thus nothing should be drawn
commit-bot@chromium.org71db8822014-05-19 14:59:04 +0000397 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000398 canvas->translate(0, SkIntToScalar(20));
399 }
Greg Daniel5fb30562017-06-29 12:27:48 -0400400
401 // Test overlapping circles.
402 canvas->translate(SkIntToScalar(5), SkIntToScalar(20));
403 paint.setAntiAlias(true);
404 paint.setStrokeCap(SkPaint::kRound_Cap);
405 paint.setColor(0x44000000);
406 paint.setStrokeWidth(40);
407 drawline(canvas, 0, 30, paint);
408
409 canvas->translate(0, SkIntToScalar(50));
410 paint.setStrokeCap(SkPaint::kSquare_Cap);
411 drawline(canvas, 0, 30, paint);
Greg Danielc96f9b52017-12-07 15:00:06 -0500412
413 // Test we draw the cap when the line length is zero.
414 canvas->translate(0, SkIntToScalar(50));
415 paint.setStrokeCap(SkPaint::kRound_Cap);
416 paint.setColor(0xFF000000);
417 paint.setStrokeWidth(11);
418 drawline(canvas, 0, 30, paint, 0);
419
420 canvas->translate(SkIntToScalar(100), 0);
421 drawline(canvas, 1, 30, paint, 0);
commit-bot@chromium.org628ed0b2014-05-19 14:32:49 +0000422 }
423};
424
425//////////////////////////////////////////////////////////////////////////////
426
robertphillips9f2251c2014-11-04 13:33:50 -0800427class Dashing5GM : public skiagm::GM {
428public:
429 Dashing5GM(bool doAA) : fDoAA(doAA) {}
reed@google.com35a81df2012-05-04 21:49:27 +0000430
robertphillips9f2251c2014-11-04 13:33:50 -0800431protected:
robertphillips9f2251c2014-11-04 13:33:50 -0800432
mtklein36352bf2015-03-25 18:17:31 -0700433 bool runAsBench() const override { return true; }
mtkleincf5d9c92015-01-23 10:31:45 -0800434
mtklein36352bf2015-03-25 18:17:31 -0700435 SkString onShortName() override {
robertphillips9f2251c2014-11-04 13:33:50 -0800436 if (fDoAA) {
437 return SkString("dashing5_aa");
438 } else {
439 return SkString("dashing5_bw");
440 }
441 }
442
mtklein36352bf2015-03-25 18:17:31 -0700443 SkISize onISize() override { return SkISize::Make(400, 200); }
robertphillips9f2251c2014-11-04 13:33:50 -0800444
mtklein36352bf2015-03-25 18:17:31 -0700445 void onDraw(SkCanvas* canvas) override {
mtkleindbfd7ab2016-09-01 11:24:54 -0700446 constexpr int kOn = 4;
447 constexpr int kOff = 4;
448 constexpr int kIntervalLength = kOn + kOff;
robertphillips9f2251c2014-11-04 13:33:50 -0800449
mtkleindbfd7ab2016-09-01 11:24:54 -0700450 constexpr SkColor gColors[kIntervalLength] = {
robertphillips9f2251c2014-11-04 13:33:50 -0800451 SK_ColorRED,
452 SK_ColorGREEN,
453 SK_ColorBLUE,
454 SK_ColorCYAN,
455 SK_ColorMAGENTA,
456 SK_ColorYELLOW,
457 SK_ColorGRAY,
458 SK_ColorDKGRAY
459 };
460
461 SkPaint paint;
462 paint.setStyle(SkPaint::kStroke_Style);
463
464 paint.setAntiAlias(fDoAA);
465
466 SkMatrix rot;
467 rot.setRotate(90);
468 SkASSERT(rot.rectStaysRect());
469
470 canvas->concat(rot);
471
472 int sign; // used to toggle the direction of the lines
473 int phase = 0;
474
475 for (int x = 0; x < 200; x += 10) {
476 paint.setStrokeWidth(SkIntToScalar(phase+1));
477 paint.setColor(gColors[phase]);
478 sign = (x % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700479 drawline(canvas, kOn, kOff, paint,
480 SkIntToScalar(x), -sign * SkIntToScalar(10003),
robertphillips9f2251c2014-11-04 13:33:50 -0800481 SkIntToScalar(phase),
482 SkIntToScalar(x), sign * SkIntToScalar(10003));
483 phase = (phase + 1) % kIntervalLength;
484 }
485
486 for (int y = -400; y < 0; y += 10) {
487 paint.setStrokeWidth(SkIntToScalar(phase+1));
488 paint.setColor(gColors[phase]);
489 sign = (y % 20) ? 1 : -1;
halcanary9d524f22016-03-29 09:03:52 -0700490 drawline(canvas, kOn, kOff, paint,
491 -sign * SkIntToScalar(10003), SkIntToScalar(y),
robertphillips9f2251c2014-11-04 13:33:50 -0800492 SkIntToScalar(phase),
493 sign * SkIntToScalar(10003), SkIntToScalar(y));
494 phase = (phase + 1) % kIntervalLength;
495 }
496 }
497
498private:
499 bool fDoAA;
500};
501
reed6dc14aa2016-04-11 07:46:38 -0700502DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) {
caryclarkf97aa742015-12-18 07:03:13 -0800503 SkPath lines;
504 for (int x = 32; x < 256; x += 16) {
505 for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) {
506 SkPoint pts[2] = {
507 { 256 + (float) sin(a) * x,
508 256 + (float) cos(a) * x },
509 { 256 + (float) sin(a + 3.141592 / 3) * (x + 64),
510 256 + (float) cos(a + 3.141592 / 3) * (x + 64) }
511 };
512 lines.moveTo(pts[0]);
513 for (SkScalar i = 0; i < 1; i += 0.05f) {
514 lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i,
515 pts[0].fY * (1 - i) + pts[1].fY * i);
516 }
517 }
518 }
519 SkPaint p;
520 p.setAntiAlias(true);
521 p.setStyle(SkPaint::kStroke_Style);
522 p.setStrokeWidth(1);
523 const SkScalar intervals[] = { 1, 1 };
reeda4393342016-03-18 11:22:57 -0700524 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
Ben Wagner63fd7602017-10-09 15:45:33 -0400525
reed6dc14aa2016-04-11 07:46:38 -0700526 canvas->translate(50, 50);
caryclarkf97aa742015-12-18 07:03:13 -0800527 canvas->drawPath(lines, p);
528}
529
caryclark70e6d602016-01-30 10:11:21 -0800530DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) {
531 SkPaint p;
532 p.setAntiAlias(true);
533 p.setStyle(SkPaint::kStroke_Style);
534 p.setStrokeWidth(80);
535
536 const SkScalar intervals[] = { 2, 2 };
reeda4393342016-03-18 11:22:57 -0700537 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
caryclark70e6d602016-01-30 10:11:21 -0800538 canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p);
539}
540
541DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) {
542 SkPaint p;
543 p.setAntiAlias(true);
544 p.setStyle(SkPaint::kStroke_Style);
545 p.setStrokeWidth(2);
546
547 SkPath wavy;
548 wavy.moveTo(-10000, 100);
549 for (SkScalar i = -10000; i < 10000; i += 20) {
550 wavy.quadTo(i + 5, 95, i + 10, 100);
551 wavy.quadTo(i + 15, 105, i + 20, 100);
552 }
553 canvas->drawPath(wavy, p);
554}
555
caryclarkd7ea92f2016-03-16 07:34:02 -0700556DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) {
557 SkPaint p;
558 p.setAntiAlias(true);
559 p.setStyle(SkPaint::kStroke_Style);
560 p.setStrokeWidth(10);
561 p.setStrokeCap(SkPaint::kRound_Cap);
caryclark1aaadbd2016-03-17 07:01:49 -0700562 p.setStrokeJoin(SkPaint::kRound_Join);
caryclarkd7ea92f2016-03-16 07:34:02 -0700563 p.setTextSize(100);
564 p.setARGB(0xff, 0xbb, 0x00, 0x00);
caryclark1aaadbd2016-03-17 07:01:49 -0700565 sk_tool_utils::set_portable_typeface(&p);
caryclarkd7ea92f2016-03-16 07:34:02 -0700566 const SkScalar intervals[] = { 12, 12 };
reeda4393342016-03-18 11:22:57 -0700567 p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0));
Cary Clark2a475ea2017-04-28 15:35:12 -0400568 canvas->drawString("Sausages", 10, 90, p);
caryclarkd7ea92f2016-03-16 07:34:02 -0700569 canvas->drawLine(8, 120, 456, 120, p);
570}
571
Brian Salomon72f78c32017-12-21 11:56:42 -0500572DEF_SIMPLE_GM(dash_line_zero_off_interval, canvas, 160, 330) {
573 static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f};
574 SkPaint dashPaint;
575 dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f));
576 SkASSERT(dashPaint.getPathEffect());
577 dashPaint.setStyle(SkPaint::kStroke_Style);
578 dashPaint.setStrokeWidth(20.f);
579 static constexpr struct {
580 SkPoint fA, fB;
581 } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal
582 {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical
583 {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point
584 {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal
585 SkScalar pad = 5.f + dashPaint.getStrokeWidth();
586 canvas->translate(pad / 2.f, pad / 2.f);
587 canvas->save();
588 SkScalar h = 0.f;
589 for (const auto& line : kLines) {
590 h = SkTMax(h, SkScalarAbs(line.fA.fY - line.fB.fY));
591 }
592 for (const auto& line : kLines) {
593 SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX);
594 for (auto cap : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) {
595 dashPaint.setStrokeCap(cap);
596 for (auto aa : {false, true}) {
597 dashPaint.setAntiAlias(aa);
598 canvas->drawLine(line.fA, line.fB, dashPaint);
599 canvas->translate(0.f, pad + h);
600 }
601 }
602 canvas->restore();
603 canvas->translate(pad + w, 0.f);
604 canvas->save();
605 }
606}
607
robertphillips9f2251c2014-11-04 13:33:50 -0800608//////////////////////////////////////////////////////////////////////////////
609
halcanary385fe4d2015-08-26 13:07:48 -0700610DEF_GM(return new DashingGM;)
611DEF_GM(return new Dashing2GM;)
612DEF_GM(return new Dashing3GM;)
613DEF_GM(return new Dashing4GM;)
614DEF_GM(return new Dashing5GM(true);)
615DEF_GM(return new Dashing5GM(false);)