blob: 76420f026a6e53ed81cc0dc1d95cedc62c2b5e21 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reed@android.com49f0ff22009-03-19 21:52:42 +00007#include "SampleCode.h"
8#include "SkView.h"
9#include "SkCanvas.h"
10#include "SkBlurMaskFilter.h"
11#include "SkPaint.h"
12#include "SkPath.h"
reed@android.com49f0ff22009-03-19 21:52:42 +000013#include "SkXfermode.h"
14#include "SkMatrix.h"
15#include "SkColor.h"
16#include "SkRandom.h"
17
18static void set2x3(SkMatrix* m, float a, float b, float c, float d, float e, float f) {
19 m->reset();
20 m->set(0, a);
21 m->set(1, b);
22 m->set(2, c);
23 m->set(3, d);
24 m->set(4, e);
25 m->set(5, f);
26}
27
28static SkRandom gRand;
29static bool return_large;
30static bool return_undef;
31static bool quick;
32static bool scale_large;
33static int scval = 1;
34static float transval = 0;
35
36static int R(float x) {
37 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
38}
39
robertphillips@google.com706f6212012-05-14 17:51:23 +000040#if defined _WIN32
41#pragma warning ( push )
42// we are intentionally causing an overflow here
43// (warning C4756: overflow in constant arithmetic)
44#pragma warning ( disable : 4756 )
45#endif
46
reed@google.com261b8e22011-04-14 17:53:24 +000047static float huge() {
48 double d = 1e100;
49 float f = (float)d;
50 return f;
51}
52
robertphillips@google.com706f6212012-05-14 17:51:23 +000053#if defined _WIN32
54#pragma warning ( pop )
55#endif
56
reed@android.com49f0ff22009-03-19 21:52:42 +000057static float make_number() {
tomhudson@google.com2c2508d2011-07-29 13:44:30 +000058 float v = 0;
reed@android.com49f0ff22009-03-19 21:52:42 +000059 int sel;
60
robertphillips@google.comb7061172013-09-06 14:16:12 +000061 if (return_large == true && R(3) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000062 sel = R(6);
robertphillips@google.comb7061172013-09-06 14:16:12 +000063 } else {
64 sel = R(4);
reed@android.com49f0ff22009-03-19 21:52:42 +000065 }
66
robertphillips@google.comb7061172013-09-06 14:16:12 +000067 if (return_undef == false && sel == 0) {
68 sel = 1;
69 }
70
71 if (R(2) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000072 v = (float)R(100);
robertphillips@google.comb7061172013-09-06 14:16:12 +000073 } else {
74
75 switch (sel) {
76 case 0: break;
77 case 1: v = 0; break;
78 case 2: v = 0.000001f; break;
79 case 3: v = 10000; break;
80 case 4: v = 2000000000; break;
81 case 5: v = huge(); break;
82 }
83
84 }
85
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000086 if (R(4) == 1) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000087 v = -v;
88 }
89
reed@android.com49f0ff22009-03-19 21:52:42 +000090 return v;
91}
92
93static SkColor make_color() {
94 if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
95}
96
97
98static SkColor make_fill() {
99#if 0
100 int sel;
101
102 if (quick == true) sel = 0; else sel = R(6);
103
104 switch (sel) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000105
reed@android.com49f0ff22009-03-19 21:52:42 +0000106 case 0:
107 case 1:
108 case 2:
109 return make_color();
110 break;
111
112 case 3:
113 var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number());
114 for (i=0;i<4;i++)
115 r.addColorStop(make_number(),make_color());
116 return r;
117 break;
118
119 case 4:
120 var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
121 for (i=0;i<4;i++)
122 r.addColorStop(make_number(),make_color());
123 return r;
124 break;
125
126 case 5:
127 var r = ctx.createPattern(imgObj,"repeat");
128 if (R(6) == 0)
129 r.addColorStop(make_number(),make_color());
130 return r;
131 break;
132 }
133#else
134 return make_color();
135#endif
136}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000137
reed@android.com49f0ff22009-03-19 21:52:42 +0000138
139static void do_fuzz(SkCanvas* canvas) {
140 SkPath path;
141 SkPaint paint;
142 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000143
reed@android.com49f0ff22009-03-19 21:52:42 +0000144 for (int i=0;i<100;i++) {
145 switch (R(33)) {
146
147 case 0:
148 paint.setColor(make_fill());
149 break;
150
151 case 1:
152 paint.setAlpha(gRand.nextU() & 0xFF);
153 break;
154
155 case 2: {
reed@android.com048522d2009-06-23 12:19:41 +0000156 SkXfermode::Mode mode;
157 switch (R(3)) {
tomhudson@google.com83a44462011-10-27 15:27:51 +0000158 case 0: mode = SkXfermode::kSrc_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000159 case 1: mode = SkXfermode::kXor_Mode; break;
tomhudson@google.com83a44462011-10-27 15:27:51 +0000160 case 2:
161 default: // silence warning
162 mode = SkXfermode::kSrcOver_Mode; break;
reed@android.com048522d2009-06-23 12:19:41 +0000163 }
reed@android.com5ebbe142009-11-05 21:04:00 +0000164 paint.setXfermodeMode(mode);
reed@android.com49f0ff22009-03-19 21:52:42 +0000165 }
166 break;
167
168 case 3:
169 switch (R(2)) {
170 case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break;
171 case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000172 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000173 break;
174
175 case 4:
176 switch (R(2)) {
177 case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break;
178 case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000179 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000180 break;
181
rmistry@google.comae933ce2012-08-23 18:19:56 +0000182 case 5:
183 paint.setStrokeWidth(make_number());
reed@android.com49f0ff22009-03-19 21:52:42 +0000184 break;
185
rmistry@google.comae933ce2012-08-23 18:19:56 +0000186 case 6:
reed@android.com49f0ff22009-03-19 21:52:42 +0000187 paint.setStrokeMiter(make_number());
188 break;
189
rmistry@google.comae933ce2012-08-23 18:19:56 +0000190 case 7:
reed@android.com49f0ff22009-03-19 21:52:42 +0000191 if (quick == true) break;
reedefdfd512016-04-04 10:02:58 -0700192 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, make_number()));
reed@android.com49f0ff22009-03-19 21:52:42 +0000193 break;
194
rmistry@google.comae933ce2012-08-23 18:19:56 +0000195 case 8:
reed@android.com49f0ff22009-03-19 21:52:42 +0000196 if (quick == true) break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000197 //ctx.shadowColor = make_fill();
reed@android.com49f0ff22009-03-19 21:52:42 +0000198 break;
199
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200 case 9:
reed@android.com49f0ff22009-03-19 21:52:42 +0000201 if (quick == true) break;
202 //ctx.shadowOffsetX = make_number();
203 //ctx.shadowOffsetY = make_number();
204 break;
205
206 case 10:
207 canvas->restore();
208 break;
209
210 case 11:
211 canvas->rotate(make_number());
212 break;
213
214 case 12:
215 canvas->save();
216 break;
217
218 case 13:
219 canvas->scale(-1,-1);
220 break;
221
222 case 14:
223
224 if (quick == true) break;
225
226 if (transval == 0) {
227 transval = make_number();
228 canvas->translate(transval,0);
229 } else {
230 canvas->translate(-transval,0);
231 transval = 0;
232 }
233
234 break;
235
236 case 15: {
237 SkRect r;
238 r.set(make_number(),make_number(),make_number(),make_number());
239 SkPaint::Style s = paint.getStyle();
240 paint.setStyle(SkPaint::kFill_Style);
241 canvas->drawRect(r, paint);
242 paint.setStyle(s);
243 // clearrect
244 } break;
245
246 case 16:
247 if (quick == true) break;
248// ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
249 break;
250
251 case 17: {
252 SkRect r;
253 r.set(make_number(),make_number(),make_number(),make_number());
254 SkPaint::Style s = paint.getStyle();
255 paint.setStyle(SkPaint::kFill_Style);
256 canvas->drawRect(r, paint);
257 paint.setStyle(s);
258 } break;
259
260 case 18:
261 path.reset();
262 break;
263
264 case 19:
265 // ctx.clip() is evil.
266 break;
267
268 case 20:
269 path.close();
270 break;
271
272 case 21: {
273 SkPaint::Style s = paint.getStyle();
274 paint.setStyle(SkPaint::kFill_Style);
275 canvas->drawPath(path, paint);
276 paint.setStyle(s);
277 } break;
278
279 case 22: {
280 SkPaint::Style s = paint.getStyle();
281 paint.setStyle(SkPaint::kFill_Style);
282 canvas->drawPath(path, paint);
283 paint.setStyle(s);
284 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000285
reed@android.com49f0ff22009-03-19 21:52:42 +0000286 case 23: {
287 SkRect r;
288 r.set(make_number(),make_number(),make_number(),make_number());
289 SkPaint::Style s = paint.getStyle();
290 paint.setStyle(SkPaint::kStroke_Style);
291 canvas->drawRect(r, paint);
292 paint.setStyle(s);
293 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000294
reed@android.com49f0ff22009-03-19 21:52:42 +0000295 case 24:
296 if (quick == true) break;
297 //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true);
298 break;
299
300 case 25:
301 if (quick == true) break;
302 //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number());
303 break;
304
305 case 26:
306 if (quick == true) break;
307 //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
308 break;
309
310 case 27:
311 path.lineTo(make_number(),make_number());
312 break;
313
314 case 28:
315 path.moveTo(make_number(),make_number());
316 break;
317
318 case 29:
319 if (quick == true) break;
320 path.quadTo(make_number(),make_number(),make_number(),make_number());
321 break;
322
323 case 30: {
324 if (quick == true) break;
325 SkMatrix matrix;
326 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
327 canvas->concat(matrix);
328 } break;
329
330 case 31: {
331 if (quick == true) break;
332 SkMatrix matrix;
333 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
334 canvas->setMatrix(matrix);
335 } break;
336
337 case 32:
338
339 if (scale_large == true) {
340
341 switch (scval) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000342 case 0: canvas->scale(-1000000000,1);
reed@android.com49f0ff22009-03-19 21:52:42 +0000343 canvas->scale(-1000000000,1);
344 scval = 1; break;
reed@google.com261b8e22011-04-14 17:53:24 +0000345 case 1: canvas->scale(-.000000001f,1); scval = 2; break;
346 case 2: canvas->scale(-.000000001f,1); scval = 0; break;
reed@android.com49f0ff22009-03-19 21:52:42 +0000347 }
348
349 }
350
351 break;
352
353
354
355 }
356 }
357
358}
359
360//////////////////////////////////////////////////////////////////////////////
361
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000362class FuzzView : public SampleView {
reed@android.com49f0ff22009-03-19 21:52:42 +0000363public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000364 FuzzView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000365 this->setBGColor(0xFFDDDDDD);
366 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000367
reed@android.com49f0ff22009-03-19 21:52:42 +0000368protected:
369 // overrides from SkEventSink
370 virtual bool onQuery(SkEvent* evt) {
371 if (SampleCode::TitleQ(*evt)) {
372 SampleCode::TitleR(evt, "Fuzzer");
373 return true;
374 }
375 return this->INHERITED::onQuery(evt);
376 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000377
reed@android.com49f0ff22009-03-19 21:52:42 +0000378 void drawBG(SkCanvas* canvas) {
379 canvas->drawColor(0xFFDDDDDD);
380 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000381
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000382 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com49f0ff22009-03-19 21:52:42 +0000383 do_fuzz(canvas);
halcanary96fcdcc2015-08-27 07:41:13 -0700384 this->inval(nullptr);
reed@android.com49f0ff22009-03-19 21:52:42 +0000385 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000386
reed@android.com49f0ff22009-03-19 21:52:42 +0000387private:
388 typedef SkView INHERITED;
389};
390
391//////////////////////////////////////////////////////////////////////////////
392
393static SkView* MyFactory() { return new FuzzView; }
394static SkViewRegister reg(MyFactory);