1 /*
2  * Copyright 2018 Google, LLC
3  *
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.
fuzz_nice_float(Fuzz * fuzz,float * f)13 static 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 
20 template <typename... Args>
fuzz_nice_float(Fuzz * fuzz,float * f,Args...rest)21 inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
22     fuzz_nice_float(fuzz, f);
23     fuzz_nice_float(fuzz, rest...);
24 }
25 
fuzz_path(Fuzz * fuzz,SkPath * path,int maxOps)26 static 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:
68                 SkASSERT(false);
69                 break;
70         }
71     }
72 }
73 
74 template <>
next(SkRegion * region)75 inline 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 }
89