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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkPaint.h" |
| 11 | #include "SkDashPathEffect.h" |
| 12 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 13 | 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] | 14 | SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 15 | SkScalar phase = SkIntToScalar(0), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 16 | SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 17 | SkPaint p(paint); |
| 18 | |
| 19 | const SkScalar intervals[] = { |
| 20 | SkIntToScalar(on), |
| 21 | SkIntToScalar(off), |
| 22 | }; |
| 23 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 24 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 25 | canvas->drawLine(startX, startY, finalX, finalY, p); |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 26 | } |
| 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. |
| 31 | static void show_giant_dash(SkCanvas* canvas) { |
| 32 | SkPaint paint; |
| 33 | |
| 34 | drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 35 | } |
| 36 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 37 | static 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.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 47 | class DashingGM : public skiagm::GM { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 48 | public: |
| 49 | DashingGM() {} |
| 50 | |
| 51 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 52 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 53 | SkString onShortName() { |
| 54 | return SkString("dashing"); |
| 55 | } |
| 56 | |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 57 | SkISize onISize() { return SkISize::Make(640, 340); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 58 | |
| 59 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 60 | constexpr struct { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 61 | int fOnInterval; |
| 62 | int fOffInterval; |
| 63 | } gData[] = { |
| 64 | { 1, 1 }, |
| 65 | { 4, 1 }, |
| 66 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 68 | SkPaint paint; |
| 69 | paint.setStyle(SkPaint::kStroke_Style); |
| 70 | |
| 71 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 72 | canvas->translate(0, SK_ScalarHalf); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 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)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 79 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 80 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 89 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 90 | show_giant_dash(canvas); |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 91 | canvas->translate(0, SkIntToScalar(20)); |
| 92 | show_zero_len_dash(canvas); |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 93 | 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.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 97 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 98 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 99 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
| 102 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 106 | 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 | |
| 115 | static 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 | |
| 120 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 121 | path->addRect(bounds); |
| 122 | } |
| 123 | |
| 124 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 125 | path->addOval(bounds); |
| 126 | } |
| 127 | |
| 128 | static 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 | |
| 135 | class Dashing2GM : public skiagm::GM { |
| 136 | public: |
| 137 | Dashing2GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 139 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 140 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 141 | SkString onShortName() { |
| 142 | return SkString("dashing2"); |
| 143 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 145 | SkISize onISize() { return SkISize::Make(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 146 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 147 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 148 | constexpr int gIntervals[] = { |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 149 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 159 | SkPaint paint; |
| 160 | paint.setAntiAlias(true); |
| 161 | paint.setStyle(SkPaint::kStroke_Style); |
| 162 | paint.setStrokeWidth(SkIntToScalar(6)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 163 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 164 | 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; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 177 | paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 179 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 184 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 185 | canvas->drawPath(path, paint); |
| 186 | } |
| 187 | } |
| 188 | } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | ////////////////////////////////////////////////////////////////////////////// |
| 192 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 193 | // Test out the on/off line dashing Chrome if fond of |
| 194 | class Dashing3GM : public skiagm::GM { |
| 195 | public: |
| 196 | Dashing3GM() {} |
| 197 | |
| 198 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 199 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 200 | SkString onShortName() { |
| 201 | return SkString("dashing3"); |
| 202 | } |
| 203 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 204 | SkISize onISize() { return SkISize::Make(640, 480); } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 205 | |
| 206 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 207 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 208 | void drawDashedLines(SkCanvas* canvas, |
| 209 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 210 | SkScalar phase, |
| 211 | SkScalar dashLength, |
| 212 | int strokeWidth, |
| 213 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 214 | SkPaint p; |
| 215 | p.setColor(SK_ColorBLACK); |
| 216 | p.setStyle(SkPaint::kStroke_Style); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 217 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 218 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 219 | if (circles) { |
| 220 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 221 | } |
| 222 | |
| 223 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 224 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 225 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 226 | |
| 227 | SkPoint pts[2]; |
| 228 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 229 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 230 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 231 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 232 | |
| 233 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 234 | } |
| 235 | |
| 236 | p.setAntiAlias(true); |
| 237 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 238 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 239 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 240 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 241 | |
| 242 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 247 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
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(2, 0); |
| 250 | this->drawDashedLines(canvas, 100, 0, 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 .5 - rects fastpath (due to partial squares) |
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(112, 0); |
| 256 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, 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 - points 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(222, 0); |
| 262 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 263 | canvas->restore(); |
| 264 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 265 | // 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] | 266 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 267 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 268 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 269 | canvas->restore(); |
| 270 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 271 | // 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.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 277 | // 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 286 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 287 | 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.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 304 | canvas->rotate(45); |
| 305 | canvas->translate(-50, -50); |
| 306 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 307 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 308 | canvas->restore(); |
| 309 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 310 | // 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.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 313 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 314 | SkIntToScalar(330)); |
| 315 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 316 | canvas->restore(); |
| 317 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | }; |
| 321 | |
| 322 | ////////////////////////////////////////////////////////////////////////////// |
| 323 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 324 | class Dashing4GM : public skiagm::GM { |
| 325 | public: |
| 326 | Dashing4GM() {} |
| 327 | |
| 328 | protected: |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 329 | |
| 330 | SkString onShortName() { |
| 331 | return SkString("dashing4"); |
| 332 | } |
| 333 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 334 | SkISize onISize() { return SkISize::Make(640, 950); } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 335 | |
| 336 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 337 | constexpr struct { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 338 | 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.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 378 | // 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] | 379 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 380 | canvas->translate(0, SkIntToScalar(20)); |
| 381 | |
| 382 | // 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] | 383 | drawline(canvas, 32, 16, paint, 56.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 384 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 387 | drawline(canvas, 32, 16, paint, 584.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 388 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 392 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 393 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 396 | 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] | 397 | canvas->translate(0, SkIntToScalar(20)); |
| 398 | } |
| 399 | } |
| 400 | }; |
| 401 | |
| 402 | ////////////////////////////////////////////////////////////////////////////// |
| 403 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 404 | class Dashing5GM : public skiagm::GM { |
| 405 | public: |
| 406 | Dashing5GM(bool doAA) : fDoAA(doAA) {} |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 407 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 408 | protected: |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 409 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 410 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 411 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 412 | SkString onShortName() override { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 413 | if (fDoAA) { |
| 414 | return SkString("dashing5_aa"); |
| 415 | } else { |
| 416 | return SkString("dashing5_bw"); |
| 417 | } |
| 418 | } |
| 419 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 420 | SkISize onISize() override { return SkISize::Make(400, 200); } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 421 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 422 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 423 | constexpr int kOn = 4; |
| 424 | constexpr int kOff = 4; |
| 425 | constexpr int kIntervalLength = kOn + kOff; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 426 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 427 | constexpr SkColor gColors[kIntervalLength] = { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 428 | 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; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 456 | drawline(canvas, kOn, kOff, paint, |
| 457 | SkIntToScalar(x), -sign * SkIntToScalar(10003), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 458 | 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; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 467 | drawline(canvas, kOn, kOff, paint, |
| 468 | -sign * SkIntToScalar(10003), SkIntToScalar(y), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 469 | SkIntToScalar(phase), |
| 470 | sign * SkIntToScalar(10003), SkIntToScalar(y)); |
| 471 | phase = (phase + 1) % kIntervalLength; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | private: |
| 476 | bool fDoAA; |
| 477 | }; |
| 478 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 479 | DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) { |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 480 | 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 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 501 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 502 | |
| 503 | canvas->translate(50, 50); |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 504 | canvas->drawPath(lines, p); |
| 505 | } |
| 506 | |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 507 | DEF_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 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 514 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 515 | canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p); |
| 516 | } |
| 517 | |
| 518 | DEF_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 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 533 | DEF_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); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 539 | p.setStrokeJoin(SkPaint::kRound_Join); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 540 | p.setTextSize(100); |
| 541 | p.setARGB(0xff, 0xbb, 0x00, 0x00); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 542 | sk_tool_utils::set_portable_typeface(&p); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 543 | const SkScalar intervals[] = { 12, 12 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 544 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 545 | canvas->drawText("Sausages", 8, 10, 90, p); |
| 546 | canvas->drawLine(8, 120, 456, 120, p); |
| 547 | } |
| 548 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 549 | ////////////////////////////////////////////////////////////////////////////// |
| 550 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 551 | DEF_GM(return new DashingGM;) |
| 552 | DEF_GM(return new Dashing2GM;) |
| 553 | DEF_GM(return new Dashing3GM;) |
| 554 | DEF_GM(return new Dashing4GM;) |
| 555 | DEF_GM(return new Dashing5GM(true);) |
| 556 | DEF_GM(return new Dashing5GM(false);) |