Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / cc / tiles / picture_layer_tiling_set_unittest.cc
blobdd3757020cfb06ee9d13a897436403803b0258d0
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 "testing/gtest/include/gtest/gtest.h"
18 #include "ui/gfx/geometry/size_conversions.h"
20 namespace cc {
21 namespace {
23 scoped_ptr<PictureLayerTilingSet> CreateTilingSet(
24 PictureLayerTilingClient* client) {
25 LayerTreeSettings defaults;
26 return PictureLayerTilingSet::Create(
27 ACTIVE_TREE, client, defaults.tiling_interest_area_viewport_multiplier,
28 defaults.skewport_target_time_in_seconds,
29 defaults.skewport_extrapolation_limit_in_content_pixels);
32 TEST(PictureLayerTilingSetTest, NoResources) {
33 FakePictureLayerTilingClient client;
34 gfx::Size layer_bounds(1000, 800);
35 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
36 client.SetTileSize(gfx::Size(256, 256));
38 scoped_refptr<FakePicturePileImpl> pile =
39 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
41 set->AddTiling(1.0, pile);
42 set->AddTiling(1.5, pile);
43 set->AddTiling(2.0, pile);
45 float contents_scale = 2.0;
46 gfx::Size content_bounds(
47 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
48 gfx::Rect content_rect(content_bounds);
50 Region remaining(content_rect);
51 PictureLayerTilingSet::CoverageIterator iter(set.get(), contents_scale,
52 content_rect, contents_scale);
53 for (; iter; ++iter) {
54 gfx::Rect geometry_rect = iter.geometry_rect();
55 EXPECT_TRUE(content_rect.Contains(geometry_rect));
56 ASSERT_TRUE(remaining.Contains(geometry_rect));
57 remaining.Subtract(geometry_rect);
59 // No tiles have resources, so no iter represents a real tile.
60 EXPECT_FALSE(*iter);
62 EXPECT_TRUE(remaining.IsEmpty());
65 TEST(PictureLayerTilingSetTest, TilingRange) {
66 FakePictureLayerTilingClient client;
67 gfx::Size layer_bounds(10, 10);
68 PictureLayerTilingSet::TilingRange higher_than_high_res_range(0, 0);
69 PictureLayerTilingSet::TilingRange high_res_range(0, 0);
70 PictureLayerTilingSet::TilingRange between_high_and_low_res_range(0, 0);
71 PictureLayerTilingSet::TilingRange low_res_range(0, 0);
72 PictureLayerTilingSet::TilingRange lower_than_low_res_range(0, 0);
73 PictureLayerTiling* high_res_tiling;
74 PictureLayerTiling* low_res_tiling;
76 scoped_refptr<FakePicturePileImpl> pile =
77 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
79 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
80 set->AddTiling(2.0, pile);
81 high_res_tiling = set->AddTiling(1.0, pile);
82 high_res_tiling->set_resolution(HIGH_RESOLUTION);
83 set->AddTiling(0.5, pile);
84 low_res_tiling = set->AddTiling(0.25, pile);
85 low_res_tiling->set_resolution(LOW_RESOLUTION);
86 set->AddTiling(0.125, pile);
88 higher_than_high_res_range =
89 set->GetTilingRange(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
90 EXPECT_EQ(0, higher_than_high_res_range.start);
91 EXPECT_EQ(1, higher_than_high_res_range.end);
93 high_res_range = set->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
94 EXPECT_EQ(1, high_res_range.start);
95 EXPECT_EQ(2, high_res_range.end);
97 between_high_and_low_res_range =
98 set->GetTilingRange(PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
99 EXPECT_EQ(2, between_high_and_low_res_range.start);
100 EXPECT_EQ(3, between_high_and_low_res_range.end);
102 low_res_range = set->GetTilingRange(PictureLayerTilingSet::LOW_RES);
103 EXPECT_EQ(3, low_res_range.start);
104 EXPECT_EQ(4, low_res_range.end);
106 lower_than_low_res_range =
107 set->GetTilingRange(PictureLayerTilingSet::LOWER_THAN_LOW_RES);
108 EXPECT_EQ(4, lower_than_low_res_range.start);
109 EXPECT_EQ(5, lower_than_low_res_range.end);
111 scoped_ptr<PictureLayerTilingSet> set_without_low_res =
112 CreateTilingSet(&client);
113 set_without_low_res->AddTiling(2.0, pile);
114 high_res_tiling = set_without_low_res->AddTiling(1.0, pile);
115 high_res_tiling->set_resolution(HIGH_RESOLUTION);
116 set_without_low_res->AddTiling(0.5, pile);
117 set_without_low_res->AddTiling(0.25, pile);
119 higher_than_high_res_range = set_without_low_res->GetTilingRange(
120 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
121 EXPECT_EQ(0, higher_than_high_res_range.start);
122 EXPECT_EQ(1, higher_than_high_res_range.end);
124 high_res_range =
125 set_without_low_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
126 EXPECT_EQ(1, high_res_range.start);
127 EXPECT_EQ(2, high_res_range.end);
129 between_high_and_low_res_range = set_without_low_res->GetTilingRange(
130 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
131 EXPECT_EQ(2, between_high_and_low_res_range.start);
132 EXPECT_EQ(4, between_high_and_low_res_range.end);
134 low_res_range =
135 set_without_low_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
136 EXPECT_EQ(0, low_res_range.end - low_res_range.start);
138 lower_than_low_res_range = set_without_low_res->GetTilingRange(
139 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
140 EXPECT_EQ(0, lower_than_low_res_range.end - lower_than_low_res_range.start);
142 scoped_ptr<PictureLayerTilingSet> set_with_only_high_and_low_res =
143 CreateTilingSet(&client);
144 high_res_tiling = set_with_only_high_and_low_res->AddTiling(1.0, pile);
145 high_res_tiling->set_resolution(HIGH_RESOLUTION);
146 low_res_tiling = set_with_only_high_and_low_res->AddTiling(0.5, pile);
147 low_res_tiling->set_resolution(LOW_RESOLUTION);
149 higher_than_high_res_range = set_with_only_high_and_low_res->GetTilingRange(
150 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
151 EXPECT_EQ(0,
152 higher_than_high_res_range.end - higher_than_high_res_range.start);
154 high_res_range = set_with_only_high_and_low_res->GetTilingRange(
155 PictureLayerTilingSet::HIGH_RES);
156 EXPECT_EQ(0, high_res_range.start);
157 EXPECT_EQ(1, high_res_range.end);
159 between_high_and_low_res_range =
160 set_with_only_high_and_low_res->GetTilingRange(
161 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
162 EXPECT_EQ(0, between_high_and_low_res_range.end -
163 between_high_and_low_res_range.start);
165 low_res_range = set_with_only_high_and_low_res->GetTilingRange(
166 PictureLayerTilingSet::LOW_RES);
167 EXPECT_EQ(1, low_res_range.start);
168 EXPECT_EQ(2, low_res_range.end);
170 lower_than_low_res_range = set_with_only_high_and_low_res->GetTilingRange(
171 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
172 EXPECT_EQ(0, lower_than_low_res_range.end - lower_than_low_res_range.start);
174 scoped_ptr<PictureLayerTilingSet> set_with_only_high_res =
175 CreateTilingSet(&client);
176 high_res_tiling = set_with_only_high_res->AddTiling(1.0, pile);
177 high_res_tiling->set_resolution(HIGH_RESOLUTION);
179 higher_than_high_res_range = set_with_only_high_res->GetTilingRange(
180 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
181 EXPECT_EQ(0,
182 higher_than_high_res_range.end - higher_than_high_res_range.start);
184 high_res_range =
185 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
186 EXPECT_EQ(0, high_res_range.start);
187 EXPECT_EQ(1, high_res_range.end);
189 between_high_and_low_res_range = set_with_only_high_res->GetTilingRange(
190 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
191 EXPECT_EQ(0, between_high_and_low_res_range.end -
192 between_high_and_low_res_range.start);
194 low_res_range =
195 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
196 EXPECT_EQ(0, low_res_range.end - low_res_range.start);
198 lower_than_low_res_range = set_with_only_high_res->GetTilingRange(
199 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
200 EXPECT_EQ(0, lower_than_low_res_range.end - lower_than_low_res_range.start);
203 class PictureLayerTilingSetTestWithResources : public testing::Test {
204 public:
205 void RunTest(int num_tilings,
206 float min_scale,
207 float scale_increment,
208 float ideal_contents_scale,
209 float expected_scale) {
210 FakeOutputSurfaceClient output_surface_client;
211 scoped_ptr<FakeOutputSurface> output_surface =
212 FakeOutputSurface::Create3d();
213 CHECK(output_surface->BindToClient(&output_surface_client));
215 scoped_ptr<SharedBitmapManager> shared_bitmap_manager(
216 new TestSharedBitmapManager());
217 scoped_ptr<ResourceProvider> resource_provider =
218 FakeResourceProvider::Create(output_surface.get(),
219 shared_bitmap_manager.get());
221 FakePictureLayerTilingClient client(resource_provider.get());
222 client.SetTileSize(gfx::Size(256, 256));
223 gfx::Size layer_bounds(1000, 800);
224 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
225 scoped_refptr<FakePicturePileImpl> pile =
226 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
228 float scale = min_scale;
229 for (int i = 0; i < num_tilings; ++i, scale += scale_increment) {
230 PictureLayerTiling* tiling = set->AddTiling(scale, pile);
231 tiling->CreateAllTilesForTesting();
232 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
233 client.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
236 float max_contents_scale = scale;
237 gfx::Size content_bounds(
238 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, max_contents_scale)));
239 gfx::Rect content_rect(content_bounds);
241 Region remaining(content_rect);
242 PictureLayerTilingSet::CoverageIterator iter(
243 set.get(), max_contents_scale, content_rect, ideal_contents_scale);
244 for (; iter; ++iter) {
245 gfx::Rect geometry_rect = iter.geometry_rect();
246 EXPECT_TRUE(content_rect.Contains(geometry_rect));
247 ASSERT_TRUE(remaining.Contains(geometry_rect));
248 remaining.Subtract(geometry_rect);
250 float scale = iter.CurrentTiling()->contents_scale();
251 EXPECT_EQ(expected_scale, scale);
253 if (num_tilings)
254 EXPECT_TRUE(*iter);
255 else
256 EXPECT_FALSE(*iter);
258 EXPECT_TRUE(remaining.IsEmpty());
262 TEST_F(PictureLayerTilingSetTestWithResources, NoTilings) {
263 RunTest(0, 0.f, 0.f, 2.f, 0.f);
265 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Smaller) {
266 RunTest(1, 1.f, 0.f, 2.f, 1.f);
268 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Larger) {
269 RunTest(1, 3.f, 0.f, 2.f, 3.f);
271 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Smaller) {
272 RunTest(2, 1.f, 1.f, 3.f, 2.f);
275 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_SmallerEqual) {
276 RunTest(2, 1.f, 1.f, 2.f, 2.f);
279 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_LargerEqual) {
280 RunTest(2, 1.f, 1.f, 1.f, 1.f);
283 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Larger) {
284 RunTest(2, 2.f, 8.f, 1.f, 2.f);
287 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_Equal) {
288 RunTest(10, 1.f, 1.f, 5.f, 5.f);
291 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_NotEqual) {
292 RunTest(10, 1.f, 1.f, 4.5f, 5.f);
295 TEST(PictureLayerTilingSetTest, TileSizeChange) {
296 FakePictureLayerTilingClient pending_client;
297 FakePictureLayerTilingClient active_client;
298 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
299 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
300 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
301 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
303 gfx::Size layer_bounds(100, 100);
304 scoped_refptr<FakePicturePileImpl> pile =
305 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
307 gfx::Size tile_size1(10, 10);
308 gfx::Size tile_size2(30, 30);
309 gfx::Size tile_size3(20, 20);
311 pending_client.SetTileSize(tile_size1);
312 pending_set->AddTiling(1.f, pile);
313 // New tilings get the correct tile size.
314 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
316 // Set some expected things for the tiling set to function.
317 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
318 active_client.set_twin_tiling_set(pending_set.get());
320 // Set a priority rect so we get tiles.
321 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
322 Occlusion(), false);
323 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
325 // The tiles should get the correct size.
326 std::vector<Tile*> pending_tiles =
327 pending_set->tiling_at(0)->AllTilesForTesting();
328 EXPECT_GT(pending_tiles.size(), 0u);
329 for (const auto& tile : pending_tiles)
330 EXPECT_EQ(tile_size1, tile->content_rect().size());
332 // Update to a new source frame with a new tile size.
333 // Note that setting a new raster source can typically only happen after
334 // activation, since we can't set the raster source twice on the pending tree
335 // without activating. For test, just remove and add a new tiling instead.
336 pending_set->RemoveAllTilings();
337 pending_set->AddTiling(1.f, pile);
338 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
339 pending_client.SetTileSize(tile_size2);
340 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
341 1.f, 1.f);
342 // The tiling should get the correct tile size.
343 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
345 // Set a priority rect so we get tiles.
346 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 2.0,
347 Occlusion(), false);
348 EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
350 // Tiles should have the new correct size.
351 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
352 EXPECT_GT(pending_tiles.size(), 0u);
353 for (const auto& tile : pending_tiles)
354 EXPECT_EQ(tile_size2, tile->content_rect().size());
356 // Clone from the pending to the active tree.
357 active_client.SetTileSize(tile_size2);
358 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
359 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
360 // The active tiling should get the right tile size.
361 EXPECT_EQ(tile_size2, active_set->tiling_at(0)->tile_size());
363 // Cloned tiles should have the right size.
364 std::vector<Tile*> active_tiles =
365 active_set->tiling_at(0)->AllTilesForTesting();
366 EXPECT_GT(active_tiles.size(), 0u);
367 for (const auto& tile : active_tiles)
368 EXPECT_EQ(tile_size2, tile->content_rect().size());
370 // A new source frame with a new tile size.
371 pending_client.SetTileSize(tile_size3);
372 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(pile.get(), Region(),
373 1.f, 1.f);
374 // The tiling gets the new size correctly.
375 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
377 // Set a priority rect so we get tiles.
378 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 3.0,
379 Occlusion(), false);
380 EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
382 // Tiles are resized for the new size.
383 pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
384 EXPECT_GT(pending_tiles.size(), 0u);
385 for (const auto& tile : pending_tiles)
386 EXPECT_EQ(tile_size3, tile->content_rect().size());
388 // Now we activate with a different tile size for the active tiling.
389 active_client.SetTileSize(tile_size3);
390 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
391 pile.get(), pending_set.get(), Region(), 1.f, 1.f);
392 // The active tiling changes its tile size.
393 EXPECT_EQ(tile_size3, active_set->tiling_at(0)->tile_size());
395 // And its tiles are resized.
396 active_tiles = active_set->tiling_at(0)->AllTilesForTesting();
397 EXPECT_GT(active_tiles.size(), 0u);
398 for (const auto& tile : active_tiles)
399 EXPECT_EQ(tile_size3, tile->content_rect().size());
402 TEST(PictureLayerTilingSetTest, MaxContentScale) {
403 FakePictureLayerTilingClient pending_client;
404 FakePictureLayerTilingClient active_client;
405 scoped_ptr<PictureLayerTilingSet> pending_set = PictureLayerTilingSet::Create(
406 PENDING_TREE, &pending_client, 1000, 1.f, 1000);
407 scoped_ptr<PictureLayerTilingSet> active_set = PictureLayerTilingSet::Create(
408 ACTIVE_TREE, &active_client, 1000, 1.f, 1000);
410 gfx::Size layer_bounds(100, 105);
411 scoped_refptr<FakePicturePileImpl> pile =
412 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
414 // Tilings can be added of any scale, the tiling client can controls this.
415 pending_set->AddTiling(1.f, pile);
416 pending_set->AddTiling(2.f, pile);
417 pending_set->AddTiling(3.f, pile);
419 // Set some expected things for the tiling set to function.
420 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
421 active_client.set_twin_tiling_set(pending_set.get());
423 // Update to a new source frame with a max content scale that is larger than
424 // everything.
425 float max_content_scale = 3.f;
426 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
427 pile.get(), Region(), 1.f, max_content_scale);
429 // All the tilings are there still.
430 EXPECT_EQ(3u, pending_set->num_tilings());
432 // Clone from the pending to the active tree with the same max content size.
433 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
434 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
435 // All the tilings are on the active tree.
436 EXPECT_EQ(3u, active_set->num_tilings());
438 // Update to a new source frame with a max content scale that will drop one
439 // tiling.
440 max_content_scale = 2.9f;
441 pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
442 pile.get(), Region(), 1.f, max_content_scale);
443 // All the tilings are there still.
444 EXPECT_EQ(2u, pending_set->num_tilings());
446 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
448 // Clone from the pending to the active tree with the same max content size.
449 active_set->UpdateTilingsToCurrentRasterSourceForActivation(
450 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
451 // All the tilings are on the active tree.
452 EXPECT_EQ(2u, active_set->num_tilings());
455 } // namespace
456 } // namespace cc