blob: 172082248abe39f583461dc321f1a92b07f97168 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Hal Canary95e3c052017-01-11 12:44:43 -05008#include "SkAutoMalloc.h"
reedc1b11f12015-03-13 08:48:26 -07009#include "SkPath.h"
reed@android.com097a3512010-07-13 18:35:14 +000010#include "SkRandom.h"
tfarina@chromium.org8f6884a2014-01-24 20:56:26 +000011#include "SkRegion.h"
12#include "Test.h"
reed@android.com097a3512010-07-13 18:35:14 +000013
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000014static void Union(SkRegion* rgn, const SkIRect& rect) {
15 rgn->op(rect, SkRegion::kUnion_Op);
16}
17
18#define TEST_NO_INTERSECT(rgn, rect) REPORTER_ASSERT(reporter, !rgn.intersects(rect))
19#define TEST_INTERSECT(rgn, rect) REPORTER_ASSERT(reporter, rgn.intersects(rect))
mike@reedtribe.org796a1752012-11-07 03:39:46 +000020#define TEST_NO_CONTAINS(rgn, rect) REPORTER_ASSERT(reporter, !rgn.contains(rect))
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000021
22// inspired by http://code.google.com/p/skia/issues/detail?id=958
23//
24static void test_fromchrome(skiatest::Reporter* reporter) {
25 SkRegion r;
26 Union(&r, SkIRect::MakeXYWH(0, 0, 1, 1));
27 TEST_NO_INTERSECT(r, SkIRect::MakeXYWH(0, 0, 0, 0));
28 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 0, 2, 2));
29 TEST_INTERSECT(r, SkIRect::MakeXYWH(-1, 0, 2, 2));
30 TEST_INTERSECT(r, SkIRect::MakeXYWH(-1, -1, 2, 2));
31 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, -1, 2, 2));
32 TEST_INTERSECT(r, SkIRect::MakeXYWH(-1, -1, 3, 3));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000033
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000034 Union(&r, SkIRect::MakeXYWH(0, 0, 3, 3));
35 Union(&r, SkIRect::MakeXYWH(10, 0, 3, 3));
36 Union(&r, SkIRect::MakeXYWH(0, 10, 13, 3));
37 TEST_INTERSECT(r, SkIRect::MakeXYWH(-1, -1, 2, 2));
38 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, -1, 2, 2));
39 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, 2, 2, 2));
40 TEST_INTERSECT(r, SkIRect::MakeXYWH(-1, 2, 2, 2));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000041
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000042 TEST_INTERSECT(r, SkIRect::MakeXYWH(9, -1, 2, 2));
43 TEST_INTERSECT(r, SkIRect::MakeXYWH(12, -1, 2, 2));
44 TEST_INTERSECT(r, SkIRect::MakeXYWH(12, 2, 2, 2));
45 TEST_INTERSECT(r, SkIRect::MakeXYWH(9, 2, 2, 2));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000046
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000047 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, -1, 13, 5));
48 TEST_INTERSECT(r, SkIRect::MakeXYWH(1, -1, 11, 5));
49 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, -1, 9, 5));
50 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, -1, 8, 5));
51 TEST_INTERSECT(r, SkIRect::MakeXYWH(3, -1, 8, 5));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000052
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000053 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 1, 13, 1));
54 TEST_INTERSECT(r, SkIRect::MakeXYWH(1, 1, 11, 1));
55 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, 1, 9, 1));
56 TEST_INTERSECT(r, SkIRect::MakeXYWH(2, 1, 8, 1));
57 TEST_INTERSECT(r, SkIRect::MakeXYWH(3, 1, 8, 1));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000058
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000059 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 0, 13, 13));
60 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 1, 13, 11));
61 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 2, 13, 9));
62 TEST_INTERSECT(r, SkIRect::MakeXYWH(0, 2, 13, 8));
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000063
64
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000065 // These test SkRegion::contains(Rect) and SkRegion::contains(Region)
skia.committer@gmail.com72b2e6f2012-11-08 02:03:56 +000066
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000067 SkRegion container;
68 Union(&container, SkIRect::MakeXYWH(0, 0, 40, 20));
69 Union(&container, SkIRect::MakeXYWH(30, 20, 10, 20));
70 TEST_NO_CONTAINS(container, SkIRect::MakeXYWH(0, 0, 10, 39));
71 TEST_NO_CONTAINS(container, SkIRect::MakeXYWH(29, 0, 10, 39));
reed@google.combb094b92012-11-07 14:23:42 +000072
73 {
74 SkRegion rgn;
75 Union(&rgn, SkIRect::MakeXYWH(0, 0, 10, 10));
76 Union(&rgn, SkIRect::MakeLTRB(5, 10, 20, 20));
77 TEST_INTERSECT(rgn, SkIRect::MakeXYWH(15, 0, 5, 11));
78 }
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +000079}
80
reed@google.com8a0d8ff2012-04-30 16:28:04 +000081static void test_empties(skiatest::Reporter* reporter) {
82 SkRegion valid(SkIRect::MakeWH(10, 10));
83 SkRegion empty, empty2;
84
85 REPORTER_ASSERT(reporter, empty.isEmpty());
86 REPORTER_ASSERT(reporter, !valid.isEmpty());
87
88 // test intersects
89 REPORTER_ASSERT(reporter, !empty.intersects(empty2));
90 REPORTER_ASSERT(reporter, !valid.intersects(empty));
91
92 // test contains
93 REPORTER_ASSERT(reporter, !empty.contains(empty2));
94 REPORTER_ASSERT(reporter, !valid.contains(empty));
95 REPORTER_ASSERT(reporter, !empty.contains(valid));
reedc1b11f12015-03-13 08:48:26 -070096
97 SkPath emptyPath;
98 emptyPath.moveTo(1, 5);
99 emptyPath.close();
100 SkRegion openClip;
101 openClip.setRect(-16000, -16000, 16000, 16000);
102 empty.setPath(emptyPath, openClip); // should not assert
reed@google.com8a0d8ff2012-04-30 16:28:04 +0000103}
104
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000105enum {
106 W = 256,
107 H = 256
108};
109
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000110static SkIRect randRect(SkRandom& rand) {
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000111 int x = rand.nextU() % W;
112 int y = rand.nextU() % H;
113 int w = rand.nextU() % W;
114 int h = rand.nextU() % H;
115 return SkIRect::MakeXYWH(x, y, w >> 1, h >> 1);
116}
117
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000118static void randRgn(SkRandom& rand, SkRegion* rgn, int n) {
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000119 rgn->setEmpty();
120 for (int i = 0; i < n; ++i) {
121 rgn->op(randRect(rand), SkRegion::kUnion_Op);
122 }
123}
124
125static bool slow_contains(const SkRegion& outer, const SkRegion& inner) {
126 SkRegion tmp;
127 tmp.op(outer, inner, SkRegion::kUnion_Op);
128 return outer == tmp;
129}
130
reed@google.com7ab71ba2012-05-02 16:09:21 +0000131static bool slow_contains(const SkRegion& outer, const SkIRect& r) {
132 SkRegion tmp;
133 tmp.op(outer, SkRegion(r), SkRegion::kUnion_Op);
134 return outer == tmp;
135}
136
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000137static bool slow_intersects(const SkRegion& outer, const SkRegion& inner) {
138 SkRegion tmp;
139 return tmp.op(outer, inner, SkRegion::kIntersect_Op);
140}
141
reed@google.com7ab71ba2012-05-02 16:09:21 +0000142static void test_contains_iter(skiatest::Reporter* reporter, const SkRegion& rgn) {
143 SkRegion::Iterator iter(rgn);
144 while (!iter.done()) {
145 SkIRect r = iter.rect();
146 REPORTER_ASSERT(reporter, rgn.contains(r));
147 r.inset(-1, -1);
148 REPORTER_ASSERT(reporter, !rgn.contains(r));
149 iter.next();
150 }
151}
152
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000153static void contains_proc(skiatest::Reporter* reporter,
154 const SkRegion& a, const SkRegion& b) {
reed@google.com7ab71ba2012-05-02 16:09:21 +0000155 // test rgn
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000156 bool c0 = a.contains(b);
157 bool c1 = slow_contains(a, b);
158 REPORTER_ASSERT(reporter, c0 == c1);
reed@google.com7ab71ba2012-05-02 16:09:21 +0000159
160 // test rect
161 SkIRect r = a.getBounds();
162 r.inset(r.width()/4, r.height()/4);
163 c0 = a.contains(r);
164 c1 = slow_contains(a, r);
165 REPORTER_ASSERT(reporter, c0 == c1);
166
167 test_contains_iter(reporter, a);
168 test_contains_iter(reporter, b);
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000169}
170
reed@google.com684119d2012-05-02 16:52:12 +0000171static void test_intersects_iter(skiatest::Reporter* reporter, const SkRegion& rgn) {
172 SkRegion::Iterator iter(rgn);
173 while (!iter.done()) {
174 SkIRect r = iter.rect();
175 REPORTER_ASSERT(reporter, rgn.intersects(r));
176 r.inset(-1, -1);
177 REPORTER_ASSERT(reporter, rgn.intersects(r));
178 iter.next();
179 }
180}
181
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000182static void intersects_proc(skiatest::Reporter* reporter,
183 const SkRegion& a, const SkRegion& b) {
184 bool c0 = a.intersects(b);
185 bool c1 = slow_intersects(a, b);
186 REPORTER_ASSERT(reporter, c0 == c1);
reed@google.com684119d2012-05-02 16:52:12 +0000187
188 test_intersects_iter(reporter, a);
189 test_intersects_iter(reporter, b);
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000190}
191
192static void test_proc(skiatest::Reporter* reporter,
193 void (*proc)(skiatest::Reporter*,
194 const SkRegion& a, const SkRegion&)) {
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000195 SkRandom rand;
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000196 for (int i = 0; i < 10000; ++i) {
197 SkRegion outer;
198 randRgn(rand, &outer, 8);
199 SkRegion inner;
200 randRgn(rand, &inner, 2);
201 proc(reporter, outer, inner);
202 }
203}
204
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000205static void rand_rect(SkIRect* rect, SkRandom& rand) {
reed@android.com097a3512010-07-13 18:35:14 +0000206 int bits = 6;
207 int shift = 32 - bits;
208 rect->set(rand.nextU() >> shift, rand.nextU() >> shift,
209 rand.nextU() >> shift, rand.nextU() >> shift);
210 rect->sort();
211}
212
213static bool test_rects(const SkIRect rect[], int count) {
214 SkRegion rgn0, rgn1;
215
216 for (int i = 0; i < count; i++) {
217 rgn0.op(rect[i], SkRegion::kUnion_Op);
218 }
219 rgn1.setRects(rect, count);
220
221 if (rgn0 != rgn1) {
222 SkDebugf("\n");
223 for (int i = 0; i < count; i++) {
224 SkDebugf(" { %d, %d, %d, %d },\n",
225 rect[i].fLeft, rect[i].fTop,
226 rect[i].fRight, rect[i].fBottom);
227 }
228 SkDebugf("\n");
229 return false;
230 }
231 return true;
232}
233
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000234DEF_TEST(Region, reporter) {
reed@android.com097a3512010-07-13 18:35:14 +0000235 const SkIRect r2[] = {
236 { 0, 0, 1, 1 },
237 { 2, 2, 3, 3 },
238 };
239 REPORTER_ASSERT(reporter, test_rects(r2, SK_ARRAY_COUNT(r2)));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000240
reed@android.com097a3512010-07-13 18:35:14 +0000241 const SkIRect rects[] = {
242 { 0, 0, 1, 2 },
243 { 2, 1, 3, 3 },
244 { 4, 0, 5, 1 },
245 { 6, 0, 7, 4 },
246 };
247 REPORTER_ASSERT(reporter, test_rects(rects, SK_ARRAY_COUNT(rects)));
248
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000249 SkRandom rand;
reed@android.com097a3512010-07-13 18:35:14 +0000250 for (int i = 0; i < 1000; i++) {
251 SkRegion rgn0, rgn1;
252
253 const int N = 8;
254 SkIRect rect[N];
255 for (int j = 0; j < N; j++) {
256 rand_rect(&rect[j], rand);
257 }
258 REPORTER_ASSERT(reporter, test_rects(rect, N));
259 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000260
mike@reedtribe.org67c31842012-04-28 20:09:19 +0000261 test_proc(reporter, contains_proc);
262 test_proc(reporter, intersects_proc);
reed@google.com8a0d8ff2012-04-30 16:28:04 +0000263 test_empties(reporter);
mike@reedtribe.org5a7ae4f2012-11-07 01:57:32 +0000264 test_fromchrome(reporter);
reed@android.com097a3512010-07-13 18:35:14 +0000265}
scroggod49f48d2015-06-18 06:16:36 -0700266
267// Test that writeToMemory reports the same number of bytes whether there was a
268// buffer to write to or not.
269static void test_write(const SkRegion& region, skiatest::Reporter* r) {
halcanary96fcdcc2015-08-27 07:41:13 -0700270 const size_t bytesNeeded = region.writeToMemory(nullptr);
scroggod49f48d2015-06-18 06:16:36 -0700271 SkAutoMalloc storage(bytesNeeded);
272 const size_t bytesWritten = region.writeToMemory(storage.get());
273 REPORTER_ASSERT(r, bytesWritten == bytesNeeded);
274}
275
276DEF_TEST(Region_writeToMemory, r) {
277 // Test an empty region.
278 SkRegion region;
279 REPORTER_ASSERT(r, region.isEmpty());
280 test_write(region, r);
281
282 // Test a rectangular region
283 bool nonEmpty = region.setRect(0, 0, 50, 50);
284 REPORTER_ASSERT(r, nonEmpty);
285 REPORTER_ASSERT(r, region.isRect());
286 test_write(region, r);
287
288 // Test a complex region
289 nonEmpty = region.op(50, 50, 100, 100, SkRegion::kUnion_Op);
290 REPORTER_ASSERT(r, nonEmpty);
291 REPORTER_ASSERT(r, region.isComplex());
292 test_write(region, r);
293}