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