2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkDashPathEffect.h"
13 static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint,
14 SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0),
15 SkScalar phase = SkIntToScalar(0),
16 SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) {
19 const SkScalar intervals[] = {
24 p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref();
25 canvas->drawLine(startX, startY, finalX, finalY, p);
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.
31 static void show_giant_dash(SkCanvas* canvas) {
34 drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000));
37 static void show_zero_len_dash(SkCanvas* canvas) {
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));
47 class DashingGM : public skiagm::GM {
53 SkString onShortName() {
54 return SkString("dashing");
57 SkISize onISize() { return SkISize::Make(640, 300); }
59 virtual void onDraw(SkCanvas* canvas) {
69 paint.setStyle(SkPaint::kStroke_Style);
71 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
72 canvas->translate(0, SK_ScalarHalf);
73 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));
80 int scale = w ? w : 1;
82 drawline(canvas, gData[data].fOnInterval * scale,
83 gData[data].fOffInterval * scale,
85 canvas->translate(0, SkIntToScalar(20));
90 show_giant_dash(canvas);
91 canvas->translate(0, SkIntToScalar(20));
92 show_zero_len_dash(canvas);
96 ///////////////////////////////////////////////////////////////////////////////
98 static void make_unit_star(SkPath* path, int n) {
99 SkScalar rad = -SK_ScalarPI / 2;
100 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
102 path->moveTo(0, -SK_Scalar1);
103 for (int i = 1; i < n; i++) {
105 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
106 path->lineTo(cosV, sinV);
111 static void make_path_line(SkPath* path, const SkRect& bounds) {
112 path->moveTo(bounds.left(), bounds.top());
113 path->lineTo(bounds.right(), bounds.bottom());
116 static void make_path_rect(SkPath* path, const SkRect& bounds) {
117 path->addRect(bounds);
120 static void make_path_oval(SkPath* path, const SkRect& bounds) {
121 path->addOval(bounds);
124 static void make_path_star(SkPath* path, const SkRect& bounds) {
125 make_unit_star(path, 5);
127 matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit);
128 path->transform(matrix);
131 class Dashing2GM : public skiagm::GM {
137 SkString onShortName() {
138 return SkString("dashing2");
141 SkISize onISize() { return SkISize::Make(640, 480); }
143 virtual void onDraw(SkCanvas* canvas) {
144 static const int gIntervals[] = {
145 3, // 3 dashes: each count [0] followed by intervals [1..count]
151 void (*gProc[])(SkPath*, const SkRect&) = {
152 make_path_line, make_path_rect, make_path_oval, make_path_star,
156 paint.setAntiAlias(true);
157 paint.setStyle(SkPaint::kStroke_Style);
158 paint.setStrokeWidth(SkIntToScalar(6));
160 SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120));
161 bounds.offset(SkIntToScalar(20), SkIntToScalar(20));
162 SkScalar dx = bounds.width() * 4 / 3;
163 SkScalar dy = bounds.height() * 4 / 3;
165 const int* intervals = &gIntervals[1];
166 for (int y = 0; y < gIntervals[0]; ++y) {
167 SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough
168 int count = *intervals++;
169 for (int i = 0; i < count; ++i) {
170 vals[i] = SkIntToScalar(*intervals++);
172 SkScalar phase = vals[0] / 2;
173 paint.setPathEffect(SkDashPathEffect::Create(vals, count, phase))->unref();
175 for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) {
178 r.offset(x * dx, y * dy);
181 canvas->drawPath(path, paint);
187 //////////////////////////////////////////////////////////////////////////////
189 // Test out the on/off line dashing Chrome if fond of
190 class Dashing3GM : public skiagm::GM {
196 SkString onShortName() {
197 return SkString("dashing3");
200 SkISize onISize() { return SkISize::Make(640, 480); }
202 // Draw a 100x100 block of dashed lines. The horizontal ones are BW
203 // while the vertical ones are AA.
204 void drawDashedLines(SkCanvas* canvas,
211 p.setColor(SK_ColorBLACK);
212 p.setStyle(SkPaint::kStroke_Style);
213 p.setStrokeWidth(SkIntToScalar(strokeWidth));
216 p.setStrokeCap(SkPaint::kRound_Cap);
219 SkScalar intervals[2] = { dashLength, dashLength };
221 p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref();
225 for (int y = 0; y < 100; y += 10*strokeWidth) {
226 pts[0].set(0, SkIntToScalar(y));
227 pts[1].set(lineLength, SkIntToScalar(y));
229 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
232 p.setAntiAlias(true);
234 for (int x = 0; x < 100; x += 14*strokeWidth) {
235 pts[0].set(SkIntToScalar(x), 0);
236 pts[1].set(SkIntToScalar(x), lineLength);
238 canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p);
242 virtual void onDraw(SkCanvas* canvas) {
243 // 1on/1off 1x1 squares with phase of 0 - points fastpath
245 canvas->translate(2, 0);
246 this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false);
249 // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares)
251 canvas->translate(112, 0);
252 this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false);
255 // 1on/1off 1x1 squares with phase of 1 - points fastpath
257 canvas->translate(222, 0);
258 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
261 // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath
263 canvas->translate(332, 0);
264 this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false);
267 // 255on/255off 1x1 squares with phase of 0 - rects fast path
269 canvas->translate(446, 0);
270 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false);
273 // 1on/1off 3x3 squares with phase of 0 - points fast path
275 canvas->translate(2, 110);
276 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false);
279 // 1on/1off 3x3 squares with phase of 1.5 - rects fast path
281 canvas->translate(112, 110);
282 this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false);
285 // 1on/1off 1x1 circles with phase of 1 - no fast path yet
287 canvas->translate(2, 220);
288 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true);
291 // 1on/1off 3x3 circles with phase of 1 - no fast path yet
293 canvas->translate(112, 220);
294 this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true);
297 // 1on/1off 1x1 squares with rotation - should break fast path
299 canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100);
301 canvas->translate(-50, -50);
303 this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false);
306 // 3on/3off 3x1 rects - should use rect fast path regardless of phase
307 for (int phase = 0; phase <= 3; ++phase) {
309 canvas->translate(SkIntToScalar(phase*110+2),
311 this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false);
318 //////////////////////////////////////////////////////////////////////////////
320 class Dashing4GM : public skiagm::GM {
326 SkString onShortName() {
327 return SkString("dashing4");
330 SkISize onISize() { return SkISize::Make(640, 950); }
332 virtual void onDraw(SkCanvas* canvas) {
333 static const struct {
339 { 0, 4 }, // test for zero length on interval
343 paint.setStyle(SkPaint::kStroke_Style);
345 canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
346 canvas->translate(0, SK_ScalarHalf);
348 for (int width = 0; width <= 2; ++width) {
349 for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) {
350 for (int aa = 0; aa <= 1; ++aa) {
351 for (int cap = 0; cap <= 1; ++cap) {
352 int w = width * width * width;
353 paint.setAntiAlias(SkToBool(aa));
354 paint.setStrokeWidth(SkIntToScalar(w));
356 SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap)
357 : paint.setStrokeCap(SkPaint::kRound_Cap);
359 int scale = w ? w : 1;
361 drawline(canvas, gData[data].fOnInterval * scale,
362 gData[data].fOffInterval * scale,
364 canvas->translate(0, SkIntToScalar(20));
370 for (int aa = 0; aa <= 1; ++aa) {
371 paint.setAntiAlias(SkToBool(aa));
372 paint.setStrokeWidth(8.f);
373 paint.setStrokeCap(SkPaint::kSquare_Cap);
374 // Single dash element that is cut off at start and end
375 drawline(canvas, 32, 16, paint, 20.f, 0, 5.f);
376 canvas->translate(0, SkIntToScalar(20));
378 // Two dash elements where each one is cut off at beginning and end respectively
379 drawline(canvas, 32, 16, paint, 56.f, 0, 5.f);
380 canvas->translate(0, SkIntToScalar(20));
382 // Many dash elements where first and last are cut off at beginning and end respectively
383 drawline(canvas, 32, 16, paint, 584.f, 0, 5.f);
384 canvas->translate(0, SkIntToScalar(20));
386 // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from
387 // a canvas rotation)
388 drawline(canvas, 32, 16, paint, 600.f, 30.f);
389 canvas->translate(0, SkIntToScalar(20));
391 // Case where only the off interval exists on the line. Thus nothing should be drawn
392 drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f);
393 canvas->translate(0, SkIntToScalar(20));
398 //////////////////////////////////////////////////////////////////////////////
400 class Dashing5GM : public skiagm::GM {
402 Dashing5GM(bool doAA) : fDoAA(doAA) {}
406 bool runAsBench() const SK_OVERRIDE { return true; }
408 SkString onShortName() SK_OVERRIDE {
410 return SkString("dashing5_aa");
412 return SkString("dashing5_bw");
416 SkISize onISize() SK_OVERRIDE { return SkISize::Make(400, 200); }
418 void onDraw(SkCanvas* canvas) SK_OVERRIDE {
419 static const int kOn = 4;
420 static const int kOff = 4;
421 static const int kIntervalLength = kOn + kOff;
423 static const SkColor gColors[kIntervalLength] = {
435 paint.setStyle(SkPaint::kStroke_Style);
437 paint.setAntiAlias(fDoAA);
441 SkASSERT(rot.rectStaysRect());
445 int sign; // used to toggle the direction of the lines
448 for (int x = 0; x < 200; x += 10) {
449 paint.setStrokeWidth(SkIntToScalar(phase+1));
450 paint.setColor(gColors[phase]);
451 sign = (x % 20) ? 1 : -1;
452 drawline(canvas, kOn, kOff, paint,
453 SkIntToScalar(x), -sign * SkIntToScalar(10003),
454 SkIntToScalar(phase),
455 SkIntToScalar(x), sign * SkIntToScalar(10003));
456 phase = (phase + 1) % kIntervalLength;
459 for (int y = -400; y < 0; y += 10) {
460 paint.setStrokeWidth(SkIntToScalar(phase+1));
461 paint.setColor(gColors[phase]);
462 sign = (y % 20) ? 1 : -1;
463 drawline(canvas, kOn, kOff, paint,
464 -sign * SkIntToScalar(10003), SkIntToScalar(y),
465 SkIntToScalar(phase),
466 sign * SkIntToScalar(10003), SkIntToScalar(y));
467 phase = (phase + 1) % kIntervalLength;
475 //////////////////////////////////////////////////////////////////////////////
477 DEF_GM(return SkNEW(DashingGM);)
478 DEF_GM(return SkNEW(Dashing2GM);)
479 DEF_GM(return SkNEW(Dashing3GM);)
480 DEF_GM(return SkNEW(Dashing4GM);)
481 DEF_GM(return SkNEW_ARGS(Dashing5GM, (true));)
482 DEF_GM(return SkNEW_ARGS(Dashing5GM, (false));)