blob: 20b12f3f4c67f58fdd63665c60637dccfe374976 [file] [log] [blame]
senorblancod6ed19c2015-02-26 06:58:17 -08001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkPaint.h"
Mike Reed093de4e2020-08-03 16:33:14 -040011#include "include/core/SkPathBuilder.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkScalar.h"
senorblancod6ed19c2015-02-26 06:58:17 -080013
senorblancod6ed19c2015-02-26 06:58:17 -080014namespace {
15// Concave test
16void test_concave(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080017 canvas->translate(0, 0);
Mike Reed093de4e2020-08-03 16:33:14 -040018 canvas->drawPath(SkPath::Polygon({{20,20}, {80,20}, {30,30}, {20,80}}, false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -080019}
20
21// Reverse concave test
22void test_reverse_concave(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080023 canvas->save();
24 canvas->translate(100, 0);
Mike Reed093de4e2020-08-03 16:33:14 -040025 canvas->drawPath(SkPath::Polygon({{20,20}, {20,80}, {30,30}, {80,20}}, false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -080026 canvas->restore();
27}
28
29// Bowtie (intersection)
30void test_bowtie(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080031 canvas->save();
32 canvas->translate(200, 0);
Mike Reed093de4e2020-08-03 16:33:14 -040033 canvas->drawPath(SkPath::Polygon({{20,20}, {80,80}, {80,20}, {20,80}}, false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -080034 canvas->restore();
35}
36
37// "fake" bowtie (concave, but no intersection)
38void test_fake_bowtie(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080039 canvas->save();
40 canvas->translate(300, 0);
Mike Reed093de4e2020-08-03 16:33:14 -040041 canvas->drawPath(SkPath::Polygon({{20,20}, {50,40}, {80,20}, {80,80}, {50,60}, {20,80}},
42 false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -080043 canvas->restore();
44}
45
Stephen White5926f2d2017-02-13 13:55:42 -050046// Bowtie with a smaller right hand lobe. The outer vertex of the left hand
47// lobe intrudes into the interior of the right hand lobe.
48void test_intruding_vertex(SkCanvas* canvas, const SkPaint& paint) {
Stephen White5926f2d2017-02-13 13:55:42 -050049 canvas->save();
50 canvas->translate(400, 0);
Mike Reed093de4e2020-08-03 16:33:14 -040051 canvas->drawPath(SkPath::Polygon({{20,20}, {50,50}, {68,20}, {68,80}, {50,50}, {20,80}},
52 false, SkPathFillType::kWinding, true), paint);
Stephen White5926f2d2017-02-13 13:55:42 -050053 canvas->restore();
54}
55
56// A shape with an edge that becomes inverted on AA stroking and that also contains
57// a repeated start/end vertex.
58void test_inversion_repeat_vertex(SkCanvas* canvas, const SkPaint& paint) {
Stephen White5926f2d2017-02-13 13:55:42 -050059 canvas->save();
60 canvas->translate(400, 100);
Mike Reed093de4e2020-08-03 16:33:14 -040061 const SkPoint pts[] = {
62 {80,50}, {40,80}, {60,20}, {20,20}, {39.99f,80}, {80,50},
63 };
Herb Derbyc37b3862022-06-21 09:49:17 -040064 canvas->drawPath(SkPath::Polygon(pts, std::size(pts), false,
Mike Reed093de4e2020-08-03 16:33:14 -040065 SkPathFillType::kWinding, true), paint);
Stephen White5926f2d2017-02-13 13:55:42 -050066 canvas->restore();
67}
68
senorblancod6ed19c2015-02-26 06:58:17 -080069// Fish test (intersection/concave)
70void test_fish(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080071 canvas->save();
72 canvas->translate(0, 100);
Mike Reed093de4e2020-08-03 16:33:14 -040073 canvas->drawPath(SkPath::Polygon({{20,20}, {80,80}, {70,50}, {80,20}, {20,80}, {0,50}}, false,
74 SkPathFillType::kWinding, true), paint);
senorblancod6ed19c2015-02-26 06:58:17 -080075 canvas->restore();
76}
77
Stephen White2f4686f2017-01-03 16:20:01 -050078// Overlapping "Fast-forward" icon: tests coincidence of inner and outer
79// vertices generated by intersection.
80void test_fast_forward(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080081 canvas->save();
82 canvas->translate(100, 100);
Mike Reed093de4e2020-08-03 16:33:14 -040083 auto path = SkPathBuilder().addPolygon({{20,20}, {60,50}, {20,80}}, false)
84 .addPolygon({{40,20}, {40,80}, {80,50}}, false)
85 .detach();
senorblancod6ed19c2015-02-26 06:58:17 -080086 canvas->drawPath(path, paint);
87 canvas->restore();
88}
89
90// Square polygon with a square hole.
91void test_hole(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -080092 canvas->save();
93 canvas->translate(200, 100);
Mike Reed093de4e2020-08-03 16:33:14 -040094 auto path = SkPathBuilder().addPolygon({{20,20}, {80,20}, {80,80}, {20,80}}, false)
95 .addPolygon({{30,30}, {30,70}, {70,70}, {70,30}}, false)
96 .detach();
senorblancod6ed19c2015-02-26 06:58:17 -080097 canvas->drawPath(path, paint);
98 canvas->restore();
99}
100
101// Star test (self-intersecting)
102void test_star(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -0800103 canvas->save();
104 canvas->translate(300, 100);
Mike Reed58cc97a2020-08-24 15:15:01 -0400105 canvas->drawPath(SkPath::Polygon({{30,20}, {50,80}, {70,20}, {20,57}, {80,57}}, false),
Mike Reedb6317422018-08-15 10:23:39 -0400106 paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800107 canvas->restore();
108}
109
Stephen White3b5a3fa2017-06-06 14:51:19 -0400110// Exercise a case where the intersection is below a bottom edge.
111void test_twist(SkCanvas* canvas, const SkPaint& paint) {
Stephen White3b5a3fa2017-06-06 14:51:19 -0400112 canvas->save();
Stephen White3b5a3fa2017-06-06 14:51:19 -0400113 canvas->translate(420, 220);
114 canvas->scale(10, 10);
Mike Reed093de4e2020-08-03 16:33:14 -0400115 const SkPoint pts[] = {
116 {0.5f, 6},
117 {5.8070392608642578125f, 6.4612660408020019531f},
118 {-2.9186885356903076172f, 2.811046600341796875f},
119 {0.49999994039535522461f, -1.4124038219451904297f},
120 };
Herb Derbyc37b3862022-06-21 09:49:17 -0400121 canvas->drawPath(SkPath::Polygon(pts, std::size(pts), false), paint);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400122 canvas->restore();
123}
124
senorblancod6ed19c2015-02-26 06:58:17 -0800125// Stairstep with repeated vert (intersection)
126void test_stairstep(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -0800127 canvas->save();
128 canvas->translate(0, 200);
Mike Reed093de4e2020-08-03 16:33:14 -0400129 canvas->drawPath(SkPath::Polygon({{50,50}, {50,20}, {80,20}, {50,50}, {20,50}, {20,80}}, false),
130 paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800131 canvas->restore();
132}
133
134void test_stairstep2(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -0800135 canvas->save();
136 canvas->translate(100, 200);
Mike Reed093de4e2020-08-03 16:33:14 -0400137 canvas->drawPath(SkPath::Polygon({{20,60}, {35,80}, {50,60}, {65,80}, {80,60}}, false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800138 canvas->restore();
139}
140
141// Overlapping segments
142void test_overlapping(SkCanvas* canvas, const SkPaint& paint) {
senorblancod6ed19c2015-02-26 06:58:17 -0800143 canvas->save();
144 canvas->translate(200, 200);
Mike Reed093de4e2020-08-03 16:33:14 -0400145 canvas->drawPath(SkPath::Polygon({{20,80}, {80,80}, {80,20}, {80,30}}, false), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800146 canvas->restore();
147}
148
senorblanco531237e2016-06-02 11:36:48 -0700149// Two "island" triangles inside a containing rect.
150// This exercises the partnering code in the tessellator.
151void test_partners(SkCanvas* canvas, const SkPaint& paint) {
senorblanco531237e2016-06-02 11:36:48 -0700152 canvas->save();
153 canvas->translate(300, 200);
Mike Reed093de4e2020-08-03 16:33:14 -0400154 auto path = SkPathBuilder().addPolygon({{20,80}, {80,80}, {80,20}, {20,20}}, false)
155 .addPolygon({{30,30}, {45,50}, {30,70}}, false)
156 .addPolygon({{70,30}, {70,70}, {55,50}}, false)
157 .detach();
senorblanco531237e2016-06-02 11:36:48 -0700158 canvas->drawPath(path, paint);
159 canvas->restore();
160}
161
Stephen White531a48e2018-06-01 09:49:39 -0400162// A split edge causes one half to be merged to zero winding (destroyed).
163// Test that the other half of the split doesn't also get zero winding.
164void test_winding_merged_to_zero(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400165 SkPathBuilder path;
Stephen White531a48e2018-06-01 09:49:39 -0400166 canvas->save();
167 canvas->translate(400, 350);
168 path.moveTo(20, 80);
169 path.moveTo(70, -0.000001f);
170 path.lineTo(70, 0.0);
171 path.lineTo(60, -30.0);
172 path.lineTo(40, 20.0);
173 path.moveTo(50, 50.0);
174 path.lineTo(50, -50.0);
175 path.lineTo(10, 50.0);
Mike Reed093de4e2020-08-03 16:33:14 -0400176 canvas->drawPath(path.detach(), paint);
Stephen White531a48e2018-06-01 09:49:39 -0400177 canvas->restore();
178}
179
senorblancod6ed19c2015-02-26 06:58:17 -0800180// Monotone test 1 (point in the middle)
181void test_monotone_1(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400182 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800183 canvas->save();
184 canvas->translate(0, 300);
Mike Reed093de4e2020-08-03 16:33:14 -0400185 path.moveTo(20, 20);
186 path.quadTo(20, 50, 80, 50);
187 path.quadTo(20, 50, 20, 80);
188 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800189 canvas->restore();
190}
191
192// Monotone test 2 (point at the top)
193void test_monotone_2(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400194 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800195 canvas->save();
196 canvas->translate(100, 300);
Mike Reed093de4e2020-08-03 16:33:14 -0400197 path.moveTo(20, 20);
198 path.lineTo(80, 30);
199 path.quadTo(20, 20, 20, 80);
200 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800201 canvas->restore();
202}
203
204// Monotone test 3 (point at the bottom)
205void test_monotone_3(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400206 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800207 canvas->save();
208 canvas->translate(200, 300);
Mike Reed093de4e2020-08-03 16:33:14 -0400209 path.moveTo(20, 80);
210 path.lineTo(80, 70);
211 path.quadTo(20, 80, 20, 20);
212 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800213 canvas->restore();
214}
215
216// Monotone test 4 (merging of two monotones)
217void test_monotone_4(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400218 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800219 canvas->save();
220 canvas->translate(300, 300);
221 path.moveTo(80, 25);
222 path.lineTo(50, 39);
223 path.lineTo(20, 25);
224 path.lineTo(40, 45);
225 path.lineTo(70, 50);
226 path.lineTo(80, 80);
Mike Reed093de4e2020-08-03 16:33:14 -0400227 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800228 canvas->restore();
229}
230
231// Monotone test 5 (aborted merging of two monotones)
232void test_monotone_5(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400233 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800234 canvas->save();
235 canvas->translate(0, 400);
236 path.moveTo(50, 20);
237 path.lineTo(80, 80);
238 path.lineTo(50, 50);
239 path.lineTo(20, 80);
Mike Reed093de4e2020-08-03 16:33:14 -0400240 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800241 canvas->restore();
242}
243// Degenerate intersection test
244void test_degenerate(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400245 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800246 canvas->save();
247 canvas->translate(100, 400);
248 path.moveTo(50, 20);
249 path.lineTo(70, 30);
250 path.lineTo(20, 50);
251 path.moveTo(50, 20);
252 path.lineTo(80, 80);
253 path.lineTo(50, 80);
Mike Reed093de4e2020-08-03 16:33:14 -0400254 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800255 canvas->restore();
256}
257// Two triangles with a coincident edge.
258void test_coincident_edge(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400259 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800260 canvas->save();
261 canvas->translate(200, 400);
262
263 path.moveTo(80, 20);
264 path.lineTo(80, 80);
265 path.lineTo(20, 80);
266
267 path.moveTo(20, 20);
268 path.lineTo(80, 80);
269 path.lineTo(20, 80);
270
Mike Reed093de4e2020-08-03 16:33:14 -0400271 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800272 canvas->restore();
273}
274// Bowtie with a coincident triangle (one triangle vertex coincident with the
275// bowtie's intersection).
276void test_bowtie_coincident_triangle(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400277 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800278 canvas->save();
279 canvas->translate(300, 400);
Mike Reed093de4e2020-08-03 16:33:14 -0400280 path.moveTo(20, 20);
281 path.lineTo(80, 80);
282 path.lineTo(80, 20);
283 path.lineTo(20, 80);
284 path.moveTo(50, 50);
285 path.lineTo(80, 20);
286 path.lineTo(80, 80);
287 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800288 canvas->restore();
289}
290
Stephen White85dcf6b2018-07-17 16:14:31 -0400291// Collinear outer boundary edges. In the edge-AA codepath, this creates an overlap region
292// which contains a boundary edge. It can't be removed, but it must have the correct winding.
293void test_collinear_outer_boundary_edge(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400294 SkPathBuilder path;
Stephen White85dcf6b2018-07-17 16:14:31 -0400295 canvas->save();
296 canvas->translate(400, 400);
297 path.moveTo(20, 20);
298 path.lineTo(20, 50);
299 path.lineTo(50, 50);
300 path.moveTo(80, 50);
301 path.lineTo(50, 50);
302 path.lineTo(80, 20);
Mike Reed093de4e2020-08-03 16:33:14 -0400303 canvas->drawPath(path.detach(), paint);
Stephen White85dcf6b2018-07-17 16:14:31 -0400304 canvas->restore();
305}
306
senorblancod6ed19c2015-02-26 06:58:17 -0800307// Coincident edges (big ones first, coincident vert on top).
308void test_coincident_edges_1(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400309 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800310 canvas->save();
311 canvas->translate(0, 500);
Mike Reed093de4e2020-08-03 16:33:14 -0400312 path.moveTo(20, 20);
313 path.lineTo(80, 80);
314 path.lineTo(20, 80);
315 path.moveTo(20, 20);
316 path.lineTo(50, 50);
317 path.lineTo(20, 50);
318 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800319 canvas->restore();
320}
321// Coincident edges (small ones first, coincident vert on top).
322void test_coincident_edges_2(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed093de4e2020-08-03 16:33:14 -0400323 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800324 canvas->save();
325 canvas->translate(100, 500);
Mike Reed093de4e2020-08-03 16:33:14 -0400326 path.moveTo(20, 20);
327 path.lineTo(50, 50);
328 path.lineTo(20, 50);
329 path.moveTo(20, 20);
330 path.lineTo(80, 80);
331 path.lineTo(20, 80);
332 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800333 canvas->restore();
334}
335// Coincident edges (small ones first, coincident vert on bottom).
336void test_coincident_edges_3(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed15a54032020-08-16 11:15:41 -0400337 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800338 canvas->save();
339 canvas->translate(200, 500);
Mike Reed093de4e2020-08-03 16:33:14 -0400340 path.moveTo(20, 80);
341 path.lineTo(20, 50);
342 path.lineTo(50, 50);
343 path.moveTo(20, 80);
344 path.lineTo(20, 20);
345 path.lineTo(80, 20);
Mike Reed15a54032020-08-16 11:15:41 -0400346 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800347 canvas->restore();
348}
349// Coincident edges (big ones first, coincident vert on bottom).
350void test_coincident_edges_4(SkCanvas* canvas, const SkPaint& paint) {
Mike Reed15a54032020-08-16 11:15:41 -0400351 SkPathBuilder path;
senorblancod6ed19c2015-02-26 06:58:17 -0800352 canvas->save();
353 canvas->translate(300, 500);
Mike Reed093de4e2020-08-03 16:33:14 -0400354 path.moveTo(20, 80);
355 path.lineTo(20, 20);
356 path.lineTo(80, 20);
357 path.moveTo(20, 80);
358 path.lineTo(20, 50);
359 path.lineTo(50, 50);
Mike Reed15a54032020-08-16 11:15:41 -0400360 canvas->drawPath(path.detach(), paint);
senorblancod6ed19c2015-02-26 06:58:17 -0800361 canvas->restore();
362}
363
John Stilesa6841be2020-08-06 14:11:56 -0400364} // namespace
senorblancod6ed19c2015-02-26 06:58:17 -0800365
Stephen White5926f2d2017-02-13 13:55:42 -0500366DEF_SIMPLE_GM(concavepaths, canvas, 500, 600) {
Stephen White930f69e2017-01-12 17:15:50 -0500367 SkPaint paint;
senorblancod6ed19c2015-02-26 06:58:17 -0800368
Stephen White930f69e2017-01-12 17:15:50 -0500369 paint.setAntiAlias(true);
370 paint.setStyle(SkPaint::kFill_Style);
senorblancod6ed19c2015-02-26 06:58:17 -0800371
Stephen White930f69e2017-01-12 17:15:50 -0500372 test_concave(canvas, paint);
373 test_reverse_concave(canvas, paint);
374 test_bowtie(canvas, paint);
375 test_fake_bowtie(canvas, paint);
Stephen White5926f2d2017-02-13 13:55:42 -0500376 test_intruding_vertex(canvas, paint);
Stephen White930f69e2017-01-12 17:15:50 -0500377 test_fish(canvas, paint);
378 test_fast_forward(canvas, paint);
379 test_hole(canvas, paint);
380 test_star(canvas, paint);
Stephen White3b5a3fa2017-06-06 14:51:19 -0400381 test_twist(canvas, paint);
Stephen White5926f2d2017-02-13 13:55:42 -0500382 test_inversion_repeat_vertex(canvas, paint);
Stephen White930f69e2017-01-12 17:15:50 -0500383 test_stairstep(canvas, paint);
384 test_stairstep2(canvas, paint);
385 test_overlapping(canvas, paint);
386 test_partners(canvas, paint);
Stephen White531a48e2018-06-01 09:49:39 -0400387 test_winding_merged_to_zero(canvas, paint);
Stephen White930f69e2017-01-12 17:15:50 -0500388 test_monotone_1(canvas, paint);
389 test_monotone_2(canvas, paint);
390 test_monotone_3(canvas, paint);
391 test_monotone_4(canvas, paint);
392 test_monotone_5(canvas, paint);
393 test_degenerate(canvas, paint);
394 test_coincident_edge(canvas, paint);
395 test_bowtie_coincident_triangle(canvas, paint);
Stephen White85dcf6b2018-07-17 16:14:31 -0400396 test_collinear_outer_boundary_edge(canvas, paint);
Stephen White930f69e2017-01-12 17:15:50 -0500397 test_coincident_edges_1(canvas, paint);
398 test_coincident_edges_2(canvas, paint);
399 test_coincident_edges_3(canvas, paint);
400 test_coincident_edges_4(canvas, paint);
401}