Fix race condition in gyp/ninja builds.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl_unittest.cc
blob2258655dbf6f678b4858290e86c6d30abbfed1d8
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/picture_layer_impl.h"
7 #include <algorithm>
8 #include <limits>
9 #include <set>
10 #include <utility>
12 #include "cc/layers/append_quads_data.h"
13 #include "cc/layers/picture_layer.h"
14 #include "cc/quads/draw_quad.h"
15 #include "cc/test/fake_content_layer_client.h"
16 #include "cc/test/fake_impl_proxy.h"
17 #include "cc/test/fake_layer_tree_host_impl.h"
18 #include "cc/test/fake_output_surface.h"
19 #include "cc/test/fake_picture_layer_impl.h"
20 #include "cc/test/fake_picture_pile_impl.h"
21 #include "cc/test/geometry_test_utils.h"
22 #include "cc/test/impl_side_painting_settings.h"
23 #include "cc/test/layer_test_common.h"
24 #include "cc/test/test_shared_bitmap_manager.h"
25 #include "cc/test/test_web_graphics_context_3d.h"
26 #include "cc/trees/layer_tree_impl.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "ui/gfx/rect_conversions.h"
29 #include "ui/gfx/size_conversions.h"
31 namespace cc {
32 namespace {
34 class MockCanvas : public SkCanvas {
35 public:
36 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
38 virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
39 // Capture calls before SkCanvas quickReject() kicks in.
40 rects_.push_back(rect);
43 std::vector<SkRect> rects_;
46 class PictureLayerImplTest : public testing::Test {
47 public:
48 PictureLayerImplTest()
49 : proxy_(base::MessageLoopProxy::current()),
50 host_impl_(ImplSidePaintingSettings(),
51 &proxy_,
52 &shared_bitmap_manager_),
53 id_(7) {}
55 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
56 : proxy_(base::MessageLoopProxy::current()),
57 host_impl_(settings, &proxy_, &shared_bitmap_manager_),
58 id_(7) {}
60 virtual ~PictureLayerImplTest() {
63 virtual void SetUp() OVERRIDE {
64 InitializeRenderer();
67 virtual void InitializeRenderer() {
68 host_impl_.InitializeRenderer(
69 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
72 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
73 gfx::Size tile_size(100, 100);
75 scoped_refptr<FakePicturePileImpl> pending_pile =
76 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
77 scoped_refptr<FakePicturePileImpl> active_pile =
78 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
80 SetupTrees(pending_pile, active_pile);
83 void ActivateTree() {
84 host_impl_.ActivateSyncTree();
85 CHECK(!host_impl_.pending_tree());
86 pending_layer_ = NULL;
87 active_layer_ = static_cast<FakePictureLayerImpl*>(
88 host_impl_.active_tree()->LayerById(id_));
91 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
92 const gfx::Size& tile_size) {
93 SetupDefaultTrees(layer_bounds);
94 pending_layer_->set_fixed_tile_size(tile_size);
95 active_layer_->set_fixed_tile_size(tile_size);
98 void SetupTrees(
99 scoped_refptr<PicturePileImpl> pending_pile,
100 scoped_refptr<PicturePileImpl> active_pile) {
101 SetupPendingTree(active_pile);
102 ActivateTree();
103 SetupPendingTree(pending_pile);
104 host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
105 host_impl_.active_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
108 void CreateHighLowResAndSetAllTilesVisible() {
109 // Active layer must get updated first so pending layer can share from it.
110 active_layer_->CreateDefaultTilingsAndTiles();
111 active_layer_->SetAllTilesVisible();
112 pending_layer_->CreateDefaultTilingsAndTiles();
113 pending_layer_->SetAllTilesVisible();
116 void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
117 active_layer_->AddTiling(2.3f);
118 active_layer_->AddTiling(1.0f);
119 active_layer_->AddTiling(0.5f);
120 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
121 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
122 pending_layer_->set_invalidation(invalidation);
123 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
124 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
127 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
128 host_impl_.CreatePendingTree();
129 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
130 // Clear recycled tree.
131 pending_tree->DetachLayerTree();
133 scoped_ptr<FakePictureLayerImpl> pending_layer =
134 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
135 pending_layer->SetDrawsContent(true);
136 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
138 pending_layer_ = static_cast<FakePictureLayerImpl*>(
139 host_impl_.pending_tree()->LayerById(id_));
140 pending_layer_->DoPostCommitInitializationIfNeeded();
143 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
144 float ideal_contents_scale,
145 float device_scale_factor,
146 float page_scale_factor,
147 float maximum_animation_contents_scale,
148 bool animating_transform_to_screen) {
149 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
150 layer->draw_properties().device_scale_factor = device_scale_factor;
151 layer->draw_properties().page_scale_factor = page_scale_factor;
152 layer->draw_properties().maximum_animation_contents_scale =
153 maximum_animation_contents_scale;
154 layer->draw_properties().screen_space_transform_is_animating =
155 animating_transform_to_screen;
156 layer->UpdateTiles(NULL);
158 static void VerifyAllTilesExistAndHavePile(
159 const PictureLayerTiling* tiling,
160 PicturePileImpl* pile) {
161 for (PictureLayerTiling::CoverageIterator iter(
162 tiling, tiling->contents_scale(), tiling->TilingRect());
163 iter;
164 ++iter) {
165 EXPECT_TRUE(*iter);
166 EXPECT_EQ(pile, iter->picture_pile());
170 void SetContentsScaleOnBothLayers(float contents_scale,
171 float device_scale_factor,
172 float page_scale_factor,
173 float maximum_animation_contents_scale,
174 bool animating_transform) {
175 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
176 contents_scale,
177 device_scale_factor,
178 page_scale_factor,
179 maximum_animation_contents_scale,
180 animating_transform);
182 SetupDrawPropertiesAndUpdateTiles(active_layer_,
183 contents_scale,
184 device_scale_factor,
185 page_scale_factor,
186 maximum_animation_contents_scale,
187 animating_transform);
190 void ResetTilingsAndRasterScales() {
191 pending_layer_->ReleaseResources();
192 active_layer_->ReleaseResources();
195 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
196 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
197 for (size_t i = 0; i < tiles.size(); ++i)
198 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
199 EXPECT_GT(tiles.size(), 0u);
202 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
203 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
204 for (size_t i = 0; i < tiles.size(); ++i)
205 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
206 EXPECT_GT(tiles.size(), 0u);
209 protected:
210 void TestTileGridAlignmentCommon() {
211 // Layer to span 4 raster tiles in x and in y
212 ImplSidePaintingSettings settings;
213 gfx::Size layer_size(
214 settings.default_tile_size.width() * 7 / 2,
215 settings.default_tile_size.height() * 7 / 2);
217 scoped_refptr<FakePicturePileImpl> pending_pile =
218 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
219 scoped_refptr<FakePicturePileImpl> active_pile =
220 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
222 SetupTrees(pending_pile, active_pile);
224 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
226 // Add 1x1 rects at the centers of each tile, then re-record pile contents
227 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
228 std::vector<Tile*> tiles =
229 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
230 EXPECT_EQ(16u, tiles.size());
231 std::vector<SkRect> rects;
232 std::vector<Tile*>::const_iterator tile_iter;
233 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
234 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
235 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
236 active_pile->add_draw_rect(rect);
237 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
239 // Force re-record with newly injected content
240 active_pile->RemoveRecordingAt(0, 0);
241 active_pile->AddRecordingAt(0, 0);
243 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
244 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
245 MockCanvas mock_canvas(1000, 1000);
246 active_pile->RasterDirect(
247 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
249 // This test verifies that when drawing the contents of a specific tile
250 // at content scale 1.0, the playback canvas never receives content from
251 // neighboring tiles which indicates that the tile grid embedded in
252 // SkPicture is perfectly aligned with the compositor's tiles.
253 EXPECT_EQ(1u, mock_canvas.rects_.size());
254 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
255 rect_iter++;
259 FakeImplProxy proxy_;
260 TestSharedBitmapManager shared_bitmap_manager_;
261 FakeLayerTreeHostImpl host_impl_;
262 int id_;
263 FakePictureLayerImpl* pending_layer_;
264 FakePictureLayerImpl* active_layer_;
266 private:
267 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
270 TEST_F(PictureLayerImplTest, TileGridAlignment) {
271 host_impl_.SetDeviceScaleFactor(1.f);
272 TestTileGridAlignmentCommon();
275 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
276 host_impl_.SetDeviceScaleFactor(2.f);
277 TestTileGridAlignmentCommon();
280 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
281 gfx::Size tile_size(100, 100);
282 gfx::Size layer_bounds(400, 400);
284 scoped_refptr<FakePicturePileImpl> pending_pile =
285 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
286 scoped_refptr<FakePicturePileImpl> active_pile =
287 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
289 SetupTrees(pending_pile, active_pile);
291 Region invalidation;
292 AddDefaultTilingsWithInvalidation(invalidation);
294 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
295 active_layer_->tilings()->num_tilings());
297 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
298 EXPECT_GT(tilings->num_tilings(), 0u);
299 for (size_t i = 0; i < tilings->num_tilings(); ++i)
300 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
303 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
304 base::TimeTicks time_ticks;
305 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
307 gfx::Size tile_size(100, 100);
308 gfx::Size layer_bounds(400, 400);
310 scoped_refptr<FakePicturePileImpl> pending_pile =
311 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
312 scoped_refptr<FakePicturePileImpl> active_pile =
313 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
315 SetupTrees(pending_pile, active_pile);
317 Region invalidation;
318 AddDefaultTilingsWithInvalidation(invalidation);
319 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
321 // UpdateTiles with valid viewport. Should update tile viewport.
322 bool valid_for_tile_management = true;
323 gfx::Rect viewport = gfx::Rect(layer_bounds);
324 gfx::Transform transform;
325 host_impl_.SetExternalDrawConstraints(
326 transform, viewport, viewport, valid_for_tile_management);
327 active_layer_->draw_properties().visible_content_rect = viewport;
328 active_layer_->draw_properties().screen_space_transform = transform;
329 active_layer_->UpdateTiles(NULL);
331 gfx::Rect visible_rect_for_tile_priority =
332 active_layer_->visible_rect_for_tile_priority();
333 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
334 gfx::Size viewport_size_for_tile_priority =
335 active_layer_->viewport_size_for_tile_priority();
336 EXPECT_FALSE(viewport_size_for_tile_priority.IsEmpty());
337 gfx::Transform screen_space_transform_for_tile_priority =
338 active_layer_->screen_space_transform_for_tile_priority();
340 // Expand viewport and set it as invalid for prioritizing tiles.
341 // Should not update tile viewport.
342 time_ticks += base::TimeDelta::FromMilliseconds(200);
343 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
344 valid_for_tile_management = false;
345 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
346 transform.Translate(1.f, 1.f);
347 active_layer_->draw_properties().visible_content_rect = viewport;
348 active_layer_->draw_properties().screen_space_transform = transform;
349 host_impl_.SetExternalDrawConstraints(
350 transform, viewport, viewport, valid_for_tile_management);
351 active_layer_->UpdateTiles(NULL);
353 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
354 active_layer_->visible_rect_for_tile_priority());
355 EXPECT_SIZE_EQ(viewport_size_for_tile_priority,
356 active_layer_->viewport_size_for_tile_priority());
357 EXPECT_TRANSFORMATION_MATRIX_EQ(
358 screen_space_transform_for_tile_priority,
359 active_layer_->screen_space_transform_for_tile_priority());
361 // Keep expanded viewport but mark it valid. Should update tile viewport.
362 time_ticks += base::TimeDelta::FromMilliseconds(200);
363 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
364 valid_for_tile_management = true;
365 host_impl_.SetExternalDrawConstraints(
366 transform, viewport, viewport, valid_for_tile_management);
367 active_layer_->UpdateTiles(NULL);
369 EXPECT_FALSE(visible_rect_for_tile_priority ==
370 active_layer_->visible_rect_for_tile_priority());
371 EXPECT_FALSE(viewport_size_for_tile_priority ==
372 active_layer_->viewport_size_for_tile_priority());
373 EXPECT_FALSE(screen_space_transform_for_tile_priority ==
374 active_layer_->screen_space_transform_for_tile_priority());
377 TEST_F(PictureLayerImplTest, InvalidViewportAfterReleaseResources) {
378 base::TimeTicks time_ticks;
379 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
381 gfx::Size tile_size(100, 100);
382 gfx::Size layer_bounds(400, 400);
384 scoped_refptr<FakePicturePileImpl> pending_pile =
385 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
386 scoped_refptr<FakePicturePileImpl> active_pile =
387 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
389 SetupTrees(pending_pile, active_pile);
391 Region invalidation;
392 AddDefaultTilingsWithInvalidation(invalidation);
394 bool valid_for_tile_management = false;
395 gfx::Rect viewport = gfx::Rect(layer_bounds);
396 host_impl_.SetExternalDrawConstraints(
397 gfx::Transform(), viewport, viewport, valid_for_tile_management);
398 ResetTilingsAndRasterScales();
399 host_impl_.pending_tree()->UpdateDrawProperties();
400 host_impl_.active_tree()->UpdateDrawProperties();
401 EXPECT_TRUE(active_layer_->HighResTiling());
403 size_t num_tilings = active_layer_->num_tilings();
404 active_layer_->UpdateTiles(NULL);
405 pending_layer_->AddTiling(0.5f);
406 EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
409 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
410 gfx::Size tile_size(100, 100);
411 gfx::Size layer_bounds(400, 400);
412 gfx::Rect layer_invalidation(150, 200, 30, 180);
414 scoped_refptr<FakePicturePileImpl> pending_pile =
415 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
416 scoped_refptr<FakePicturePileImpl> active_pile =
417 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
419 SetupTrees(pending_pile, active_pile);
421 Region invalidation(layer_invalidation);
422 AddDefaultTilingsWithInvalidation(invalidation);
424 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
425 EXPECT_GT(tilings->num_tilings(), 0u);
426 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
427 const PictureLayerTiling* tiling = tilings->tiling_at(i);
428 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
429 layer_invalidation,
430 tiling->contents_scale());
431 for (PictureLayerTiling::CoverageIterator iter(
432 tiling, tiling->contents_scale(), tiling->TilingRect());
433 iter;
434 ++iter) {
435 EXPECT_TRUE(*iter);
436 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
437 if (iter.geometry_rect().Intersects(content_invalidation))
438 EXPECT_EQ(pending_pile, iter->picture_pile());
439 else
440 EXPECT_EQ(active_pile, iter->picture_pile());
445 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
446 gfx::Size tile_size(90, 80);
447 gfx::Size layer_bounds(300, 500);
449 scoped_refptr<FakePicturePileImpl> pending_pile =
450 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
451 scoped_refptr<FakePicturePileImpl> active_pile =
452 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
454 SetupTrees(pending_pile, active_pile);
456 Region invalidation((gfx::Rect(layer_bounds)));
457 AddDefaultTilingsWithInvalidation(invalidation);
459 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
460 active_layer_->tilings()->num_tilings());
462 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
463 EXPECT_GT(tilings->num_tilings(), 0u);
464 for (size_t i = 0; i < tilings->num_tilings(); ++i)
465 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
468 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
469 gfx::Size tile_size(90, 80);
470 gfx::Size active_layer_bounds(300, 500);
471 gfx::Size pending_layer_bounds(400, 800);
473 scoped_refptr<FakePicturePileImpl> pending_pile =
474 FakePicturePileImpl::CreateFilledPile(tile_size,
475 pending_layer_bounds);
476 scoped_refptr<FakePicturePileImpl> active_pile =
477 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
479 SetupTrees(pending_pile, active_pile);
480 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
482 Region invalidation;
483 AddDefaultTilingsWithInvalidation(invalidation);
485 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
486 EXPECT_GT(tilings->num_tilings(), 0u);
487 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
488 const PictureLayerTiling* tiling = tilings->tiling_at(i);
489 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
490 gfx::Rect(active_layer_bounds),
491 tiling->contents_scale());
492 for (PictureLayerTiling::CoverageIterator iter(
493 tiling, tiling->contents_scale(), tiling->TilingRect());
494 iter;
495 ++iter) {
496 EXPECT_TRUE(*iter);
497 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
498 std::vector<Tile*> active_tiles =
499 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
500 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
501 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
502 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
503 active_tiles[0]->content_rect().size() !=
504 pending_tiles[0]->content_rect().size()) {
505 EXPECT_EQ(pending_pile, iter->picture_pile());
506 } else {
507 EXPECT_EQ(active_pile, iter->picture_pile());
513 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
514 gfx::Size tile_size(400, 400);
515 gfx::Size layer_bounds(1300, 1900);
517 scoped_refptr<FakePicturePileImpl> pending_pile =
518 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
519 scoped_refptr<FakePicturePileImpl> active_pile =
520 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
522 // Fill in some of active pile, but more of pending pile.
523 int hole_count = 0;
524 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
525 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
526 if ((x + y) % 2) {
527 pending_pile->AddRecordingAt(x, y);
528 active_pile->AddRecordingAt(x, y);
529 } else {
530 hole_count++;
531 if (hole_count % 2)
532 pending_pile->AddRecordingAt(x, y);
537 SetupTrees(pending_pile, active_pile);
538 Region invalidation;
539 AddDefaultTilingsWithInvalidation(invalidation);
541 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
542 EXPECT_GT(tilings->num_tilings(), 0u);
543 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
544 const PictureLayerTiling* tiling = tilings->tiling_at(i);
546 for (PictureLayerTiling::CoverageIterator iter(
547 tiling, tiling->contents_scale(), tiling->TilingRect());
548 iter;
549 ++iter) {
550 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
551 // Ensure there is a recording for this tile.
552 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
553 iter.full_tile_geometry_rect());
554 bool in_active = active_pile->CanRaster(tiling->contents_scale(),
555 iter.full_tile_geometry_rect());
557 if (in_pending && !in_active)
558 EXPECT_EQ(pending_pile, iter->picture_pile());
559 else if (in_active)
560 EXPECT_EQ(active_pile, iter->picture_pile());
561 else
562 EXPECT_FALSE(*iter);
567 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
568 gfx::Size tile_size(400, 400);
569 gfx::Size layer_bounds(1300, 1900);
571 scoped_refptr<FakePicturePileImpl> pending_pile =
572 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
573 scoped_refptr<FakePicturePileImpl> active_pile =
574 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
576 SetupTrees(pending_pile, active_pile);
578 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
580 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
583 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
584 gfx::Size tile_size(400, 400);
585 gfx::Size layer_bounds(1300, 1900);
587 scoped_refptr<FakePicturePileImpl> pending_pile =
588 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
589 scoped_refptr<FakePicturePileImpl> active_pile =
590 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
592 SetupTrees(pending_pile, active_pile);
593 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
595 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
596 EXPECT_LT(low_res_factor, 1.f);
598 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
599 6.f, // ideal contents scale
600 3.f, // device scale
601 2.f, // page scale
602 1.f, // maximum animation scale
603 false);
604 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
605 EXPECT_FLOAT_EQ(6.f,
606 pending_layer_->tilings()->tiling_at(0)->contents_scale());
607 EXPECT_FLOAT_EQ(6.f * low_res_factor,
608 pending_layer_->tilings()->tiling_at(1)->contents_scale());
610 // If we change the page scale factor, then we should get new tilings.
611 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
612 6.6f, // ideal contents scale
613 3.f, // device scale
614 2.2f, // page scale
615 1.f, // maximum animation scale
616 false);
617 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
618 EXPECT_FLOAT_EQ(6.6f,
619 pending_layer_->tilings()->tiling_at(0)->contents_scale());
620 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
621 pending_layer_->tilings()->tiling_at(2)->contents_scale());
623 // If we change the device scale factor, then we should get new tilings.
624 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
625 7.26f, // ideal contents scale
626 3.3f, // device scale
627 2.2f, // page scale
628 1.f, // maximum animation scale
629 false);
630 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
631 EXPECT_FLOAT_EQ(7.26f,
632 pending_layer_->tilings()->tiling_at(0)->contents_scale());
633 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
634 pending_layer_->tilings()->tiling_at(3)->contents_scale());
636 // If we change the device scale factor, but end up at the same total scale
637 // factor somehow, then we don't get new tilings.
638 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
639 7.26f, // ideal contents scale
640 2.2f, // device scale
641 3.3f, // page scale
642 1.f, // maximum animation scale
643 false);
644 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
645 EXPECT_FLOAT_EQ(7.26f,
646 pending_layer_->tilings()->tiling_at(0)->contents_scale());
647 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
648 pending_layer_->tilings()->tiling_at(3)->contents_scale());
651 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
652 // This test makes sure that if a layer can have tilings, then a commit makes
653 // it not able to have tilings (empty size), and then a future commit that
654 // makes it valid again should be able to create tilings.
655 gfx::Size tile_size(400, 400);
656 gfx::Size layer_bounds(1300, 1900);
658 scoped_refptr<FakePicturePileImpl> empty_pile =
659 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
660 scoped_refptr<FakePicturePileImpl> valid_pile =
661 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
663 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
664 EXPECT_LT(low_res_factor, 1.f);
666 float high_res_scale = 1.3f;
667 float low_res_scale = high_res_scale * low_res_factor;
668 float device_scale = 1.7f;
669 float page_scale = 3.2f;
670 float maximum_animation_scale = 1.f;
672 SetupPendingTree(valid_pile);
673 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
674 high_res_scale,
675 device_scale,
676 page_scale,
677 maximum_animation_scale,
678 false);
679 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
680 EXPECT_FLOAT_EQ(high_res_scale,
681 pending_layer_->HighResTiling()->contents_scale());
682 EXPECT_FLOAT_EQ(low_res_scale,
683 pending_layer_->LowResTiling()->contents_scale());
685 ActivateTree();
686 SetupPendingTree(empty_pile);
687 EXPECT_FALSE(pending_layer_->CanHaveTilings());
688 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
689 high_res_scale,
690 device_scale,
691 page_scale,
692 maximum_animation_scale,
693 false);
694 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
695 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
697 ActivateTree();
698 EXPECT_FALSE(active_layer_->CanHaveTilings());
699 SetupDrawPropertiesAndUpdateTiles(active_layer_,
700 high_res_scale,
701 device_scale,
702 page_scale,
703 maximum_animation_scale,
704 false);
705 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
707 SetupPendingTree(valid_pile);
708 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
709 high_res_scale,
710 device_scale,
711 page_scale,
712 maximum_animation_scale,
713 false);
714 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
715 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
716 EXPECT_FLOAT_EQ(high_res_scale,
717 pending_layer_->HighResTiling()->contents_scale());
718 EXPECT_FLOAT_EQ(low_res_scale,
719 pending_layer_->LowResTiling()->contents_scale());
722 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
723 gfx::Size tile_size(400, 400);
724 gfx::Size layer_bounds(1300, 1900);
726 // Set up the high and low res tilings before pinch zoom.
727 scoped_refptr<FakePicturePileImpl> pending_pile =
728 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
729 scoped_refptr<FakePicturePileImpl> active_pile =
730 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
732 SetupTrees(pending_pile, active_pile);
733 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
734 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
735 host_impl_.PinchGestureBegin();
736 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
737 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
738 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
741 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
742 gfx::Size tile_size(400, 400);
743 gfx::Size layer_bounds(1300, 1900);
745 scoped_refptr<FakePicturePileImpl> pending_pile =
746 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
747 scoped_refptr<FakePicturePileImpl> active_pile =
748 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
750 // Set up the high and low res tilings before pinch zoom.
751 SetupTrees(pending_pile, active_pile);
752 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
753 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
754 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
755 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
756 EXPECT_FLOAT_EQ(2.0f,
757 active_layer_->tilings()->tiling_at(0)->contents_scale());
758 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
759 active_layer_->tilings()->tiling_at(1)->contents_scale());
761 // Start a pinch gesture.
762 host_impl_.PinchGestureBegin();
764 // Zoom out by a small amount. We should create a tiling at half
765 // the scale (2/kMaxScaleRatioDuringPinch).
766 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
767 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
768 EXPECT_FLOAT_EQ(2.0f,
769 active_layer_->tilings()->tiling_at(0)->contents_scale());
770 EXPECT_FLOAT_EQ(1.0f,
771 active_layer_->tilings()->tiling_at(1)->contents_scale());
772 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
773 active_layer_->tilings()->tiling_at(2)->contents_scale());
775 // Zoom out further, close to our low-res scale factor. We should
776 // use that tiling as high-res, and not create a new tiling.
777 SetContentsScaleOnBothLayers(
778 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
779 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
781 // Zoom in a lot now. Since we increase by increments of
782 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
783 // and then finally create a new tiling at 4.0.
784 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
785 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
786 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
787 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
788 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
789 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
790 EXPECT_FLOAT_EQ(4.0f,
791 active_layer_->tilings()->tiling_at(0)->contents_scale());
794 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
795 gfx::Size tile_size(300, 300);
796 gfx::Size layer_bounds(2600, 3800);
798 scoped_refptr<FakePicturePileImpl> pending_pile =
799 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
800 scoped_refptr<FakePicturePileImpl> active_pile =
801 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
803 // Set up the high and low res tilings before pinch zoom.
804 SetupTrees(pending_pile, active_pile);
805 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
806 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
807 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
808 EXPECT_FLOAT_EQ(0.24f,
809 active_layer_->tilings()->tiling_at(0)->contents_scale());
810 EXPECT_FLOAT_EQ(0.0625f,
811 active_layer_->tilings()->tiling_at(1)->contents_scale());
813 // Start a pinch gesture.
814 host_impl_.PinchGestureBegin();
816 // Zoom out by a small amount. We should create a tiling at half
817 // the scale (1/kMaxScaleRatioDuringPinch).
818 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
819 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
820 EXPECT_FLOAT_EQ(0.24f,
821 active_layer_->tilings()->tiling_at(0)->contents_scale());
822 EXPECT_FLOAT_EQ(0.12f,
823 active_layer_->tilings()->tiling_at(1)->contents_scale());
824 EXPECT_FLOAT_EQ(0.0625,
825 active_layer_->tilings()->tiling_at(2)->contents_scale());
827 // Zoom out further, close to our low-res scale factor. We should
828 // use that tiling as high-res, and not create a new tiling.
829 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
830 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
832 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
833 // because 0.125(desired_scale) is within the ratio(1.2)
834 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
835 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
838 TEST_F(PictureLayerImplTest, CleanUpTilings) {
839 gfx::Size tile_size(400, 400);
840 gfx::Size layer_bounds(1300, 1900);
842 scoped_refptr<FakePicturePileImpl> pending_pile =
843 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
844 scoped_refptr<FakePicturePileImpl> active_pile =
845 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
847 std::vector<PictureLayerTiling*> used_tilings;
849 SetupTrees(pending_pile, active_pile);
850 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
852 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
853 EXPECT_LT(low_res_factor, 1.f);
855 float device_scale = 1.7f;
856 float page_scale = 3.2f;
857 float scale = 1.f;
859 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
860 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
862 // We only have ideal tilings, so they aren't removed.
863 used_tilings.clear();
864 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
865 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
867 host_impl_.PinchGestureBegin();
869 // Changing the ideal but not creating new tilings.
870 scale *= 1.5f;
871 page_scale *= 1.5f;
872 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
873 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
875 // The tilings are still our target scale, so they aren't removed.
876 used_tilings.clear();
877 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
878 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
880 host_impl_.PinchGestureEnd();
882 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
883 scale /= 4.f;
884 page_scale /= 4.f;
885 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
886 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
887 EXPECT_FLOAT_EQ(
888 1.f,
889 active_layer_->tilings()->tiling_at(1)->contents_scale());
890 EXPECT_FLOAT_EQ(
891 1.f * low_res_factor,
892 active_layer_->tilings()->tiling_at(3)->contents_scale());
894 // Mark the non-ideal tilings as used. They won't be removed.
895 used_tilings.clear();
896 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
897 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
898 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
899 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
901 // Now move the ideal scale to 0.5. Our target stays 1.2.
902 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
904 // The high resolution tiling is between target and ideal, so is not
905 // removed. The low res tiling for the old ideal=1.0 scale is removed.
906 used_tilings.clear();
907 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
908 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
910 // Now move the ideal scale to 1.0. Our target stays 1.2.
911 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
913 // All the tilings are between are target and the ideal, so they are not
914 // removed.
915 used_tilings.clear();
916 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
917 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
919 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
920 SetupDrawPropertiesAndUpdateTiles(
921 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
923 // Because the pending layer's ideal scale is still 1.0, our tilings fall
924 // in the range [1.0,1.2] and are kept.
925 used_tilings.clear();
926 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
927 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
929 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
930 // 1.2 still.
931 SetupDrawPropertiesAndUpdateTiles(
932 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
934 // Our 1.0 tiling now falls outside the range between our ideal scale and our
935 // target raster scale. But it is in our used tilings set, so nothing is
936 // deleted.
937 used_tilings.clear();
938 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
939 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
940 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
942 // If we remove it from our used tilings set, it is outside the range to keep
943 // so it is deleted.
944 used_tilings.clear();
945 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
946 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
949 #define EXPECT_BOTH_EQ(expression, x) \
950 do { \
951 EXPECT_EQ(x, pending_layer_->expression); \
952 EXPECT_EQ(x, active_layer_->expression); \
953 } while (false)
955 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
956 // Make sure this layer covers multiple tiles, since otherwise low
957 // res won't get created because it is too small.
958 gfx::Size tile_size(host_impl_.settings().default_tile_size);
959 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
960 // Avoid max untiled layer size heuristics via fixed tile size.
961 pending_layer_->set_fixed_tile_size(tile_size);
962 active_layer_->set_fixed_tile_size(tile_size);
964 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
965 float contents_scale = 1.f;
966 float device_scale = 1.f;
967 float page_scale = 1.f;
968 float maximum_animation_scale = 1.f;
969 bool animating_transform = true;
971 // Animating, so don't create low res even if there isn't one already.
972 SetContentsScaleOnBothLayers(contents_scale,
973 device_scale,
974 page_scale,
975 maximum_animation_scale,
976 animating_transform);
977 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
978 EXPECT_BOTH_EQ(num_tilings(), 1u);
980 // Stop animating, low res gets created.
981 animating_transform = false;
982 SetContentsScaleOnBothLayers(contents_scale,
983 device_scale,
984 page_scale,
985 maximum_animation_scale,
986 animating_transform);
987 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
988 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
989 EXPECT_BOTH_EQ(num_tilings(), 2u);
991 // Page scale animation, new high res, but not new low res because animating.
992 contents_scale = 2.f;
993 page_scale = 2.f;
994 animating_transform = true;
995 SetContentsScaleOnBothLayers(contents_scale,
996 device_scale,
997 page_scale,
998 maximum_animation_scale,
999 animating_transform);
1000 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1001 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1002 EXPECT_BOTH_EQ(num_tilings(), 3u);
1004 // Stop animating, new low res gets created for final page scale.
1005 animating_transform = false;
1006 SetContentsScaleOnBothLayers(contents_scale,
1007 device_scale,
1008 page_scale,
1009 maximum_animation_scale,
1010 animating_transform);
1011 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1012 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1013 EXPECT_BOTH_EQ(num_tilings(), 4u);
1016 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1017 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1018 SetupDefaultTrees(tile_size);
1020 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1021 float device_scale = 1.f;
1022 float page_scale = 1.f;
1023 float maximum_animation_scale = 1.f;
1024 bool animating_transform = false;
1026 // Contents exactly fit on one tile at scale 1, no low res.
1027 float contents_scale = 1.f;
1028 SetContentsScaleOnBothLayers(contents_scale,
1029 device_scale,
1030 page_scale,
1031 maximum_animation_scale,
1032 animating_transform);
1033 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1034 EXPECT_BOTH_EQ(num_tilings(), 1u);
1036 ResetTilingsAndRasterScales();
1038 // Contents that are smaller than one tile, no low res.
1039 contents_scale = 0.123f;
1040 SetContentsScaleOnBothLayers(contents_scale,
1041 device_scale,
1042 page_scale,
1043 maximum_animation_scale,
1044 animating_transform);
1045 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1046 EXPECT_BOTH_EQ(num_tilings(), 1u);
1048 ResetTilingsAndRasterScales();
1050 // Any content bounds that would create more than one tile will
1051 // generate a low res tiling.
1052 contents_scale = 2.5f;
1053 SetContentsScaleOnBothLayers(contents_scale,
1054 device_scale,
1055 page_scale,
1056 maximum_animation_scale,
1057 animating_transform);
1058 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1059 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1060 contents_scale * low_res_factor);
1061 EXPECT_BOTH_EQ(num_tilings(), 2u);
1063 ResetTilingsAndRasterScales();
1065 // Mask layers dont create low res since they always fit on one tile.
1066 pending_layer_->SetIsMask(true);
1067 active_layer_->SetIsMask(true);
1068 SetContentsScaleOnBothLayers(contents_scale,
1069 device_scale,
1070 page_scale,
1071 maximum_animation_scale,
1072 animating_transform);
1073 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1074 EXPECT_BOTH_EQ(num_tilings(), 1u);
1077 TEST_F(PictureLayerImplTest, ReleaseResources) {
1078 gfx::Size tile_size(400, 400);
1079 gfx::Size layer_bounds(1300, 1900);
1081 scoped_refptr<FakePicturePileImpl> pending_pile =
1082 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1083 scoped_refptr<FakePicturePileImpl> active_pile =
1084 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1086 SetupTrees(pending_pile, active_pile);
1087 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1089 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1090 1.3f, // ideal contents scale
1091 2.7f, // device scale
1092 3.2f, // page scale
1093 1.f, // maximum animation scale
1094 false);
1095 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1097 // All tilings should be removed when losing output surface.
1098 active_layer_->ReleaseResources();
1099 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1100 pending_layer_->ReleaseResources();
1101 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1103 // This should create new tilings.
1104 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1105 1.3f, // ideal contents scale
1106 2.7f, // device scale
1107 3.2f, // page scale
1108 1.f, // maximum animation scale
1109 false);
1110 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1113 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1114 // The default max tile size is larger than 400x400.
1115 gfx::Size tile_size(400, 400);
1116 gfx::Size layer_bounds(5000, 5000);
1118 scoped_refptr<FakePicturePileImpl> pending_pile =
1119 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1120 scoped_refptr<FakePicturePileImpl> active_pile =
1121 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1123 SetupTrees(pending_pile, active_pile);
1124 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1126 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1127 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1129 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1131 // The default value.
1132 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1133 host_impl_.settings().default_tile_size.ToString());
1135 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1136 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1137 tile->content_rect().size().ToString());
1139 pending_layer_->ReleaseResources();
1141 // Change the max texture size on the output surface context.
1142 scoped_ptr<TestWebGraphicsContext3D> context =
1143 TestWebGraphicsContext3D::Create();
1144 context->set_max_texture_size(140);
1145 host_impl_.DidLoseOutputSurface();
1146 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1147 context.Pass()).PassAs<OutputSurface>());
1149 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1150 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1152 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1154 // Verify the tiles are not larger than the context's max texture size.
1155 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1156 EXPECT_GE(140, tile->content_rect().width());
1157 EXPECT_GE(140, tile->content_rect().height());
1160 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1161 // The default max tile size is larger than 400x400.
1162 gfx::Size tile_size(400, 400);
1163 gfx::Size layer_bounds(500, 500);
1165 scoped_refptr<FakePicturePileImpl> pending_pile =
1166 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1167 scoped_refptr<FakePicturePileImpl> active_pile =
1168 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1170 SetupTrees(pending_pile, active_pile);
1171 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1173 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1174 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1176 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1178 // The default value. The layer is smaller than this.
1179 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1180 host_impl_.settings().max_untiled_layer_size.ToString());
1182 // There should be a single tile since the layer is small.
1183 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1184 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1186 pending_layer_->ReleaseResources();
1188 // Change the max texture size on the output surface context.
1189 scoped_ptr<TestWebGraphicsContext3D> context =
1190 TestWebGraphicsContext3D::Create();
1191 context->set_max_texture_size(140);
1192 host_impl_.DidLoseOutputSurface();
1193 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1194 context.Pass()).PassAs<OutputSurface>());
1196 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1197 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1199 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1201 // There should be more than one tile since the max texture size won't cover
1202 // the layer.
1203 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1204 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1206 // Verify the tiles are not larger than the context's max texture size.
1207 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1208 EXPECT_GE(140, tile->content_rect().width());
1209 EXPECT_GE(140, tile->content_rect().height());
1212 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1213 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1214 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1216 gfx::Size tile_size(400, 400);
1217 gfx::Size layer_bounds(1300, 1900);
1219 scoped_refptr<FakePicturePileImpl> pending_pile =
1220 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1221 scoped_refptr<FakePicturePileImpl> active_pile =
1222 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1224 SetupTrees(pending_pile, active_pile);
1226 active_layer_->draw_properties().visible_content_rect =
1227 gfx::Rect(layer_bounds);
1229 gfx::Rect layer_invalidation(150, 200, 30, 180);
1230 Region invalidation(layer_invalidation);
1231 AddDefaultTilingsWithInvalidation(invalidation);
1233 AppendQuadsData data;
1234 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1235 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1236 active_layer_->DidDraw(NULL);
1238 ASSERT_EQ(1U, render_pass->quad_list.size());
1239 EXPECT_EQ(DrawQuad::PICTURE_CONTENT, render_pass->quad_list[0]->material);
1242 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1243 gfx::Size tile_size(100, 100);
1244 gfx::Size layer_bounds(1000, 1000);
1246 scoped_refptr<FakePicturePileImpl> pending_pile =
1247 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1248 // Layers with entirely empty piles can't get tilings.
1249 pending_pile->AddRecordingAt(0, 0);
1251 SetupPendingTree(pending_pile);
1253 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1254 pending_layer_->AddTiling(1.0f);
1255 pending_layer_->AddTiling(2.0f);
1257 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1258 // on a layer with no recordings.
1259 host_impl_.pending_tree()->UpdateDrawProperties();
1260 pending_layer_->MarkVisibleResourcesAsRequired();
1263 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1264 gfx::Size tile_size(100, 100);
1265 gfx::Size layer_bounds(200, 200);
1267 scoped_refptr<FakePicturePileImpl> pending_pile =
1268 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1269 SetupPendingTree(pending_pile);
1271 pending_layer_->set_fixed_tile_size(tile_size);
1272 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1273 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1274 host_impl_.pending_tree()->UpdateDrawProperties();
1275 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1277 pending_layer_->draw_properties().visible_content_rect =
1278 gfx::Rect(0, 0, 100, 200);
1280 // Fake set priorities.
1281 for (PictureLayerTiling::CoverageIterator iter(
1282 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1283 iter;
1284 ++iter) {
1285 if (!*iter)
1286 continue;
1287 Tile* tile = *iter;
1288 TilePriority priority;
1289 priority.resolution = HIGH_RESOLUTION;
1290 gfx::Rect tile_bounds = iter.geometry_rect();
1291 if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1292 priority.priority_bin = TilePriority::NOW;
1293 priority.distance_to_visible = 0.f;
1294 } else {
1295 priority.priority_bin = TilePriority::SOON;
1296 priority.distance_to_visible = 1.f;
1298 tile->SetPriority(PENDING_TREE, priority);
1301 pending_layer_->MarkVisibleResourcesAsRequired();
1303 int num_visible = 0;
1304 int num_offscreen = 0;
1306 for (PictureLayerTiling::CoverageIterator iter(
1307 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1308 iter;
1309 ++iter) {
1310 if (!*iter)
1311 continue;
1312 const Tile* tile = *iter;
1313 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1314 EXPECT_TRUE(tile->required_for_activation());
1315 num_visible++;
1316 } else {
1317 EXPECT_FALSE(tile->required_for_activation());
1318 num_offscreen++;
1322 EXPECT_GT(num_visible, 0);
1323 EXPECT_GT(num_offscreen, 0);
1326 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1327 gfx::Size layer_bounds(400, 400);
1328 gfx::Size tile_size(100, 100);
1329 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1331 // No tiles shared.
1332 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1334 CreateHighLowResAndSetAllTilesVisible();
1336 active_layer_->SetAllTilesReady();
1338 // No shared tiles and all active tiles ready, so pending can only
1339 // activate with all high res tiles.
1340 pending_layer_->MarkVisibleResourcesAsRequired();
1341 AssertAllTilesRequired(pending_layer_->HighResTiling());
1342 AssertNoTilesRequired(pending_layer_->LowResTiling());
1345 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1346 gfx::Size layer_bounds(400, 400);
1347 gfx::Size tile_size(100, 100);
1348 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1350 // All tiles shared (no invalidation).
1351 CreateHighLowResAndSetAllTilesVisible();
1353 // Verify active tree not ready.
1354 Tile* some_active_tile =
1355 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1356 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1358 // When high res are required, even if the active tree is not ready,
1359 // the high res tiles must be ready.
1360 host_impl_.active_tree()->SetRequiresHighResToDraw();
1361 pending_layer_->MarkVisibleResourcesAsRequired();
1362 AssertAllTilesRequired(pending_layer_->HighResTiling());
1363 AssertNoTilesRequired(pending_layer_->LowResTiling());
1366 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1367 gfx::Size layer_bounds(400, 400);
1368 gfx::Size tile_size(100, 100);
1369 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1371 CreateHighLowResAndSetAllTilesVisible();
1373 Tile* some_active_tile =
1374 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1375 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1377 // All tiles shared (no invalidation), so even though the active tree's
1378 // tiles aren't ready, there is nothing required.
1379 pending_layer_->MarkVisibleResourcesAsRequired();
1380 AssertNoTilesRequired(pending_layer_->HighResTiling());
1381 AssertNoTilesRequired(pending_layer_->LowResTiling());
1384 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1385 gfx::Size layer_bounds(400, 400);
1386 gfx::Size tile_size(100, 100);
1387 scoped_refptr<FakePicturePileImpl> pending_pile =
1388 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1389 // This pile will create tilings, but has no recordings so will not create any
1390 // tiles. This is attempting to simulate scrolling past the end of recorded
1391 // content on the active layer, where the recordings are so far away that
1392 // no tiles are created.
1393 scoped_refptr<FakePicturePileImpl> active_pile =
1394 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1395 tile_size, layer_bounds);
1396 SetupTrees(pending_pile, active_pile);
1397 pending_layer_->set_fixed_tile_size(tile_size);
1398 active_layer_->set_fixed_tile_size(tile_size);
1400 CreateHighLowResAndSetAllTilesVisible();
1402 // Active layer has tilings, but no tiles due to missing recordings.
1403 EXPECT_TRUE(active_layer_->CanHaveTilings());
1404 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1405 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1407 // Since the active layer has no tiles at all, the pending layer doesn't
1408 // need content in order to activate.
1409 pending_layer_->MarkVisibleResourcesAsRequired();
1410 AssertNoTilesRequired(pending_layer_->HighResTiling());
1411 AssertNoTilesRequired(pending_layer_->LowResTiling());
1414 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1415 gfx::Size layer_bounds(400, 400);
1416 gfx::Size tile_size(100, 100);
1417 scoped_refptr<FakePicturePileImpl> pending_pile =
1418 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1419 scoped_refptr<FakePicturePileImpl> active_pile =
1420 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1421 SetupTrees(pending_pile, active_pile);
1422 pending_layer_->set_fixed_tile_size(tile_size);
1423 active_layer_->set_fixed_tile_size(tile_size);
1425 CreateHighLowResAndSetAllTilesVisible();
1427 // Active layer can't have tiles.
1428 EXPECT_FALSE(active_layer_->CanHaveTilings());
1430 // All high res tiles required. This should be considered identical
1431 // to the case where there is no active layer, to avoid flashing content.
1432 // This can happen if a layer exists for a while and switches from
1433 // not being able to have content to having content.
1434 pending_layer_->MarkVisibleResourcesAsRequired();
1435 AssertAllTilesRequired(pending_layer_->HighResTiling());
1436 AssertNoTilesRequired(pending_layer_->LowResTiling());
1439 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1440 gfx::Size layer_bounds(200, 200);
1441 gfx::Size tile_size(100, 100);
1442 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1444 gfx::Size pending_layer_bounds(400, 400);
1445 pending_layer_->SetBounds(pending_layer_bounds);
1447 CreateHighLowResAndSetAllTilesVisible();
1449 active_layer_->SetAllTilesReady();
1451 // Since the active layer has different bounds, the pending layer needs all
1452 // high res tiles in order to activate.
1453 pending_layer_->MarkVisibleResourcesAsRequired();
1454 AssertAllTilesRequired(pending_layer_->HighResTiling());
1455 AssertNoTilesRequired(pending_layer_->LowResTiling());
1458 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1459 gfx::Size tile_size(100, 100);
1460 gfx::Size layer_bounds(400, 400);
1461 scoped_refptr<FakePicturePileImpl> pending_pile =
1462 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1464 host_impl_.CreatePendingTree();
1465 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1467 scoped_ptr<FakePictureLayerImpl> pending_layer =
1468 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1469 pending_layer->SetDrawsContent(true);
1470 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1472 pending_layer_ = static_cast<FakePictureLayerImpl*>(
1473 host_impl_.pending_tree()->LayerById(id_));
1475 // Set some state on the pending layer, make sure it is not clobbered
1476 // by a sync from the active layer. This could happen because if the
1477 // pending layer has not been post-commit initialized it will attempt
1478 // to sync from the active layer.
1479 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1480 pending_layer_->set_raster_page_scale(raster_page_scale);
1481 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1483 host_impl_.ActivateSyncTree();
1485 active_layer_ = static_cast<FakePictureLayerImpl*>(
1486 host_impl_.active_tree()->LayerById(id_));
1488 EXPECT_EQ(0u, active_layer_->num_tilings());
1489 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1490 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1493 TEST_F(PictureLayerImplTest, RemoveInvalidTilesOnActivation) {
1494 SetupDefaultTrees(gfx::Size(1500, 1500));
1495 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1497 FakePictureLayerImpl* recycled_layer = pending_layer_;
1498 host_impl_.ActivateSyncTree();
1500 active_layer_ = static_cast<FakePictureLayerImpl*>(
1501 host_impl_.active_tree()->LayerById(id_));
1503 EXPECT_EQ(3u, active_layer_->num_tilings());
1504 EXPECT_EQ(3u, recycled_layer->num_tilings());
1505 EXPECT_FALSE(host_impl_.pending_tree());
1506 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1507 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1508 PictureLayerTiling* recycled_tiling =
1509 recycled_layer->tilings()->tiling_at(i);
1511 ASSERT_TRUE(active_tiling);
1512 ASSERT_TRUE(recycled_tiling);
1514 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1515 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1516 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1517 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1519 EXPECT_FALSE(recycled_tiling->TileAt(0, 0));
1520 EXPECT_TRUE(recycled_tiling->TileAt(1, 0));
1521 EXPECT_TRUE(recycled_tiling->TileAt(0, 1));
1522 EXPECT_TRUE(recycled_tiling->TileAt(1, 1));
1524 EXPECT_EQ(active_tiling->TileAt(1, 0), recycled_tiling->TileAt(1, 0));
1525 EXPECT_EQ(active_tiling->TileAt(0, 1), recycled_tiling->TileAt(0, 1));
1526 EXPECT_EQ(active_tiling->TileAt(1, 1), recycled_tiling->TileAt(1, 1));
1530 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1531 SetupDefaultTrees(gfx::Size(10, 10));
1532 host_impl_.active_tree()->UpdateDrawProperties();
1533 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1535 // Contrived unit test of a real crash. A layer is transparent during a
1536 // context loss, and later becomes opaque, causing active layer SyncTiling to
1537 // be called.
1538 float new_scale = 1.f;
1539 active_layer_->ReleaseResources();
1540 pending_layer_->ReleaseResources();
1541 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
1542 pending_layer_->AddTiling(new_scale);
1543 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
1545 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
1546 // responsible for calling ManageTilings. These checks verify that
1547 // ReleaseResources has set needs update draw properties so that the
1548 // new tiling gets the appropriate resolution set in ManageTilings.
1549 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1550 host_impl_.active_tree()->UpdateDrawProperties();
1551 PictureLayerTiling* high_res =
1552 active_layer_->tilings()->TilingAtScale(new_scale);
1553 ASSERT_TRUE(!!high_res);
1554 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
1557 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
1558 SetupDefaultTrees(gfx::Size(10, 10));
1560 const float kScale = 1.f;
1561 pending_layer_->AddTiling(kScale);
1562 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1563 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1565 // Gpu rasterization is disabled by default.
1566 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1567 // Toggling the gpu rasterization clears all tilings on both trees.
1568 host_impl_.SetUseGpuRasterization(true);
1569 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1570 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1572 // Make sure that we can still add tiling to the pending layer,
1573 // that gets synced to the active layer.
1574 pending_layer_->AddTiling(kScale);
1575 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1576 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1578 // Toggling the gpu rasterization clears all tilings on both trees.
1579 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1580 host_impl_.SetUseGpuRasterization(false);
1581 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1582 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1585 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
1586 SetupDefaultTrees(gfx::Size(10, 10));
1587 host_impl_.active_tree()->UpdateDrawProperties();
1588 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1590 SetupDrawPropertiesAndUpdateTiles(
1591 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
1592 active_layer_->tilings()->RemoveAllTilings();
1593 PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
1594 active_layer_->tilings()->AddTiling(1.5f);
1595 active_layer_->tilings()->AddTiling(0.25f);
1596 tiling->set_resolution(HIGH_RESOLUTION);
1598 // Sanity checks.
1599 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1600 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
1602 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
1603 // 1.0f). Note that we should also ensure that the pending layer needs post
1604 // commit initialization, since this is what would happen during commit. In
1605 // other words we want the pending layer to sync from the active layer.
1606 pending_layer_->SetBounds(gfx::Size(1, 1));
1607 pending_layer_->SetNeedsPostCommitInitialization();
1608 pending_layer_->set_twin_layer(NULL);
1609 active_layer_->set_twin_layer(NULL);
1610 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1612 // Update the draw properties: sync from active tree should happen here.
1613 host_impl_.pending_tree()->UpdateDrawProperties();
1615 // Another sanity check.
1616 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
1618 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
1619 // violate minimum contents scale. At the same time, we should've created a
1620 // new high res tiling at scale 1.0f.
1621 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1622 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
1623 EXPECT_EQ(HIGH_RESOLUTION,
1624 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
1625 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
1626 EXPECT_EQ(NON_IDEAL_RESOLUTION,
1627 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
1630 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
1631 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
1632 gfx::Size layer_bounds(default_tile_size.width() * 4,
1633 default_tile_size.height() * 4);
1635 SetupDefaultTrees(layer_bounds);
1636 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1637 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1638 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1639 // Should have a low-res and a high-res tiling.
1640 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1642 ResetTilingsAndRasterScales();
1644 host_impl_.SetUseGpuRasterization(true);
1645 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1646 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1648 // Should only have the high-res tiling.
1649 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
1652 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
1653 // Set up layers with tilings.
1654 SetupDefaultTrees(gfx::Size(10, 10));
1655 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
1656 pending_layer_->PushPropertiesTo(active_layer_);
1657 EXPECT_TRUE(pending_layer_->DrawsContent());
1658 EXPECT_TRUE(pending_layer_->CanHaveTilings());
1659 EXPECT_GE(pending_layer_->num_tilings(), 0u);
1660 EXPECT_GE(active_layer_->num_tilings(), 0u);
1662 // Set content to false, which should make CanHaveTilings return false.
1663 pending_layer_->SetDrawsContent(false);
1664 EXPECT_FALSE(pending_layer_->DrawsContent());
1665 EXPECT_FALSE(pending_layer_->CanHaveTilings());
1667 // No tilings should be pushed to active layer.
1668 pending_layer_->PushPropertiesTo(active_layer_);
1669 EXPECT_EQ(0u, active_layer_->num_tilings());
1672 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
1673 SetupDefaultTrees(gfx::Size(10, 10));
1674 host_impl_.PinchGestureBegin();
1675 float high_res_scale = 2.3f;
1676 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1678 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1679 EXPECT_FLOAT_EQ(high_res_scale,
1680 pending_layer_->HighResTiling()->contents_scale());
1683 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
1684 SetupDefaultTrees(gfx::Size(10, 10));
1685 host_impl_.PinchGestureBegin();
1686 float high_res_scale = 0.0001f;
1687 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
1689 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1691 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1692 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1693 pending_layer_->HighResTiling()->contents_scale());
1696 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
1697 SetupDefaultTrees(gfx::Size(10, 10));
1699 float contents_scale = 0.15f;
1700 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
1702 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1703 EXPECT_FLOAT_EQ(contents_scale,
1704 pending_layer_->HighResTiling()->contents_scale());
1706 host_impl_.PinchGestureBegin();
1708 float page_scale = 0.0001f;
1709 EXPECT_LT(page_scale * contents_scale,
1710 pending_layer_->MinimumContentsScale());
1712 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
1713 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1714 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1715 pending_layer_->HighResTiling()->contents_scale());
1718 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1719 public:
1720 virtual void InitializeRenderer() OVERRIDE {
1721 bool delegated_rendering = false;
1722 host_impl_.InitializeRenderer(
1723 FakeOutputSurface::CreateDeferredGL(
1724 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
1725 delegated_rendering).PassAs<OutputSurface>());
1728 virtual void SetUp() OVERRIDE {
1729 PictureLayerImplTest::SetUp();
1731 // Create some default active and pending trees.
1732 gfx::Size tile_size(100, 100);
1733 gfx::Size layer_bounds(400, 400);
1735 scoped_refptr<FakePicturePileImpl> pending_pile =
1736 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1737 scoped_refptr<FakePicturePileImpl> active_pile =
1738 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1740 SetupTrees(pending_pile, active_pile);
1744 // This test is really a LayerTreeHostImpl test, in that it makes sure
1745 // that trees need update draw properties after deferred initialization.
1746 // However, this is also a regression test for PictureLayerImpl in that
1747 // not having this update will cause a crash.
1748 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
1749 host_impl_.pending_tree()->UpdateDrawProperties();
1750 host_impl_.active_tree()->UpdateDrawProperties();
1751 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1752 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1754 FakeOutputSurface* fake_output_surface =
1755 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1756 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1757 TestContextProvider::Create()));
1759 // These will crash PictureLayerImpl if this is not true.
1760 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1761 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1762 host_impl_.active_tree()->UpdateDrawProperties();
1765 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
1766 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1767 SetupDefaultTrees(tile_size);
1769 float contents_scale = 1.f;
1770 float device_scale = 1.3f;
1771 float page_scale = 1.4f;
1772 float maximum_animation_scale = 1.f;
1773 bool animating_transform = false;
1775 SetContentsScaleOnBothLayers(contents_scale,
1776 device_scale,
1777 page_scale,
1778 maximum_animation_scale,
1779 animating_transform);
1780 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1782 // Since we're CPU-rasterizing, starting an animation should cause tiling
1783 // resolution to get set to the maximum animation scale factor.
1784 animating_transform = true;
1785 maximum_animation_scale = 3.f;
1786 contents_scale = 2.f;
1788 SetContentsScaleOnBothLayers(contents_scale,
1789 device_scale,
1790 page_scale,
1791 maximum_animation_scale,
1792 animating_transform);
1793 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
1795 // Further changes to scale during the animation should not cause a new
1796 // high-res tiling to get created.
1797 contents_scale = 4.f;
1798 maximum_animation_scale = 5.f;
1800 SetContentsScaleOnBothLayers(contents_scale,
1801 device_scale,
1802 page_scale,
1803 maximum_animation_scale,
1804 animating_transform);
1805 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
1807 // Once we stop animating, a new high-res tiling should be created.
1808 animating_transform = false;
1810 SetContentsScaleOnBothLayers(contents_scale,
1811 device_scale,
1812 page_scale,
1813 maximum_animation_scale,
1814 animating_transform);
1815 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
1817 // When animating with an unknown maximum animation scale factor, a new
1818 // high-res tiling should be created at the animation's initial scale.
1819 animating_transform = true;
1820 contents_scale = 2.f;
1821 maximum_animation_scale = 0.f;
1823 SetContentsScaleOnBothLayers(contents_scale,
1824 device_scale,
1825 page_scale,
1826 maximum_animation_scale,
1827 animating_transform);
1828 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1830 // Further changes to scale during the animation should not cause a new
1831 // high-res tiling to get created.
1832 contents_scale = 3.f;
1834 SetContentsScaleOnBothLayers(contents_scale,
1835 device_scale,
1836 page_scale,
1837 maximum_animation_scale,
1838 animating_transform);
1839 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1841 // Once we stop animating, a new high-res tiling should be created.
1842 animating_transform = false;
1843 contents_scale = 4.f;
1845 SetContentsScaleOnBothLayers(contents_scale,
1846 device_scale,
1847 page_scale,
1848 maximum_animation_scale,
1849 animating_transform);
1850 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
1853 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
1854 gfx::Size tile_size(100, 100);
1855 gfx::Size layer_bounds(1000, 1000);
1857 scoped_refptr<FakePicturePileImpl> pending_pile =
1858 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1860 SetupPendingTree(pending_pile);
1862 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1864 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1866 // Empty iterator
1867 PictureLayerImpl::LayerRasterTileIterator it;
1868 EXPECT_FALSE(it);
1870 // No tilings.
1871 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
1872 EXPECT_FALSE(it);
1874 pending_layer_->AddTiling(low_res_factor);
1875 pending_layer_->AddTiling(0.3f);
1876 pending_layer_->AddTiling(0.7f);
1877 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
1878 pending_layer_->AddTiling(2.0f);
1880 host_impl_.SetViewportSize(gfx::Size(500, 500));
1881 host_impl_.pending_tree()->UpdateDrawProperties();
1883 std::set<Tile*> unique_tiles;
1884 bool reached_prepaint = false;
1885 size_t non_ideal_tile_count = 0u;
1886 size_t low_res_tile_count = 0u;
1887 size_t high_res_tile_count = 0u;
1888 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
1890 ++it) {
1891 Tile* tile = *it;
1892 TilePriority priority = tile->priority(PENDING_TREE);
1894 EXPECT_TRUE(tile);
1896 // Non-high res tiles only get visible tiles. Also, prepaint should only
1897 // come at the end of the iteration.
1898 if (priority.resolution != HIGH_RESOLUTION)
1899 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
1900 else if (reached_prepaint)
1901 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
1902 else
1903 reached_prepaint = priority.priority_bin != TilePriority::NOW;
1905 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
1906 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
1907 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
1909 unique_tiles.insert(tile);
1912 EXPECT_TRUE(reached_prepaint);
1913 EXPECT_EQ(0u, non_ideal_tile_count);
1914 EXPECT_EQ(1u, low_res_tile_count);
1915 EXPECT_EQ(16u, high_res_tile_count);
1916 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
1917 unique_tiles.size());
1919 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
1920 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
1921 tile_it != high_res_tiles.end();
1922 ++tile_it) {
1923 Tile* tile = *tile_it;
1924 ManagedTileState::TileVersion& tile_version =
1925 tile->GetTileVersionForTesting(
1926 tile->DetermineRasterModeForTree(ACTIVE_TREE));
1927 tile_version.SetSolidColorForTesting(SK_ColorRED);
1930 non_ideal_tile_count = 0;
1931 low_res_tile_count = 0;
1932 high_res_tile_count = 0;
1933 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
1935 ++it) {
1936 Tile* tile = *it;
1937 TilePriority priority = tile->priority(PENDING_TREE);
1939 EXPECT_TRUE(tile);
1941 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
1942 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
1943 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
1946 EXPECT_EQ(0u, non_ideal_tile_count);
1947 EXPECT_EQ(1u, low_res_tile_count);
1948 EXPECT_EQ(0u, high_res_tile_count);
1951 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
1952 gfx::Size tile_size(100, 100);
1953 gfx::Size layer_bounds(1000, 1000);
1955 scoped_refptr<FakePicturePileImpl> pending_pile =
1956 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1958 SetupPendingTree(pending_pile);
1960 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1962 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1964 std::vector<PictureLayerTiling*> tilings;
1965 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
1966 tilings.push_back(pending_layer_->AddTiling(0.3f));
1967 tilings.push_back(pending_layer_->AddTiling(0.7f));
1968 tilings.push_back(pending_layer_->AddTiling(1.0f));
1969 tilings.push_back(pending_layer_->AddTiling(2.0f));
1971 host_impl_.SetViewportSize(gfx::Size(500, 500));
1972 host_impl_.pending_tree()->UpdateDrawProperties();
1974 std::vector<Tile*> all_tiles;
1975 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
1976 tilings.begin();
1977 tiling_iterator != tilings.end();
1978 ++tiling_iterator) {
1979 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
1980 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
1983 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
1985 bool mark_required = false;
1986 size_t number_of_marked_tiles = 0u;
1987 size_t number_of_unmarked_tiles = 0u;
1988 for (size_t i = 0; i < tilings.size(); ++i) {
1989 PictureLayerTiling* tiling = tilings.at(i);
1990 for (PictureLayerTiling::CoverageIterator iter(
1991 tiling,
1992 pending_layer_->contents_scale_x(),
1993 pending_layer_->visible_content_rect());
1994 iter;
1995 ++iter) {
1996 if (mark_required) {
1997 number_of_marked_tiles++;
1998 iter->MarkRequiredForActivation();
1999 } else {
2000 number_of_unmarked_tiles++;
2002 mark_required = !mark_required;
2006 // Sanity checks.
2007 EXPECT_EQ(91u, all_tiles.size());
2008 EXPECT_EQ(91u, all_tiles_set.size());
2009 EXPECT_GT(number_of_marked_tiles, 1u);
2010 EXPECT_GT(number_of_unmarked_tiles, 1u);
2012 // Empty iterator.
2013 PictureLayerImpl::LayerEvictionTileIterator it;
2014 EXPECT_FALSE(it);
2016 // Tiles don't have resources yet.
2017 it = PictureLayerImpl::LayerEvictionTileIterator(
2018 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2019 EXPECT_FALSE(it);
2021 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2023 std::set<Tile*> unique_tiles;
2024 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2025 size_t scale_index = 0;
2026 bool reached_visible = false;
2027 Tile* last_tile = NULL;
2028 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2029 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2031 ++it) {
2032 Tile* tile = *it;
2033 if (!last_tile)
2034 last_tile = tile;
2036 EXPECT_TRUE(tile);
2038 TilePriority priority = tile->priority(PENDING_TREE);
2040 if (priority.priority_bin == TilePriority::NOW) {
2041 reached_visible = true;
2042 last_tile = tile;
2043 break;
2046 EXPECT_FALSE(tile->required_for_activation());
2048 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2049 std::numeric_limits<float>::epsilon()) {
2050 ++scale_index;
2051 ASSERT_LT(scale_index, arraysize(expected_scales));
2054 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2055 unique_tiles.insert(tile);
2057 // If the tile is the same rough bin as last tile (same activation, bin, and
2058 // scale), then distance should be decreasing.
2059 if (tile->required_for_activation() ==
2060 last_tile->required_for_activation() &&
2061 priority.priority_bin ==
2062 last_tile->priority(PENDING_TREE).priority_bin &&
2063 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2064 std::numeric_limits<float>::epsilon()) {
2065 EXPECT_LE(priority.distance_to_visible,
2066 last_tile->priority(PENDING_TREE).distance_to_visible);
2069 last_tile = tile;
2072 EXPECT_TRUE(reached_visible);
2073 EXPECT_EQ(65u, unique_tiles.size());
2075 scale_index = 0;
2076 bool reached_required = false;
2077 for (; it; ++it) {
2078 Tile* tile = *it;
2079 EXPECT_TRUE(tile);
2081 TilePriority priority = tile->priority(PENDING_TREE);
2082 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2084 if (reached_required) {
2085 EXPECT_TRUE(tile->required_for_activation());
2086 } else if (tile->required_for_activation()) {
2087 reached_required = true;
2088 scale_index = 0;
2091 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2092 std::numeric_limits<float>::epsilon()) {
2093 ++scale_index;
2094 ASSERT_LT(scale_index, arraysize(expected_scales));
2097 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2098 unique_tiles.insert(tile);
2101 EXPECT_TRUE(reached_required);
2102 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2105 TEST_F(PictureLayerImplTest, Occlusion) {
2106 gfx::Size tile_size(102, 102);
2107 gfx::Size layer_bounds(1000, 1000);
2108 gfx::Size viewport_size(1000, 1000);
2110 LayerTestCommon::LayerImplTest impl;
2112 scoped_refptr<FakePicturePileImpl> pending_pile =
2113 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2114 SetupPendingTree(pending_pile);
2115 pending_layer_->SetBounds(layer_bounds);
2116 ActivateTree();
2117 active_layer_->set_fixed_tile_size(tile_size);
2119 host_impl_.SetViewportSize(viewport_size);
2120 host_impl_.active_tree()->UpdateDrawProperties();
2122 std::vector<Tile*> tiles =
2123 active_layer_->HighResTiling()->AllTilesForTesting();
2124 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2127 SCOPED_TRACE("No occlusion");
2128 gfx::Rect occluded;
2129 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2131 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2132 gfx::Rect(layer_bounds));
2133 EXPECT_EQ(100u, impl.quad_list().size());
2137 SCOPED_TRACE("Full occlusion");
2138 gfx::Rect occluded(active_layer_->visible_content_rect());
2139 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2141 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2142 EXPECT_EQ(impl.quad_list().size(), 0u);
2146 SCOPED_TRACE("Partial occlusion");
2147 gfx::Rect occluded(150, 0, 200, 1000);
2148 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2150 size_t partially_occluded_count = 0;
2151 LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
2152 impl.quad_list(),
2153 gfx::Rect(layer_bounds),
2154 occluded,
2155 &partially_occluded_count);
2156 // The layer outputs one quad, which is partially occluded.
2157 EXPECT_EQ(100u - 10u, impl.quad_list().size());
2158 EXPECT_EQ(10u + 10u, partially_occluded_count);
2162 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2163 gfx::Size tile_size(host_impl_.settings().default_tile_size);
2164 SetupDefaultTrees(tile_size);
2166 float contents_scale = 2.f;
2167 float device_scale = 1.f;
2168 float page_scale = 1.f;
2169 float maximum_animation_scale = 1.f;
2170 bool animating_transform = false;
2172 SetContentsScaleOnBothLayers(contents_scale,
2173 device_scale,
2174 page_scale,
2175 maximum_animation_scale,
2176 animating_transform);
2177 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2179 // Changing the source scale without being in an animation will cause
2180 // the layer to reset its source scale to 1.f.
2181 contents_scale = 3.f;
2183 SetContentsScaleOnBothLayers(contents_scale,
2184 device_scale,
2185 page_scale,
2186 maximum_animation_scale,
2187 animating_transform);
2188 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2190 // Further changes to the source scale will no longer be reflected in the
2191 // contents scale.
2192 contents_scale = 0.5f;
2194 SetContentsScaleOnBothLayers(contents_scale,
2195 device_scale,
2196 page_scale,
2197 maximum_animation_scale,
2198 animating_transform);
2199 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2202 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2203 gfx::Size tile_size(100, 100);
2204 gfx::Size layer_bounds(1000, 1000);
2206 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2208 // Make sure some tiles are not shared.
2209 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2211 CreateHighLowResAndSetAllTilesVisible();
2212 active_layer_->SetAllTilesReady();
2213 pending_layer_->MarkVisibleResourcesAsRequired();
2215 // All pending layer tiles required are not ready.
2216 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2218 // Initialize all low-res tiles.
2219 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2221 // Low-res tiles should not be enough.
2222 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2224 // Initialize remaining tiles.
2225 pending_layer_->SetAllTilesReady();
2227 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2230 TEST_F(PictureLayerImplTest, HighResReadyToDrawNotEnoughToActivate) {
2231 gfx::Size tile_size(100, 100);
2232 gfx::Size layer_bounds(1000, 1000);
2234 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2236 // Make sure some tiles are not shared.
2237 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2239 CreateHighLowResAndSetAllTilesVisible();
2240 active_layer_->SetAllTilesReady();
2241 pending_layer_->MarkVisibleResourcesAsRequired();
2243 // All pending layer tiles required are not ready.
2244 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2246 // Initialize all high-res tiles.
2247 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2249 // High-res tiles should not be enough.
2250 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2252 // Initialize remaining tiles.
2253 pending_layer_->SetAllTilesReady();
2255 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2258 class NoLowResTilingsSettings : public ImplSidePaintingSettings {
2259 public:
2260 NoLowResTilingsSettings() { create_low_res_tiling = false; }
2263 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
2264 public:
2265 NoLowResPictureLayerImplTest()
2266 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
2269 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
2270 gfx::Size tile_size(400, 400);
2271 gfx::Size layer_bounds(1300, 1900);
2273 scoped_refptr<FakePicturePileImpl> pending_pile =
2274 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2275 scoped_refptr<FakePicturePileImpl> active_pile =
2276 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2278 SetupTrees(pending_pile, active_pile);
2279 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2281 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2282 EXPECT_LT(low_res_factor, 1.f);
2284 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2285 6.f, // ideal contents scale
2286 3.f, // device scale
2287 2.f, // page scale
2288 1.f, // maximum animation scale
2289 false);
2290 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2291 EXPECT_FLOAT_EQ(6.f,
2292 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2294 // If we change the page scale factor, then we should get new tilings.
2295 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2296 6.6f, // ideal contents scale
2297 3.f, // device scale
2298 2.2f, // page scale
2299 1.f, // maximum animation scale
2300 false);
2301 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2302 EXPECT_FLOAT_EQ(6.6f,
2303 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2305 // If we change the device scale factor, then we should get new tilings.
2306 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2307 7.26f, // ideal contents scale
2308 3.3f, // device scale
2309 2.2f, // page scale
2310 1.f, // maximum animation scale
2311 false);
2312 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2313 EXPECT_FLOAT_EQ(7.26f,
2314 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2316 // If we change the device scale factor, but end up at the same total scale
2317 // factor somehow, then we don't get new tilings.
2318 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2319 7.26f, // ideal contents scale
2320 2.2f, // device scale
2321 3.3f, // page scale
2322 1.f, // maximum animation scale
2323 false);
2324 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2325 EXPECT_FLOAT_EQ(7.26f,
2326 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2329 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
2330 gfx::Size tile_size(100, 100);
2331 gfx::Size layer_bounds(1000, 1000);
2333 scoped_refptr<FakePicturePileImpl> pending_pile =
2334 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2335 // Layers with entirely empty piles can't get tilings.
2336 pending_pile->AddRecordingAt(0, 0);
2338 SetupPendingTree(pending_pile);
2340 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2341 pending_layer_->AddTiling(1.0f);
2342 pending_layer_->AddTiling(2.0f);
2344 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
2345 // on a layer with no recordings.
2346 host_impl_.pending_tree()->UpdateDrawProperties();
2347 pending_layer_->MarkVisibleResourcesAsRequired();
2350 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
2351 gfx::Size layer_bounds(400, 400);
2352 gfx::Size tile_size(100, 100);
2353 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2355 CreateHighLowResAndSetAllTilesVisible();
2357 Tile* some_active_tile =
2358 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2359 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2361 // All tiles shared (no invalidation), so even though the active tree's
2362 // tiles aren't ready, there is nothing required.
2363 pending_layer_->MarkVisibleResourcesAsRequired();
2364 AssertNoTilesRequired(pending_layer_->HighResTiling());
2365 if (host_impl_.settings().create_low_res_tiling) {
2366 AssertNoTilesRequired(pending_layer_->LowResTiling());
2370 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2371 gfx::Size layer_bounds(400, 400);
2372 gfx::Size tile_size(100, 100);
2373 scoped_refptr<FakePicturePileImpl> pending_pile =
2374 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2375 // This pile will create tilings, but has no recordings so will not create any
2376 // tiles. This is attempting to simulate scrolling past the end of recorded
2377 // content on the active layer, where the recordings are so far away that
2378 // no tiles are created.
2379 scoped_refptr<FakePicturePileImpl> active_pile =
2380 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2381 tile_size, layer_bounds);
2382 SetupTrees(pending_pile, active_pile);
2383 pending_layer_->set_fixed_tile_size(tile_size);
2384 active_layer_->set_fixed_tile_size(tile_size);
2386 CreateHighLowResAndSetAllTilesVisible();
2388 // Active layer has tilings, but no tiles due to missing recordings.
2389 EXPECT_TRUE(active_layer_->CanHaveTilings());
2390 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
2391 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
2392 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2394 // Since the active layer has no tiles at all, the pending layer doesn't
2395 // need content in order to activate.
2396 pending_layer_->MarkVisibleResourcesAsRequired();
2397 AssertNoTilesRequired(pending_layer_->HighResTiling());
2398 if (host_impl_.settings().create_low_res_tiling)
2399 AssertNoTilesRequired(pending_layer_->LowResTiling());
2402 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
2403 base::TimeTicks time_ticks;
2404 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2406 gfx::Size tile_size(100, 100);
2407 gfx::Size layer_bounds(400, 400);
2409 scoped_refptr<FakePicturePileImpl> pending_pile =
2410 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2411 scoped_refptr<FakePicturePileImpl> active_pile =
2412 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2414 SetupTrees(pending_pile, active_pile);
2416 Region invalidation;
2417 AddDefaultTilingsWithInvalidation(invalidation);
2418 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
2420 // UpdateTiles with valid viewport. Should update tile viewport.
2421 bool valid_for_tile_management = true;
2422 gfx::Rect viewport = gfx::Rect(layer_bounds);
2423 gfx::Transform transform;
2424 host_impl_.SetExternalDrawConstraints(
2425 transform, viewport, viewport, valid_for_tile_management);
2426 active_layer_->draw_properties().visible_content_rect = viewport;
2427 active_layer_->draw_properties().screen_space_transform = transform;
2428 active_layer_->UpdateTiles(NULL);
2430 gfx::Rect visible_rect_for_tile_priority =
2431 active_layer_->visible_rect_for_tile_priority();
2432 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
2433 gfx::Size viewport_size_for_tile_priority =
2434 active_layer_->viewport_size_for_tile_priority();
2435 EXPECT_FALSE(viewport_size_for_tile_priority.IsEmpty());
2436 gfx::Transform screen_space_transform_for_tile_priority =
2437 active_layer_->screen_space_transform_for_tile_priority();
2439 // Expand viewport and set it as invalid for prioritizing tiles.
2440 // Should not update tile viewport.
2441 time_ticks += base::TimeDelta::FromMilliseconds(200);
2442 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2443 valid_for_tile_management = false;
2444 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
2445 transform.Translate(1.f, 1.f);
2446 active_layer_->draw_properties().visible_content_rect = viewport;
2447 active_layer_->draw_properties().screen_space_transform = transform;
2448 host_impl_.SetExternalDrawConstraints(
2449 transform, viewport, viewport, valid_for_tile_management);
2450 active_layer_->UpdateTiles(NULL);
2452 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
2453 active_layer_->visible_rect_for_tile_priority());
2454 EXPECT_SIZE_EQ(viewport_size_for_tile_priority,
2455 active_layer_->viewport_size_for_tile_priority());
2456 EXPECT_TRANSFORMATION_MATRIX_EQ(
2457 screen_space_transform_for_tile_priority,
2458 active_layer_->screen_space_transform_for_tile_priority());
2460 // Keep expanded viewport but mark it valid. Should update tile viewport.
2461 time_ticks += base::TimeDelta::FromMilliseconds(200);
2462 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2463 valid_for_tile_management = true;
2464 host_impl_.SetExternalDrawConstraints(
2465 transform, viewport, viewport, valid_for_tile_management);
2466 active_layer_->UpdateTiles(NULL);
2468 EXPECT_FALSE(visible_rect_for_tile_priority ==
2469 active_layer_->visible_rect_for_tile_priority());
2470 EXPECT_FALSE(viewport_size_for_tile_priority ==
2471 active_layer_->viewport_size_for_tile_priority());
2472 EXPECT_FALSE(screen_space_transform_for_tile_priority ==
2473 active_layer_->screen_space_transform_for_tile_priority());
2476 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportAfterReleaseResources) {
2477 base::TimeTicks time_ticks;
2478 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2480 gfx::Size tile_size(100, 100);
2481 gfx::Size layer_bounds(400, 400);
2483 scoped_refptr<FakePicturePileImpl> pending_pile =
2484 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2485 scoped_refptr<FakePicturePileImpl> active_pile =
2486 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2488 SetupTrees(pending_pile, active_pile);
2490 Region invalidation;
2491 AddDefaultTilingsWithInvalidation(invalidation);
2493 bool valid_for_tile_management = false;
2494 gfx::Rect viewport = gfx::Rect(layer_bounds);
2495 host_impl_.SetExternalDrawConstraints(
2496 gfx::Transform(), viewport, viewport, valid_for_tile_management);
2497 ResetTilingsAndRasterScales();
2498 host_impl_.pending_tree()->UpdateDrawProperties();
2499 host_impl_.active_tree()->UpdateDrawProperties();
2500 EXPECT_TRUE(active_layer_->HighResTiling());
2502 size_t num_tilings = active_layer_->num_tilings();
2503 active_layer_->UpdateTiles(NULL);
2504 pending_layer_->AddTiling(0.5f);
2505 EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
2508 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
2509 gfx::Size tile_size(400, 400);
2510 gfx::Size layer_bounds(1300, 1900);
2512 scoped_refptr<FakePicturePileImpl> pending_pile =
2513 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2514 scoped_refptr<FakePicturePileImpl> active_pile =
2515 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2517 std::vector<PictureLayerTiling*> used_tilings;
2519 SetupTrees(pending_pile, active_pile);
2520 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2522 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2523 EXPECT_LT(low_res_factor, 1.f);
2525 float device_scale = 1.7f;
2526 float page_scale = 3.2f;
2527 float scale = 1.f;
2529 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2530 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2532 // We only have ideal tilings, so they aren't removed.
2533 used_tilings.clear();
2534 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2535 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2537 host_impl_.PinchGestureBegin();
2539 // Changing the ideal but not creating new tilings.
2540 scale *= 1.5f;
2541 page_scale *= 1.5f;
2542 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2543 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2545 // The tilings are still our target scale, so they aren't removed.
2546 used_tilings.clear();
2547 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2548 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2550 host_impl_.PinchGestureEnd();
2552 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
2553 scale /= 4.f;
2554 page_scale /= 4.f;
2555 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
2556 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2557 EXPECT_FLOAT_EQ(1.f,
2558 active_layer_->tilings()->tiling_at(1)->contents_scale());
2560 // Mark the non-ideal tilings as used. They won't be removed.
2561 used_tilings.clear();
2562 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2563 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2564 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2566 // Now move the ideal scale to 0.5. Our target stays 1.2.
2567 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
2569 // The high resolution tiling is between target and ideal, so is not
2570 // removed. The low res tiling for the old ideal=1.0 scale is removed.
2571 used_tilings.clear();
2572 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2573 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2575 // Now move the ideal scale to 1.0. Our target stays 1.2.
2576 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
2578 // All the tilings are between are target and the ideal, so they are not
2579 // removed.
2580 used_tilings.clear();
2581 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2582 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2584 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
2585 SetupDrawPropertiesAndUpdateTiles(
2586 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2588 // Because the pending layer's ideal scale is still 1.0, our tilings fall
2589 // in the range [1.0,1.2] and are kept.
2590 used_tilings.clear();
2591 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2592 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2594 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
2595 // 1.2 still.
2596 SetupDrawPropertiesAndUpdateTiles(
2597 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2599 // Our 1.0 tiling now falls outside the range between our ideal scale and our
2600 // target raster scale. But it is in our used tilings set, so nothing is
2601 // deleted.
2602 used_tilings.clear();
2603 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2604 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2605 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2607 // If we remove it from our used tilings set, it is outside the range to keep
2608 // so it is deleted.
2609 used_tilings.clear();
2610 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2611 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2614 TEST_F(PictureLayerImplTest, ScaleCollision) {
2615 gfx::Size tile_size(400, 400);
2616 gfx::Size layer_bounds(1300, 1900);
2618 scoped_refptr<FakePicturePileImpl> pending_pile =
2619 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2620 scoped_refptr<FakePicturePileImpl> active_pile =
2621 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2623 std::vector<PictureLayerTiling*> used_tilings;
2625 SetupTrees(pending_pile, active_pile);
2627 float pending_contents_scale = 1.f;
2628 float active_contents_scale = 2.f;
2629 float device_scale_factor = 1.f;
2630 float page_scale_factor = 1.f;
2631 float maximum_animation_contents_scale = 1.f;
2632 bool animating_transform = false;
2634 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
2635 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2636 EXPECT_LT(low_res_factor, 1.f);
2638 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2639 pending_contents_scale,
2640 device_scale_factor,
2641 page_scale_factor,
2642 maximum_animation_contents_scale,
2643 animating_transform);
2644 SetupDrawPropertiesAndUpdateTiles(active_layer_,
2645 active_contents_scale,
2646 device_scale_factor,
2647 page_scale_factor,
2648 maximum_animation_contents_scale,
2649 animating_transform);
2651 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
2652 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2654 EXPECT_EQ(active_contents_scale,
2655 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2656 EXPECT_EQ(pending_contents_scale,
2657 pending_layer_->tilings()->tiling_at(1)->contents_scale());
2658 EXPECT_EQ(active_contents_scale * low_res_factor,
2659 pending_layer_->tilings()->tiling_at(2)->contents_scale());
2660 EXPECT_EQ(pending_contents_scale * low_res_factor,
2661 pending_layer_->tilings()->tiling_at(3)->contents_scale());
2663 EXPECT_EQ(active_contents_scale,
2664 active_layer_->tilings()->tiling_at(0)->contents_scale());
2665 EXPECT_EQ(pending_contents_scale,
2666 active_layer_->tilings()->tiling_at(1)->contents_scale());
2667 EXPECT_EQ(active_contents_scale * low_res_factor,
2668 active_layer_->tilings()->tiling_at(2)->contents_scale());
2669 EXPECT_EQ(pending_contents_scale * low_res_factor,
2670 active_layer_->tilings()->tiling_at(3)->contents_scale());
2672 // The unused low res tiling from the pending tree must be kept or we may add
2673 // it again on the active tree and collide with the pending tree.
2674 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2675 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2676 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2678 EXPECT_EQ(active_contents_scale,
2679 active_layer_->tilings()->tiling_at(0)->contents_scale());
2680 EXPECT_EQ(pending_contents_scale,
2681 active_layer_->tilings()->tiling_at(1)->contents_scale());
2682 EXPECT_EQ(active_contents_scale * low_res_factor,
2683 active_layer_->tilings()->tiling_at(2)->contents_scale());
2684 EXPECT_EQ(pending_contents_scale * low_res_factor,
2685 active_layer_->tilings()->tiling_at(3)->contents_scale());
2688 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
2689 gfx::Size tile_size(400, 400);
2690 gfx::Size layer_bounds(1300, 1900);
2692 scoped_refptr<FakePicturePileImpl> pending_pile =
2693 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2694 scoped_refptr<FakePicturePileImpl> active_pile =
2695 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2697 SetupTrees(pending_pile, active_pile);
2698 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2700 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2701 1.3f, // ideal contents scale
2702 2.7f, // device scale
2703 3.2f, // page scale
2704 1.f, // maximum animation scale
2705 false);
2706 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2708 // All tilings should be removed when losing output surface.
2709 active_layer_->ReleaseResources();
2710 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2711 pending_layer_->ReleaseResources();
2712 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2714 // This should create new tilings.
2715 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2716 1.3f, // ideal contents scale
2717 2.7f, // device scale
2718 3.2f, // page scale
2719 1.f, // maximum animation scale
2720 false);
2721 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2724 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
2725 MockOcclusionTracker<LayerImpl> occlusion_tracker;
2726 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
2728 gfx::Size tile_size(400, 400);
2729 gfx::Size layer_bounds(1000, 2000);
2731 scoped_refptr<FakePicturePileImpl> pending_pile =
2732 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2733 scoped_refptr<FakePicturePileImpl> active_pile =
2734 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2736 SetupTrees(pending_pile, active_pile);
2738 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
2739 host_impl_.pending_tree()->UpdateDrawProperties();
2741 active_layer_->draw_properties().visible_content_rect =
2742 gfx::Rect(layer_bounds);
2743 host_impl_.active_tree()->UpdateDrawProperties();
2745 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
2746 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
2747 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
2748 SK_MScalar1 / max_contents_scale);
2750 AppendQuadsData data;
2751 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
2753 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
2754 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
2755 // The content_to_target_transform should be scaled by the
2756 // MaximumTilingContentsScale on the layer.
2757 EXPECT_EQ(scaled_draw_transform.ToString(),
2758 render_pass->shared_quad_state_list[0]
2759 ->content_to_target_transform.ToString());
2760 // The content_bounds should be scaled by the
2761 // MaximumTilingContentsScale on the layer.
2762 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
2763 render_pass->shared_quad_state_list[0]->content_bounds.ToString());
2764 // The visible_content_rect should be scaled by the
2765 // MaximumTilingContentsScale on the layer.
2766 EXPECT_EQ(
2767 gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
2768 render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
2771 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
2772 gfx::Size tile_size(400, 400);
2773 gfx::Size bounds(100000, 100);
2775 host_impl_.CreatePendingTree();
2777 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
2779 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
2780 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
2782 layer_with_mask->SetBounds(bounds);
2783 layer_with_mask->SetContentBounds(bounds);
2785 scoped_refptr<FakePicturePileImpl> pending_pile =
2786 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
2787 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
2788 host_impl_.pending_tree(), 3, pending_pile);
2790 mask->SetIsMask(true);
2791 mask->SetBounds(bounds);
2792 mask->SetContentBounds(bounds);
2793 mask->SetDrawsContent(true);
2795 FakePictureLayerImpl* pending_mask_content = mask.get();
2796 layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
2798 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
2799 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
2801 child_of_layer_with_mask->SetBounds(bounds);
2802 child_of_layer_with_mask->SetContentBounds(bounds);
2803 child_of_layer_with_mask->SetDrawsContent(true);
2805 layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
2807 root->AddChild(layer_with_mask.PassAs<LayerImpl>());
2809 host_impl_.pending_tree()->SetRootLayer(root.Pass());
2811 EXPECT_FALSE(pending_mask_content->tilings());
2812 host_impl_.pending_tree()->UpdateDrawProperties();
2813 EXPECT_NE(0u, pending_mask_content->num_tilings());
2816 class OcclusionTrackingSettings : public ImplSidePaintingSettings {
2817 public:
2818 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
2821 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
2822 public:
2823 OcclusionTrackingPictureLayerImplTest()
2824 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
2827 TEST_F(OcclusionTrackingPictureLayerImplTest,
2828 OccludedTilesSkippedDuringRasterization) {
2829 gfx::Size tile_size(102, 102);
2830 gfx::Size layer_bounds(1000, 1000);
2831 gfx::Size viewport_size(500, 500);
2832 gfx::Point occluding_layer_position(310, 0);
2834 scoped_refptr<FakePicturePileImpl> pending_pile =
2835 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2836 SetupPendingTree(pending_pile);
2837 pending_layer_->set_fixed_tile_size(tile_size);
2839 host_impl_.SetViewportSize(viewport_size);
2840 host_impl_.pending_tree()->UpdateDrawProperties();
2842 // No occlusion.
2843 int unoccluded_tile_count = 0;
2844 for (PictureLayerImpl::LayerRasterTileIterator it =
2845 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2847 ++it) {
2848 Tile* tile = *it;
2850 // Occluded tiles should not be iterated over.
2851 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
2853 // Some tiles may not be visible (i.e. outside the viewport). The rest are
2854 // visible and at least partially unoccluded, verified by the above expect.
2855 bool tile_is_visible =
2856 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
2857 if (tile_is_visible)
2858 unoccluded_tile_count++;
2860 EXPECT_EQ(unoccluded_tile_count, 25 + 4);
2862 // Partial occlusion.
2863 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
2864 LayerImpl* layer1 = pending_layer_->children()[0];
2865 layer1->SetBounds(layer_bounds);
2866 layer1->SetContentBounds(layer_bounds);
2867 layer1->SetDrawsContent(true);
2868 layer1->SetContentsOpaque(true);
2869 layer1->SetPosition(occluding_layer_position);
2871 host_impl_.pending_tree()->UpdateDrawProperties();
2873 unoccluded_tile_count = 0;
2874 for (PictureLayerImpl::LayerRasterTileIterator it =
2875 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2877 ++it) {
2878 Tile* tile = *it;
2880 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
2882 bool tile_is_visible =
2883 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
2884 if (tile_is_visible)
2885 unoccluded_tile_count++;
2887 EXPECT_EQ(unoccluded_tile_count, 20 + 2);
2889 // Full occlusion.
2890 layer1->SetPosition(gfx::Point(0, 0));
2892 host_impl_.pending_tree()->UpdateDrawProperties();
2894 unoccluded_tile_count = 0;
2895 for (PictureLayerImpl::LayerRasterTileIterator it =
2896 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2898 ++it) {
2899 Tile* tile = *it;
2901 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
2903 bool tile_is_visible =
2904 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
2905 if (tile_is_visible)
2906 unoccluded_tile_count++;
2908 EXPECT_EQ(unoccluded_tile_count, 0);
2911 TEST_F(OcclusionTrackingPictureLayerImplTest,
2912 OccludedTilesNotMarkedAsRequired) {
2913 gfx::Size tile_size(102, 102);
2914 gfx::Size layer_bounds(1000, 1000);
2915 gfx::Size viewport_size(500, 500);
2916 gfx::Point occluding_layer_position(310, 0);
2918 scoped_refptr<FakePicturePileImpl> pending_pile =
2919 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2920 SetupPendingTree(pending_pile);
2921 pending_layer_->set_fixed_tile_size(tile_size);
2923 host_impl_.SetViewportSize(viewport_size);
2924 host_impl_.pending_tree()->UpdateDrawProperties();
2926 // No occlusion.
2927 int occluded_tile_count = 0;
2928 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
2929 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
2931 occluded_tile_count = 0;
2932 for (PictureLayerTiling::CoverageIterator iter(
2933 tiling,
2934 pending_layer_->contents_scale_x(),
2935 gfx::Rect(layer_bounds));
2936 iter;
2937 ++iter) {
2938 if (!*iter)
2939 continue;
2940 const Tile* tile = *iter;
2942 // Fully occluded tiles are not required for activation.
2943 if (tile->is_occluded(PENDING_TREE)) {
2944 EXPECT_FALSE(tile->required_for_activation());
2945 occluded_tile_count++;
2948 EXPECT_EQ(occluded_tile_count, 0);
2951 // Partial occlusion.
2952 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
2953 LayerImpl* layer1 = pending_layer_->children()[0];
2954 layer1->SetBounds(layer_bounds);
2955 layer1->SetContentBounds(layer_bounds);
2956 layer1->SetDrawsContent(true);
2957 layer1->SetContentsOpaque(true);
2958 layer1->SetPosition(occluding_layer_position);
2960 host_impl_.pending_tree()->UpdateDrawProperties();
2962 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
2963 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
2965 occluded_tile_count = 0;
2966 for (PictureLayerTiling::CoverageIterator iter(
2967 tiling,
2968 pending_layer_->contents_scale_x(),
2969 gfx::Rect(layer_bounds));
2970 iter;
2971 ++iter) {
2972 if (!*iter)
2973 continue;
2974 const Tile* tile = *iter;
2976 if (tile->is_occluded(PENDING_TREE)) {
2977 EXPECT_FALSE(tile->required_for_activation());
2978 occluded_tile_count++;
2981 switch (i) {
2982 case 0:
2983 EXPECT_EQ(occluded_tile_count, 5);
2984 break;
2985 case 1:
2986 EXPECT_EQ(occluded_tile_count, 2);
2987 break;
2988 default:
2989 NOTREACHED();
2993 // Full occlusion.
2994 layer1->SetPosition(gfx::PointF(0, 0));
2996 host_impl_.pending_tree()->UpdateDrawProperties();
2998 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
2999 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3001 occluded_tile_count = 0;
3002 for (PictureLayerTiling::CoverageIterator iter(
3003 tiling,
3004 pending_layer_->contents_scale_x(),
3005 gfx::Rect(layer_bounds));
3006 iter;
3007 ++iter) {
3008 if (!*iter)
3009 continue;
3010 const Tile* tile = *iter;
3012 if (tile->is_occluded(PENDING_TREE)) {
3013 EXPECT_FALSE(tile->required_for_activation());
3014 occluded_tile_count++;
3017 switch (i) {
3018 case 0:
3019 EXPECT_EQ(occluded_tile_count, 25);
3020 break;
3021 case 1:
3022 EXPECT_EQ(occluded_tile_count, 4);
3023 break;
3024 default:
3025 NOTREACHED();
3030 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3031 gfx::Size tile_size(102, 102);
3032 gfx::Size layer_bounds(1000, 1000);
3033 gfx::Size viewport_size(500, 500);
3034 gfx::Point occluding_layer_position(310, 0);
3036 scoped_refptr<FakePicturePileImpl> pending_pile =
3037 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3038 SetupPendingTree(pending_pile);
3039 pending_layer_->set_fixed_tile_size(tile_size);
3041 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3043 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3045 std::vector<PictureLayerTiling*> tilings;
3046 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3047 tilings.push_back(pending_layer_->AddTiling(0.3f));
3048 tilings.push_back(pending_layer_->AddTiling(0.7f));
3049 tilings.push_back(pending_layer_->AddTiling(1.0f));
3050 tilings.push_back(pending_layer_->AddTiling(2.0f));
3052 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3053 LayerImpl* layer1 = pending_layer_->children()[0];
3054 layer1->SetBounds(layer_bounds);
3055 layer1->SetContentBounds(layer_bounds);
3056 layer1->SetDrawsContent(true);
3057 layer1->SetContentsOpaque(true);
3058 layer1->SetPosition(occluding_layer_position);
3060 host_impl_.SetViewportSize(viewport_size);
3061 host_impl_.pending_tree()->UpdateDrawProperties();
3063 int tiling_count = 0;
3064 int occluded_tile_count = 0;
3065 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3066 tilings.begin();
3067 tiling_iterator != tilings.end();
3068 ++tiling_iterator) {
3069 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3071 occluded_tile_count = 0;
3072 for (size_t i = 0; i < tiles.size(); ++i) {
3073 if (tiles[i]->is_occluded(PENDING_TREE)) {
3074 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3075 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
3076 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
3077 occluded_tile_count++;
3080 switch (tiling_count) {
3081 case 0:
3082 case 1:
3083 EXPECT_EQ(occluded_tile_count, 2);
3084 break;
3085 case 2:
3086 EXPECT_EQ(occluded_tile_count, 4);
3087 break;
3088 case 3:
3089 EXPECT_EQ(occluded_tile_count, 5);
3090 break;
3091 case 4:
3092 EXPECT_EQ(occluded_tile_count, 30);
3093 break;
3094 default:
3095 NOTREACHED();
3098 tiling_count++;
3101 EXPECT_EQ(tiling_count, 5);
3104 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
3105 gfx::Size tile_size(102, 102);
3106 gfx::Size layer_bounds(1000, 1000);
3107 gfx::Size viewport_size(1000, 1000);
3108 gfx::Point occluding_layer_position(310, 0);
3109 gfx::Rect invalidation_rect(230, 230, 102, 102);
3111 scoped_refptr<FakePicturePileImpl> pending_pile =
3112 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3113 scoped_refptr<FakePicturePileImpl> active_pile =
3114 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3115 SetupTrees(pending_pile, active_pile);
3117 // Partially occlude the active layer.
3118 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3119 LayerImpl* layer1 = active_layer_->children()[0];
3120 layer1->SetBounds(layer_bounds);
3121 layer1->SetContentBounds(layer_bounds);
3122 layer1->SetDrawsContent(true);
3123 layer1->SetContentsOpaque(true);
3124 layer1->SetPosition(occluding_layer_position);
3126 // Partially invalidate the pending layer.
3127 pending_layer_->set_invalidation(invalidation_rect);
3129 host_impl_.SetViewportSize(viewport_size);
3131 active_layer_->CreateDefaultTilingsAndTiles();
3132 pending_layer_->CreateDefaultTilingsAndTiles();
3134 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3135 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3137 for (PictureLayerTiling::CoverageIterator iter(
3138 tiling,
3139 pending_layer_->contents_scale_x(),
3140 gfx::Rect(layer_bounds));
3141 iter;
3142 ++iter) {
3143 if (!*iter)
3144 continue;
3145 const Tile* tile = *iter;
3147 // All tiles are unoccluded on the pending tree.
3148 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3150 Tile* twin_tile =
3151 pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3152 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3153 tile->content_rect(), 1.0f / tile->contents_scale());
3155 if (scaled_content_rect.Intersects(invalidation_rect)) {
3156 // Tiles inside the invalidation rect are only on the pending tree.
3157 EXPECT_NE(tile, twin_tile);
3159 // Unshared tiles should be unoccluded on the active tree by default.
3160 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
3161 } else {
3162 // Tiles outside the invalidation rect are shared between both trees.
3163 EXPECT_EQ(tile, twin_tile);
3164 // Shared tiles are occluded on the active tree iff they lie beneath the
3165 // occluding layer.
3166 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3167 scaled_content_rect.x() >= occluding_layer_position.x());
3172 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
3173 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
3175 for (PictureLayerTiling::CoverageIterator iter(
3176 tiling,
3177 active_layer_->contents_scale_x(),
3178 gfx::Rect(layer_bounds));
3179 iter;
3180 ++iter) {
3181 if (!*iter)
3182 continue;
3183 const Tile* tile = *iter;
3185 Tile* twin_tile =
3186 active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3187 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3188 tile->content_rect(), 1.0f / tile->contents_scale());
3190 // Since we've already checked the shared tiles, only consider tiles in
3191 // the invalidation rect.
3192 if (scaled_content_rect.Intersects(invalidation_rect)) {
3193 // Tiles inside the invalidation rect are only on the active tree.
3194 EXPECT_NE(tile, twin_tile);
3196 // Unshared tiles should be unoccluded on the pending tree by default.
3197 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3199 // Unshared tiles are occluded on the active tree iff they lie beneath
3200 // the occluding layer.
3201 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3202 scaled_content_rect.x() >= occluding_layer_position.x());
3207 } // namespace
3208 } // namespace cc