Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "fuzz/Fuzz.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 9 | |
Kevin Lubick | 62f57a0 | 2022-04-28 11:04:30 -0400 | [diff] [blame] | 10 | #include "include/core/SkPoint.h" |
Kevin Lubick | dc6cc02 | 2023-01-13 11:24:27 -0500 | [diff] [blame] | 11 | #include "include/private/base/SkTDArray.h" |
Kevin Lubick | 46572b4 | 2023-01-18 13:11:06 -0500 | [diff] [blame] | 12 | #include "include/private/base/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/utils/SkPolyUtils.h" |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 14 | |
Herb Derby | 916dcef | 2023-01-09 16:12:26 -0500 | [diff] [blame] | 15 | using namespace skia_private; |
| 16 | |
Jim Van Verth | fee3dbe | 2022-10-26 15:45:48 -0400 | [diff] [blame] | 17 | #if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 18 | void inline ignoreResult(bool ) {} |
| 19 | |
Jim Van Verth | 2e6181c | 2022-01-10 11:56:17 -0500 | [diff] [blame] | 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 | |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 28 | DEF_FUZZ(PolyUtils, fuzz) { |
| 29 | int count; |
| 30 | fuzz->nextRange(&count, 0, 512); |
Herb Derby | 916dcef | 2023-01-09 16:12:26 -0500 | [diff] [blame] | 31 | AutoSTMalloc<64, SkPoint> polygon(count); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 32 | for (int index = 0; index < count; ++index) { |
| 33 | fuzz->next(&polygon[index].fX, &polygon[index].fY); |
Jim Van Verth | 2e6181c | 2022-01-10 11:56:17 -0500 | [diff] [blame] | 34 | polygon[index] = sanitize_point(polygon[index]); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 35 | } |
Jim Van Verth | a5ef397 | 2019-05-01 13:28:07 -0400 | [diff] [blame] | 36 | SkRect bounds; |
| 37 | bounds.setBoundsCheck(polygon, count); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 38 | |
| 39 | ignoreResult(SkGetPolygonWinding(polygon, count)); |
Jim Van Verth | 1f0e64a | 2022-01-13 15:29:24 -0500 | [diff] [blame] | 40 | bool isConvex = SkIsConvexPolygon(polygon, count); |
| 41 | bool isSimple = SkIsSimplePolygon(polygon, count); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 42 | |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 43 | SkTDArray<SkPoint> output; |
Jim Van Verth | 1f0e64a | 2022-01-13 15:29:24 -0500 | [diff] [blame] | 44 | if (isConvex) { |
| 45 | SkScalar inset; |
| 46 | fuzz->next(&inset); |
| 47 | ignoreResult(SkInsetConvexPolygon(polygon, count, inset, &output)); |
Jim Van Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 48 | } |
Jim Van Verth | 1f0e64a | 2022-01-13 15:29:24 -0500 | [diff] [blame] | 49 | |
| 50 | if (isSimple) { |
| 51 | SkScalar offset; |
Jim Van Verth | 62bb436 | 2022-01-20 15:25:14 -0500 | [diff] [blame] | 52 | // 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 Verth | 1f0e64a | 2022-01-13 15:29:24 -0500 | [diff] [blame] | 55 | ignoreResult(SkOffsetSimplePolygon(polygon, count, bounds, offset, &output)); |
| 56 | |
Herb Derby | 916dcef | 2023-01-09 16:12:26 -0500 | [diff] [blame] | 57 | AutoSTMalloc<64, uint16_t> indexMap(count); |
Jim Van Verth | 1f0e64a | 2022-01-13 15:29:24 -0500 | [diff] [blame] | 58 | 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 Verth | 061cc21 | 2018-07-11 14:09:09 -0400 | [diff] [blame] | 64 | } |
Jim Van Verth | fee3dbe | 2022-10-26 15:45:48 -0400 | [diff] [blame] | 65 | #else |
| 66 | DEF_FUZZ(PolyUtils, fuzz) {} |
| 67 | #endif // !defined(SK_ENABLE_OPTIMIZE_SIZE) |