ServiceWorker: Introduce SWMessageSender in order to mock IPC messaging
[chromium-blink-merge.git] / cc / resources / picture_layer_tiling_set_unittest.cc
blob3cafa4c64bd2f2903342e7fad9edb42009028aef
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/resources/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/test_shared_bitmap_manager.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/size_conversions.h"
19 namespace cc {
20 namespace {
22 scoped_ptr<PictureLayerTilingSet> CreateTilingSet(
23 PictureLayerTilingClient* client) {
24 LayerTreeSettings defaults;
25 return PictureLayerTilingSet::Create(
26 client, defaults.max_tiles_for_interest_area,
27 defaults.skewport_target_time_in_seconds,
28 defaults.skewport_extrapolation_limit_in_content_pixels);
31 TEST(PictureLayerTilingSetTest, NoResources) {
32 FakePictureLayerTilingClient client;
33 gfx::Size layer_bounds(1000, 800);
34 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
35 client.SetTileSize(gfx::Size(256, 256));
37 scoped_refptr<FakePicturePileImpl> pile =
38 FakePicturePileImpl::CreateEmptyPileWithDefaultTileSize(layer_bounds);
40 set->AddTiling(1.0, pile);
41 set->AddTiling(1.5, pile);
42 set->AddTiling(2.0, pile);
44 float contents_scale = 2.0;
45 gfx::Size content_bounds(
46 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, contents_scale)));
47 gfx::Rect content_rect(content_bounds);
49 Region remaining(content_rect);
50 PictureLayerTilingSet::CoverageIterator iter(set.get(), contents_scale,
51 content_rect, contents_scale);
52 for (; iter; ++iter) {
53 gfx::Rect geometry_rect = iter.geometry_rect();
54 EXPECT_TRUE(content_rect.Contains(geometry_rect));
55 ASSERT_TRUE(remaining.Contains(geometry_rect));
56 remaining.Subtract(geometry_rect);
58 // No tiles have resources, so no iter represents a real tile.
59 EXPECT_FALSE(*iter);
61 EXPECT_TRUE(remaining.IsEmpty());
64 TEST(PictureLayerTilingSetTest, TilingRange) {
65 FakePictureLayerTilingClient client;
66 gfx::Size layer_bounds(10, 10);
67 PictureLayerTilingSet::TilingRange higher_than_high_res_range(0, 0);
68 PictureLayerTilingSet::TilingRange high_res_range(0, 0);
69 PictureLayerTilingSet::TilingRange between_high_and_low_res_range(0, 0);
70 PictureLayerTilingSet::TilingRange low_res_range(0, 0);
71 PictureLayerTilingSet::TilingRange lower_than_low_res_range(0, 0);
72 PictureLayerTiling* high_res_tiling;
73 PictureLayerTiling* low_res_tiling;
75 scoped_refptr<FakePicturePileImpl> pile =
76 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
78 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
79 set->AddTiling(2.0, pile);
80 high_res_tiling = set->AddTiling(1.0, pile);
81 high_res_tiling->set_resolution(HIGH_RESOLUTION);
82 set->AddTiling(0.5, pile);
83 low_res_tiling = set->AddTiling(0.25, pile);
84 low_res_tiling->set_resolution(LOW_RESOLUTION);
85 set->AddTiling(0.125, pile);
87 higher_than_high_res_range =
88 set->GetTilingRange(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
89 EXPECT_EQ(0u, higher_than_high_res_range.start);
90 EXPECT_EQ(1u, higher_than_high_res_range.end);
92 high_res_range = set->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
93 EXPECT_EQ(1u, high_res_range.start);
94 EXPECT_EQ(2u, high_res_range.end);
96 between_high_and_low_res_range =
97 set->GetTilingRange(PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
98 EXPECT_EQ(2u, between_high_and_low_res_range.start);
99 EXPECT_EQ(3u, between_high_and_low_res_range.end);
101 low_res_range = set->GetTilingRange(PictureLayerTilingSet::LOW_RES);
102 EXPECT_EQ(3u, low_res_range.start);
103 EXPECT_EQ(4u, low_res_range.end);
105 lower_than_low_res_range =
106 set->GetTilingRange(PictureLayerTilingSet::LOWER_THAN_LOW_RES);
107 EXPECT_EQ(4u, lower_than_low_res_range.start);
108 EXPECT_EQ(5u, lower_than_low_res_range.end);
110 scoped_ptr<PictureLayerTilingSet> set_without_low_res =
111 CreateTilingSet(&client);
112 set_without_low_res->AddTiling(2.0, pile);
113 high_res_tiling = set_without_low_res->AddTiling(1.0, pile);
114 high_res_tiling->set_resolution(HIGH_RESOLUTION);
115 set_without_low_res->AddTiling(0.5, pile);
116 set_without_low_res->AddTiling(0.25, pile);
118 higher_than_high_res_range = set_without_low_res->GetTilingRange(
119 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
120 EXPECT_EQ(0u, higher_than_high_res_range.start);
121 EXPECT_EQ(1u, higher_than_high_res_range.end);
123 high_res_range =
124 set_without_low_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
125 EXPECT_EQ(1u, high_res_range.start);
126 EXPECT_EQ(2u, high_res_range.end);
128 between_high_and_low_res_range = set_without_low_res->GetTilingRange(
129 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
130 EXPECT_EQ(2u, between_high_and_low_res_range.start);
131 EXPECT_EQ(4u, between_high_and_low_res_range.end);
133 low_res_range =
134 set_without_low_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
135 EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
137 lower_than_low_res_range = set_without_low_res->GetTilingRange(
138 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
139 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
141 scoped_ptr<PictureLayerTilingSet> set_with_only_high_and_low_res =
142 CreateTilingSet(&client);
143 high_res_tiling = set_with_only_high_and_low_res->AddTiling(1.0, pile);
144 high_res_tiling->set_resolution(HIGH_RESOLUTION);
145 low_res_tiling = set_with_only_high_and_low_res->AddTiling(0.5, pile);
146 low_res_tiling->set_resolution(LOW_RESOLUTION);
148 higher_than_high_res_range = set_with_only_high_and_low_res->GetTilingRange(
149 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
150 EXPECT_EQ(0u,
151 higher_than_high_res_range.end - higher_than_high_res_range.start);
153 high_res_range = set_with_only_high_and_low_res->GetTilingRange(
154 PictureLayerTilingSet::HIGH_RES);
155 EXPECT_EQ(0u, high_res_range.start);
156 EXPECT_EQ(1u, high_res_range.end);
158 between_high_and_low_res_range =
159 set_with_only_high_and_low_res->GetTilingRange(
160 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
161 EXPECT_EQ(0u, between_high_and_low_res_range.end -
162 between_high_and_low_res_range.start);
164 low_res_range = set_with_only_high_and_low_res->GetTilingRange(
165 PictureLayerTilingSet::LOW_RES);
166 EXPECT_EQ(1u, low_res_range.start);
167 EXPECT_EQ(2u, low_res_range.end);
169 lower_than_low_res_range = set_with_only_high_and_low_res->GetTilingRange(
170 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
171 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
173 scoped_ptr<PictureLayerTilingSet> set_with_only_high_res =
174 CreateTilingSet(&client);
175 high_res_tiling = set_with_only_high_res->AddTiling(1.0, pile);
176 high_res_tiling->set_resolution(HIGH_RESOLUTION);
178 higher_than_high_res_range = set_with_only_high_res->GetTilingRange(
179 PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
180 EXPECT_EQ(0u,
181 higher_than_high_res_range.end - higher_than_high_res_range.start);
183 high_res_range =
184 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
185 EXPECT_EQ(0u, high_res_range.start);
186 EXPECT_EQ(1u, high_res_range.end);
188 between_high_and_low_res_range = set_with_only_high_res->GetTilingRange(
189 PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
190 EXPECT_EQ(0u, between_high_and_low_res_range.end -
191 between_high_and_low_res_range.start);
193 low_res_range =
194 set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
195 EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
197 lower_than_low_res_range = set_with_only_high_res->GetTilingRange(
198 PictureLayerTilingSet::LOWER_THAN_LOW_RES);
199 EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
202 class PictureLayerTilingSetTestWithResources : public testing::Test {
203 public:
204 void runTest(
205 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 ResourceProvider::Create(output_surface.get(),
219 shared_bitmap_manager.get(),
220 NULL,
221 NULL,
223 false,
226 FakePictureLayerTilingClient client(resource_provider.get());
227 client.SetTileSize(gfx::Size(256, 256));
228 client.set_tree(PENDING_TREE);
229 gfx::Size layer_bounds(1000, 800);
230 scoped_ptr<PictureLayerTilingSet> set = CreateTilingSet(&client);
231 scoped_refptr<FakePicturePileImpl> pile =
232 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
234 float scale = min_scale;
235 for (int i = 0; i < num_tilings; ++i, scale += scale_increment) {
236 PictureLayerTiling* tiling = set->AddTiling(scale, pile);
237 tiling->CreateAllTilesForTesting();
238 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
239 client.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
242 float max_contents_scale = scale;
243 gfx::Size content_bounds(
244 gfx::ToCeiledSize(gfx::ScaleSize(layer_bounds, max_contents_scale)));
245 gfx::Rect content_rect(content_bounds);
247 Region remaining(content_rect);
248 PictureLayerTilingSet::CoverageIterator iter(
249 set.get(), max_contents_scale, content_rect, ideal_contents_scale);
250 for (; iter; ++iter) {
251 gfx::Rect geometry_rect = iter.geometry_rect();
252 EXPECT_TRUE(content_rect.Contains(geometry_rect));
253 ASSERT_TRUE(remaining.Contains(geometry_rect));
254 remaining.Subtract(geometry_rect);
256 float scale = iter.CurrentTiling()->contents_scale();
257 EXPECT_EQ(expected_scale, scale);
259 if (num_tilings)
260 EXPECT_TRUE(*iter);
261 else
262 EXPECT_FALSE(*iter);
264 EXPECT_TRUE(remaining.IsEmpty());
268 TEST_F(PictureLayerTilingSetTestWithResources, NoTilings) {
269 runTest(0, 0.f, 0.f, 2.f, 0.f);
271 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Smaller) {
272 runTest(1, 1.f, 0.f, 2.f, 1.f);
274 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Larger) {
275 runTest(1, 3.f, 0.f, 2.f, 3.f);
277 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Smaller) {
278 runTest(2, 1.f, 1.f, 3.f, 2.f);
281 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_SmallerEqual) {
282 runTest(2, 1.f, 1.f, 2.f, 2.f);
285 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_LargerEqual) {
286 runTest(2, 1.f, 1.f, 1.f, 1.f);
289 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Larger) {
290 runTest(2, 2.f, 8.f, 1.f, 2.f);
293 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_Equal) {
294 runTest(10, 1.f, 1.f, 5.f, 5.f);
297 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_NotEqual) {
298 runTest(10, 1.f, 1.f, 4.5f, 5.f);
301 TEST(PictureLayerTilingSetTest, TileSizeChange) {
302 FakePictureLayerTilingClient pending_client;
303 FakePictureLayerTilingClient active_client;
304 scoped_ptr<PictureLayerTilingSet> pending_set =
305 PictureLayerTilingSet::Create(&pending_client, 1000, 1.f, 1000);
306 scoped_ptr<PictureLayerTilingSet> active_set =
307 PictureLayerTilingSet::Create(&active_client, 1000, 1.f, 1000);
309 gfx::Size layer_bounds(100, 100);
310 scoped_refptr<FakePicturePileImpl> pile =
311 FakePicturePileImpl::CreateFilledPileWithDefaultTileSize(layer_bounds);
313 gfx::Size tile_size1(10, 10);
314 gfx::Size tile_size2(30, 30);
315 gfx::Size tile_size3(20, 20);
317 pending_client.SetTileSize(tile_size1);
318 pending_set->AddTiling(1.f, pile);
319 // New tilings get the correct tile size.
320 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
322 // Set some expected things for the tiling set to function.
323 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
324 active_client.set_twin_tiling_set(pending_set.get());
326 // Set a priority rect so we get tiles.
327 pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
328 Occlusion(), false);
329 EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
331 // The tiles should get the correct size.
332 std::vector<Tile*> pending_tiles =
333 pending_set->tiling_at(0)->AllTilesForTesting();
334 EXPECT_GT(pending_tiles.size(), 0u);
335 for (const auto& tile : pending_tiles)
336 EXPECT_EQ(tile_size1, tile->content_rect().size());
338 // Update to a new source frame with a new tile size.
339 pending_client.SetTileSize(tile_size2);
340 pending_set->UpdateTilingsToCurrentRasterSource(pile.get(), nullptr, 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->UpdateTilingsToCurrentRasterSource(pile.get(), pending_set.get(),
359 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->UpdateTilingsToCurrentRasterSource(pile.get(), nullptr, 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->UpdateTilingsToCurrentRasterSource(pile.get(), pending_set.get(),
391 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 =
406 PictureLayerTilingSet::Create(&pending_client, 1000, 1.f, 1000);
407 scoped_ptr<PictureLayerTilingSet> active_set =
408 PictureLayerTilingSet::Create(&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->UpdateTilingsToCurrentRasterSource(pile.get(), nullptr, Region(),
427 1.f, max_content_scale);
428 // All the tilings are there still.
429 EXPECT_EQ(3u, pending_set->num_tilings());
431 // Clone from the pending to the active tree with the same max content size.
432 active_set->UpdateTilingsToCurrentRasterSource(
433 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
434 // All the tilings are on the active tree.
435 EXPECT_EQ(3u, active_set->num_tilings());
437 // Update to a new source frame with a max content scale that will drop one
438 // tiling.
439 max_content_scale = 2.9f;
440 pending_set->UpdateTilingsToCurrentRasterSource(pile.get(), nullptr, Region(),
441 1.f, max_content_scale);
442 // All the tilings are there still.
443 EXPECT_EQ(2u, pending_set->num_tilings());
445 pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
447 // Clone from the pending to the active tree with the same max content size.
448 active_set->UpdateTilingsToCurrentRasterSource(
449 pile.get(), pending_set.get(), Region(), 1.f, max_content_scale);
450 // All the tilings are on the active tree.
451 EXPECT_EQ(2u, active_set->num_tilings());
454 } // namespace
455 } // namespace cc