epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 11 | #include "gm.h" |
| 12 | #include "SkRandom.h" |
| 13 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 14 | #define W 400 |
| 15 | #define H 400 |
| 16 | #define N 50 |
| 17 | |
| 18 | static const SkScalar SW = SkIntToScalar(W); |
| 19 | static const SkScalar SH = SkIntToScalar(H); |
| 20 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 21 | static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 22 | SkScalar x = rand.nextUScalar1() * W; |
| 23 | SkScalar y = rand.nextUScalar1() * H; |
| 24 | SkScalar w = rand.nextUScalar1() * (W >> 2); |
| 25 | SkScalar h = rand.nextUScalar1() * (H >> 2); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 26 | SkScalar hoffset = rand.nextSScalar1(); |
| 27 | SkScalar woffset = rand.nextSScalar1(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 28 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 29 | r->set(x, y, x + w, y + h); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 30 | r->offset(-w/2 + woffset, -h/2 + hoffset); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 32 | paint->setColor(rand.nextU()); |
| 33 | paint->setAlpha(0xFF); |
| 34 | } |
| 35 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 36 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 37 | class StrokesGM : public skiagm::GM { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 38 | public: |
| 39 | StrokesGM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 40 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 41 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 42 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 43 | SkString onShortName() override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 44 | return SkString("strokes_round"); |
| 45 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | SkISize onISize() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 48 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 49 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 51 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | paint.setStyle(SkPaint::kStroke_Style); |
| 54 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 56 | for (int y = 0; y < 2; y++) { |
| 57 | paint.setAntiAlias(!!y); |
| 58 | SkAutoCanvasRestore acr(canvas, true); |
| 59 | canvas->translate(0, SH * y); |
| 60 | canvas->clipRect(SkRect::MakeLTRB( |
| 61 | SkIntToScalar(2), SkIntToScalar(2) |
| 62 | , SW - SkIntToScalar(2), SH - SkIntToScalar(2) |
| 63 | )); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 65 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 66 | for (int i = 0; i < N; i++) { |
| 67 | SkRect r; |
| 68 | rnd_rect(&r, &paint, rand); |
| 69 | canvas->drawOval(r, paint); |
| 70 | rnd_rect(&r, &paint, rand); |
| 71 | canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint); |
| 72 | rnd_rect(&r, &paint, rand); |
| 73 | } |
| 74 | } |
| 75 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 77 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 78 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 81 | class Strokes2GM : public skiagm::GM { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 82 | SkPath fPath; |
caryclark | 63c684a | 2015-02-25 09:04:04 -0800 | [diff] [blame] | 83 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 84 | void onOnceBeforeDraw() override { |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 85 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 86 | fPath.moveTo(0, 0); |
| 87 | for (int i = 0; i < 13; i++) { |
| 88 | SkScalar x = rand.nextUScalar1() * (W >> 1); |
| 89 | SkScalar y = rand.nextUScalar1() * (H >> 1); |
| 90 | fPath.lineTo(x, y); |
| 91 | } |
| 92 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 93 | |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 94 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 95 | SkString onShortName() override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 96 | return SkString("strokes_poly"); |
| 97 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 98 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 99 | SkISize onISize() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 100 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 101 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 102 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 103 | static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) { |
| 104 | SkMatrix matrix; |
| 105 | matrix.setRotate(angle, px, py); |
| 106 | canvas->concat(matrix); |
| 107 | } |
| 108 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 109 | void onDraw(SkCanvas* canvas) override { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 110 | canvas->drawColor(SK_ColorWHITE); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 111 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 112 | SkPaint paint; |
| 113 | paint.setStyle(SkPaint::kStroke_Style); |
| 114 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 115 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 116 | for (int y = 0; y < 2; y++) { |
| 117 | paint.setAntiAlias(!!y); |
| 118 | SkAutoCanvasRestore acr(canvas, true); |
| 119 | canvas->translate(0, SH * y); |
| 120 | canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2), |
| 121 | SkIntToScalar(2), |
| 122 | SW - SkIntToScalar(2), |
| 123 | SH - SkIntToScalar(2))); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 125 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 126 | for (int i = 0; i < N/2; i++) { |
| 127 | SkRect r; |
| 128 | rnd_rect(&r, &paint, rand); |
| 129 | rotate(SkIntToScalar(15), SW/2, SH/2, canvas); |
| 130 | canvas->drawPath(fPath, paint); |
| 131 | } |
| 132 | } |
| 133 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 134 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 135 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 136 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 141 | static SkRect inset(const SkRect& r) { |
| 142 | SkRect rr(r); |
| 143 | rr.inset(r.width()/10, r.height()/10); |
| 144 | return rr; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 145 | } |
| 146 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 147 | class Strokes3GM : public skiagm::GM { |
| 148 | static void make0(SkPath* path, const SkRect& bounds, SkString* title) { |
| 149 | path->addRect(bounds, SkPath::kCW_Direction); |
| 150 | path->addRect(inset(bounds), SkPath::kCW_Direction); |
| 151 | title->set("CW CW"); |
| 152 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 153 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 154 | static void make1(SkPath* path, const SkRect& bounds, SkString* title) { |
| 155 | path->addRect(bounds, SkPath::kCW_Direction); |
| 156 | path->addRect(inset(bounds), SkPath::kCCW_Direction); |
| 157 | title->set("CW CCW"); |
| 158 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 160 | static void make2(SkPath* path, const SkRect& bounds, SkString* title) { |
| 161 | path->addOval(bounds, SkPath::kCW_Direction); |
| 162 | path->addOval(inset(bounds), SkPath::kCW_Direction); |
| 163 | title->set("CW CW"); |
| 164 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 165 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 166 | static void make3(SkPath* path, const SkRect& bounds, SkString* title) { |
| 167 | path->addOval(bounds, SkPath::kCW_Direction); |
| 168 | path->addOval(inset(bounds), SkPath::kCCW_Direction); |
| 169 | title->set("CW CCW"); |
| 170 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 171 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 172 | static void make4(SkPath* path, const SkRect& bounds, SkString* title) { |
| 173 | path->addRect(bounds, SkPath::kCW_Direction); |
| 174 | SkRect r = bounds; |
| 175 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 176 | path->addOval(r, SkPath::kCW_Direction); |
| 177 | title->set("CW CW"); |
| 178 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 179 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 180 | static void make5(SkPath* path, const SkRect& bounds, SkString* title) { |
| 181 | path->addRect(bounds, SkPath::kCW_Direction); |
| 182 | SkRect r = bounds; |
| 183 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 184 | path->addOval(r, SkPath::kCCW_Direction); |
| 185 | title->set("CW CCW"); |
| 186 | } |
| 187 | |
| 188 | public: |
| 189 | Strokes3GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 190 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 191 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 192 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 193 | SkString onShortName() override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 194 | return SkString("strokes3"); |
| 195 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 197 | SkISize onISize() override { |
caryclark | 3760457 | 2015-02-23 06:51:04 -0800 | [diff] [blame] | 198 | return SkISize::Make(1500, 1500); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 199 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 200 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 201 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 202 | SkPaint origPaint; |
| 203 | origPaint.setAntiAlias(true); |
| 204 | origPaint.setStyle(SkPaint::kStroke_Style); |
| 205 | SkPaint fillPaint(origPaint); |
| 206 | fillPaint.setColor(SK_ColorRED); |
| 207 | SkPaint strokePaint(origPaint); |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame^] | 208 | strokePaint.setColor(sk_tool_utils::color_to_565(0xFF4444FF)); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 209 | |
| 210 | void (*procs[])(SkPath*, const SkRect&, SkString*) = { |
| 211 | make0, make1, make2, make3, make4, make5 |
| 212 | }; |
| 213 | |
caryclark | 3760457 | 2015-02-23 06:51:04 -0800 | [diff] [blame] | 214 | canvas->translate(SkIntToScalar(20), SkIntToScalar(80)); |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 215 | |
| 216 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)); |
| 217 | SkScalar dx = bounds.width() * 4/3; |
| 218 | SkScalar dy = bounds.height() * 5; |
| 219 | |
| 220 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
| 221 | SkPath orig; |
| 222 | SkString str; |
| 223 | procs[i](&orig, bounds, &str); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 224 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 225 | canvas->save(); |
| 226 | for (int j = 0; j < 13; ++j) { |
| 227 | strokePaint.setStrokeWidth(SK_Scalar1 * j * j); |
| 228 | canvas->drawPath(orig, strokePaint); |
| 229 | canvas->drawPath(orig, origPaint); |
| 230 | SkPath fill; |
| 231 | strokePaint.getFillPath(orig, &fill); |
| 232 | canvas->drawPath(fill, fillPaint); |
| 233 | canvas->translate(dx + strokePaint.getStrokeWidth(), 0); |
| 234 | } |
| 235 | canvas->restore(); |
| 236 | canvas->translate(0, dy); |
| 237 | } |
| 238 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 239 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 240 | private: |
| 241 | typedef skiagm::GM INHERITED; |
| 242 | }; |
| 243 | |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 244 | class Strokes4GM : public skiagm::GM { |
| 245 | public: |
| 246 | Strokes4GM() {} |
| 247 | |
| 248 | protected: |
| 249 | |
| 250 | SkString onShortName() override { |
| 251 | return SkString("strokes_zoomed"); |
| 252 | } |
| 253 | |
| 254 | SkISize onISize() override { |
| 255 | return SkISize::Make(W, H*2); |
| 256 | } |
| 257 | |
| 258 | void onDraw(SkCanvas* canvas) override { |
| 259 | SkPaint paint; |
| 260 | paint.setStyle(SkPaint::kStroke_Style); |
| 261 | paint.setStrokeWidth(0.055f); |
| 262 | |
| 263 | canvas->scale(1000, 1000); |
| 264 | canvas->drawCircle(0, 2, 1.97f, paint); |
| 265 | } |
| 266 | |
| 267 | private: |
| 268 | typedef skiagm::GM INHERITED; |
| 269 | }; |
| 270 | |
| 271 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 272 | ////////////////////////////////////////////////////////////////////////////// |
| 273 | |
| 274 | static skiagm::GM* F0(void*) { return new StrokesGM; } |
| 275 | static skiagm::GM* F1(void*) { return new Strokes2GM; } |
| 276 | static skiagm::GM* F2(void*) { return new Strokes3GM; } |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 277 | static skiagm::GM* F3(void*) { return new Strokes4GM; } |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 278 | |
| 279 | static skiagm::GMRegistry R0(F0); |
| 280 | static skiagm::GMRegistry R1(F1); |
| 281 | static skiagm::GMRegistry R2(F2); |
caryclark | 612f70d | 2015-05-19 11:05:37 -0700 | [diff] [blame] | 282 | static skiagm::GMRegistry R3(F3); |