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