blob: c908606be1945b06916e44c9a6d5200890d4bb81 [file] [log] [blame]
Kevin Lubick2541edf2018-01-11 10:27:14 -05001/*
Kevin Lubickdb1e5c62018-02-27 08:30:43 -05002 * Copyright 2018 Google, LLC
Kevin Lubick2541edf2018-01-11 10:27:14 -05003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "Fuzz.h"
9#include "SkPath.h"
10#include "SkRegion.h"
11
12// We don't always want to test NaNs and infinities.
13static void fuzz_nice_float(Fuzz* fuzz, float* f) {
14 float v;
15 fuzz->next(&v);
16 constexpr float kLimit = 1.0e35f; // FLT_MAX?
17 *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
18}
19
20template <typename... Args>
21inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
22 fuzz_nice_float(fuzz, f);
23 fuzz_nice_float(fuzz, rest...);
24}
25
26static void fuzz_path(Fuzz* fuzz, SkPath* path, int maxOps) {
27 if (maxOps < 2) {
28 maxOps = 2;
29 }
30 uint8_t fillType;
31 fuzz->nextRange(&fillType, 0, (uint8_t)SkPath::kInverseEvenOdd_FillType);
32 path->setFillType((SkPath::FillType)fillType);
33 uint8_t numOps;
34 fuzz->nextRange(&numOps, 2, maxOps);
35 for (uint8_t i = 0; i < numOps; ++i) {
36 uint8_t op;
37 fuzz->nextRange(&op, 0, 6);
38 SkScalar a, b, c, d, e, f;
39 switch (op) {
40 case 0:
41 fuzz_nice_float(fuzz, &a, &b);
42 path->moveTo(a, b);
43 break;
44 case 1:
45 fuzz_nice_float(fuzz, &a, &b);
46 path->lineTo(a, b);
47 break;
48 case 2:
49 fuzz_nice_float(fuzz, &a, &b, &c, &d);
50 path->quadTo(a, b, c, d);
51 break;
52 case 3:
53 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
54 path->conicTo(a, b, c, d, e);
55 break;
56 case 4:
57 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e, &f);
58 path->cubicTo(a, b, c, d, e, f);
59 break;
60 case 5:
61 fuzz_nice_float(fuzz, &a, &b, &c, &d, &e);
62 path->arcTo(a, b, c, d, e);
63 break;
64 case 6:
65 path->close();
66 break;
67 default:
Kevin Lubick54f20e02018-01-11 14:50:21 -050068 SkASSERT(false);
Kevin Lubick2541edf2018-01-11 10:27:14 -050069 break;
70 }
71 }
72}
73
74template <>
75inline void Fuzz::next(SkRegion* region) {
76 uint8_t N;
77 this->nextRange(&N, 0, 10);
78 for (uint8_t i = 0; i < N; ++i) {
79 SkIRect r;
80 uint8_t op;
81 this->next(&r);
82 r.sort();
83 this->nextRange(&op, 0, (uint8_t)SkRegion::kLastOp);
84 if (!region->op(r, (SkRegion::Op)op)) {
85 return;
86 }
87 }
88}