blob: 00d35993234ebd34479c8546cc0668cb2f76cfb9 [file] [log] [blame]
Jim Van Verth061cc212018-07-11 14:09:09 -04001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "fuzz/Fuzz.h"
Michael Ludwig8ee6cf32019-08-02 09:57:04 -04009
Kevin Lubick62f57a02022-04-28 11:04:30 -040010#include "include/core/SkPoint.h"
Kevin Lubickdc6cc022023-01-13 11:24:27 -050011#include "include/private/base/SkTDArray.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050012#include "include/private/base/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/utils/SkPolyUtils.h"
Jim Van Verth061cc212018-07-11 14:09:09 -040014
Herb Derby916dcef2023-01-09 16:12:26 -050015using namespace skia_private;
16
Jim Van Verthfee3dbe2022-10-26 15:45:48 -040017#if !defined(SK_ENABLE_OPTIMIZE_SIZE)
Jim Van Verth061cc212018-07-11 14:09:09 -040018void inline ignoreResult(bool ) {}
19
Jim Van Verth2e6181c2022-01-10 11:56:17 -050020// clamps the point to the nearest 16th of a pixel
21static 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
Jim Van Verth061cc212018-07-11 14:09:09 -040028DEF_FUZZ(PolyUtils, fuzz) {
29 int count;
30 fuzz->nextRange(&count, 0, 512);
Herb Derby916dcef2023-01-09 16:12:26 -050031 AutoSTMalloc<64, SkPoint> polygon(count);
Jim Van Verth061cc212018-07-11 14:09:09 -040032 for (int index = 0; index < count; ++index) {
33 fuzz->next(&polygon[index].fX, &polygon[index].fY);
Jim Van Verth2e6181c2022-01-10 11:56:17 -050034 polygon[index] = sanitize_point(polygon[index]);
Jim Van Verth061cc212018-07-11 14:09:09 -040035 }
Jim Van Vertha5ef3972019-05-01 13:28:07 -040036 SkRect bounds;
37 bounds.setBoundsCheck(polygon, count);
Jim Van Verth061cc212018-07-11 14:09:09 -040038
39 ignoreResult(SkGetPolygonWinding(polygon, count));
Jim Van Verth1f0e64a2022-01-13 15:29:24 -050040 bool isConvex = SkIsConvexPolygon(polygon, count);
41 bool isSimple = SkIsSimplePolygon(polygon, count);
Jim Van Verth061cc212018-07-11 14:09:09 -040042
Jim Van Verth061cc212018-07-11 14:09:09 -040043 SkTDArray<SkPoint> output;
Jim Van Verth1f0e64a2022-01-13 15:29:24 -050044 if (isConvex) {
45 SkScalar inset;
46 fuzz->next(&inset);
47 ignoreResult(SkInsetConvexPolygon(polygon, count, inset, &output));
Jim Van Verth061cc212018-07-11 14:09:09 -040048 }
Jim Van Verth1f0e64a2022-01-13 15:29:24 -050049
50 if (isSimple) {
51 SkScalar offset;
Jim Van Verth62bb4362022-01-20 15:25:14 -050052 // Limit this to prevent timeouts.
53 // This should be fine, as this is roughly the range we expect from the shadow algorithm.
54 fuzz->nextRange(&offset, -1000, 1000);
Jim Van Verth1f0e64a2022-01-13 15:29:24 -050055 ignoreResult(SkOffsetSimplePolygon(polygon, count, bounds, offset, &output));
56
Herb Derby916dcef2023-01-09 16:12:26 -050057 AutoSTMalloc<64, uint16_t> indexMap(count);
Jim Van Verth1f0e64a2022-01-13 15:29:24 -050058 for (int index = 0; index < count; ++index) {
59 fuzz->next(&indexMap[index]);
60 }
61 SkTDArray<uint16_t> outputIndices;
62 ignoreResult(SkTriangulateSimplePolygon(polygon, indexMap, count, &outputIndices));
63 }
Jim Van Verth061cc212018-07-11 14:09:09 -040064}
Jim Van Verthfee3dbe2022-10-26 15:45:48 -040065#else
66DEF_FUZZ(PolyUtils, fuzz) {}
67#endif // !defined(SK_ENABLE_OPTIMIZE_SIZE)