blob: df77100b0104f10e97dfaeb9bb8ea7ddff424b86 [file] [log] [blame]
caryclark45fa4472015-01-16 07:04:10 -08001/*
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 */
caryclark54359292015-03-26 07:52:43 -07007
8#include "PathOpsExtendedTest.h"
caryclark45fa4472015-01-16 07:04:10 -08009#include "PathOpsTestCommon.h"
caryclark54359292015-03-26 07:52:43 -070010#include "SkBitmap.h"
caryclark45fa4472015-01-16 07:04:10 -080011#include "Test.h"
12
13DEF_TEST(PathOpsBuilder, reporter) {
14 SkOpBuilder builder;
15 SkPath result;
16 REPORTER_ASSERT(reporter, builder.resolve(&result));
17 REPORTER_ASSERT(reporter, result.isEmpty());
18
caryclark54359292015-03-26 07:52:43 -070019 builder.add(result, kDifference_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080020 REPORTER_ASSERT(reporter, builder.resolve(&result));
21 REPORTER_ASSERT(reporter, result.isEmpty());
22
caryclark54359292015-03-26 07:52:43 -070023 builder.add(result, kUnion_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080024 REPORTER_ASSERT(reporter, builder.resolve(&result));
25 REPORTER_ASSERT(reporter, result.isEmpty());
26
27 SkPath rectPath;
caryclark54359292015-03-26 07:52:43 -070028 rectPath.setFillType(SkPath::kEvenOdd_FillType);
caryclark45fa4472015-01-16 07:04:10 -080029 rectPath.addRect(0, 1, 2, 3, SkPath::kCW_Direction);
caryclark54359292015-03-26 07:52:43 -070030 builder.add(rectPath, kUnion_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080031 REPORTER_ASSERT(reporter, builder.resolve(&result));
32 bool closed;
33 SkPath::Direction dir;
34 REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
35 REPORTER_ASSERT(reporter, closed);
caryclark5b5ddd72015-05-18 05:12:56 -070036 REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction);
caryclark4e1a4c92015-05-18 12:56:57 -070037 int pixelDiff = comparePaths(reporter, __FUNCTION__, rectPath, result);
caryclark5b5ddd72015-05-18 05:12:56 -070038 REPORTER_ASSERT(reporter, pixelDiff == 0);
caryclark45fa4472015-01-16 07:04:10 -080039
40 rectPath.reset();
caryclark54359292015-03-26 07:52:43 -070041 rectPath.setFillType(SkPath::kEvenOdd_FillType);
caryclark45fa4472015-01-16 07:04:10 -080042 rectPath.addRect(0, 1, 2, 3, SkPath::kCCW_Direction);
caryclark54359292015-03-26 07:52:43 -070043 builder.add(rectPath, kUnion_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080044 REPORTER_ASSERT(reporter, builder.resolve(&result));
45 REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
46 REPORTER_ASSERT(reporter, closed);
47 REPORTER_ASSERT(reporter, dir == SkPath::kCCW_Direction);
caryclark5b5ddd72015-05-18 05:12:56 -070048 REPORTER_ASSERT(reporter, rectPath == result);
caryclark45fa4472015-01-16 07:04:10 -080049
caryclark54359292015-03-26 07:52:43 -070050 builder.add(rectPath, kDifference_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080051 REPORTER_ASSERT(reporter, builder.resolve(&result));
52 REPORTER_ASSERT(reporter, result.isEmpty());
53
54 SkPath rect2, rect3;
55 rect2.addRect(2, 1, 4, 3, SkPath::kCW_Direction);
56 rect3.addRect(4, 1, 5, 3, SkPath::kCCW_Direction);
caryclark54359292015-03-26 07:52:43 -070057 builder.add(rectPath, kUnion_SkPathOp);
58 builder.add(rect2, kUnion_SkPathOp);
59 builder.add(rect3, kUnion_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080060 REPORTER_ASSERT(reporter, builder.resolve(&result));
61 REPORTER_ASSERT(reporter, result.isRect(NULL, &closed, &dir));
62 REPORTER_ASSERT(reporter, closed);
63 SkRect expected;
64 expected.set(0, 1, 5, 3);
65 REPORTER_ASSERT(reporter, result.getBounds() == expected);
66
67 SkPath circle1, circle2, circle3;
68 circle1.addCircle(5, 6, 4, SkPath::kCW_Direction);
69 circle2.addCircle(7, 4, 8, SkPath::kCCW_Direction);
70 circle3.addCircle(6, 5, 6, SkPath::kCW_Direction);
71 SkPath opCompare;
caryclark54359292015-03-26 07:52:43 -070072 Op(circle1, circle2, kUnion_SkPathOp, &opCompare);
73 Op(opCompare, circle3, kDifference_SkPathOp, &opCompare);
74 builder.add(circle1, kUnion_SkPathOp);
75 builder.add(circle2, kUnion_SkPathOp);
76 builder.add(circle3, kDifference_SkPathOp);
caryclark45fa4472015-01-16 07:04:10 -080077 REPORTER_ASSERT(reporter, builder.resolve(&result));
caryclark4e1a4c92015-05-18 12:56:57 -070078 pixelDiff = comparePaths(reporter, __FUNCTION__, opCompare, result);
caryclark54359292015-03-26 07:52:43 -070079 REPORTER_ASSERT(reporter, pixelDiff == 0);
caryclark45fa4472015-01-16 07:04:10 -080080}
caryclarkfba9da72015-05-14 14:18:13 -070081
caryclark5b5ddd72015-05-18 05:12:56 -070082DEF_TEST(BuilderIssue3838, reporter) {
caryclarkfba9da72015-05-14 14:18:13 -070083 SkPath path;
84 path.moveTo(200, 170);
85 path.lineTo(220, 170);
86 path.lineTo(220, 230);
87 path.lineTo(240, 230);
88 path.lineTo(240, 210);
89 path.lineTo(180, 210);
90 path.lineTo(180, 190);
91 path.lineTo(260, 190);
92 path.lineTo(260, 250);
93 path.lineTo(200, 250);
94 path.lineTo(200, 170);
95 path.close();
caryclarkfba9da72015-05-14 14:18:13 -070096 SkPath path2;
97 SkOpBuilder builder;
98 builder.add(path, kUnion_SkPathOp);
99 builder.resolve(&path2);
caryclark4e1a4c92015-05-18 12:56:57 -0700100 int pixelDiff = comparePaths(reporter, __FUNCTION__, path, path2);
caryclarkfba9da72015-05-14 14:18:13 -0700101 REPORTER_ASSERT(reporter, pixelDiff == 0);
102}
caryclark5b5ddd72015-05-18 05:12:56 -0700103
104DEF_TEST(BuilderIssue3838_2, reporter) {
105 SkPath path;
106 path.addCircle(100, 100, 50);
107
108 SkOpBuilder builder;
109 builder.add(path, kUnion_SkPathOp);
110 builder.add(path, kUnion_SkPathOp);
111
112 SkPath result;
caryclark5b5ddd72015-05-18 05:12:56 -0700113 builder.resolve(&result);
caryclark4e1a4c92015-05-18 12:56:57 -0700114 int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
115 REPORTER_ASSERT(reporter, pixelDiff == 0);
116}
117
118DEF_TEST(BuilderIssue3838_3, reporter) {
119 SkPath path;
120 path.moveTo(40, 10);
121 path.lineTo(60, 10);
122 path.lineTo(60, 30);
123 path.lineTo(40, 30);
124 path.lineTo(40, 10);
125 path.moveTo(41, 11);
126 path.lineTo(41, 29);
127 path.lineTo(59, 29);
128 path.lineTo(59, 11);
129 path.lineTo(41, 11);
130
131 SkOpBuilder builder;
132 builder.add(path, kUnion_SkPathOp);
133 SkPath result;
134 builder.resolve(&result);
135 int pixelDiff = comparePaths(reporter, __FUNCTION__, path, result);
caryclark5b5ddd72015-05-18 05:12:56 -0700136 REPORTER_ASSERT(reporter, pixelDiff == 0);
137}
caryclark218f21a2015-06-29 11:41:52 -0700138
139DEF_TEST(BuilderIssue502792_2, reporter) {
140 SkPath path, pathB;
141 path.setFillType(SkPath::kWinding_FillType);
142 path.addRect(0, 0, 1, 1, SkPath::kCW_Direction);
143 path.addRect(2, 2, 3, 3, SkPath::kCW_Direction);
144 pathB.setFillType(SkPath::kEvenOdd_FillType);
145 pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction);
146 pathB.addRect(3, 3, 4, 4, SkPath::kCW_Direction);
147 SkOpBuilder builder;
148 builder.add(path, kUnion_SkPathOp);
149 builder.add(pathB, kDifference_SkPathOp);
150 SkPath result;
151 builder.resolve(&result);
152}