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"
12 #include "cc/base/math_util.h"
13 #include "cc/layers/append_quads_data.h"
14 #include "cc/layers/picture_layer.h"
15 #include "cc/quads/draw_quad.h"
16 #include "cc/test/fake_content_layer_client.h"
17 #include "cc/test/fake_impl_proxy.h"
18 #include "cc/test/fake_layer_tree_host_impl.h"
19 #include "cc/test/fake_output_surface.h"
20 #include "cc/test/fake_picture_layer_impl.h"
21 #include "cc/test/fake_picture_pile_impl.h"
22 #include "cc/test/geometry_test_utils.h"
23 #include "cc/test/impl_side_painting_settings.h"
24 #include "cc/test/layer_test_common.h"
25 #include "cc/test/test_shared_bitmap_manager.h"
26 #include "cc/test/test_web_graphics_context_3d.h"
27 #include "cc/trees/layer_tree_impl.h"
28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "ui/gfx/rect_conversions.h"
30 #include "ui/gfx/size_conversions.h"
35 class MockCanvas : public SkCanvas {
37 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
39 virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE {
40 // Capture calls before SkCanvas quickReject() kicks in.
41 rects_.push_back(rect);
44 std::vector<SkRect> rects_;
47 class PictureLayerImplTest : public testing::Test {
49 PictureLayerImplTest()
50 : proxy_(base::MessageLoopProxy::current()),
51 host_impl_(ImplSidePaintingSettings(),
53 &shared_bitmap_manager_),
56 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
57 : proxy_(base::MessageLoopProxy::current()),
58 host_impl_(settings, &proxy_, &shared_bitmap_manager_),
61 virtual ~PictureLayerImplTest() {
64 virtual void SetUp() OVERRIDE {
68 virtual void InitializeRenderer() {
69 host_impl_.InitializeRenderer(
70 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
73 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
74 gfx::Size tile_size(100, 100);
76 scoped_refptr<FakePicturePileImpl> pending_pile =
77 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
78 scoped_refptr<FakePicturePileImpl> active_pile =
79 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
81 SetupTrees(pending_pile, active_pile);
85 host_impl_.ActivateSyncTree();
86 CHECK(!host_impl_.pending_tree());
87 pending_layer_ = NULL;
88 active_layer_ = static_cast<FakePictureLayerImpl*>(
89 host_impl_.active_tree()->LayerById(id_));
92 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
93 const gfx::Size& tile_size) {
94 SetupDefaultTrees(layer_bounds);
95 pending_layer_->set_fixed_tile_size(tile_size);
96 active_layer_->set_fixed_tile_size(tile_size);
100 scoped_refptr<PicturePileImpl> pending_pile,
101 scoped_refptr<PicturePileImpl> active_pile) {
102 SetupPendingTree(active_pile);
104 SetupPendingTree(pending_pile);
105 host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
106 host_impl_.active_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
109 void CreateHighLowResAndSetAllTilesVisible() {
110 // Active layer must get updated first so pending layer can share from it.
111 active_layer_->CreateDefaultTilingsAndTiles();
112 active_layer_->SetAllTilesVisible();
113 pending_layer_->CreateDefaultTilingsAndTiles();
114 pending_layer_->SetAllTilesVisible();
117 void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
118 active_layer_->AddTiling(2.3f);
119 active_layer_->AddTiling(1.0f);
120 active_layer_->AddTiling(0.5f);
121 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
122 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
123 pending_layer_->set_invalidation(invalidation);
124 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
125 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
128 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
129 host_impl_.CreatePendingTree();
130 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
131 // Clear recycled tree.
132 pending_tree->DetachLayerTree();
134 scoped_ptr<FakePictureLayerImpl> pending_layer =
135 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
136 pending_layer->SetDrawsContent(true);
137 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
139 pending_layer_ = static_cast<FakePictureLayerImpl*>(
140 host_impl_.pending_tree()->LayerById(id_));
141 pending_layer_->DoPostCommitInitializationIfNeeded();
144 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
145 float ideal_contents_scale,
146 float device_scale_factor,
147 float page_scale_factor,
148 float maximum_animation_contents_scale,
149 bool animating_transform_to_screen) {
150 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
151 layer->draw_properties().device_scale_factor = device_scale_factor;
152 layer->draw_properties().page_scale_factor = page_scale_factor;
153 layer->draw_properties().maximum_animation_contents_scale =
154 maximum_animation_contents_scale;
155 layer->draw_properties().screen_space_transform_is_animating =
156 animating_transform_to_screen;
157 layer->UpdateTiles(NULL);
159 static void VerifyAllTilesExistAndHavePile(
160 const PictureLayerTiling* tiling,
161 PicturePileImpl* pile) {
162 for (PictureLayerTiling::CoverageIterator iter(
164 tiling->contents_scale(),
165 gfx::Rect(tiling->tiling_size()));
169 EXPECT_EQ(pile, iter->picture_pile());
173 void SetContentsScaleOnBothLayers(float contents_scale,
174 float device_scale_factor,
175 float page_scale_factor,
176 float maximum_animation_contents_scale,
177 bool animating_transform) {
178 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
182 maximum_animation_contents_scale,
183 animating_transform);
185 SetupDrawPropertiesAndUpdateTiles(active_layer_,
189 maximum_animation_contents_scale,
190 animating_transform);
193 void ResetTilingsAndRasterScales() {
194 pending_layer_->ReleaseResources();
195 active_layer_->ReleaseResources();
198 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
199 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
200 for (size_t i = 0; i < tiles.size(); ++i)
201 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
202 EXPECT_GT(tiles.size(), 0u);
205 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
206 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
207 for (size_t i = 0; i < tiles.size(); ++i)
208 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
209 EXPECT_GT(tiles.size(), 0u);
213 void TestTileGridAlignmentCommon() {
214 // Layer to span 4 raster tiles in x and in y
215 ImplSidePaintingSettings settings;
216 gfx::Size layer_size(
217 settings.default_tile_size.width() * 7 / 2,
218 settings.default_tile_size.height() * 7 / 2);
220 scoped_refptr<FakePicturePileImpl> pending_pile =
221 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
222 scoped_refptr<FakePicturePileImpl> active_pile =
223 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
225 SetupTrees(pending_pile, active_pile);
227 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
229 // Add 1x1 rects at the centers of each tile, then re-record pile contents
230 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
231 std::vector<Tile*> tiles =
232 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
233 EXPECT_EQ(16u, tiles.size());
234 std::vector<SkRect> rects;
235 std::vector<Tile*>::const_iterator tile_iter;
236 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
237 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
238 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
239 active_pile->add_draw_rect(rect);
240 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
242 // Force re-record with newly injected content
243 active_pile->RemoveRecordingAt(0, 0);
244 active_pile->AddRecordingAt(0, 0);
246 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
247 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
248 MockCanvas mock_canvas(1000, 1000);
249 active_pile->RasterDirect(
250 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
252 // This test verifies that when drawing the contents of a specific tile
253 // at content scale 1.0, the playback canvas never receives content from
254 // neighboring tiles which indicates that the tile grid embedded in
255 // SkPicture is perfectly aligned with the compositor's tiles.
256 EXPECT_EQ(1u, mock_canvas.rects_.size());
257 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
262 FakeImplProxy proxy_;
263 TestSharedBitmapManager shared_bitmap_manager_;
264 FakeLayerTreeHostImpl host_impl_;
266 FakePictureLayerImpl* pending_layer_;
267 FakePictureLayerImpl* active_layer_;
270 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
273 TEST_F(PictureLayerImplTest, TileGridAlignment) {
274 host_impl_.SetDeviceScaleFactor(1.f);
275 TestTileGridAlignmentCommon();
278 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
279 host_impl_.SetDeviceScaleFactor(2.f);
280 TestTileGridAlignmentCommon();
283 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
284 gfx::Size tile_size(100, 100);
285 gfx::Size layer_bounds(400, 400);
287 scoped_refptr<FakePicturePileImpl> pending_pile =
288 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
289 scoped_refptr<FakePicturePileImpl> active_pile =
290 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
292 SetupTrees(pending_pile, active_pile);
295 AddDefaultTilingsWithInvalidation(invalidation);
297 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
298 active_layer_->tilings()->num_tilings());
300 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
301 EXPECT_GT(tilings->num_tilings(), 0u);
302 for (size_t i = 0; i < tilings->num_tilings(); ++i)
303 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
306 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
307 base::TimeTicks time_ticks;
308 time_ticks += base::TimeDelta::FromMilliseconds(1);
309 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
310 gfx::Size tile_size(100, 100);
311 gfx::Size layer_bounds(400, 400);
313 scoped_refptr<FakePicturePileImpl> pending_pile =
314 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
315 scoped_refptr<FakePicturePileImpl> active_pile =
316 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
318 SetupTrees(pending_pile, active_pile);
321 AddDefaultTilingsWithInvalidation(invalidation);
322 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
324 time_ticks += base::TimeDelta::FromMilliseconds(200);
325 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
327 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
328 // identify transform for tile priority.
329 bool resourceless_software_draw = false;
330 gfx::Rect viewport = gfx::Rect(layer_bounds),
331 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
332 gfx::Transform transform, transform_for_tile_priority;
334 host_impl_.SetExternalDrawConstraints(transform,
337 viewport_rect_for_tile_priority,
338 transform_for_tile_priority,
339 resourceless_software_draw);
340 active_layer_->draw_properties().visible_content_rect = viewport;
341 active_layer_->draw_properties().screen_space_transform = transform;
342 active_layer_->UpdateTiles(NULL);
344 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
345 viewport_rect_for_tile_priority;
347 // Verify the viewport rect for tile priority is used in picture layer impl.
348 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
349 viewport_rect_for_tile_priority_in_view_space);
351 // Verify the viewport rect for tile priority is used in picture layer tiling.
352 PictureLayerTilingSet* tilings = active_layer_->tilings();
353 for (size_t i = 0; i < tilings->num_tilings(); i++) {
354 PictureLayerTiling* tiling = tilings->tiling_at(i);
356 tiling->GetCurrentVisibleRectForTesting(),
357 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
358 tiling->contents_scale()));
361 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
362 // screen space and the transform for tile priority is translated and
363 // rotated. The actual viewport for tile priority used by PictureLayerImpl
364 // should be (200, 200, 100, 100) applied with the said transform.
365 time_ticks += base::TimeDelta::FromMilliseconds(200);
366 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
368 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
369 transform_for_tile_priority.Translate(100, 100);
370 transform_for_tile_priority.Rotate(45);
371 host_impl_.SetExternalDrawConstraints(transform,
374 viewport_rect_for_tile_priority,
375 transform_for_tile_priority,
376 resourceless_software_draw);
377 active_layer_->draw_properties().visible_content_rect = viewport;
378 active_layer_->draw_properties().screen_space_transform = transform;
379 active_layer_->UpdateTiles(NULL);
381 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
382 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
383 EXPECT_TRUE(success);
385 viewport_rect_for_tile_priority_in_view_space =
386 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
387 screen_to_view, viewport_rect_for_tile_priority));
389 // Verify the viewport rect for tile priority is used in PictureLayerImpl.
390 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
391 viewport_rect_for_tile_priority_in_view_space);
393 // Interset viewport_rect_for_tile_priority_in_view_space with the layer
394 // bounds and the result should be used in PictureLayerTiling.
395 viewport_rect_for_tile_priority_in_view_space.Intersect(
396 gfx::Rect(layer_bounds));
397 tilings = active_layer_->tilings();
398 for (size_t i = 0; i < tilings->num_tilings(); i++) {
399 PictureLayerTiling* tiling = tilings->tiling_at(i);
401 tiling->GetCurrentVisibleRectForTesting(),
402 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
403 tiling->contents_scale()));
407 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
408 base::TimeTicks time_ticks;
409 time_ticks += base::TimeDelta::FromMilliseconds(1);
410 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
412 gfx::Size tile_size(100, 100);
413 gfx::Size layer_bounds(400, 400);
415 scoped_refptr<FakePicturePileImpl> pending_pile =
416 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
417 scoped_refptr<FakePicturePileImpl> active_pile =
418 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
420 SetupTrees(pending_pile, active_pile);
423 AddDefaultTilingsWithInvalidation(invalidation);
424 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
426 // UpdateTiles with valid viewport. Should update tile viewport.
427 // Note viewport is considered invalid if and only if in resourceless
429 bool resourceless_software_draw = false;
430 gfx::Rect viewport = gfx::Rect(layer_bounds);
431 gfx::Transform transform;
432 host_impl_.SetExternalDrawConstraints(transform,
437 resourceless_software_draw);
438 active_layer_->draw_properties().visible_content_rect = viewport;
439 active_layer_->draw_properties().screen_space_transform = transform;
440 active_layer_->UpdateTiles(NULL);
442 gfx::Rect visible_rect_for_tile_priority =
443 active_layer_->visible_rect_for_tile_priority();
444 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
445 gfx::Rect viewport_rect_for_tile_priority =
446 active_layer_->viewport_rect_for_tile_priority();
447 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
448 gfx::Transform screen_space_transform_for_tile_priority =
449 active_layer_->screen_space_transform_for_tile_priority();
451 // Expand viewport and set it as invalid for prioritizing tiles.
452 // Should not update tile viewport.
453 time_ticks += base::TimeDelta::FromMilliseconds(200);
454 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
455 resourceless_software_draw = true;
456 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
457 transform.Translate(1.f, 1.f);
458 active_layer_->draw_properties().visible_content_rect = viewport;
459 active_layer_->draw_properties().screen_space_transform = transform;
460 host_impl_.SetExternalDrawConstraints(transform,
465 resourceless_software_draw);
466 active_layer_->UpdateTiles(NULL);
468 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
469 active_layer_->visible_rect_for_tile_priority());
470 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
471 active_layer_->viewport_rect_for_tile_priority());
472 EXPECT_TRANSFORMATION_MATRIX_EQ(
473 screen_space_transform_for_tile_priority,
474 active_layer_->screen_space_transform_for_tile_priority());
476 // Keep expanded viewport but mark it valid. Should update tile viewport.
477 time_ticks += base::TimeDelta::FromMilliseconds(200);
478 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
479 resourceless_software_draw = false;
480 host_impl_.SetExternalDrawConstraints(transform,
485 resourceless_software_draw);
486 active_layer_->UpdateTiles(NULL);
488 EXPECT_FALSE(visible_rect_for_tile_priority ==
489 active_layer_->visible_rect_for_tile_priority());
490 EXPECT_FALSE(viewport_rect_for_tile_priority ==
491 active_layer_->viewport_rect_for_tile_priority());
492 EXPECT_FALSE(screen_space_transform_for_tile_priority ==
493 active_layer_->screen_space_transform_for_tile_priority());
496 TEST_F(PictureLayerImplTest, InvalidViewportAfterReleaseResources) {
497 gfx::Size tile_size(100, 100);
498 gfx::Size layer_bounds(400, 400);
500 scoped_refptr<FakePicturePileImpl> pending_pile =
501 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
502 scoped_refptr<FakePicturePileImpl> active_pile =
503 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
505 SetupTrees(pending_pile, active_pile);
508 AddDefaultTilingsWithInvalidation(invalidation);
510 bool resourceless_software_draw = true;
511 gfx::Rect viewport = gfx::Rect(layer_bounds);
512 gfx::Transform identity = gfx::Transform();
513 host_impl_.SetExternalDrawConstraints(identity,
518 resourceless_software_draw);
519 ResetTilingsAndRasterScales();
520 host_impl_.pending_tree()->UpdateDrawProperties();
521 host_impl_.active_tree()->UpdateDrawProperties();
522 EXPECT_TRUE(active_layer_->HighResTiling());
524 size_t num_tilings = active_layer_->num_tilings();
525 active_layer_->UpdateTiles(NULL);
526 pending_layer_->AddTiling(0.5f);
527 EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
530 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
531 gfx::Size tile_size(100, 100);
532 gfx::Size layer_bounds(400, 400);
533 gfx::Rect layer_invalidation(150, 200, 30, 180);
535 scoped_refptr<FakePicturePileImpl> pending_pile =
536 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
537 scoped_refptr<FakePicturePileImpl> active_pile =
538 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
540 SetupTrees(pending_pile, active_pile);
542 Region invalidation(layer_invalidation);
543 AddDefaultTilingsWithInvalidation(invalidation);
545 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
546 EXPECT_GT(tilings->num_tilings(), 0u);
547 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
548 const PictureLayerTiling* tiling = tilings->tiling_at(i);
549 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
551 tiling->contents_scale());
552 for (PictureLayerTiling::CoverageIterator iter(
554 tiling->contents_scale(),
555 gfx::Rect(tiling->tiling_size()));
559 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
560 if (iter.geometry_rect().Intersects(content_invalidation))
561 EXPECT_EQ(pending_pile, iter->picture_pile());
563 EXPECT_EQ(active_pile, iter->picture_pile());
568 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
569 gfx::Size tile_size(90, 80);
570 gfx::Size layer_bounds(300, 500);
572 scoped_refptr<FakePicturePileImpl> pending_pile =
573 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
574 scoped_refptr<FakePicturePileImpl> active_pile =
575 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
577 SetupTrees(pending_pile, active_pile);
579 Region invalidation((gfx::Rect(layer_bounds)));
580 AddDefaultTilingsWithInvalidation(invalidation);
582 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
583 active_layer_->tilings()->num_tilings());
585 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
586 EXPECT_GT(tilings->num_tilings(), 0u);
587 for (size_t i = 0; i < tilings->num_tilings(); ++i)
588 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
591 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
592 gfx::Size tile_size(90, 80);
593 gfx::Size active_layer_bounds(300, 500);
594 gfx::Size pending_layer_bounds(400, 800);
596 scoped_refptr<FakePicturePileImpl> pending_pile =
597 FakePicturePileImpl::CreateFilledPile(tile_size,
598 pending_layer_bounds);
599 scoped_refptr<FakePicturePileImpl> active_pile =
600 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
602 SetupTrees(pending_pile, active_pile);
603 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
606 AddDefaultTilingsWithInvalidation(invalidation);
608 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
609 EXPECT_GT(tilings->num_tilings(), 0u);
610 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
611 const PictureLayerTiling* tiling = tilings->tiling_at(i);
612 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
613 gfx::Rect(active_layer_bounds),
614 tiling->contents_scale());
615 for (PictureLayerTiling::CoverageIterator iter(
617 tiling->contents_scale(),
618 gfx::Rect(tiling->tiling_size()));
622 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
623 std::vector<Tile*> active_tiles =
624 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
625 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
626 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
627 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
628 active_tiles[0]->content_rect().size() !=
629 pending_tiles[0]->content_rect().size()) {
630 EXPECT_EQ(pending_pile, iter->picture_pile());
632 EXPECT_EQ(active_pile, iter->picture_pile());
638 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
639 gfx::Size tile_size(400, 400);
640 gfx::Size layer_bounds(1300, 1900);
642 scoped_refptr<FakePicturePileImpl> pending_pile =
643 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
644 scoped_refptr<FakePicturePileImpl> active_pile =
645 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
647 // Fill in some of active pile, but more of pending pile.
649 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
650 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
652 pending_pile->AddRecordingAt(x, y);
653 active_pile->AddRecordingAt(x, y);
657 pending_pile->AddRecordingAt(x, y);
662 SetupTrees(pending_pile, active_pile);
664 AddDefaultTilingsWithInvalidation(invalidation);
666 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
667 EXPECT_GT(tilings->num_tilings(), 0u);
668 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
669 const PictureLayerTiling* tiling = tilings->tiling_at(i);
671 for (PictureLayerTiling::CoverageIterator iter(
673 tiling->contents_scale(),
674 gfx::Rect(tiling->tiling_size()));
677 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
678 // Ensure there is a recording for this tile.
679 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
680 iter.full_tile_geometry_rect());
681 bool in_active = active_pile->CanRaster(tiling->contents_scale(),
682 iter.full_tile_geometry_rect());
684 if (in_pending && !in_active)
685 EXPECT_EQ(pending_pile, iter->picture_pile());
687 EXPECT_EQ(active_pile, iter->picture_pile());
694 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
695 gfx::Size tile_size(400, 400);
696 gfx::Size layer_bounds(1300, 1900);
698 scoped_refptr<FakePicturePileImpl> pending_pile =
699 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
700 scoped_refptr<FakePicturePileImpl> active_pile =
701 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
703 SetupTrees(pending_pile, active_pile);
705 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
707 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
710 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
711 gfx::Size tile_size(400, 400);
712 gfx::Size layer_bounds(1300, 1900);
714 scoped_refptr<FakePicturePileImpl> pending_pile =
715 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
716 scoped_refptr<FakePicturePileImpl> active_pile =
717 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
719 SetupTrees(pending_pile, active_pile);
720 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
722 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
723 EXPECT_LT(low_res_factor, 1.f);
725 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
726 6.f, // ideal contents scale
729 1.f, // maximum animation scale
731 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
733 pending_layer_->tilings()->tiling_at(0)->contents_scale());
734 EXPECT_FLOAT_EQ(6.f * low_res_factor,
735 pending_layer_->tilings()->tiling_at(1)->contents_scale());
737 // If we change the page scale factor, then we should get new tilings.
738 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
739 6.6f, // ideal contents scale
742 1.f, // maximum animation scale
744 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
745 EXPECT_FLOAT_EQ(6.6f,
746 pending_layer_->tilings()->tiling_at(0)->contents_scale());
747 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
748 pending_layer_->tilings()->tiling_at(2)->contents_scale());
750 // If we change the device scale factor, then we should get new tilings.
751 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
752 7.26f, // ideal contents scale
753 3.3f, // device scale
755 1.f, // maximum animation scale
757 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
758 EXPECT_FLOAT_EQ(7.26f,
759 pending_layer_->tilings()->tiling_at(0)->contents_scale());
760 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
761 pending_layer_->tilings()->tiling_at(3)->contents_scale());
763 // If we change the device scale factor, but end up at the same total scale
764 // factor somehow, then we don't get new tilings.
765 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
766 7.26f, // ideal contents scale
767 2.2f, // device scale
769 1.f, // maximum animation scale
771 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
772 EXPECT_FLOAT_EQ(7.26f,
773 pending_layer_->tilings()->tiling_at(0)->contents_scale());
774 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
775 pending_layer_->tilings()->tiling_at(3)->contents_scale());
778 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
779 // This test makes sure that if a layer can have tilings, then a commit makes
780 // it not able to have tilings (empty size), and then a future commit that
781 // makes it valid again should be able to create tilings.
782 gfx::Size tile_size(400, 400);
783 gfx::Size layer_bounds(1300, 1900);
785 scoped_refptr<FakePicturePileImpl> empty_pile =
786 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
787 scoped_refptr<FakePicturePileImpl> valid_pile =
788 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
790 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
791 EXPECT_LT(low_res_factor, 1.f);
793 float high_res_scale = 1.3f;
794 float low_res_scale = high_res_scale * low_res_factor;
795 float device_scale = 1.7f;
796 float page_scale = 3.2f;
797 float maximum_animation_scale = 1.f;
799 SetupPendingTree(valid_pile);
800 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
804 maximum_animation_scale,
806 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
807 EXPECT_FLOAT_EQ(high_res_scale,
808 pending_layer_->HighResTiling()->contents_scale());
809 EXPECT_FLOAT_EQ(low_res_scale,
810 pending_layer_->LowResTiling()->contents_scale());
813 SetupPendingTree(empty_pile);
814 EXPECT_FALSE(pending_layer_->CanHaveTilings());
815 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
819 maximum_animation_scale,
821 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
822 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
825 EXPECT_FALSE(active_layer_->CanHaveTilings());
826 SetupDrawPropertiesAndUpdateTiles(active_layer_,
830 maximum_animation_scale,
832 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
834 SetupPendingTree(valid_pile);
835 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
839 maximum_animation_scale,
841 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
842 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
843 EXPECT_FLOAT_EQ(high_res_scale,
844 pending_layer_->HighResTiling()->contents_scale());
845 EXPECT_FLOAT_EQ(low_res_scale,
846 pending_layer_->LowResTiling()->contents_scale());
849 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
850 gfx::Size tile_size(400, 400);
851 gfx::Size layer_bounds(1300, 1900);
853 // Set up the high and low res tilings before pinch zoom.
854 scoped_refptr<FakePicturePileImpl> pending_pile =
855 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
856 scoped_refptr<FakePicturePileImpl> active_pile =
857 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
859 SetupTrees(pending_pile, active_pile);
860 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
861 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
862 host_impl_.PinchGestureBegin();
863 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
864 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
865 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
868 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
869 gfx::Size tile_size(400, 400);
870 gfx::Size layer_bounds(1300, 1900);
872 scoped_refptr<FakePicturePileImpl> pending_pile =
873 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
874 scoped_refptr<FakePicturePileImpl> active_pile =
875 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
877 // Set up the high and low res tilings before pinch zoom.
878 SetupTrees(pending_pile, active_pile);
879 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
880 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
881 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
882 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
883 EXPECT_FLOAT_EQ(2.0f,
884 active_layer_->tilings()->tiling_at(0)->contents_scale());
885 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
886 active_layer_->tilings()->tiling_at(1)->contents_scale());
888 // Start a pinch gesture.
889 host_impl_.PinchGestureBegin();
891 // Zoom out by a small amount. We should create a tiling at half
892 // the scale (2/kMaxScaleRatioDuringPinch).
893 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
894 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
895 EXPECT_FLOAT_EQ(2.0f,
896 active_layer_->tilings()->tiling_at(0)->contents_scale());
897 EXPECT_FLOAT_EQ(1.0f,
898 active_layer_->tilings()->tiling_at(1)->contents_scale());
899 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
900 active_layer_->tilings()->tiling_at(2)->contents_scale());
902 // Zoom out further, close to our low-res scale factor. We should
903 // use that tiling as high-res, and not create a new tiling.
904 SetContentsScaleOnBothLayers(
905 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
906 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
908 // Zoom in a lot now. Since we increase by increments of
909 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
910 // and then finally create a new tiling at 4.0.
911 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
912 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
913 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
914 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
915 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
916 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
917 EXPECT_FLOAT_EQ(4.0f,
918 active_layer_->tilings()->tiling_at(0)->contents_scale());
921 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
922 gfx::Size tile_size(300, 300);
923 gfx::Size layer_bounds(2600, 3800);
925 scoped_refptr<FakePicturePileImpl> pending_pile =
926 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
927 scoped_refptr<FakePicturePileImpl> active_pile =
928 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
930 // Set up the high and low res tilings before pinch zoom.
931 SetupTrees(pending_pile, active_pile);
932 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
933 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
934 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
935 EXPECT_FLOAT_EQ(0.24f,
936 active_layer_->tilings()->tiling_at(0)->contents_scale());
937 EXPECT_FLOAT_EQ(0.0625f,
938 active_layer_->tilings()->tiling_at(1)->contents_scale());
940 // Start a pinch gesture.
941 host_impl_.PinchGestureBegin();
943 // Zoom out by a small amount. We should create a tiling at half
944 // the scale (1/kMaxScaleRatioDuringPinch).
945 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
946 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
947 EXPECT_FLOAT_EQ(0.24f,
948 active_layer_->tilings()->tiling_at(0)->contents_scale());
949 EXPECT_FLOAT_EQ(0.12f,
950 active_layer_->tilings()->tiling_at(1)->contents_scale());
951 EXPECT_FLOAT_EQ(0.0625,
952 active_layer_->tilings()->tiling_at(2)->contents_scale());
954 // Zoom out further, close to our low-res scale factor. We should
955 // use that tiling as high-res, and not create a new tiling.
956 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
957 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
959 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
960 // because 0.125(desired_scale) is within the ratio(1.2)
961 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
962 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
965 TEST_F(PictureLayerImplTest, CleanUpTilings) {
966 gfx::Size tile_size(400, 400);
967 gfx::Size layer_bounds(1300, 1900);
969 scoped_refptr<FakePicturePileImpl> pending_pile =
970 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
971 scoped_refptr<FakePicturePileImpl> active_pile =
972 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
974 std::vector<PictureLayerTiling*> used_tilings;
976 SetupTrees(pending_pile, active_pile);
977 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
979 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
980 EXPECT_LT(low_res_factor, 1.f);
982 float device_scale = 1.7f;
983 float page_scale = 3.2f;
986 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
987 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
989 // We only have ideal tilings, so they aren't removed.
990 used_tilings.clear();
991 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
992 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
994 host_impl_.PinchGestureBegin();
996 // Changing the ideal but not creating new tilings.
999 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
1000 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1002 // The tilings are still our target scale, so they aren't removed.
1003 used_tilings.clear();
1004 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1005 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1007 host_impl_.PinchGestureEnd();
1009 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
1012 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
1013 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1016 active_layer_->tilings()->tiling_at(1)->contents_scale());
1018 1.f * low_res_factor,
1019 active_layer_->tilings()->tiling_at(3)->contents_scale());
1021 // Mark the non-ideal tilings as used. They won't be removed.
1022 used_tilings.clear();
1023 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1024 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1025 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1026 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1028 // Now move the ideal scale to 0.5. Our target stays 1.2.
1029 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1031 // The high resolution tiling is between target and ideal, so is not
1032 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1033 used_tilings.clear();
1034 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1035 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1037 // Now move the ideal scale to 1.0. Our target stays 1.2.
1038 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1040 // All the tilings are between are target and the ideal, so they are not
1042 used_tilings.clear();
1043 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1044 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1046 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1047 SetupDrawPropertiesAndUpdateTiles(
1048 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1050 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1051 // in the range [1.0,1.2] and are kept.
1052 used_tilings.clear();
1053 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1054 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1056 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1058 SetupDrawPropertiesAndUpdateTiles(
1059 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1061 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1062 // target raster scale. But it is in our used tilings set, so nothing is
1064 used_tilings.clear();
1065 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1066 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1067 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1069 // If we remove it from our used tilings set, it is outside the range to keep
1070 // so it is deleted.
1071 used_tilings.clear();
1072 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1073 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1076 #define EXPECT_BOTH_EQ(expression, x) \
1078 EXPECT_EQ(x, pending_layer_->expression); \
1079 EXPECT_EQ(x, active_layer_->expression); \
1082 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1083 // Make sure this layer covers multiple tiles, since otherwise low
1084 // res won't get created because it is too small.
1085 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1086 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1087 // Avoid max untiled layer size heuristics via fixed tile size.
1088 pending_layer_->set_fixed_tile_size(tile_size);
1089 active_layer_->set_fixed_tile_size(tile_size);
1091 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1092 float contents_scale = 1.f;
1093 float device_scale = 1.f;
1094 float page_scale = 1.f;
1095 float maximum_animation_scale = 1.f;
1096 bool animating_transform = true;
1098 // Animating, so don't create low res even if there isn't one already.
1099 SetContentsScaleOnBothLayers(contents_scale,
1102 maximum_animation_scale,
1103 animating_transform);
1104 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1105 EXPECT_BOTH_EQ(num_tilings(), 1u);
1107 // Stop animating, low res gets created.
1108 animating_transform = false;
1109 SetContentsScaleOnBothLayers(contents_scale,
1112 maximum_animation_scale,
1113 animating_transform);
1114 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1115 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1116 EXPECT_BOTH_EQ(num_tilings(), 2u);
1118 // Page scale animation, new high res, but not new low res because animating.
1119 contents_scale = 2.f;
1121 animating_transform = true;
1122 SetContentsScaleOnBothLayers(contents_scale,
1125 maximum_animation_scale,
1126 animating_transform);
1127 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1128 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1129 EXPECT_BOTH_EQ(num_tilings(), 3u);
1131 // Stop animating, new low res gets created for final page scale.
1132 animating_transform = false;
1133 SetContentsScaleOnBothLayers(contents_scale,
1136 maximum_animation_scale,
1137 animating_transform);
1138 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1139 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1140 EXPECT_BOTH_EQ(num_tilings(), 4u);
1143 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1144 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1145 SetupDefaultTrees(tile_size);
1147 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1148 float device_scale = 1.f;
1149 float page_scale = 1.f;
1150 float maximum_animation_scale = 1.f;
1151 bool animating_transform = false;
1153 // Contents exactly fit on one tile at scale 1, no low res.
1154 float contents_scale = 1.f;
1155 SetContentsScaleOnBothLayers(contents_scale,
1158 maximum_animation_scale,
1159 animating_transform);
1160 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1161 EXPECT_BOTH_EQ(num_tilings(), 1u);
1163 ResetTilingsAndRasterScales();
1165 // Contents that are smaller than one tile, no low res.
1166 contents_scale = 0.123f;
1167 SetContentsScaleOnBothLayers(contents_scale,
1170 maximum_animation_scale,
1171 animating_transform);
1172 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1173 EXPECT_BOTH_EQ(num_tilings(), 1u);
1175 ResetTilingsAndRasterScales();
1177 // Any content bounds that would create more than one tile will
1178 // generate a low res tiling.
1179 contents_scale = 2.5f;
1180 SetContentsScaleOnBothLayers(contents_scale,
1183 maximum_animation_scale,
1184 animating_transform);
1185 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1186 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1187 contents_scale * low_res_factor);
1188 EXPECT_BOTH_EQ(num_tilings(), 2u);
1190 ResetTilingsAndRasterScales();
1192 // Mask layers dont create low res since they always fit on one tile.
1193 pending_layer_->SetIsMask(true);
1194 active_layer_->SetIsMask(true);
1195 SetContentsScaleOnBothLayers(contents_scale,
1198 maximum_animation_scale,
1199 animating_transform);
1200 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1201 EXPECT_BOTH_EQ(num_tilings(), 1u);
1204 TEST_F(PictureLayerImplTest, ReleaseResources) {
1205 gfx::Size tile_size(400, 400);
1206 gfx::Size layer_bounds(1300, 1900);
1208 scoped_refptr<FakePicturePileImpl> pending_pile =
1209 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1210 scoped_refptr<FakePicturePileImpl> active_pile =
1211 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1213 SetupTrees(pending_pile, active_pile);
1214 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1216 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1217 1.3f, // ideal contents scale
1218 2.7f, // device scale
1220 1.f, // maximum animation scale
1222 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1224 // All tilings should be removed when losing output surface.
1225 active_layer_->ReleaseResources();
1226 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1227 pending_layer_->ReleaseResources();
1228 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1230 // This should create new tilings.
1231 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1232 1.3f, // ideal contents scale
1233 2.7f, // device scale
1235 1.f, // maximum animation scale
1237 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1240 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1241 // The default max tile size is larger than 400x400.
1242 gfx::Size tile_size(400, 400);
1243 gfx::Size layer_bounds(5000, 5000);
1245 scoped_refptr<FakePicturePileImpl> pending_pile =
1246 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1247 scoped_refptr<FakePicturePileImpl> active_pile =
1248 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1250 SetupTrees(pending_pile, active_pile);
1251 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1253 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1254 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1256 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1258 // The default value.
1259 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1260 host_impl_.settings().default_tile_size.ToString());
1262 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1263 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1264 tile->content_rect().size().ToString());
1266 pending_layer_->ReleaseResources();
1268 // Change the max texture size on the output surface context.
1269 scoped_ptr<TestWebGraphicsContext3D> context =
1270 TestWebGraphicsContext3D::Create();
1271 context->set_max_texture_size(140);
1272 host_impl_.DidLoseOutputSurface();
1273 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1274 context.Pass()).PassAs<OutputSurface>());
1276 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1277 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1279 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1281 // Verify the tiles are not larger than the context's max texture size.
1282 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1283 EXPECT_GE(140, tile->content_rect().width());
1284 EXPECT_GE(140, tile->content_rect().height());
1287 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1288 // The default max tile size is larger than 400x400.
1289 gfx::Size tile_size(400, 400);
1290 gfx::Size layer_bounds(500, 500);
1292 scoped_refptr<FakePicturePileImpl> pending_pile =
1293 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1294 scoped_refptr<FakePicturePileImpl> active_pile =
1295 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1297 SetupTrees(pending_pile, active_pile);
1298 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1300 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1301 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1303 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1305 // The default value. The layer is smaller than this.
1306 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1307 host_impl_.settings().max_untiled_layer_size.ToString());
1309 // There should be a single tile since the layer is small.
1310 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1311 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1313 pending_layer_->ReleaseResources();
1315 // Change the max texture size on the output surface context.
1316 scoped_ptr<TestWebGraphicsContext3D> context =
1317 TestWebGraphicsContext3D::Create();
1318 context->set_max_texture_size(140);
1319 host_impl_.DidLoseOutputSurface();
1320 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1321 context.Pass()).PassAs<OutputSurface>());
1323 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1324 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1326 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1328 // There should be more than one tile since the max texture size won't cover
1330 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1331 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1333 // Verify the tiles are not larger than the context's max texture size.
1334 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1335 EXPECT_GE(140, tile->content_rect().width());
1336 EXPECT_GE(140, tile->content_rect().height());
1339 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1340 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1341 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1343 gfx::Size tile_size(400, 400);
1344 gfx::Size layer_bounds(1300, 1900);
1346 scoped_refptr<FakePicturePileImpl> pending_pile =
1347 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1348 scoped_refptr<FakePicturePileImpl> active_pile =
1349 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1351 SetupTrees(pending_pile, active_pile);
1353 active_layer_->draw_properties().visible_content_rect =
1354 gfx::Rect(layer_bounds);
1356 gfx::Rect layer_invalidation(150, 200, 30, 180);
1357 Region invalidation(layer_invalidation);
1358 AddDefaultTilingsWithInvalidation(invalidation);
1360 AppendQuadsData data;
1361 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1362 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1363 active_layer_->DidDraw(NULL);
1365 ASSERT_EQ(1U, render_pass->quad_list.size());
1366 EXPECT_EQ(DrawQuad::PICTURE_CONTENT, render_pass->quad_list[0]->material);
1369 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1370 gfx::Size tile_size(100, 100);
1371 gfx::Size layer_bounds(1000, 1000);
1373 scoped_refptr<FakePicturePileImpl> pending_pile =
1374 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1375 // Layers with entirely empty piles can't get tilings.
1376 pending_pile->AddRecordingAt(0, 0);
1378 SetupPendingTree(pending_pile);
1380 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1381 pending_layer_->AddTiling(1.0f);
1382 pending_layer_->AddTiling(2.0f);
1384 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1385 // on a layer with no recordings.
1386 host_impl_.pending_tree()->UpdateDrawProperties();
1387 pending_layer_->MarkVisibleResourcesAsRequired();
1390 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1391 gfx::Size tile_size(100, 100);
1392 gfx::Size layer_bounds(200, 200);
1394 scoped_refptr<FakePicturePileImpl> pending_pile =
1395 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1396 SetupPendingTree(pending_pile);
1398 pending_layer_->set_fixed_tile_size(tile_size);
1399 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1400 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1401 host_impl_.pending_tree()->UpdateDrawProperties();
1402 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1404 pending_layer_->draw_properties().visible_content_rect =
1405 gfx::Rect(0, 0, 100, 200);
1407 // Fake set priorities.
1408 for (PictureLayerTiling::CoverageIterator iter(
1409 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1415 TilePriority priority;
1416 priority.resolution = HIGH_RESOLUTION;
1417 gfx::Rect tile_bounds = iter.geometry_rect();
1418 if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1419 priority.priority_bin = TilePriority::NOW;
1420 priority.distance_to_visible = 0.f;
1422 priority.priority_bin = TilePriority::SOON;
1423 priority.distance_to_visible = 1.f;
1425 tile->SetPriority(PENDING_TREE, priority);
1428 pending_layer_->MarkVisibleResourcesAsRequired();
1430 int num_visible = 0;
1431 int num_offscreen = 0;
1433 for (PictureLayerTiling::CoverageIterator iter(
1434 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1439 const Tile* tile = *iter;
1440 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1441 EXPECT_TRUE(tile->required_for_activation());
1444 EXPECT_FALSE(tile->required_for_activation());
1449 EXPECT_GT(num_visible, 0);
1450 EXPECT_GT(num_offscreen, 0);
1453 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1454 gfx::Size layer_bounds(400, 400);
1455 gfx::Size tile_size(100, 100);
1456 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1459 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1461 CreateHighLowResAndSetAllTilesVisible();
1463 active_layer_->SetAllTilesReady();
1465 // No shared tiles and all active tiles ready, so pending can only
1466 // activate with all high res tiles.
1467 pending_layer_->MarkVisibleResourcesAsRequired();
1468 AssertAllTilesRequired(pending_layer_->HighResTiling());
1469 AssertNoTilesRequired(pending_layer_->LowResTiling());
1472 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1473 gfx::Size layer_bounds(400, 400);
1474 gfx::Size tile_size(100, 100);
1475 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1477 // All tiles shared (no invalidation).
1478 CreateHighLowResAndSetAllTilesVisible();
1480 // Verify active tree not ready.
1481 Tile* some_active_tile =
1482 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1483 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1485 // When high res are required, even if the active tree is not ready,
1486 // the high res tiles must be ready.
1487 host_impl_.active_tree()->SetRequiresHighResToDraw();
1488 pending_layer_->MarkVisibleResourcesAsRequired();
1489 AssertAllTilesRequired(pending_layer_->HighResTiling());
1490 AssertNoTilesRequired(pending_layer_->LowResTiling());
1493 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1494 gfx::Size layer_bounds(400, 400);
1495 gfx::Size tile_size(100, 100);
1496 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1498 CreateHighLowResAndSetAllTilesVisible();
1500 Tile* some_active_tile =
1501 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1502 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1504 // All tiles shared (no invalidation), so even though the active tree's
1505 // tiles aren't ready, there is nothing required.
1506 pending_layer_->MarkVisibleResourcesAsRequired();
1507 AssertNoTilesRequired(pending_layer_->HighResTiling());
1508 AssertNoTilesRequired(pending_layer_->LowResTiling());
1511 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1512 gfx::Size layer_bounds(400, 400);
1513 gfx::Size tile_size(100, 100);
1514 scoped_refptr<FakePicturePileImpl> pending_pile =
1515 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1516 // This pile will create tilings, but has no recordings so will not create any
1517 // tiles. This is attempting to simulate scrolling past the end of recorded
1518 // content on the active layer, where the recordings are so far away that
1519 // no tiles are created.
1520 scoped_refptr<FakePicturePileImpl> active_pile =
1521 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1522 tile_size, layer_bounds);
1523 SetupTrees(pending_pile, active_pile);
1524 pending_layer_->set_fixed_tile_size(tile_size);
1525 active_layer_->set_fixed_tile_size(tile_size);
1527 CreateHighLowResAndSetAllTilesVisible();
1529 // Active layer has tilings, but no tiles due to missing recordings.
1530 EXPECT_TRUE(active_layer_->CanHaveTilings());
1531 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1532 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1534 // Since the active layer has no tiles at all, the pending layer doesn't
1535 // need content in order to activate.
1536 pending_layer_->MarkVisibleResourcesAsRequired();
1537 AssertNoTilesRequired(pending_layer_->HighResTiling());
1538 AssertNoTilesRequired(pending_layer_->LowResTiling());
1541 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1542 gfx::Size layer_bounds(400, 400);
1543 gfx::Size tile_size(100, 100);
1544 scoped_refptr<FakePicturePileImpl> pending_pile =
1545 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1546 scoped_refptr<FakePicturePileImpl> active_pile =
1547 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1548 SetupTrees(pending_pile, active_pile);
1549 pending_layer_->set_fixed_tile_size(tile_size);
1550 active_layer_->set_fixed_tile_size(tile_size);
1552 CreateHighLowResAndSetAllTilesVisible();
1554 // Active layer can't have tiles.
1555 EXPECT_FALSE(active_layer_->CanHaveTilings());
1557 // All high res tiles required. This should be considered identical
1558 // to the case where there is no active layer, to avoid flashing content.
1559 // This can happen if a layer exists for a while and switches from
1560 // not being able to have content to having content.
1561 pending_layer_->MarkVisibleResourcesAsRequired();
1562 AssertAllTilesRequired(pending_layer_->HighResTiling());
1563 AssertNoTilesRequired(pending_layer_->LowResTiling());
1566 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1567 gfx::Size layer_bounds(200, 200);
1568 gfx::Size tile_size(100, 100);
1569 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1571 gfx::Size pending_layer_bounds(400, 400);
1572 pending_layer_->SetBounds(pending_layer_bounds);
1574 CreateHighLowResAndSetAllTilesVisible();
1576 active_layer_->SetAllTilesReady();
1578 // Since the active layer has different bounds, the pending layer needs all
1579 // high res tiles in order to activate.
1580 pending_layer_->MarkVisibleResourcesAsRequired();
1581 AssertAllTilesRequired(pending_layer_->HighResTiling());
1582 AssertNoTilesRequired(pending_layer_->LowResTiling());
1585 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1586 gfx::Size tile_size(100, 100);
1587 gfx::Size layer_bounds(400, 400);
1588 scoped_refptr<FakePicturePileImpl> pending_pile =
1589 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1591 host_impl_.CreatePendingTree();
1592 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1594 scoped_ptr<FakePictureLayerImpl> pending_layer =
1595 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1596 pending_layer->SetDrawsContent(true);
1597 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1599 pending_layer_ = static_cast<FakePictureLayerImpl*>(
1600 host_impl_.pending_tree()->LayerById(id_));
1602 // Set some state on the pending layer, make sure it is not clobbered
1603 // by a sync from the active layer. This could happen because if the
1604 // pending layer has not been post-commit initialized it will attempt
1605 // to sync from the active layer.
1606 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1607 pending_layer_->set_raster_page_scale(raster_page_scale);
1608 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1610 host_impl_.ActivateSyncTree();
1612 active_layer_ = static_cast<FakePictureLayerImpl*>(
1613 host_impl_.active_tree()->LayerById(id_));
1615 EXPECT_EQ(0u, active_layer_->num_tilings());
1616 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1617 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1620 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1621 SetupDefaultTrees(gfx::Size(1500, 1500));
1622 AddDefaultTilingsWithInvalidation(gfx::Rect());
1624 host_impl_.ActivateSyncTree();
1625 host_impl_.CreatePendingTree();
1626 active_layer_ = static_cast<FakePictureLayerImpl*>(
1627 host_impl_.active_tree()->LayerById(id_));
1629 // Force the active tree to sync to the pending tree "post-commit".
1630 pending_layer_->DoPostCommitInitializationIfNeeded();
1632 // Both invalidations should drop tiles from the pending tree.
1633 EXPECT_EQ(3u, active_layer_->num_tilings());
1634 EXPECT_EQ(3u, pending_layer_->num_tilings());
1635 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1636 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1637 PictureLayerTiling* pending_tiling =
1638 pending_layer_->tilings()->tiling_at(i);
1640 ASSERT_TRUE(active_tiling);
1641 ASSERT_TRUE(pending_tiling);
1643 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1644 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1645 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1646 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1648 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1649 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1650 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1651 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1653 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1654 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1655 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1656 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1660 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1661 SetupDefaultTrees(gfx::Size(1500, 1500));
1662 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1664 // This activates the 0,0,1,1 invalidation.
1665 host_impl_.ActivateSyncTree();
1666 host_impl_.CreatePendingTree();
1667 active_layer_ = static_cast<FakePictureLayerImpl*>(
1668 host_impl_.active_tree()->LayerById(id_));
1670 // Force the active tree to sync to the pending tree "post-commit".
1671 pending_layer_->DoPostCommitInitializationIfNeeded();
1673 // The active tree invalidation was handled by the active tiles, so they
1674 // can be shared with the pending tree.
1675 EXPECT_EQ(3u, active_layer_->num_tilings());
1676 EXPECT_EQ(3u, pending_layer_->num_tilings());
1677 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1678 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1679 PictureLayerTiling* pending_tiling =
1680 pending_layer_->tilings()->tiling_at(i);
1682 ASSERT_TRUE(active_tiling);
1683 ASSERT_TRUE(pending_tiling);
1685 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1686 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1687 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1688 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1690 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1691 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1692 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1693 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1695 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1696 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1697 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1698 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1702 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
1703 SetupDefaultTrees(gfx::Size(1500, 1500));
1704 AddDefaultTilingsWithInvalidation(gfx::Rect());
1706 host_impl_.ActivateSyncTree();
1707 host_impl_.CreatePendingTree();
1708 active_layer_ = static_cast<FakePictureLayerImpl*>(
1709 host_impl_.active_tree()->LayerById(id_));
1711 // Set some invalidation on the pending tree "during commit". We should
1712 // replace raster tiles that touch this.
1713 pending_layer_->set_invalidation(gfx::Rect(1, 1));
1715 // Force the active tree to sync to the pending tree "post-commit".
1716 pending_layer_->DoPostCommitInitializationIfNeeded();
1718 // The pending tree invalidation means tiles can not be shared with the
1720 EXPECT_EQ(3u, active_layer_->num_tilings());
1721 EXPECT_EQ(3u, pending_layer_->num_tilings());
1722 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1723 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1724 PictureLayerTiling* pending_tiling =
1725 pending_layer_->tilings()->tiling_at(i);
1727 ASSERT_TRUE(active_tiling);
1728 ASSERT_TRUE(pending_tiling);
1730 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1731 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1732 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1733 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1735 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1736 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1737 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1738 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1740 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1741 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1742 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1743 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1747 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1748 SetupDefaultTrees(gfx::Size(10, 10));
1749 host_impl_.active_tree()->UpdateDrawProperties();
1750 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1752 // Contrived unit test of a real crash. A layer is transparent during a
1753 // context loss, and later becomes opaque, causing active layer SyncTiling to
1755 float new_scale = 1.f;
1756 active_layer_->ReleaseResources();
1757 pending_layer_->ReleaseResources();
1758 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
1759 pending_layer_->AddTiling(new_scale);
1760 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
1762 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
1763 // responsible for calling ManageTilings. These checks verify that
1764 // ReleaseResources has set needs update draw properties so that the
1765 // new tiling gets the appropriate resolution set in ManageTilings.
1766 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1767 host_impl_.active_tree()->UpdateDrawProperties();
1768 PictureLayerTiling* high_res =
1769 active_layer_->tilings()->TilingAtScale(new_scale);
1770 ASSERT_TRUE(!!high_res);
1771 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
1774 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
1775 SetupDefaultTrees(gfx::Size(10, 10));
1777 const float kScale = 1.f;
1778 pending_layer_->AddTiling(kScale);
1779 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1780 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1782 // Gpu rasterization is disabled by default.
1783 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1784 // Toggling the gpu rasterization clears all tilings on both trees.
1785 host_impl_.SetUseGpuRasterization(true);
1786 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1787 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1789 // Make sure that we can still add tiling to the pending layer,
1790 // that gets synced to the active layer.
1791 pending_layer_->AddTiling(kScale);
1792 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1793 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
1795 // Toggling the gpu rasterization clears all tilings on both trees.
1796 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1797 host_impl_.SetUseGpuRasterization(false);
1798 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1799 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1802 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
1803 SetupDefaultTrees(gfx::Size(10, 10));
1804 host_impl_.active_tree()->UpdateDrawProperties();
1805 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1807 SetupDrawPropertiesAndUpdateTiles(
1808 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
1809 active_layer_->tilings()->RemoveAllTilings();
1810 PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
1811 active_layer_->tilings()->AddTiling(1.5f);
1812 active_layer_->tilings()->AddTiling(0.25f);
1813 tiling->set_resolution(HIGH_RESOLUTION);
1816 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1817 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
1819 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
1820 // 1.0f). Note that we should also ensure that the pending layer needs post
1821 // commit initialization, since this is what would happen during commit. In
1822 // other words we want the pending layer to sync from the active layer.
1823 pending_layer_->SetBounds(gfx::Size(1, 1));
1824 pending_layer_->SetNeedsPostCommitInitialization();
1825 pending_layer_->set_twin_layer(NULL);
1826 active_layer_->set_twin_layer(NULL);
1827 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1829 // Update the draw properties: sync from active tree should happen here.
1830 host_impl_.pending_tree()->UpdateDrawProperties();
1832 // Another sanity check.
1833 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
1835 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
1836 // violate minimum contents scale. At the same time, we should've created a
1837 // new high res tiling at scale 1.0f.
1838 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1839 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
1840 EXPECT_EQ(HIGH_RESOLUTION,
1841 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
1842 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
1843 EXPECT_EQ(NON_IDEAL_RESOLUTION,
1844 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
1847 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
1848 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
1849 gfx::Size layer_bounds(default_tile_size.width() * 4,
1850 default_tile_size.height() * 4);
1852 SetupDefaultTrees(layer_bounds);
1853 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
1854 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1855 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1856 // Should have a low-res and a high-res tiling.
1857 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1859 ResetTilingsAndRasterScales();
1861 host_impl_.SetUseGpuRasterization(true);
1862 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
1863 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1865 // Should only have the high-res tiling.
1866 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
1869 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
1870 // Set up layers with tilings.
1871 SetupDefaultTrees(gfx::Size(10, 10));
1872 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
1873 pending_layer_->PushPropertiesTo(active_layer_);
1874 EXPECT_TRUE(pending_layer_->DrawsContent());
1875 EXPECT_TRUE(pending_layer_->CanHaveTilings());
1876 EXPECT_GE(pending_layer_->num_tilings(), 0u);
1877 EXPECT_GE(active_layer_->num_tilings(), 0u);
1879 // Set content to false, which should make CanHaveTilings return false.
1880 pending_layer_->SetDrawsContent(false);
1881 EXPECT_FALSE(pending_layer_->DrawsContent());
1882 EXPECT_FALSE(pending_layer_->CanHaveTilings());
1884 // No tilings should be pushed to active layer.
1885 pending_layer_->PushPropertiesTo(active_layer_);
1886 EXPECT_EQ(0u, active_layer_->num_tilings());
1889 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
1890 SetupDefaultTrees(gfx::Size(10, 10));
1891 host_impl_.PinchGestureBegin();
1892 float high_res_scale = 2.3f;
1893 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1895 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1896 EXPECT_FLOAT_EQ(high_res_scale,
1897 pending_layer_->HighResTiling()->contents_scale());
1900 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
1901 SetupDefaultTrees(gfx::Size(10, 10));
1902 host_impl_.PinchGestureBegin();
1903 float high_res_scale = 0.0001f;
1904 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
1906 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
1908 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1909 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1910 pending_layer_->HighResTiling()->contents_scale());
1913 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
1914 SetupDefaultTrees(gfx::Size(10, 10));
1916 float contents_scale = 0.15f;
1917 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
1919 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1920 EXPECT_FLOAT_EQ(contents_scale,
1921 pending_layer_->HighResTiling()->contents_scale());
1923 host_impl_.PinchGestureBegin();
1925 float page_scale = 0.0001f;
1926 EXPECT_LT(page_scale * contents_scale,
1927 pending_layer_->MinimumContentsScale());
1929 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
1930 ASSERT_GE(pending_layer_->num_tilings(), 0u);
1931 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
1932 pending_layer_->HighResTiling()->contents_scale());
1935 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
1937 virtual void InitializeRenderer() OVERRIDE {
1938 bool delegated_rendering = false;
1939 host_impl_.InitializeRenderer(
1940 FakeOutputSurface::CreateDeferredGL(
1941 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
1942 delegated_rendering).PassAs<OutputSurface>());
1945 virtual void SetUp() OVERRIDE {
1946 PictureLayerImplTest::SetUp();
1948 // Create some default active and pending trees.
1949 gfx::Size tile_size(100, 100);
1950 gfx::Size layer_bounds(400, 400);
1952 scoped_refptr<FakePicturePileImpl> pending_pile =
1953 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1954 scoped_refptr<FakePicturePileImpl> active_pile =
1955 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1957 SetupTrees(pending_pile, active_pile);
1961 // This test is really a LayerTreeHostImpl test, in that it makes sure
1962 // that trees need update draw properties after deferred initialization.
1963 // However, this is also a regression test for PictureLayerImpl in that
1964 // not having this update will cause a crash.
1965 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
1966 host_impl_.pending_tree()->UpdateDrawProperties();
1967 host_impl_.active_tree()->UpdateDrawProperties();
1968 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
1969 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1971 FakeOutputSurface* fake_output_surface =
1972 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
1973 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
1974 TestContextProvider::Create()));
1976 // These will crash PictureLayerImpl if this is not true.
1977 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
1978 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1979 host_impl_.active_tree()->UpdateDrawProperties();
1982 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
1983 gfx::Size layer_bounds(100, 100);
1984 gfx::Size viewport_size(1000, 1000);
1985 SetupDefaultTrees(layer_bounds);
1986 host_impl_.SetViewportSize(viewport_size);
1988 float contents_scale = 1.f;
1989 float device_scale = 1.3f;
1990 float page_scale = 1.4f;
1991 float maximum_animation_scale = 1.f;
1992 bool animating_transform = false;
1994 SetContentsScaleOnBothLayers(contents_scale,
1997 maximum_animation_scale,
1998 animating_transform);
1999 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2001 // Since we're CPU-rasterizing, starting an animation should cause tiling
2002 // resolution to get set to the maximum animation scale factor.
2003 animating_transform = true;
2004 maximum_animation_scale = 3.f;
2005 contents_scale = 2.f;
2007 SetContentsScaleOnBothLayers(contents_scale,
2010 maximum_animation_scale,
2011 animating_transform);
2012 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2014 // Further changes to scale during the animation should not cause a new
2015 // high-res tiling to get created.
2016 contents_scale = 4.f;
2017 maximum_animation_scale = 5.f;
2019 SetContentsScaleOnBothLayers(contents_scale,
2022 maximum_animation_scale,
2023 animating_transform);
2024 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2026 // Once we stop animating, a new high-res tiling should be created.
2027 animating_transform = false;
2029 SetContentsScaleOnBothLayers(contents_scale,
2032 maximum_animation_scale,
2033 animating_transform);
2034 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2036 // When animating with an unknown maximum animation scale factor, a new
2037 // high-res tiling should be created at the animation's initial scale.
2038 animating_transform = true;
2039 contents_scale = 2.f;
2040 maximum_animation_scale = 0.f;
2042 SetContentsScaleOnBothLayers(contents_scale,
2045 maximum_animation_scale,
2046 animating_transform);
2047 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2049 // Further changes to scale during the animation should not cause a new
2050 // high-res tiling to get created.
2051 contents_scale = 3.f;
2053 SetContentsScaleOnBothLayers(contents_scale,
2056 maximum_animation_scale,
2057 animating_transform);
2058 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2060 // Once we stop animating, a new high-res tiling should be created.
2061 animating_transform = false;
2062 contents_scale = 4.f;
2064 SetContentsScaleOnBothLayers(contents_scale,
2067 maximum_animation_scale,
2068 animating_transform);
2069 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2071 // When animating with a maxmium animation scale factor that is so large
2072 // that the layer grows larger than the viewport at this scale, a new
2073 // high-res tiling should get created at the animation's initial scale, not
2074 // at its maximum scale.
2075 animating_transform = true;
2076 contents_scale = 2.f;
2077 maximum_animation_scale = 11.f;
2079 SetContentsScaleOnBothLayers(contents_scale,
2082 maximum_animation_scale,
2083 animating_transform);
2084 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2086 // Once we stop animating, a new high-res tiling should be created.
2087 animating_transform = false;
2088 contents_scale = 11.f;
2090 SetContentsScaleOnBothLayers(contents_scale,
2093 maximum_animation_scale,
2094 animating_transform);
2095 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2097 // When animating with a maxmium animation scale factor that is so large
2098 // that the layer grows larger than the viewport at this scale, and where
2099 // the intial source scale is < 1, a new high-res tiling should get created
2100 // at source scale 1.
2101 animating_transform = true;
2102 contents_scale = 0.1f;
2103 maximum_animation_scale = 11.f;
2105 SetContentsScaleOnBothLayers(contents_scale,
2108 maximum_animation_scale,
2109 animating_transform);
2110 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2112 // Once we stop animating, a new high-res tiling should be created.
2113 animating_transform = false;
2114 contents_scale = 11.f;
2116 SetContentsScaleOnBothLayers(contents_scale,
2119 maximum_animation_scale,
2120 animating_transform);
2121 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2124 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2125 gfx::Size tile_size(100, 100);
2126 gfx::Size layer_bounds(1000, 1000);
2128 scoped_refptr<FakePicturePileImpl> pending_pile =
2129 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2131 SetupPendingTree(pending_pile);
2133 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2135 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2138 PictureLayerImpl::LayerRasterTileIterator it;
2142 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2145 pending_layer_->AddTiling(low_res_factor);
2146 pending_layer_->AddTiling(0.3f);
2147 pending_layer_->AddTiling(0.7f);
2148 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2149 pending_layer_->AddTiling(2.0f);
2151 host_impl_.SetViewportSize(gfx::Size(500, 500));
2152 host_impl_.pending_tree()->UpdateDrawProperties();
2154 std::set<Tile*> unique_tiles;
2155 bool reached_prepaint = false;
2156 size_t non_ideal_tile_count = 0u;
2157 size_t low_res_tile_count = 0u;
2158 size_t high_res_tile_count = 0u;
2159 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2163 TilePriority priority = tile->priority(PENDING_TREE);
2167 // Non-high res tiles only get visible tiles. Also, prepaint should only
2168 // come at the end of the iteration.
2169 if (priority.resolution != HIGH_RESOLUTION)
2170 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2171 else if (reached_prepaint)
2172 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2174 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2176 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2177 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2178 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2180 unique_tiles.insert(tile);
2183 EXPECT_TRUE(reached_prepaint);
2184 EXPECT_EQ(0u, non_ideal_tile_count);
2185 EXPECT_EQ(1u, low_res_tile_count);
2186 EXPECT_EQ(16u, high_res_tile_count);
2187 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2188 unique_tiles.size());
2190 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2191 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2192 tile_it != high_res_tiles.end();
2194 Tile* tile = *tile_it;
2195 ManagedTileState::TileVersion& tile_version =
2196 tile->GetTileVersionForTesting(
2197 tile->DetermineRasterModeForTree(ACTIVE_TREE));
2198 tile_version.SetSolidColorForTesting(SK_ColorRED);
2201 non_ideal_tile_count = 0;
2202 low_res_tile_count = 0;
2203 high_res_tile_count = 0;
2204 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2208 TilePriority priority = tile->priority(PENDING_TREE);
2212 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2213 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2214 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2217 EXPECT_EQ(0u, non_ideal_tile_count);
2218 EXPECT_EQ(1u, low_res_tile_count);
2219 EXPECT_EQ(0u, high_res_tile_count);
2222 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2223 gfx::Size tile_size(100, 100);
2224 gfx::Size layer_bounds(1000, 1000);
2226 scoped_refptr<FakePicturePileImpl> pending_pile =
2227 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2229 SetupPendingTree(pending_pile);
2231 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2233 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2235 std::vector<PictureLayerTiling*> tilings;
2236 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2237 tilings.push_back(pending_layer_->AddTiling(0.3f));
2238 tilings.push_back(pending_layer_->AddTiling(0.7f));
2239 tilings.push_back(pending_layer_->AddTiling(1.0f));
2240 tilings.push_back(pending_layer_->AddTiling(2.0f));
2242 host_impl_.SetViewportSize(gfx::Size(500, 500));
2243 host_impl_.pending_tree()->UpdateDrawProperties();
2245 std::vector<Tile*> all_tiles;
2246 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2248 tiling_iterator != tilings.end();
2249 ++tiling_iterator) {
2250 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2251 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2254 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2256 bool mark_required = false;
2257 size_t number_of_marked_tiles = 0u;
2258 size_t number_of_unmarked_tiles = 0u;
2259 for (size_t i = 0; i < tilings.size(); ++i) {
2260 PictureLayerTiling* tiling = tilings.at(i);
2261 for (PictureLayerTiling::CoverageIterator iter(
2263 pending_layer_->contents_scale_x(),
2264 pending_layer_->visible_content_rect());
2267 if (mark_required) {
2268 number_of_marked_tiles++;
2269 iter->MarkRequiredForActivation();
2271 number_of_unmarked_tiles++;
2273 mark_required = !mark_required;
2278 EXPECT_EQ(91u, all_tiles.size());
2279 EXPECT_EQ(91u, all_tiles_set.size());
2280 EXPECT_GT(number_of_marked_tiles, 1u);
2281 EXPECT_GT(number_of_unmarked_tiles, 1u);
2284 PictureLayerImpl::LayerEvictionTileIterator it;
2287 // Tiles don't have resources yet.
2288 it = PictureLayerImpl::LayerEvictionTileIterator(
2289 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2292 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2294 std::set<Tile*> unique_tiles;
2295 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2296 size_t scale_index = 0;
2297 bool reached_visible = false;
2298 Tile* last_tile = NULL;
2299 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2300 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2309 TilePriority priority = tile->priority(PENDING_TREE);
2311 if (priority.priority_bin == TilePriority::NOW) {
2312 reached_visible = true;
2317 EXPECT_FALSE(tile->required_for_activation());
2319 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2320 std::numeric_limits<float>::epsilon()) {
2322 ASSERT_LT(scale_index, arraysize(expected_scales));
2325 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2326 unique_tiles.insert(tile);
2328 // If the tile is the same rough bin as last tile (same activation, bin, and
2329 // scale), then distance should be decreasing.
2330 if (tile->required_for_activation() ==
2331 last_tile->required_for_activation() &&
2332 priority.priority_bin ==
2333 last_tile->priority(PENDING_TREE).priority_bin &&
2334 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2335 std::numeric_limits<float>::epsilon()) {
2336 EXPECT_LE(priority.distance_to_visible,
2337 last_tile->priority(PENDING_TREE).distance_to_visible);
2343 EXPECT_TRUE(reached_visible);
2344 EXPECT_EQ(65u, unique_tiles.size());
2347 bool reached_required = false;
2352 TilePriority priority = tile->priority(PENDING_TREE);
2353 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2355 if (reached_required) {
2356 EXPECT_TRUE(tile->required_for_activation());
2357 } else if (tile->required_for_activation()) {
2358 reached_required = true;
2362 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2363 std::numeric_limits<float>::epsilon()) {
2365 ASSERT_LT(scale_index, arraysize(expected_scales));
2368 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2369 unique_tiles.insert(tile);
2372 EXPECT_TRUE(reached_required);
2373 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2376 TEST_F(PictureLayerImplTest, Occlusion) {
2377 gfx::Size tile_size(102, 102);
2378 gfx::Size layer_bounds(1000, 1000);
2379 gfx::Size viewport_size(1000, 1000);
2381 LayerTestCommon::LayerImplTest impl;
2383 scoped_refptr<FakePicturePileImpl> pending_pile =
2384 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2385 SetupPendingTree(pending_pile);
2386 pending_layer_->SetBounds(layer_bounds);
2388 active_layer_->set_fixed_tile_size(tile_size);
2390 host_impl_.SetViewportSize(viewport_size);
2391 host_impl_.active_tree()->UpdateDrawProperties();
2393 std::vector<Tile*> tiles =
2394 active_layer_->HighResTiling()->AllTilesForTesting();
2395 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2398 SCOPED_TRACE("No occlusion");
2400 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2402 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2403 gfx::Rect(layer_bounds));
2404 EXPECT_EQ(100u, impl.quad_list().size());
2408 SCOPED_TRACE("Full occlusion");
2409 gfx::Rect occluded(active_layer_->visible_content_rect());
2410 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2412 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2413 EXPECT_EQ(impl.quad_list().size(), 0u);
2417 SCOPED_TRACE("Partial occlusion");
2418 gfx::Rect occluded(150, 0, 200, 1000);
2419 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2421 size_t partially_occluded_count = 0;
2422 LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
2424 gfx::Rect(layer_bounds),
2426 &partially_occluded_count);
2427 // The layer outputs one quad, which is partially occluded.
2428 EXPECT_EQ(100u - 10u, impl.quad_list().size());
2429 EXPECT_EQ(10u + 10u, partially_occluded_count);
2433 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2434 gfx::Size tile_size(host_impl_.settings().default_tile_size);
2435 SetupDefaultTrees(tile_size);
2437 float contents_scale = 2.f;
2438 float device_scale = 1.f;
2439 float page_scale = 1.f;
2440 float maximum_animation_scale = 1.f;
2441 bool animating_transform = false;
2443 SetContentsScaleOnBothLayers(contents_scale,
2446 maximum_animation_scale,
2447 animating_transform);
2448 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2450 // Changing the source scale without being in an animation will cause
2451 // the layer to reset its source scale to 1.f.
2452 contents_scale = 3.f;
2454 SetContentsScaleOnBothLayers(contents_scale,
2457 maximum_animation_scale,
2458 animating_transform);
2459 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2461 // Further changes to the source scale will no longer be reflected in the
2463 contents_scale = 0.5f;
2465 SetContentsScaleOnBothLayers(contents_scale,
2468 maximum_animation_scale,
2469 animating_transform);
2470 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2473 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2474 gfx::Size tile_size(100, 100);
2475 gfx::Size layer_bounds(1000, 1000);
2477 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2479 // Make sure some tiles are not shared.
2480 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2482 CreateHighLowResAndSetAllTilesVisible();
2483 active_layer_->SetAllTilesReady();
2484 pending_layer_->MarkVisibleResourcesAsRequired();
2486 // All pending layer tiles required are not ready.
2487 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2489 // Initialize all low-res tiles.
2490 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2492 // Low-res tiles should not be enough.
2493 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2495 // Initialize remaining tiles.
2496 pending_layer_->SetAllTilesReady();
2498 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2501 TEST_F(PictureLayerImplTest, HighResReadyToDrawNotEnoughToActivate) {
2502 gfx::Size tile_size(100, 100);
2503 gfx::Size layer_bounds(1000, 1000);
2505 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2507 // Make sure some tiles are not shared.
2508 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2510 CreateHighLowResAndSetAllTilesVisible();
2511 active_layer_->SetAllTilesReady();
2512 pending_layer_->MarkVisibleResourcesAsRequired();
2514 // All pending layer tiles required are not ready.
2515 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2517 // Initialize all high-res tiles.
2518 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2520 // High-res tiles should not be enough.
2521 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2523 // Initialize remaining tiles.
2524 pending_layer_->SetAllTilesReady();
2526 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2529 class NoLowResTilingsSettings : public ImplSidePaintingSettings {
2531 NoLowResTilingsSettings() { create_low_res_tiling = false; }
2534 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
2536 NoLowResPictureLayerImplTest()
2537 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
2540 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
2541 gfx::Size tile_size(400, 400);
2542 gfx::Size layer_bounds(1300, 1900);
2544 scoped_refptr<FakePicturePileImpl> pending_pile =
2545 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2546 scoped_refptr<FakePicturePileImpl> active_pile =
2547 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2549 SetupTrees(pending_pile, active_pile);
2550 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2552 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2553 EXPECT_LT(low_res_factor, 1.f);
2555 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2556 6.f, // ideal contents scale
2557 3.f, // device scale
2559 1.f, // maximum animation scale
2561 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2562 EXPECT_FLOAT_EQ(6.f,
2563 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2565 // If we change the page scale factor, then we should get new tilings.
2566 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2567 6.6f, // ideal contents scale
2568 3.f, // device scale
2570 1.f, // maximum animation scale
2572 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2573 EXPECT_FLOAT_EQ(6.6f,
2574 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2576 // If we change the device scale factor, then we should get new tilings.
2577 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2578 7.26f, // ideal contents scale
2579 3.3f, // device scale
2581 1.f, // maximum animation scale
2583 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2584 EXPECT_FLOAT_EQ(7.26f,
2585 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2587 // If we change the device scale factor, but end up at the same total scale
2588 // factor somehow, then we don't get new tilings.
2589 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2590 7.26f, // ideal contents scale
2591 2.2f, // device scale
2593 1.f, // maximum animation scale
2595 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2596 EXPECT_FLOAT_EQ(7.26f,
2597 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2600 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
2601 gfx::Size tile_size(100, 100);
2602 gfx::Size layer_bounds(1000, 1000);
2604 scoped_refptr<FakePicturePileImpl> pending_pile =
2605 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2606 // Layers with entirely empty piles can't get tilings.
2607 pending_pile->AddRecordingAt(0, 0);
2609 SetupPendingTree(pending_pile);
2611 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2612 pending_layer_->AddTiling(1.0f);
2613 pending_layer_->AddTiling(2.0f);
2615 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
2616 // on a layer with no recordings.
2617 host_impl_.pending_tree()->UpdateDrawProperties();
2618 pending_layer_->MarkVisibleResourcesAsRequired();
2621 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
2622 gfx::Size layer_bounds(400, 400);
2623 gfx::Size tile_size(100, 100);
2624 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2626 CreateHighLowResAndSetAllTilesVisible();
2628 Tile* some_active_tile =
2629 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2630 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2632 // All tiles shared (no invalidation), so even though the active tree's
2633 // tiles aren't ready, there is nothing required.
2634 pending_layer_->MarkVisibleResourcesAsRequired();
2635 AssertNoTilesRequired(pending_layer_->HighResTiling());
2636 if (host_impl_.settings().create_low_res_tiling) {
2637 AssertNoTilesRequired(pending_layer_->LowResTiling());
2641 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2642 gfx::Size layer_bounds(400, 400);
2643 gfx::Size tile_size(100, 100);
2644 scoped_refptr<FakePicturePileImpl> pending_pile =
2645 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2646 // This pile will create tilings, but has no recordings so will not create any
2647 // tiles. This is attempting to simulate scrolling past the end of recorded
2648 // content on the active layer, where the recordings are so far away that
2649 // no tiles are created.
2650 scoped_refptr<FakePicturePileImpl> active_pile =
2651 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2652 tile_size, layer_bounds);
2653 SetupTrees(pending_pile, active_pile);
2654 pending_layer_->set_fixed_tile_size(tile_size);
2655 active_layer_->set_fixed_tile_size(tile_size);
2657 CreateHighLowResAndSetAllTilesVisible();
2659 // Active layer has tilings, but no tiles due to missing recordings.
2660 EXPECT_TRUE(active_layer_->CanHaveTilings());
2661 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
2662 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
2663 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2665 // Since the active layer has no tiles at all, the pending layer doesn't
2666 // need content in order to activate.
2667 pending_layer_->MarkVisibleResourcesAsRequired();
2668 AssertNoTilesRequired(pending_layer_->HighResTiling());
2669 if (host_impl_.settings().create_low_res_tiling)
2670 AssertNoTilesRequired(pending_layer_->LowResTiling());
2673 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
2674 base::TimeTicks time_ticks;
2675 time_ticks += base::TimeDelta::FromMilliseconds(1);
2676 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2678 gfx::Size tile_size(100, 100);
2679 gfx::Size layer_bounds(400, 400);
2681 scoped_refptr<FakePicturePileImpl> pending_pile =
2682 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2683 scoped_refptr<FakePicturePileImpl> active_pile =
2684 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2686 SetupTrees(pending_pile, active_pile);
2688 Region invalidation;
2689 AddDefaultTilingsWithInvalidation(invalidation);
2690 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
2692 // UpdateTiles with valid viewport. Should update tile viewport.
2693 // Note viewport is considered invalid if and only if in resourceless
2695 bool resourceless_software_draw = false;
2696 gfx::Rect viewport = gfx::Rect(layer_bounds);
2697 gfx::Transform transform;
2698 host_impl_.SetExternalDrawConstraints(transform,
2703 resourceless_software_draw);
2704 active_layer_->draw_properties().visible_content_rect = viewport;
2705 active_layer_->draw_properties().screen_space_transform = transform;
2706 active_layer_->UpdateTiles(NULL);
2708 gfx::Rect visible_rect_for_tile_priority =
2709 active_layer_->visible_rect_for_tile_priority();
2710 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
2711 gfx::Rect viewport_rect_for_tile_priority =
2712 active_layer_->viewport_rect_for_tile_priority();
2713 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
2714 gfx::Transform screen_space_transform_for_tile_priority =
2715 active_layer_->screen_space_transform_for_tile_priority();
2717 // Expand viewport and set it as invalid for prioritizing tiles.
2718 // Should not update tile viewport.
2719 time_ticks += base::TimeDelta::FromMilliseconds(200);
2720 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2721 resourceless_software_draw = true;
2722 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
2723 transform.Translate(1.f, 1.f);
2724 active_layer_->draw_properties().visible_content_rect = viewport;
2725 active_layer_->draw_properties().screen_space_transform = transform;
2726 host_impl_.SetExternalDrawConstraints(transform,
2731 resourceless_software_draw);
2732 active_layer_->UpdateTiles(NULL);
2734 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
2735 active_layer_->visible_rect_for_tile_priority());
2736 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
2737 active_layer_->viewport_rect_for_tile_priority());
2738 EXPECT_TRANSFORMATION_MATRIX_EQ(
2739 screen_space_transform_for_tile_priority,
2740 active_layer_->screen_space_transform_for_tile_priority());
2742 // Keep expanded viewport but mark it valid. Should update tile viewport.
2743 time_ticks += base::TimeDelta::FromMilliseconds(200);
2744 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
2745 resourceless_software_draw = false;
2746 host_impl_.SetExternalDrawConstraints(transform,
2751 resourceless_software_draw);
2752 active_layer_->UpdateTiles(NULL);
2754 EXPECT_FALSE(visible_rect_for_tile_priority ==
2755 active_layer_->visible_rect_for_tile_priority());
2756 EXPECT_FALSE(viewport_rect_for_tile_priority ==
2757 active_layer_->viewport_rect_for_tile_priority());
2758 EXPECT_FALSE(screen_space_transform_for_tile_priority ==
2759 active_layer_->screen_space_transform_for_tile_priority());
2762 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportAfterReleaseResources) {
2763 gfx::Size tile_size(100, 100);
2764 gfx::Size layer_bounds(400, 400);
2766 scoped_refptr<FakePicturePileImpl> pending_pile =
2767 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2768 scoped_refptr<FakePicturePileImpl> active_pile =
2769 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2771 SetupTrees(pending_pile, active_pile);
2773 Region invalidation;
2774 AddDefaultTilingsWithInvalidation(invalidation);
2776 bool resourceless_software_draw = true;
2777 gfx::Rect viewport = gfx::Rect(layer_bounds);
2778 gfx::Transform identity = gfx::Transform();
2779 host_impl_.SetExternalDrawConstraints(identity,
2784 resourceless_software_draw);
2785 ResetTilingsAndRasterScales();
2786 host_impl_.pending_tree()->UpdateDrawProperties();
2787 host_impl_.active_tree()->UpdateDrawProperties();
2788 EXPECT_TRUE(active_layer_->HighResTiling());
2790 size_t num_tilings = active_layer_->num_tilings();
2791 active_layer_->UpdateTiles(NULL);
2792 pending_layer_->AddTiling(0.5f);
2793 EXPECT_EQ(num_tilings + 1, active_layer_->num_tilings());
2796 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
2797 gfx::Size tile_size(400, 400);
2798 gfx::Size layer_bounds(1300, 1900);
2800 scoped_refptr<FakePicturePileImpl> pending_pile =
2801 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2802 scoped_refptr<FakePicturePileImpl> active_pile =
2803 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2805 std::vector<PictureLayerTiling*> used_tilings;
2807 SetupTrees(pending_pile, active_pile);
2808 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2810 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2811 EXPECT_LT(low_res_factor, 1.f);
2813 float device_scale = 1.7f;
2814 float page_scale = 3.2f;
2817 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2818 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2820 // We only have ideal tilings, so they aren't removed.
2821 used_tilings.clear();
2822 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2823 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2825 host_impl_.PinchGestureBegin();
2827 // Changing the ideal but not creating new tilings.
2830 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
2831 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2833 // The tilings are still our target scale, so they aren't removed.
2834 used_tilings.clear();
2835 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2836 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2838 host_impl_.PinchGestureEnd();
2840 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
2843 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
2844 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2845 EXPECT_FLOAT_EQ(1.f,
2846 active_layer_->tilings()->tiling_at(1)->contents_scale());
2848 // Mark the non-ideal tilings as used. They won't be removed.
2849 used_tilings.clear();
2850 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2851 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2852 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2854 // Now move the ideal scale to 0.5. Our target stays 1.2.
2855 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
2857 // The high resolution tiling is between target and ideal, so is not
2858 // removed. The low res tiling for the old ideal=1.0 scale is removed.
2859 used_tilings.clear();
2860 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2861 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2863 // Now move the ideal scale to 1.0. Our target stays 1.2.
2864 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
2866 // All the tilings are between are target and the ideal, so they are not
2868 used_tilings.clear();
2869 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2870 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2872 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
2873 SetupDrawPropertiesAndUpdateTiles(
2874 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2876 // Because the pending layer's ideal scale is still 1.0, our tilings fall
2877 // in the range [1.0,1.2] and are kept.
2878 used_tilings.clear();
2879 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2880 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2882 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
2884 SetupDrawPropertiesAndUpdateTiles(
2885 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
2887 // Our 1.0 tiling now falls outside the range between our ideal scale and our
2888 // target raster scale. But it is in our used tilings set, so nothing is
2890 used_tilings.clear();
2891 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2892 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2893 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
2895 // If we remove it from our used tilings set, it is outside the range to keep
2896 // so it is deleted.
2897 used_tilings.clear();
2898 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2899 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
2902 TEST_F(PictureLayerImplTest, ScaleCollision) {
2903 gfx::Size tile_size(400, 400);
2904 gfx::Size layer_bounds(1300, 1900);
2906 scoped_refptr<FakePicturePileImpl> pending_pile =
2907 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2908 scoped_refptr<FakePicturePileImpl> active_pile =
2909 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2911 std::vector<PictureLayerTiling*> used_tilings;
2913 SetupTrees(pending_pile, active_pile);
2915 float pending_contents_scale = 1.f;
2916 float active_contents_scale = 2.f;
2917 float device_scale_factor = 1.f;
2918 float page_scale_factor = 1.f;
2919 float maximum_animation_contents_scale = 1.f;
2920 bool animating_transform = false;
2922 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
2923 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2924 EXPECT_LT(low_res_factor, 1.f);
2926 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2927 pending_contents_scale,
2928 device_scale_factor,
2930 maximum_animation_contents_scale,
2931 animating_transform);
2932 SetupDrawPropertiesAndUpdateTiles(active_layer_,
2933 active_contents_scale,
2934 device_scale_factor,
2936 maximum_animation_contents_scale,
2937 animating_transform);
2939 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
2940 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2942 EXPECT_EQ(active_contents_scale,
2943 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2944 EXPECT_EQ(pending_contents_scale,
2945 pending_layer_->tilings()->tiling_at(1)->contents_scale());
2946 EXPECT_EQ(active_contents_scale * low_res_factor,
2947 pending_layer_->tilings()->tiling_at(2)->contents_scale());
2948 EXPECT_EQ(pending_contents_scale * low_res_factor,
2949 pending_layer_->tilings()->tiling_at(3)->contents_scale());
2951 EXPECT_EQ(active_contents_scale,
2952 active_layer_->tilings()->tiling_at(0)->contents_scale());
2953 EXPECT_EQ(pending_contents_scale,
2954 active_layer_->tilings()->tiling_at(1)->contents_scale());
2955 EXPECT_EQ(active_contents_scale * low_res_factor,
2956 active_layer_->tilings()->tiling_at(2)->contents_scale());
2957 EXPECT_EQ(pending_contents_scale * low_res_factor,
2958 active_layer_->tilings()->tiling_at(3)->contents_scale());
2960 // The unused low res tiling from the pending tree must be kept or we may add
2961 // it again on the active tree and collide with the pending tree.
2962 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
2963 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
2964 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
2966 EXPECT_EQ(active_contents_scale,
2967 active_layer_->tilings()->tiling_at(0)->contents_scale());
2968 EXPECT_EQ(pending_contents_scale,
2969 active_layer_->tilings()->tiling_at(1)->contents_scale());
2970 EXPECT_EQ(active_contents_scale * low_res_factor,
2971 active_layer_->tilings()->tiling_at(2)->contents_scale());
2972 EXPECT_EQ(pending_contents_scale * low_res_factor,
2973 active_layer_->tilings()->tiling_at(3)->contents_scale());
2976 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
2977 gfx::Size tile_size(400, 400);
2978 gfx::Size layer_bounds(1300, 1900);
2980 scoped_refptr<FakePicturePileImpl> pending_pile =
2981 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2982 scoped_refptr<FakePicturePileImpl> active_pile =
2983 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2985 SetupTrees(pending_pile, active_pile);
2986 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2988 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2989 1.3f, // ideal contents scale
2990 2.7f, // device scale
2992 1.f, // maximum animation scale
2994 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
2996 // All tilings should be removed when losing output surface.
2997 active_layer_->ReleaseResources();
2998 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2999 pending_layer_->ReleaseResources();
3000 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3002 // This should create new tilings.
3003 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3004 1.3f, // ideal contents scale
3005 2.7f, // device scale
3007 1.f, // maximum animation scale
3009 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3012 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3013 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3014 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3016 gfx::Size tile_size(400, 400);
3017 gfx::Size layer_bounds(1000, 2000);
3019 scoped_refptr<FakePicturePileImpl> pending_pile =
3020 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3021 scoped_refptr<FakePicturePileImpl> active_pile =
3022 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3024 SetupTrees(pending_pile, active_pile);
3026 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3027 host_impl_.pending_tree()->UpdateDrawProperties();
3029 active_layer_->draw_properties().visible_content_rect =
3030 gfx::Rect(layer_bounds);
3031 host_impl_.active_tree()->UpdateDrawProperties();
3033 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3034 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3035 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3036 SK_MScalar1 / max_contents_scale);
3038 AppendQuadsData data;
3039 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3041 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3042 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3043 // The content_to_target_transform should be scaled by the
3044 // MaximumTilingContentsScale on the layer.
3045 EXPECT_EQ(scaled_draw_transform.ToString(),
3046 render_pass->shared_quad_state_list[0]
3047 ->content_to_target_transform.ToString());
3048 // The content_bounds should be scaled by the
3049 // MaximumTilingContentsScale on the layer.
3050 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3051 render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3052 // The visible_content_rect should be scaled by the
3053 // MaximumTilingContentsScale on the layer.
3055 gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3056 render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3059 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3060 gfx::Size tile_size(400, 400);
3061 gfx::Size bounds(100000, 100);
3063 host_impl_.CreatePendingTree();
3065 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3067 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3068 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3070 layer_with_mask->SetBounds(bounds);
3071 layer_with_mask->SetContentBounds(bounds);
3073 scoped_refptr<FakePicturePileImpl> pending_pile =
3074 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3075 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3076 host_impl_.pending_tree(), 3, pending_pile);
3078 mask->SetIsMask(true);
3079 mask->SetBounds(bounds);
3080 mask->SetContentBounds(bounds);
3081 mask->SetDrawsContent(true);
3083 FakePictureLayerImpl* pending_mask_content = mask.get();
3084 layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3086 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3087 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3089 child_of_layer_with_mask->SetBounds(bounds);
3090 child_of_layer_with_mask->SetContentBounds(bounds);
3091 child_of_layer_with_mask->SetDrawsContent(true);
3093 layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3095 root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3097 host_impl_.pending_tree()->SetRootLayer(root.Pass());
3099 EXPECT_FALSE(pending_mask_content->tilings());
3100 host_impl_.pending_tree()->UpdateDrawProperties();
3101 EXPECT_NE(0u, pending_mask_content->num_tilings());
3104 class OcclusionTrackingSettings : public ImplSidePaintingSettings {
3106 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3109 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3111 OcclusionTrackingPictureLayerImplTest()
3112 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3114 void VerifyEvictionConsidersOcclusion(
3115 PictureLayerImpl* layer,
3116 size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3117 for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3119 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3120 size_t occluded_tile_count = 0u;
3121 Tile* last_tile = NULL;
3123 for (PictureLayerImpl::LayerEvictionTileIterator it =
3124 PictureLayerImpl::LayerEvictionTileIterator(layer,
3132 // The only way we will encounter an occluded tile after an unoccluded
3133 // tile is if the priorty bin decreased, the tile is required for
3134 // activation, or the scale changed.
3135 bool tile_is_occluded =
3136 tile->is_occluded_for_tree_priority(tree_priority);
3137 if (tile_is_occluded) {
3138 occluded_tile_count++;
3140 bool last_tile_is_occluded =
3141 last_tile->is_occluded_for_tree_priority(tree_priority);
3142 if (!last_tile_is_occluded) {
3143 TilePriority::PriorityBin tile_priority_bin =
3144 tile->priority_for_tree_priority(tree_priority).priority_bin;
3145 TilePriority::PriorityBin last_tile_priority_bin =
3146 last_tile->priority_for_tree_priority(tree_priority)
3150 (tile_priority_bin < last_tile_priority_bin) ||
3151 tile->required_for_activation() ||
3152 (tile->contents_scale() != last_tile->contents_scale()));
3157 EXPECT_EQ(expected_occluded_tile_count[priority_count],
3158 occluded_tile_count);
3163 TEST_F(OcclusionTrackingPictureLayerImplTest,
3164 OccludedTilesSkippedDuringRasterization) {
3165 base::TimeTicks time_ticks;
3166 time_ticks += base::TimeDelta::FromMilliseconds(1);
3167 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3169 gfx::Size tile_size(102, 102);
3170 gfx::Size layer_bounds(1000, 1000);
3171 gfx::Size viewport_size(500, 500);
3172 gfx::Point occluding_layer_position(310, 0);
3174 scoped_refptr<FakePicturePileImpl> pending_pile =
3175 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3176 SetupPendingTree(pending_pile);
3177 pending_layer_->set_fixed_tile_size(tile_size);
3179 host_impl_.SetViewportSize(viewport_size);
3180 host_impl_.pending_tree()->UpdateDrawProperties();
3183 int unoccluded_tile_count = 0;
3184 for (PictureLayerImpl::LayerRasterTileIterator it =
3185 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3190 // Occluded tiles should not be iterated over.
3191 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3193 // Some tiles may not be visible (i.e. outside the viewport). The rest are
3194 // visible and at least partially unoccluded, verified by the above expect.
3195 bool tile_is_visible =
3196 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3197 if (tile_is_visible)
3198 unoccluded_tile_count++;
3200 EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3202 // Partial occlusion.
3203 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3204 LayerImpl* layer1 = pending_layer_->children()[0];
3205 layer1->SetBounds(layer_bounds);
3206 layer1->SetContentBounds(layer_bounds);
3207 layer1->SetDrawsContent(true);
3208 layer1->SetContentsOpaque(true);
3209 layer1->SetPosition(occluding_layer_position);
3211 time_ticks += base::TimeDelta::FromMilliseconds(200);
3212 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3213 host_impl_.pending_tree()->UpdateDrawProperties();
3215 unoccluded_tile_count = 0;
3216 for (PictureLayerImpl::LayerRasterTileIterator it =
3217 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3222 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3224 bool tile_is_visible =
3225 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3226 if (tile_is_visible)
3227 unoccluded_tile_count++;
3229 EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3232 layer1->SetPosition(gfx::Point(0, 0));
3234 time_ticks += base::TimeDelta::FromMilliseconds(200);
3235 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3236 host_impl_.pending_tree()->UpdateDrawProperties();
3238 unoccluded_tile_count = 0;
3239 for (PictureLayerImpl::LayerRasterTileIterator it =
3240 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3245 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3247 bool tile_is_visible =
3248 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3249 if (tile_is_visible)
3250 unoccluded_tile_count++;
3252 EXPECT_EQ(unoccluded_tile_count, 0);
3255 TEST_F(OcclusionTrackingPictureLayerImplTest,
3256 OccludedTilesNotMarkedAsRequired) {
3257 base::TimeTicks time_ticks;
3258 time_ticks += base::TimeDelta::FromMilliseconds(1);
3259 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3261 gfx::Size tile_size(102, 102);
3262 gfx::Size layer_bounds(1000, 1000);
3263 gfx::Size viewport_size(500, 500);
3264 gfx::Point occluding_layer_position(310, 0);
3266 scoped_refptr<FakePicturePileImpl> pending_pile =
3267 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3268 SetupPendingTree(pending_pile);
3269 pending_layer_->set_fixed_tile_size(tile_size);
3271 host_impl_.SetViewportSize(viewport_size);
3272 host_impl_.pending_tree()->UpdateDrawProperties();
3275 int occluded_tile_count = 0;
3276 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3277 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3279 occluded_tile_count = 0;
3280 for (PictureLayerTiling::CoverageIterator iter(
3282 pending_layer_->contents_scale_x(),
3283 gfx::Rect(layer_bounds));
3288 const Tile* tile = *iter;
3290 // Fully occluded tiles are not required for activation.
3291 if (tile->is_occluded(PENDING_TREE)) {
3292 EXPECT_FALSE(tile->required_for_activation());
3293 occluded_tile_count++;
3296 EXPECT_EQ(occluded_tile_count, 0);
3299 // Partial occlusion.
3300 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3301 LayerImpl* layer1 = pending_layer_->children()[0];
3302 layer1->SetBounds(layer_bounds);
3303 layer1->SetContentBounds(layer_bounds);
3304 layer1->SetDrawsContent(true);
3305 layer1->SetContentsOpaque(true);
3306 layer1->SetPosition(occluding_layer_position);
3308 time_ticks += base::TimeDelta::FromMilliseconds(200);
3309 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3310 host_impl_.pending_tree()->UpdateDrawProperties();
3312 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3313 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3315 occluded_tile_count = 0;
3316 for (PictureLayerTiling::CoverageIterator iter(
3318 pending_layer_->contents_scale_x(),
3319 gfx::Rect(layer_bounds));
3324 const Tile* tile = *iter;
3326 if (tile->is_occluded(PENDING_TREE)) {
3327 EXPECT_FALSE(tile->required_for_activation());
3328 occluded_tile_count++;
3333 EXPECT_EQ(occluded_tile_count, 5);
3336 EXPECT_EQ(occluded_tile_count, 2);
3344 layer1->SetPosition(gfx::PointF(0, 0));
3346 time_ticks += base::TimeDelta::FromMilliseconds(200);
3347 host_impl_.SetCurrentFrameTimeTicks(time_ticks);
3348 host_impl_.pending_tree()->UpdateDrawProperties();
3350 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3351 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3353 occluded_tile_count = 0;
3354 for (PictureLayerTiling::CoverageIterator iter(
3356 pending_layer_->contents_scale_x(),
3357 gfx::Rect(layer_bounds));
3362 const Tile* tile = *iter;
3364 if (tile->is_occluded(PENDING_TREE)) {
3365 EXPECT_FALSE(tile->required_for_activation());
3366 occluded_tile_count++;
3371 EXPECT_EQ(occluded_tile_count, 25);
3374 EXPECT_EQ(occluded_tile_count, 4);
3382 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3383 gfx::Size tile_size(102, 102);
3384 gfx::Size layer_bounds(1000, 1000);
3385 gfx::Size viewport_size(500, 500);
3386 gfx::Point occluding_layer_position(310, 0);
3388 scoped_refptr<FakePicturePileImpl> pending_pile =
3389 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3390 SetupPendingTree(pending_pile);
3391 pending_layer_->set_fixed_tile_size(tile_size);
3393 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3395 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3397 std::vector<PictureLayerTiling*> tilings;
3398 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3399 tilings.push_back(pending_layer_->AddTiling(0.3f));
3400 tilings.push_back(pending_layer_->AddTiling(0.7f));
3401 tilings.push_back(pending_layer_->AddTiling(1.0f));
3402 tilings.push_back(pending_layer_->AddTiling(2.0f));
3404 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3405 LayerImpl* layer1 = pending_layer_->children()[0];
3406 layer1->SetBounds(layer_bounds);
3407 layer1->SetContentBounds(layer_bounds);
3408 layer1->SetDrawsContent(true);
3409 layer1->SetContentsOpaque(true);
3410 layer1->SetPosition(occluding_layer_position);
3412 host_impl_.SetViewportSize(viewport_size);
3413 host_impl_.pending_tree()->UpdateDrawProperties();
3415 int tiling_count = 0;
3416 int occluded_tile_count = 0;
3417 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3419 tiling_iterator != tilings.end();
3420 ++tiling_iterator) {
3421 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3423 occluded_tile_count = 0;
3424 for (size_t i = 0; i < tiles.size(); ++i) {
3425 if (tiles[i]->is_occluded(PENDING_TREE)) {
3426 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3427 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
3428 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
3429 occluded_tile_count++;
3432 switch (tiling_count) {
3435 EXPECT_EQ(occluded_tile_count, 2);
3438 EXPECT_EQ(occluded_tile_count, 4);
3441 EXPECT_EQ(occluded_tile_count, 5);
3444 EXPECT_EQ(occluded_tile_count, 30);
3453 EXPECT_EQ(tiling_count, 5);
3456 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
3457 gfx::Size tile_size(102, 102);
3458 gfx::Size layer_bounds(1000, 1000);
3459 gfx::Size viewport_size(1000, 1000);
3460 gfx::Point occluding_layer_position(310, 0);
3461 gfx::Rect invalidation_rect(230, 230, 102, 102);
3463 scoped_refptr<FakePicturePileImpl> pending_pile =
3464 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3465 scoped_refptr<FakePicturePileImpl> active_pile =
3466 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3467 SetupTrees(pending_pile, active_pile);
3469 // Partially occlude the active layer.
3470 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3471 LayerImpl* layer1 = active_layer_->children()[0];
3472 layer1->SetBounds(layer_bounds);
3473 layer1->SetContentBounds(layer_bounds);
3474 layer1->SetDrawsContent(true);
3475 layer1->SetContentsOpaque(true);
3476 layer1->SetPosition(occluding_layer_position);
3478 // Partially invalidate the pending layer.
3479 pending_layer_->set_invalidation(invalidation_rect);
3481 host_impl_.SetViewportSize(viewport_size);
3483 active_layer_->CreateDefaultTilingsAndTiles();
3484 pending_layer_->CreateDefaultTilingsAndTiles();
3486 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3487 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3489 for (PictureLayerTiling::CoverageIterator iter(
3491 pending_layer_->contents_scale_x(),
3492 gfx::Rect(layer_bounds));
3497 const Tile* tile = *iter;
3499 // All tiles are unoccluded on the pending tree.
3500 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3503 pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3504 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3505 tile->content_rect(), 1.0f / tile->contents_scale());
3507 if (scaled_content_rect.Intersects(invalidation_rect)) {
3508 // Tiles inside the invalidation rect are only on the pending tree.
3509 EXPECT_NE(tile, twin_tile);
3511 // Unshared tiles should be unoccluded on the active tree by default.
3512 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
3514 // Tiles outside the invalidation rect are shared between both trees.
3515 EXPECT_EQ(tile, twin_tile);
3516 // Shared tiles are occluded on the active tree iff they lie beneath the
3518 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3519 scaled_content_rect.x() >= occluding_layer_position.x());
3524 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
3525 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
3527 for (PictureLayerTiling::CoverageIterator iter(
3529 active_layer_->contents_scale_x(),
3530 gfx::Rect(layer_bounds));
3535 const Tile* tile = *iter;
3538 active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3539 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3540 tile->content_rect(), 1.0f / tile->contents_scale());
3542 // Since we've already checked the shared tiles, only consider tiles in
3543 // the invalidation rect.
3544 if (scaled_content_rect.Intersects(invalidation_rect)) {
3545 // Tiles inside the invalidation rect are only on the active tree.
3546 EXPECT_NE(tile, twin_tile);
3548 // Unshared tiles should be unoccluded on the pending tree by default.
3549 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3551 // Unshared tiles are occluded on the active tree iff they lie beneath
3552 // the occluding layer.
3553 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3554 scaled_content_rect.x() >= occluding_layer_position.x());
3560 TEST_F(OcclusionTrackingPictureLayerImplTest,
3561 OccludedTilesConsideredDuringEviction) {
3562 gfx::Size tile_size(102, 102);
3563 gfx::Size layer_bounds(1000, 1000);
3564 gfx::Size viewport_size(500, 500);
3565 gfx::Point pending_occluding_layer_position(310, 0);
3566 gfx::Point active_occluding_layer_position(0, 310);
3567 gfx::Rect invalidation_rect(230, 230, 102, 102);
3569 scoped_refptr<FakePicturePileImpl> pending_pile =
3570 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3571 scoped_refptr<FakePicturePileImpl> active_pile =
3572 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3573 SetupTrees(pending_pile, active_pile);
3575 pending_layer_->set_fixed_tile_size(tile_size);
3576 active_layer_->set_fixed_tile_size(tile_size);
3578 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3580 std::vector<PictureLayerTiling*> tilings;
3581 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3582 tilings.push_back(pending_layer_->AddTiling(0.3f));
3583 tilings.push_back(pending_layer_->AddTiling(0.7f));
3584 tilings.push_back(pending_layer_->AddTiling(1.0f));
3585 tilings.push_back(pending_layer_->AddTiling(2.0f));
3587 EXPECT_EQ(5u, pending_layer_->num_tilings());
3588 EXPECT_EQ(5u, active_layer_->num_tilings());
3590 // Partially occlude the pending layer.
3591 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3592 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
3593 pending_occluding_layer->SetBounds(layer_bounds);
3594 pending_occluding_layer->SetContentBounds(layer_bounds);
3595 pending_occluding_layer->SetDrawsContent(true);
3596 pending_occluding_layer->SetContentsOpaque(true);
3597 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
3599 // Partially occlude the active layer.
3600 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3601 LayerImpl* active_occluding_layer = active_layer_->children()[0];
3602 active_occluding_layer->SetBounds(layer_bounds);
3603 active_occluding_layer->SetContentBounds(layer_bounds);
3604 active_occluding_layer->SetDrawsContent(true);
3605 active_occluding_layer->SetContentsOpaque(true);
3606 active_occluding_layer->SetPosition(active_occluding_layer_position);
3608 // Partially invalidate the pending layer. Tiles inside the invalidation rect
3609 // are not shared between trees.
3610 pending_layer_->set_invalidation(invalidation_rect);
3612 host_impl_.SetViewportSize(viewport_size);
3613 host_impl_.active_tree()->UpdateDrawProperties();
3614 host_impl_.pending_tree()->UpdateDrawProperties();
3616 // The expected number of occluded tiles on each of the 5 tilings for each of
3617 // the 3 tree priorities.
3618 size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
3619 size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
3620 size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
3622 // The total expected number of occluded tiles on all tilings for each of the
3623 // 3 tree priorities.
3624 size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
3626 ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
3628 // Verify number of occluded tiles on the pending layer for each tiling.
3629 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3630 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3631 tiling->CreateAllTilesForTesting();
3633 size_t occluded_tile_count_on_pending = 0u;
3634 size_t occluded_tile_count_on_active = 0u;
3635 size_t occluded_tile_count_on_both = 0u;
3636 for (PictureLayerTiling::CoverageIterator iter(
3638 pending_layer_->contents_scale_x(),
3639 gfx::Rect(layer_bounds));
3644 if (tile->is_occluded(PENDING_TREE))
3645 occluded_tile_count_on_pending++;
3646 if (tile->is_occluded(ACTIVE_TREE))
3647 occluded_tile_count_on_active++;
3648 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
3649 occluded_tile_count_on_both++;
3651 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
3652 occluded_tile_count_on_pending)
3654 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
3655 occluded_tile_count_on_active)
3657 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
3658 occluded_tile_count_on_both)
3662 // Verify number of occluded tiles on the active layer for each tiling.
3663 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
3664 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
3665 tiling->CreateAllTilesForTesting();
3667 size_t occluded_tile_count_on_pending = 0u;
3668 size_t occluded_tile_count_on_active = 0u;
3669 size_t occluded_tile_count_on_both = 0u;
3670 for (PictureLayerTiling::CoverageIterator iter(
3672 pending_layer_->contents_scale_x(),
3673 gfx::Rect(layer_bounds));
3678 if (tile->is_occluded(PENDING_TREE))
3679 occluded_tile_count_on_pending++;
3680 if (tile->is_occluded(ACTIVE_TREE))
3681 occluded_tile_count_on_active++;
3682 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
3683 occluded_tile_count_on_both++;
3685 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
3686 occluded_tile_count_on_pending)
3688 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
3689 occluded_tile_count_on_active)
3691 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
3692 occluded_tile_count_on_both)
3696 std::vector<Tile*> all_tiles;
3697 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3699 tiling_iterator != tilings.end();
3700 ++tiling_iterator) {
3701 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3702 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
3705 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
3707 VerifyEvictionConsidersOcclusion(pending_layer_,
3708 total_expected_occluded_tile_count);
3709 VerifyEvictionConsidersOcclusion(active_layer_,
3710 total_expected_occluded_tile_count);