xref: /aosp_15_r20/external/skia/tests/PathOpsBuilderTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2014 Google Inc.
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 "include/core/SkPath.h"
9 #include "include/core/SkPathTypes.h"
10 #include "include/core/SkRect.h"
11 #include "include/pathops/SkPathOps.h"
12 #include "src/base/SkFloatBits.h"
13 #include "tests/PathOpsExtendedTest.h"
14 #include "tests/Test.h"
15 
DEF_TEST(PathOpsBuilder,reporter)16 DEF_TEST(PathOpsBuilder, reporter) {
17     SkOpBuilder builder;
18     SkPath result;
19     REPORTER_ASSERT(reporter, builder.resolve(&result));
20     REPORTER_ASSERT(reporter, result.isEmpty());
21 
22     builder.add(result, kDifference_SkPathOp);
23     REPORTER_ASSERT(reporter, builder.resolve(&result));
24     REPORTER_ASSERT(reporter, result.isEmpty());
25 
26     builder.add(result, kUnion_SkPathOp);
27     REPORTER_ASSERT(reporter, builder.resolve(&result));
28     REPORTER_ASSERT(reporter, result.isEmpty());
29 
30     SkPath rectPath;
31     rectPath.setFillType(SkPathFillType::kEvenOdd);
32     rectPath.addRect(0, 1, 2, 3, SkPathDirection::kCW);
33     builder.add(rectPath, kUnion_SkPathOp);
34     REPORTER_ASSERT(reporter, builder.resolve(&result));
35     bool closed;
36     SkPathDirection dir;
37     REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
38     REPORTER_ASSERT(reporter, closed);
39     REPORTER_ASSERT(reporter, dir == SkPathDirection::kCCW);
40     int pixelDiff = comparePaths(reporter, __FUNCTION__, rectPath, result);
41     REPORTER_ASSERT(reporter, pixelDiff == 0);
42 
43     rectPath.reset();
44     rectPath.setFillType(SkPathFillType::kEvenOdd);
45     rectPath.addRect(0, 1, 2, 3, SkPathDirection::kCCW);
46     builder.add(rectPath, kUnion_SkPathOp);
47     REPORTER_ASSERT(reporter, builder.resolve(&result));
48     REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
49     REPORTER_ASSERT(reporter, closed);
50     REPORTER_ASSERT(reporter, dir == SkPathDirection::kCCW);
51     REPORTER_ASSERT(reporter, rectPath == result);
52 
53     builder.add(rectPath, kDifference_SkPathOp);
54     REPORTER_ASSERT(reporter, builder.resolve(&result));
55     REPORTER_ASSERT(reporter, result.isEmpty());
56 
57     SkPath rect2, rect3;
58     rect2.addRect(2, 1, 4, 3, SkPathDirection::kCW);
59     rect3.addRect(4, 1, 5, 3, SkPathDirection::kCCW);
60     builder.add(rectPath, kUnion_SkPathOp);
61     builder.add(rect2, kUnion_SkPathOp);
62     builder.add(rect3, kUnion_SkPathOp);
63     REPORTER_ASSERT(reporter, builder.resolve(&result));
64     REPORTER_ASSERT(reporter, result.isRect(nullptr, &closed, &dir));
65     REPORTER_ASSERT(reporter, closed);
66     SkRect expected;
67     expected.setLTRB(0, 1, 5, 3);
68     REPORTER_ASSERT(reporter, result.getBounds() == expected);
69 
70     SkPath circle1, circle2, circle3;
71     circle1.addCircle(5, 6, 4, SkPathDirection::kCW);
72     circle2.addCircle(7, 4, 8, SkPathDirection::kCCW);
73     circle3.addCircle(6, 5, 6, SkPathDirection::kCW);
74     SkPath opCompare;
75     Op(circle1, circle2, kUnion_SkPathOp, &opCompare);
76     Op(opCompare, circle3, kDifference_SkPathOp, &opCompare);
77     builder.add(circle1, kUnion_SkPathOp);
78     builder.add(circle2, kUnion_SkPathOp);
79     builder.add(circle3, kDifference_SkPathOp);
80     REPORTER_ASSERT(reporter, builder.resolve(&result));
81     pixelDiff = comparePaths(reporter, __FUNCTION__, opCompare, result);
82     REPORTER_ASSERT(reporter, pixelDiff == 0);
83 }
84 
DEF_TEST(BuilderIssue3838,reporter)85 DEF_TEST(BuilderIssue3838, reporter) {
86     SkPath path;
87     path.moveTo(200, 170);
88     path.lineTo(220, 170);
89     path.lineTo(220, 230);
90     path.lineTo(240, 230);
91     path.lineTo(240, 210);
92     path.lineTo(180, 210);
93     path.lineTo(180, 190);
94     path.lineTo(260, 190);
95     path.lineTo(260, 250);
96     path.lineTo(200, 250);
97     path.lineTo(200, 170);
98     path.close();
99     SkPath path2;
100     SkOpBuilder builder;
101     builder.add(path, kUnion_SkPathOp);
102     builder.resolve(&path2);
103     int pixelDiff = comparePaths(reporter, __FUNCTION__, path, path2);
104     REPORTER_ASSERT(reporter, pixelDiff == 0);
105 }
106 
DEF_TEST(BuilderIssue3838_2,reporter)107 DEF_TEST(BuilderIssue3838_2, reporter) {
108     SkPath path;
109     path.addCircle(100, 100, 50);
110 
111     SkOpBuilder builder;
112     builder.add(path, kUnion_SkPathOp);
113     builder.add(path, kUnion_SkPathOp);
114 
115     SkPath result;
116     builder.resolve(&result);
117     int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
118     REPORTER_ASSERT(reporter, pixelDiff == 0);
119 }
120 
DEF_TEST(BuilderIssue3838_3,reporter)121 DEF_TEST(BuilderIssue3838_3, reporter) {
122     SkPath path;
123     path.moveTo(40, 10);
124     path.lineTo(60, 10);
125     path.lineTo(60, 30);
126     path.lineTo(40, 30);
127     path.lineTo(40, 10);
128     path.moveTo(41, 11);
129     path.lineTo(41, 29);
130     path.lineTo(59, 29);
131     path.lineTo(59, 11);
132     path.lineTo(41, 11);
133 
134     SkOpBuilder builder;
135     builder.add(path, kUnion_SkPathOp);
136     SkPath result;
137     builder.resolve(&result);
138     int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
139     REPORTER_ASSERT(reporter, pixelDiff == 0);
140 }
141 
DEF_TEST(BuilderIssue502792_2,reporter)142 DEF_TEST(BuilderIssue502792_2, reporter) {
143     SkPath path, pathB;
144     path.setFillType(SkPathFillType::kWinding);
145     path.addRect(0, 0, 1, 1, SkPathDirection::kCW);
146     path.addRect(2, 2, 3, 3, SkPathDirection::kCW);
147     pathB.setFillType(SkPathFillType::kEvenOdd);
148     pathB.addRect(3, 3, 4, 4, SkPathDirection::kCW);
149     pathB.addRect(3, 3, 4, 4, SkPathDirection::kCW);
150     SkOpBuilder builder;
151     builder.add(path, kUnion_SkPathOp);
152     builder.add(pathB, kDifference_SkPathOp);
153     SkPath result;
154     builder.resolve(&result);
155 }
156 
DEF_TEST(Fuzz846,reporter)157 DEF_TEST(Fuzz846, reporter) {
158 /*
159 <clipPath id="clip-circle">
160     <circle id="circle" cx="60" cy="60" r="50" />
161 </clipPath>
162 <clipPath id="clip-rect">
163     <clipPath id="clip-rect">
164         <clipPath id="clip-rect">
165             <clipPath id="clip-rect">
166                 <rect x="10" y="30" width="0" height="60" />
167                 <rect x="10" y="30" width="0" height="60" />
168                 <rect x="10" y="30" width="100" height="60" />
169                 <rect x="10" y="30" width="32668" />
170                 <rect x="10" y="30" width="100" height="18446744073709551615" />
171                 <rect x="10" y="255" width="100" height="60" />
172                 <rect width="100" height="60" />
173                 <rect x="10" y="30" width="100" height="60" />
174                 <rect x="10" y="30" width="100" height="4294967236" />
175                 <rect x="10" y="30" width="100" height="60" />
176             </clipPath>
177             <rect x="10" y="30" width="0" height="60" />
178             <rect x="10" y="30" width="0" height="0.18093252719929986369568203" />
179             <rect x="10" y="30" width="100" height="60" />
180             <rect x="10" y="30" width="32668" height="60" />
181             <rect x="10" y="30" width="100" height="18446744073709551615" />
182             <rect x="10" y="255" width="100" height="60" />
183             <rect x="2147483649" y="30" width="100" height="60" />
184             <rect x="10" y="30" width="100" height="60" />
185             <rect x="10" y="30" width="100" height="60" />
186             <rect x="10" y="30" width="100" height="60" />
187         </clipPath>
188         <rect x="10" y="30" width="0" height="60" />
189         <rect x="10" y="30" width="0" height="60" />
190         <rect x="10" y="30" width="100" height="60" />
191         <rect x="10" y="30" width="32668" height="60" />
192         <rect x="10" y="30" width="100" height="18446744073709551615" />
193         <rect x="10" y="255" width="100" height="60" />
194         <rect x="2147483649" y="30" width="100" height="60" />
195         <rect x="10" y="30" width="100" height="60" />
196         <rect x="10" y="2879753595" width="100" height="60" />
197         <rect x="10" y="30" width="100" height="60" />
198     </clipPath>
199     <rect x="10" y="30" width="100" height="60" />
200     <rect x="10" y="30" width="0" height="60" />
201     <rect x="10" y="30" width="100" height="60" />
202     <rect x="10" y="30" width="32668" height="60" />
203     <rect x="10" y="30" width="100" height="18446744073709551615" />
204     <rect x="10" y="255" width="100" height="60" />
205     <rect x="2147483649" y="30" width="100" height="60" />
206     <rect x="10" y="30" width="100" height="60" />
207     <rect x="10" y="30" width="100" height="4294967236" />
208     <rect x="10" y="30" width="100" height="4294967236" />
209     <rect x="10" y="30" width="100" height="4294967236" />
210     <rect x="10" y="30" width="100" height="4294967236" />
211     <rect x="10" y="30" width="100" height="60" />
212     <rect x="757798030" y="30" width="100" height="60" />
213 */
214     SkPath clipCircle, clipRect;
215     SkPath inner;
216     clipCircle.addCircle(60, 60, 50);             // <circle id="circle" cx="60" cy="60" r="50" />
217 
218     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
219     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
220     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
221     inner.addRect(10, 30, 10+32668, 30+0);        // <rect x="10" y="30" width="32668" />
222     inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
223     inner.addRect(10, 255, 10+100, 255+60);       // <rect x="10" y="255" width="100" height="60" />
224     inner.addRect(0, 0, 0+100, 0+60);             //  <rect width="100" height="60" />
225     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
226     inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
227     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
228     clipRect.addPath(inner);
229     inner.reset();
230     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
231     inner.addRect(10, 30, 10+0, 30+0.18093252719929986369568203f); // <rect x="10" y="30" width="0" height="0.18093252719929986369568203" />
232     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
233     inner.addRect(10, 30, 10+32668, 30+60);       // <rect x="10" y="30" width="32668" height="60" />
234     inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
235     inner.addRect(10, 255, 10+100, 255+60);       // <rect x="10" y="255" width="100" height="60" />
236     inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
237     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
238     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
239     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
240     clipRect.addPath(inner);
241     inner.reset();
242     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
243     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
244     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
245     inner.addRect(10, 30, 10+32668, 30+60);       // <rect x="10" y="30" width="32668" height="60" />
246     inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
247     inner.addRect(10, 255, 10+100, 255+60);       // <rect x="10" y="255" width="100" height="60" />
248     inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
249     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
250     inner.addRect(10, 2879753595.f, 10+100, 30+2879753595.f); // <rect x="10" y="2879753595" width="100" height="60" />
251     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
252     clipRect.addPath(inner);
253     inner.reset();
254     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
255     inner.addRect(10, 30, 10+0, 30+60);           // <rect x="10" y="30" width="0" height="60" />
256     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
257     inner.addRect(10, 30, 10+32668, 30+60);       // <rect x="10" y="30" width="32668" height="60" />
258     inner.addRect(10, 30, 10+100, 30+18446744073709551615.f); // <rect x="10" y="30" width="100" height="18446744073709551615" />
259     inner.addRect(10, 255, 10+100, 255+60);       // <rect x="10" y="255" width="100" height="60" />
260     inner.addRect(2147483649.f, 30, 2147483649.f+100, 30+60); // <rect x="2147483649" y="30" width="100" height="60" />
261     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
262     inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
263     inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
264     inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
265     inner.addRect(10, 30, 10+100, 30+4294967236.f); // <rect x="10" y="30" width="100" height="4294967236" />
266     inner.addRect(10, 30, 10+100, 30+60);         // <rect x="10" y="30" width="100" height="60" />
267     inner.addRect(757798030.f, 30, 757798030.f+100, 30+60); // <rect x="757798030" y="30" width="100" height="60" />
268     clipRect.addPath(inner);
269 
270     SkOpBuilder builder;
271     builder.add(clipCircle, kUnion_SkPathOp);
272     builder.add(clipRect, kDifference_SkPathOp);
273     SkPath result;
274     builder.resolve(&result);
275 }
276 
DEF_TEST(Issue569540,reporter)277 DEF_TEST(Issue569540, reporter) {
278     SkPath path1;
279     path1.moveTo(5, -225);
280     path1.lineTo(-225, 7425);
281     path1.lineTo(7425, 7425);
282     path1.lineTo(7425, -225);
283     path1.lineTo(-225, -225);
284     path1.lineTo(5, -225);
285     path1.close();
286 
287     SkPath path2;
288     path2.moveTo(5940, 2790);
289     path2.lineTo(5940, 2160);
290     path2.lineTo(5970, 1980);
291     path2.lineTo(5688, 773669888);
292     path2.lineTo(5688, 2160);
293     path2.lineTo(5688, 2430);
294     path2.lineTo(5400, 4590);
295     path2.lineTo(5220, 4590);
296     path2.lineTo(5220, 4920);
297     path2.cubicTo(5182.22900390625f, 4948.328125f, 5160, 4992.78662109375f, 5160, 5040.00048828125f);
298     path2.lineTo(5940, 2790);
299     path2.close();
300 
301     SkOpBuilder builder;
302     builder.add(path1, kUnion_SkPathOp);
303     builder.add(path2, kUnion_SkPathOp);
304     SkPath result;
305     builder.resolve(&result);
306 }
307 
DEF_TEST(SkOpBuilderFuzz665,reporter)308 DEF_TEST(SkOpBuilderFuzz665, reporter) {
309     SkPath path;
310     path.setFillType(SkPathFillType::kEvenOdd);
311 path.moveTo(SkBits2Float(0xcc4264a7), SkBits2Float(0x4bb12e50));  // -5.0959e+07f, 2.32235e+07f
312 path.lineTo(SkBits2Float(0xcc4264b0), SkBits2Float(0x4bb12e48));  // -5.0959e+07f, 2.32234e+07f
313 path.lineTo(SkBits2Float(0xcc4264a7), SkBits2Float(0x4bb12e50));  // -5.0959e+07f, 2.32235e+07f
314 path.close();
315     SkPath path1(path);
316     path.reset();
317     path.setFillType(SkPathFillType::kWinding);
318 path.moveTo(SkBits2Float(0x43213333), SkBits2Float(0x43080000));  // 161.2f, 136
319 path.lineTo(SkBits2Float(0x43038000), SkBits2Float(0x43080000));  // 131.5f, 136
320 path.cubicTo(SkBits2Float(0x43038000), SkBits2Float(0x42f00000), SkBits2Float(0x42f16666), SkBits2Float(0x42d53333), SkBits2Float(0x42d3cccd), SkBits2Float(0x42cd6666));  // 131.5f, 120, 120.7f, 106.6f, 105.9f, 102.7f
321 path.lineTo(SkBits2Float(0x42e33333), SkBits2Float(0x42940000));  // 113.6f, 74
322     SkPath path2(path);
323     SkOpBuilder builder;
324     builder.add(path1, kUnion_SkPathOp);
325     builder.add(path2, kUnion_SkPathOp);
326     SkPath result;
327     builder.resolve(&result);
328 }
329 
DEF_TEST(SkOpBuilder618991,reporter)330 DEF_TEST(SkOpBuilder618991, reporter) {
331     SkPath path0;
332     path0.moveTo(140, 40);
333     path0.lineTo(200, 210);
334     path0.lineTo(40, 100);
335     path0.lineTo(2.22223e+07f, 2.22222e+14f);
336     path0.lineTo(2.22223e+07f, 2.22222e+14f);
337 
338     SkPath path1;
339     path1.moveTo(160, 60);
340     path1.lineTo(220, 230);
341     path1.lineTo(60, 120);
342     path1.lineTo(2.22223e+07f, 2.22222e+14f);
343     path1.lineTo(2.22223e+07f, 2.22222e+14f);
344 
345     SkOpBuilder builder;
346     builder.add(path0, SkPathOp::kUnion_SkPathOp);
347     builder.add(path1, SkPathOp::kUnion_SkPathOp);
348     builder.resolve(&path0);
349 }
350 
DEF_TEST(SkOpBuilderKFuzz1,reporter)351 DEF_TEST(SkOpBuilderKFuzz1, reporter) {
352     SkPath path;
353 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000));  // 0, 0
354 path.lineTo(SkBits2Float(0x39008001), SkBits2Float(0xd31fbc1d));  // 0.000122547f, -6.86056e+11f
355 path.conicTo(SkBits2Float(0x246a205a), SkBits2Float(0x0080d3fb), SkBits2Float(0xce000001), SkBits2Float(0x04d31fbc), SkBits2Float(0x57a82c00));  // 5.07681e-17f, 1.1831e-38f, -5.36871e+08f, 4.9635e-36f, 3.69814e+14f
356     SkPath path0(path);
357     path.reset();
358 path.moveTo(SkBits2Float(0x00000000), SkBits2Float(0x00000000));  // 0, 0
359 path.cubicTo(SkBits2Float(0x80d3f924), SkBits2Float(0xcecece4f), SkBits2Float(0xcececece), SkBits2Float(0xcececece), SkBits2Float(0x9a9a9ace), SkBits2Float(0x9a9a9a9a));  // -1.94667e-38f, -1.73481e+09f, -1.73483e+09f, -1.73483e+09f, -6.3943e-23f, -6.39427e-23f
360 path.moveTo(SkBits2Float(0x9a9a019a), SkBits2Float(0xa59a9a9a));  // -6.36955e-23f, -2.68195e-16f
361     SkPath path1(path);
362 SkOpBuilder builder;
363     builder.add(path0, SkPathOp::kUnion_SkPathOp);
364     builder.add(path1, SkPathOp::kUnion_SkPathOp);
365     builder.resolve(&path);
366 }
367