blob: a5d891772e10ac2b5502a286eea284e6f1260626 [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
62 if (return_large == true && R(3) == 1) sel = R(6); else sel = R(4);
63 if (return_undef == false && sel == 0) sel = 1;
64
reed@google.com261b8e22011-04-14 17:53:24 +000065 if (R(2) == 1) v = (float)R(100); else
reed@android.com49f0ff22009-03-19 21:52:42 +000066
67 switch (sel) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000068 case 0: break;
reed@android.com49f0ff22009-03-19 21:52:42 +000069 case 1: v = 0; break;
reed@google.com261b8e22011-04-14 17:53:24 +000070 case 2: v = 0.000001f; break;
reed@android.com49f0ff22009-03-19 21:52:42 +000071 case 3: v = 10000; break;
72 case 4: v = 2000000000; break;
reed@google.com261b8e22011-04-14 17:53:24 +000073 case 5: v = huge(); break;
reed@android.com49f0ff22009-03-19 21:52:42 +000074 }
75
76 if (R(4) == 1) v = -v;
77 return v;
78}
79
80static SkColor make_color() {
81 if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
82}
83
84
85static SkColor make_fill() {
86#if 0
87 int sel;
88
89 if (quick == true) sel = 0; else sel = R(6);
90
91 switch (sel) {
rmistry@google.comae933ce2012-08-23 18:19:56 +000092
reed@android.com49f0ff22009-03-19 21:52:42 +000093 case 0:
94 case 1:
95 case 2:
96 return make_color();
97 break;
98
99 case 3:
100 var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number());
101 for (i=0;i<4;i++)
102 r.addColorStop(make_number(),make_color());
103 return r;
104 break;
105
106 case 4:
107 var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
108 for (i=0;i<4;i++)
109 r.addColorStop(make_number(),make_color());
110 return r;
111 break;
112
113 case 5:
114 var r = ctx.createPattern(imgObj,"repeat");
115 if (R(6) == 0)
116 r.addColorStop(make_number(),make_color());
117 return r;
118 break;
119 }
120#else
121 return make_color();
122#endif
123}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000124
reed@android.com49f0ff22009-03-19 21:52:42 +0000125
126static void do_fuzz(SkCanvas* canvas) {
127 SkPath path;
128 SkPaint paint;
129 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000130
reed@android.com49f0ff22009-03-19 21:52:42 +0000131 for (int i=0;i<100;i++) {
132 switch (R(33)) {
133
134 case 0:
135 paint.setColor(make_fill());
136 break;
137
138 case 1:
139 paint.setAlpha(gRand.nextU() & 0xFF);
140 break;
141
142 case 2: {
reed@android.com048522d2009-06-23 12:19:41 +0000143 SkXfermode::Mode mode;
144 switch (R(3)) {
tomhudson@google.com83a44462011-10-27 15:27:51 +0000145 case 0: mode = SkXfermode::kSrc_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000146 case 1: mode = SkXfermode::kXor_Mode; break;
tomhudson@google.com83a44462011-10-27 15:27:51 +0000147 case 2:
148 default: // silence warning
149 mode = SkXfermode::kSrcOver_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000150 }
reed@android.com5ebbe142009-11-05 21:04:00 +0000151 paint.setXfermodeMode(mode);
reed@android.com49f0ff22009-03-19 21:52:42 +0000152 }
153 break;
154
155 case 3:
156 switch (R(2)) {
157 case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break;
158 case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000159 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000160 break;
161
162 case 4:
163 switch (R(2)) {
164 case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break;
165 case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000166 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000167 break;
168
rmistry@google.comae933ce2012-08-23 18:19:56 +0000169 case 5:
170 paint.setStrokeWidth(make_number());
reed@android.com49f0ff22009-03-19 21:52:42 +0000171 break;
172
rmistry@google.comae933ce2012-08-23 18:19:56 +0000173 case 6:
reed@android.com49f0ff22009-03-19 21:52:42 +0000174 paint.setStrokeMiter(make_number());
175 break;
176
rmistry@google.comae933ce2012-08-23 18:19:56 +0000177 case 7:
reed@android.com49f0ff22009-03-19 21:52:42 +0000178 if (quick == true) break;
bsalomon@google.com96959212011-02-08 22:03:36 +0000179 SkSafeUnref(paint.setMaskFilter(SkBlurMaskFilter::Create(make_number(), SkBlurMaskFilter::kNormal_BlurStyle)));
reed@android.com49f0ff22009-03-19 21:52:42 +0000180 break;
181
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182 case 8:
reed@android.com49f0ff22009-03-19 21:52:42 +0000183 if (quick == true) break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000184 //ctx.shadowColor = make_fill();
reed@android.com49f0ff22009-03-19 21:52:42 +0000185 break;
186
rmistry@google.comae933ce2012-08-23 18:19:56 +0000187 case 9:
reed@android.com49f0ff22009-03-19 21:52:42 +0000188 if (quick == true) break;
189 //ctx.shadowOffsetX = make_number();
190 //ctx.shadowOffsetY = make_number();
191 break;
192
193 case 10:
194 canvas->restore();
195 break;
196
197 case 11:
198 canvas->rotate(make_number());
199 break;
200
201 case 12:
202 canvas->save();
203 break;
204
205 case 13:
206 canvas->scale(-1,-1);
207 break;
208
209 case 14:
210
211 if (quick == true) break;
212
213 if (transval == 0) {
214 transval = make_number();
215 canvas->translate(transval,0);
216 } else {
217 canvas->translate(-transval,0);
218 transval = 0;
219 }
220
221 break;
222
223 case 15: {
224 SkRect r;
225 r.set(make_number(),make_number(),make_number(),make_number());
226 SkPaint::Style s = paint.getStyle();
227 paint.setStyle(SkPaint::kFill_Style);
228 canvas->drawRect(r, paint);
229 paint.setStyle(s);
230 // clearrect
231 } break;
232
233 case 16:
234 if (quick == true) break;
235// ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
236 break;
237
238 case 17: {
239 SkRect r;
240 r.set(make_number(),make_number(),make_number(),make_number());
241 SkPaint::Style s = paint.getStyle();
242 paint.setStyle(SkPaint::kFill_Style);
243 canvas->drawRect(r, paint);
244 paint.setStyle(s);
245 } break;
246
247 case 18:
248 path.reset();
249 break;
250
251 case 19:
252 // ctx.clip() is evil.
253 break;
254
255 case 20:
256 path.close();
257 break;
258
259 case 21: {
260 SkPaint::Style s = paint.getStyle();
261 paint.setStyle(SkPaint::kFill_Style);
262 canvas->drawPath(path, paint);
263 paint.setStyle(s);
264 } break;
265
266 case 22: {
267 SkPaint::Style s = paint.getStyle();
268 paint.setStyle(SkPaint::kFill_Style);
269 canvas->drawPath(path, paint);
270 paint.setStyle(s);
271 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000272
reed@android.com49f0ff22009-03-19 21:52:42 +0000273 case 23: {
274 SkRect r;
275 r.set(make_number(),make_number(),make_number(),make_number());
276 SkPaint::Style s = paint.getStyle();
277 paint.setStyle(SkPaint::kStroke_Style);
278 canvas->drawRect(r, paint);
279 paint.setStyle(s);
280 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000281
reed@android.com49f0ff22009-03-19 21:52:42 +0000282 case 24:
283 if (quick == true) break;
284 //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true);
285 break;
286
287 case 25:
288 if (quick == true) break;
289 //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number());
290 break;
291
292 case 26:
293 if (quick == true) break;
294 //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
295 break;
296
297 case 27:
298 path.lineTo(make_number(),make_number());
299 break;
300
301 case 28:
302 path.moveTo(make_number(),make_number());
303 break;
304
305 case 29:
306 if (quick == true) break;
307 path.quadTo(make_number(),make_number(),make_number(),make_number());
308 break;
309
310 case 30: {
311 if (quick == true) break;
312 SkMatrix matrix;
313 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
314 canvas->concat(matrix);
315 } break;
316
317 case 31: {
318 if (quick == true) break;
319 SkMatrix matrix;
320 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
321 canvas->setMatrix(matrix);
322 } break;
323
324 case 32:
325
326 if (scale_large == true) {
327
328 switch (scval) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000329 case 0: canvas->scale(-1000000000,1);
reed@android.com49f0ff22009-03-19 21:52:42 +0000330 canvas->scale(-1000000000,1);
331 scval = 1; break;
reed@google.com261b8e22011-04-14 17:53:24 +0000332 case 1: canvas->scale(-.000000001f,1); scval = 2; break;
333 case 2: canvas->scale(-.000000001f,1); scval = 0; break;
reed@android.com49f0ff22009-03-19 21:52:42 +0000334 }
335
336 }
337
338 break;
339
340
341
342 }
343 }
344
345}
346
347//////////////////////////////////////////////////////////////////////////////
348
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000349class FuzzView : public SampleView {
reed@android.com49f0ff22009-03-19 21:52:42 +0000350public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000351 FuzzView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000352 this->setBGColor(0xFFDDDDDD);
353 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000354
reed@android.com49f0ff22009-03-19 21:52:42 +0000355protected:
356 // overrides from SkEventSink
357 virtual bool onQuery(SkEvent* evt) {
358 if (SampleCode::TitleQ(*evt)) {
359 SampleCode::TitleR(evt, "Fuzzer");
360 return true;
361 }
362 return this->INHERITED::onQuery(evt);
363 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000364
reed@android.com49f0ff22009-03-19 21:52:42 +0000365 void drawBG(SkCanvas* canvas) {
366 canvas->drawColor(0xFFDDDDDD);
367 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000368
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000369 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com49f0ff22009-03-19 21:52:42 +0000370 do_fuzz(canvas);
371 this->inval(NULL);
372 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000373
reed@android.com49f0ff22009-03-19 21:52:42 +0000374private:
375 typedef SkView INHERITED;
376};
377
378//////////////////////////////////////////////////////////////////////////////
379
380static SkView* MyFactory() { return new FuzzView; }
381static SkViewRegister reg(MyFactory);