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
|
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/resources/picture_layer_tiling.h"
#include "cc/resources/picture_layer_tiling_set.h"
#include "cc/test/fake_picture_layer_tiling_client.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/rect_conversions.h"
#include "ui/gfx/size_conversions.h"
namespace cc {
namespace {
class TestablePictureLayerTiling : public PictureLayerTiling {
public:
using PictureLayerTiling::SetLiveTilesRect;
static scoped_ptr<TestablePictureLayerTiling> Create(
float contents_scale,
gfx::Size layer_bounds,
PictureLayerTilingClient* client) {
return make_scoped_ptr(new TestablePictureLayerTiling(
contents_scale,
layer_bounds,
client));
}
protected:
TestablePictureLayerTiling(float contents_scale,
gfx::Size layer_bounds,
PictureLayerTilingClient* client)
: PictureLayerTiling(contents_scale, layer_bounds, client) { }
};
class PictureLayerTilingIteratorTest : public testing::Test {
public:
PictureLayerTilingIteratorTest() {}
virtual ~PictureLayerTilingIteratorTest() {}
void Initialize(gfx::Size tile_size,
float contents_scale,
gfx::Size layer_bounds) {
client_.SetTileSize(tile_size);
tiling_ = TestablePictureLayerTiling::Create(contents_scale,
layer_bounds,
&client_);
}
void SetLiveRectAndVerifyTiles(gfx::Rect live_tiles_rect) {
tiling_->SetLiveTilesRect(live_tiles_rect);
std::vector<Tile*> tiles = tiling_->AllTilesForTesting();
for (std::vector<Tile*>::iterator iter = tiles.begin();
iter != tiles.end();
++iter) {
EXPECT_TRUE(live_tiles_rect.Intersects((*iter)->content_rect()));
}
}
void VerifyTilesExactlyCoverRect(
float rect_scale,
gfx::Rect request_rect,
gfx::Rect expect_rect) {
EXPECT_TRUE(request_rect.Contains(expect_rect));
// Iterators are not valid if this ratio is too large (i.e. the
// tiling is too high-res for a low-res destination rect.) This is an
// artifact of snapping geometry to integer coordinates and then mapping
// back to floating point texture coordinates.
float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
ASSERT_LE(dest_to_contents_scale, 2.0);
Region remaining = expect_rect;
for (PictureLayerTiling::CoverageIterator
iter(tiling_.get(), rect_scale, request_rect);
iter;
++iter) {
// Geometry cannot overlap previous geometry at all
gfx::Rect geometry = iter.geometry_rect();
EXPECT_TRUE(expect_rect.Contains(geometry));
EXPECT_TRUE(remaining.Contains(geometry));
remaining.Subtract(geometry);
// Sanity check that texture coords are within the texture rect.
gfx::RectF texture_rect = iter.texture_rect();
EXPECT_GE(texture_rect.x(), 0);
EXPECT_GE(texture_rect.y(), 0);
EXPECT_LE(texture_rect.right(), client_.TileSize().width());
EXPECT_LE(texture_rect.bottom(), client_.TileSize().height());
EXPECT_EQ(iter.texture_size(), client_.TileSize());
}
// The entire rect must be filled by geometry from the tiling.
EXPECT_TRUE(remaining.IsEmpty());
}
void VerifyTilesExactlyCoverRect(float rect_scale, gfx::Rect rect) {
VerifyTilesExactlyCoverRect(rect_scale, rect, rect);
}
void VerifyTiles(
float rect_scale,
gfx::Rect rect,
base::Callback<void(Tile* tile, gfx::Rect geometry_rect)> callback) {
VerifyTiles(tiling_.get(),
rect_scale,
rect,
callback);
}
void VerifyTiles(
PictureLayerTiling* tiling,
float rect_scale,
gfx::Rect rect,
base::Callback<void(Tile* tile, gfx::Rect geometry_rect)> callback) {
Region remaining = rect;
for (PictureLayerTiling::CoverageIterator iter(tiling, rect_scale, rect);
iter;
++iter) {
remaining.Subtract(iter.geometry_rect());
callback.Run(*iter, iter.geometry_rect());
}
EXPECT_TRUE(remaining.IsEmpty());
}
void VerifyTilesCoverNonContainedRect(float rect_scale, gfx::Rect dest_rect) {
float dest_to_contents_scale = tiling_->contents_scale() / rect_scale;
gfx::Rect clamped_rect = gfx::ScaleToEnclosingRect(
tiling_->ContentRect(), 1.f / dest_to_contents_scale);
clamped_rect.Intersect(dest_rect);
VerifyTilesExactlyCoverRect(rect_scale, dest_rect, clamped_rect);
}
protected:
FakePictureLayerTilingClient client_;
scoped_ptr<TestablePictureLayerTiling> tiling_;
private:
DISALLOW_COPY_AND_ASSIGN(PictureLayerTilingIteratorTest);
};
TEST_F(PictureLayerTilingIteratorTest, LiveTilesExactlyCoverLiveTileRect) {
Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
SetLiveRectAndVerifyTiles(gfx::Rect(100, 100));
SetLiveRectAndVerifyTiles(gfx::Rect(101, 99));
SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
SetLiveRectAndVerifyTiles(gfx::Rect(1, 801));
SetLiveRectAndVerifyTiles(gfx::Rect(1099, 1));
SetLiveRectAndVerifyTiles(gfx::Rect(201, 800));
}
TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsNoScale) {
Initialize(gfx::Size(100, 100), 1, gfx::Size(1099, 801));
VerifyTilesExactlyCoverRect(1, gfx::Rect());
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1099, 801));
VerifyTilesExactlyCoverRect(1, gfx::Rect(52, 83, 789, 412));
// With borders, a size of 3x3 = 1 pixel of content.
Initialize(gfx::Size(3, 3), 1, gfx::Size(10, 10));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
}
TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsTilingScale) {
Initialize(gfx::Size(200, 100), 2.0f, gfx::Size(1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect());
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
Initialize(gfx::Size(3, 3), 2.0f, gfx::Size(10, 10));
VerifyTilesExactlyCoverRect(1, gfx::Rect());
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1, 1));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 2, 2));
VerifyTilesExactlyCoverRect(1, gfx::Rect(1, 1, 2, 2));
VerifyTilesExactlyCoverRect(1, gfx::Rect(3, 2, 5, 2));
Initialize(gfx::Size(100, 200), 0.5f, gfx::Size(1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
Initialize(gfx::Size(150, 250), 0.37f, gfx::Size(1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
Initialize(gfx::Size(312, 123), 0.01f, gfx::Size(1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(0, 0, 1005, 2010));
VerifyTilesExactlyCoverRect(1, gfx::Rect(50, 112, 512, 381));
}
TEST_F(PictureLayerTilingIteratorTest, IteratorCoversLayerBoundsBothScale) {
Initialize(gfx::Size(50, 50), 4.0f, gfx::Size(800, 600));
VerifyTilesExactlyCoverRect(2.0f, gfx::Rect());
VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(0, 0, 1600, 1200));
VerifyTilesExactlyCoverRect(2.0f, gfx::Rect(512, 365, 253, 182));
float scale = 6.7f;
gfx::Size bounds(800, 600);
gfx::Rect full_rect(gfx::ToCeiledSize(gfx::ScaleSize(bounds, scale)));
Initialize(gfx::Size(256, 512), 5.2f, bounds);
VerifyTilesExactlyCoverRect(scale, full_rect);
VerifyTilesExactlyCoverRect(scale, gfx::Rect(2014, 1579, 867, 1033));
}
TEST_F(PictureLayerTilingIteratorTest, IteratorEmptyRect) {
Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
gfx::Rect empty;
PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1.0f, empty);
EXPECT_FALSE(iter);
}
TEST_F(PictureLayerTilingIteratorTest, NonIntersectingRect) {
Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(800, 600));
gfx::Rect non_intersecting(1000, 1000, 50, 50);
PictureLayerTiling::CoverageIterator iter(tiling_.get(), 1, non_intersecting);
EXPECT_FALSE(iter);
}
TEST_F(PictureLayerTilingIteratorTest, LayerEdgeTextureCoordinates) {
Initialize(gfx::Size(300, 300), 1.0f, gfx::Size(256, 256));
// All of these sizes are 256x256, scaled and ceiled.
VerifyTilesExactlyCoverRect(1.0f, gfx::Rect(0, 0, 256, 256));
VerifyTilesExactlyCoverRect(0.8f, gfx::Rect(0, 0, 205, 205));
VerifyTilesExactlyCoverRect(1.2f, gfx::Rect(0, 0, 308, 308));
}
TEST_F(PictureLayerTilingIteratorTest, NonContainedDestRect) {
Initialize(gfx::Size(100, 100), 1.0f, gfx::Size(400, 400));
// Too large in all dimensions
VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, -1000, 2000, 2000));
VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, -1000, 2000, 2000));
VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, -1000, 2000, 2000));
// Partially covering content, but too large
VerifyTilesCoverNonContainedRect(1.0f, gfx::Rect(-1000, 100, 2000, 100));
VerifyTilesCoverNonContainedRect(1.5f, gfx::Rect(-1000, 100, 2000, 100));
VerifyTilesCoverNonContainedRect(0.5f, gfx::Rect(-1000, 100, 2000, 100));
}
TEST(PictureLayerTilingTest, ExpandRectEqual) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, -1000, 10000, 10000);
int64 target_area = 100 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(in.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectSmaller) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, -1000, 10000, 10000);
int64 target_area = 100 * 100;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
EXPECT_NEAR(100 * 100, out.width() * out.height(), 50);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectUnbounded) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, -1000, 10000, 10000);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
EXPECT_EQ(out.width() - in.width(), out.height() - in.height());
EXPECT_NEAR(200 * 200, out.width() * out.height(), 100);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectBoundedSmaller) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(50, 60, 40, 30);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedEqual) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds = in;
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchVertical) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(45, 0, 90, 300);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchVertical) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(40, 0, 100, 300);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedSmallerStretchHorizontal) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(0, 55, 180, 190);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedEqualStretchHorizontal) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(0, 50, 180, 200);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectBoundedLeft) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(20, -1000, 10000, 10000);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
EXPECT_EQ(out.bottom() - in.bottom(), out.right() - in.right());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() - out.height() * 2);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectBoundedRight) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, -1000, 1000+120, 10000);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
EXPECT_EQ(out.bottom() - in.bottom(), in.x() - out.x());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() - out.height() * 2);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectBoundedTop) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, 30, 10000, 10000);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
EXPECT_EQ(out.right() - in.right(), out.bottom() - in.bottom());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() * 2 - out.height());
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectBoundedBottom) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-1000, -1000, 10000, 1000 + 220);
int64 target_area = 200 * 200;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
EXPECT_EQ(out.right() - in.right(), in.y() - out.y());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() * 2 - out.height());
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectSquishedHorizontally) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(0, -4000, 100+40+20, 100000);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(20, out.right() - in.right());
EXPECT_EQ(40, in.x() - out.x());
EXPECT_EQ(out.bottom() - in.bottom(), in.y() - out.y());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() * 2);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectSquishedVertically) {
gfx::Rect in(40, 50, 100, 200);
gfx::Rect bounds(-4000, 0, 100000, 200+50+30);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(30, out.bottom() - in.bottom());
EXPECT_EQ(50, in.y() - out.y());
EXPECT_EQ(out.right() - in.right(), in.x() - out.x());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.height() * 2);
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsFarAway) {
gfx::Rect in(400, 500, 100, 200);
gfx::Rect bounds(0, 0, 10, 10);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_TRUE(out.IsEmpty());
}
TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedFullyCover) {
gfx::Rect in(40, 50, 100, 100);
gfx::Rect bounds(0, 0, 10, 10);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.ToString(), out.ToString());
}
TEST(PictureLayerTilingTest, ExpandRectOutOfBoundsExpandedPartlyCover) {
gfx::Rect in(600, 600, 100, 100);
gfx::Rect bounds(0, 0, 500, 500);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_EQ(bounds.right(), out.right());
EXPECT_EQ(bounds.bottom(), out.bottom());
EXPECT_LE(out.width() * out.height(), target_area);
EXPECT_GT(out.width() * out.height(),
target_area - out.width() - out.height());
EXPECT_TRUE(bounds.Contains(out));
}
TEST(PictureLayerTilingTest, EmptyStartingRect) {
// If a layer has a non-invertible transform, then the starting rect
// for the layer would be empty.
gfx::Rect in(40, 40, 0, 0);
gfx::Rect bounds(0, 0, 10, 10);
int64 target_area = 400 * 400;
gfx::Rect out = PictureLayerTiling::ExpandRectEquallyToAreaBoundedBy(
in, target_area, bounds);
EXPECT_TRUE(out.IsEmpty());
}
static void TileExists(bool exists, Tile* tile, gfx::Rect geometry_rect) {
EXPECT_EQ(exists, tile != NULL) << geometry_rect.ToString();
}
TEST_F(PictureLayerTilingIteratorTest, TilesExist) {
gfx::Size layer_bounds(1099, 801);
Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
gfx::Rect(layer_bounds), // viewport in layer space
gfx::Rect(layer_bounds), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
// Make the viewport rect empty. All tiles are killed and become zombies.
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
gfx::Rect(), // viewport in layer space
gfx::Rect(), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
2.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
}
TEST_F(PictureLayerTilingIteratorTest, TilesExistGiantViewport) {
gfx::Size layer_bounds(1099, 801);
Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
gfx::Rect giant_rect(-10000000, -10000000, 1000000000, 1000000000);
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
giant_rect, // viewport in layer space
gfx::Rect(layer_bounds), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
// If the visible content rect is empty, it should still have live tiles.
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
giant_rect, // viewport in layer space
gfx::Rect(), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
2.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
}
TEST_F(PictureLayerTilingIteratorTest, TilesExistOutsideViewport) {
gfx::Size layer_bounds(1099, 801);
Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
// This rect does not intersect with the layer, as the layer is outside the
// viewport.
gfx::Rect viewport_rect(1100, 0, 1000, 1000);
EXPECT_FALSE(viewport_rect.Intersects(gfx::Rect(layer_bounds)));
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
viewport_rect, // viewport in layer space
gfx::Rect(), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, true));
}
static void TilesIntersectingRectExist(gfx::Rect rect,
bool intersect_exists,
Tile* tile,
gfx::Rect geometry_rect) {
bool intersects = rect.Intersects(geometry_rect);
bool expected_exists = intersect_exists ? intersects : !intersects;
EXPECT_EQ(expected_exists, tile != NULL)
<< "Rects intersecting " << rect.ToString() << " should exist. "
<< "Current tile rect is " << geometry_rect.ToString();
}
TEST_F(PictureLayerTilingIteratorTest,
TilesExistLargeViewportAndLayerWithSmallVisibleArea) {
gfx::Size layer_bounds(10000, 10000);
Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
gfx::Rect visible_rect(8000, 8000, 50, 50);
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
gfx::Rect(layer_bounds), // viewport in layer space
visible_rect, // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
1); // max tiles in tile manager
VerifyTiles(1.f,
gfx::Rect(layer_bounds),
base::Bind(&TilesIntersectingRectExist, visible_rect, true));
}
static void CountExistingTiles(int *count,
Tile* tile,
gfx::Rect geometry_rect) {
if (tile != NULL)
++(*count);
}
TEST_F(PictureLayerTilingIteratorTest,
TilesExistLargeViewportAndLayerWithLargeVisibleArea) {
gfx::Size layer_bounds(10000, 10000);
Initialize(gfx::Size(100, 100), 1.f, layer_bounds);
VerifyTilesExactlyCoverRect(1.f, gfx::Rect(layer_bounds));
VerifyTiles(1.f, gfx::Rect(layer_bounds), base::Bind(&TileExists, false));
tiling_->UpdateTilePriorities(
ACTIVE_TREE,
layer_bounds, // device viewport
gfx::Rect(layer_bounds), // viewport in layer space
gfx::Rect(layer_bounds), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
1); // max tiles in tile manager
int num_tiles = 0;
VerifyTiles(1.f,
gfx::Rect(layer_bounds),
base::Bind(&CountExistingTiles, &num_tiles));
// If we're making a rect the size of one tile, it can only overlap up to 4
// tiles depending on its position.
EXPECT_LE(num_tiles, 4);
VerifyTiles(1.f, gfx::Rect(), base::Bind(&TileExists, false));
}
TEST_F(PictureLayerTilingIteratorTest, AddTilingsToMatchScale) {
gfx::Size layer_bounds(1099, 801);
gfx::Size tile_size(100, 100);
client_.SetTileSize(tile_size);
PictureLayerTilingSet active_set(&client_, layer_bounds);
active_set.AddTiling(1.f);
VerifyTiles(active_set.tiling_at(0),
1.f,
gfx::Rect(layer_bounds),
base::Bind(&TileExists, false));
active_set.UpdateTilePriorities(
PENDING_TREE,
layer_bounds, // device viewport
gfx::Rect(layer_bounds), // viewport in layer space
gfx::Rect(layer_bounds), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
10000); // max tiles in tile manager
// The active tiling has tiles now.
VerifyTiles(active_set.tiling_at(0),
1.f,
gfx::Rect(layer_bounds),
base::Bind(&TileExists, true));
// Add the same tilings to the pending set.
PictureLayerTilingSet pending_set(&client_, layer_bounds);
pending_set.AddTilingsToMatchScales(active_set, 0.f);
// The pending tiling starts with no tiles.
VerifyTiles(pending_set.tiling_at(0),
1.f,
gfx::Rect(layer_bounds),
base::Bind(&TileExists, false));
// UpdateTilePriorities on the pending tiling at the same frame time. The
// pending tiling should get tiles.
pending_set.UpdateTilePriorities(
PENDING_TREE,
layer_bounds, // device viewport
gfx::Rect(layer_bounds), // viewport in layer space
gfx::Rect(layer_bounds), // visible content rect
layer_bounds, // last layer bounds
layer_bounds, // current layer bounds
1.f, // last contents scale
1.f, // current contents scale
gfx::Transform(), // last screen transform
gfx::Transform(), // current screen transform
1.0, // current frame time
10000); // max tiles in tile manager
VerifyTiles(pending_set.tiling_at(0),
1.f,
gfx::Rect(layer_bounds),
base::Bind(&TileExists, true));
}
} // namespace
} // namespace cc
|