blob: a0cb0e02ba6ace7382931dd4ac98e96069e08395 [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 "SkMatrix.h"
14#include "SkColor.h"
15#include "SkRandom.h"
16
17static void set2x3(SkMatrix* m, float a, float b, float c, float d, float e, float f) {
18 m->reset();
19 m->set(0, a);
20 m->set(1, b);
21 m->set(2, c);
22 m->set(3, d);
23 m->set(4, e);
24 m->set(5, f);
25}
26
27static SkRandom gRand;
28static bool return_large;
29static bool return_undef;
30static bool quick;
31static bool scale_large;
32static int scval = 1;
33static float transval = 0;
34
35static int R(float x) {
36 return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
37}
38
robertphillips@google.com706f6212012-05-14 17:51:23 +000039#if defined _WIN32
40#pragma warning ( push )
41// we are intentionally causing an overflow here
42// (warning C4756: overflow in constant arithmetic)
43#pragma warning ( disable : 4756 )
44#endif
45
reed@google.com261b8e22011-04-14 17:53:24 +000046static float huge() {
47 double d = 1e100;
48 float f = (float)d;
49 return f;
50}
51
robertphillips@google.com706f6212012-05-14 17:51:23 +000052#if defined _WIN32
53#pragma warning ( pop )
54#endif
55
reed@android.com49f0ff22009-03-19 21:52:42 +000056static float make_number() {
tomhudson@google.com2c2508d2011-07-29 13:44:30 +000057 float v = 0;
reed@android.com49f0ff22009-03-19 21:52:42 +000058 int sel;
59
robertphillips@google.comb7061172013-09-06 14:16:12 +000060 if (return_large == true && R(3) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000061 sel = R(6);
robertphillips@google.comb7061172013-09-06 14:16:12 +000062 } else {
63 sel = R(4);
reed@android.com49f0ff22009-03-19 21:52:42 +000064 }
65
robertphillips@google.comb7061172013-09-06 14:16:12 +000066 if (return_undef == false && sel == 0) {
67 sel = 1;
68 }
69
70 if (R(2) == 1) {
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000071 v = (float)R(100);
robertphillips@google.comb7061172013-09-06 14:16:12 +000072 } else {
73
74 switch (sel) {
75 case 0: break;
76 case 1: v = 0; break;
77 case 2: v = 0.000001f; break;
78 case 3: v = 10000; break;
79 case 4: v = 2000000000; break;
80 case 5: v = huge(); break;
81 }
82
83 }
84
skia.committer@gmail.comb3ec29d2013-09-07 07:01:16 +000085 if (R(4) == 1) {
robertphillips@google.comb7061172013-09-06 14:16:12 +000086 v = -v;
87 }
88
reed@android.com49f0ff22009-03-19 21:52:42 +000089 return v;
90}
91
92static SkColor make_color() {
93 if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
94}
95
96
97static SkColor make_fill() {
98#if 0
99 int sel;
100
101 if (quick == true) sel = 0; else sel = R(6);
102
103 switch (sel) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000104
reed@android.com49f0ff22009-03-19 21:52:42 +0000105 case 0:
106 case 1:
107 case 2:
108 return make_color();
109 break;
110
111 case 3:
112 var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number());
113 for (i=0;i<4;i++)
114 r.addColorStop(make_number(),make_color());
115 return r;
116 break;
117
118 case 4:
119 var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
120 for (i=0;i<4;i++)
121 r.addColorStop(make_number(),make_color());
122 return r;
123 break;
124
125 case 5:
126 var r = ctx.createPattern(imgObj,"repeat");
127 if (R(6) == 0)
128 r.addColorStop(make_number(),make_color());
129 return r;
130 break;
131 }
132#else
133 return make_color();
134#endif
135}
rmistry@google.comae933ce2012-08-23 18:19:56 +0000136
reed@android.com49f0ff22009-03-19 21:52:42 +0000137
138static void do_fuzz(SkCanvas* canvas) {
139 SkPath path;
140 SkPaint paint;
141 paint.setAntiAlias(true);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000142
reed@android.com49f0ff22009-03-19 21:52:42 +0000143 for (int i=0;i<100;i++) {
144 switch (R(33)) {
145
146 case 0:
147 paint.setColor(make_fill());
148 break;
149
150 case 1:
151 paint.setAlpha(gRand.nextU() & 0xFF);
152 break;
153
154 case 2: {
reed374772b2016-10-05 17:33:02 -0700155 SkBlendMode mode;
reed@android.com048522d2009-06-23 12:19:41 +0000156 switch (R(3)) {
reed374772b2016-10-05 17:33:02 -0700157 case 0: mode = SkBlendMode::kSrc; break;
158 case 1: mode = SkBlendMode::kXor; break;
tomhudson@google.com83a44462011-10-27 15:27:51 +0000159 case 2:
160 default: // silence warning
reed374772b2016-10-05 17:33:02 -0700161 mode = SkBlendMode::kSrcOver; break;
reed@android.com048522d2009-06-23 12:19:41 +0000162 }
reed374772b2016-10-05 17:33:02 -0700163 paint.setBlendMode(mode);
reed@android.com49f0ff22009-03-19 21:52:42 +0000164 }
165 break;
166
167 case 3:
168 switch (R(2)) {
169 case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break;
170 case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000171 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000172 break;
173
174 case 4:
175 switch (R(2)) {
176 case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break;
177 case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000178 }
reed@android.com49f0ff22009-03-19 21:52:42 +0000179 break;
180
rmistry@google.comae933ce2012-08-23 18:19:56 +0000181 case 5:
182 paint.setStrokeWidth(make_number());
reed@android.com49f0ff22009-03-19 21:52:42 +0000183 break;
184
rmistry@google.comae933ce2012-08-23 18:19:56 +0000185 case 6:
reed@android.com49f0ff22009-03-19 21:52:42 +0000186 paint.setStrokeMiter(make_number());
187 break;
188
rmistry@google.comae933ce2012-08-23 18:19:56 +0000189 case 7:
reed@android.com49f0ff22009-03-19 21:52:42 +0000190 if (quick == true) break;
reedefdfd512016-04-04 10:02:58 -0700191 paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, make_number()));
reed@android.com49f0ff22009-03-19 21:52:42 +0000192 break;
193
rmistry@google.comae933ce2012-08-23 18:19:56 +0000194 case 8:
reed@android.com49f0ff22009-03-19 21:52:42 +0000195 if (quick == true) break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000196 //ctx.shadowColor = make_fill();
reed@android.com49f0ff22009-03-19 21:52:42 +0000197 break;
198
rmistry@google.comae933ce2012-08-23 18:19:56 +0000199 case 9:
reed@android.com49f0ff22009-03-19 21:52:42 +0000200 if (quick == true) break;
201 //ctx.shadowOffsetX = make_number();
202 //ctx.shadowOffsetY = make_number();
203 break;
204
205 case 10:
206 canvas->restore();
207 break;
208
209 case 11:
210 canvas->rotate(make_number());
211 break;
212
213 case 12:
214 canvas->save();
215 break;
216
217 case 13:
218 canvas->scale(-1,-1);
219 break;
220
221 case 14:
222
223 if (quick == true) break;
224
225 if (transval == 0) {
226 transval = make_number();
227 canvas->translate(transval,0);
228 } else {
229 canvas->translate(-transval,0);
230 transval = 0;
231 }
232
233 break;
234
235 case 15: {
236 SkRect r;
237 r.set(make_number(),make_number(),make_number(),make_number());
238 SkPaint::Style s = paint.getStyle();
239 paint.setStyle(SkPaint::kFill_Style);
240 canvas->drawRect(r, paint);
241 paint.setStyle(s);
242 // clearrect
243 } break;
244
245 case 16:
246 if (quick == true) break;
247// ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
248 break;
249
250 case 17: {
251 SkRect r;
252 r.set(make_number(),make_number(),make_number(),make_number());
253 SkPaint::Style s = paint.getStyle();
254 paint.setStyle(SkPaint::kFill_Style);
255 canvas->drawRect(r, paint);
256 paint.setStyle(s);
257 } break;
258
259 case 18:
260 path.reset();
261 break;
262
263 case 19:
264 // ctx.clip() is evil.
265 break;
266
267 case 20:
268 path.close();
269 break;
270
271 case 21: {
272 SkPaint::Style s = paint.getStyle();
273 paint.setStyle(SkPaint::kFill_Style);
274 canvas->drawPath(path, paint);
275 paint.setStyle(s);
276 } break;
277
278 case 22: {
279 SkPaint::Style s = paint.getStyle();
280 paint.setStyle(SkPaint::kFill_Style);
281 canvas->drawPath(path, paint);
282 paint.setStyle(s);
283 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000284
reed@android.com49f0ff22009-03-19 21:52:42 +0000285 case 23: {
286 SkRect r;
287 r.set(make_number(),make_number(),make_number(),make_number());
288 SkPaint::Style s = paint.getStyle();
289 paint.setStyle(SkPaint::kStroke_Style);
290 canvas->drawRect(r, paint);
291 paint.setStyle(s);
292 } break;
rmistry@google.comae933ce2012-08-23 18:19:56 +0000293
reed@android.com49f0ff22009-03-19 21:52:42 +0000294 case 24:
295 if (quick == true) break;
296 //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true);
297 break;
298
299 case 25:
300 if (quick == true) break;
301 //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number());
302 break;
303
304 case 26:
305 if (quick == true) break;
306 //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
307 break;
308
309 case 27:
310 path.lineTo(make_number(),make_number());
311 break;
312
313 case 28:
314 path.moveTo(make_number(),make_number());
315 break;
316
317 case 29:
318 if (quick == true) break;
319 path.quadTo(make_number(),make_number(),make_number(),make_number());
320 break;
321
322 case 30: {
323 if (quick == true) break;
324 SkMatrix matrix;
325 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
326 canvas->concat(matrix);
327 } break;
328
329 case 31: {
330 if (quick == true) break;
331 SkMatrix matrix;
332 set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number());
333 canvas->setMatrix(matrix);
334 } break;
335
336 case 32:
337
338 if (scale_large == true) {
339
340 switch (scval) {
rmistry@google.comae933ce2012-08-23 18:19:56 +0000341 case 0: canvas->scale(-1000000000,1);
reed@android.com49f0ff22009-03-19 21:52:42 +0000342 canvas->scale(-1000000000,1);
343 scval = 1; break;
reed@google.com261b8e22011-04-14 17:53:24 +0000344 case 1: canvas->scale(-.000000001f,1); scval = 2; break;
345 case 2: canvas->scale(-.000000001f,1); scval = 0; break;
reed@android.com49f0ff22009-03-19 21:52:42 +0000346 }
347
348 }
349
350 break;
351
352
353
354 }
355 }
356
357}
358
359//////////////////////////////////////////////////////////////////////////////
360
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000361class FuzzView : public SampleView {
reed@android.com49f0ff22009-03-19 21:52:42 +0000362public:
rmistry@google.comae933ce2012-08-23 18:19:56 +0000363 FuzzView() {
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000364 this->setBGColor(0xFFDDDDDD);
365 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000366
reed@android.com49f0ff22009-03-19 21:52:42 +0000367protected:
368 // overrides from SkEventSink
369 virtual bool onQuery(SkEvent* evt) {
370 if (SampleCode::TitleQ(*evt)) {
371 SampleCode::TitleR(evt, "Fuzzer");
372 return true;
373 }
374 return this->INHERITED::onQuery(evt);
375 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000376
reed@android.com49f0ff22009-03-19 21:52:42 +0000377 void drawBG(SkCanvas* canvas) {
378 canvas->drawColor(0xFFDDDDDD);
379 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000380
mike@reedtribe.org5fd92432011-05-05 01:59:48 +0000381 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com49f0ff22009-03-19 21:52:42 +0000382 do_fuzz(canvas);
halcanary96fcdcc2015-08-27 07:41:13 -0700383 this->inval(nullptr);
reed@android.com49f0ff22009-03-19 21:52:42 +0000384 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000385
reed@android.com49f0ff22009-03-19 21:52:42 +0000386private:
387 typedef SkView INHERITED;
388};
389
390//////////////////////////////////////////////////////////////////////////////
391
392static SkView* MyFactory() { return new FuzzView; }
393static SkViewRegister reg(MyFactory);