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