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), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 15 | SkScalar phase = SkIntToScalar(0), |
| 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 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 24 | p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref(); |
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 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 57 | SkISize onISize() { return SkISize::Make(640, 300); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 58 | |
| 59 | virtual void onDraw(SkCanvas* canvas) { |
| 60 | static const struct { |
| 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); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 93 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 94 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 95 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | |
| 98 | static void make_unit_star(SkPath* path, int n) { |
| 99 | SkScalar rad = -SK_ScalarPI / 2; |
| 100 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 101 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 102 | path->moveTo(0, -SK_Scalar1); |
| 103 | for (int i = 1; i < n; i++) { |
| 104 | rad += drad; |
| 105 | SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV); |
| 106 | path->lineTo(cosV, sinV); |
| 107 | } |
| 108 | path->close(); |
| 109 | } |
| 110 | |
| 111 | static void make_path_line(SkPath* path, const SkRect& bounds) { |
| 112 | path->moveTo(bounds.left(), bounds.top()); |
| 113 | path->lineTo(bounds.right(), bounds.bottom()); |
| 114 | } |
| 115 | |
| 116 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 117 | path->addRect(bounds); |
| 118 | } |
| 119 | |
| 120 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 121 | path->addOval(bounds); |
| 122 | } |
| 123 | |
| 124 | static void make_path_star(SkPath* path, const SkRect& bounds) { |
| 125 | make_unit_star(path, 5); |
| 126 | SkMatrix matrix; |
| 127 | matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit); |
| 128 | path->transform(matrix); |
| 129 | } |
| 130 | |
| 131 | class Dashing2GM : public skiagm::GM { |
| 132 | public: |
| 133 | Dashing2GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 135 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 136 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 137 | SkString onShortName() { |
| 138 | return SkString("dashing2"); |
| 139 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 140 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 141 | SkISize onISize() { return SkISize::Make(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 143 | virtual void onDraw(SkCanvas* canvas) { |
| 144 | static const int gIntervals[] = { |
| 145 | 3, // 3 dashes: each count [0] followed by intervals [1..count] |
| 146 | 2, 10, 10, |
| 147 | 4, 20, 5, 5, 5, |
| 148 | 2, 2, 2 |
| 149 | }; |
| 150 | |
| 151 | void (*gProc[])(SkPath*, const SkRect&) = { |
| 152 | make_path_line, make_path_rect, make_path_oval, make_path_star, |
| 153 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 154 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 155 | SkPaint paint; |
| 156 | paint.setAntiAlias(true); |
| 157 | paint.setStyle(SkPaint::kStroke_Style); |
| 158 | paint.setStrokeWidth(SkIntToScalar(6)); |
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 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120)); |
| 161 | bounds.offset(SkIntToScalar(20), SkIntToScalar(20)); |
| 162 | SkScalar dx = bounds.width() * 4 / 3; |
| 163 | SkScalar dy = bounds.height() * 4 / 3; |
| 164 | |
| 165 | const int* intervals = &gIntervals[1]; |
| 166 | for (int y = 0; y < gIntervals[0]; ++y) { |
| 167 | SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough |
| 168 | int count = *intervals++; |
| 169 | for (int i = 0; i < count; ++i) { |
| 170 | vals[i] = SkIntToScalar(*intervals++); |
| 171 | } |
| 172 | SkScalar phase = vals[0] / 2; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 173 | paint.setPathEffect(SkDashPathEffect::Create(vals, count, phase))->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 174 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 175 | for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) { |
| 176 | SkPath path; |
| 177 | SkRect r = bounds; |
| 178 | r.offset(x * dx, y * dy); |
| 179 | gProc[x](&path, r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 181 | canvas->drawPath(path, paint); |
| 182 | } |
| 183 | } |
| 184 | } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | ////////////////////////////////////////////////////////////////////////////// |
| 188 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 189 | // Test out the on/off line dashing Chrome if fond of |
| 190 | class Dashing3GM : public skiagm::GM { |
| 191 | public: |
| 192 | Dashing3GM() {} |
| 193 | |
| 194 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 195 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 196 | SkString onShortName() { |
| 197 | return SkString("dashing3"); |
| 198 | } |
| 199 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 200 | SkISize onISize() { return SkISize::Make(640, 480); } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 201 | |
| 202 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 203 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 204 | void drawDashedLines(SkCanvas* canvas, |
| 205 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 206 | SkScalar phase, |
| 207 | SkScalar dashLength, |
| 208 | int strokeWidth, |
| 209 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 210 | SkPaint p; |
| 211 | p.setColor(SK_ColorBLACK); |
| 212 | p.setStyle(SkPaint::kStroke_Style); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 213 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 214 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 215 | if (circles) { |
| 216 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 217 | } |
| 218 | |
| 219 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 220 | |
commit-bot@chromium.org | 35c03fb | 2014-03-31 18:52:51 +0000 | [diff] [blame] | 221 | p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref(); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 222 | |
| 223 | SkPoint pts[2]; |
| 224 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 225 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 226 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 227 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 228 | |
| 229 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 230 | } |
| 231 | |
| 232 | p.setAntiAlias(true); |
| 233 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 234 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 235 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 236 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 237 | |
| 238 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 243 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 244 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 245 | canvas->translate(2, 0); |
| 246 | this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 247 | canvas->restore(); |
| 248 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 249 | // 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] | 250 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 251 | canvas->translate(112, 0); |
| 252 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 253 | canvas->restore(); |
| 254 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 255 | // 1on/1off 1x1 squares with phase of 1 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 256 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 257 | canvas->translate(222, 0); |
| 258 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 259 | canvas->restore(); |
| 260 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 261 | // 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] | 262 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 263 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 264 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 265 | canvas->restore(); |
| 266 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 267 | // 255on/255off 1x1 squares with phase of 0 - rects fast path |
| 268 | canvas->save(); |
| 269 | canvas->translate(446, 0); |
| 270 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false); |
| 271 | canvas->restore(); |
| 272 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 273 | // 1on/1off 3x3 squares with phase of 0 - points fast path |
| 274 | canvas->save(); |
| 275 | canvas->translate(2, 110); |
| 276 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false); |
| 277 | canvas->restore(); |
| 278 | |
| 279 | // 1on/1off 3x3 squares with phase of 1.5 - rects fast path |
| 280 | canvas->save(); |
| 281 | canvas->translate(112, 110); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 282 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 283 | canvas->restore(); |
| 284 | |
| 285 | // 1on/1off 1x1 circles with phase of 1 - no fast path yet |
| 286 | canvas->save(); |
| 287 | canvas->translate(2, 220); |
| 288 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true); |
| 289 | canvas->restore(); |
| 290 | |
| 291 | // 1on/1off 3x3 circles with phase of 1 - no fast path yet |
| 292 | canvas->save(); |
| 293 | canvas->translate(112, 220); |
| 294 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true); |
| 295 | canvas->restore(); |
| 296 | |
| 297 | // 1on/1off 1x1 squares with rotation - should break fast path |
| 298 | canvas->save(); |
| 299 | canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 300 | canvas->rotate(45); |
| 301 | canvas->translate(-50, -50); |
| 302 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 303 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 304 | canvas->restore(); |
| 305 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 306 | // 3on/3off 3x1 rects - should use rect fast path regardless of phase |
| 307 | for (int phase = 0; phase <= 3; ++phase) { |
| 308 | canvas->save(); |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 309 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 310 | SkIntToScalar(330)); |
| 311 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 312 | canvas->restore(); |
| 313 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | }; |
| 317 | |
| 318 | ////////////////////////////////////////////////////////////////////////////// |
| 319 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 320 | class Dashing4GM : public skiagm::GM { |
| 321 | public: |
| 322 | Dashing4GM() {} |
| 323 | |
| 324 | protected: |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 325 | |
| 326 | SkString onShortName() { |
| 327 | return SkString("dashing4"); |
| 328 | } |
| 329 | |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 330 | SkISize onISize() { return SkISize::Make(640, 950); } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 331 | |
| 332 | virtual void onDraw(SkCanvas* canvas) { |
| 333 | static const struct { |
| 334 | int fOnInterval; |
| 335 | int fOffInterval; |
| 336 | } gData[] = { |
| 337 | { 1, 1 }, |
| 338 | { 4, 2 }, |
| 339 | { 0, 4 }, // test for zero length on interval |
| 340 | }; |
| 341 | |
| 342 | SkPaint paint; |
| 343 | paint.setStyle(SkPaint::kStroke_Style); |
| 344 | |
| 345 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 346 | canvas->translate(0, SK_ScalarHalf); |
| 347 | |
| 348 | for (int width = 0; width <= 2; ++width) { |
| 349 | for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 350 | for (int aa = 0; aa <= 1; ++aa) { |
| 351 | for (int cap = 0; cap <= 1; ++cap) { |
| 352 | int w = width * width * width; |
| 353 | paint.setAntiAlias(SkToBool(aa)); |
| 354 | paint.setStrokeWidth(SkIntToScalar(w)); |
| 355 | |
| 356 | SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap) |
| 357 | : paint.setStrokeCap(SkPaint::kRound_Cap); |
| 358 | |
| 359 | int scale = w ? w : 1; |
| 360 | |
| 361 | drawline(canvas, gData[data].fOnInterval * scale, |
| 362 | gData[data].fOffInterval * scale, |
| 363 | paint); |
| 364 | canvas->translate(0, SkIntToScalar(20)); |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | for (int aa = 0; aa <= 1; ++aa) { |
| 371 | paint.setAntiAlias(SkToBool(aa)); |
| 372 | paint.setStrokeWidth(8.f); |
| 373 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 374 | // 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] | 375 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 376 | canvas->translate(0, SkIntToScalar(20)); |
| 377 | |
| 378 | // 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] | 379 | drawline(canvas, 32, 16, paint, 56.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 | // 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] | 383 | drawline(canvas, 32, 16, paint, 584.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 | // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from |
| 387 | // a canvas rotation) |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 388 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 389 | canvas->translate(0, SkIntToScalar(20)); |
| 390 | |
| 391 | // 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] | 392 | 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] | 393 | canvas->translate(0, SkIntToScalar(20)); |
| 394 | } |
| 395 | } |
| 396 | }; |
| 397 | |
| 398 | ////////////////////////////////////////////////////////////////////////////// |
| 399 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 400 | class Dashing5GM : public skiagm::GM { |
| 401 | public: |
| 402 | Dashing5GM(bool doAA) : fDoAA(doAA) {} |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 403 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 404 | protected: |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 405 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 406 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 407 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 408 | SkString onShortName() override { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 409 | if (fDoAA) { |
| 410 | return SkString("dashing5_aa"); |
| 411 | } else { |
| 412 | return SkString("dashing5_bw"); |
| 413 | } |
| 414 | } |
| 415 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 416 | SkISize onISize() override { return SkISize::Make(400, 200); } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 417 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 418 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 419 | static const int kOn = 4; |
| 420 | static const int kOff = 4; |
| 421 | static const int kIntervalLength = kOn + kOff; |
| 422 | |
| 423 | static const SkColor gColors[kIntervalLength] = { |
| 424 | SK_ColorRED, |
| 425 | SK_ColorGREEN, |
| 426 | SK_ColorBLUE, |
| 427 | SK_ColorCYAN, |
| 428 | SK_ColorMAGENTA, |
| 429 | SK_ColorYELLOW, |
| 430 | SK_ColorGRAY, |
| 431 | SK_ColorDKGRAY |
| 432 | }; |
| 433 | |
| 434 | SkPaint paint; |
| 435 | paint.setStyle(SkPaint::kStroke_Style); |
| 436 | |
| 437 | paint.setAntiAlias(fDoAA); |
| 438 | |
| 439 | SkMatrix rot; |
| 440 | rot.setRotate(90); |
| 441 | SkASSERT(rot.rectStaysRect()); |
| 442 | |
| 443 | canvas->concat(rot); |
| 444 | |
| 445 | int sign; // used to toggle the direction of the lines |
| 446 | int phase = 0; |
| 447 | |
| 448 | for (int x = 0; x < 200; x += 10) { |
| 449 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 450 | paint.setColor(gColors[phase]); |
| 451 | sign = (x % 20) ? 1 : -1; |
| 452 | drawline(canvas, kOn, kOff, paint, |
| 453 | SkIntToScalar(x), -sign * SkIntToScalar(10003), |
| 454 | SkIntToScalar(phase), |
| 455 | SkIntToScalar(x), sign * SkIntToScalar(10003)); |
| 456 | phase = (phase + 1) % kIntervalLength; |
| 457 | } |
| 458 | |
| 459 | for (int y = -400; y < 0; y += 10) { |
| 460 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 461 | paint.setColor(gColors[phase]); |
| 462 | sign = (y % 20) ? 1 : -1; |
| 463 | drawline(canvas, kOn, kOff, paint, |
| 464 | -sign * SkIntToScalar(10003), SkIntToScalar(y), |
| 465 | SkIntToScalar(phase), |
| 466 | sign * SkIntToScalar(10003), SkIntToScalar(y)); |
| 467 | phase = (phase + 1) % kIntervalLength; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | private: |
| 472 | bool fDoAA; |
| 473 | }; |
| 474 | |
| 475 | ////////////////////////////////////////////////////////////////////////////// |
| 476 | |
| 477 | DEF_GM(return SkNEW(DashingGM);) |
| 478 | DEF_GM(return SkNEW(Dashing2GM);) |
| 479 | DEF_GM(return SkNEW(Dashing3GM);) |
| 480 | DEF_GM(return SkNEW(Dashing4GM);) |
| 481 | DEF_GM(return SkNEW_ARGS(Dashing5GM, (true));) |
| 482 | DEF_GM(return SkNEW_ARGS(Dashing5GM, (false));) |
| 483 | |