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), |
| 15 | SkScalar phase = SkIntToScalar(0)) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 16 | SkPaint p(paint); |
| 17 | |
| 18 | const SkScalar intervals[] = { |
| 19 | SkIntToScalar(on), |
| 20 | SkIntToScalar(off), |
| 21 | }; |
| 22 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 23 | p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref(); |
| 24 | canvas->drawLine(0, 0, finalX, finalY, p); |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | // earlier bug stopped us from drawing very long single-segment dashes, because |
| 28 | // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is |
| 29 | // now fixes, so this giant dash should appear. |
| 30 | static void show_giant_dash(SkCanvas* canvas) { |
| 31 | SkPaint paint; |
| 32 | |
| 33 | drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 34 | } |
| 35 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 36 | class DashingGM : public skiagm::GM { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 37 | public: |
| 38 | DashingGM() {} |
| 39 | |
| 40 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 41 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 42 | return kSkipTiled_Flag; |
| 43 | } |
| 44 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 45 | SkString onShortName() { |
| 46 | return SkString("dashing"); |
| 47 | } |
| 48 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 49 | SkISize onISize() { return skiagm::make_isize(640, 300); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 50 | |
| 51 | virtual void onDraw(SkCanvas* canvas) { |
| 52 | static const struct { |
| 53 | int fOnInterval; |
| 54 | int fOffInterval; |
| 55 | } gData[] = { |
| 56 | { 1, 1 }, |
| 57 | { 4, 1 }, |
| 58 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 59 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 60 | SkPaint paint; |
| 61 | paint.setStyle(SkPaint::kStroke_Style); |
| 62 | |
| 63 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 64 | canvas->translate(0, SK_ScalarHalf); |
| 65 | |
| 66 | for (int width = 0; width <= 2; ++width) { |
| 67 | for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 68 | for (int aa = 0; aa <= 1; ++aa) { |
| 69 | int w = width * width * width; |
| 70 | paint.setAntiAlias(SkToBool(aa)); |
| 71 | paint.setStrokeWidth(SkIntToScalar(w)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 72 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 73 | int scale = w ? w : 1; |
| 74 | |
| 75 | drawline(canvas, gData[data].fOnInterval * scale, |
| 76 | gData[data].fOffInterval * scale, |
| 77 | paint); |
| 78 | canvas->translate(0, SkIntToScalar(20)); |
| 79 | } |
| 80 | } |
| 81 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 82 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 83 | show_giant_dash(canvas); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 84 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 85 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 86 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 87 | /////////////////////////////////////////////////////////////////////////////// |
| 88 | |
| 89 | static void make_unit_star(SkPath* path, int n) { |
| 90 | SkScalar rad = -SK_ScalarPI / 2; |
| 91 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 93 | path->moveTo(0, -SK_Scalar1); |
| 94 | for (int i = 1; i < n; i++) { |
| 95 | rad += drad; |
| 96 | SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV); |
| 97 | path->lineTo(cosV, sinV); |
| 98 | } |
| 99 | path->close(); |
| 100 | } |
| 101 | |
| 102 | static void make_path_line(SkPath* path, const SkRect& bounds) { |
| 103 | path->moveTo(bounds.left(), bounds.top()); |
| 104 | path->lineTo(bounds.right(), bounds.bottom()); |
| 105 | } |
| 106 | |
| 107 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 108 | path->addRect(bounds); |
| 109 | } |
| 110 | |
| 111 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 112 | path->addOval(bounds); |
| 113 | } |
| 114 | |
| 115 | static void make_path_star(SkPath* path, const SkRect& bounds) { |
| 116 | make_unit_star(path, 5); |
| 117 | SkMatrix matrix; |
| 118 | matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit); |
| 119 | path->transform(matrix); |
| 120 | } |
| 121 | |
| 122 | class Dashing2GM : public skiagm::GM { |
| 123 | public: |
| 124 | Dashing2GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 125 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 126 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 127 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 128 | return kSkipTiled_Flag; |
| 129 | } |
| 130 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 131 | SkString onShortName() { |
| 132 | return SkString("dashing2"); |
| 133 | } |
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 | SkISize onISize() { return skiagm::make_isize(640, 480); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 137 | virtual void onDraw(SkCanvas* canvas) { |
| 138 | static const int gIntervals[] = { |
| 139 | 3, // 3 dashes: each count [0] followed by intervals [1..count] |
| 140 | 2, 10, 10, |
| 141 | 4, 20, 5, 5, 5, |
| 142 | 2, 2, 2 |
| 143 | }; |
| 144 | |
| 145 | void (*gProc[])(SkPath*, const SkRect&) = { |
| 146 | make_path_line, make_path_rect, make_path_oval, make_path_star, |
| 147 | }; |
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 | SkPaint paint; |
| 150 | paint.setAntiAlias(true); |
| 151 | paint.setStyle(SkPaint::kStroke_Style); |
| 152 | paint.setStrokeWidth(SkIntToScalar(6)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 154 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120)); |
| 155 | bounds.offset(SkIntToScalar(20), SkIntToScalar(20)); |
| 156 | SkScalar dx = bounds.width() * 4 / 3; |
| 157 | SkScalar dy = bounds.height() * 4 / 3; |
| 158 | |
| 159 | const int* intervals = &gIntervals[1]; |
| 160 | for (int y = 0; y < gIntervals[0]; ++y) { |
| 161 | SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough |
| 162 | int count = *intervals++; |
| 163 | for (int i = 0; i < count; ++i) { |
| 164 | vals[i] = SkIntToScalar(*intervals++); |
| 165 | } |
| 166 | SkScalar phase = vals[0] / 2; |
commit-bot@chromium.org | 0a2bf90 | 2014-02-20 20:40:19 +0000 | [diff] [blame] | 167 | paint.setPathEffect(SkDashPathEffect::Create(vals, count, phase))->unref(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 168 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 169 | for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) { |
| 170 | SkPath path; |
| 171 | SkRect r = bounds; |
| 172 | r.offset(x * dx, y * dy); |
| 173 | gProc[x](&path, r); |
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 | canvas->drawPath(path, paint); |
| 176 | } |
| 177 | } |
| 178 | } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | ////////////////////////////////////////////////////////////////////////////// |
| 182 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 183 | // Test out the on/off line dashing Chrome if fond of |
| 184 | class Dashing3GM : public skiagm::GM { |
| 185 | public: |
| 186 | Dashing3GM() {} |
| 187 | |
| 188 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 189 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 190 | return kSkipTiled_Flag; |
| 191 | } |
| 192 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 193 | SkString onShortName() { |
| 194 | return SkString("dashing3"); |
| 195 | } |
| 196 | |
| 197 | SkISize onISize() { return skiagm::make_isize(640, 480); } |
| 198 | |
| 199 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 200 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 201 | void drawDashedLines(SkCanvas* canvas, |
| 202 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 203 | SkScalar phase, |
| 204 | SkScalar dashLength, |
| 205 | int strokeWidth, |
| 206 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 207 | SkPaint p; |
| 208 | p.setColor(SK_ColorBLACK); |
| 209 | p.setStyle(SkPaint::kStroke_Style); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 210 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 211 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 212 | if (circles) { |
| 213 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 214 | } |
| 215 | |
| 216 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 217 | |
commit-bot@chromium.org | 35c03fb | 2014-03-31 18:52:51 +0000 | [diff] [blame] | 218 | p.setPathEffect(SkDashPathEffect::Create(intervals, 2, phase))->unref(); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 219 | |
| 220 | SkPoint pts[2]; |
| 221 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 222 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 223 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 224 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 225 | |
| 226 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 227 | } |
| 228 | |
| 229 | p.setAntiAlias(true); |
| 230 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 231 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 232 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 233 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 234 | |
| 235 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 236 | } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 237 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | virtual void onDraw(SkCanvas* canvas) { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 241 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 242 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 243 | canvas->translate(2, 0); |
| 244 | this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 245 | canvas->restore(); |
| 246 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 247 | // 1on/1off 1x1 squares with phase of .5 - rects fastpath (due to partial squares) |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 248 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 249 | canvas->translate(112, 0); |
| 250 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 251 | canvas->restore(); |
| 252 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 253 | // 1on/1off 1x1 squares with phase of 1 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 254 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 255 | canvas->translate(222, 0); |
| 256 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 257 | canvas->restore(); |
| 258 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 259 | // 1on/1off 1x1 squares with phase of 1 and non-integer length - rects fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 260 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 261 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 262 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 263 | canvas->restore(); |
| 264 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 265 | // 255on/255off 1x1 squares with phase of 0 - rects fast path |
| 266 | canvas->save(); |
| 267 | canvas->translate(446, 0); |
| 268 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false); |
| 269 | canvas->restore(); |
| 270 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 271 | // 1on/1off 3x3 squares with phase of 0 - points fast path |
| 272 | canvas->save(); |
| 273 | canvas->translate(2, 110); |
| 274 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false); |
| 275 | canvas->restore(); |
| 276 | |
| 277 | // 1on/1off 3x3 squares with phase of 1.5 - rects fast path |
| 278 | canvas->save(); |
| 279 | canvas->translate(112, 110); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 280 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 281 | canvas->restore(); |
| 282 | |
| 283 | // 1on/1off 1x1 circles with phase of 1 - no fast path yet |
| 284 | canvas->save(); |
| 285 | canvas->translate(2, 220); |
| 286 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true); |
| 287 | canvas->restore(); |
| 288 | |
| 289 | // 1on/1off 3x3 circles with phase of 1 - no fast path yet |
| 290 | canvas->save(); |
| 291 | canvas->translate(112, 220); |
| 292 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true); |
| 293 | canvas->restore(); |
| 294 | |
| 295 | // 1on/1off 1x1 squares with rotation - should break fast path |
| 296 | canvas->save(); |
| 297 | canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 298 | canvas->rotate(45); |
| 299 | canvas->translate(-50, -50); |
| 300 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 301 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 302 | canvas->restore(); |
| 303 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 304 | // 3on/3off 3x1 rects - should use rect fast path regardless of phase |
| 305 | for (int phase = 0; phase <= 3; ++phase) { |
| 306 | canvas->save(); |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 307 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 308 | SkIntToScalar(330)); |
| 309 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 310 | canvas->restore(); |
| 311 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | }; |
| 315 | |
| 316 | ////////////////////////////////////////////////////////////////////////////// |
| 317 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 318 | class Dashing4GM : public skiagm::GM { |
| 319 | public: |
| 320 | Dashing4GM() {} |
| 321 | |
| 322 | protected: |
| 323 | virtual uint32_t onGetFlags() const SK_OVERRIDE { |
| 324 | return kSkipTiled_Flag; |
| 325 | } |
| 326 | |
| 327 | SkString onShortName() { |
| 328 | return SkString("dashing4"); |
| 329 | } |
| 330 | |
| 331 | SkISize onISize() { return skiagm::make_isize(640, 950); } |
| 332 | |
| 333 | virtual void onDraw(SkCanvas* canvas) { |
| 334 | static const struct { |
| 335 | int fOnInterval; |
| 336 | int fOffInterval; |
| 337 | } gData[] = { |
| 338 | { 1, 1 }, |
| 339 | { 4, 2 }, |
| 340 | { 0, 4 }, // test for zero length on interval |
| 341 | }; |
| 342 | |
| 343 | SkPaint paint; |
| 344 | paint.setStyle(SkPaint::kStroke_Style); |
| 345 | |
| 346 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 347 | canvas->translate(0, SK_ScalarHalf); |
| 348 | |
| 349 | for (int width = 0; width <= 2; ++width) { |
| 350 | for (size_t data = 0; data < SK_ARRAY_COUNT(gData); ++data) { |
| 351 | for (int aa = 0; aa <= 1; ++aa) { |
| 352 | for (int cap = 0; cap <= 1; ++cap) { |
| 353 | int w = width * width * width; |
| 354 | paint.setAntiAlias(SkToBool(aa)); |
| 355 | paint.setStrokeWidth(SkIntToScalar(w)); |
| 356 | |
| 357 | SkToBool(cap) ? paint.setStrokeCap(SkPaint::kSquare_Cap) |
| 358 | : paint.setStrokeCap(SkPaint::kRound_Cap); |
| 359 | |
| 360 | int scale = w ? w : 1; |
| 361 | |
| 362 | drawline(canvas, gData[data].fOnInterval * scale, |
| 363 | gData[data].fOffInterval * scale, |
| 364 | paint); |
| 365 | canvas->translate(0, SkIntToScalar(20)); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | for (int aa = 0; aa <= 1; ++aa) { |
| 372 | paint.setAntiAlias(SkToBool(aa)); |
| 373 | paint.setStrokeWidth(8.f); |
| 374 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 375 | |
| 376 | // 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] | 377 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 378 | canvas->translate(0, SkIntToScalar(20)); |
| 379 | |
| 380 | // 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] | 381 | drawline(canvas, 32, 16, paint, 56.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 382 | canvas->translate(0, SkIntToScalar(20)); |
| 383 | |
| 384 | // 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] | 385 | drawline(canvas, 32, 16, paint, 584.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 386 | canvas->translate(0, SkIntToScalar(20)); |
| 387 | |
| 388 | // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from |
| 389 | // a canvas rotation) |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 390 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 391 | canvas->translate(0, SkIntToScalar(20)); |
| 392 | |
| 393 | // 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] | 394 | 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] | 395 | canvas->translate(0, SkIntToScalar(20)); |
| 396 | } |
| 397 | } |
| 398 | }; |
| 399 | |
| 400 | ////////////////////////////////////////////////////////////////////////////// |
| 401 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 402 | static skiagm::GM* F0(void*) { return new DashingGM; } |
| 403 | static skiagm::GM* F1(void*) { return new Dashing2GM; } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 404 | static skiagm::GM* F2(void*) { return new Dashing3GM; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 405 | static skiagm::GM* F3(void*) { return new Dashing4GM; } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 406 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 407 | static skiagm::GMRegistry gR0(F0); |
| 408 | static skiagm::GMRegistry gR1(F1); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 409 | static skiagm::GMRegistry gR2(F2); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 410 | static skiagm::GMRegistry gR3(F3); |