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.
4 
5 #include "cc/tiles/picture_layer_tiling_set.h"
6 
7 #include <map>
8 #include <vector>
9 
10 #include "cc/test/fake_output_surface_client.h"
11 #include "cc/test/fake_picture_layer_tiling_client.h"
12 #include "cc/test/fake_raster_source.h"
13 #include "cc/trees/layer_tree_settings.h"
14 #include "components/viz/client/client_resource_provider.h"
15 #include "components/viz/test/fake_output_surface.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/gfx/geometry/size_conversions.h"
18 
19 namespace cc {
20 namespace {
21 
22 class TestablePictureLayerTilingSet : public PictureLayerTilingSet {
23  public:
TestablePictureLayerTilingSet(WhichTree tree,PictureLayerTilingClient * client,int tiling_interest_area_padding,float skewport_target_time_in_seconds,int skewport_extrapolation_limit_in_screen_pixels,float max_preraster_distance)24   TestablePictureLayerTilingSet(
25       WhichTree tree,
26       PictureLayerTilingClient* client,
27       int tiling_interest_area_padding,
28       float skewport_target_time_in_seconds,
29       int skewport_extrapolation_limit_in_screen_pixels,
30       float max_preraster_distance)
31       : PictureLayerTilingSet(tree,
32                               client,
33                               tiling_interest_area_padding,
34                               skewport_target_time_in_seconds,
35                               skewport_extrapolation_limit_in_screen_pixels,
36                               max_preraster_distance) {}
37 
38   using PictureLayerTilingSet::ComputeSkewport;
39   using PictureLayerTilingSet::ComputeSoonBorderRect;
40   using PictureLayerTilingSet::TilingsNeedUpdate;
41 };
42 
CreateTilingSetWithSettings(PictureLayerTilingClient * client,const LayerTreeSettings & settings)43 std::unique_ptr<TestablePictureLayerTilingSet> CreateTilingSetWithSettings(
44     PictureLayerTilingClient* client,
45     const LayerTreeSettings& settings) {
46   return std::make_unique<TestablePictureLayerTilingSet>(
47       ACTIVE_TREE, client, settings.tiling_interest_area_padding,
48       settings.skewport_target_time_in_seconds,
49       settings.skewport_extrapolation_limit_in_screen_pixels,
50       settings.max_preraster_distance_in_screen_pixels);
51 }
52 
CreateTilingSet(PictureLayerTilingClient * client)53 std::unique_ptr<TestablePictureLayerTilingSet> CreateTilingSet(
54     PictureLayerTilingClient* client) {
55   return CreateTilingSetWithSettings(client, LayerTreeSettings());
56 }
57 
TEST(PictureLayerTilingSetTest,NoResources)58 TEST(PictureLayerTilingSetTest, NoResources) {
59   FakePictureLayerTilingClient client;
60   gfx::Size layer_bounds(1000, 800);
61   std::unique_ptr<TestablePictureLayerTilingSet> set = CreateTilingSet(&client);
62   client.SetTileSize(gfx::Size(256, 256));
63 
64   scoped_refptr<FakeRasterSource> raster_source =
65       FakeRasterSource::CreateEmpty(layer_bounds);
66 
67   set->AddTiling(gfx::AxisTransform2d(), raster_source);
68   set->AddTiling(gfx::AxisTransform2d(1.5, gfx::Vector2dF()), raster_source);
69   set->AddTiling(gfx::AxisTransform2d(2.0, gfx::Vector2dF()), raster_source);
70 
71   float contents_scale = 2.0;
72   gfx::Size content_bounds(
73       gfx::ScaleToCeiledSize(layer_bounds, contents_scale));
74   gfx::Rect content_rect(content_bounds);
75 
76   Region remaining(content_rect);
77   PictureLayerTilingSet::CoverageIterator iter(set.get(), contents_scale,
78                                                content_rect, contents_scale);
79   for (; iter; ++iter) {
80     gfx::Rect geometry_rect = iter.geometry_rect();
81     EXPECT_TRUE(content_rect.Contains(geometry_rect));
82     ASSERT_TRUE(remaining.Contains(geometry_rect));
83     remaining.Subtract(geometry_rect);
84 
85     // No tiles have resources, so no iter represents a real tile.
86     EXPECT_FALSE(*iter);
87   }
88   EXPECT_TRUE(remaining.IsEmpty());
89 }
90 
TEST(PictureLayerTilingSetTest,TilingRange)91 TEST(PictureLayerTilingSetTest, TilingRange) {
92   FakePictureLayerTilingClient client;
93   gfx::Size layer_bounds(10, 10);
94   PictureLayerTilingSet::TilingRange higher_than_high_res_range(0, 0);
95   PictureLayerTilingSet::TilingRange high_res_range(0, 0);
96   PictureLayerTilingSet::TilingRange between_high_and_low_res_range(0, 0);
97   PictureLayerTilingSet::TilingRange low_res_range(0, 0);
98   PictureLayerTilingSet::TilingRange lower_than_low_res_range(0, 0);
99   PictureLayerTiling* high_res_tiling;
100   PictureLayerTiling* low_res_tiling;
101 
102   scoped_refptr<FakeRasterSource> raster_source =
103       FakeRasterSource::CreateFilled(layer_bounds);
104 
105   std::unique_ptr<TestablePictureLayerTilingSet> set = CreateTilingSet(&client);
106   set->AddTiling(gfx::AxisTransform2d(2.0, gfx::Vector2dF()), raster_source);
107   high_res_tiling = set->AddTiling(gfx::AxisTransform2d(), raster_source);
108   high_res_tiling->set_resolution(HIGH_RESOLUTION);
109   set->AddTiling(gfx::AxisTransform2d(0.5, gfx::Vector2dF()), raster_source);
110   low_res_tiling = set->AddTiling(gfx::AxisTransform2d(0.25, gfx::Vector2dF()),
111                                   raster_source);
112   low_res_tiling->set_resolution(LOW_RESOLUTION);
113   set->AddTiling(gfx::AxisTransform2d(0.125, gfx::Vector2dF()), raster_source);
114 
115   higher_than_high_res_range =
116       set->GetTilingRange(PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
117   EXPECT_EQ(0u, higher_than_high_res_range.start);
118   EXPECT_EQ(1u, higher_than_high_res_range.end);
119 
120   high_res_range = set->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
121   EXPECT_EQ(1u, high_res_range.start);
122   EXPECT_EQ(2u, high_res_range.end);
123 
124   between_high_and_low_res_range =
125       set->GetTilingRange(PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
126   EXPECT_EQ(2u, between_high_and_low_res_range.start);
127   EXPECT_EQ(3u, between_high_and_low_res_range.end);
128 
129   low_res_range = set->GetTilingRange(PictureLayerTilingSet::LOW_RES);
130   EXPECT_EQ(3u, low_res_range.start);
131   EXPECT_EQ(4u, low_res_range.end);
132 
133   lower_than_low_res_range =
134       set->GetTilingRange(PictureLayerTilingSet::LOWER_THAN_LOW_RES);
135   EXPECT_EQ(4u, lower_than_low_res_range.start);
136   EXPECT_EQ(5u, lower_than_low_res_range.end);
137 
138   std::unique_ptr<TestablePictureLayerTilingSet> set_without_low_res =
139       CreateTilingSet(&client);
140   set_without_low_res->AddTiling(gfx::AxisTransform2d(2.0, gfx::Vector2dF()),
141                                  raster_source);
142   high_res_tiling =
143       set_without_low_res->AddTiling(gfx::AxisTransform2d(), raster_source);
144   high_res_tiling->set_resolution(HIGH_RESOLUTION);
145   set_without_low_res->AddTiling(gfx::AxisTransform2d(0.5, gfx::Vector2dF()),
146                                  raster_source);
147   set_without_low_res->AddTiling(gfx::AxisTransform2d(0.25, gfx::Vector2dF()),
148                                  raster_source);
149 
150   higher_than_high_res_range = set_without_low_res->GetTilingRange(
151       PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
152   EXPECT_EQ(0u, higher_than_high_res_range.start);
153   EXPECT_EQ(1u, higher_than_high_res_range.end);
154 
155   high_res_range =
156       set_without_low_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
157   EXPECT_EQ(1u, high_res_range.start);
158   EXPECT_EQ(2u, high_res_range.end);
159 
160   between_high_and_low_res_range = set_without_low_res->GetTilingRange(
161       PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
162   EXPECT_EQ(2u, between_high_and_low_res_range.start);
163   EXPECT_EQ(4u, between_high_and_low_res_range.end);
164 
165   low_res_range =
166       set_without_low_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
167   EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
168 
169   lower_than_low_res_range = set_without_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);
172 
173   std::unique_ptr<TestablePictureLayerTilingSet>
174       set_with_only_high_and_low_res = CreateTilingSet(&client);
175   high_res_tiling = set_with_only_high_and_low_res->AddTiling(
176       gfx::AxisTransform2d(), raster_source);
177   high_res_tiling->set_resolution(HIGH_RESOLUTION);
178   low_res_tiling = set_with_only_high_and_low_res->AddTiling(
179       gfx::AxisTransform2d(0.5, gfx::Vector2dF()), raster_source);
180   low_res_tiling->set_resolution(LOW_RESOLUTION);
181 
182   higher_than_high_res_range = set_with_only_high_and_low_res->GetTilingRange(
183       PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
184   EXPECT_EQ(0u,
185             higher_than_high_res_range.end - higher_than_high_res_range.start);
186 
187   high_res_range = set_with_only_high_and_low_res->GetTilingRange(
188       PictureLayerTilingSet::HIGH_RES);
189   EXPECT_EQ(0u, high_res_range.start);
190   EXPECT_EQ(1u, high_res_range.end);
191 
192   between_high_and_low_res_range =
193       set_with_only_high_and_low_res->GetTilingRange(
194           PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
195   EXPECT_EQ(0u, between_high_and_low_res_range.end -
196                     between_high_and_low_res_range.start);
197 
198   low_res_range = set_with_only_high_and_low_res->GetTilingRange(
199       PictureLayerTilingSet::LOW_RES);
200   EXPECT_EQ(1u, low_res_range.start);
201   EXPECT_EQ(2u, low_res_range.end);
202 
203   lower_than_low_res_range = set_with_only_high_and_low_res->GetTilingRange(
204       PictureLayerTilingSet::LOWER_THAN_LOW_RES);
205   EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
206 
207   std::unique_ptr<TestablePictureLayerTilingSet> set_with_only_high_res =
208       CreateTilingSet(&client);
209   high_res_tiling =
210       set_with_only_high_res->AddTiling(gfx::AxisTransform2d(), raster_source);
211   high_res_tiling->set_resolution(HIGH_RESOLUTION);
212 
213   higher_than_high_res_range = set_with_only_high_res->GetTilingRange(
214       PictureLayerTilingSet::HIGHER_THAN_HIGH_RES);
215   EXPECT_EQ(0u,
216             higher_than_high_res_range.end - higher_than_high_res_range.start);
217 
218   high_res_range =
219       set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::HIGH_RES);
220   EXPECT_EQ(0u, high_res_range.start);
221   EXPECT_EQ(1u, high_res_range.end);
222 
223   between_high_and_low_res_range = set_with_only_high_res->GetTilingRange(
224       PictureLayerTilingSet::BETWEEN_HIGH_AND_LOW_RES);
225   EXPECT_EQ(0u, between_high_and_low_res_range.end -
226                     between_high_and_low_res_range.start);
227 
228   low_res_range =
229       set_with_only_high_res->GetTilingRange(PictureLayerTilingSet::LOW_RES);
230   EXPECT_EQ(0u, low_res_range.end - low_res_range.start);
231 
232   lower_than_low_res_range = set_with_only_high_res->GetTilingRange(
233       PictureLayerTilingSet::LOWER_THAN_LOW_RES);
234   EXPECT_EQ(0u, lower_than_low_res_range.end - lower_than_low_res_range.start);
235 }
236 
237 class PictureLayerTilingSetTestWithResources : public testing::Test {
238  public:
RunTest(int num_tilings,float min_scale,float scale_increment,float ideal_contents_scale,float expected_scale)239   void RunTest(int num_tilings,
240                float min_scale,
241                float scale_increment,
242                float ideal_contents_scale,
243                float expected_scale) {
244     scoped_refptr<viz::TestContextProvider> context_provider =
245         viz::TestContextProvider::Create();
246     ASSERT_EQ(context_provider->BindToCurrentThread(),
247               gpu::ContextResult::kSuccess);
248     std::unique_ptr<viz::ClientResourceProvider> resource_provider =
249         std::make_unique<viz::ClientResourceProvider>();
250 
251     FakePictureLayerTilingClient client(resource_provider.get(),
252                                         context_provider.get());
253     client.SetTileSize(gfx::Size(256, 256));
254     gfx::Size layer_bounds(1000, 800);
255     std::unique_ptr<TestablePictureLayerTilingSet> set =
256         CreateTilingSet(&client);
257     scoped_refptr<FakeRasterSource> raster_source =
258         FakeRasterSource::CreateFilled(layer_bounds);
259 
260     float scale = min_scale;
261     for (int i = 0; i < num_tilings; ++i, scale += scale_increment) {
262       PictureLayerTiling* tiling = set->AddTiling(
263           gfx::AxisTransform2d(scale, gfx::Vector2dF()), raster_source);
264       tiling->set_resolution(HIGH_RESOLUTION);
265       tiling->CreateAllTilesForTesting();
266       std::vector<Tile*> tiles = tiling->AllTilesForTesting();
267       client.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
268     }
269 
270     float max_contents_scale = scale;
271     gfx::Size content_bounds(
272         gfx::ScaleToCeiledSize(layer_bounds, max_contents_scale));
273     gfx::Rect content_rect(content_bounds);
274 
275     Region remaining(content_rect);
276     PictureLayerTilingSet::CoverageIterator iter(
277         set.get(), max_contents_scale, content_rect, ideal_contents_scale);
278     for (; iter; ++iter) {
279       gfx::Rect geometry_rect = iter.geometry_rect();
280       EXPECT_TRUE(content_rect.Contains(geometry_rect));
281       ASSERT_TRUE(remaining.Contains(geometry_rect));
282       remaining.Subtract(geometry_rect);
283 
284       EXPECT_EQ(expected_scale, iter.CurrentTiling()->contents_scale_key());
285 
286       if (num_tilings)
287         EXPECT_TRUE(*iter);
288       else
289         EXPECT_FALSE(*iter);
290     }
291     EXPECT_TRUE(remaining.IsEmpty());
292   }
293 };
294 
TEST_F(PictureLayerTilingSetTestWithResources,NoTilings)295 TEST_F(PictureLayerTilingSetTestWithResources, NoTilings) {
296   RunTest(0, 0.f, 0.f, 2.f, 0.f);
297 }
TEST_F(PictureLayerTilingSetTestWithResources,OneTiling_Smaller)298 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Smaller) {
299   RunTest(1, 1.f, 0.f, 2.f, 1.f);
300 }
TEST_F(PictureLayerTilingSetTestWithResources,OneTiling_Larger)301 TEST_F(PictureLayerTilingSetTestWithResources, OneTiling_Larger) {
302   RunTest(1, 3.f, 0.f, 2.f, 3.f);
303 }
TEST_F(PictureLayerTilingSetTestWithResources,TwoTilings_Smaller)304 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Smaller) {
305   RunTest(2, 1.f, 1.f, 3.f, 2.f);
306 }
307 
TEST_F(PictureLayerTilingSetTestWithResources,TwoTilings_SmallerEqual)308 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_SmallerEqual) {
309   RunTest(2, 1.f, 1.f, 2.f, 2.f);
310 }
311 
TEST_F(PictureLayerTilingSetTestWithResources,TwoTilings_LargerEqual)312 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_LargerEqual) {
313   RunTest(2, 1.f, 1.f, 1.f, 1.f);
314 }
315 
TEST_F(PictureLayerTilingSetTestWithResources,TwoTilings_Larger)316 TEST_F(PictureLayerTilingSetTestWithResources, TwoTilings_Larger) {
317   RunTest(2, 2.f, 8.f, 1.f, 2.f);
318 }
319 
TEST_F(PictureLayerTilingSetTestWithResources,ManyTilings_Equal)320 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_Equal) {
321   RunTest(10, 1.f, 1.f, 5.f, 5.f);
322 }
323 
TEST_F(PictureLayerTilingSetTestWithResources,ManyTilings_NotEqual)324 TEST_F(PictureLayerTilingSetTestWithResources, ManyTilings_NotEqual) {
325   RunTest(10, 1.f, 1.f, 4.5f, 5.f);
326 }
327 
TEST(PictureLayerTilingSetTest,TileSizeChange)328 TEST(PictureLayerTilingSetTest, TileSizeChange) {
329   FakePictureLayerTilingClient pending_client;
330   FakePictureLayerTilingClient active_client;
331   std::unique_ptr<PictureLayerTilingSet> pending_set =
332       PictureLayerTilingSet::Create(PENDING_TREE, &pending_client, 1000, 1.f,
333                                     1000, 1000.f);
334   std::unique_ptr<PictureLayerTilingSet> active_set =
335       PictureLayerTilingSet::Create(ACTIVE_TREE, &active_client, 1000, 1.f,
336                                     1000, 1000.f);
337 
338   gfx::Size layer_bounds(100, 100);
339   scoped_refptr<FakeRasterSource> raster_source =
340       FakeRasterSource::CreateFilled(layer_bounds);
341 
342   gfx::Size tile_size1(10, 10);
343   gfx::Size tile_size2(30, 30);
344   gfx::Size tile_size3(20, 20);
345 
346   pending_client.SetTileSize(tile_size1);
347   pending_set->AddTiling(gfx::AxisTransform2d(), raster_source);
348   // New tilings get the correct tile size.
349   EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
350 
351   // Set some expected things for the tiling set to function.
352   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
353   active_client.set_twin_tiling_set(pending_set.get());
354 
355   // Set a priority rect so we get tiles.
356   pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
357                                     Occlusion(), false);
358   EXPECT_EQ(tile_size1, pending_set->tiling_at(0)->tile_size());
359 
360   // The tiles should get the correct size.
361   std::vector<Tile*> pending_tiles =
362       pending_set->tiling_at(0)->AllTilesForTesting();
363   EXPECT_GT(pending_tiles.size(), 0u);
364   for (auto* tile : pending_tiles)
365     EXPECT_EQ(tile_size1, tile->content_rect().size());
366 
367   // Update to a new source frame with a new tile size.
368   // Note that setting a new raster source can typically only happen after
369   // activation, since we can't set the raster source twice on the pending tree
370   // without activating. For test, just remove and add a new tiling instead.
371   pending_set->RemoveAllTilings();
372   pending_set->AddTiling(gfx::AxisTransform2d(), raster_source);
373   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
374   pending_client.SetTileSize(tile_size2);
375   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(raster_source.get(),
376                                                            Region(), 1.f, 1.f);
377   // The tiling should get the correct tile size.
378   EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
379 
380   // Set a priority rect so we get tiles.
381   pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 2.0,
382                                     Occlusion(), false);
383   EXPECT_EQ(tile_size2, pending_set->tiling_at(0)->tile_size());
384 
385   // Tiles should have the new correct size.
386   pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
387   EXPECT_GT(pending_tiles.size(), 0u);
388   for (auto* tile : pending_tiles)
389     EXPECT_EQ(tile_size2, tile->content_rect().size());
390 
391   // Clone from the pending to the active tree.
392   active_client.SetTileSize(tile_size2);
393   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
394       raster_source.get(), pending_set.get(), Region(), 1.f, 1.f);
395   // The active tiling should get the right tile size.
396   EXPECT_EQ(tile_size2, active_set->tiling_at(0)->tile_size());
397 
398   // Cloned tiles should have the right size.
399   std::vector<Tile*> active_tiles =
400       active_set->tiling_at(0)->AllTilesForTesting();
401   EXPECT_GT(active_tiles.size(), 0u);
402   for (auto* tile : active_tiles)
403     EXPECT_EQ(tile_size2, tile->content_rect().size());
404 
405   // A new source frame with a new tile size.
406   pending_client.SetTileSize(tile_size3);
407   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(raster_source.get(),
408                                                            Region(), 1.f, 1.f);
409   // The tiling gets the new size correctly.
410   EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
411 
412   // Set a priority rect so we get tiles.
413   pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 3.0,
414                                     Occlusion(), false);
415   EXPECT_EQ(tile_size3, pending_set->tiling_at(0)->tile_size());
416 
417   // Tiles are resized for the new size.
418   pending_tiles = pending_set->tiling_at(0)->AllTilesForTesting();
419   EXPECT_GT(pending_tiles.size(), 0u);
420   for (auto* tile : pending_tiles)
421     EXPECT_EQ(tile_size3, tile->content_rect().size());
422 
423   // Now we activate with a different tile size for the active tiling.
424   active_client.SetTileSize(tile_size3);
425   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
426       raster_source.get(), pending_set.get(), Region(), 1.f, 1.f);
427   // The active tiling changes its tile size.
428   EXPECT_EQ(tile_size3, active_set->tiling_at(0)->tile_size());
429 
430   // And its tiles are resized.
431   active_tiles = active_set->tiling_at(0)->AllTilesForTesting();
432   EXPECT_GT(active_tiles.size(), 0u);
433   for (auto* tile : active_tiles)
434     EXPECT_EQ(tile_size3, tile->content_rect().size());
435 }
436 
TEST(PictureLayerTilingSetTest,MaxContentScale)437 TEST(PictureLayerTilingSetTest, MaxContentScale) {
438   FakePictureLayerTilingClient pending_client;
439   FakePictureLayerTilingClient active_client;
440   std::unique_ptr<PictureLayerTilingSet> pending_set =
441       PictureLayerTilingSet::Create(PENDING_TREE, &pending_client, 1000, 1.f,
442                                     1000, 1000.f);
443   std::unique_ptr<PictureLayerTilingSet> active_set =
444       PictureLayerTilingSet::Create(ACTIVE_TREE, &active_client, 1000, 1.f,
445                                     1000, 1000.f);
446 
447   gfx::Size layer_bounds(100, 105);
448   scoped_refptr<FakeRasterSource> raster_source =
449       FakeRasterSource::CreateEmpty(layer_bounds);
450 
451   // Tilings can be added of any scale, the tiling client can controls this.
452   pending_set->AddTiling(gfx::AxisTransform2d(), raster_source);
453   pending_set->AddTiling(gfx::AxisTransform2d(2.f, gfx::Vector2dF()),
454                          raster_source);
455   pending_set->AddTiling(gfx::AxisTransform2d(3.f, gfx::Vector2dF()),
456                          raster_source);
457 
458   // Set some expected things for the tiling set to function.
459   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
460   active_client.set_twin_tiling_set(pending_set.get());
461 
462   // Update to a new source frame with a max content scale that is larger than
463   // everything.
464   float max_content_scale = 3.f;
465   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
466       raster_source.get(), Region(), 1.f, max_content_scale);
467 
468   // All the tilings are there still.
469   EXPECT_EQ(3u, pending_set->num_tilings());
470 
471   // Clone from the pending to the active tree with the same max content size.
472   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
473       raster_source.get(), pending_set.get(), Region(), 1.f, max_content_scale);
474   // All the tilings are on the active tree.
475   EXPECT_EQ(3u, active_set->num_tilings());
476 
477   // Update to a new source frame with a max content scale that will drop one
478   // tiling.
479   max_content_scale = 2.9f;
480   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(
481       raster_source.get(), Region(), 1.f, max_content_scale);
482   // All the tilings are there still.
483   EXPECT_EQ(2u, pending_set->num_tilings());
484 
485   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
486 
487   // Clone from the pending to the active tree with the same max content size.
488   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
489       raster_source.get(), pending_set.get(), Region(), 1.f, max_content_scale);
490   // All the tilings are on the active tree.
491   EXPECT_EQ(2u, active_set->num_tilings());
492 }
493 
TEST(PictureLayerTilingSetTest,SkewportLimits)494 TEST(PictureLayerTilingSetTest, SkewportLimits) {
495   FakePictureLayerTilingClient client;
496 
497   gfx::Rect viewport(0, 0, 100, 100);
498   gfx::Size layer_bounds(200, 200);
499 
500   client.SetTileSize(gfx::Size(100, 100));
501   LayerTreeSettings settings;
502   settings.skewport_extrapolation_limit_in_screen_pixels = 75;
503   settings.tiling_interest_area_padding = 1000000;
504 
505   scoped_refptr<FakeRasterSource> raster_source =
506       FakeRasterSource::CreateFilled(layer_bounds);
507   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
508       CreateTilingSetWithSettings(&client, settings);
509 
510   EXPECT_FALSE(tiling_set->TilingsNeedUpdate(viewport, 1.0));
511   tiling_set->AddTiling(gfx::AxisTransform2d(), raster_source);
512   EXPECT_TRUE(tiling_set->TilingsNeedUpdate(viewport, 1.0));
513 
514   tiling_set->UpdateTilePriorities(viewport, 1.f, 1.0, Occlusion(), true);
515 
516   // Move viewport down 50 pixels in 0.5 seconds.
517   gfx::Rect down_skewport =
518       tiling_set->ComputeSkewport(gfx::Rect(0, 50, 100, 100), 1.5, 1.f);
519 
520   EXPECT_EQ(0, down_skewport.x());
521   EXPECT_EQ(50, down_skewport.y());
522   EXPECT_EQ(100, down_skewport.width());
523   EXPECT_EQ(175, down_skewport.height());
524   EXPECT_TRUE(down_skewport.Contains(gfx::Rect(0, 50, 100, 100)));
525 
526   // Move viewport down 50 and right 10 pixels.
527   gfx::Rect down_right_skewport =
528       tiling_set->ComputeSkewport(gfx::Rect(10, 50, 100, 100), 1.5, 1.f);
529 
530   EXPECT_EQ(10, down_right_skewport.x());
531   EXPECT_EQ(50, down_right_skewport.y());
532   EXPECT_EQ(120, down_right_skewport.width());
533   EXPECT_EQ(175, down_right_skewport.height());
534   EXPECT_TRUE(down_right_skewport.Contains(gfx::Rect(10, 50, 100, 100)));
535 
536   // Move viewport left.
537   gfx::Rect left_skewport =
538       tiling_set->ComputeSkewport(gfx::Rect(-50, 0, 100, 100), 1.5, 1.f);
539 
540   EXPECT_EQ(-125, left_skewport.x());
541   EXPECT_EQ(0, left_skewport.y());
542   EXPECT_EQ(175, left_skewport.width());
543   EXPECT_EQ(100, left_skewport.height());
544   EXPECT_TRUE(left_skewport.Contains(gfx::Rect(-50, 0, 100, 100)));
545 
546   // Expand viewport.
547   gfx::Rect expand_skewport =
548       tiling_set->ComputeSkewport(gfx::Rect(-50, -50, 200, 200), 1.5, 1.f);
549 
550   // x and y moved by -75 (-50 - 75 = -125).
551   // right side and bottom side moved by 75 [(350 - 125) - (200 - 50) = 75].
552   EXPECT_EQ(-125, expand_skewport.x());
553   EXPECT_EQ(-125, expand_skewport.y());
554   EXPECT_EQ(350, expand_skewport.width());
555   EXPECT_EQ(350, expand_skewport.height());
556   EXPECT_TRUE(expand_skewport.Contains(gfx::Rect(-50, -50, 200, 200)));
557 
558   // Expand the viewport past the limit in all directions.
559   gfx::Rect big_expand_skewport =
560       tiling_set->ComputeSkewport(gfx::Rect(-500, -500, 1500, 1500), 1.5, 1.f);
561 
562   EXPECT_EQ(-575, big_expand_skewport.x());
563   EXPECT_EQ(-575, big_expand_skewport.y());
564   EXPECT_EQ(1650, big_expand_skewport.width());
565   EXPECT_EQ(1650, big_expand_skewport.height());
566   EXPECT_TRUE(big_expand_skewport.Contains(gfx::Rect(-500, -500, 1500, 1500)));
567 
568   // Shrink the skewport in all directions.
569   gfx::Rect shrink_viewport =
570       tiling_set->ComputeSkewport(gfx::Rect(0, 0, 100, 100), 1.5, 1.f);
571   EXPECT_EQ(0, shrink_viewport.x());
572   EXPECT_EQ(0, shrink_viewport.y());
573   EXPECT_EQ(100, shrink_viewport.width());
574   EXPECT_EQ(100, shrink_viewport.height());
575 
576   // Move the skewport really far in one direction.
577   gfx::Rect move_skewport_far =
578       tiling_set->ComputeSkewport(gfx::Rect(0, 5000, 100, 100), 1.5, 1.f);
579   EXPECT_EQ(0, move_skewport_far.x());
580   EXPECT_EQ(5000, move_skewport_far.y());
581   EXPECT_EQ(100, move_skewport_far.width());
582   EXPECT_EQ(175, move_skewport_far.height());
583   EXPECT_TRUE(move_skewport_far.Contains(gfx::Rect(0, 5000, 100, 100)));
584 }
585 
TEST(PictureLayerTilingSetTest,ComputeSkewportExtremeCases)586 TEST(PictureLayerTilingSetTest, ComputeSkewportExtremeCases) {
587   FakePictureLayerTilingClient client;
588 
589   gfx::Size layer_bounds(200, 200);
590   client.SetTileSize(gfx::Size(100, 100));
591   LayerTreeSettings settings;
592   settings.tiling_interest_area_padding = 1000000000;
593   scoped_refptr<FakeRasterSource> raster_source =
594       FakeRasterSource::CreateFilled(layer_bounds);
595   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
596       CreateTilingSetWithSettings(&client, settings);
597   tiling_set->AddTiling(gfx::AxisTransform2d(), raster_source);
598 
599   gfx::Rect viewport1(-1918, 255860, 4010, 2356);
600   gfx::Rect viewport2(-7088, -91738, 14212, 8350);
601   gfx::Rect viewport3(-12730024, -158883296, 24607540, 14454512);
602   double time = 1.0;
603   tiling_set->UpdateTilePriorities(viewport1, 1.f, time, Occlusion(), true);
604   time += 0.016;
605   EXPECT_TRUE(
606       tiling_set->ComputeSkewport(viewport2, time, 1.f).Contains(viewport2));
607   tiling_set->UpdateTilePriorities(viewport2, 1.f, time, Occlusion(), true);
608   time += 0.016;
609   EXPECT_TRUE(
610       tiling_set->ComputeSkewport(viewport3, time, 1.f).Contains(viewport3));
611 
612   // Use a tiling with a large scale, so the viewport times the scale no longer
613   // fits into integers, and the viewport is not anywhere close to the tiling.
614   PictureLayerTiling* tiling = tiling_set->AddTiling(
615       gfx::AxisTransform2d(1000.f, gfx::Vector2dF()), raster_source);
616   EXPECT_TRUE(tiling_set->TilingsNeedUpdate(viewport3, time));
617   tiling_set->UpdateTilePriorities(viewport3, 1.f, time, Occlusion(), true);
618   EXPECT_TRUE(tiling->GetCurrentVisibleRectForTesting().IsEmpty());
619 }
620 
TEST(PictureLayerTilingSetTest,ComputeSkewport)621 TEST(PictureLayerTilingSetTest, ComputeSkewport) {
622   FakePictureLayerTilingClient client;
623 
624   gfx::Rect viewport(0, 0, 100, 100);
625   gfx::Size layer_bounds(200, 200);
626 
627   client.SetTileSize(gfx::Size(100, 100));
628 
629   scoped_refptr<FakeRasterSource> raster_source =
630       FakeRasterSource::CreateFilled(layer_bounds);
631   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
632       CreateTilingSet(&client);
633   tiling_set->AddTiling(gfx::AxisTransform2d(), raster_source);
634 
635   tiling_set->UpdateTilePriorities(viewport, 1.f, 1.0, Occlusion(), true);
636 
637   // Move viewport down 50 pixels in 0.5 seconds.
638   gfx::Rect down_skewport =
639       tiling_set->ComputeSkewport(gfx::Rect(0, 50, 100, 100), 1.5, 1.f);
640 
641   EXPECT_EQ(0, down_skewport.x());
642   EXPECT_EQ(50, down_skewport.y());
643   EXPECT_EQ(100, down_skewport.width());
644   EXPECT_EQ(200, down_skewport.height());
645 
646   // Shrink viewport.
647   gfx::Rect shrink_skewport =
648       tiling_set->ComputeSkewport(gfx::Rect(25, 25, 50, 50), 1.5, 1.f);
649 
650   EXPECT_EQ(25, shrink_skewport.x());
651   EXPECT_EQ(25, shrink_skewport.y());
652   EXPECT_EQ(50, shrink_skewport.width());
653   EXPECT_EQ(50, shrink_skewport.height());
654 
655   // Move viewport down 50 and right 10 pixels.
656   gfx::Rect down_right_skewport =
657       tiling_set->ComputeSkewport(gfx::Rect(10, 50, 100, 100), 1.5, 1.f);
658 
659   EXPECT_EQ(10, down_right_skewport.x());
660   EXPECT_EQ(50, down_right_skewport.y());
661   EXPECT_EQ(120, down_right_skewport.width());
662   EXPECT_EQ(200, down_right_skewport.height());
663 
664   // Move viewport left.
665   gfx::Rect left_skewport =
666       tiling_set->ComputeSkewport(gfx::Rect(-20, 0, 100, 100), 1.5, 1.f);
667 
668   EXPECT_EQ(-60, left_skewport.x());
669   EXPECT_EQ(0, left_skewport.y());
670   EXPECT_EQ(140, left_skewport.width());
671   EXPECT_EQ(100, left_skewport.height());
672 
673   // Expand viewport in 0.2 seconds.
674   gfx::Rect expanded_skewport =
675       tiling_set->ComputeSkewport(gfx::Rect(-5, -5, 110, 110), 1.2, 1.f);
676 
677   EXPECT_EQ(-30, expanded_skewport.x());
678   EXPECT_EQ(-30, expanded_skewport.y());
679   EXPECT_EQ(160, expanded_skewport.width());
680   EXPECT_EQ(160, expanded_skewport.height());
681 }
682 
TEST(PictureLayerTilingSetTest,SkewportThroughUpdateTilePriorities)683 TEST(PictureLayerTilingSetTest, SkewportThroughUpdateTilePriorities) {
684   FakePictureLayerTilingClient client;
685 
686   gfx::Rect viewport(0, 0, 100, 100);
687   gfx::Size layer_bounds(200, 200);
688 
689   client.SetTileSize(gfx::Size(100, 100));
690 
691   scoped_refptr<FakeRasterSource> raster_source =
692       FakeRasterSource::CreateFilled(layer_bounds);
693   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
694       CreateTilingSet(&client);
695   tiling_set->AddTiling(gfx::AxisTransform2d(), raster_source);
696 
697   tiling_set->UpdateTilePriorities(viewport, 1.f, 1.0, Occlusion(), true);
698 
699   // Move viewport down 50 pixels in 0.5 seconds.
700   gfx::Rect viewport_50 = gfx::Rect(0, 50, 100, 100);
701   gfx::Rect skewport_50 = tiling_set->ComputeSkewport(viewport_50, 1.5, 1.f);
702 
703   EXPECT_EQ(gfx::Rect(0, 50, 100, 200), skewport_50);
704   tiling_set->UpdateTilePriorities(viewport_50, 1.f, 1.5, Occlusion(), true);
705 
706   gfx::Rect viewport_100 = gfx::Rect(0, 100, 100, 100);
707   gfx::Rect skewport_100 = tiling_set->ComputeSkewport(viewport_100, 2.0, 1.f);
708 
709   EXPECT_EQ(gfx::Rect(0, 100, 100, 200), skewport_100);
710   tiling_set->UpdateTilePriorities(viewport_100, 1.f, 2.0, Occlusion(), true);
711 
712   // Advance time, but not the viewport.
713   gfx::Rect result = tiling_set->ComputeSkewport(viewport_100, 2.5, 1.f);
714   // Since the history did advance, we should still get a skewport but a smaller
715   // one.
716   EXPECT_EQ(gfx::Rect(0, 100, 100, 150), result);
717   tiling_set->UpdateTilePriorities(viewport_100, 1.f, 2.5, Occlusion(), true);
718 
719   // Advance time again.
720   result = tiling_set->ComputeSkewport(viewport_100, 3.0, 1.f);
721   EXPECT_EQ(viewport_100, result);
722   tiling_set->UpdateTilePriorities(viewport_100, 1.f, 3.0, Occlusion(), true);
723 
724   // Ensure we have a skewport.
725   gfx::Rect viewport_150 = gfx::Rect(0, 150, 100, 100);
726   gfx::Rect skewport_150 = tiling_set->ComputeSkewport(viewport_150, 3.5, 1.f);
727   EXPECT_EQ(gfx::Rect(0, 150, 100, 150), skewport_150);
728   tiling_set->UpdateTilePriorities(viewport_150, 1.f, 3.5, Occlusion(), true);
729 
730   // Advance the viewport, but not the time.
731   gfx::Rect viewport_200 = gfx::Rect(0, 200, 100, 100);
732   gfx::Rect skewport_200 = tiling_set->ComputeSkewport(viewport_200, 3.5, 1.f);
733   EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200);
734 
735   // Ensure that continued calls with the same value, produce the same skewport.
736   tiling_set->UpdateTilePriorities(viewport_150, 1.f, 3.5, Occlusion(), true);
737   EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200);
738   tiling_set->UpdateTilePriorities(viewport_150, 1.f, 3.5, Occlusion(), true);
739   EXPECT_EQ(gfx::Rect(0, 200, 100, 300), skewport_200);
740 
741   tiling_set->UpdateTilePriorities(viewport_200, 1.f, 3.5, Occlusion(), true);
742 
743   // This should never happen, but advance the viewport yet again keeping the
744   // time the same.
745   gfx::Rect viewport_250 = gfx::Rect(0, 250, 100, 100);
746   gfx::Rect skewport_250 = tiling_set->ComputeSkewport(viewport_250, 3.5, 1.f);
747   EXPECT_EQ(viewport_250, skewport_250);
748   tiling_set->UpdateTilePriorities(viewport_250, 1.f, 3.5, Occlusion(), true);
749 }
750 
TEST(PictureLayerTilingTest,ViewportDistanceWithScale)751 TEST(PictureLayerTilingTest, ViewportDistanceWithScale) {
752   FakePictureLayerTilingClient client;
753 
754   gfx::Rect viewport(0, 0, 100, 100);
755   gfx::Size layer_bounds(1500, 1500);
756 
757   client.SetTileSize(gfx::Size(10, 10));
758   LayerTreeSettings settings;
759 
760   // Tiling at 0.25 scale: this should create 47x47 tiles of size 10x10.
761   // The reason is that each tile has a one pixel border, so tile at (1, 2)
762   // for instance begins at (8, 16) pixels. So tile at (46, 46) will begin at
763   // (368, 368) and extend to the end of 1500 * 0.25 = 375 edge of the
764   // tiling.
765   scoped_refptr<FakeRasterSource> raster_source =
766       FakeRasterSource::CreateFilled(layer_bounds);
767   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
768       CreateTilingSet(&client);
769   auto* tiling = tiling_set->AddTiling(
770       gfx::AxisTransform2d(0.25f, gfx::Vector2dF()), raster_source);
771   tiling->set_resolution(HIGH_RESOLUTION);
772   gfx::Rect viewport_in_content_space =
773       gfx::ScaleToEnclosedRect(viewport, 0.25f);
774 
775   tiling_set->UpdateTilePriorities(viewport, 1.f, 1.0, Occlusion(), true);
776   auto prioritized_tiles = tiling->UpdateAndGetAllPrioritizedTilesForTesting();
777 
778   // Compute the soon border.
779   gfx::Rect soon_border_rect_in_content_space =
780       tiling_set->ComputeSoonBorderRect(viewport, 1.f);
781   soon_border_rect_in_content_space =
782       gfx::ScaleToEnclosedRect(soon_border_rect_in_content_space, 0.25f);
783 
784   // Sanity checks.
785   for (int i = 0; i < 47; ++i) {
786     for (int j = 0; j < 47; ++j) {
787       EXPECT_TRUE(tiling->TileAt(i, j)) << "i: " << i << " j: " << j;
788     }
789   }
790   for (int i = 0; i < 47; ++i) {
791     EXPECT_FALSE(tiling->TileAt(i, 47)) << "i: " << i;
792     EXPECT_FALSE(tiling->TileAt(47, i)) << "i: " << i;
793   }
794 
795   // No movement in the viewport implies that tiles will either be NOW
796   // or EVENTUALLY, with the exception of tiles that are between 0 and 312
797   // pixels away from the viewport, which will be in the SOON bin.
798   bool have_now = false;
799   bool have_eventually = false;
800   bool have_soon = false;
801   for (int i = 0; i < 47; ++i) {
802     for (int j = 0; j < 47; ++j) {
803       Tile* tile = tiling->TileAt(i, j);
804       PrioritizedTile prioritized_tile = prioritized_tiles[tile];
805       TilePriority priority = prioritized_tile.priority();
806 
807       gfx::Rect tile_rect = tiling->TilingDataForTesting().TileBounds(i, j);
808       if (viewport_in_content_space.Intersects(tile_rect)) {
809         EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
810         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible);
811         have_now = true;
812       } else if (soon_border_rect_in_content_space.Intersects(tile_rect)) {
813         EXPECT_EQ(TilePriority::SOON, priority.priority_bin);
814         have_soon = true;
815       } else {
816         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin);
817         EXPECT_GT(priority.distance_to_visible, 0.f);
818         have_eventually = true;
819       }
820     }
821   }
822 
823   EXPECT_TRUE(have_now);
824   EXPECT_TRUE(have_soon);
825   EXPECT_TRUE(have_eventually);
826 
827   // Spot check some distances.
828   // Tile at 5, 1 should begin at 41x9 in content space (without borders),
829   // so the distance to a viewport that ends at 25x25 in content space
830   // should be 17 (41 - 25 + 1). In layer space, then that should be
831   // 17 / 0.25 = 68 pixels.
832 
833   // We can verify that the content rect (with borders) is one pixel off
834   // 41,9 8x8 on all sides.
835   EXPECT_EQ(tiling->TileAt(5, 1)->content_rect().ToString(), "40,8 10x10");
836   TilePriority priority = prioritized_tiles[tiling->TileAt(5, 1)].priority();
837   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
838 
839   priority = prioritized_tiles[tiling->TileAt(2, 5)].priority();
840   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
841 
842   priority = prioritized_tiles[tiling->TileAt(3, 4)].priority();
843   EXPECT_FLOAT_EQ(40.f, priority.distance_to_visible);
844 
845   // Move the viewport down 40 pixels.
846   viewport = gfx::Rect(0, 40, 100, 100);
847   viewport_in_content_space = gfx::ScaleToEnclosedRect(viewport, 0.25f);
848   gfx::Rect skewport_in_content_space =
849       tiling_set->ComputeSkewport(viewport, 2.0, 1.f);
850   skewport_in_content_space =
851       gfx::ScaleToEnclosedRect(skewport_in_content_space, 0.25f);
852 
853   // Compute the soon border.
854   soon_border_rect_in_content_space =
855       tiling_set->ComputeSoonBorderRect(viewport, 1.f);
856   soon_border_rect_in_content_space =
857       gfx::ScaleToEnclosedRect(soon_border_rect_in_content_space, 0.25f);
858 
859   EXPECT_EQ(0, skewport_in_content_space.x());
860   EXPECT_EQ(10, skewport_in_content_space.y());
861   EXPECT_EQ(25, skewport_in_content_space.width());
862   EXPECT_EQ(35, skewport_in_content_space.height());
863 
864   EXPECT_TRUE(tiling_set->TilingsNeedUpdate(viewport, 2.0));
865   tiling_set->UpdateTilePriorities(viewport, 1.f, 2.0, Occlusion(), true);
866   prioritized_tiles = tiling->UpdateAndGetAllPrioritizedTilesForTesting();
867 
868   have_now = false;
869   have_eventually = false;
870   have_soon = false;
871 
872   // Viewport moved, so we expect to find some NOW tiles, some SOON tiles and
873   // some EVENTUALLY tiles.
874   for (int i = 0; i < 47; ++i) {
875     for (int j = 0; j < 47; ++j) {
876       Tile* tile = tiling->TileAt(i, j);
877       priority = prioritized_tiles[tile].priority();
878 
879       gfx::Rect tile_rect = tiling->TilingDataForTesting().TileBounds(i, j);
880       if (viewport_in_content_space.Intersects(tile_rect)) {
881         EXPECT_EQ(TilePriority::NOW, priority.priority_bin) << "i: " << i
882                                                             << " j: " << j;
883         EXPECT_FLOAT_EQ(0.f, priority.distance_to_visible) << "i: " << i
884                                                            << " j: " << j;
885         have_now = true;
886       } else if (skewport_in_content_space.Intersects(tile_rect) ||
887                  soon_border_rect_in_content_space.Intersects(tile_rect)) {
888         EXPECT_EQ(TilePriority::SOON, priority.priority_bin) << "i: " << i
889                                                              << " j: " << j;
890         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
891                                                      << " j: " << j;
892         have_soon = true;
893       } else {
894         EXPECT_EQ(TilePriority::EVENTUALLY, priority.priority_bin)
895             << "i: " << i << " j: " << j;
896         EXPECT_GT(priority.distance_to_visible, 0.f) << "i: " << i
897                                                      << " j: " << j;
898         have_eventually = true;
899       }
900     }
901   }
902 
903   EXPECT_TRUE(have_now);
904   EXPECT_TRUE(have_soon);
905   EXPECT_TRUE(have_eventually);
906 
907   priority = prioritized_tiles[tiling->TileAt(5, 1)].priority();
908   EXPECT_FLOAT_EQ(68.f, priority.distance_to_visible);
909 
910   priority = prioritized_tiles[tiling->TileAt(2, 5)].priority();
911   EXPECT_FLOAT_EQ(28.f, priority.distance_to_visible);
912 
913   priority = prioritized_tiles[tiling->TileAt(3, 4)].priority();
914   EXPECT_FLOAT_EQ(4.f, priority.distance_to_visible);
915 
916   // Change the underlying layer scale.
917   tiling_set->UpdateTilePriorities(viewport, 2.0f, 3.0, Occlusion(), true);
918   prioritized_tiles = tiling->UpdateAndGetAllPrioritizedTilesForTesting();
919 
920   priority = prioritized_tiles[tiling->TileAt(5, 1)].priority();
921   EXPECT_FLOAT_EQ(136.f, priority.distance_to_visible);
922 
923   priority = prioritized_tiles[tiling->TileAt(2, 5)].priority();
924   EXPECT_FLOAT_EQ(56.f, priority.distance_to_visible);
925 
926   priority = prioritized_tiles[tiling->TileAt(3, 4)].priority();
927   EXPECT_FLOAT_EQ(8.f, priority.distance_to_visible);
928 
929   // Test additional scales.
930   tiling = tiling_set->AddTiling(gfx::AxisTransform2d(0.2f, gfx::Vector2dF()),
931                                  raster_source);
932   tiling->set_resolution(HIGH_RESOLUTION);
933   tiling_set->UpdateTilePriorities(viewport, 1.0f, 4.0, Occlusion(), true);
934   prioritized_tiles = tiling->UpdateAndGetAllPrioritizedTilesForTesting();
935 
936   priority = prioritized_tiles[tiling->TileAt(5, 1)].priority();
937   EXPECT_FLOAT_EQ(110.f, priority.distance_to_visible);
938 
939   priority = prioritized_tiles[tiling->TileAt(2, 5)].priority();
940   EXPECT_FLOAT_EQ(70.f, priority.distance_to_visible);
941 
942   priority = prioritized_tiles[tiling->TileAt(3, 4)].priority();
943   EXPECT_FLOAT_EQ(60.f, priority.distance_to_visible);
944 
945   tiling_set->UpdateTilePriorities(viewport, 0.5f, 5.0, Occlusion(), true);
946   prioritized_tiles = tiling->UpdateAndGetAllPrioritizedTilesForTesting();
947 
948   priority = prioritized_tiles[tiling->TileAt(5, 1)].priority();
949   EXPECT_FLOAT_EQ(55.f, priority.distance_to_visible);
950 
951   priority = prioritized_tiles[tiling->TileAt(2, 5)].priority();
952   EXPECT_FLOAT_EQ(35.f, priority.distance_to_visible);
953 
954   priority = prioritized_tiles[tiling->TileAt(3, 4)].priority();
955   EXPECT_FLOAT_EQ(30.f, priority.distance_to_visible);
956 }
957 
TEST(PictureLayerTilingTest,InvalidateAfterComputeTilePriorityRects)958 TEST(PictureLayerTilingTest, InvalidateAfterComputeTilePriorityRects) {
959   FakePictureLayerTilingClient pending_client;
960   pending_client.SetTileSize(gfx::Size(100, 100));
961 
962   scoped_refptr<FakeRasterSource> raster_source =
963       FakeRasterSource::CreateFilled(gfx::Size(100, 100));
964   std::unique_ptr<TestablePictureLayerTilingSet> tiling_set =
965       CreateTilingSet(&pending_client);
966   auto* pending_tiling =
967       tiling_set->AddTiling(gfx::AxisTransform2d(), raster_source);
968   pending_tiling->set_resolution(HIGH_RESOLUTION);
969 
970   // Ensure that we can compute tile priority rects, invalidate, and compute the
971   // rects again. It is important that the second compute tile priority rects
972   // return true, indicating that things have changed (since invalidation has
973   // changed things). This causes PrepareTiles to be properly scheduled. If the
974   // second ComputeTilePriorityRects returns false, then we assume that
975   // PrepareTiles isn't needed and we signal that we're ready to draw
976   // immediately, which can cause visual glitches.
977   //
978   // This can happen we if we process an impl frame deadline before processing a
979   // commit. That is, when we draw we ComputeTilePriorityRects. If we process
980   // the commit afterwards, it would use the same timestamp and sometimes would
981   // use the same viewport to compute tile priority rects again.
982   double time = 1.;
983   gfx::Rect viewport(0, 0, 100, 100);
984   EXPECT_TRUE(
985       tiling_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
986   EXPECT_FALSE(
987       tiling_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
988 
989   // This will invalidate tilings.
990   tiling_set->Invalidate(Region());
991 
992   EXPECT_TRUE(
993       tiling_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
994 }
995 
TEST(PictureLayerTilingTest,InvalidateAfterUpdateRasterSourceForCommit)996 TEST(PictureLayerTilingTest, InvalidateAfterUpdateRasterSourceForCommit) {
997   FakePictureLayerTilingClient pending_client;
998   FakePictureLayerTilingClient active_client;
999   std::unique_ptr<PictureLayerTilingSet> pending_set =
1000       PictureLayerTilingSet::Create(PENDING_TREE, &pending_client, 1000, 1.f,
1001                                     1000, 1000.f);
1002   std::unique_ptr<PictureLayerTilingSet> active_set =
1003       PictureLayerTilingSet::Create(ACTIVE_TREE, &active_client, 1000, 1.f,
1004                                     1000, 1000.f);
1005 
1006   gfx::Size layer_bounds(100, 100);
1007   scoped_refptr<FakeRasterSource> raster_source =
1008       FakeRasterSource::CreateFilled(layer_bounds);
1009 
1010   auto* pending_tiling =
1011       pending_set->AddTiling(gfx::AxisTransform2d(), raster_source);
1012   pending_tiling->set_resolution(HIGH_RESOLUTION);
1013   active_client.set_twin_tiling_set(pending_set.get());
1014 
1015   double time = 1.;
1016   gfx::Rect viewport(0, 0, 100, 100);
1017 
1018   // The first commit will update the raster source for pending tilings.
1019   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(raster_source,
1020                                                            Region(), 1.f, 1.f);
1021   // UpdateTilePriorities for pending set gets called during UDP in commit.
1022   EXPECT_TRUE(pending_set->UpdateTilePriorities(viewport, 1.f, time,
1023                                                 Occlusion(), true));
1024   // The active set doesn't have tilings yet.
1025   EXPECT_FALSE(
1026       active_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
1027 
1028   // On activation tilings are copied from pending set to active set.
1029   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
1030       raster_source, pending_set.get(), Region(), 1.f, 1.f);
1031   // Pending set doesn't have any tilings now.
1032   EXPECT_FALSE(pending_set->UpdateTilePriorities(viewport, 1.f, time,
1033                                                  Occlusion(), true));
1034   // UpdateTilePriorities for active set gets called during UDP in draw.
1035   EXPECT_TRUE(
1036       active_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
1037 
1038   // Even though frame time and viewport haven't changed since last commit we
1039   // update tile priorities because of potential invalidations.
1040   pending_set->UpdateTilingsToCurrentRasterSourceForCommit(raster_source,
1041                                                            Region(), 1.f, 1.f);
1042   // UpdateTilePriorities for pending set gets called during UDP in commit.
1043   EXPECT_TRUE(pending_set->UpdateTilePriorities(viewport, 1.f, time,
1044                                                 Occlusion(), true));
1045   // No changes for active set until activation.
1046   EXPECT_FALSE(
1047       active_set->UpdateTilePriorities(viewport, 1.f, time, Occlusion(), true));
1048 }
1049 
TEST(PictureLayerTilingSetTest,TilingTranslationChanges)1050 TEST(PictureLayerTilingSetTest, TilingTranslationChanges) {
1051   gfx::Size tile_size(64, 64);
1052   FakePictureLayerTilingClient pending_client;
1053   FakePictureLayerTilingClient active_client;
1054   pending_client.SetTileSize(tile_size);
1055   active_client.SetTileSize(tile_size);
1056   std::unique_ptr<PictureLayerTilingSet> pending_set =
1057       PictureLayerTilingSet::Create(PENDING_TREE, &pending_client, 0, 1.f, 0,
1058                                     0.f);
1059   std::unique_ptr<PictureLayerTilingSet> active_set =
1060       PictureLayerTilingSet::Create(ACTIVE_TREE, &active_client, 0, 1.f, 0,
1061                                     0.f);
1062   active_client.set_twin_tiling_set(pending_set.get());
1063 
1064   gfx::Size layer_bounds(100, 100);
1065   scoped_refptr<FakeRasterSource> raster_source =
1066       FakeRasterSource::CreateFilled(layer_bounds);
1067 
1068   gfx::AxisTransform2d raster_transform1(1.f, gfx::Vector2dF(0.25f, 0.25f));
1069   pending_set->AddTiling(raster_transform1, raster_source);
1070   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
1071 
1072   // Set a priority rect so we get tiles.
1073   pending_set->UpdateTilePriorities(gfx::Rect(layer_bounds), 1.f, 1.0,
1074                                     Occlusion(), false);
1075 
1076   // Make sure all tiles are generated.
1077   EXPECT_EQ(4u, pending_set->tiling_at(0)->AllTilesForTesting().size());
1078 
1079   // Clone from the pending to the active tree.
1080   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
1081       raster_source.get(), pending_set.get(), Region(), 1.f, 1.f);
1082 
1083   // Verifies active tree cloned the tiling correctly.
1084   ASSERT_EQ(1u, active_set->num_tilings());
1085   EXPECT_EQ(active_set->tiling_at(0)->raster_transform(), raster_transform1);
1086   EXPECT_EQ(4u, active_set->tiling_at(0)->AllTilesForTesting().size());
1087 
1088   // Change raster translation on the pending set.
1089   gfx::AxisTransform2d raster_transform2(1.f, gfx::Vector2dF(0.75f, 0.75f));
1090   pending_set->RemoveAllTilings();
1091   pending_set->AddTiling(raster_transform2, raster_source);
1092   pending_set->tiling_at(0)->set_resolution(HIGH_RESOLUTION);
1093 
1094   // Set a different priority rect to get one tile.
1095   pending_set->UpdateTilePriorities(gfx::Rect(1, 1), 1.f, 1.0, Occlusion(),
1096                                     false);
1097   EXPECT_EQ(1u, pending_set->tiling_at(0)->AllTilesForTesting().size());
1098 
1099   // Commit the pending to the active tree again.
1100   active_set->UpdateTilingsToCurrentRasterSourceForActivation(
1101       raster_source.get(), pending_set.get(), Region(), 1.f, 1.f);
1102 
1103   // Verifies the old tiling with a different translation is dropped.
1104   ASSERT_EQ(1u, active_set->num_tilings());
1105   EXPECT_EQ(active_set->tiling_at(0)->raster_transform(), raster_transform2);
1106   EXPECT_EQ(1u, active_set->tiling_at(0)->AllTilesForTesting().size());
1107 }
1108 
1109 }  // namespace
1110 }  // namespace cc
1111