/src/skia/fuzz/FuzzPolyUtils.cpp
Line | Count | Source |
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/Fuzz.h" |
9 | | |
10 | | #include "include/core/SkPoint.h" |
11 | | #include "include/private/base/SkTDArray.h" |
12 | | #include "include/private/base/SkTemplates.h" |
13 | | #include "src/utils/SkPolyUtils.h" |
14 | | |
15 | | using namespace skia_private; |
16 | | |
17 | | #if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
18 | 7.11k | void inline ignoreResult(bool ) {} |
19 | | |
20 | | // clamps the point to the nearest 16th of a pixel |
21 | 99.2k | static SkPoint sanitize_point(const SkPoint& in) { |
22 | 99.2k | SkPoint out; |
23 | 99.2k | out.fX = SkScalarRoundToScalar(16.f*in.fX)*0.0625f; |
24 | 99.2k | out.fY = SkScalarRoundToScalar(16.f*in.fY)*0.0625f; |
25 | 99.2k | return out; |
26 | 99.2k | } |
27 | | |
28 | 993 | DEF_FUZZ(PolyUtils, fuzz) { |
29 | 993 | int count; |
30 | 993 | fuzz->nextRange(&count, 0, 512); |
31 | 993 | AutoSTMalloc<64, SkPoint> polygon(count); |
32 | 100k | for (int index = 0; index < count; ++index) { |
33 | 99.2k | fuzz->next(&polygon[index].fX, &polygon[index].fY); |
34 | 99.2k | polygon[index] = sanitize_point(polygon[index]); |
35 | 99.2k | } |
36 | 993 | SkRect bounds; |
37 | 993 | bounds.setBoundsCheck(polygon, count); |
38 | | |
39 | 993 | ignoreResult(SkGetPolygonWinding(polygon, count)); |
40 | 993 | bool isConvex = SkIsConvexPolygon(polygon, count); |
41 | 993 | bool isSimple = SkIsSimplePolygon(polygon, count); |
42 | | |
43 | 993 | SkTDArray<SkPoint> output; |
44 | 993 | if (isConvex) { |
45 | 514 | SkScalar inset; |
46 | 514 | fuzz->next(&inset); |
47 | 514 | ignoreResult(SkInsetConvexPolygon(polygon, count, inset, &output)); |
48 | 514 | } |
49 | | |
50 | 993 | if (isSimple) { |
51 | 865 | SkScalar offset; |
52 | | // Limit this to prevent timeouts. |
53 | | // This should be fine, as this is roughly the range we expect from the shadow algorithm. |
54 | 865 | fuzz->nextRange(&offset, -1000, 1000); |
55 | 865 | ignoreResult(SkOffsetSimplePolygon(polygon, count, bounds, offset, &output)); |
56 | | |
57 | 865 | AutoSTMalloc<64, uint16_t> indexMap(count); |
58 | 85.2k | for (int index = 0; index < count; ++index) { |
59 | 84.4k | fuzz->next(&indexMap[index]); |
60 | 84.4k | } |
61 | 865 | SkTDArray<uint16_t> outputIndices; |
62 | 865 | ignoreResult(SkTriangulateSimplePolygon(polygon, indexMap, count, &outputIndices)); |
63 | 865 | } |
64 | 993 | } |
65 | | #else |
66 | | DEF_FUZZ(PolyUtils, fuzz) {} |
67 | | #endif // !defined(SK_ENABLE_OPTIMIZE_SIZE) |