epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 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.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 8 | #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.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 14 | #include "SkXfermode.h" |
| 15 | #include "SkMatrix.h" |
| 16 | #include "SkColor.h" |
| 17 | #include "SkRandom.h" |
| 18 | |
| 19 | static 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 | |
| 29 | static SkRandom gRand; |
| 30 | static bool return_large; |
| 31 | static bool return_undef; |
| 32 | static bool quick; |
| 33 | static bool scale_large; |
| 34 | static int scval = 1; |
| 35 | static float transval = 0; |
| 36 | |
| 37 | static int R(float x) { |
| 38 | return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x); |
| 39 | } |
| 40 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 41 | #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.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 48 | static float huge() { |
| 49 | double d = 1e100; |
| 50 | float f = (float)d; |
| 51 | return f; |
| 52 | } |
| 53 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 54 | #if defined _WIN32 |
| 55 | #pragma warning ( pop ) |
| 56 | #endif |
| 57 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 58 | static float make_number() { |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 59 | float v = 0; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 60 | 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.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 65 | if (R(2) == 1) v = (float)R(100); else |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 66 | |
| 67 | switch (sel) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | case 0: break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 69 | case 1: v = 0; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 70 | case 2: v = 0.000001f; break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 71 | case 3: v = 10000; break; |
| 72 | case 4: v = 2000000000; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 73 | case 5: v = huge(); break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | if (R(4) == 1) v = -v; |
| 77 | return v; |
| 78 | } |
| 79 | |
| 80 | static SkColor make_color() { |
| 81 | if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 92 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 93 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 124 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 125 | |
| 126 | static void do_fuzz(SkCanvas* canvas) { |
| 127 | SkPath path; |
| 128 | SkPaint paint; |
| 129 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 130 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 131 | 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.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 143 | SkXfermode::Mode mode; |
| 144 | switch (R(3)) { |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 145 | case 0: mode = SkXfermode::kSrc_Mode; break; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 146 | case 1: mode = SkXfermode::kXor_Mode; break; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 147 | case 2: |
| 148 | default: // silence warning |
| 149 | mode = SkXfermode::kSrcOver_Mode; break; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 150 | } |
reed@android.com | 5ebbe14 | 2009-11-05 21:04:00 +0000 | [diff] [blame] | 151 | paint.setXfermodeMode(mode); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 152 | } |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 159 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 160 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 166 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 167 | break; |
| 168 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 169 | case 5: |
| 170 | paint.setStrokeWidth(make_number()); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 173 | case 6: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 174 | paint.setStrokeMiter(make_number()); |
| 175 | break; |
| 176 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 177 | case 7: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 178 | if (quick == true) break; |
bsalomon@google.com | 9695921 | 2011-02-08 22:03:36 +0000 | [diff] [blame] | 179 | SkSafeUnref(paint.setMaskFilter(SkBlurMaskFilter::Create(make_number(), SkBlurMaskFilter::kNormal_BlurStyle))); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 180 | break; |
| 181 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 182 | case 8: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 183 | if (quick == true) break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 184 | //ctx.shadowColor = make_fill(); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 185 | break; |
| 186 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 187 | case 9: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 188 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 272 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 273 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 281 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 282 | 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 329 | case 0: canvas->scale(-1000000000,1); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 330 | canvas->scale(-1000000000,1); |
| 331 | scval = 1; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 332 | case 1: canvas->scale(-.000000001f,1); scval = 2; break; |
| 333 | case 2: canvas->scale(-.000000001f,1); scval = 0; break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | } |
| 337 | |
| 338 | break; |
| 339 | |
| 340 | |
| 341 | |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | } |
| 346 | |
| 347 | ////////////////////////////////////////////////////////////////////////////// |
| 348 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 349 | class FuzzView : public SampleView { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 350 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 351 | FuzzView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 352 | this->setBGColor(0xFFDDDDDD); |
| 353 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 354 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 355 | protected: |
| 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.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 364 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 365 | void drawBG(SkCanvas* canvas) { |
| 366 | canvas->drawColor(0xFFDDDDDD); |
| 367 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 368 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 369 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 370 | do_fuzz(canvas); |
| 371 | this->inval(NULL); |
| 372 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 373 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 374 | private: |
| 375 | typedef SkView INHERITED; |
| 376 | }; |
| 377 | |
| 378 | ////////////////////////////////////////////////////////////////////////////// |
| 379 | |
| 380 | static SkView* MyFactory() { return new FuzzView; } |
| 381 | static SkViewRegister reg(MyFactory); |