• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  */
7 #include "SampleCode.h"
8 #include "SkView.h"
9 #include "SkCanvas.h"
10 #include "SkBlurMaskFilter.h"
11 #include "SkPaint.h"
12 #include "SkPath.h"
13 #include "SkMatrix.h"
14 #include "SkColor.h"
15 #include "SkRandom.h"
16 
set2x3(SkMatrix * m,float a,float b,float c,float d,float e,float f)17 static 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 
27 static SkRandom gRand;
28 static bool return_large;
29 static bool return_undef;
30 static bool quick;
31 static bool scale_large;
32 static int scval = 1;
33 static float transval = 0;
34 
R(float x)35 static int R(float x) {
36   return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x);
37 }
38 
39 #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 
huge()46 static float huge() {
47     double d = 1e100;
48     float f = (float)d;
49     return f;
50 }
51 
52 #if defined _WIN32
53 #pragma warning ( pop )
54 #endif
55 
make_number()56 static float make_number() {
57   float v = 0;
58   int sel;
59 
60   if (return_large == true && R(3) == 1) {
61       sel = R(6);
62   } else {
63       sel = R(4);
64   }
65 
66   if (return_undef == false && sel == 0) {
67       sel = 1;
68   }
69 
70   if (R(2) == 1) {
71       v = (float)R(100);
72   } 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 
85   if (R(4) == 1) {
86       v = -v;
87   }
88 
89   return v;
90 }
91 
make_color()92 static SkColor make_color() {
93   if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090;
94 }
95 
96 
make_fill()97 static SkColor make_fill() {
98 #if 0
99   int sel;
100 
101   if (quick == true) sel = 0; else sel = R(6);
102 
103   switch (sel) {
104 
105     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 }
136 
137 
do_fuzz(SkCanvas * canvas)138 static void do_fuzz(SkCanvas* canvas) {
139     SkPath path;
140     SkPaint paint;
141     paint.setAntiAlias(true);
142 
143   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: {
155           SkBlendMode mode;
156           switch (R(3)) {
157             case 0: mode = SkBlendMode::kSrc; break;
158             case 1: mode = SkBlendMode::kXor; break;
159             case 2:
160             default:  // silence warning
161               mode = SkBlendMode::kSrcOver; break;
162           }
163           paint.setBlendMode(mode);
164       }
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;
171       }
172       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;
178       }
179       break;
180 
181     case 5:
182       paint.setStrokeWidth(make_number());
183       break;
184 
185     case 6:
186       paint.setStrokeMiter(make_number());
187       break;
188 
189     case 7:
190       if (quick == true) break;
191       paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, make_number()));
192       break;
193 
194     case 8:
195       if (quick == true) break;
196       //ctx.shadowColor = make_fill();
197       break;
198 
199     case 9:
200       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;
284 
285           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;
293 
294     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) {
341           case 0: canvas->scale(-1000000000,1);
342                   canvas->scale(-1000000000,1);
343                   scval = 1; break;
344           case 1: canvas->scale(-.000000001f,1); scval = 2; break;
345           case 2: canvas->scale(-.000000001f,1); scval = 0; break;
346         }
347 
348       }
349 
350       break;
351 
352 
353 
354   }
355   }
356 
357 }
358 
359 //////////////////////////////////////////////////////////////////////////////
360 
361 class FuzzView : public SampleView {
362 public:
FuzzView()363     FuzzView() {
364         this->setBGColor(0xFFDDDDDD);
365     }
366 
367 protected:
368     // overrides from SkEventSink
onQuery(SkEvent * evt)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     }
376 
drawBG(SkCanvas * canvas)377     void drawBG(SkCanvas* canvas) {
378         canvas->drawColor(0xFFDDDDDD);
379     }
380 
onDrawContent(SkCanvas * canvas)381     virtual void onDrawContent(SkCanvas* canvas) {
382         do_fuzz(canvas);
383     }
384 
385 private:
386     typedef SkView INHERITED;
387 };
388 
389 //////////////////////////////////////////////////////////////////////////////
390 
MyFactory()391 static SkView* MyFactory() { return new FuzzView; }
392 static SkViewRegister reg(MyFactory);
393