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 "SkMatrix.h" |
| 14 | #include "SkColor.h" |
| 15 | #include "SkRandom.h" |
| 16 | |
| 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 | |
| 35 | static int R(float x) { |
| 36 | return (int)floor(SkScalarToFloat(gRand.nextUScalar1()) * x); |
| 37 | } |
| 38 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 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 | |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 46 | static float huge() { |
| 47 | double d = 1e100; |
| 48 | float f = (float)d; |
| 49 | return f; |
| 50 | } |
| 51 | |
robertphillips@google.com | 706f621 | 2012-05-14 17:51:23 +0000 | [diff] [blame] | 52 | #if defined _WIN32 |
| 53 | #pragma warning ( pop ) |
| 54 | #endif |
| 55 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 56 | static float make_number() { |
tomhudson@google.com | 2c2508d | 2011-07-29 13:44:30 +0000 | [diff] [blame] | 57 | float v = 0; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 58 | int sel; |
| 59 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 60 | if (return_large == true && R(3) == 1) { |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 61 | sel = R(6); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 62 | } else { |
| 63 | sel = R(4); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 64 | } |
| 65 | |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 66 | if (return_undef == false && sel == 0) { |
| 67 | sel = 1; |
| 68 | } |
| 69 | |
| 70 | if (R(2) == 1) { |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 71 | v = (float)R(100); |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 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 | |
skia.committer@gmail.com | b3ec29d | 2013-09-07 07:01:16 +0000 | [diff] [blame] | 85 | if (R(4) == 1) { |
robertphillips@google.com | b706117 | 2013-09-06 14:16:12 +0000 | [diff] [blame] | 86 | v = -v; |
| 87 | } |
| 88 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 89 | return v; |
| 90 | } |
| 91 | |
| 92 | static SkColor make_color() { |
| 93 | if (R(2) == 1) return 0xFFC0F0A0; else return 0xFF000090; |
| 94 | } |
| 95 | |
| 96 | |
| 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) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 104 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 136 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 137 | |
| 138 | static void do_fuzz(SkCanvas* canvas) { |
| 139 | SkPath path; |
| 140 | SkPaint paint; |
| 141 | paint.setAntiAlias(true); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 142 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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: { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 155 | SkBlendMode mode; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 156 | switch (R(3)) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 157 | case 0: mode = SkBlendMode::kSrc; break; |
| 158 | case 1: mode = SkBlendMode::kXor; break; |
tomhudson@google.com | 83a4446 | 2011-10-27 15:27:51 +0000 | [diff] [blame] | 159 | case 2: |
| 160 | default: // silence warning |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 161 | mode = SkBlendMode::kSrcOver; break; |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 162 | } |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 163 | paint.setBlendMode(mode); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 171 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 178 | } |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 179 | break; |
| 180 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 181 | case 5: |
| 182 | paint.setStrokeWidth(make_number()); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 183 | break; |
| 184 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 185 | case 6: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 186 | paint.setStrokeMiter(make_number()); |
| 187 | break; |
| 188 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 189 | case 7: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 190 | if (quick == true) break; |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 191 | paint.setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle, make_number())); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 192 | break; |
| 193 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 194 | case 8: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 195 | if (quick == true) break; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 196 | //ctx.shadowColor = make_fill(); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 197 | break; |
| 198 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 199 | case 9: |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 284 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 293 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 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) { |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 341 | case 0: canvas->scale(-1000000000,1); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 342 | canvas->scale(-1000000000,1); |
| 343 | scval = 1; break; |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 344 | case 1: canvas->scale(-.000000001f,1); scval = 2; break; |
| 345 | case 2: canvas->scale(-.000000001f,1); scval = 0; break; |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | } |
| 349 | |
| 350 | break; |
| 351 | |
| 352 | |
| 353 | |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | } |
| 358 | |
| 359 | ////////////////////////////////////////////////////////////////////////////// |
| 360 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 361 | class FuzzView : public SampleView { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 362 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 363 | FuzzView() { |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 364 | this->setBGColor(0xFFDDDDDD); |
| 365 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 366 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 367 | protected: |
| 368 | // overrides from SkEventSink |
| 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 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 376 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 377 | void drawBG(SkCanvas* canvas) { |
| 378 | canvas->drawColor(0xFFDDDDDD); |
| 379 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 380 | |
mike@reedtribe.org | 5fd9243 | 2011-05-05 01:59:48 +0000 | [diff] [blame] | 381 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 382 | do_fuzz(canvas); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 383 | this->inval(nullptr); |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 384 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 385 | |
reed@android.com | 49f0ff2 | 2009-03-19 21:52:42 +0000 | [diff] [blame] | 386 | private: |
| 387 | typedef SkView INHERITED; |
| 388 | }; |
| 389 | |
| 390 | ////////////////////////////////////////////////////////////////////////////// |
| 391 | |
| 392 | static SkView* MyFactory() { return new FuzzView; } |
| 393 | static SkViewRegister reg(MyFactory); |