blob: 98fab259ecf15fa28dd102365a421eccba48e849 [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
Cary Clark91390c82018-03-09 14:02:46 -05008#ifndef FuzzCommon_DEFINED
9#define FuzzCommon_DEFINED
10
Kevin Lubick2541edf2018-01-11 10:27:14 -050011#include "Fuzz.h"
12#include "SkPath.h"
13#include "SkRegion.h"
14
15// We don't always want to test NaNs and infinities.
Cary Clark91390c82018-03-09 14:02:46 -050016static inline void fuzz_nice_float(Fuzz* fuzz, float* f) {
Kevin Lubick2541edf2018-01-11 10:27:14 -050017 float v;
18 fuzz->next(&v);
19 constexpr float kLimit = 1.0e35f; // FLT_MAX?
20 *f = (v == v && v <= kLimit && v >= -kLimit) ? v : 0.0f;
21}
22
23template <typename... Args>
24inline void fuzz_nice_float(Fuzz* fuzz, float* f, Args... rest) {
25 fuzz_nice_float(fuzz, f);
26 fuzz_nice_float(fuzz, rest...);
27}
28
Hal Canary13872dd2018-04-06 10:25:12 -040029template <typename T, typename Min, typename Max>
30inline void fuzz_enum_range(Fuzz* fuzz, T* value, Min rmin, Max rmax) {
31 using U = skstd::underlying_type_t<T>;
32 fuzz->nextRange((U*)value, (U)rmin, (U)rmax);
33}
34
35inline void fuzz_region(Fuzz* fuzz, SkRegion* region, int maxN) {
Kevin Lubick2541edf2018-01-11 10:27:14 -050036 uint8_t N;
Hal Canary13872dd2018-04-06 10:25:12 -040037 fuzz->nextRange(&N, 0, maxN);
Kevin Lubick2541edf2018-01-11 10:27:14 -050038 for (uint8_t i = 0; i < N; ++i) {
39 SkIRect r;
Hal Canary13872dd2018-04-06 10:25:12 -040040 SkRegion::Op op;
41 // Avoid the sentinal value used by Region.
42 fuzz->nextRange(&r.fLeft, -2147483646, 2147483646);
43 fuzz->nextRange(&r.fTop, -2147483646, 2147483646);
44 fuzz->nextRange(&r.fRight, -2147483646, 2147483646);
45 fuzz->nextRange(&r.fBottom, -2147483646, 2147483646);
Kevin Lubick2541edf2018-01-11 10:27:14 -050046 r.sort();
Hal Canary13872dd2018-04-06 10:25:12 -040047 fuzz_enum_range(fuzz, &op, (SkRegion::Op)0, SkRegion::kLastOp);
48 if (!region->op(r, op)) {
Kevin Lubick2541edf2018-01-11 10:27:14 -050049 return;
50 }
51 }
52}
Cary Clark91390c82018-03-09 14:02:46 -050053
Hal Canary13872dd2018-04-06 10:25:12 -040054template <>
55inline void Fuzz::next(SkRegion* region) { fuzz_region(this, region, 10); }
56
Cary Clark91390c82018-03-09 14:02:46 -050057// allows some float values for path points
58void FuzzPath(Fuzz* fuzz, SkPath* path, int maxOps);
59// allows all float values for path points
60void BuildPath(Fuzz* fuzz, SkPath* path, int last_verb);
61
62#endif
Kevin Lubickfe6b4892018-06-05 17:21:30 -040063