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: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 42 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 43 | return kSkipTiled_Flag; |
| 44 | } |
| 45 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 46 | SkString onShortName() SK_OVERRIDE { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 47 | return SkString("strokes_round"); |
| 48 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 49 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 50 | SkISize onISize() SK_OVERRIDE { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 51 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 52 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 53 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 55 | SkPaint paint; |
| 56 | paint.setStyle(SkPaint::kStroke_Style); |
| 57 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 58 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 59 | for (int y = 0; y < 2; y++) { |
| 60 | paint.setAntiAlias(!!y); |
| 61 | SkAutoCanvasRestore acr(canvas, true); |
| 62 | canvas->translate(0, SH * y); |
| 63 | canvas->clipRect(SkRect::MakeLTRB( |
| 64 | SkIntToScalar(2), SkIntToScalar(2) |
| 65 | , SW - SkIntToScalar(2), SH - SkIntToScalar(2) |
| 66 | )); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 67 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 68 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 69 | for (int i = 0; i < N; i++) { |
| 70 | SkRect r; |
| 71 | rnd_rect(&r, &paint, rand); |
| 72 | canvas->drawOval(r, paint); |
| 73 | rnd_rect(&r, &paint, rand); |
| 74 | canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint); |
| 75 | rnd_rect(&r, &paint, rand); |
| 76 | } |
| 77 | } |
| 78 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 79 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 80 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 81 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 84 | class Strokes2GM : public skiagm::GM { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 85 | SkPath fPath; |
| 86 | public: |
| 87 | Strokes2GM() { |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 88 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 89 | fPath.moveTo(0, 0); |
| 90 | for (int i = 0; i < 13; i++) { |
| 91 | SkScalar x = rand.nextUScalar1() * (W >> 1); |
| 92 | SkScalar y = rand.nextUScalar1() * (H >> 1); |
| 93 | fPath.lineTo(x, y); |
| 94 | } |
| 95 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 96 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 97 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 98 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 99 | return kSkipTiled_Flag; |
| 100 | } |
| 101 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 102 | SkString onShortName() SK_OVERRIDE { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 103 | return SkString("strokes_poly"); |
| 104 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 105 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 106 | SkISize onISize() SK_OVERRIDE { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 107 | return SkISize::Make(W, H*2); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 108 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 109 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 110 | static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) { |
| 111 | SkMatrix matrix; |
| 112 | matrix.setRotate(angle, px, py); |
| 113 | canvas->concat(matrix); |
| 114 | } |
| 115 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 116 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 117 | canvas->drawColor(SK_ColorWHITE); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 118 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 119 | SkPaint paint; |
| 120 | paint.setStyle(SkPaint::kStroke_Style); |
| 121 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 122 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 123 | for (int y = 0; y < 2; y++) { |
| 124 | paint.setAntiAlias(!!y); |
| 125 | SkAutoCanvasRestore acr(canvas, true); |
| 126 | canvas->translate(0, SH * y); |
| 127 | canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2), |
| 128 | SkIntToScalar(2), |
| 129 | SW - SkIntToScalar(2), |
| 130 | SH - SkIntToScalar(2))); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 131 | |
scroggo | f9d6101 | 2014-12-15 12:54:51 -0800 | [diff] [blame] | 132 | SkRandom rand; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 133 | for (int i = 0; i < N/2; i++) { |
| 134 | SkRect r; |
| 135 | rnd_rect(&r, &paint, rand); |
| 136 | rotate(SkIntToScalar(15), SW/2, SH/2, canvas); |
| 137 | canvas->drawPath(fPath, paint); |
| 138 | } |
| 139 | } |
| 140 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 141 | |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 142 | private: |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 143 | typedef skiagm::GM INHERITED; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | ////////////////////////////////////////////////////////////////////////////// |
| 147 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 148 | static SkRect inset(const SkRect& r) { |
| 149 | SkRect rr(r); |
| 150 | rr.inset(r.width()/10, r.height()/10); |
| 151 | return rr; |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 152 | } |
| 153 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 154 | class Strokes3GM : public skiagm::GM { |
| 155 | static void make0(SkPath* path, const SkRect& bounds, SkString* title) { |
| 156 | path->addRect(bounds, SkPath::kCW_Direction); |
| 157 | path->addRect(inset(bounds), SkPath::kCW_Direction); |
| 158 | title->set("CW CW"); |
| 159 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 160 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 161 | static void make1(SkPath* path, const SkRect& bounds, SkString* title) { |
| 162 | path->addRect(bounds, SkPath::kCW_Direction); |
| 163 | path->addRect(inset(bounds), SkPath::kCCW_Direction); |
| 164 | title->set("CW CCW"); |
| 165 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 167 | static void make2(SkPath* path, const SkRect& bounds, SkString* title) { |
| 168 | path->addOval(bounds, SkPath::kCW_Direction); |
| 169 | path->addOval(inset(bounds), SkPath::kCW_Direction); |
| 170 | title->set("CW CW"); |
| 171 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 172 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 173 | static void make3(SkPath* path, const SkRect& bounds, SkString* title) { |
| 174 | path->addOval(bounds, SkPath::kCW_Direction); |
| 175 | path->addOval(inset(bounds), SkPath::kCCW_Direction); |
| 176 | title->set("CW CCW"); |
| 177 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 179 | static void make4(SkPath* path, const SkRect& bounds, SkString* title) { |
| 180 | path->addRect(bounds, SkPath::kCW_Direction); |
| 181 | SkRect r = bounds; |
| 182 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 183 | path->addOval(r, SkPath::kCW_Direction); |
| 184 | title->set("CW CW"); |
| 185 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 186 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 187 | static void make5(SkPath* path, const SkRect& bounds, SkString* title) { |
| 188 | path->addRect(bounds, SkPath::kCW_Direction); |
| 189 | SkRect r = bounds; |
| 190 | r.inset(bounds.width() / 10, -bounds.height() / 10); |
| 191 | path->addOval(r, SkPath::kCCW_Direction); |
| 192 | title->set("CW CCW"); |
| 193 | } |
| 194 | |
| 195 | public: |
| 196 | Strokes3GM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 197 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 198 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 199 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 200 | return kSkipTiled_Flag; |
| 201 | } |
| 202 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 203 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 204 | return SkString("strokes3"); |
| 205 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 206 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 207 | SkISize onISize() SK_OVERRIDE { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 208 | return SkISize::Make(W, H*2); |
| 209 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 210 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 211 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 212 | SkPaint origPaint; |
| 213 | origPaint.setAntiAlias(true); |
| 214 | origPaint.setStyle(SkPaint::kStroke_Style); |
| 215 | SkPaint fillPaint(origPaint); |
| 216 | fillPaint.setColor(SK_ColorRED); |
| 217 | SkPaint strokePaint(origPaint); |
| 218 | strokePaint.setColor(0xFF4444FF); |
| 219 | |
| 220 | void (*procs[])(SkPath*, const SkRect&, SkString*) = { |
| 221 | make0, make1, make2, make3, make4, make5 |
| 222 | }; |
| 223 | |
| 224 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 225 | |
| 226 | SkRect bounds = SkRect::MakeWH(SkIntToScalar(50), SkIntToScalar(50)); |
| 227 | SkScalar dx = bounds.width() * 4/3; |
| 228 | SkScalar dy = bounds.height() * 5; |
| 229 | |
| 230 | for (size_t i = 0; i < SK_ARRAY_COUNT(procs); ++i) { |
| 231 | SkPath orig; |
| 232 | SkString str; |
| 233 | procs[i](&orig, bounds, &str); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 234 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 235 | canvas->save(); |
| 236 | for (int j = 0; j < 13; ++j) { |
| 237 | strokePaint.setStrokeWidth(SK_Scalar1 * j * j); |
| 238 | canvas->drawPath(orig, strokePaint); |
| 239 | canvas->drawPath(orig, origPaint); |
| 240 | SkPath fill; |
| 241 | strokePaint.getFillPath(orig, &fill); |
| 242 | canvas->drawPath(fill, fillPaint); |
| 243 | canvas->translate(dx + strokePaint.getStrokeWidth(), 0); |
| 244 | } |
| 245 | canvas->restore(); |
| 246 | canvas->translate(0, dy); |
| 247 | } |
| 248 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 249 | |
reed@google.com | 4384fab | 2012-06-05 16:14:23 +0000 | [diff] [blame] | 250 | private: |
| 251 | typedef skiagm::GM INHERITED; |
| 252 | }; |
| 253 | |
| 254 | ////////////////////////////////////////////////////////////////////////////// |
| 255 | |
| 256 | static skiagm::GM* F0(void*) { return new StrokesGM; } |
| 257 | static skiagm::GM* F1(void*) { return new Strokes2GM; } |
| 258 | static skiagm::GM* F2(void*) { return new Strokes3GM; } |
| 259 | |
| 260 | static skiagm::GMRegistry R0(F0); |
| 261 | static skiagm::GMRegistry R1(F1); |
| 262 | static skiagm::GMRegistry R2(F2); |