1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <algorithm>
#include "PingPongRegion.h"
#include "gtest/gtest.h"
#include "gtest/MozGTestBench.h"
#include "nsRect.h"
#include "nsRegion.h"
#include "RegionBuilder.h"
#include "mozilla/gfx/TiledRegion.h"
#include "mozilla/UniquePtr.h"
using namespace std;
using namespace mozilla::gfx;
class TestLargestRegion {
public:
static void TestSingleRect(nsRect r) {
nsRegion region(r);
EXPECT_TRUE(region.GetLargestRectangle().IsEqualInterior(r));
}
// Construct a rectangle, remove part of it, then check the remainder
static void TestNonRectangular() {
nsRegion r(nsRect(0, 0, 30, 30));
const int nTests = 19;
struct {
nsRect rect;
int64_t expectedArea;
} tests[nTests] = {
// Remove a 20x10 chunk from the square
{ nsRect(0, 0, 20, 10), 600 },
{ nsRect(10, 0, 20, 10), 600 },
{ nsRect(10, 20, 20, 10), 600 },
{ nsRect(0, 20, 20, 10), 600 },
// Remove a 10x20 chunk from the square
{ nsRect(0, 0, 10, 20), 600 },
{ nsRect(20, 0, 10, 20), 600 },
{ nsRect(20, 10, 10, 20), 600 },
{ nsRect(0, 10, 10, 20), 600 },
// Remove the center 10x10
{ nsRect(10, 10, 10, 10), 300 },
// Remove the middle column
{ nsRect(10, 0, 10, 30), 300 },
// Remove the middle row
{ nsRect(0, 10, 30, 10), 300 },
// Remove the corners 10x10
{ nsRect(0, 0, 10, 10), 600 },
{ nsRect(20, 20, 10, 10), 600 },
{ nsRect(20, 0, 10, 10), 600 },
{ nsRect(0, 20, 10, 10), 600 },
// Remove the corners 20x20
{ nsRect(0, 0, 20, 20), 300 },
{ nsRect(10, 10, 20, 20), 300 },
{ nsRect(10, 0, 20, 20), 300 },
{ nsRect(0, 10, 20, 20), 300 }
};
for (int32_t i = 0; i < nTests; i++) {
nsRegion r2;
r2.Sub(r, tests[i].rect);
EXPECT_TRUE(r2.IsComplex()) << "nsRegion code got unexpectedly smarter!";
nsRect largest = r2.GetLargestRectangle();
EXPECT_TRUE(largest.width * largest.height == tests[i].expectedArea) <<
"Did not successfully find largest rectangle in non-rectangular region on iteration " << i;
}
}
static void TwoRectTest() {
nsRegion r(nsRect(0, 0, 100, 100));
const int nTests = 4;
struct {
nsRect rect1, rect2;
int64_t expectedArea;
} tests[nTests] = {
{ nsRect(0, 0, 75, 40), nsRect(0, 60, 75, 40), 2500 },
{ nsRect(25, 0, 75, 40), nsRect(25, 60, 75, 40), 2500 },
{ nsRect(25, 0, 75, 40), nsRect(0, 60, 75, 40), 2000 },
{ nsRect(0, 0, 75, 40), nsRect(25, 60, 75, 40), 2000 },
};
for (int32_t i = 0; i < nTests; i++) {
nsRegion r2;
r2.Sub(r, tests[i].rect1);
r2.Sub(r2, tests[i].rect2);
EXPECT_TRUE(r2.IsComplex()) << "nsRegion code got unexpectedly smarter!";
nsRect largest = r2.GetLargestRectangle();
EXPECT_TRUE(largest.width * largest.height == tests[i].expectedArea) <<
"Did not successfully find largest rectangle in two-rect-subtract region on iteration " << i;
}
}
static void TestContainsSpecifiedRect() {
nsRegion r(nsRect(0, 0, 100, 100));
r.Or(r, nsRect(0, 300, 50, 50));
EXPECT_TRUE(r.GetLargestRectangle(nsRect(0, 300, 10, 10)).IsEqualInterior(nsRect(0, 300, 50, 50))) <<
"Chose wrong rectangle";
}
static void TestContainsSpecifiedOverflowingRect() {
nsRegion r(nsRect(0, 0, 100, 100));
r.Or(r, nsRect(0, 300, 50, 50));
EXPECT_TRUE(r.GetLargestRectangle(nsRect(0, 290, 10, 20)).IsEqualInterior(nsRect(0, 300, 50, 50))) <<
"Chose wrong rectangle";
}
};
TEST(Gfx, RegionSingleRect) {
TestLargestRegion::TestSingleRect(nsRect(0, 52, 720, 480));
TestLargestRegion::TestSingleRect(nsRect(-20, 40, 50, 20));
TestLargestRegion::TestSingleRect(nsRect(-20, 40, 10, 8));
TestLargestRegion::TestSingleRect(nsRect(-20, -40, 10, 8));
TestLargestRegion::TestSingleRect(nsRect(-10, -10, 20, 20));
}
TEST(Gfx, RegionNonRectangular) {
TestLargestRegion::TestNonRectangular();
}
TEST(Gfx, RegionTwoRectTest) {
TestLargestRegion::TwoRectTest();
}
TEST(Gfx, RegionContainsSpecifiedRect) {
TestLargestRegion::TestContainsSpecifiedRect();
}
TEST(Gfx, RegionTestContainsSpecifiedOverflowingRect) {
TestLargestRegion::TestContainsSpecifiedOverflowingRect();
}
TEST(Gfx, RegionScaleToInside) {
{ // no rectangles
nsRegion r;
nsIntRegion scaled = r.ScaleToInsidePixels(1, 1, 60);
nsIntRegion result;
EXPECT_TRUE(result.IsEqual(scaled)) <<
"scaled result incorrect";
}
{ // one rectangle
nsRegion r(nsRect(0,44760,19096,264));
nsIntRegion scaled = r.ScaleToInsidePixels(1, 1, 60);
nsIntRegion result(mozilla::gfx::IntRect(0,746,318,4));
EXPECT_TRUE(result.IsEqual(scaled)) <<
"scaled result incorrect";
}
{ // the first rectangle gets adjusted
nsRegion r(nsRect(0,44760,19096,264));
r.Or(r, nsRect(0,45024,19360,1056));
nsIntRegion scaled = r.ScaleToInsidePixels(1, 1, 60);
nsIntRegion result(mozilla::gfx::IntRect(0,746,318,5));
result.Or(result, mozilla::gfx::IntRect(0,751,322,17));
EXPECT_TRUE(result.IsEqual(scaled)) <<
"scaled result incorrect";
}
{ // the second rectangle gets adjusted
nsRegion r(nsRect(0,44760,19360,264));
r.Or(r, nsRect(0,45024,19096,1056));
nsIntRegion scaled = r.ScaleToInsidePixels(1, 1, 60);
nsIntRegion result(mozilla::gfx::IntRect(0,746,322,4));
result.Or(result, mozilla::gfx::IntRect(0,750,318,18));
EXPECT_TRUE(result.IsEqual(scaled)) <<
"scaled result incorrect";
}
}
TEST(Gfx, RegionSimplify) {
{ // ensure simplify works on a single rect
nsRegion r(nsRect(0,100,200,100));
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,200,100));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not the same";
}
{ // the rectangles will be merged
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,300,300));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // two rectangle on the first span
// one on the second
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.Or(r, nsRect(250,100,50,100));
EXPECT_TRUE(r.GetNumRects() == 3) <<
"wrong number of rects";
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,300,300));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // the rectangles will be merged
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.Or(r, nsRect(250,100,50,100));
r.Sub(r, nsRect(200,200,40,200));
EXPECT_TRUE(r.GetNumRects() == 4) <<
"wrong number of rects";
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,300,300));
result.Sub(result, nsRect(200,100,40,300));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // three spans of rectangles
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.Or(r, nsRect(250,100,50,50));
r.Sub(r, nsRect(200,200,40,200));
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,300,300));
result.Sub(result, nsRect(200,100,40,300));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // three spans of rectangles and an unmerged rectangle
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.Or(r, nsRect(250,100,50,50));
r.Sub(r, nsRect(200,200,40,200));
r.Or(r, nsRect(250,900,150,50));
r.SimplifyOutwardByArea(100*100);
nsRegion result(nsRect(0,100,300,300));
result.Sub(result, nsRect(200,100,40,300));
result.Or(result, nsRect(250,900,150,50));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // unmerged regions
nsRegion r(nsRect(0,100,200,100));
r.Or(r, nsRect(0,200,300,200));
r.SimplifyOutwardByArea(100);
nsRegion result(nsRect(0,100,200,100));
result.Or(result, nsRect(0,200,300,200));
EXPECT_TRUE(r.IsEqual(result)) <<
"regions not merged";
}
{ // empty region
// just make sure this doesn't crash.
nsRegion r;
r.SimplifyOutwardByArea(100);
}
}
TEST(Gfx, RegionContains)
{
{ // ensure Contains works on a simple region
nsRegion r(nsRect(0, 0, 100, 100));
EXPECT_TRUE(r.Contains(0, 0));
EXPECT_TRUE(r.Contains(0, 99));
EXPECT_TRUE(r.Contains(99, 0));
EXPECT_TRUE(r.Contains(99, 99));
EXPECT_FALSE(r.Contains(-1, 50));
EXPECT_FALSE(r.Contains(100, 50));
EXPECT_FALSE(r.Contains(50, -1));
EXPECT_FALSE(r.Contains(50, 100));
EXPECT_TRUE(r.Contains(nsRect(0, 0, 100, 100)));
EXPECT_TRUE(r.Contains(nsRect(99, 99, 1, 1)));
EXPECT_FALSE(r.Contains(nsRect(100, 100, 1, 1)));
EXPECT_FALSE(r.Contains(nsRect(100, 100, 0, 0)));
}
{ // empty regions contain nothing
nsRegion r(nsRect(100, 100, 0, 0));
EXPECT_FALSE(r.Contains(0, 0));
EXPECT_FALSE(r.Contains(100, 100));
EXPECT_FALSE(r.Contains(nsRect(100, 100, 0, 0)));
EXPECT_FALSE(r.Contains(nsRect(100, 100, 1, 1)));
}
{ // complex region contain tests
// The region looks like this, with two squares that overlap.
// (hard to do accurately with ASCII art)
// +------+
// | |
// | +--+
// | |
// +--+ |
// | |
// +------+
nsRegion r(nsRect(0, 0, 100, 100));
r.OrWith(nsRect(50, 50, 100, 100));
EXPECT_TRUE(r.Contains(0, 0));
EXPECT_TRUE(r.Contains(99, 99));
EXPECT_TRUE(r.Contains(50, 100));
EXPECT_TRUE(r.Contains(100, 50));
EXPECT_TRUE(r.Contains(149, 149));
EXPECT_FALSE(r.Contains(49, 100));
EXPECT_FALSE(r.Contains(100, 49));
EXPECT_FALSE(r.Contains(150, 150));
EXPECT_TRUE(r.Contains(nsRect(100, 100, 1, 1)));
EXPECT_FALSE(r.Contains(nsRect(49, 99, 2, 2)));
}
{ // region with a hole
nsRegion r(nsRect(0, 0, 100, 100));
r.SubOut(nsRect(40, 40, 10, 10));
EXPECT_TRUE(r.Contains(0, 0));
EXPECT_TRUE(r.Contains(39, 39));
EXPECT_FALSE(r.Contains(40, 40));
EXPECT_FALSE(r.Contains(49, 49));
EXPECT_TRUE(r.Contains(50, 50));
EXPECT_FALSE(r.Contains(nsRect(40, 40, 10, 10)));
EXPECT_FALSE(r.Contains(nsRect(39, 39, 2, 2)));
}
}
#define DILATE_VALUE 0x88
#define REGION_VALUE 0xff
struct RegionBitmap {
RegionBitmap(unsigned char *bitmap, int width, int height) : bitmap(bitmap), width(width), height(height) {}
void clear() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
bitmap[x + y * width] = 0;
}
}
}
void set(nsRegion ®ion) {
clear();
for (auto iter = region.RectIter(); !iter.Done(); iter.Next()) {
const nsRect& r = iter.Get();
for (int y = r.y; y < r.YMost(); y++) {
for (int x = r.x; x < r.XMost(); x++) {
bitmap[x + y * width] = REGION_VALUE;
}
}
}
}
void dilate() {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (bitmap[x + y * width] == REGION_VALUE) {
for (int yn = max(y - 1, 0); yn <= min(y + 1, height - 1); yn++) {
for (int xn = max(x - 1, 0); xn <= min(x + 1, width - 1); xn++) {
if (bitmap[xn + yn * width] == 0)
bitmap[xn + yn * width] = DILATE_VALUE;
}
}
}
}
}
}
void compare(RegionBitmap &reference) {
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
EXPECT_EQ(bitmap[x + y * width], reference.bitmap[x + y * width]);
}
}
}
unsigned char *bitmap;
int width;
int height;
};
void VisitEdge(void *closure, VisitSide side, int x1, int y1, int x2, int y2)
{
EXPECT_GE(x2, x1);
RegionBitmap *visitor = static_cast<RegionBitmap*>(closure);
unsigned char *bitmap = visitor->bitmap;
const int width = visitor->width;
if (side == VisitSide::TOP) {
while (x1 != x2) {
bitmap[x1 + (y1 - 1) * width] = DILATE_VALUE;
x1++;
}
} else if (side == VisitSide::BOTTOM) {
while (x1 != x2) {
bitmap[x1 + y1 * width] = DILATE_VALUE;
x1++;
}
} else if (side == VisitSide::LEFT) {
while (y1 != y2) {
bitmap[x1 - 1 + y1 *width] = DILATE_VALUE;
y1++;
}
} else if (side == VisitSide::RIGHT) {
while (y1 != y2) {
bitmap[x1 + y1 * width] = DILATE_VALUE;
y1++;
}
}
}
void TestVisit(nsRegion &r)
{
auto reference = mozilla::MakeUnique<unsigned char[]>(600 * 600);
auto result = mozilla::MakeUnique<unsigned char[]>(600 * 600);
RegionBitmap ref(reference.get(), 600, 600);
RegionBitmap res(result.get(), 600, 600);
ref.set(r);
ref.dilate();
res.set(r);
r.VisitEdges(VisitEdge, &res);
res.compare(ref);
}
TEST(Gfx, RegionVisitEdges) {
{ // visit edges
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(20, 120, 200, 100));
TestVisit(r);
}
{ // two rects side by side - 1 pixel inbetween
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(121, 20, 100, 100));
TestVisit(r);
}
{ // two rects side by side - 2 pixels inbetween
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(122, 20, 100, 100));
TestVisit(r);
}
{
// only corner of the rects are touching
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(120, 120, 100, 100));
TestVisit(r);
}
{
// corners are 1 pixel away
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(121, 120, 100, 100));
TestVisit(r);
}
{
// vertically separated
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(120, 125, 100, 100));
TestVisit(r);
}
{
// not touching
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(130, 120, 100, 100));
r.Or(r, nsRect(240, 20, 100, 100));
TestVisit(r);
}
{ // rect with a hole in it
nsRegion r(nsRect(20, 20, 100, 100));
r.Sub(r, nsRect(40, 40, 10, 10));
TestVisit(r);
}
{
// left overs
nsRegion r(nsRect(20, 20, 10, 10));
r.Or(r, nsRect(50, 20, 10, 10));
r.Or(r, nsRect(90, 20, 10, 10));
r.Or(r, nsRect(24, 30, 10, 10));
r.Or(r, nsRect(20, 40, 15, 10));
r.Or(r, nsRect(50, 40, 15, 10));
r.Or(r, nsRect(90, 40, 15, 10));
TestVisit(r);
}
{
// vertically separated
nsRegion r(nsRect(20, 20, 100, 100));
r.Or(r, nsRect(120, 125, 100, 100));
TestVisit(r);
}
{
// two upper rects followed by a lower one
// on the same line
nsRegion r(nsRect(5, 5, 50, 50));
r.Or(r, nsRect(100, 5, 50, 50));
r.Or(r, nsRect(200, 50, 50, 50));
TestVisit(r);
}
{
// bug 1130978.
nsRegion r(nsRect(4, 1, 61, 49));
r.Or(r, nsRect(115, 1, 99, 49));
r.Or(r, nsRect(115, 49, 99, 1));
r.Or(r, nsRect(12, 50, 11, 5));
r.Or(r, nsRect(25, 50, 28, 5));
r.Or(r, nsRect(115, 50, 99, 5));
r.Or(r, nsRect(115, 55, 99, 12));
TestVisit(r);
}
}
TEST(Gfx, PingPongRegion) {
nsRect rects[] = {
nsRect(4, 1, 61, 49),
nsRect(115, 1, 99, 49),
nsRect(115, 49, 99, 1),
nsRect(12, 50, 11, 5),
nsRect(25, 50, 28, 5),
nsRect(115, 50, 99, 5),
nsRect(115, 55, 99, 12),
};
// Test accumulations of various sizes to make sure
// the ping-pong behavior of PingPongRegion is working.
for (size_t size = 0; size < mozilla::ArrayLength(rects); size++) {
// bug 1130978.
nsRegion r;
PingPongRegion<nsRegion> ar;
for (size_t i = 0; i < size; i++) {
r.Or(r, rects[i]);
ar.OrWith(rects[i]);
EXPECT_TRUE(ar.Region().IsEqual(r));
}
for (size_t i = 0; i < size; i++) {
ar.SubOut(rects[i]);
r.SubOut(rects[i]);
EXPECT_TRUE(ar.Region().IsEqual(r));
}
}
}
// The TiledRegion tests use nsIntRect / IntRegion because nsRect doesn't have
// InflateToMultiple which is required by TiledRegion.
TEST(Gfx, TiledRegionNoSimplification2Rects) {
// Add two rectangles, both rectangles are completely inside
// different tiles.
nsIntRegion region;
region.OrWith(nsIntRect(50, 50, 50, 50));
region.OrWith(nsIntRect(300, 50, 50, 50));
TiledIntRegion tiledRegion;
tiledRegion.Add(nsIntRect(50, 50, 50, 50));
tiledRegion.Add(nsIntRect(300, 50, 50, 50));
// No simplification should have happened.
EXPECT_TRUE(region.IsEqual(tiledRegion.GetRegion()));
}
TEST(Gfx, TiledRegionNoSimplification1Region) {
// Add two rectangles, both rectangles are completely inside
// different tiles.
nsIntRegion region;
region.OrWith(nsIntRect(50, 50, 50, 50));
region.OrWith(nsIntRect(300, 50, 50, 50));
TiledIntRegion tiledRegion;
tiledRegion.Add(region);
// No simplification should have happened.
EXPECT_TRUE(region.IsEqual(tiledRegion.GetRegion()));
}
TEST(Gfx, TiledRegionWithSimplification3Rects) {
// Add three rectangles. The first two rectangles are completely inside
// different tiles, but the third rectangle intersects both tiles.
TiledIntRegion tiledRegion;
tiledRegion.Add(nsIntRect(50, 50, 50, 50));
tiledRegion.Add(nsIntRect(300, 50, 50, 50));
tiledRegion.Add(nsIntRect(250, 70, 10, 10));
// Both tiles should have simplified their rectangles, and those two
// rectangles are adjacent to each other, so they just build up one rect.
EXPECT_TRUE(tiledRegion.GetRegion().IsEqual(nsIntRect(50, 50, 300, 50)));
}
TEST(Gfx, TiledRegionWithSimplification1Region) {
// Add three rectangles. The first two rectangles are completely inside
// different tiles, but the third rectangle intersects both tiles.
nsIntRegion region;
region.OrWith(nsIntRect(50, 50, 50, 50));
region.OrWith(nsIntRect(300, 50, 50, 50));
region.OrWith(nsIntRect(250, 70, 10, 10));
TiledIntRegion tiledRegion;
tiledRegion.Add(region);
// Both tiles should have simplified their rectangles, and those two
// rectangles are adjacent to each other, so they just build up one rect.
EXPECT_TRUE(tiledRegion.GetRegion().IsEqual(nsIntRect(50, 50, 300, 50)));
}
TEST(Gfx, TiledRegionContains) {
// Add three rectangles. The first two rectangles are completely inside
// different tiles, but the third rectangle intersects both tiles.
TiledIntRegion tiledRegion;
tiledRegion.Add(nsIntRect(50, 50, 50, 50));
tiledRegion.Add(nsIntRect(300, 50, 50, 50));
tiledRegion.Add(nsIntRect(250, 70, 10, 10));
// Both tiles should have simplified their rectangles, and those two
// rectangles are adjacent to each other, so they just build up one rect.
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(50, 50, 300, 50)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(50, 50, 50, 50)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(50, 50, 301, 50)));
}
TEST(Gfx, TiledRegionIntersects) {
// Add three rectangles. The first two rectangles are completely inside
// different tiles, but the third rectangle intersects both tiles.
TiledIntRegion tiledRegion;
tiledRegion.Add(nsIntRect(50, 50, 50, 50));
tiledRegion.Add(nsIntRect(300, 50, 50, 50));
tiledRegion.Add(nsIntRect(250, 70, 10, 10));
// Both tiles should have simplified their rectangles, and those two
// rectangles are adjacent to each other, so they just build up one rect.
EXPECT_TRUE(tiledRegion.Intersects(nsIntRect(50, 50, 300, 50)));
EXPECT_TRUE(tiledRegion.Intersects(nsIntRect(200, 10, 10, 50)));
EXPECT_TRUE(tiledRegion.Intersects(nsIntRect(50, 50, 301, 50)));
EXPECT_FALSE(tiledRegion.Intersects(nsIntRect(0, 0, 50, 500)));
}
TEST(Gfx, TiledRegionBoundaryConditions1) {
TiledIntRegion tiledRegion;
// This one works fine
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MIN, INT_MIN, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(INT_MIN, INT_MIN, 1, 1)));
// This causes the tiledRegion.mBounds to overflow, so it is ignored
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MAX - 1, INT_MAX - 1, 1, 1)));
// Verify that the tiledRegion contains only things we expect
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(INT_MIN, INT_MIN, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MAX - 1, INT_MAX - 1, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(0, 0, 1, 1)));
}
TEST(Gfx, TiledRegionBoundaryConditions2) {
TiledIntRegion tiledRegion;
// This one works fine
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MAX - 1, INT_MIN, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(INT_MAX - 1, INT_MIN, 1, 1)));
// As with TiledRegionBoundaryConditions1, this overflows, so it is ignored
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MIN, INT_MAX - 1, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(INT_MAX - 1, INT_MIN, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MIN, INT_MAX - 1, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(0, 0, 1, 1)));
}
TEST(Gfx, TiledRegionBigRects) {
TiledIntRegion tiledRegion;
// Super wide region, forces simplification into bounds mode
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MIN, INT_MIN, INT_MAX, 100)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(INT_MIN, INT_MIN, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(-2, INT_MIN + 99, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(-2, INT_MIN + 100, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(-1, INT_MIN + 99, 1, 1)));
// Add another rect, verify that simplification caused the entire bounds
// to expand by a lot more.
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MIN, INT_MIN + 200, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(-2, INT_MIN + 100, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(-2, INT_MIN + 200, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(-2, INT_MIN + 201, 1, 1)));
}
TEST(Gfx, TiledRegionBoundaryOverflow) {
TiledIntRegion tiledRegion;
tiledRegion.Add(nsIntRegion(nsIntRect(100, 100, 1, 1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(100, 100, 1, 1)));
// The next region is invalid, so it gets ignored
tiledRegion.Add(nsIntRegion(nsIntRect(INT_MAX, INT_MAX, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MAX, INT_MAX, 1, 1)));
// Try that again as a rect, it will also get ignored
tiledRegion.Add(nsIntRect(INT_MAX, INT_MAX, 1, 1));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MAX, INT_MAX, 1, 1)));
// Try with a bigger overflowing rect
tiledRegion.Add(nsIntRect(INT_MAX, INT_MAX, 500, 500));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MIN, INT_MIN, 10, 10)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(INT_MAX, INT_MAX, 100, 100)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(0, 0, 1, 1)));
}
TEST(Gfx, TiledRegionNegativeRect) {
TiledIntRegion tiledRegion;
// The next region is invalid, so it gets ignored
tiledRegion.Add(nsIntRegion(nsIntRect(0, 0, -500, -500)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(-50, -50, 1, 1)));
// Rects with negative widths/heights are treated as empty and ignored
tiledRegion.Add(nsIntRect(0, 0, -500, -500));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(-1, -1, 1, 1)));
EXPECT_FALSE(tiledRegion.Contains(nsIntRect(0, 0, 1, 1)));
// Empty rects are always contained
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(0, 0, -1, -1)));
EXPECT_TRUE(tiledRegion.Contains(nsIntRect(100, 100, -1, -1)));
}
MOZ_GTEST_BENCH(GfxBench, RegionOr, []{
const int size = 5000;
nsRegion r;
for (int i = 0; i < size; i++) {
r = r.Or(r, nsRect(i, i, i + 10, i + 10));
}
nsIntRegion rInt;
for (int i = 0; i < size; i++) {
rInt = rInt.Or(rInt, nsIntRect(i, i, i + 10, i + 10));
}
});
MOZ_GTEST_BENCH(GfxBench, RegionAnd, []{
const int size = 5000;
nsRegion r(nsRect(0, 0, size, size));
for (int i = 0; i < size; i++) {
nsRegion rMissingPixel(nsRect(0, 0, size, size));
rMissingPixel = rMissingPixel.Sub(rMissingPixel, nsRect(i, i, 1, 1));
r = r.And(r, rMissingPixel);
}
});
void BenchRegionBuilderOr() {
const int size = 5000;
RegionBuilder<nsRegion> r;
for (int i = 0; i < size; i++) {
r.OrWith(nsRect(i, i, i + 10, i + 10));
}
r.ToRegion();
RegionBuilder<nsIntRegion> rInt;
for (int i = 0; i < size; i++) {
rInt.OrWith(nsIntRect(i, i, i + 10, i + 10));
}
rInt.ToRegion();
}
MOZ_GTEST_BENCH(GfxBench, RegionBuilderOr, []{
BenchRegionBuilderOr();
});
void BenchPingPongRegionOr() {
const int size = 5000;
PingPongRegion<nsRegion> r;
for (int i = 0; i < size; i++) {
r.OrWith(nsRect(i, i, i + 10, i + 10));
}
r.Region();
PingPongRegion<nsIntRegion> rInt;
for (int i = 0; i < size; i++) {
rInt.OrWith(nsIntRect(i, i, i + 10, i + 10));
}
rInt.Region();
}
MOZ_GTEST_BENCH(GfxBench, PingPongRegionOr, []{
BenchPingPongRegionOr();
});
|