reed@google.com | 3b14dc1 | 2011-04-04 14:31:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2011 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "gm.h" |
| 19 | #include "SkRandom.h" |
| 20 | |
| 21 | namespace skiagm { |
| 22 | |
| 23 | #define W 400 |
| 24 | #define H 400 |
| 25 | #define N 100 |
| 26 | |
| 27 | static const SkScalar SW = SkIntToScalar(W); |
| 28 | static const SkScalar SH = SkIntToScalar(H); |
| 29 | |
| 30 | class StrokeRectGM : public GM { |
| 31 | public: |
| 32 | StrokeRectGM() {} |
| 33 | |
| 34 | protected: |
| 35 | virtual SkString onShortName() { |
| 36 | return SkString("strokerects"); |
| 37 | } |
| 38 | |
| 39 | virtual SkISize onISize() { |
| 40 | return make_isize(W*2, H*2); |
| 41 | } |
| 42 | |
| 43 | static void rnd_rect(SkRect* r, SkRandom& rand) { |
| 44 | SkScalar x = rand.nextUScalar1() * W; |
| 45 | SkScalar y = rand.nextUScalar1() * H; |
| 46 | SkScalar w = rand.nextUScalar1() * (W >> 2); |
| 47 | SkScalar h = rand.nextUScalar1() * (H >> 2); |
| 48 | |
| 49 | r->set(x, y, x + w, y + h); |
| 50 | r->offset(-w/2 + rand.nextSScalar1(), -h/2 + + rand.nextSScalar1()); |
| 51 | } |
| 52 | |
| 53 | virtual void onDraw(SkCanvas* canvas) { |
| 54 | canvas->drawColor(SK_ColorWHITE); |
| 55 | |
| 56 | SkPaint paint; |
| 57 | paint.setStyle(SkPaint::kStroke_Style); |
| 58 | |
| 59 | for (int y = 0; y < 2; y++) { |
| 60 | paint.setAntiAlias(!!y); |
| 61 | for (int x = 0; x < 2; x++) { |
| 62 | paint.setStrokeWidth(x * SkIntToScalar(3)); |
| 63 | |
| 64 | SkAutoCanvasRestore acr(canvas, true); |
| 65 | canvas->translate(SW * x, SH * y); |
| 66 | canvas->clipRect(SkRect::MakeLTRB(2, 2, SW - 2, SH - 2)); |
| 67 | |
| 68 | SkRandom rand; |
| 69 | for (int i = 0; i < N; i++) { |
| 70 | SkRect r; |
| 71 | rnd_rect(&r, rand); |
| 72 | canvas->drawRect(r, paint); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | typedef GM INHERITED; |
| 80 | }; |
| 81 | |
| 82 | ////////////////////////////////////////////////////////////////////////////// |
| 83 | |
| 84 | static GM* MyFactory(void*) { return new StrokeRectGM; } |
| 85 | static GMRegistry reg(MyFactory); |
| 86 | |
| 87 | } |
| 88 | |