blob: 8f53e92b5ea236eaecd9b1d2069b4923636296f1 [file] [log] [blame]
reed@google.com3b14dc12011-04-04 14:31:36 +00001/*
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
21namespace skiagm {
22
23#define W 400
24#define H 400
25#define N 100
26
27static const SkScalar SW = SkIntToScalar(W);
28static const SkScalar SH = SkIntToScalar(H);
29
30class StrokeRectGM : public GM {
31public:
32 StrokeRectGM() {}
33
34protected:
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);
bungeman@google.com0368d062011-05-19 21:29:25 +000066 canvas->clipRect(SkRect::MakeLTRB(SkIntToScalar(2), SkIntToScalar(2), SW - SkIntToScalar(2), SH - SkIntToScalar(2)));
reed@google.com3b14dc12011-04-04 14:31:36 +000067
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
78private:
79 typedef GM INHERITED;
80};
81
82//////////////////////////////////////////////////////////////////////////////
83
84static GM* MyFactory(void*) { return new StrokeRectGM; }
85static GMRegistry reg(MyFactory);
86
87}
88