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 | |
| 14 | namespace skiagm { |
| 15 | |
| 16 | #define W 400 |
| 17 | #define H 400 |
| 18 | #define N 50 |
| 19 | |
| 20 | static const SkScalar SW = SkIntToScalar(W); |
| 21 | static const SkScalar SH = SkIntToScalar(H); |
| 22 | |
| 23 | static void rnd_rect(SkRect* r, SkPaint* paint, SkRandom& rand) { |
| 24 | SkScalar x = rand.nextUScalar1() * W; |
| 25 | SkScalar y = rand.nextUScalar1() * H; |
| 26 | SkScalar w = rand.nextUScalar1() * (W >> 2); |
| 27 | SkScalar h = rand.nextUScalar1() * (H >> 2); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 28 | SkScalar hoffset = rand.nextSScalar1(); |
| 29 | SkScalar woffset = rand.nextSScalar1(); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 30 | |
| 31 | r->set(x, y, x + w, y + h); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 32 | r->offset(-w/2 + woffset, -h/2 + hoffset); |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 33 | |
| 34 | paint->setColor(rand.nextU()); |
| 35 | paint->setAlpha(0xFF); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | class StrokesGM : public GM { |
| 40 | public: |
| 41 | StrokesGM() {} |
| 42 | |
| 43 | protected: |
| 44 | virtual SkString onShortName() { |
| 45 | return SkString("strokes_round"); |
| 46 | } |
| 47 | |
| 48 | virtual SkISize onISize() { |
| 49 | return make_isize(W, H*2); |
| 50 | } |
| 51 | |
bsalomon@google.com | 48dd1a2 | 2011-10-31 14:18:20 +0000 | [diff] [blame] | 52 | virtual void onDraw(SkCanvas* canvas) { |
mike@reedtribe.org | f2c21cd | 2011-06-18 00:15:04 +0000 | [diff] [blame] | 53 | SkPaint paint; |
| 54 | paint.setStyle(SkPaint::kStroke_Style); |
| 55 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
| 56 | |
| 57 | for (int y = 0; y < 2; y++) { |
| 58 | paint.setAntiAlias(!!y); |
| 59 | SkAutoCanvasRestore acr(canvas, true); |
| 60 | canvas->translate(0, SH * y); |
| 61 | canvas->clipRect(SkRect::MakeLTRB( |
| 62 | SkIntToScalar(2), SkIntToScalar(2) |
| 63 | , SW - SkIntToScalar(2), SH - SkIntToScalar(2) |
| 64 | )); |
| 65 | |
| 66 | SkRandom rand; |
| 67 | for (int i = 0; i < N; i++) { |
| 68 | SkRect r; |
| 69 | rnd_rect(&r, &paint, rand); |
| 70 | canvas->drawOval(r, paint); |
| 71 | rnd_rect(&r, &paint, rand); |
| 72 | canvas->drawRoundRect(r, r.width()/4, r.height()/4, paint); |
| 73 | rnd_rect(&r, &paint, rand); |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | typedef GM INHERITED; |
| 80 | }; |
| 81 | |
| 82 | class Strokes2GM : public GM { |
| 83 | SkPath fPath; |
| 84 | public: |
| 85 | Strokes2GM() { |
| 86 | SkRandom rand; |
| 87 | fPath.moveTo(0, 0); |
| 88 | for (int i = 0; i < 13; i++) { |
| 89 | SkScalar x = rand.nextUScalar1() * (W >> 1); |
| 90 | SkScalar y = rand.nextUScalar1() * (H >> 1); |
| 91 | fPath.lineTo(x, y); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected: |
| 96 | virtual SkString onShortName() { |
| 97 | return SkString("strokes_poly"); |
| 98 | } |
| 99 | |
| 100 | virtual SkISize onISize() { |
| 101 | return make_isize(W, H*2); |
| 102 | } |
| 103 | |
| 104 | static void rotate(SkScalar angle, SkScalar px, SkScalar py, SkCanvas* canvas) { |
| 105 | SkMatrix matrix; |
| 106 | matrix.setRotate(angle, px, py); |
| 107 | canvas->concat(matrix); |
| 108 | } |
| 109 | |
| 110 | virtual void onDraw(SkCanvas* canvas) { |
| 111 | canvas->drawColor(SK_ColorWHITE); |
| 112 | |
| 113 | SkPaint paint; |
| 114 | paint.setStyle(SkPaint::kStroke_Style); |
| 115 | paint.setStrokeWidth(SkIntToScalar(9)/2); |
| 116 | |
| 117 | for (int y = 0; y < 2; y++) { |
| 118 | paint.setAntiAlias(!!y); |
| 119 | SkAutoCanvasRestore acr(canvas, true); |
| 120 | canvas->translate(0, SH * y); |
| 121 | canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2), |
| 122 | SkIntToScalar(2), |
| 123 | SW - SkIntToScalar(2), |
| 124 | SH - SkIntToScalar(2))); |
| 125 | |
| 126 | SkRandom rand; |
| 127 | for (int i = 0; i < N/2; i++) { |
| 128 | SkRect r; |
| 129 | rnd_rect(&r, &paint, rand); |
| 130 | rotate(SkIntToScalar(15), SW/2, SH/2, canvas); |
| 131 | canvas->drawPath(fPath, paint); |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private: |
| 137 | typedef GM INHERITED; |
| 138 | }; |
| 139 | |
| 140 | ////////////////////////////////////////////////////////////////////////////// |
| 141 | |
| 142 | static GM* MyFactory(void*) { return new StrokesGM; } |
| 143 | static GMRegistry reg(MyFactory); |
| 144 | |
| 145 | static GM* MyFactory2(void*) { return new Strokes2GM; } |
| 146 | static GMRegistry reg2(MyFactory2); |
| 147 | |
| 148 | } |
| 149 | |