reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPath.h" |
| 15 | #include "include/core/SkPathEffect.h" |
| 16 | #include "include/core/SkPoint.h" |
| 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTypeface.h" |
| 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/effects/SkDashPathEffect.h" |
| 24 | #include "tools/ToolUtils.h" |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 25 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include <math.h> |
| 27 | #include <initializer_list> |
| 28 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 29 | static void drawline(SkCanvas* canvas, int on, int off, const SkPaint& paint, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 30 | SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 31 | SkScalar phase = SkIntToScalar(0), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 32 | SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 33 | SkPaint p(paint); |
| 34 | |
| 35 | const SkScalar intervals[] = { |
| 36 | SkIntToScalar(on), |
| 37 | SkIntToScalar(off), |
| 38 | }; |
| 39 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 40 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 41 | canvas->drawLine(startX, startY, finalX, finalY, p); |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // earlier bug stopped us from drawing very long single-segment dashes, because |
| 45 | // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is |
| 46 | // now fixes, so this giant dash should appear. |
| 47 | static void show_giant_dash(SkCanvas* canvas) { |
| 48 | SkPaint paint; |
| 49 | |
| 50 | drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 51 | } |
| 52 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 53 | static void show_zero_len_dash(SkCanvas* canvas) { |
| 54 | SkPaint paint; |
| 55 | |
| 56 | drawline(canvas, 2, 2, paint, SkIntToScalar(0)); |
| 57 | paint.setStyle(SkPaint::kStroke_Style); |
| 58 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 59 | canvas->translate(0, SkIntToScalar(20)); |
| 60 | drawline(canvas, 4, 4, paint, SkIntToScalar(0)); |
| 61 | } |
| 62 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 63 | class DashingGM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 64 | SkString onShortName() override { return SkString("dashing"); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 65 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 66 | SkISize onISize() override { return {640, 340}; } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 67 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 68 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 69 | constexpr struct { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 70 | int fOnInterval; |
| 71 | int fOffInterval; |
| 72 | } gData[] = { |
| 73 | { 1, 1 }, |
| 74 | { 4, 1 }, |
| 75 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 77 | SkPaint paint; |
| 78 | paint.setStyle(SkPaint::kStroke_Style); |
| 79 | |
| 80 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 81 | canvas->translate(0, SK_ScalarHalf); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 82 | for (int width = 0; width <= 2; ++width) { |
| 83 | for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 84 | for (int aa = 0; aa <= 1; ++aa) { |
| 85 | int w = width * width * width; |
| 86 | paint.setAntiAlias(SkToBool(aa)); |
| 87 | paint.setStrokeWidth(SkIntToScalar(w)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 88 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 89 | int scale = w ? w : 1; |
| 90 | |
| 91 | drawline(canvas, gData[data].fOnInterval * scale, |
| 92 | gData[data].fOffInterval * scale, |
| 93 | paint); |
| 94 | canvas->translate(0, SkIntToScalar(20)); |
| 95 | } |
| 96 | } |
| 97 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 99 | show_giant_dash(canvas); |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 100 | canvas->translate(0, SkIntToScalar(20)); |
| 101 | show_zero_len_dash(canvas); |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 102 | canvas->translate(0, SkIntToScalar(20)); |
| 103 | // Draw 0 on, 0 off dashed line |
| 104 | paint.setStrokeWidth(SkIntToScalar(8)); |
| 105 | drawline(canvas, 0, 0, paint); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 106 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 107 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 108 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 109 | /////////////////////////////////////////////////////////////////////////////// |
| 110 | |
| 111 | static void make_unit_star(SkPath* path, int n) { |
| 112 | SkScalar rad = -SK_ScalarPI / 2; |
| 113 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 114 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 115 | path->moveTo(0, -SK_Scalar1); |
| 116 | for (int i = 1; i < n; i++) { |
| 117 | rad += drad; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 118 | path->lineTo(SkScalarCos(rad), SkScalarSin(rad)); |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 119 | } |
| 120 | path->close(); |
| 121 | } |
| 122 | |
| 123 | static void make_path_line(SkPath* path, const SkRect& bounds) { |
| 124 | path->moveTo(bounds.left(), bounds.top()); |
| 125 | path->lineTo(bounds.right(), bounds.bottom()); |
| 126 | } |
| 127 | |
| 128 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 129 | path->addRect(bounds); |
| 130 | } |
| 131 | |
| 132 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 133 | path->addOval(bounds); |
| 134 | } |
| 135 | |
| 136 | static void make_path_star(SkPath* path, const SkRect& bounds) { |
| 137 | make_unit_star(path, 5); |
| 138 | SkMatrix matrix; |
| 139 | matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit); |
| 140 | path->transform(matrix); |
| 141 | } |
| 142 | |
| 143 | class Dashing2GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 144 | SkString onShortName() override { return SkString("dashing2"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 146 | SkISize onISize() override { return {640, 480}; } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 147 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 148 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 149 | constexpr int gIntervals[] = { |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 150 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 160 | SkPaint paint; |
| 161 | paint.setAntiAlias(true); |
| 162 | paint.setStyle(SkPaint::kStroke_Style); |
| 163 | paint.setStrokeWidth(SkIntToScalar(6)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 164 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 165 | 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; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 178 | paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 180 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 185 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 186 | canvas->drawPath(path, paint); |
| 187 | } |
| 188 | } |
| 189 | } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | ////////////////////////////////////////////////////////////////////////////// |
| 193 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 194 | // Test out the on/off line dashing Chrome if fond of |
| 195 | class Dashing3GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 196 | SkString onShortName() override { return SkString("dashing3"); } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 197 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 198 | SkISize onISize() override { return {640, 480}; } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 199 | |
| 200 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 201 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 202 | void drawDashedLines(SkCanvas* canvas, |
| 203 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 204 | SkScalar phase, |
| 205 | SkScalar dashLength, |
| 206 | int strokeWidth, |
| 207 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 208 | SkPaint p; |
| 209 | p.setColor(SK_ColorBLACK); |
| 210 | p.setStyle(SkPaint::kStroke_Style); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 211 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 212 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 213 | if (circles) { |
| 214 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 215 | } |
| 216 | |
| 217 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 218 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 219 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 220 | |
| 221 | SkPoint pts[2]; |
| 222 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 223 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 224 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 225 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 226 | |
| 227 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 228 | } |
| 229 | |
| 230 | p.setAntiAlias(true); |
| 231 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 232 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 233 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 234 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 235 | |
| 236 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 237 | } |
| 238 | } |
| 239 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 240 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 241 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 242 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 243 | canvas->translate(2, 0); |
| 244 | this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 245 | canvas->restore(); |
| 246 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 247 | // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares) |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 248 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 249 | canvas->translate(112, 0); |
| 250 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 251 | canvas->restore(); |
| 252 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 253 | // 1on/1off 1x1 squares with phase of 1 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 254 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 255 | canvas->translate(222, 0); |
| 256 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 257 | canvas->restore(); |
| 258 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 259 | // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 260 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 261 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 262 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 263 | canvas->restore(); |
| 264 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 265 | // 255on/255off 1x1 squares with phase of 0 - rects fast path |
| 266 | canvas->save(); |
| 267 | canvas->translate(446, 0); |
| 268 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false); |
| 269 | canvas->restore(); |
| 270 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 271 | // 1on/1off 3x3 squares with phase of 0 - points fast path |
| 272 | canvas->save(); |
| 273 | canvas->translate(2, 110); |
| 274 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false); |
| 275 | canvas->restore(); |
| 276 | |
| 277 | // 1on/1off 3x3 squares with phase of 1.5 - rects fast path |
| 278 | canvas->save(); |
| 279 | canvas->translate(112, 110); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 280 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 281 | canvas->restore(); |
| 282 | |
| 283 | // 1on/1off 1x1 circles with phase of 1 - no fast path yet |
| 284 | canvas->save(); |
| 285 | canvas->translate(2, 220); |
| 286 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true); |
| 287 | canvas->restore(); |
| 288 | |
| 289 | // 1on/1off 3x3 circles with phase of 1 - no fast path yet |
| 290 | canvas->save(); |
| 291 | canvas->translate(112, 220); |
| 292 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true); |
| 293 | canvas->restore(); |
| 294 | |
| 295 | // 1on/1off 1x1 squares with rotation - should break fast path |
| 296 | canvas->save(); |
| 297 | canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 298 | canvas->rotate(45); |
| 299 | canvas->translate(-50, -50); |
| 300 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 301 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 302 | canvas->restore(); |
| 303 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 304 | // 3on/3off 3x1 rects - should use rect fast path regardless of phase |
| 305 | for (int phase = 0; phase <= 3; ++phase) { |
| 306 | canvas->save(); |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 307 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 308 | SkIntToScalar(330)); |
| 309 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 310 | canvas->restore(); |
| 311 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | }; |
| 315 | |
| 316 | ////////////////////////////////////////////////////////////////////////////// |
| 317 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 318 | class Dashing4GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 319 | SkString onShortName() override { return SkString("dashing4"); } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 320 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 321 | SkISize onISize() override { return {640, 1100}; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 322 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 323 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 324 | constexpr struct { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 325 | int fOnInterval; |
| 326 | int fOffInterval; |
| 327 | } gData[] = { |
| 328 | { 1, 1 }, |
| 329 | { 4, 2 }, |
| 330 | { 0, 4 }, // test for zero length on interval |
| 331 | }; |
| 332 | |
| 333 | SkPaint paint; |
| 334 | paint.setStyle(SkPaint::kStroke_Style); |
| 335 | |
| 336 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
Greg Daniel | 95581bb | 2017-06-30 10:36:38 -0400 | [diff] [blame] | 337 | canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 338 | |
| 339 | for (int width = 0; width <= 2; ++width) { |
| 340 | for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 341 | for (int aa = 0; aa <= 1; ++aa) { |
| 342 | for (int cap = 0; cap <= 1; ++cap) { |
| 343 | int w = width * width * width; |
| 344 | paint.setAntiAlias(SkToBool(aa)); |
| 345 | paint.setStrokeWidth(SkIntToScalar(w)); |
| 346 | |
| 347 | SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap) |
| 348 | : paint.setStrokeCap(SkPaint::kRound_Cap); |
| 349 | |
| 350 | int scale = w ? w : 1; |
| 351 | |
| 352 | drawline(canvas, gData[data].fOnInterval * scale, |
| 353 | gData[data].fOffInterval * scale, |
| 354 | paint); |
| 355 | canvas->translate(0, SkIntToScalar(20)); |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | for (int aa = 0; aa <= 1; ++aa) { |
| 362 | paint.setAntiAlias(SkToBool(aa)); |
| 363 | paint.setStrokeWidth(8.f); |
| 364 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 365 | // Single dash element that is cut off at start and end |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 366 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 367 | canvas->translate(0, SkIntToScalar(20)); |
| 368 | |
| 369 | // Two dash elements where each one is cut off at beginning and end respectively |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 370 | drawline(canvas, 32, 16, paint, 56.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 371 | canvas->translate(0, SkIntToScalar(20)); |
| 372 | |
| 373 | // Many dash elements where first and last are cut off at beginning and end respectively |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 374 | drawline(canvas, 32, 16, paint, 584.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 375 | canvas->translate(0, SkIntToScalar(20)); |
| 376 | |
| 377 | // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from |
| 378 | // a canvas rotation) |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 379 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 380 | canvas->translate(0, SkIntToScalar(20)); |
| 381 | |
| 382 | // Case where only the off interval exists on the line. Thus nothing should be drawn |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 383 | drawline(canvas, 32, 16, paint, 8.f, 0.f, 40.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 384 | canvas->translate(0, SkIntToScalar(20)); |
| 385 | } |
Greg Daniel | 5fb3056 | 2017-06-29 12:27:48 -0400 | [diff] [blame] | 386 | |
| 387 | // Test overlapping circles. |
| 388 | canvas->translate(SkIntToScalar(5), SkIntToScalar(20)); |
| 389 | paint.setAntiAlias(true); |
| 390 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 391 | paint.setColor(0x44000000); |
| 392 | paint.setStrokeWidth(40); |
| 393 | drawline(canvas, 0, 30, paint); |
| 394 | |
| 395 | canvas->translate(0, SkIntToScalar(50)); |
| 396 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 397 | drawline(canvas, 0, 30, paint); |
Greg Daniel | c96f9b5 | 2017-12-07 15:00:06 -0500 | [diff] [blame] | 398 | |
| 399 | // Test we draw the cap when the line length is zero. |
| 400 | canvas->translate(0, SkIntToScalar(50)); |
| 401 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 402 | paint.setColor(0xFF000000); |
| 403 | paint.setStrokeWidth(11); |
| 404 | drawline(canvas, 0, 30, paint, 0); |
| 405 | |
| 406 | canvas->translate(SkIntToScalar(100), 0); |
| 407 | drawline(canvas, 1, 30, paint, 0); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 408 | } |
| 409 | }; |
| 410 | |
| 411 | ////////////////////////////////////////////////////////////////////////////// |
| 412 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 413 | class Dashing5GM : public skiagm::GM { |
| 414 | public: |
| 415 | Dashing5GM(bool doAA) : fDoAA(doAA) {} |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 416 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 417 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 418 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 419 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 420 | SkString onShortName() override { return SkString(fDoAA ? "dashing5_aa" : "dashing5_bw"); } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 421 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 422 | SkISize onISize() override { return {400, 200}; } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 423 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 424 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 425 | constexpr int kOn = 4; |
| 426 | constexpr int kOff = 4; |
| 427 | constexpr int kIntervalLength = kOn + kOff; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 428 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 429 | constexpr SkColor gColors[kIntervalLength] = { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 430 | SK_ColorRED, |
| 431 | SK_ColorGREEN, |
| 432 | SK_ColorBLUE, |
| 433 | SK_ColorCYAN, |
| 434 | SK_ColorMAGENTA, |
| 435 | SK_ColorYELLOW, |
| 436 | SK_ColorGRAY, |
| 437 | SK_ColorDKGRAY |
| 438 | }; |
| 439 | |
| 440 | SkPaint paint; |
| 441 | paint.setStyle(SkPaint::kStroke_Style); |
| 442 | |
| 443 | paint.setAntiAlias(fDoAA); |
| 444 | |
| 445 | SkMatrix rot; |
| 446 | rot.setRotate(90); |
| 447 | SkASSERT(rot.rectStaysRect()); |
| 448 | |
| 449 | canvas->concat(rot); |
| 450 | |
| 451 | int sign; // used to toggle the direction of the lines |
| 452 | int phase = 0; |
| 453 | |
| 454 | for (int x = 0; x < 200; x += 10) { |
| 455 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 456 | paint.setColor(gColors[phase]); |
| 457 | sign = (x % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 458 | drawline(canvas, kOn, kOff, paint, |
| 459 | SkIntToScalar(x), -sign * SkIntToScalar(10003), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 460 | SkIntToScalar(phase), |
| 461 | SkIntToScalar(x), sign * SkIntToScalar(10003)); |
| 462 | phase = (phase + 1) % kIntervalLength; |
| 463 | } |
| 464 | |
| 465 | for (int y = -400; y < 0; y += 10) { |
| 466 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 467 | paint.setColor(gColors[phase]); |
| 468 | sign = (y % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 469 | drawline(canvas, kOn, kOff, paint, |
| 470 | -sign * SkIntToScalar(10003), SkIntToScalar(y), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 471 | SkIntToScalar(phase), |
| 472 | sign * SkIntToScalar(10003), SkIntToScalar(y)); |
| 473 | phase = (phase + 1) % kIntervalLength; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | private: |
| 478 | bool fDoAA; |
| 479 | }; |
| 480 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 481 | DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) { |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 482 | SkPath lines; |
| 483 | for (int x = 32; x < 256; x += 16) { |
| 484 | for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) { |
| 485 | SkPoint pts[2] = { |
| 486 | { 256 + (float) sin(a) * x, |
| 487 | 256 + (float) cos(a) * x }, |
| 488 | { 256 + (float) sin(a + 3.141592 / 3) * (x + 64), |
| 489 | 256 + (float) cos(a + 3.141592 / 3) * (x + 64) } |
| 490 | }; |
| 491 | lines.moveTo(pts[0]); |
| 492 | for (SkScalar i = 0; i < 1; i += 0.05f) { |
| 493 | lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i, |
| 494 | pts[0].fY * (1 - i) + pts[1].fY * i); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | SkPaint p; |
| 499 | p.setAntiAlias(true); |
| 500 | p.setStyle(SkPaint::kStroke_Style); |
| 501 | p.setStrokeWidth(1); |
| 502 | const SkScalar intervals[] = { 1, 1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 503 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 504 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 505 | canvas->translate(50, 50); |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 506 | canvas->drawPath(lines, p); |
| 507 | } |
| 508 | |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 509 | DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) { |
| 510 | SkPaint p; |
| 511 | p.setAntiAlias(true); |
| 512 | p.setStyle(SkPaint::kStroke_Style); |
| 513 | p.setStrokeWidth(80); |
| 514 | |
| 515 | const SkScalar intervals[] = { 2, 2 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 516 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 517 | canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p); |
| 518 | } |
| 519 | |
| 520 | DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) { |
| 521 | SkPaint p; |
| 522 | p.setAntiAlias(true); |
| 523 | p.setStyle(SkPaint::kStroke_Style); |
| 524 | p.setStrokeWidth(2); |
| 525 | |
| 526 | SkPath wavy; |
| 527 | wavy.moveTo(-10000, 100); |
| 528 | for (SkScalar i = -10000; i < 10000; i += 20) { |
| 529 | wavy.quadTo(i + 5, 95, i + 10, 100); |
| 530 | wavy.quadTo(i + 15, 105, i + 20, 100); |
| 531 | } |
| 532 | canvas->drawPath(wavy, p); |
| 533 | } |
| 534 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 535 | DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) { |
| 536 | SkPaint p; |
| 537 | p.setAntiAlias(true); |
| 538 | p.setStyle(SkPaint::kStroke_Style); |
| 539 | p.setStrokeWidth(10); |
| 540 | p.setStrokeCap(SkPaint::kRound_Cap); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 541 | p.setStrokeJoin(SkPaint::kRound_Join); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 542 | p.setARGB(0xff, 0xbb, 0x00, 0x00); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 543 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 544 | SkFont font(ToolUtils::create_portable_typeface(), 100); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 545 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 546 | const SkScalar intervals[] = { 12, 12 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 547 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 548 | canvas->drawString("Sausages", 10, 90, font, p); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 549 | canvas->drawLine(8, 120, 456, 120, p); |
| 550 | } |
| 551 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 552 | DEF_SIMPLE_GM(dash_line_zero_off_interval, canvas, 160, 330) { |
| 553 | static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f}; |
| 554 | SkPaint dashPaint; |
| 555 | dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f)); |
| 556 | SkASSERT(dashPaint.getPathEffect()); |
| 557 | dashPaint.setStyle(SkPaint::kStroke_Style); |
| 558 | dashPaint.setStrokeWidth(20.f); |
| 559 | static constexpr struct { |
| 560 | SkPoint fA, fB; |
| 561 | } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal |
| 562 | {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical |
| 563 | {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point |
| 564 | {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal |
| 565 | SkScalar pad = 5.f + dashPaint.getStrokeWidth(); |
| 566 | canvas->translate(pad / 2.f, pad / 2.f); |
| 567 | canvas->save(); |
| 568 | SkScalar h = 0.f; |
| 569 | for (const auto& line : kLines) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 570 | h = std::max(h, SkScalarAbs(line.fA.fY - line.fB.fY)); |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 571 | } |
| 572 | for (const auto& line : kLines) { |
| 573 | SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX); |
| 574 | for (auto cap : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) { |
| 575 | dashPaint.setStrokeCap(cap); |
| 576 | for (auto aa : {false, true}) { |
| 577 | dashPaint.setAntiAlias(aa); |
| 578 | canvas->drawLine(line.fA, line.fB, dashPaint); |
| 579 | canvas->translate(0.f, pad + h); |
| 580 | } |
| 581 | } |
| 582 | canvas->restore(); |
| 583 | canvas->translate(pad + w, 0.f); |
| 584 | canvas->save(); |
| 585 | } |
| 586 | } |
| 587 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 588 | ////////////////////////////////////////////////////////////////////////////// |
| 589 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 590 | DEF_GM(return new DashingGM;) |
| 591 | DEF_GM(return new Dashing2GM;) |
| 592 | DEF_GM(return new Dashing3GM;) |
| 593 | DEF_GM(return new Dashing4GM;) |
| 594 | DEF_GM(return new Dashing5GM(true);) |
| 595 | DEF_GM(return new Dashing5GM(false);) |