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; |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 193 | SkSafeUnref(paint.setMaskFilter(SkBlurMaskFilter::Create( |
| 194 | SkBlurMaskFilter::kNormal_BlurStyle, |
| 195 | make_number()))); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 196 | break; |
| 197 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 198 | case 8: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 199 | if (quick == true) break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 200 | //ctx.shadowColor = make_fill(); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 201 | break; |
| 202 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 203 | case 9: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 204 | if (quick == true) break; |
| 205 | //ctx.shadowOffsetX = make_number(); |
| 206 | //ctx.shadowOffsetY = make_number(); |
| 207 | break; |
| 208 | |
| 209 | case 10: |
| 210 | canvas->restore(); |
| 211 | break; |
| 212 | |
| 213 | case 11: |
| 214 | canvas->rotate(make_number()); |
| 215 | break; |
| 216 | |
| 217 | case 12: |
| 218 | canvas->save(); |
| 219 | break; |
| 220 | |
| 221 | case 13: |
| 222 | canvas->scale(-1,-1); |
| 223 | break; |
| 224 | |
| 225 | case 14: |
| 226 | |
| 227 | if (quick == true) break; |
| 228 | |
| 229 | if (transval == 0) { |
| 230 | transval = make_number(); |
| 231 | canvas->translate(transval,0); |
| 232 | } else { |
| 233 | canvas->translate(-transval,0); |
| 234 | transval = 0; |
| 235 | } |
| 236 | |
| 237 | break; |
| 238 | |
| 239 | case 15: { |
| 240 | SkRect r; |
| 241 | r.set(make_number(),make_number(),make_number(),make_number()); |
| 242 | SkPaint::Style s = paint.getStyle(); |
| 243 | paint.setStyle(SkPaint::kFill_Style); |
| 244 | canvas->drawRect(r, paint); |
| 245 | paint.setStyle(s); |
| 246 | // clearrect |
| 247 | } break; |
| 248 | |
| 249 | case 16: |
| 250 | if (quick == true) break; |
| 251 | // ctx.drawImage(imgObj,make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 252 | break; |
| 253 | |
| 254 | case 17: { |
| 255 | SkRect r; |
| 256 | r.set(make_number(),make_number(),make_number(),make_number()); |
| 257 | SkPaint::Style s = paint.getStyle(); |
| 258 | paint.setStyle(SkPaint::kFill_Style); |
| 259 | canvas->drawRect(r, paint); |
| 260 | paint.setStyle(s); |
| 261 | } break; |
| 262 | |
| 263 | case 18: |
| 264 | path.reset(); |
| 265 | break; |
| 266 | |
| 267 | case 19: |
| 268 | // ctx.clip() is evil. |
| 269 | break; |
| 270 | |
| 271 | case 20: |
| 272 | path.close(); |
| 273 | break; |
| 274 | |
| 275 | case 21: { |
| 276 | SkPaint::Style s = paint.getStyle(); |
| 277 | paint.setStyle(SkPaint::kFill_Style); |
| 278 | canvas->drawPath(path, paint); |
| 279 | paint.setStyle(s); |
| 280 | } break; |
| 281 | |
| 282 | case 22: { |
| 283 | SkPaint::Style s = paint.getStyle(); |
| 284 | paint.setStyle(SkPaint::kFill_Style); |
| 285 | canvas->drawPath(path, paint); |
| 286 | paint.setStyle(s); |
| 287 | } break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 288 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 289 | case 23: { |
| 290 | SkRect r; |
| 291 | r.set(make_number(),make_number(),make_number(),make_number()); |
| 292 | SkPaint::Style s = paint.getStyle(); |
| 293 | paint.setStyle(SkPaint::kStroke_Style); |
| 294 | canvas->drawRect(r, paint); |
| 295 | paint.setStyle(s); |
| 296 | } break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 297 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 298 | case 24: |
| 299 | if (quick == true) break; |
| 300 | //ctx.arc(make_number(),make_number(),make_number(),make_number(),make_number(),true); |
| 301 | break; |
| 302 | |
| 303 | case 25: |
| 304 | if (quick == true) break; |
| 305 | //ctx.arcTo(make_number(),make_number(),make_number(),make_number(),make_number()); |
| 306 | break; |
| 307 | |
| 308 | case 26: |
| 309 | if (quick == true) break; |
| 310 | //ctx.bezierCurveTo(make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 311 | break; |
| 312 | |
| 313 | case 27: |
| 314 | path.lineTo(make_number(),make_number()); |
| 315 | break; |
| 316 | |
| 317 | case 28: |
| 318 | path.moveTo(make_number(),make_number()); |
| 319 | break; |
| 320 | |
| 321 | case 29: |
| 322 | if (quick == true) break; |
| 323 | path.quadTo(make_number(),make_number(),make_number(),make_number()); |
| 324 | break; |
| 325 | |
| 326 | case 30: { |
| 327 | if (quick == true) break; |
| 328 | SkMatrix matrix; |
| 329 | set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 330 | canvas->concat(matrix); |
| 331 | } break; |
| 332 | |
| 333 | case 31: { |
| 334 | if (quick == true) break; |
| 335 | SkMatrix matrix; |
| 336 | set2x3(&matrix, make_number(),make_number(),make_number(),make_number(),make_number(),make_number()); |
| 337 | canvas->setMatrix(matrix); |
| 338 | } break; |
| 339 | |
| 340 | case 32: |
| 341 | |
| 342 | if (scale_large == true) { |
| 343 | |
| 344 | switch (scval) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 345 | case 0: canvas->scale(-1000000000,1); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 346 | canvas->scale(-1000000000,1); |
| 347 | scval = 1; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 348 | case 1: canvas->scale(-.000000001f,1); scval = 2; break; |
| 349 | case 2: canvas->scale(-.000000001f,1); scval = 0; break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | } |
| 353 | |
| 354 | break; |
| 355 | |
| 356 | |
| 357 | |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | } |
| 362 | |
| 363 | ////////////////////////////////////////////////////////////////////////////// |
| 364 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 365 | class FuzzView : public SampleView { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 366 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 367 | FuzzView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 368 | this->setBGColor(0xFFDDDDDD); |
| 369 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 370 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 371 | protected: |
| 372 | // overrides from SkEventSink |
| 373 | virtual bool onQuery(SkEvent* evt) { |
| 374 | if (SampleCode::TitleQ(*evt)) { |
| 375 | SampleCode::TitleR(evt, "Fuzzer"); |
| 376 | return true; |
| 377 | } |
| 378 | return this->INHERITED::onQuery(evt); |
| 379 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 380 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 381 | void drawBG(SkCanvas* canvas) { |
| 382 | canvas->drawColor(0xFFDDDDDD); |
| 383 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 384 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 385 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 386 | do_fuzz(canvas); |
| 387 | this->inval(NULL); |
| 388 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 389 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 390 | private: |
| 391 | typedef SkView INHERITED; |
| 392 | }; |
| 393 | |
| 394 | ////////////////////////////////////////////////////////////////////////////// |
| 395 | |
| 396 | static SkView* MyFactory() { return new FuzzView; } |
| 397 | static SkViewRegister reg(MyFactory); |