blob: e66f086954c8184645c74c33882d987c4b331437 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
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.
7 */
reed@android.com49f0ff22009-03-19 21:52:42 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkCanvas.h"
11#include "SkBlurMaskFilter.h"
12#include "SkPaint.h"
13#include "SkPath.h"
reed@android.com49f0ff22009-03-19 21:52:42 +000014#include "SkXfermode.h"
15#include "SkMatrix.h"
16#include "SkColor.h"
17#include "SkRandom.h"
18
19static void set2x3(SkMatrix* m, float a, float b, float c, float d, float e, float f) {
20 m->reset();
21 m->set(0, a);
22 m->set(1, b);
23 m->set(2, c);
24 m->set(3, d);
25 m->set(4, e);
26 m->set(5, f);
27}
28
29static SkRandom gRand;
30static bool return_large;
31static bool return_undef;
32static bool quick;
33static bool scale_large;
34static int scval = 1;
35static float transval = 0;
36
37static int R(float x) {
38 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
39}
40
robertphillips@google.com706f6212012-05-14 17:51:23 +000041#if defined _WIN32
42#pragma warning ( push )
43// we are intentionally causing an overflow here
44// (warning C4756: overflow in constant arithmetic)
45#pragma warning ( disable : 4756 )
46#endif
47
reed@google.com261b8e22011-04-14 17:53:24 +000048static float huge() {
49 double d = 1e100;
50 float f = (float)d;
51 return f;
52}
53
robertphillips@google.com706f6212012-05-14 17:51:23 +000054#if defined _WIN32
55#pragma warning ( pop )
56#endif
57
reed@android.com49f0ff22009-03-19 21:52:42 +000058static float make_number() {
tomhudson@google.com2c2508d2011-07-29 13:44:30 +000059 float v = 0;
reed@android.com49f0ff22009-03-19 21:52:42 +000060 int sel;
61
robertphillips@google.comb7061172013-09-06 14:16:12 +000062 if (return_large == true && R(3) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000063 sel = R(6);
robertphillips@google.comb7061172013-09-06 14:16:12 +000064 } else {
65 sel = R(4);
reed@android.com49f0ff22009-03-19 21:52:42 +000066 }
67
robertphillips@google.comb7061172013-09-06 14:16:12 +000068 if (return_undef == false && sel == 0) {
69 sel = 1;
70 }
71
72 if (R(2) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000073 v = (float)R(100);
robertphillips@google.comb7061172013-09-06 14:16:12 +000074 } else {
75
76 switch (sel) {
77 case 0: break;
78 case 1: v = 0; break;
79 case 2: v = 0.000001f; break;
80 case 3: v = 10000; break;
81 case 4: v = 2000000000; break;
82 case 5: v = huge(); break;
83 }
84
85 }
86
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000087 if (R(4) == 1) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000088 v = -v;
89 }
90
reed@android.com49f0ff22009-03-19 21:52:42 +000091 return v;
92}
93
94static SkColor make_color() {
95 if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
96}
97
98
99static SkColor make_fill() {
100#if 0
101 int sel;
102
103 if (quick == true) sel = 0; else sel = R(6);
104
105 switch (sel) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000106
reed@android.com49f0ff22009-03-19 21:52:42 +0000107 case 0:
108 case 1:
109 case 2:
110 return make_color();
111 break;
112
113 case 3:
114 var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number());
115 for (i=0;i<4;i++)
116 r.addColorStop(make_number(),make_color());
117 return r;
118 break;
119
120 case 4:
121 var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
122 for (i=0;i<4;i++)
123 r.addColorStop(make_number(),make_color());
124 return r;
125 break;
126
127 case 5:
128 var r = ctx.createPattern(imgObj,"repeat");
129 if (R(6) == 0)
130 r.addColorStop(make_number(),make_color());
131 return r;
132 break;
133 }
134#else
135 return make_color();
136#endif
137}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000138
reed@android.com49f0ff22009-03-19 21:52:42 +0000139
140static void do_fuzz(SkCanvas* canvas) {
141 SkPath path;
142 SkPaint paint;
143 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000144
reed@android.com49f0ff22009-03-19 21:52:42 +0000145 for (int i=0;i<100;i++) {
146 switch (R(33)) {
147
148 case 0:
149 paint.setColor(make_fill());
150 break;
151
152 case 1:
153 paint.setAlpha(gRand.nextU() & 0xFF);
154 break;
155
156 case 2: {
reed@android.com048522d2009-06-23 12:19:41 +0000157 SkXfermode::Mode mode;
158 switch (R(3)) {
tomhudson@google.com83a44462011-10-27 15:27:51 +0000159 case 0: mode = SkXfermode::kSrc_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000160 case 1: mode = SkXfermode::kXor_Mode; break;
tomhudson@google.com83a44462011-10-27 15:27:51 +0000161 case 2:
162 default: // silence warning
163 mode = SkXfermode::kSrcOver_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000164 }
reed@android.com5ebbe142009-11-05 21:04:00 +0000165 paint.setXfermodeMode(mode);
reed@android.com49f0ff22009-03-19 21:52:42 +0000166 }
167 break;
168
169 case 3:
170 switch (R(2)) {
171 case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break;
172 case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000174 break;
175
176 case 4:
177 switch (R(2)) {
178 case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break;
179 case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000180 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000181 break;
182
rmistry@google.comae933ce2012-08-23 18:19:56 +0000183 case 5:
184 paint.setStrokeWidth(make_number());
reed@android.com49f0ff22009-03-19 21:52:42 +0000185 break;
186
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187 case 6:
reed@android.com49f0ff22009-03-19 21:52:42 +0000188 paint.setStrokeMiter(make_number());
189 break;
190
rmistry@google.comae933ce2012-08-23 18:19:56 +0000191 case 7:
reed@android.com49f0ff22009-03-19 21:52:42 +0000192 if (quick == true) break;
robertphillips@google.comb7061172013-09-06 14:16:12 +0000193 SkSafeUnref(paint.setMaskFilter(SkBlurMaskFilter::Create(
194 SkBlurMaskFilter::kNormal_BlurStyle,
195 make_number())));
reed@android.com49f0ff22009-03-19 21:52:42 +0000196 break;
197
rmistry@google.comae933ce2012-08-23 18:19:56 +0000198 case 8:
reed@android.com49f0ff22009-03-19 21:52:42 +0000199 if (quick == true) break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200 //ctx.shadowColor = make_fill();
reed@android.com49f0ff22009-03-19 21:52:42 +0000201 break;
202
rmistry@google.comae933ce2012-08-23 18:19:56 +0000203 case 9:
reed@android.com49f0ff22009-03-19 21:52:42 +0000204 if (quick == true) break;
205 //ctx.shadowOffsetX = make_number();
206 //ctx.shadowOffsetY = make_number();
207 break;
208
209 case 10:
210 canvas->restore();
211 break;
212
213 case 11:
214 canvas->rotate(make_number());
215 break;
216
217 case 12:
218 canvas->save();
219 break;
220
221 case 13:
222 canvas->scale(-1,-1);
223 break;
224
225 case 14:
226
227 if (quick == true) break;
228
229 if (transval == 0) {
230 transval = make_number();
231 canvas->translate(transval,0);
232 } else {
233 canvas->translate(-transval,0);
234 transval = 0;
235 }
236
237 break;
238
239 case 15: {
240 SkRect r;
241 r.set(make_number(),make_number(),make_number(),make_number());
242 SkPaint::Style s = paint.getStyle();
243 paint.setStyle(SkPaint::kFill_Style);
244 canvas->drawRect(r, paint);
245 paint.setStyle(s);
246 // clearrect
247 } break;
248
249 case 16:
250 if (quick == true) break;
251// ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
252 break;
253
254 case 17: {
255 SkRect r;
256 r.set(make_number(),make_number(),make_number(),make_number());
257 SkPaint::Style s = paint.getStyle();
258 paint.setStyle(SkPaint::kFill_Style);
259 canvas->drawRect(r, paint);
260 paint.setStyle(s);
261 } break;
262
263 case 18:
264 path.reset();
265 break;
266
267 case 19:
268 // ctx.clip() is evil.
269 break;
270
271 case 20:
272 path.close();
273 break;
274
275 case 21: {
276 SkPaint::Style s = paint.getStyle();
277 paint.setStyle(SkPaint::kFill_Style);
278 canvas->drawPath(path, paint);
279 paint.setStyle(s);
280 } break;
281
282 case 22: {
283 SkPaint::Style s = paint.getStyle();
284 paint.setStyle(SkPaint::kFill_Style);
285 canvas->drawPath(path, paint);
286 paint.setStyle(s);
287 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000288
reed@android.com49f0ff22009-03-19 21:52:42 +0000289 case 23: {
290 SkRect r;
291 r.set(make_number(),make_number(),make_number(),make_number());
292 SkPaint::Style s = paint.getStyle();
293 paint.setStyle(SkPaint::kStroke_Style);
294 canvas->drawRect(r, paint);
295 paint.setStyle(s);
296 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000297
reed@android.com49f0ff22009-03-19 21:52:42 +0000298 case 24:
299 if (quick == true) break;
300 //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true);
301 break;
302
303 case 25:
304 if (quick == true) break;
305 //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number());
306 break;
307
308 case 26:
309 if (quick == true) break;
310 //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
311 break;
312
313 case 27:
314 path.lineTo(make_number(),make_number());
315 break;
316
317 case 28:
318 path.moveTo(make_number(),make_number());
319 break;
320
321 case 29:
322 if (quick == true) break;
323 path.quadTo(make_number(),make_number(),make_number(),make_number());
324 break;
325
326 case 30: {
327 if (quick == true) break;
328 SkMatrix matrix;
329 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
330 canvas->concat(matrix);
331 } break;
332
333 case 31: {
334 if (quick == true) break;
335 SkMatrix matrix;
336 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
337 canvas->setMatrix(matrix);
338 } break;
339
340 case 32:
341
342 if (scale_large == true) {
343
344 switch (scval) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000345 case 0: canvas->scale(-1000000000,1);
reed@android.com49f0ff22009-03-19 21:52:42 +0000346 canvas->scale(-1000000000,1);
347 scval = 1; break;
reed@google.com261b8e22011-04-14 17:53:24 +0000348 case 1: canvas->scale(-.000000001f,1); scval = 2; break;
349 case 2: canvas->scale(-.000000001f,1); scval = 0; break;
reed@android.com49f0ff22009-03-19 21:52:42 +0000350 }
351
352 }
353
354 break;
355
356
357
358 }
359 }
360
361}
362
363//////////////////////////////////////////////////////////////////////////////
364
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000365class FuzzView : public SampleView {
reed@android.com49f0ff22009-03-19 21:52:42 +0000366public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000367 FuzzView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000368 this->setBGColor(0xFFDDDDDD);
369 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000370
reed@android.com49f0ff22009-03-19 21:52:42 +0000371protected:
372 // overrides from SkEventSink
373 virtual bool onQuery(SkEvent* evt) {
374 if (SampleCode::TitleQ(*evt)) {
375 SampleCode::TitleR(evt, "Fuzzer");
376 return true;
377 }
378 return this->INHERITED::onQuery(evt);
379 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000380
reed@android.com49f0ff22009-03-19 21:52:42 +0000381 void drawBG(SkCanvas* canvas) {
382 canvas->drawColor(0xFFDDDDDD);
383 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000384
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000385 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com49f0ff22009-03-19 21:52:42 +0000386 do_fuzz(canvas);
387 this->inval(NULL);
388 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000389
reed@android.com49f0ff22009-03-19 21:52:42 +0000390private:
391 typedef SkView INHERITED;
392};
393
394//////////////////////////////////////////////////////////////////////////////
395
396static SkView* MyFactory() { return new FuzzView; }
397static SkViewRegister reg(MyFactory);