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