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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkPaint.h" |
| 12 | #include "SkDashPathEffect.h" |
| 13 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 14 | 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] | 15 | SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 16 | SkScalar phase = SkIntToScalar(0), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 17 | SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 18 | SkPaint p(paint); |
| 19 | |
| 20 | const SkScalar intervals[] = { |
| 21 | SkIntToScalar(on), |
| 22 | SkIntToScalar(off), |
| 23 | }; |
| 24 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 25 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 26 | canvas->drawLine(startX, startY, finalX, finalY, p); |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 27 | } |
| 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. |
| 32 | static void show_giant_dash(SkCanvas* canvas) { |
| 33 | SkPaint paint; |
| 34 | |
| 35 | drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 36 | } |
| 37 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 38 | static 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.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 48 | class DashingGM : public skiagm::GM { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 49 | public: |
| 50 | DashingGM() {} |
| 51 | |
| 52 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 53 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 54 | SkString onShortName() { |
| 55 | return SkString("dashing"); |
| 56 | } |
| 57 | |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 58 | SkISize onISize() { return SkISize::Make(640, 340); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 59 | |
| 60 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 61 | constexpr struct { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 62 | int fOnInterval; |
| 63 | int fOffInterval; |
| 64 | } gData[] = { |
| 65 | { 1, 1 }, |
| 66 | { 4, 1 }, |
| 67 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 69 | SkPaint paint; |
| 70 | paint.setStyle(SkPaint::kStroke_Style); |
| 71 | |
| 72 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 73 | canvas->translate(0, SK_ScalarHalf); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 74 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 80 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 81 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 90 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 91 | show_giant_dash(canvas); |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 92 | canvas->translate(0, SkIntToScalar(20)); |
| 93 | show_zero_len_dash(canvas); |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 94 | 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.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 98 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 99 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 100 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 107 | 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 | |
| 116 | static 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 | |
| 121 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 122 | path->addRect(bounds); |
| 123 | } |
| 124 | |
| 125 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 126 | path->addOval(bounds); |
| 127 | } |
| 128 | |
| 129 | static 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 | |
| 136 | class Dashing2GM : public skiagm::GM { |
| 137 | public: |
| 138 | Dashing2GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 139 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 140 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 141 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 142 | SkString onShortName() { |
| 143 | return SkString("dashing2"); |
| 144 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 145 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 146 | SkISize onISize() { return SkISize::Make(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 147 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 148 | virtual void onDraw(SkCanvas* canvas) { |
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 { |
| 196 | public: |
| 197 | Dashing3GM() {} |
| 198 | |
| 199 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 200 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 201 | SkString onShortName() { |
| 202 | return SkString("dashing3"); |
| 203 | } |
| 204 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 205 | SkISize onISize() { return SkISize::Make(640, 480); } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 206 | |
| 207 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 208 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 209 | void drawDashedLines(SkCanvas* canvas, |
| 210 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 211 | SkScalar phase, |
| 212 | SkScalar dashLength, |
| 213 | int strokeWidth, |
| 214 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 215 | SkPaint p; |
| 216 | p.setColor(SK_ColorBLACK); |
| 217 | p.setStyle(SkPaint::kStroke_Style); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 218 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 219 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 220 | if (circles) { |
| 221 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 222 | } |
| 223 | |
| 224 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 225 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 226 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 227 | |
| 228 | SkPoint pts[2]; |
| 229 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 230 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 231 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 232 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 233 | |
| 234 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 235 | } |
| 236 | |
| 237 | p.setAntiAlias(true); |
| 238 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 239 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 240 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 241 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 242 | |
| 243 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 248 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 249 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 250 | canvas->translate(2, 0); |
| 251 | this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 252 | canvas->restore(); |
| 253 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 254 | // 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] | 255 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 256 | canvas->translate(112, 0); |
| 257 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 258 | canvas->restore(); |
| 259 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 260 | // 1on/1off 1x1 squares with phase of 1 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 261 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 262 | canvas->translate(222, 0); |
| 263 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 264 | canvas->restore(); |
| 265 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 266 | // 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] | 267 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 268 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 269 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 270 | canvas->restore(); |
| 271 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 272 | // 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.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 278 | // 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.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 287 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 288 | 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.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 305 | canvas->rotate(45); |
| 306 | canvas->translate(-50, -50); |
| 307 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 308 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 309 | canvas->restore(); |
| 310 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 311 | // 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.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 314 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 315 | SkIntToScalar(330)); |
| 316 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 317 | canvas->restore(); |
| 318 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | }; |
| 322 | |
| 323 | ////////////////////////////////////////////////////////////////////////////// |
| 324 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 325 | class Dashing4GM : public skiagm::GM { |
| 326 | public: |
| 327 | Dashing4GM() {} |
| 328 | |
| 329 | protected: |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 330 | |
| 331 | SkString onShortName() { |
| 332 | return SkString("dashing4"); |
| 333 | } |
| 334 | |
Greg Daniel | 5fb3056 | 2017-06-29 12:27:48 -0400 | [diff] [blame] | 335 | SkISize onISize() { return SkISize::Make(640, 1050); } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 336 | |
| 337 | virtual void onDraw(SkCanvas* canvas) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 338 | constexpr struct { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 339 | 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 Daniel | 95581bb | 2017-06-30 10:36:38 -0400 | [diff] [blame] | 351 | canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 352 | |
| 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.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 379 | // 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] | 380 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 381 | canvas->translate(0, SkIntToScalar(20)); |
| 382 | |
| 383 | // 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] | 384 | drawline(canvas, 32, 16, paint, 56.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 385 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 388 | drawline(canvas, 32, 16, paint, 584.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 389 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 393 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 394 | 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.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 397 | 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] | 398 | canvas->translate(0, SkIntToScalar(20)); |
| 399 | } |
Greg Daniel | 5fb3056 | 2017-06-29 12:27:48 -0400 | [diff] [blame] | 400 | |
| 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); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 412 | } |
| 413 | }; |
| 414 | |
| 415 | ////////////////////////////////////////////////////////////////////////////// |
| 416 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 417 | class Dashing5GM : public skiagm::GM { |
| 418 | public: |
| 419 | Dashing5GM(bool doAA) : fDoAA(doAA) {} |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 420 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 421 | protected: |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 422 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 423 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 424 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 425 | SkString onShortName() override { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 426 | if (fDoAA) { |
| 427 | return SkString("dashing5_aa"); |
| 428 | } else { |
| 429 | return SkString("dashing5_bw"); |
| 430 | } |
| 431 | } |
| 432 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 433 | SkISize onISize() override { return SkISize::Make(400, 200); } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 434 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 435 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 436 | constexpr int kOn = 4; |
| 437 | constexpr int kOff = 4; |
| 438 | constexpr int kIntervalLength = kOn + kOff; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 439 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 440 | constexpr SkColor gColors[kIntervalLength] = { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 441 | SK_ColorRED, |
| 442 | SK_ColorGREEN, |
| 443 | SK_ColorBLUE, |
| 444 | SK_ColorCYAN, |
| 445 | SK_ColorMAGENTA, |
| 446 | SK_ColorYELLOW, |
| 447 | SK_ColorGRAY, |
| 448 | SK_ColorDKGRAY |
| 449 | }; |
| 450 | |
| 451 | SkPaint paint; |
| 452 | paint.setStyle(SkPaint::kStroke_Style); |
| 453 | |
| 454 | paint.setAntiAlias(fDoAA); |
| 455 | |
| 456 | SkMatrix rot; |
| 457 | rot.setRotate(90); |
| 458 | SkASSERT(rot.rectStaysRect()); |
| 459 | |
| 460 | canvas->concat(rot); |
| 461 | |
| 462 | int sign; // used to toggle the direction of the lines |
| 463 | int phase = 0; |
| 464 | |
| 465 | for (int x = 0; x < 200; x += 10) { |
| 466 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 467 | paint.setColor(gColors[phase]); |
| 468 | sign = (x % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 469 | drawline(canvas, kOn, kOff, paint, |
| 470 | SkIntToScalar(x), -sign * SkIntToScalar(10003), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 471 | SkIntToScalar(phase), |
| 472 | SkIntToScalar(x), sign * SkIntToScalar(10003)); |
| 473 | phase = (phase + 1) % kIntervalLength; |
| 474 | } |
| 475 | |
| 476 | for (int y = -400; y < 0; y += 10) { |
| 477 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 478 | paint.setColor(gColors[phase]); |
| 479 | sign = (y % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 480 | drawline(canvas, kOn, kOff, paint, |
| 481 | -sign * SkIntToScalar(10003), SkIntToScalar(y), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 482 | SkIntToScalar(phase), |
| 483 | sign * SkIntToScalar(10003), SkIntToScalar(y)); |
| 484 | phase = (phase + 1) % kIntervalLength; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | bool fDoAA; |
| 490 | }; |
| 491 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 492 | DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) { |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 493 | SkPath lines; |
| 494 | for (int x = 32; x < 256; x += 16) { |
| 495 | for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) { |
| 496 | SkPoint pts[2] = { |
| 497 | { 256 + (float) sin(a) * x, |
| 498 | 256 + (float) cos(a) * x }, |
| 499 | { 256 + (float) sin(a + 3.141592 / 3) * (x + 64), |
| 500 | 256 + (float) cos(a + 3.141592 / 3) * (x + 64) } |
| 501 | }; |
| 502 | lines.moveTo(pts[0]); |
| 503 | for (SkScalar i = 0; i < 1; i += 0.05f) { |
| 504 | lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i, |
| 505 | pts[0].fY * (1 - i) + pts[1].fY * i); |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | SkPaint p; |
| 510 | p.setAntiAlias(true); |
| 511 | p.setStyle(SkPaint::kStroke_Style); |
| 512 | p.setStrokeWidth(1); |
| 513 | const SkScalar intervals[] = { 1, 1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 514 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 515 | |
| 516 | canvas->translate(50, 50); |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 517 | canvas->drawPath(lines, p); |
| 518 | } |
| 519 | |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 520 | DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) { |
| 521 | SkPaint p; |
| 522 | p.setAntiAlias(true); |
| 523 | p.setStyle(SkPaint::kStroke_Style); |
| 524 | p.setStrokeWidth(80); |
| 525 | |
| 526 | const SkScalar intervals[] = { 2, 2 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 527 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 528 | canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p); |
| 529 | } |
| 530 | |
| 531 | DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) { |
| 532 | SkPaint p; |
| 533 | p.setAntiAlias(true); |
| 534 | p.setStyle(SkPaint::kStroke_Style); |
| 535 | p.setStrokeWidth(2); |
| 536 | |
| 537 | SkPath wavy; |
| 538 | wavy.moveTo(-10000, 100); |
| 539 | for (SkScalar i = -10000; i < 10000; i += 20) { |
| 540 | wavy.quadTo(i + 5, 95, i + 10, 100); |
| 541 | wavy.quadTo(i + 15, 105, i + 20, 100); |
| 542 | } |
| 543 | canvas->drawPath(wavy, p); |
| 544 | } |
| 545 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 546 | DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) { |
| 547 | SkPaint p; |
| 548 | p.setAntiAlias(true); |
| 549 | p.setStyle(SkPaint::kStroke_Style); |
| 550 | p.setStrokeWidth(10); |
| 551 | p.setStrokeCap(SkPaint::kRound_Cap); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 552 | p.setStrokeJoin(SkPaint::kRound_Join); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 553 | p.setTextSize(100); |
| 554 | p.setARGB(0xff, 0xbb, 0x00, 0x00); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 555 | sk_tool_utils::set_portable_typeface(&p); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 556 | const SkScalar intervals[] = { 12, 12 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 557 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 558 | canvas->drawString("Sausages", 10, 90, p); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 559 | canvas->drawLine(8, 120, 456, 120, p); |
| 560 | } |
| 561 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 562 | ////////////////////////////////////////////////////////////////////////////// |
| 563 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 564 | DEF_GM(return new DashingGM;) |
| 565 | DEF_GM(return new Dashing2GM;) |
| 566 | DEF_GM(return new Dashing3GM;) |
| 567 | DEF_GM(return new Dashing4GM;) |
| 568 | DEF_GM(return new Dashing5GM(true);) |
| 569 | DEF_GM(return new Dashing5GM(false);) |