Preserve viewport scrolling layer when a fling starts
[chromium-blink-merge.git] / cc / tiles / picture_layer_tiling_set_unittest.cc
blobe7593cde80b58609000e9882b28061aea3ff3788
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.max_tiles_for_interest_area,
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->CreateAllTilesForTesting();
233 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
234 client.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
237 float max_contents_scale = scale;
238 gfx::Size content_bounds(
239 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, max_contents_scale)));
240 gfx::Rect content_rect(content_bounds);
242 Region remaining(content_rect);
243 PictureLayerTilingSet::CoverageIterator iter(
244 set.get(), max_contents_scale, content_rect, ideal_contents_scale);
245 for (; iter; ++iter) {
246 gfx::Rect geometry_rect = iter.geometry_rect();
247 EXPECT_TRUE(content_rect.Contains(geometry_rect));
248 ASSERT_TRUE(remaining.Contains(geometry_rect));
249 remaining.Subtract(geometry_rect);
251 float scale = iter.CurrentTiling()->contents_scale();
252 EXPECT_EQ(expected_scale, scale);
254 if (num_tilings)
255 EXPECT_TRUE(*iter);
256 else
257 EXPECT_FALSE(*iter);
259 EXPECT_TRUE(remaining.IsEmpty());
263 TEST_F(PictureLayerTilingSetTestWithResources, NoTilings) {
264 RunTest(0, 0.f, 0.f, 2.f, 0.f);
266 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Smaller) {
267 RunTest(1, 1.f, 0.f, 2.f, 1.f);
269 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Larger) {
270 RunTest(1, 3.f, 0.f, 2.f, 3.f);
272 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Smaller) {
273 RunTest(2, 1.f, 1.f, 3.f, 2.f);
276 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_SmallerEqual) {
277 RunTest(2, 1.f, 1.f, 2.f, 2.f);
280 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_LargerEqual) {
281 RunTest(2, 1.f, 1.f, 1.f, 1.f);
284 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Larger) {
285 RunTest(2, 2.f, 8.f, 1.f, 2.f);
288 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_Equal) {
289 RunTest(10, 1.f, 1.f, 5.f, 5.f);
292 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_NotEqual) {
293 RunTest(10, 1.f, 1.f, 4.5f, 5.f);
296 TEST(PictureLayerTilingSetTest, TileSizeChange) {
297 FakePictureLayerTilingClient pending_client;
298 FakePictureLayerTilingClient active_client;
299 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
300 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
301 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
302 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
304 gfx::Size layer_bounds(100, 100);
305 scoped_refptr<FakePicturePileImpl> pile =
306 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
308 gfx::Size tile_size1(10, 10);
309 gfx::Size tile_size2(30, 30);
310 gfx::Size tile_size3(20, 20);
312 pending_client.SetTileSize(tile_size1);
313 pending_set->AddTiling(1.f, pile);
314 // New tilings get the correct tile size.
315 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
317 // Set some expected things for the tiling set to function.
318 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
319 active_client.set_twin_tiling_set(pending_set.get());
321 // Set a priority rect so we get tiles.
322 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
323 Occlusion(), false);
324 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
326 // The tiles should get the correct size.
327 std::vector<Tile*> pending_tiles =
328 pending_set->tiling_at(0)->AllTilesForTesting();
329 EXPECT_GT(pending_tiles.size(), 0u);
330 for (const auto& tile : pending_tiles)
331 EXPECT_EQ(tile_size1, tile->content_rect().size());
333 // Update to a new source frame with a new tile size.
334 // Note that setting a new raster source can typically only happen after
335 // activation, since we can't set the raster source twice on the pending tree
336 // without activating. For test, just remove and add a new tiling instead.
337 pending_set->RemoveAllTilings();
338 pending_set->AddTiling(1.f, pile);
339 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
340 pending_client.SetTileSize(tile_size2);
341 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
342 1.f, 1.f);
343 // The tiling should get the correct tile size.
344 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
346 // Set a priority rect so we get tiles.
347 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 2.0,
348 Occlusion(), false);
349 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
351 // Tiles should have the new correct size.
352 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
353 EXPECT_GT(pending_tiles.size(), 0u);
354 for (const auto& tile : pending_tiles)
355 EXPECT_EQ(tile_size2, tile->content_rect().size());
357 // Clone from the pending to the active tree.
358 active_client.SetTileSize(tile_size2);
359 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
360 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
361 // The active tiling should get the right tile size.
362 EXPECT_EQ(tile_size2, active_set->tiling_at(0)->tile_size());
364 // Cloned tiles should have the right size.
365 std::vector<Tile*> active_tiles =
366 active_set->tiling_at(0)->AllTilesForTesting();
367 EXPECT_GT(active_tiles.size(), 0u);
368 for (const auto& tile : active_tiles)
369 EXPECT_EQ(tile_size2, tile->content_rect().size());
371 // A new source frame with a new tile size.
372 pending_client.SetTileSize(tile_size3);
373 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
374 1.f, 1.f);
375 // The tiling gets the new size correctly.
376 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
378 // Set a priority rect so we get tiles.
379 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 3.0,
380 Occlusion(), false);
381 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
383 // Tiles are resized for the new size.
384 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
385 EXPECT_GT(pending_tiles.size(), 0u);
386 for (const auto& tile : pending_tiles)
387 EXPECT_EQ(tile_size3, tile->content_rect().size());
389 // Now we activate with a different tile size for the active tiling.
390 active_client.SetTileSize(tile_size3);
391 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
392 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
393 // The active tiling changes its tile size.
394 EXPECT_EQ(tile_size3, active_set->tiling_at(0)->tile_size());
396 // And its tiles are resized.
397 active_tiles = active_set->tiling_at(0)->AllTilesForTesting();
398 EXPECT_GT(active_tiles.size(), 0u);
399 for (const auto& tile : active_tiles)
400 EXPECT_EQ(tile_size3, tile->content_rect().size());
403 TEST(PictureLayerTilingSetTest, MaxContentScale) {
404 FakePictureLayerTilingClient pending_client;
405 FakePictureLayerTilingClient active_client;
406 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
407 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
408 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
409 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
411 gfx::Size layer_bounds(100, 105);
412 scoped_refptr<FakePicturePileImpl> pile =
413 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
415 // Tilings can be added of any scale, the tiling client can controls this.
416 pending_set->AddTiling(1.f, pile);
417 pending_set->AddTiling(2.f, pile);
418 pending_set->AddTiling(3.f, pile);
420 // Set some expected things for the tiling set to function.
421 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
422 active_client.set_twin_tiling_set(pending_set.get());
424 // Update to a new source frame with a max content scale that is larger than
425 // everything.
426 float max_content_scale = 3.f;
427 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
428 pile.get(), Region(), 1.f, max_content_scale);
430 // All the tilings are there still.
431 EXPECT_EQ(3u, pending_set->num_tilings());
433 // Clone from the pending to the active tree with the same max content size.
434 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
435 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
436 // All the tilings are on the active tree.
437 EXPECT_EQ(3u, active_set->num_tilings());
439 // Update to a new source frame with a max content scale that will drop one
440 // tiling.
441 max_content_scale = 2.9f;
442 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
443 pile.get(), Region(), 1.f, max_content_scale);
444 // All the tilings are there still.
445 EXPECT_EQ(2u, pending_set->num_tilings());
447 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
449 // Clone from the pending to the active tree with the same max content size.
450 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
451 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
452 // All the tilings are on the active tree.
453 EXPECT_EQ(2u, active_set->num_tilings());
456 } // namespace
457 } // namespace cc