Debian Codegraph @

fuzz/FuzzPolyUtils.cpp

raw
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 void inline ignoreResult(bool ) {}
19
20 // clamps the point to the nearest 16th of a pixel
21 static SkPoint sanitize_point(const SkPoint& in) {
22 SkPoint out;
23 out.fX = SkScalarRoundToScalar(16.f*in.fX)*0.0625f;
24 out.fY = SkScalarRoundToScalar(16.f*in.fY)*0.0625f;
25 return out;
26 }
27
28 DEF_FUZZ(PolyUtils, fuzz) {
29 int count;
30 fuzz->nextRange(&count, 0, 512);
31 AutoSTMalloc<64, SkPoint> polygon(count);
32 for (int index = 0; index < count; ++index) {
33 fuzz->next(&polygon[index].fX, &polygon[index].fY);
34 polygon[index] = sanitize_point(polygon[index]);
35 }
36 const auto bounds = SkRect::BoundsOrEmpty({polygon.data(), (size_t)count});
37
38 ignoreResult(SkGetPolygonWinding(polygon, count));
39 bool isConvex = SkIsConvexPolygon(polygon, count);
40 bool isSimple = SkIsSimplePolygon(polygon, count);
41
42 SkTDArray<SkPoint> output;
43 if (isConvex) {
44 SkScalar inset;
45 fuzz->next(&inset);
46 ignoreResult(SkInsetConvexPolygon(polygon, count, inset, &output));
47 }
48
49 if (isSimple) {
50 SkScalar offset;
51 // Limit this to prevent timeouts.
52 // This should be fine, as this is roughly the range we expect from the shadow algorithm.
53 fuzz->nextRange(&offset, -1000, 1000);
54 ignoreResult(SkOffsetSimplePolygon(polygon, count, bounds, offset, &output));
55
56 AutoSTMalloc<64, uint16_t> indexMap(count);
57 for (int index = 0; index < count; ++index) {
58 fuzz->next(&indexMap[index]);
59 }
60 SkTDArray<uint16_t> outputIndices;
61 ignoreResult(SkTriangulateSimplePolygon(polygon, indexMap, count, &outputIndices));
62 }
63 }
64 #else
65 DEF_FUZZ(PolyUtils, fuzz) {}
66 #endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)