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 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 62 | if (return_large == true && R(3) == 1) { |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 63 | sel = R(6); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 64 | } else { |
| 65 | sel = R(4); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 66 | } |
| 67 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 68 | if (return_undef == false && sel == 0) { |
| 69 | sel = 1; |
| 70 | } |
| 71 | |
| 72 | if (R(2) == 1) { |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 73 | v = (float)R(100); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 74 | } else { |
| 75 | |
| 76 | switch (sel) { |
| 77 | case 0: break; |
| 78 | case 1: v = 0; break; |
| 79 | case 2: v = 0.000001f; break; |
| 80 | case 3: v = 10000; break; |
| 81 | case 4: v = 2000000000; break; |
| 82 | case 5: v = huge(); break; |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 87 | if (R(4) == 1) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 88 | v = -v; |
| 89 | } |
| 90 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 91 | return v; |
| 92 | } |
| 93 | |
| 94 | static SkColor make_color() { |
| 95 | if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static SkColor make_fill() { |
| 100 | #if 0 |
| 101 | int sel; |
| 102 | |
| 103 | if (quick == true) sel = 0; else sel = R(6); |
| 104 | |
| 105 | switch (sel) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 106 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 107 | case 0: |
| 108 | case 1: |
| 109 | case 2: |
| 110 | return make_color(); |
| 111 | break; |
| 112 | |
| 113 | case 3: |
| 114 | var r = ctx.createLinearGradient(make_number(),make_number(),make_number(),make_number()); |
| 115 | for (i=0;i<4;i++) |
| 116 | r.addColorStop(make_number(),make_color()); |
| 117 | return r; |
| 118 | break; |
| 119 | |
| 120 | case 4: |
| 121 | var r = ctx.createRadialGradient(make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 122 | for (i=0;i<4;i++) |
| 123 | r.addColorStop(make_number(),make_color()); |
| 124 | return r; |
| 125 | break; |
| 126 | |
| 127 | case 5: |
| 128 | var r = ctx.createPattern(imgObj,"repeat"); |
| 129 | if (R(6) == 0) |
| 130 | r.addColorStop(make_number(),make_color()); |
| 131 | return r; |
| 132 | break; |
| 133 | } |
| 134 | #else |
| 135 | return make_color(); |
| 136 | #endif |
| 137 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 138 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 139 | |
| 140 | static void do_fuzz(SkCanvas* canvas) { |
| 141 | SkPath path; |
| 142 | SkPaint paint; |
| 143 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 144 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 145 | for (int i=0;i<100;i++) { |
| 146 | switch (R(33)) { |
| 147 | |
| 148 | case 0: |
| 149 | paint.setColor(make_fill()); |
| 150 | break; |
| 151 | |
| 152 | case 1: |
| 153 | paint.setAlpha(gRand.nextU() & 0xFF); |
| 154 | break; |
| 155 | |
| 156 | case 2: { |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 157 | SkXfermode::Mode mode; |
| 158 | switch (R(3)) { |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 159 | case 0: mode = SkXfermode::kSrc_Mode; break; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 160 | case 1: mode = SkXfermode::kXor_Mode; break; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 161 | case 2: |
| 162 | default: // silence warning |
| 163 | mode = SkXfermode::kSrcOver_Mode; break; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 164 | } |
reed@android.com | 5ebbe14 | 2009-11-05 21:04:00 +0000 | [diff] [blame] | 165 | paint.setXfermodeMode(mode); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 166 | } |
| 167 | break; |
| 168 | |
| 169 | case 3: |
| 170 | switch (R(2)) { |
| 171 | case 0: paint.setStrokeCap(SkPaint::kRound_Cap); break; |
| 172 | case 1: paint.setStrokeCap(SkPaint::kButt_Cap); break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 173 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | case 4: |
| 177 | switch (R(2)) { |
| 178 | case 0: paint.setStrokeJoin(SkPaint::kRound_Join); break; |
| 179 | case 1: paint.setStrokeJoin(SkPaint::kMiter_Join); break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 180 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 181 | break; |
| 182 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 183 | case 5: |
| 184 | paint.setStrokeWidth(make_number()); |
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 6: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 188 | paint.setStrokeMiter(make_number()); |
| 189 | break; |
| 190 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 191 | case 7: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 192 | if (quick == true) break; |
commit-bot@chromium.org | e396455 | 2014-04-28 16:25:35 +0000 | [diff] [blame] | 193 | SkSafeUnref(paint.setMaskFilter(SkBlurMaskFilter::Create(kNormal_SkBlurStyle, |
| 194 | make_number()))); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 195 | break; |
| 196 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 197 | case 8: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 198 | if (quick == true) break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 199 | //ctx.shadowColor = make_fill(); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 202 | case 9: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 203 | if (quick == true) break; |
| 204 | //ctx.shadowOffsetX = make_number(); |
| 205 | //ctx.shadowOffsetY = make_number(); |
| 206 | break; |
| 207 | |
| 208 | case 10: |
| 209 | canvas->restore(); |
| 210 | break; |
| 211 | |
| 212 | case 11: |
| 213 | canvas->rotate(make_number()); |
| 214 | break; |
| 215 | |
| 216 | case 12: |
| 217 | canvas->save(); |
| 218 | break; |
| 219 | |
| 220 | case 13: |
| 221 | canvas->scale(-1,-1); |
| 222 | break; |
| 223 | |
| 224 | case 14: |
| 225 | |
| 226 | if (quick == true) break; |
| 227 | |
| 228 | if (transval == 0) { |
| 229 | transval = make_number(); |
| 230 | canvas->translate(transval,0); |
| 231 | } else { |
| 232 | canvas->translate(-transval,0); |
| 233 | transval = 0; |
| 234 | } |
| 235 | |
| 236 | break; |
| 237 | |
| 238 | case 15: { |
| 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 | // clearrect |
| 246 | } break; |
| 247 | |
| 248 | case 16: |
| 249 | if (quick == true) break; |
| 250 | // ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 251 | break; |
| 252 | |
| 253 | case 17: { |
| 254 | SkRect r; |
| 255 | r.set(make_number(),make_number(),make_number(),make_number()); |
| 256 | SkPaint::Style s = paint.getStyle(); |
| 257 | paint.setStyle(SkPaint::kFill_Style); |
| 258 | canvas->drawRect(r, paint); |
| 259 | paint.setStyle(s); |
| 260 | } break; |
| 261 | |
| 262 | case 18: |
| 263 | path.reset(); |
| 264 | break; |
| 265 | |
| 266 | case 19: |
| 267 | // ctx.clip() is evil. |
| 268 | break; |
| 269 | |
| 270 | case 20: |
| 271 | path.close(); |
| 272 | break; |
| 273 | |
| 274 | case 21: { |
| 275 | SkPaint::Style s = paint.getStyle(); |
| 276 | paint.setStyle(SkPaint::kFill_Style); |
| 277 | canvas->drawPath(path, paint); |
| 278 | paint.setStyle(s); |
| 279 | } break; |
| 280 | |
| 281 | case 22: { |
| 282 | SkPaint::Style s = paint.getStyle(); |
| 283 | paint.setStyle(SkPaint::kFill_Style); |
| 284 | canvas->drawPath(path, paint); |
| 285 | paint.setStyle(s); |
| 286 | } break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 287 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 288 | case 23: { |
| 289 | SkRect r; |
| 290 | r.set(make_number(),make_number(),make_number(),make_number()); |
| 291 | SkPaint::Style s = paint.getStyle(); |
| 292 | paint.setStyle(SkPaint::kStroke_Style); |
| 293 | canvas->drawRect(r, paint); |
| 294 | paint.setStyle(s); |
| 295 | } break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 296 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 297 | case 24: |
| 298 | if (quick == true) break; |
| 299 | //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true); |
| 300 | break; |
| 301 | |
| 302 | case 25: |
| 303 | if (quick == true) break; |
| 304 | //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number()); |
| 305 | break; |
| 306 | |
| 307 | case 26: |
| 308 | if (quick == true) break; |
| 309 | //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 310 | break; |
| 311 | |
| 312 | case 27: |
| 313 | path.lineTo(make_number(),make_number()); |
| 314 | break; |
| 315 | |
| 316 | case 28: |
| 317 | path.moveTo(make_number(),make_number()); |
| 318 | break; |
| 319 | |
| 320 | case 29: |
| 321 | if (quick == true) break; |
| 322 | path.quadTo(make_number(),make_number(),make_number(),make_number()); |
| 323 | break; |
| 324 | |
| 325 | case 30: { |
| 326 | if (quick == true) break; |
| 327 | SkMatrix matrix; |
| 328 | set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 329 | canvas->concat(matrix); |
| 330 | } break; |
| 331 | |
| 332 | case 31: { |
| 333 | if (quick == true) break; |
| 334 | SkMatrix matrix; |
| 335 | set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 336 | canvas->setMatrix(matrix); |
| 337 | } break; |
| 338 | |
| 339 | case 32: |
| 340 | |
| 341 | if (scale_large == true) { |
| 342 | |
| 343 | switch (scval) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 344 | case 0: canvas->scale(-1000000000,1); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 345 | canvas->scale(-1000000000,1); |
| 346 | scval = 1; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 347 | case 1: canvas->scale(-.000000001f,1); scval = 2; break; |
| 348 | case 2: canvas->scale(-.000000001f,1); scval = 0; break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | } |
| 352 | |
| 353 | break; |
| 354 | |
| 355 | |
| 356 | |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | } |
| 361 | |
| 362 | ////////////////////////////////////////////////////////////////////////////// |
| 363 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 364 | class FuzzView : public SampleView { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 365 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 366 | FuzzView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 367 | this->setBGColor(0xFFDDDDDD); |
| 368 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 369 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 370 | protected: |
| 371 | // overrides from SkEventSink |
| 372 | virtual bool onQuery(SkEvent* evt) { |
| 373 | if (SampleCode::TitleQ(*evt)) { |
| 374 | SampleCode::TitleR(evt, "Fuzzer"); |
| 375 | return true; |
| 376 | } |
| 377 | return this->INHERITED::onQuery(evt); |
| 378 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 379 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 380 | void drawBG(SkCanvas* canvas) { |
| 381 | canvas->drawColor(0xFFDDDDDD); |
| 382 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 383 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 384 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 385 | do_fuzz(canvas); |
| 386 | this->inval(NULL); |
| 387 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 388 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 389 | private: |
| 390 | typedef SkView INHERITED; |
| 391 | }; |
| 392 | |
| 393 | ////////////////////////////////////////////////////////////////////////////// |
| 394 | |
| 395 | static SkView* MyFactory() { return new FuzzView; } |
| 396 | static SkViewRegister reg(MyFactory); |