blob: ba1fc0338214387424c893b09bc39c960a0ae5ff [file] [log] [blame]
reed@android.com49f0ff22009-03-19 21:52:42 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkCanvas.h"
4#include "SkBlurMaskFilter.h"
5#include "SkPaint.h"
6#include "SkPath.h"
reed@android.com49f0ff22009-03-19 21:52:42 +00007#include "SkXfermode.h"
8#include "SkMatrix.h"
9#include "SkColor.h"
10#include "SkRandom.h"
11
12static void set2x3(SkMatrix* m, float a, float b, float c, float d, float e, float f) {
13 m->reset();
14 m->set(0, a);
15 m->set(1, b);
16 m->set(2, c);
17 m->set(3, d);
18 m->set(4, e);
19 m->set(5, f);
20}
21
22static SkRandom gRand;
23static bool return_large;
24static bool return_undef;
25static bool quick;
26static bool scale_large;
27static int scval = 1;
28static float transval = 0;
29
30static int R(float x) {
31 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
32}
33
34static float make_number() {
35 float v;
36 int sel;
37
38 if (return_large == true && R(3) == 1) sel = R(6); else sel = R(4);
39 if (return_undef == false && sel == 0) sel = 1;
40
41 if (R(2) == 1) v = R(100); else
42
43 switch (sel) {
44 case 0: break;
45 case 1: v = 0; break;
46 case 2: v = 0.000001; break;
47 case 3: v = 10000; break;
48 case 4: v = 2000000000; break;
49 case 5: v = 1e100; break;
50 }
51
52 if (R(4) == 1) v = -v;
53 return v;
54}
55
56static SkColor make_color() {
57 if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
58}
59
60
61static SkColor make_fill() {
62#if 0
63 int sel;
64
65 if (quick == true) sel = 0; else sel = R(6);
66
67 switch (sel) {
68
69 case 0:
70 case 1:
71 case 2:
72 return make_color();
73 break;
74
75 case 3:
76 var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number());
77 for (i=0;i<4;i++)
78 r.addColorStop(make_number(),make_color());
79 return r;
80 break;
81
82 case 4:
83 var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
84 for (i=0;i<4;i++)
85 r.addColorStop(make_number(),make_color());
86 return r;
87 break;
88
89 case 5:
90 var r = ctx.createPattern(imgObj,"repeat");
91 if (R(6) == 0)
92 r.addColorStop(make_number(),make_color());
93 return r;
94 break;
95 }
96#else
97 return make_color();
98#endif
99}
100
101
102static void do_fuzz(SkCanvas* canvas) {
103 SkPath path;
104 SkPaint paint;
105 paint.setAntiAlias(true);
106
107 for (int i=0;i<100;i++) {
108 switch (R(33)) {
109
110 case 0:
111 paint.setColor(make_fill());
112 break;
113
114 case 1:
115 paint.setAlpha(gRand.nextU() & 0xFF);
116 break;
117
118 case 2: {
reed@android.com048522d2009-06-23 12:19:41 +0000119 SkXfermode::Mode mode;
120 switch (R(3)) {
121 case 0: mode = SkXfermode::kSrc_Mode; break;
122 case 1: mode = SkXfermode::kXor_Mode; break;
123 case 2: mode = SkXfermode::kSrcOver_Mode; break;
124 }
reed@android.com5ebbe142009-11-05 21:04:00 +0000125 paint.setXfermodeMode(mode);
reed@android.com49f0ff22009-03-19 21:52:42 +0000126 }
127 break;
128
129 case 3:
130 switch (R(2)) {
131 case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break;
132 case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break;
133 }
134 break;
135
136 case 4:
137 switch (R(2)) {
138 case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break;
139 case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break;
140 }
141 break;
142
143 case 5:
144 paint.setStrokeWidth(make_number());
145 break;
146
147 case 6:
148 paint.setStrokeMiter(make_number());
149 break;
150
151 case 7:
152 if (quick == true) break;
153 paint.setMaskFilter(SkBlurMaskFilter::Create(make_number(), SkBlurMaskFilter::kNormal_BlurStyle))->safeUnref();
154 break;
155
156 case 8:
157 if (quick == true) break;
158 //ctx.shadowColor = make_fill();
159 break;
160
161 case 9:
162 if (quick == true) break;
163 //ctx.shadowOffsetX = make_number();
164 //ctx.shadowOffsetY = make_number();
165 break;
166
167 case 10:
168 canvas->restore();
169 break;
170
171 case 11:
172 canvas->rotate(make_number());
173 break;
174
175 case 12:
176 canvas->save();
177 break;
178
179 case 13:
180 canvas->scale(-1,-1);
181 break;
182
183 case 14:
184
185 if (quick == true) break;
186
187 if (transval == 0) {
188 transval = make_number();
189 canvas->translate(transval,0);
190 } else {
191 canvas->translate(-transval,0);
192 transval = 0;
193 }
194
195 break;
196
197 case 15: {
198 SkRect r;
199 r.set(make_number(),make_number(),make_number(),make_number());
200 SkPaint::Style s = paint.getStyle();
201 paint.setStyle(SkPaint::kFill_Style);
202 canvas->drawRect(r, paint);
203 paint.setStyle(s);
204 // clearrect
205 } break;
206
207 case 16:
208 if (quick == true) break;
209// ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
210 break;
211
212 case 17: {
213 SkRect r;
214 r.set(make_number(),make_number(),make_number(),make_number());
215 SkPaint::Style s = paint.getStyle();
216 paint.setStyle(SkPaint::kFill_Style);
217 canvas->drawRect(r, paint);
218 paint.setStyle(s);
219 } break;
220
221 case 18:
222 path.reset();
223 break;
224
225 case 19:
226 // ctx.clip() is evil.
227 break;
228
229 case 20:
230 path.close();
231 break;
232
233 case 21: {
234 SkPaint::Style s = paint.getStyle();
235 paint.setStyle(SkPaint::kFill_Style);
236 canvas->drawPath(path, paint);
237 paint.setStyle(s);
238 } break;
239
240 case 22: {
241 SkPaint::Style s = paint.getStyle();
242 paint.setStyle(SkPaint::kFill_Style);
243 canvas->drawPath(path, paint);
244 paint.setStyle(s);
245 } break;
246
247 case 23: {
248 SkRect r;
249 r.set(make_number(),make_number(),make_number(),make_number());
250 SkPaint::Style s = paint.getStyle();
251 paint.setStyle(SkPaint::kStroke_Style);
252 canvas->drawRect(r, paint);
253 paint.setStyle(s);
254 } break;
255
256 case 24:
257 if (quick == true) break;
258 //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true);
259 break;
260
261 case 25:
262 if (quick == true) break;
263 //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number());
264 break;
265
266 case 26:
267 if (quick == true) break;
268 //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
269 break;
270
271 case 27:
272 path.lineTo(make_number(),make_number());
273 break;
274
275 case 28:
276 path.moveTo(make_number(),make_number());
277 break;
278
279 case 29:
280 if (quick == true) break;
281 path.quadTo(make_number(),make_number(),make_number(),make_number());
282 break;
283
284 case 30: {
285 if (quick == true) break;
286 SkMatrix matrix;
287 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
288 canvas->concat(matrix);
289 } break;
290
291 case 31: {
292 if (quick == true) break;
293 SkMatrix matrix;
294 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
295 canvas->setMatrix(matrix);
296 } break;
297
298 case 32:
299
300 if (scale_large == true) {
301
302 switch (scval) {
303 case 0: canvas->scale(-1000000000,1);
304 canvas->scale(-1000000000,1);
305 scval = 1; break;
306 case 1: canvas->scale(-.000000001,1); scval = 2; break;
307 case 2: canvas->scale(-.000000001,1); scval = 0; break;
308 }
309
310 }
311
312 break;
313
314
315
316 }
317 }
318
319}
320
321//////////////////////////////////////////////////////////////////////////////
322
323class FuzzView : public SkView {
324public:
325 FuzzView() {}
326
327protected:
328 // overrides from SkEventSink
329 virtual bool onQuery(SkEvent* evt) {
330 if (SampleCode::TitleQ(*evt)) {
331 SampleCode::TitleR(evt, "Fuzzer");
332 return true;
333 }
334 return this->INHERITED::onQuery(evt);
335 }
336
337 void drawBG(SkCanvas* canvas) {
338 canvas->drawColor(0xFFDDDDDD);
339 }
340
341 virtual void onDraw(SkCanvas* canvas) {
reed@android.com5ebbe142009-11-05 21:04:00 +0000342 SkIRect r = canvas->getTotalClip().getBounds();
reed@android.com49f0ff22009-03-19 21:52:42 +0000343 this->drawBG(canvas);
344 do_fuzz(canvas);
345 this->inval(NULL);
346 }
347
348private:
349 typedef SkView INHERITED;
350};
351
352//////////////////////////////////////////////////////////////////////////////
353
354static SkView* MyFactory() { return new FuzzView; }
355static SkViewRegister reg(MyFactory);
356