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