Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / cc / tiles / picture_layer_tiling_set_unittest.cc
blob1e9a436b66dfb5b8d6fcf2bdd364112c0137aa4d
1 // Copyright 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/tiles/picture_layer_tiling_set.h"
7 #include <map>
8 #include <vector>
10 #include "cc/resources/resource_provider.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/fake_output_surface_client.h"
13 #include "cc/test/fake_picture_layer_tiling_client.h"
14 #include "cc/test/fake_picture_pile_impl.h"
15 #include "cc/test/fake_resource_provider.h"
16 #include "cc/test/test_shared_bitmap_manager.h"
17 #include "cc/trees/layer_tree_settings.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/gfx/geometry/size_conversions.h"
21 namespace cc {
22 namespace {
24 scoped_ptr<PictureLayerTilingSet> CreateTilingSet(
25 PictureLayerTilingClient* client) {
26 LayerTreeSettings defaults;
27 return PictureLayerTilingSet::Create(
28 ACTIVE_TREE, client, defaults.tiling_interest_area_padding,
29 defaults.skewport_target_time_in_seconds,
30 defaults.skewport_extrapolation_limit_in_content_pixels);
33 TEST(PictureLayerTilingSetTest, NoResources) {
34 FakePictureLayerTilingClient client;
35 gfx::Size layer_bounds(1000, 800);
36 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
37 client.SetTileSize(gfx::Size(256, 256));
39 scoped_refptr<FakePicturePileImpl> pile =
40 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
42 set->AddTiling(1.0, pile);
43 set->AddTiling(1.5, pile);
44 set->AddTiling(2.0, pile);
46 float contents_scale = 2.0;
47 gfx::Size content_bounds(
48 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
49 gfx::Rect content_rect(content_bounds);
51 Region remaining(content_rect);
52 PictureLayerTilingSet::CoverageIterator iter(set.get(), contents_scale,
53 content_rect, contents_scale);
54 for (; iter; ++iter) {
55 gfx::Rect geometry_rect = iter.geometry_rect();
56 EXPECT_TRUE(content_rect.Contains(geometry_rect));
57 ASSERT_TRUE(remaining.Contains(geometry_rect));
58 remaining.Subtract(geometry_rect);
60 // No tiles have resources, so no iter represents a real tile.
61 EXPECT_FALSE(*iter);
63 EXPECT_TRUE(remaining.IsEmpty());
66 TEST(PictureLayerTilingSetTest, TilingRange) {
67 FakePictureLayerTilingClient client;
68 gfx::Size layer_bounds(10, 10);
69 PictureLayerTilingSet::TilingRange higher_than_high_res_range(0, 0);
70 PictureLayerTilingSet::TilingRange high_res_range(0, 0);
71 PictureLayerTilingSet::TilingRange between_high_and_low_res_range(0, 0);
72 PictureLayerTilingSet::TilingRange low_res_range(0, 0);
73 PictureLayerTilingSet::TilingRange lower_than_low_res_range(0, 0);
74 PictureLayerTiling* high_res_tiling;
75 PictureLayerTiling* low_res_tiling;
77 scoped_refptr<FakePicturePileImpl> pile =
78 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
80 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
81 set->AddTiling(2.0, pile);
82 high_res_tiling = set->AddTiling(1.0, pile);
83 high_res_tiling->set_resolution(HIGH_RESOLUTION);
84 set->AddTiling(0.5, pile);
85 low_res_tiling = set->AddTiling(0.25, pile);
86 low_res_tiling->set_resolution(LOW_RESOLUTION);
87 set->AddTiling(0.125, pile);
89 higher_than_high_res_range =
90 set->GetTilingRange(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
91 EXPECT_EQ(0u, higher_than_high_res_range.start);
92 EXPECT_EQ(1u, higher_than_high_res_range.end);
94 high_res_range = set->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
95 EXPECT_EQ(1u, high_res_range.start);
96 EXPECT_EQ(2u, high_res_range.end);
98 between_high_and_low_res_range =
99 set->GetTilingRange(PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
100 EXPECT_EQ(2u, between_high_and_low_res_range.start);
101 EXPECT_EQ(3u, between_high_and_low_res_range.end);
103 low_res_range = set->GetTilingRange(PictureLayerTilingSet::LOW_RES);
104 EXPECT_EQ(3u, low_res_range.start);
105 EXPECT_EQ(4u, low_res_range.end);
107 lower_than_low_res_range =
108 set->GetTilingRange(PictureLayerTilingSet::LOWER_THAN_LOW_RES);
109 EXPECT_EQ(4u, lower_than_low_res_range.start);
110 EXPECT_EQ(5u, lower_than_low_res_range.end);
112 scoped_ptr<PictureLayerTilingSet> set_without_low_res =
113 CreateTilingSet(&client);
114 set_without_low_res->AddTiling(2.0, pile);
115 high_res_tiling = set_without_low_res->AddTiling(1.0, pile);
116 high_res_tiling->set_resolution(HIGH_RESOLUTION);
117 set_without_low_res->AddTiling(0.5, pile);
118 set_without_low_res->AddTiling(0.25, pile);
120 higher_than_high_res_range = set_without_low_res->GetTilingRange(
121 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
122 EXPECT_EQ(0u, higher_than_high_res_range.start);
123 EXPECT_EQ(1u, higher_than_high_res_range.end);
125 high_res_range =
126 set_without_low_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
127 EXPECT_EQ(1u, high_res_range.start);
128 EXPECT_EQ(2u, high_res_range.end);
130 between_high_and_low_res_range = set_without_low_res->GetTilingRange(
131 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
132 EXPECT_EQ(2u, between_high_and_low_res_range.start);
133 EXPECT_EQ(4u, between_high_and_low_res_range.end);
135 low_res_range =
136 set_without_low_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
137 EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
139 lower_than_low_res_range = set_without_low_res->GetTilingRange(
140 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
141 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
143 scoped_ptr<PictureLayerTilingSet> set_with_only_high_and_low_res =
144 CreateTilingSet(&client);
145 high_res_tiling = set_with_only_high_and_low_res->AddTiling(1.0, pile);
146 high_res_tiling->set_resolution(HIGH_RESOLUTION);
147 low_res_tiling = set_with_only_high_and_low_res->AddTiling(0.5, pile);
148 low_res_tiling->set_resolution(LOW_RESOLUTION);
150 higher_than_high_res_range = set_with_only_high_and_low_res->GetTilingRange(
151 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
152 EXPECT_EQ(0u,
153 higher_than_high_res_range.end - higher_than_high_res_range.start);
155 high_res_range = set_with_only_high_and_low_res->GetTilingRange(
156 PictureLayerTilingSet::HIGH_RES);
157 EXPECT_EQ(0u, high_res_range.start);
158 EXPECT_EQ(1u, high_res_range.end);
160 between_high_and_low_res_range =
161 set_with_only_high_and_low_res->GetTilingRange(
162 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
163 EXPECT_EQ(0u, between_high_and_low_res_range.end -
164 between_high_and_low_res_range.start);
166 low_res_range = set_with_only_high_and_low_res->GetTilingRange(
167 PictureLayerTilingSet::LOW_RES);
168 EXPECT_EQ(1u, low_res_range.start);
169 EXPECT_EQ(2u, low_res_range.end);
171 lower_than_low_res_range = set_with_only_high_and_low_res->GetTilingRange(
172 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
173 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
175 scoped_ptr<PictureLayerTilingSet> set_with_only_high_res =
176 CreateTilingSet(&client);
177 high_res_tiling = set_with_only_high_res->AddTiling(1.0, pile);
178 high_res_tiling->set_resolution(HIGH_RESOLUTION);
180 higher_than_high_res_range = set_with_only_high_res->GetTilingRange(
181 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
182 EXPECT_EQ(0u,
183 higher_than_high_res_range.end - higher_than_high_res_range.start);
185 high_res_range =
186 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
187 EXPECT_EQ(0u, high_res_range.start);
188 EXPECT_EQ(1u, high_res_range.end);
190 between_high_and_low_res_range = set_with_only_high_res->GetTilingRange(
191 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
192 EXPECT_EQ(0u, between_high_and_low_res_range.end -
193 between_high_and_low_res_range.start);
195 low_res_range =
196 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
197 EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
199 lower_than_low_res_range = set_with_only_high_res->GetTilingRange(
200 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
201 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
204 class PictureLayerTilingSetTestWithResources : public testing::Test {
205 public:
206 void RunTest(int num_tilings,
207 float min_scale,
208 float scale_increment,
209 float ideal_contents_scale,
210 float expected_scale) {
211 FakeOutputSurfaceClient output_surface_client;
212 scoped_ptr<FakeOutputSurface> output_surface =
213 FakeOutputSurface::Create3d();
214 CHECK(output_surface->BindToClient(&output_surface_client));
216 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
217 new TestSharedBitmapManager());
218 scoped_ptr<ResourceProvider> resource_provider =
219 FakeResourceProvider::Create(output_surface.get(),
220 shared_bitmap_manager.get());
222 FakePictureLayerTilingClient client(resource_provider.get());
223 client.SetTileSize(gfx::Size(256, 256));
224 gfx::Size layer_bounds(1000, 800);
225 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
226 scoped_refptr<FakePicturePileImpl> pile =
227 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
229 float scale = min_scale;
230 for (int i = 0; i < num_tilings; ++i, scale += scale_increment) {
231 PictureLayerTiling* tiling = set->AddTiling(scale, pile);
232 tiling->set_resolution(HIGH_RESOLUTION);
233 tiling->CreateAllTilesForTesting();
234 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
235 client.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
238 float max_contents_scale = scale;
239 gfx::Size content_bounds(
240 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, max_contents_scale)));
241 gfx::Rect content_rect(content_bounds);
243 Region remaining(content_rect);
244 PictureLayerTilingSet::CoverageIterator iter(
245 set.get(), max_contents_scale, content_rect, ideal_contents_scale);
246 for (; iter; ++iter) {
247 gfx::Rect geometry_rect = iter.geometry_rect();
248 EXPECT_TRUE(content_rect.Contains(geometry_rect));
249 ASSERT_TRUE(remaining.Contains(geometry_rect));
250 remaining.Subtract(geometry_rect);
252 float scale = iter.CurrentTiling()->contents_scale();
253 EXPECT_EQ(expected_scale, scale);
255 if (num_tilings)
256 EXPECT_TRUE(*iter);
257 else
258 EXPECT_FALSE(*iter);
260 EXPECT_TRUE(remaining.IsEmpty());
264 TEST_F(PictureLayerTilingSetTestWithResources, NoTilings) {
265 RunTest(0, 0.f, 0.f, 2.f, 0.f);
267 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Smaller) {
268 RunTest(1, 1.f, 0.f, 2.f, 1.f);
270 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Larger) {
271 RunTest(1, 3.f, 0.f, 2.f, 3.f);
273 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Smaller) {
274 RunTest(2, 1.f, 1.f, 3.f, 2.f);
277 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_SmallerEqual) {
278 RunTest(2, 1.f, 1.f, 2.f, 2.f);
281 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_LargerEqual) {
282 RunTest(2, 1.f, 1.f, 1.f, 1.f);
285 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Larger) {
286 RunTest(2, 2.f, 8.f, 1.f, 2.f);
289 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_Equal) {
290 RunTest(10, 1.f, 1.f, 5.f, 5.f);
293 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_NotEqual) {
294 RunTest(10, 1.f, 1.f, 4.5f, 5.f);
297 TEST(PictureLayerTilingSetTest, TileSizeChange) {
298 FakePictureLayerTilingClient pending_client;
299 FakePictureLayerTilingClient active_client;
300 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
301 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
302 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
303 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
305 gfx::Size layer_bounds(100, 100);
306 scoped_refptr<FakePicturePileImpl> pile =
307 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
309 gfx::Size tile_size1(10, 10);
310 gfx::Size tile_size2(30, 30);
311 gfx::Size tile_size3(20, 20);
313 pending_client.SetTileSize(tile_size1);
314 pending_set->AddTiling(1.f, pile);
315 // New tilings get the correct tile size.
316 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
318 // Set some expected things for the tiling set to function.
319 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
320 active_client.set_twin_tiling_set(pending_set.get());
322 // Set a priority rect so we get tiles.
323 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
324 Occlusion(), false);
325 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
327 // The tiles should get the correct size.
328 std::vector<Tile*> pending_tiles =
329 pending_set->tiling_at(0)->AllTilesForTesting();
330 EXPECT_GT(pending_tiles.size(), 0u);
331 for (const auto& tile : pending_tiles)
332 EXPECT_EQ(tile_size1, tile->content_rect().size());
334 // Update to a new source frame with a new tile size.
335 // Note that setting a new raster source can typically only happen after
336 // activation, since we can't set the raster source twice on the pending tree
337 // without activating. For test, just remove and add a new tiling instead.
338 pending_set->RemoveAllTilings();
339 pending_set->AddTiling(1.f, pile);
340 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
341 pending_client.SetTileSize(tile_size2);
342 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
343 1.f, 1.f);
344 // The tiling should get the correct tile size.
345 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
347 // Set a priority rect so we get tiles.
348 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 2.0,
349 Occlusion(), false);
350 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
352 // Tiles should have the new correct size.
353 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
354 EXPECT_GT(pending_tiles.size(), 0u);
355 for (const auto& tile : pending_tiles)
356 EXPECT_EQ(tile_size2, tile->content_rect().size());
358 // Clone from the pending to the active tree.
359 active_client.SetTileSize(tile_size2);
360 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
361 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
362 // The active tiling should get the right tile size.
363 EXPECT_EQ(tile_size2, active_set->tiling_at(0)->tile_size());
365 // Cloned tiles should have the right size.
366 std::vector<Tile*> active_tiles =
367 active_set->tiling_at(0)->AllTilesForTesting();
368 EXPECT_GT(active_tiles.size(), 0u);
369 for (const auto& tile : active_tiles)
370 EXPECT_EQ(tile_size2, tile->content_rect().size());
372 // A new source frame with a new tile size.
373 pending_client.SetTileSize(tile_size3);
374 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
375 1.f, 1.f);
376 // The tiling gets the new size correctly.
377 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
379 // Set a priority rect so we get tiles.
380 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 3.0,
381 Occlusion(), false);
382 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
384 // Tiles are resized for the new size.
385 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
386 EXPECT_GT(pending_tiles.size(), 0u);
387 for (const auto& tile : pending_tiles)
388 EXPECT_EQ(tile_size3, tile->content_rect().size());
390 // Now we activate with a different tile size for the active tiling.
391 active_client.SetTileSize(tile_size3);
392 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
393 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
394 // The active tiling changes its tile size.
395 EXPECT_EQ(tile_size3, active_set->tiling_at(0)->tile_size());
397 // And its tiles are resized.
398 active_tiles = active_set->tiling_at(0)->AllTilesForTesting();
399 EXPECT_GT(active_tiles.size(), 0u);
400 for (const auto& tile : active_tiles)
401 EXPECT_EQ(tile_size3, tile->content_rect().size());
404 TEST(PictureLayerTilingSetTest, MaxContentScale) {
405 FakePictureLayerTilingClient pending_client;
406 FakePictureLayerTilingClient active_client;
407 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
408 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
409 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
410 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
412 gfx::Size layer_bounds(100, 105);
413 scoped_refptr<FakePicturePileImpl> pile =
414 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
416 // Tilings can be added of any scale, the tiling client can controls this.
417 pending_set->AddTiling(1.f, pile);
418 pending_set->AddTiling(2.f, pile);
419 pending_set->AddTiling(3.f, pile);
421 // Set some expected things for the tiling set to function.
422 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
423 active_client.set_twin_tiling_set(pending_set.get());
425 // Update to a new source frame with a max content scale that is larger than
426 // everything.
427 float max_content_scale = 3.f;
428 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
429 pile.get(), Region(), 1.f, max_content_scale);
431 // All the tilings are there still.
432 EXPECT_EQ(3u, pending_set->num_tilings());
434 // Clone from the pending to the active tree with the same max content size.
435 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
436 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
437 // All the tilings are on the active tree.
438 EXPECT_EQ(3u, active_set->num_tilings());
440 // Update to a new source frame with a max content scale that will drop one
441 // tiling.
442 max_content_scale = 2.9f;
443 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
444 pile.get(), Region(), 1.f, max_content_scale);
445 // All the tilings are there still.
446 EXPECT_EQ(2u, pending_set->num_tilings());
448 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
450 // Clone from the pending to the active tree with the same max content size.
451 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
452 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
453 // All the tilings are on the active tree.
454 EXPECT_EQ(2u, active_set->num_tilings());
457 } // namespace
458 } // namespace cc