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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
| 9 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 10 | #include "include/core/SkColor.h" |
| 11 | #include "include/core/SkFont.h" |
| 12 | #include "include/core/SkMatrix.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkPaint.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkPath.h" |
| 15 | #include "include/core/SkPathEffect.h" |
| 16 | #include "include/core/SkPoint.h" |
| 17 | #include "include/core/SkRect.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
| 21 | #include "include/core/SkTypeface.h" |
| 22 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/effects/SkDashPathEffect.h" |
| 24 | #include "tools/ToolUtils.h" |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 25 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 26 | #include <math.h> |
| 27 | #include <initializer_list> |
| 28 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 29 | 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] | 30 | SkScalar finalX = SkIntToScalar(600), SkScalar finalY = SkIntToScalar(0), |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 31 | SkScalar phase = SkIntToScalar(0), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 32 | SkScalar startX = SkIntToScalar(0), SkScalar startY = SkIntToScalar(0)) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 33 | SkPaint p(paint); |
| 34 | |
| 35 | const SkScalar intervals[] = { |
| 36 | SkIntToScalar(on), |
| 37 | SkIntToScalar(off), |
| 38 | }; |
| 39 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 40 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 41 | canvas->drawLine(startX, startY, finalX, finalY, p); |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // earlier bug stopped us from drawing very long single-segment dashes, because |
| 45 | // SkPathMeasure was skipping very small delta-T values (nearlyzero). This is |
| 46 | // now fixes, so this giant dash should appear. |
| 47 | static void show_giant_dash(SkCanvas* canvas) { |
| 48 | SkPaint paint; |
| 49 | |
| 50 | drawline(canvas, 1, 1, paint, SkIntToScalar(20 * 1000)); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 51 | } |
| 52 | |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 53 | static void show_zero_len_dash(SkCanvas* canvas) { |
| 54 | SkPaint paint; |
| 55 | |
| 56 | drawline(canvas, 2, 2, paint, SkIntToScalar(0)); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 57 | paint.setStroke(true); |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 58 | paint.setStrokeWidth(SkIntToScalar(2)); |
| 59 | canvas->translate(0, SkIntToScalar(20)); |
| 60 | drawline(canvas, 4, 4, paint, SkIntToScalar(0)); |
| 61 | } |
| 62 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 63 | class DashingGM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 64 | SkString onShortName() override { return SkString("dashing"); } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 65 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 66 | SkISize onISize() override { return {640, 340}; } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 67 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 68 | void onDraw(SkCanvas* canvas) override { |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 69 | struct Intervals { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 70 | int fOnInterval; |
| 71 | int fOffInterval; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 72 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 73 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 74 | SkPaint paint; |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 75 | paint.setStroke(true); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 76 | |
| 77 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 78 | canvas->translate(0, SK_ScalarHalf); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 79 | for (int width = 0; width <= 2; ++width) { |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 80 | for (const Intervals& data : {Intervals{1, 1}, |
| 81 | Intervals{4, 1}}) { |
| 82 | for (bool aa : {false, true}) { |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 83 | int w = width * width * width; |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 84 | paint.setAntiAlias(aa); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 85 | paint.setStrokeWidth(SkIntToScalar(w)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 86 | |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 87 | int scale = w ? w : 1; |
| 88 | |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 89 | drawline(canvas, data.fOnInterval * scale, data.fOffInterval * scale, |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 90 | paint); |
| 91 | canvas->translate(0, SkIntToScalar(20)); |
| 92 | } |
| 93 | } |
| 94 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 95 | |
reed@google.com | de1837b | 2012-05-21 16:47:43 +0000 | [diff] [blame] | 96 | show_giant_dash(canvas); |
egdaniel | 21402e3 | 2014-11-05 05:02:27 -0800 | [diff] [blame] | 97 | canvas->translate(0, SkIntToScalar(20)); |
| 98 | show_zero_len_dash(canvas); |
egdaniel | c00389e | 2015-10-05 08:11:49 -0700 | [diff] [blame] | 99 | canvas->translate(0, SkIntToScalar(20)); |
| 100 | // Draw 0 on, 0 off dashed line |
| 101 | paint.setStrokeWidth(SkIntToScalar(8)); |
| 102 | drawline(canvas, 0, 0, paint); |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 103 | } |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 104 | }; |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 105 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 106 | /////////////////////////////////////////////////////////////////////////////// |
| 107 | |
| 108 | static void make_unit_star(SkPath* path, int n) { |
| 109 | SkScalar rad = -SK_ScalarPI / 2; |
| 110 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 112 | path->moveTo(0, -SK_Scalar1); |
| 113 | for (int i = 1; i < n; i++) { |
| 114 | rad += drad; |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 115 | path->lineTo(SkScalarCos(rad), SkScalarSin(rad)); |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 116 | } |
| 117 | path->close(); |
| 118 | } |
| 119 | |
| 120 | static void make_path_line(SkPath* path, const SkRect& bounds) { |
| 121 | path->moveTo(bounds.left(), bounds.top()); |
| 122 | path->lineTo(bounds.right(), bounds.bottom()); |
| 123 | } |
| 124 | |
| 125 | static void make_path_rect(SkPath* path, const SkRect& bounds) { |
| 126 | path->addRect(bounds); |
| 127 | } |
| 128 | |
| 129 | static void make_path_oval(SkPath* path, const SkRect& bounds) { |
| 130 | path->addOval(bounds); |
| 131 | } |
| 132 | |
| 133 | static void make_path_star(SkPath* path, const SkRect& bounds) { |
| 134 | make_unit_star(path, 5); |
| 135 | SkMatrix matrix; |
| 136 | matrix.setRectToRect(path->getBounds(), bounds, SkMatrix::kCenter_ScaleToFit); |
| 137 | path->transform(matrix); |
| 138 | } |
| 139 | |
| 140 | class Dashing2GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 141 | SkString onShortName() override { return SkString("dashing2"); } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 143 | SkISize onISize() override { return {640, 480}; } |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 144 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 145 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 146 | constexpr int gIntervals[] = { |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 147 | 3, // 3 dashes: each count [0] followed by intervals [1..count] |
| 148 | 2, 10, 10, |
| 149 | 4, 20, 5, 5, 5, |
| 150 | 2, 2, 2 |
| 151 | }; |
| 152 | |
| 153 | void (*gProc[])(SkPath*, const SkRect&) = { |
| 154 | make_path_line, make_path_rect, make_path_oval, make_path_star, |
| 155 | }; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 156 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 157 | SkPaint paint; |
| 158 | paint.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 159 | paint.setStroke(true); |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 160 | paint.setStrokeWidth(SkIntToScalar(6)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 161 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 162 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(120), SkIntToScalar(120)); |
| 163 | bounds.offset(SkIntToScalar(20), SkIntToScalar(20)); |
| 164 | SkScalar dx = bounds.width() * 4 / 3; |
| 165 | SkScalar dy = bounds.height() * 4 / 3; |
| 166 | |
| 167 | const int* intervals = &gIntervals[1]; |
| 168 | for (int y = 0; y < gIntervals[0]; ++y) { |
| 169 | SkScalar vals[SK_ARRAY_COUNT(gIntervals)]; // more than enough |
| 170 | int count = *intervals++; |
| 171 | for (int i = 0; i < count; ++i) { |
| 172 | vals[i] = SkIntToScalar(*intervals++); |
| 173 | } |
| 174 | SkScalar phase = vals[0] / 2; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 175 | paint.setPathEffect(SkDashPathEffect::Make(vals, count, phase)); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 176 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 177 | for (size_t x = 0; x < SK_ARRAY_COUNT(gProc); ++x) { |
| 178 | SkPath path; |
| 179 | SkRect r = bounds; |
| 180 | r.offset(x * dx, y * dy); |
| 181 | gProc[x](&path, r); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | |
reed@google.com | 21384df | 2012-05-18 17:59:08 +0000 | [diff] [blame] | 183 | canvas->drawPath(path, paint); |
| 184 | } |
| 185 | } |
| 186 | } |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | ////////////////////////////////////////////////////////////////////////////// |
| 190 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 191 | // Test out the on/off line dashing Chrome if fond of |
| 192 | class Dashing3GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 193 | SkString onShortName() override { return SkString("dashing3"); } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 194 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 195 | SkISize onISize() override { return {640, 480}; } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 196 | |
| 197 | // Draw a 100x100 block of dashed lines. The horizontal ones are BW |
| 198 | // while the vertical ones are AA. |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 199 | void drawDashedLines(SkCanvas* canvas, |
| 200 | SkScalar lineLength, |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 201 | SkScalar phase, |
| 202 | SkScalar dashLength, |
| 203 | int strokeWidth, |
| 204 | bool circles) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 205 | SkPaint p; |
| 206 | p.setColor(SK_ColorBLACK); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 207 | p.setStroke(true); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 208 | p.setStrokeWidth(SkIntToScalar(strokeWidth)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 209 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 210 | if (circles) { |
| 211 | p.setStrokeCap(SkPaint::kRound_Cap); |
| 212 | } |
| 213 | |
| 214 | SkScalar intervals[2] = { dashLength, dashLength }; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 215 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 216 | p.setPathEffect(SkDashPathEffect::Make(intervals, 2, phase)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 217 | |
| 218 | SkPoint pts[2]; |
| 219 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 220 | for (int y = 0; y < 100; y += 10*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 221 | pts[0].set(0, SkIntToScalar(y)); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 222 | pts[1].set(lineLength, SkIntToScalar(y)); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 223 | |
| 224 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 225 | } |
| 226 | |
| 227 | p.setAntiAlias(true); |
| 228 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 229 | for (int x = 0; x < 100; x += 14*strokeWidth) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 230 | pts[0].set(SkIntToScalar(x), 0); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 231 | pts[1].set(SkIntToScalar(x), lineLength); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 232 | |
| 233 | canvas->drawPoints(SkCanvas::kLines_PointMode, 2, pts, p); |
| 234 | } |
| 235 | } |
| 236 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 237 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 238 | // 1on/1off 1x1 squares with phase of 0 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 239 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 240 | canvas->translate(2, 0); |
| 241 | this->drawDashedLines(canvas, 100, 0, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 242 | canvas->restore(); |
| 243 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 244 | // 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] | 245 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 246 | canvas->translate(112, 0); |
| 247 | this->drawDashedLines(canvas, 100, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 248 | canvas->restore(); |
| 249 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 250 | // 1on/1off 1x1 squares with phase of 1 - points fastpath |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 251 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 252 | canvas->translate(222, 0); |
| 253 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 254 | canvas->restore(); |
| 255 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 256 | // 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] | 257 | canvas->save(); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 258 | canvas->translate(332, 0); |
reed@google.com | 140d728 | 2013-01-07 20:25:04 +0000 | [diff] [blame] | 259 | this->drawDashedLines(canvas, 99.5f, SK_ScalarHalf, SK_Scalar1, 1, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 260 | canvas->restore(); |
| 261 | |
robertphillips@google.com | 5c4d558 | 2013-01-15 12:53:31 +0000 | [diff] [blame] | 262 | // 255on/255off 1x1 squares with phase of 0 - rects fast path |
| 263 | canvas->save(); |
| 264 | canvas->translate(446, 0); |
| 265 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(255), 1, false); |
| 266 | canvas->restore(); |
| 267 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 268 | // 1on/1off 3x3 squares with phase of 0 - points fast path |
| 269 | canvas->save(); |
| 270 | canvas->translate(2, 110); |
| 271 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, false); |
| 272 | canvas->restore(); |
| 273 | |
| 274 | // 1on/1off 3x3 squares with phase of 1.5 - rects fast path |
| 275 | canvas->save(); |
| 276 | canvas->translate(112, 110); |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 277 | this->drawDashedLines(canvas, 100, 1.5f, SkIntToScalar(3), 3, false); |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 278 | canvas->restore(); |
| 279 | |
| 280 | // 1on/1off 1x1 circles with phase of 1 - no fast path yet |
| 281 | canvas->save(); |
| 282 | canvas->translate(2, 220); |
| 283 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, true); |
| 284 | canvas->restore(); |
| 285 | |
| 286 | // 1on/1off 3x3 circles with phase of 1 - no fast path yet |
| 287 | canvas->save(); |
| 288 | canvas->translate(112, 220); |
| 289 | this->drawDashedLines(canvas, 100, 0, SkIntToScalar(3), 3, true); |
| 290 | canvas->restore(); |
| 291 | |
| 292 | // 1on/1off 1x1 squares with rotation - should break fast path |
| 293 | canvas->save(); |
| 294 | canvas->translate(332+SK_ScalarRoot2Over2*100, 110+SK_ScalarRoot2Over2*100); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 295 | canvas->rotate(45); |
| 296 | canvas->translate(-50, -50); |
| 297 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 298 | this->drawDashedLines(canvas, 100, SK_Scalar1, SK_Scalar1, 1, false); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 299 | canvas->restore(); |
| 300 | |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 301 | // 3on/3off 3x1 rects - should use rect fast path regardless of phase |
| 302 | for (int phase = 0; phase <= 3; ++phase) { |
| 303 | canvas->save(); |
skia.committer@gmail.com | 73b140a | 2012-12-05 02:01:21 +0000 | [diff] [blame] | 304 | canvas->translate(SkIntToScalar(phase*110+2), |
robertphillips@google.com | 8c685c5 | 2012-12-04 20:34:11 +0000 | [diff] [blame] | 305 | SkIntToScalar(330)); |
| 306 | this->drawDashedLines(canvas, 100, SkIntToScalar(phase), SkIntToScalar(3), 1, false); |
| 307 | canvas->restore(); |
| 308 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | }; |
| 312 | |
| 313 | ////////////////////////////////////////////////////////////////////////////// |
| 314 | |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 315 | class Dashing4GM : public skiagm::GM { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 316 | SkString onShortName() override { return SkString("dashing4"); } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 317 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 318 | SkISize onISize() override { return {640, 1100}; } |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 319 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 320 | void onDraw(SkCanvas* canvas) override { |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 321 | struct Intervals { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 322 | int fOnInterval; |
| 323 | int fOffInterval; |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | SkPaint paint; |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 327 | paint.setStroke(true); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 328 | |
| 329 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
Greg Daniel | 95581bb | 2017-06-30 10:36:38 -0400 | [diff] [blame] | 330 | canvas->translate(SK_ScalarHalf, SK_ScalarHalf); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 331 | |
| 332 | for (int width = 0; width <= 2; ++width) { |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 333 | for (const Intervals& data : {Intervals{1, 1}, |
| 334 | Intervals{4, 2}, |
| 335 | Intervals{0, 4}}) { // test for zero length on interval |
| 336 | for (bool aa : {false, true}) { |
| 337 | for (auto cap : {SkPaint::kRound_Cap, SkPaint::kSquare_Cap}) { |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 338 | int w = width * width * width; |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 339 | paint.setAntiAlias(aa); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 340 | paint.setStrokeWidth(SkIntToScalar(w)); |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 341 | paint.setStrokeCap(cap); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 342 | |
| 343 | int scale = w ? w : 1; |
| 344 | |
John Stiles | dbcf680 | 2020-05-18 10:58:51 -0400 | [diff] [blame] | 345 | drawline(canvas, data.fOnInterval * scale, data.fOffInterval * scale, |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 346 | paint); |
| 347 | canvas->translate(0, SkIntToScalar(20)); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | for (int aa = 0; aa <= 1; ++aa) { |
| 354 | paint.setAntiAlias(SkToBool(aa)); |
| 355 | paint.setStrokeWidth(8.f); |
| 356 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 357 | // 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] | 358 | drawline(canvas, 32, 16, paint, 20.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 359 | canvas->translate(0, SkIntToScalar(20)); |
| 360 | |
| 361 | // 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] | 362 | drawline(canvas, 32, 16, paint, 56.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 363 | canvas->translate(0, SkIntToScalar(20)); |
| 364 | |
| 365 | // 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] | 366 | drawline(canvas, 32, 16, paint, 584.f, 0, 5.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 367 | canvas->translate(0, SkIntToScalar(20)); |
| 368 | |
| 369 | // Diagonal dash line where src pnts are not axis aligned (as apposed to being diagonal from |
| 370 | // a canvas rotation) |
commit-bot@chromium.org | 71db882 | 2014-05-19 14:59:04 +0000 | [diff] [blame] | 371 | drawline(canvas, 32, 16, paint, 600.f, 30.f); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 372 | canvas->translate(0, SkIntToScalar(20)); |
| 373 | |
| 374 | // 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] | 375 | 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] | 376 | canvas->translate(0, SkIntToScalar(20)); |
| 377 | } |
Greg Daniel | 5fb3056 | 2017-06-29 12:27:48 -0400 | [diff] [blame] | 378 | |
| 379 | // Test overlapping circles. |
| 380 | canvas->translate(SkIntToScalar(5), SkIntToScalar(20)); |
| 381 | paint.setAntiAlias(true); |
| 382 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 383 | paint.setColor(0x44000000); |
| 384 | paint.setStrokeWidth(40); |
| 385 | drawline(canvas, 0, 30, paint); |
| 386 | |
| 387 | canvas->translate(0, SkIntToScalar(50)); |
| 388 | paint.setStrokeCap(SkPaint::kSquare_Cap); |
| 389 | drawline(canvas, 0, 30, paint); |
Greg Daniel | c96f9b5 | 2017-12-07 15:00:06 -0500 | [diff] [blame] | 390 | |
| 391 | // Test we draw the cap when the line length is zero. |
| 392 | canvas->translate(0, SkIntToScalar(50)); |
| 393 | paint.setStrokeCap(SkPaint::kRound_Cap); |
| 394 | paint.setColor(0xFF000000); |
| 395 | paint.setStrokeWidth(11); |
| 396 | drawline(canvas, 0, 30, paint, 0); |
| 397 | |
| 398 | canvas->translate(SkIntToScalar(100), 0); |
| 399 | drawline(canvas, 1, 30, paint, 0); |
commit-bot@chromium.org | 628ed0b | 2014-05-19 14:32:49 +0000 | [diff] [blame] | 400 | } |
| 401 | }; |
| 402 | |
| 403 | ////////////////////////////////////////////////////////////////////////////// |
| 404 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 405 | class Dashing5GM : public skiagm::GM { |
| 406 | public: |
| 407 | Dashing5GM(bool doAA) : fDoAA(doAA) {} |
reed@google.com | 35a81df | 2012-05-04 21:49:27 +0000 | [diff] [blame] | 408 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 409 | private: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 410 | bool runAsBench() const override { return true; } |
mtklein | cf5d9c9 | 2015-01-23 10:31:45 -0800 | [diff] [blame] | 411 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 412 | SkString onShortName() override { return SkString(fDoAA ? "dashing5_aa" : "dashing5_bw"); } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 413 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 414 | SkISize onISize() override { return {400, 200}; } |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 415 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 416 | void onDraw(SkCanvas* canvas) override { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 417 | constexpr int kOn = 4; |
| 418 | constexpr int kOff = 4; |
| 419 | constexpr int kIntervalLength = kOn + kOff; |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 420 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 421 | constexpr SkColor gColors[kIntervalLength] = { |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 422 | SK_ColorRED, |
| 423 | SK_ColorGREEN, |
| 424 | SK_ColorBLUE, |
| 425 | SK_ColorCYAN, |
| 426 | SK_ColorMAGENTA, |
| 427 | SK_ColorYELLOW, |
| 428 | SK_ColorGRAY, |
| 429 | SK_ColorDKGRAY |
| 430 | }; |
| 431 | |
| 432 | SkPaint paint; |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 433 | paint.setStroke(true); |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 434 | |
| 435 | paint.setAntiAlias(fDoAA); |
| 436 | |
| 437 | SkMatrix rot; |
| 438 | rot.setRotate(90); |
| 439 | SkASSERT(rot.rectStaysRect()); |
| 440 | |
| 441 | canvas->concat(rot); |
| 442 | |
| 443 | int sign; // used to toggle the direction of the lines |
| 444 | int phase = 0; |
| 445 | |
| 446 | for (int x = 0; x < 200; x += 10) { |
| 447 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 448 | paint.setColor(gColors[phase]); |
| 449 | sign = (x % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 450 | drawline(canvas, kOn, kOff, paint, |
| 451 | SkIntToScalar(x), -sign * SkIntToScalar(10003), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 452 | SkIntToScalar(phase), |
| 453 | SkIntToScalar(x), sign * SkIntToScalar(10003)); |
| 454 | phase = (phase + 1) % kIntervalLength; |
| 455 | } |
| 456 | |
| 457 | for (int y = -400; y < 0; y += 10) { |
| 458 | paint.setStrokeWidth(SkIntToScalar(phase+1)); |
| 459 | paint.setColor(gColors[phase]); |
| 460 | sign = (y % 20) ? 1 : -1; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 461 | drawline(canvas, kOn, kOff, paint, |
| 462 | -sign * SkIntToScalar(10003), SkIntToScalar(y), |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 463 | SkIntToScalar(phase), |
| 464 | sign * SkIntToScalar(10003), SkIntToScalar(y)); |
| 465 | phase = (phase + 1) % kIntervalLength; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | private: |
| 470 | bool fDoAA; |
| 471 | }; |
| 472 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 473 | DEF_SIMPLE_GM(longpathdash, canvas, 612, 612) { |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 474 | SkPath lines; |
| 475 | for (int x = 32; x < 256; x += 16) { |
| 476 | for (SkScalar a = 0; a < 3.141592f * 2; a += 0.03141592f) { |
| 477 | SkPoint pts[2] = { |
| 478 | { 256 + (float) sin(a) * x, |
| 479 | 256 + (float) cos(a) * x }, |
| 480 | { 256 + (float) sin(a + 3.141592 / 3) * (x + 64), |
| 481 | 256 + (float) cos(a + 3.141592 / 3) * (x + 64) } |
| 482 | }; |
| 483 | lines.moveTo(pts[0]); |
| 484 | for (SkScalar i = 0; i < 1; i += 0.05f) { |
| 485 | lines.lineTo(pts[0].fX * (1 - i) + pts[1].fX * i, |
| 486 | pts[0].fY * (1 - i) + pts[1].fY * i); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | SkPaint p; |
| 491 | p.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 492 | p.setStroke(true); |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 493 | p.setStrokeWidth(1); |
| 494 | const SkScalar intervals[] = { 1, 1 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 495 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 496 | |
reed | 6dc14aa | 2016-04-11 07:46:38 -0700 | [diff] [blame] | 497 | canvas->translate(50, 50); |
caryclark | f97aa74 | 2015-12-18 07:03:13 -0800 | [diff] [blame] | 498 | canvas->drawPath(lines, p); |
| 499 | } |
| 500 | |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 501 | DEF_SIMPLE_GM(longlinedash, canvas, 512, 512) { |
| 502 | SkPaint p; |
| 503 | p.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 504 | p.setStroke(true); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 505 | p.setStrokeWidth(80); |
| 506 | |
| 507 | const SkScalar intervals[] = { 2, 2 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 508 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 509 | canvas->drawRect(SkRect::MakeXYWH(-10000, 100, 20000, 20), p); |
| 510 | } |
| 511 | |
| 512 | DEF_SIMPLE_GM(longwavyline, canvas, 512, 512) { |
| 513 | SkPaint p; |
| 514 | p.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 515 | p.setStroke(true); |
caryclark | 70e6d60 | 2016-01-30 10:11:21 -0800 | [diff] [blame] | 516 | p.setStrokeWidth(2); |
| 517 | |
| 518 | SkPath wavy; |
| 519 | wavy.moveTo(-10000, 100); |
| 520 | for (SkScalar i = -10000; i < 10000; i += 20) { |
| 521 | wavy.quadTo(i + 5, 95, i + 10, 100); |
| 522 | wavy.quadTo(i + 15, 105, i + 20, 100); |
| 523 | } |
| 524 | canvas->drawPath(wavy, p); |
| 525 | } |
| 526 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 527 | DEF_SIMPLE_GM(dashtextcaps, canvas, 512, 512) { |
| 528 | SkPaint p; |
| 529 | p.setAntiAlias(true); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 530 | p.setStroke(true); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 531 | p.setStrokeWidth(10); |
| 532 | p.setStrokeCap(SkPaint::kRound_Cap); |
caryclark | 1aaadbd | 2016-03-17 07:01:49 -0700 | [diff] [blame] | 533 | p.setStrokeJoin(SkPaint::kRound_Join); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 534 | p.setARGB(0xff, 0xbb, 0x00, 0x00); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 535 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 536 | SkFont font(ToolUtils::create_portable_typeface(), 100); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 537 | |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 538 | const SkScalar intervals[] = { 12, 12 }; |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 539 | p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), 0)); |
Mike Reed | 4de2f1f | 2019-01-05 16:35:13 -0500 | [diff] [blame] | 540 | canvas->drawString("Sausages", 10, 90, font, p); |
caryclark | d7ea92f | 2016-03-16 07:34:02 -0700 | [diff] [blame] | 541 | canvas->drawLine(8, 120, 456, 120, p); |
| 542 | } |
| 543 | |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 544 | DEF_SIMPLE_GM(dash_line_zero_off_interval, canvas, 160, 330) { |
| 545 | static constexpr SkScalar kIntervals[] = {5.f, 0.f, 2.f, 0.f}; |
| 546 | SkPaint dashPaint; |
| 547 | dashPaint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f)); |
| 548 | SkASSERT(dashPaint.getPathEffect()); |
Mike Reed | 1963009 | 2020-05-18 21:25:44 -0400 | [diff] [blame] | 549 | dashPaint.setStroke(true); |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 550 | dashPaint.setStrokeWidth(20.f); |
| 551 | static constexpr struct { |
| 552 | SkPoint fA, fB; |
| 553 | } kLines[] = {{{0.5f, 0.5f}, {30.5f, 0.5f}}, // horizontal |
| 554 | {{0.5f, 0.5f}, {0.5f, 30.5f}}, // vertical |
| 555 | {{0.5f, 0.5f}, {0.5f, 0.5f}}, // point |
| 556 | {{0.5f, 0.5f}, {25.5f, 25.5f}}}; // diagonal |
| 557 | SkScalar pad = 5.f + dashPaint.getStrokeWidth(); |
| 558 | canvas->translate(pad / 2.f, pad / 2.f); |
| 559 | canvas->save(); |
| 560 | SkScalar h = 0.f; |
| 561 | for (const auto& line : kLines) { |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 562 | h = std::max(h, SkScalarAbs(line.fA.fY - line.fB.fY)); |
Brian Salomon | 72f78c3 | 2017-12-21 11:56:42 -0500 | [diff] [blame] | 563 | } |
| 564 | for (const auto& line : kLines) { |
| 565 | SkScalar w = SkScalarAbs(line.fA.fX - line.fB.fX); |
| 566 | for (auto cap : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) { |
| 567 | dashPaint.setStrokeCap(cap); |
| 568 | for (auto aa : {false, true}) { |
| 569 | dashPaint.setAntiAlias(aa); |
| 570 | canvas->drawLine(line.fA, line.fB, dashPaint); |
| 571 | canvas->translate(0.f, pad + h); |
| 572 | } |
| 573 | } |
| 574 | canvas->restore(); |
| 575 | canvas->translate(pad + w, 0.f); |
| 576 | canvas->save(); |
| 577 | } |
| 578 | } |
| 579 | |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 580 | DEF_SIMPLE_GM(thin_aa_dash_lines, canvas, 330, 110) { |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 581 | SkPaint paint; |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 582 | static constexpr SkScalar kScale = 100.f; |
| 583 | static constexpr SkScalar kIntervals[] = {10/kScale, 5/kScale}; |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 584 | paint.setPathEffect(SkDashPathEffect::Make(kIntervals, SK_ARRAY_COUNT(kIntervals), 0.f)); |
| 585 | paint.setAntiAlias(true); |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 586 | paint.setStrokeWidth(0.25f/kScale); |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 587 | // substep moves the subpixel offset every iteration. |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 588 | static constexpr SkScalar kSubstep = 0.05f/kScale; |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 589 | // We will draw a grid of horiz/vertical lines that pass through each other's off intervals. |
| 590 | static constexpr SkScalar kStep = kIntervals[0] + kIntervals[1]; |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 591 | canvas->scale(kScale, kScale); |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 592 | canvas->translate(kIntervals[1], kIntervals[1]); |
Brian Salomon | 518fd4d | 2020-05-08 15:32:28 -0400 | [diff] [blame] | 593 | for (auto c : {SkPaint::kButt_Cap, SkPaint::kSquare_Cap, SkPaint::kRound_Cap}) { |
| 594 | paint.setStrokeCap(c); |
| 595 | for (SkScalar x = -.5f*kIntervals[1]; x < 105/kScale; x += (kStep + kSubstep)) { |
| 596 | canvas->drawLine({x, 0}, {x, 100/kScale}, paint); |
| 597 | canvas->drawLine({0, x}, {100/kScale, x}, paint); |
| 598 | } |
| 599 | canvas->translate(110/kScale, 0); |
Brian Salomon | c683912 | 2020-05-07 12:43:19 -0400 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 603 | ////////////////////////////////////////////////////////////////////////////// |
| 604 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 605 | DEF_GM(return new DashingGM;) |
| 606 | DEF_GM(return new Dashing2GM;) |
| 607 | DEF_GM(return new Dashing3GM;) |
| 608 | DEF_GM(return new Dashing4GM;) |
| 609 | DEF_GM(return new Dashing5GM(true);) |
| 610 | DEF_GM(return new Dashing5GM(false);) |