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/quads/tile_draw_quad.h"
17 #include "cc/test/begin_frame_args_test.h"
18 #include "cc/test/fake_content_layer_client.h"
19 #include "cc/test/fake_impl_proxy.h"
20 #include "cc/test/fake_layer_tree_host_impl.h"
21 #include "cc/test/fake_output_surface.h"
22 #include "cc/test/fake_picture_layer_impl.h"
23 #include "cc/test/fake_picture_pile_impl.h"
24 #include "cc/test/geometry_test_utils.h"
25 #include "cc/test/impl_side_painting_settings.h"
26 #include "cc/test/layer_test_common.h"
27 #include "cc/test/test_shared_bitmap_manager.h"
28 #include "cc/test/test_web_graphics_context_3d.h"
29 #include "cc/trees/layer_tree_impl.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/gfx/geometry/rect_conversions.h"
32 #include "ui/gfx/geometry/size_conversions.h"
37 class MockCanvas : public SkCanvas {
39 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
41 void drawRect(const SkRect& rect, const SkPaint& paint) override {
42 // Capture calls before SkCanvas quickReject() kicks in.
43 rects_.push_back(rect);
46 std::vector<SkRect> rects_;
49 class NoLowResTilingsSettings : public ImplSidePaintingSettings {};
51 class LowResTilingsSettings : public ImplSidePaintingSettings {
53 LowResTilingsSettings() { create_low_res_tiling = true; }
56 class PictureLayerImplTest : public testing::Test {
58 PictureLayerImplTest()
59 : proxy_(base::MessageLoopProxy::current()),
60 host_impl_(LowResTilingsSettings(), &proxy_, &shared_bitmap_manager_),
62 pending_layer_(nullptr),
63 old_pending_layer_(nullptr),
64 active_layer_(nullptr) {}
66 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
67 : proxy_(base::MessageLoopProxy::current()),
68 host_impl_(settings, &proxy_, &shared_bitmap_manager_),
71 virtual ~PictureLayerImplTest() {
74 virtual void SetUp() override {
78 virtual void InitializeRenderer() {
79 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d());
82 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
83 gfx::Size tile_size(100, 100);
85 scoped_refptr<FakePicturePileImpl> pending_pile =
86 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
87 scoped_refptr<FakePicturePileImpl> active_pile =
88 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
90 SetupTrees(pending_pile, active_pile);
94 host_impl_.ActivateSyncTree();
95 CHECK(!host_impl_.pending_tree());
96 CHECK(host_impl_.recycle_tree());
97 old_pending_layer_ = pending_layer_;
98 pending_layer_ = nullptr;
99 active_layer_ = static_cast<FakePictureLayerImpl*>(
100 host_impl_.active_tree()->LayerById(id_));
103 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
104 const gfx::Size& tile_size) {
105 SetupDefaultTrees(layer_bounds);
106 pending_layer_->set_fixed_tile_size(tile_size);
107 active_layer_->set_fixed_tile_size(tile_size);
111 scoped_refptr<PicturePileImpl> pending_pile,
112 scoped_refptr<PicturePileImpl> active_pile) {
113 SetupPendingTree(active_pile);
115 SetupPendingTree(pending_pile);
118 void CreateHighLowResAndSetAllTilesVisible() {
119 // Active layer must get updated first so pending layer can share from it.
120 active_layer_->CreateDefaultTilingsAndTiles();
121 active_layer_->SetAllTilesVisible();
122 pending_layer_->CreateDefaultTilingsAndTiles();
123 pending_layer_->SetAllTilesVisible();
126 void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
127 active_layer_->AddTiling(2.3f);
128 active_layer_->AddTiling(1.0f);
129 active_layer_->AddTiling(0.5f);
130 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
131 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
132 pending_layer_->set_invalidation(invalidation);
133 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
134 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
137 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
138 host_impl_.CreatePendingTree();
139 host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
140 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
142 // Steal from the recycled tree.
143 scoped_ptr<LayerImpl> old_pending_root = pending_tree->DetachLayerTree();
144 DCHECK_IMPLIES(old_pending_root, old_pending_root->id() == id_);
146 scoped_ptr<FakePictureLayerImpl> pending_layer;
147 if (old_pending_root) {
149 static_cast<FakePictureLayerImpl*>(old_pending_root.release()));
150 pending_layer->SetPile(pile);
153 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
154 pending_layer->SetDrawsContent(true);
156 // The bounds() just mirror the pile size.
157 pending_layer->SetBounds(pending_layer->pile()->tiling_size());
158 pending_tree->SetRootLayer(pending_layer.Pass());
160 pending_layer_ = static_cast<FakePictureLayerImpl*>(
161 host_impl_.pending_tree()->LayerById(id_));
162 pending_layer_->DoPostCommitInitializationIfNeeded();
165 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
166 float ideal_contents_scale,
167 float device_scale_factor,
168 float page_scale_factor,
169 float maximum_animation_contents_scale,
170 bool animating_transform_to_screen) {
171 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
172 layer->draw_properties().device_scale_factor = device_scale_factor;
173 layer->draw_properties().page_scale_factor = page_scale_factor;
174 layer->draw_properties().maximum_animation_contents_scale =
175 maximum_animation_contents_scale;
176 layer->draw_properties().screen_space_transform_is_animating =
177 animating_transform_to_screen;
178 bool resourceless_software_draw = false;
179 layer->UpdateTiles(Occlusion(), resourceless_software_draw);
181 static void VerifyAllTilesExistAndHavePile(
182 const PictureLayerTiling* tiling,
183 PicturePileImpl* pile) {
184 for (PictureLayerTiling::CoverageIterator iter(
186 tiling->contents_scale(),
187 gfx::Rect(tiling->tiling_size()));
191 EXPECT_EQ(pile, iter->raster_source());
195 void SetContentsScaleOnBothLayers(float contents_scale,
196 float device_scale_factor,
197 float page_scale_factor,
198 float maximum_animation_contents_scale,
199 bool animating_transform) {
200 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
204 maximum_animation_contents_scale,
205 animating_transform);
207 SetupDrawPropertiesAndUpdateTiles(active_layer_,
211 maximum_animation_contents_scale,
212 animating_transform);
215 void ResetTilingsAndRasterScales() {
216 pending_layer_->ReleaseResources();
217 active_layer_->ReleaseResources();
220 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
221 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
222 for (size_t i = 0; i < tiles.size(); ++i)
223 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
224 EXPECT_GT(tiles.size(), 0u);
227 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
228 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
229 for (size_t i = 0; i < tiles.size(); ++i)
230 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
231 EXPECT_GT(tiles.size(), 0u);
235 void TestTileGridAlignmentCommon() {
236 // Layer to span 4 raster tiles in x and in y
237 ImplSidePaintingSettings settings;
238 gfx::Size layer_size(
239 settings.default_tile_size.width() * 7 / 2,
240 settings.default_tile_size.height() * 7 / 2);
242 scoped_refptr<FakePicturePileImpl> pending_pile =
243 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
244 scoped_refptr<FakePicturePileImpl> active_pile =
245 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
247 SetupTrees(pending_pile, active_pile);
249 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
251 // Add 1x1 rects at the centers of each tile, then re-record pile contents
252 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
253 std::vector<Tile*> tiles =
254 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
255 EXPECT_EQ(16u, tiles.size());
256 std::vector<SkRect> rects;
257 std::vector<Tile*>::const_iterator tile_iter;
258 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
259 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
260 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
261 active_pile->add_draw_rect(rect);
262 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
264 // Force re-record with newly injected content
265 active_pile->RemoveRecordingAt(0, 0);
266 active_pile->AddRecordingAt(0, 0);
268 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
269 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
270 MockCanvas mock_canvas(1000, 1000);
271 active_pile->RasterDirect(&mock_canvas, (*tile_iter)->content_rect(),
274 // This test verifies that when drawing the contents of a specific tile
275 // at content scale 1.0, the playback canvas never receives content from
276 // neighboring tiles which indicates that the tile grid embedded in
277 // SkPicture is perfectly aligned with the compositor's tiles.
278 EXPECT_EQ(1u, mock_canvas.rects_.size());
279 EXPECT_EQ(*rect_iter, mock_canvas.rects_[0]);
284 void TestQuadsForSolidColor(bool test_for_solid);
286 FakeImplProxy proxy_;
287 TestSharedBitmapManager shared_bitmap_manager_;
288 FakeLayerTreeHostImpl host_impl_;
290 FakePictureLayerImpl* pending_layer_;
291 FakePictureLayerImpl* old_pending_layer_;
292 FakePictureLayerImpl* active_layer_;
295 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
298 TEST_F(PictureLayerImplTest, TileGridAlignment) {
299 host_impl_.SetDeviceScaleFactor(1.f);
300 TestTileGridAlignmentCommon();
303 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
304 host_impl_.SetDeviceScaleFactor(2.f);
305 TestTileGridAlignmentCommon();
308 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
309 gfx::Size tile_size(100, 100);
310 gfx::Size layer_bounds(400, 400);
312 scoped_refptr<FakePicturePileImpl> pending_pile =
313 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
314 scoped_refptr<FakePicturePileImpl> active_pile =
315 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
317 SetupTrees(pending_pile, active_pile);
320 AddDefaultTilingsWithInvalidation(invalidation);
322 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
323 active_layer_->tilings()->num_tilings());
325 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
326 EXPECT_GT(tilings->num_tilings(), 0u);
327 for (size_t i = 0; i < tilings->num_tilings(); ++i)
328 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
331 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
332 base::TimeTicks time_ticks;
333 time_ticks += base::TimeDelta::FromMilliseconds(1);
334 host_impl_.SetCurrentBeginFrameArgs(
335 CreateBeginFrameArgsForTesting(time_ticks));
336 gfx::Size tile_size(100, 100);
337 gfx::Size layer_bounds(400, 400);
339 scoped_refptr<FakePicturePileImpl> pending_pile =
340 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
341 scoped_refptr<FakePicturePileImpl> active_pile =
342 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
344 SetupTrees(pending_pile, active_pile);
347 AddDefaultTilingsWithInvalidation(invalidation);
348 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
350 time_ticks += base::TimeDelta::FromMilliseconds(200);
351 host_impl_.SetCurrentBeginFrameArgs(
352 CreateBeginFrameArgsForTesting(time_ticks));
354 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
355 // identify transform for tile priority.
356 bool resourceless_software_draw = false;
357 gfx::Rect viewport = gfx::Rect(layer_bounds),
358 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
359 gfx::Transform transform, transform_for_tile_priority;
361 host_impl_.SetExternalDrawConstraints(transform,
364 viewport_rect_for_tile_priority,
365 transform_for_tile_priority,
366 resourceless_software_draw);
367 host_impl_.active_tree()->UpdateDrawProperties();
369 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
370 viewport_rect_for_tile_priority;
372 // Verify the viewport rect for tile priority is used in picture layer tiling.
373 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
374 active_layer_->GetViewportForTilePriorityInContentSpace());
375 PictureLayerTilingSet* tilings = active_layer_->tilings();
376 for (size_t i = 0; i < tilings->num_tilings(); i++) {
377 PictureLayerTiling* tiling = tilings->tiling_at(i);
379 tiling->GetCurrentVisibleRectForTesting(),
380 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
381 tiling->contents_scale()));
384 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
385 // screen space and the transform for tile priority is translated and
386 // rotated. The actual viewport for tile priority used by PictureLayerImpl
387 // should be (200, 200, 100, 100) applied with the said transform.
388 time_ticks += base::TimeDelta::FromMilliseconds(200);
389 host_impl_.SetCurrentBeginFrameArgs(
390 CreateBeginFrameArgsForTesting(time_ticks));
392 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
393 transform_for_tile_priority.Translate(100, 100);
394 transform_for_tile_priority.Rotate(45);
395 host_impl_.SetExternalDrawConstraints(transform,
398 viewport_rect_for_tile_priority,
399 transform_for_tile_priority,
400 resourceless_software_draw);
401 host_impl_.active_tree()->UpdateDrawProperties();
403 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
404 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
405 EXPECT_TRUE(success);
407 // Note that we don't clip this to the layer bounds, since it is expected that
408 // the rect will sometimes be outside of the layer bounds. If we clip to
409 // bounds, then tile priorities will end up being incorrect in cases of fully
411 viewport_rect_for_tile_priority_in_view_space =
412 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
413 screen_to_view, viewport_rect_for_tile_priority));
415 EXPECT_EQ(viewport_rect_for_tile_priority_in_view_space,
416 active_layer_->GetViewportForTilePriorityInContentSpace());
417 tilings = active_layer_->tilings();
418 for (size_t i = 0; i < tilings->num_tilings(); i++) {
419 PictureLayerTiling* tiling = tilings->tiling_at(i);
421 tiling->GetCurrentVisibleRectForTesting(),
422 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
423 tiling->contents_scale()));
427 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
428 base::TimeTicks time_ticks;
429 time_ticks += base::TimeDelta::FromMilliseconds(1);
430 host_impl_.SetCurrentBeginFrameArgs(
431 CreateBeginFrameArgsForTesting(time_ticks));
433 gfx::Size tile_size(100, 100);
434 gfx::Size layer_bounds(400, 400);
436 scoped_refptr<FakePicturePileImpl> pending_pile =
437 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
438 scoped_refptr<FakePicturePileImpl> active_pile =
439 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
441 SetupTrees(pending_pile, active_pile);
444 AddDefaultTilingsWithInvalidation(invalidation);
445 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
447 // UpdateTiles with valid viewport. Should update tile viewport.
448 // Note viewport is considered invalid if and only if in resourceless
450 bool resourceless_software_draw = false;
451 gfx::Rect viewport = gfx::Rect(layer_bounds);
452 gfx::Transform transform;
453 host_impl_.SetExternalDrawConstraints(transform,
458 resourceless_software_draw);
459 active_layer_->draw_properties().visible_content_rect = viewport;
460 active_layer_->draw_properties().screen_space_transform = transform;
461 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
463 gfx::Rect visible_rect_for_tile_priority =
464 active_layer_->visible_rect_for_tile_priority();
465 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
466 gfx::Transform screen_space_transform_for_tile_priority =
467 active_layer_->screen_space_transform();
469 // Expand viewport and set it as invalid for prioritizing tiles.
470 // Should update viewport and transform, but not update visible rect.
471 time_ticks += base::TimeDelta::FromMilliseconds(200);
472 host_impl_.SetCurrentBeginFrameArgs(
473 CreateBeginFrameArgsForTesting(time_ticks));
474 resourceless_software_draw = true;
475 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
476 transform.Translate(1.f, 1.f);
477 active_layer_->draw_properties().visible_content_rect = viewport;
478 active_layer_->draw_properties().screen_space_transform = transform;
479 host_impl_.SetExternalDrawConstraints(transform,
484 resourceless_software_draw);
485 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
487 // Transform for tile priority is updated.
488 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
489 active_layer_->screen_space_transform());
490 // Visible rect for tile priority retains old value.
491 EXPECT_EQ(visible_rect_for_tile_priority,
492 active_layer_->visible_rect_for_tile_priority());
494 // Keep expanded viewport but mark it valid. Should update tile viewport.
495 time_ticks += base::TimeDelta::FromMilliseconds(200);
496 host_impl_.SetCurrentBeginFrameArgs(
497 CreateBeginFrameArgsForTesting(time_ticks));
498 resourceless_software_draw = false;
499 host_impl_.SetExternalDrawConstraints(transform,
504 resourceless_software_draw);
505 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
507 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
508 active_layer_->screen_space_transform());
509 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
512 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
513 gfx::Size tile_size(100, 100);
514 gfx::Size layer_bounds(400, 400);
515 gfx::Rect layer_invalidation(150, 200, 30, 180);
517 scoped_refptr<FakePicturePileImpl> pending_pile =
518 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
519 scoped_refptr<FakePicturePileImpl> active_pile =
520 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
522 SetupTrees(pending_pile, active_pile);
524 Region invalidation(layer_invalidation);
525 AddDefaultTilingsWithInvalidation(invalidation);
527 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
528 EXPECT_GT(tilings->num_tilings(), 0u);
529 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
530 const PictureLayerTiling* tiling = tilings->tiling_at(i);
531 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
533 tiling->contents_scale());
534 for (PictureLayerTiling::CoverageIterator iter(
536 tiling->contents_scale(),
537 gfx::Rect(tiling->tiling_size()));
541 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
542 if (iter.geometry_rect().Intersects(content_invalidation))
543 EXPECT_EQ(pending_pile.get(), iter->raster_source());
545 EXPECT_EQ(active_pile.get(), iter->raster_source());
550 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
551 gfx::Size tile_size(90, 80);
552 gfx::Size layer_bounds(300, 500);
554 scoped_refptr<FakePicturePileImpl> pending_pile =
555 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
556 scoped_refptr<FakePicturePileImpl> active_pile =
557 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
559 SetupTrees(pending_pile, active_pile);
561 Region invalidation((gfx::Rect(layer_bounds)));
562 AddDefaultTilingsWithInvalidation(invalidation);
564 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
565 active_layer_->tilings()->num_tilings());
567 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
568 EXPECT_GT(tilings->num_tilings(), 0u);
569 for (size_t i = 0; i < tilings->num_tilings(); ++i)
570 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
573 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
574 gfx::Size tile_size(90, 80);
575 gfx::Size active_layer_bounds(300, 500);
576 gfx::Size pending_layer_bounds(400, 800);
578 scoped_refptr<FakePicturePileImpl> pending_pile =
579 FakePicturePileImpl::CreateFilledPile(tile_size,
580 pending_layer_bounds);
581 scoped_refptr<FakePicturePileImpl> active_pile =
582 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
584 SetupTrees(pending_pile, active_pile);
585 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
588 AddDefaultTilingsWithInvalidation(invalidation);
590 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
591 EXPECT_GT(tilings->num_tilings(), 0u);
592 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
593 const PictureLayerTiling* tiling = tilings->tiling_at(i);
594 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
595 gfx::Rect(active_layer_bounds),
596 tiling->contents_scale());
597 for (PictureLayerTiling::CoverageIterator iter(
599 tiling->contents_scale(),
600 gfx::Rect(tiling->tiling_size()));
604 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
605 std::vector<Tile*> active_tiles =
606 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
607 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
608 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
609 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
610 active_tiles[0]->content_rect().size() !=
611 pending_tiles[0]->content_rect().size()) {
612 EXPECT_EQ(pending_pile.get(), iter->raster_source());
614 EXPECT_EQ(active_pile.get(), iter->raster_source());
620 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
621 gfx::Size tile_size(400, 400);
622 gfx::Size layer_bounds(1300, 1900);
624 scoped_refptr<FakePicturePileImpl> pending_pile =
625 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
626 scoped_refptr<FakePicturePileImpl> active_pile =
627 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
629 // Fill in some of active pile, but more of pending pile.
631 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
632 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
634 pending_pile->AddRecordingAt(x, y);
635 active_pile->AddRecordingAt(x, y);
639 pending_pile->AddRecordingAt(x, y);
644 SetupTrees(pending_pile, active_pile);
646 AddDefaultTilingsWithInvalidation(invalidation);
648 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
649 EXPECT_GT(tilings->num_tilings(), 0u);
650 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
651 const PictureLayerTiling* tiling = tilings->tiling_at(i);
653 for (PictureLayerTiling::CoverageIterator iter(
655 tiling->contents_scale(),
656 gfx::Rect(tiling->tiling_size()));
659 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
660 // Ensure there is a recording for this tile.
661 bool in_pending = pending_pile->CoversRect(iter.full_tile_geometry_rect(),
662 tiling->contents_scale());
663 bool in_active = active_pile->CoversRect(iter.full_tile_geometry_rect(),
664 tiling->contents_scale());
666 if (in_pending && !in_active)
667 EXPECT_EQ(pending_pile.get(), iter->raster_source());
669 EXPECT_EQ(active_pile.get(), iter->raster_source());
676 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
677 gfx::Size tile_size(400, 400);
678 gfx::Size layer_bounds(1300, 1900);
680 scoped_refptr<FakePicturePileImpl> pending_pile =
681 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
682 scoped_refptr<FakePicturePileImpl> active_pile =
683 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
685 SetupTrees(pending_pile, active_pile);
687 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
689 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
692 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
693 gfx::Size tile_size(400, 400);
694 gfx::Size layer_bounds(1300, 1900);
696 scoped_refptr<FakePicturePileImpl> pending_pile =
697 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
698 scoped_refptr<FakePicturePileImpl> active_pile =
699 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
701 SetupTrees(pending_pile, active_pile);
702 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
704 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
705 EXPECT_LT(low_res_factor, 1.f);
707 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
708 6.f, // ideal contents scale
711 1.f, // maximum animation scale
713 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
715 pending_layer_->tilings()->tiling_at(0)->contents_scale());
716 EXPECT_FLOAT_EQ(6.f * low_res_factor,
717 pending_layer_->tilings()->tiling_at(1)->contents_scale());
719 // If we change the page scale factor, then we should get new tilings.
720 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
721 6.6f, // ideal contents scale
724 1.f, // maximum animation scale
726 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
727 EXPECT_FLOAT_EQ(6.6f,
728 pending_layer_->tilings()->tiling_at(0)->contents_scale());
729 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
730 pending_layer_->tilings()->tiling_at(2)->contents_scale());
732 // If we change the device scale factor, then we should get new tilings.
733 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
734 7.26f, // ideal contents scale
735 3.3f, // device scale
737 1.f, // maximum animation scale
739 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
740 EXPECT_FLOAT_EQ(7.26f,
741 pending_layer_->tilings()->tiling_at(0)->contents_scale());
742 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
743 pending_layer_->tilings()->tiling_at(3)->contents_scale());
745 // If we change the device scale factor, but end up at the same total scale
746 // factor somehow, then we don't get new tilings.
747 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
748 7.26f, // ideal contents scale
749 2.2f, // device scale
751 1.f, // maximum animation scale
753 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
754 EXPECT_FLOAT_EQ(7.26f,
755 pending_layer_->tilings()->tiling_at(0)->contents_scale());
756 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
757 pending_layer_->tilings()->tiling_at(3)->contents_scale());
760 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
761 // This test makes sure that if a layer can have tilings, then a commit makes
762 // it not able to have tilings (empty size), and then a future commit that
763 // makes it valid again should be able to create tilings.
764 gfx::Size tile_size(400, 400);
765 gfx::Size layer_bounds(1300, 1900);
767 scoped_refptr<FakePicturePileImpl> empty_pile =
768 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
769 scoped_refptr<FakePicturePileImpl> valid_pile =
770 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
772 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
773 EXPECT_LT(low_res_factor, 1.f);
775 float high_res_scale = 1.3f;
776 float low_res_scale = high_res_scale * low_res_factor;
777 float device_scale = 1.7f;
778 float page_scale = 3.2f;
779 float maximum_animation_scale = 1.f;
781 SetupPendingTree(valid_pile);
782 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
786 maximum_animation_scale,
788 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
789 EXPECT_FLOAT_EQ(high_res_scale,
790 pending_layer_->HighResTiling()->contents_scale());
791 EXPECT_FLOAT_EQ(low_res_scale,
792 pending_layer_->LowResTiling()->contents_scale());
795 SetupPendingTree(empty_pile);
796 EXPECT_FALSE(pending_layer_->CanHaveTilings());
797 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
801 maximum_animation_scale,
803 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
804 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
807 EXPECT_FALSE(active_layer_->CanHaveTilings());
808 SetupDrawPropertiesAndUpdateTiles(active_layer_,
812 maximum_animation_scale,
814 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
816 SetupPendingTree(valid_pile);
817 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
821 maximum_animation_scale,
823 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
824 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
825 EXPECT_FLOAT_EQ(high_res_scale,
826 pending_layer_->HighResTiling()->contents_scale());
827 EXPECT_FLOAT_EQ(low_res_scale,
828 pending_layer_->LowResTiling()->contents_scale());
831 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
832 gfx::Size tile_size(400, 400);
833 gfx::Size layer_bounds(1300, 1900);
835 // Set up the high and low res tilings before pinch zoom.
836 scoped_refptr<FakePicturePileImpl> pending_pile =
837 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
838 scoped_refptr<FakePicturePileImpl> active_pile =
839 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
841 SetupTrees(pending_pile, active_pile);
842 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
843 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
844 host_impl_.PinchGestureBegin();
845 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
846 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
847 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
850 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
851 gfx::Size tile_size(400, 400);
852 gfx::Size layer_bounds(1300, 1900);
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 // Set up the high and low res tilings before pinch zoom.
860 SetupTrees(pending_pile, active_pile);
861 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
862 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
863 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
864 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
865 EXPECT_FLOAT_EQ(2.0f,
866 active_layer_->tilings()->tiling_at(0)->contents_scale());
867 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
868 active_layer_->tilings()->tiling_at(1)->contents_scale());
870 // Start a pinch gesture.
871 host_impl_.PinchGestureBegin();
873 // Zoom out by a small amount. We should create a tiling at half
874 // the scale (2/kMaxScaleRatioDuringPinch).
875 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
876 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
877 EXPECT_FLOAT_EQ(2.0f,
878 active_layer_->tilings()->tiling_at(0)->contents_scale());
879 EXPECT_FLOAT_EQ(1.0f,
880 active_layer_->tilings()->tiling_at(1)->contents_scale());
881 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
882 active_layer_->tilings()->tiling_at(2)->contents_scale());
884 // Zoom out further, close to our low-res scale factor. We should
885 // use that tiling as high-res, and not create a new tiling.
886 SetContentsScaleOnBothLayers(
887 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
888 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
890 // Zoom in a lot now. Since we increase by increments of
891 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
892 // and then finally create a new tiling at 4.0.
893 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
894 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
895 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
896 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
897 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
898 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
899 EXPECT_FLOAT_EQ(4.0f,
900 active_layer_->tilings()->tiling_at(0)->contents_scale());
903 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
904 gfx::Size tile_size(300, 300);
905 gfx::Size layer_bounds(2600, 3800);
907 scoped_refptr<FakePicturePileImpl> pending_pile =
908 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
909 scoped_refptr<FakePicturePileImpl> active_pile =
910 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
912 // Set up the high and low res tilings before pinch zoom.
913 SetupTrees(pending_pile, active_pile);
914 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
915 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
916 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
917 EXPECT_FLOAT_EQ(0.24f,
918 active_layer_->tilings()->tiling_at(0)->contents_scale());
919 EXPECT_FLOAT_EQ(0.0625f,
920 active_layer_->tilings()->tiling_at(1)->contents_scale());
922 // Start a pinch gesture.
923 host_impl_.PinchGestureBegin();
925 // Zoom out by a small amount. We should create a tiling at half
926 // the scale (1/kMaxScaleRatioDuringPinch).
927 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
928 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
929 EXPECT_FLOAT_EQ(0.24f,
930 active_layer_->tilings()->tiling_at(0)->contents_scale());
931 EXPECT_FLOAT_EQ(0.12f,
932 active_layer_->tilings()->tiling_at(1)->contents_scale());
933 EXPECT_FLOAT_EQ(0.0625,
934 active_layer_->tilings()->tiling_at(2)->contents_scale());
936 // Zoom out further, close to our low-res scale factor. We should
937 // use that tiling as high-res, and not create a new tiling.
938 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
939 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
941 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
942 // because 0.125(desired_scale) is within the ratio(1.2)
943 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
944 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
947 TEST_F(PictureLayerImplTest, CleanUpTilings) {
948 gfx::Size tile_size(400, 400);
949 gfx::Size layer_bounds(1300, 1900);
951 scoped_refptr<FakePicturePileImpl> pending_pile =
952 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
953 scoped_refptr<FakePicturePileImpl> active_pile =
954 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
956 std::vector<PictureLayerTiling*> used_tilings;
958 SetupTrees(pending_pile, active_pile);
959 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
961 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
962 EXPECT_LT(low_res_factor, 1.f);
964 float device_scale = 1.7f;
965 float page_scale = 3.2f;
968 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
969 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
971 // We only have ideal tilings, so they aren't removed.
972 used_tilings.clear();
973 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
974 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
976 host_impl_.PinchGestureBegin();
978 // Changing the ideal but not creating new tilings.
981 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
982 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
984 // The tilings are still our target scale, so they aren't removed.
985 used_tilings.clear();
986 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
987 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
989 host_impl_.PinchGestureEnd();
991 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
994 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
995 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
998 active_layer_->tilings()->tiling_at(1)->contents_scale());
1000 1.f * low_res_factor,
1001 active_layer_->tilings()->tiling_at(3)->contents_scale());
1003 // Mark the non-ideal tilings as used. They won't be removed.
1004 used_tilings.clear();
1005 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1006 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1007 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1008 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1010 // Now move the ideal scale to 0.5. Our target stays 1.2.
1011 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1013 // The high resolution tiling is between target and ideal, so is not
1014 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1015 used_tilings.clear();
1016 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1017 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1019 // Now move the ideal scale to 1.0. Our target stays 1.2.
1020 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1022 // All the tilings are between are target and the ideal, so they are not
1024 used_tilings.clear();
1025 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1026 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1028 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1029 SetupDrawPropertiesAndUpdateTiles(
1030 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1032 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1033 // in the range [1.0,1.2] and are kept.
1034 used_tilings.clear();
1035 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1036 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1038 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1040 SetupDrawPropertiesAndUpdateTiles(
1041 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1043 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1044 // target raster scale. But it is in our used tilings set, so nothing is
1046 used_tilings.clear();
1047 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1048 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1049 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1051 // If we remove it from our used tilings set, it is outside the range to keep
1052 // so it is deleted.
1053 used_tilings.clear();
1054 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1055 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1058 #define EXPECT_BOTH_EQ(expression, x) \
1060 EXPECT_EQ(x, pending_layer_->expression); \
1061 EXPECT_EQ(x, active_layer_->expression); \
1064 #define EXPECT_BOTH_NE(expression, x) \
1066 EXPECT_NE(x, pending_layer_->expression); \
1067 EXPECT_NE(x, active_layer_->expression); \
1070 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1071 // Make sure this layer covers multiple tiles, since otherwise low
1072 // res won't get created because it is too small.
1073 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1074 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1075 // Avoid max untiled layer size heuristics via fixed tile size.
1076 pending_layer_->set_fixed_tile_size(tile_size);
1077 active_layer_->set_fixed_tile_size(tile_size);
1079 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1080 float contents_scale = 1.f;
1081 float device_scale = 1.f;
1082 float page_scale = 1.f;
1083 float maximum_animation_scale = 1.f;
1084 bool animating_transform = true;
1086 // Animating, so don't create low res even if there isn't one already.
1087 SetContentsScaleOnBothLayers(contents_scale,
1090 maximum_animation_scale,
1091 animating_transform);
1092 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1093 EXPECT_BOTH_EQ(num_tilings(), 1u);
1095 // Stop animating, low res gets created.
1096 animating_transform = false;
1097 SetContentsScaleOnBothLayers(contents_scale,
1100 maximum_animation_scale,
1101 animating_transform);
1102 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1103 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1104 EXPECT_BOTH_EQ(num_tilings(), 2u);
1106 // Page scale animation, new high res, but no low res. We still have
1107 // a tiling at the previous scale, it's just not marked as low res.
1108 contents_scale = 2.f;
1110 maximum_animation_scale = 2.f;
1111 animating_transform = true;
1112 SetContentsScaleOnBothLayers(contents_scale,
1115 maximum_animation_scale,
1116 animating_transform);
1117 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1118 EXPECT_FALSE(active_layer_->LowResTiling());
1119 EXPECT_FALSE(pending_layer_->LowResTiling());
1120 EXPECT_BOTH_EQ(num_tilings(), 3u);
1122 // Stop animating, new low res gets created for final page scale.
1123 animating_transform = false;
1124 SetContentsScaleOnBothLayers(contents_scale,
1127 maximum_animation_scale,
1128 animating_transform);
1129 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1130 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1131 EXPECT_BOTH_EQ(num_tilings(), 4u);
1134 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1135 gfx::Size layer_bounds(host_impl_.settings().default_tile_size);
1136 gfx::Size tile_size(100, 100);
1138 scoped_refptr<FakePicturePileImpl> pending_pile =
1139 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1140 scoped_refptr<FakePicturePileImpl> active_pile =
1141 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1143 SetupTrees(pending_pile, active_pile);
1145 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1146 float device_scale = 1.f;
1147 float page_scale = 1.f;
1148 float maximum_animation_scale = 1.f;
1149 bool animating_transform = false;
1151 // Contents exactly fit on one tile at scale 1, no low res.
1152 float contents_scale = 1.f;
1153 SetContentsScaleOnBothLayers(contents_scale,
1156 maximum_animation_scale,
1157 animating_transform);
1158 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1159 EXPECT_BOTH_EQ(num_tilings(), 1u);
1161 ResetTilingsAndRasterScales();
1163 // Contents that are smaller than one tile, no low res.
1164 contents_scale = 0.123f;
1165 SetContentsScaleOnBothLayers(contents_scale,
1168 maximum_animation_scale,
1169 animating_transform);
1170 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1171 EXPECT_BOTH_EQ(num_tilings(), 1u);
1173 ResetTilingsAndRasterScales();
1175 // Any content bounds that would create more than one tile will
1176 // generate a low res tiling.
1177 contents_scale = 2.5f;
1178 SetContentsScaleOnBothLayers(contents_scale,
1181 maximum_animation_scale,
1182 animating_transform);
1183 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1184 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1185 contents_scale * low_res_factor);
1186 EXPECT_BOTH_EQ(num_tilings(), 2u);
1188 ResetTilingsAndRasterScales();
1190 // Mask layers dont create low res since they always fit on one tile.
1191 pending_pile->SetIsMask(true);
1192 active_pile->SetIsMask(true);
1193 SetContentsScaleOnBothLayers(contents_scale,
1196 maximum_animation_scale,
1197 animating_transform);
1198 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1199 EXPECT_BOTH_EQ(num_tilings(), 1u);
1202 TEST_F(PictureLayerImplTest, HugeMasksDontGetTiles) {
1203 gfx::Size tile_size(100, 100);
1205 scoped_refptr<FakePicturePileImpl> valid_pile =
1206 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1207 valid_pile->SetIsMask(true);
1208 SetupPendingTree(valid_pile);
1210 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1211 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1212 EXPECT_EQ(1u, pending_layer_->num_tilings());
1214 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1215 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1216 pending_layer_->HighResTiling()->AllTilesForTesting());
1220 // Mask layers have a tiling with a single tile in it.
1221 EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1222 // The mask resource exists.
1223 ResourceProvider::ResourceId mask_resource_id;
1224 gfx::Size mask_texture_size;
1225 active_layer_->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1226 EXPECT_NE(0u, mask_resource_id);
1227 EXPECT_EQ(mask_texture_size, active_layer_->bounds());
1229 // Resize larger than the max texture size.
1230 int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1231 scoped_refptr<FakePicturePileImpl> huge_pile =
1232 FakePicturePileImpl::CreateFilledPile(
1233 tile_size, gfx::Size(max_texture_size + 1, 10));
1234 huge_pile->SetIsMask(true);
1235 SetupPendingTree(huge_pile);
1237 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1238 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1239 EXPECT_EQ(1u, pending_layer_->num_tilings());
1241 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1242 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1243 pending_layer_->HighResTiling()->AllTilesForTesting());
1247 // Mask layers have a tiling, but there should be no tiles in it.
1248 EXPECT_EQ(0u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1249 // The mask resource is empty.
1250 active_layer_->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1251 EXPECT_EQ(0u, mask_resource_id);
1254 TEST_F(PictureLayerImplTest, ScaledMaskLayer) {
1255 gfx::Size tile_size(100, 100);
1257 scoped_refptr<FakePicturePileImpl> valid_pile =
1258 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1259 valid_pile->SetIsMask(true);
1260 SetupPendingTree(valid_pile);
1262 float ideal_contents_scale = 1.3f;
1263 SetupDrawPropertiesAndUpdateTiles(
1264 pending_layer_, ideal_contents_scale, 1.f, 1.f, 1.f, false);
1265 EXPECT_EQ(ideal_contents_scale,
1266 pending_layer_->HighResTiling()->contents_scale());
1267 EXPECT_EQ(1u, pending_layer_->num_tilings());
1269 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1270 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1271 pending_layer_->HighResTiling()->AllTilesForTesting());
1275 // Mask layers have a tiling with a single tile in it.
1276 EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1277 // The mask resource exists.
1278 ResourceProvider::ResourceId mask_resource_id;
1279 gfx::Size mask_texture_size;
1280 active_layer_->GetContentsResourceId(&mask_resource_id, &mask_texture_size);
1281 EXPECT_NE(0u, mask_resource_id);
1282 gfx::Size expected_mask_texture_size = gfx::ToCeiledSize(
1283 gfx::ScaleSize(active_layer_->bounds(), ideal_contents_scale));
1284 EXPECT_EQ(mask_texture_size, expected_mask_texture_size);
1287 TEST_F(PictureLayerImplTest, ReleaseResources) {
1288 gfx::Size tile_size(400, 400);
1289 gfx::Size layer_bounds(1300, 1900);
1291 scoped_refptr<FakePicturePileImpl> pending_pile =
1292 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1293 scoped_refptr<FakePicturePileImpl> active_pile =
1294 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1296 SetupTrees(pending_pile, active_pile);
1297 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1299 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1300 1.3f, // ideal contents scale
1301 2.7f, // device scale
1303 1.f, // maximum animation scale
1305 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1307 // All tilings should be removed when losing output surface.
1308 active_layer_->ReleaseResources();
1309 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1310 pending_layer_->ReleaseResources();
1311 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1313 // This should create new tilings.
1314 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1315 1.3f, // ideal contents scale
1316 2.7f, // device scale
1318 1.f, // maximum animation scale
1320 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1323 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1324 // The default max tile size is larger than 400x400.
1325 gfx::Size tile_size(400, 400);
1326 gfx::Size layer_bounds(5000, 5000);
1328 scoped_refptr<FakePicturePileImpl> pending_pile =
1329 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1330 scoped_refptr<FakePicturePileImpl> active_pile =
1331 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1333 SetupTrees(pending_pile, active_pile);
1334 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1336 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1337 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1339 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1341 // The default value.
1342 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1343 host_impl_.settings().default_tile_size.ToString());
1345 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1346 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1347 tile->content_rect().size().ToString());
1349 pending_layer_->ReleaseResources();
1351 // Change the max texture size on the output surface context.
1352 scoped_ptr<TestWebGraphicsContext3D> context =
1353 TestWebGraphicsContext3D::Create();
1354 context->set_max_texture_size(140);
1355 host_impl_.DidLoseOutputSurface();
1356 host_impl_.InitializeRenderer(
1357 FakeOutputSurface::Create3d(context.Pass()).Pass());
1359 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1360 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1362 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1364 // Verify the tiles are not larger than the context's max texture size.
1365 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1366 EXPECT_GE(140, tile->content_rect().width());
1367 EXPECT_GE(140, tile->content_rect().height());
1370 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1371 // The default max tile size is larger than 400x400.
1372 gfx::Size tile_size(400, 400);
1373 gfx::Size layer_bounds(500, 500);
1375 scoped_refptr<FakePicturePileImpl> pending_pile =
1376 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1377 scoped_refptr<FakePicturePileImpl> active_pile =
1378 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1380 SetupTrees(pending_pile, active_pile);
1381 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1383 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1384 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1386 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1388 // The default value. The layer is smaller than this.
1389 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1390 host_impl_.settings().max_untiled_layer_size.ToString());
1392 // There should be a single tile since the layer is small.
1393 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1394 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1396 pending_layer_->ReleaseResources();
1398 // Change the max texture size on the output surface context.
1399 scoped_ptr<TestWebGraphicsContext3D> context =
1400 TestWebGraphicsContext3D::Create();
1401 context->set_max_texture_size(140);
1402 host_impl_.DidLoseOutputSurface();
1403 host_impl_.InitializeRenderer(
1404 FakeOutputSurface::Create3d(context.Pass()).Pass());
1406 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1407 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1409 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1411 // There should be more than one tile since the max texture size won't cover
1413 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1414 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1416 // Verify the tiles are not larger than the context's max texture size.
1417 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1418 EXPECT_GE(140, tile->content_rect().width());
1419 EXPECT_GE(140, tile->content_rect().height());
1422 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1423 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1425 gfx::Size tile_size(400, 400);
1426 gfx::Size layer_bounds(1300, 1900);
1428 scoped_refptr<FakePicturePileImpl> pending_pile =
1429 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1430 scoped_refptr<FakePicturePileImpl> active_pile =
1431 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1433 SetupTrees(pending_pile, active_pile);
1435 active_layer_->draw_properties().visible_content_rect =
1436 gfx::Rect(layer_bounds);
1438 gfx::Rect layer_invalidation(150, 200, 30, 180);
1439 Region invalidation(layer_invalidation);
1440 AddDefaultTilingsWithInvalidation(invalidation);
1442 AppendQuadsData data;
1443 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, nullptr);
1444 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1445 active_layer_->DidDraw(nullptr);
1447 ASSERT_EQ(1U, render_pass->quad_list.size());
1448 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1449 render_pass->quad_list.front()->material);
1452 TEST_F(PictureLayerImplTest, SolidColorLayerHasVisibleFullCoverage) {
1453 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1455 gfx::Size tile_size(1000, 1000);
1456 gfx::Size layer_bounds(1500, 1500);
1457 gfx::Rect visible_rect(250, 250, 1000, 1000);
1459 scoped_refptr<FakePicturePileImpl> pending_pile =
1460 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1461 scoped_refptr<FakePicturePileImpl> active_pile =
1462 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1464 pending_pile->set_is_solid_color(true);
1465 active_pile->set_is_solid_color(true);
1467 SetupTrees(pending_pile, active_pile);
1469 active_layer_->draw_properties().visible_content_rect = visible_rect;
1471 AppendQuadsData data;
1472 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1473 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1474 active_layer_->DidDraw(nullptr);
1476 Region remaining = visible_rect;
1477 for (const auto& quad : render_pass->quad_list) {
1478 EXPECT_TRUE(visible_rect.Contains(quad->rect));
1479 EXPECT_TRUE(remaining.Contains(quad->rect));
1480 remaining.Subtract(quad->rect);
1483 EXPECT_TRUE(remaining.IsEmpty());
1486 TEST_F(PictureLayerImplTest, TileScalesWithSolidColorPile) {
1487 gfx::Size layer_bounds(200, 200);
1488 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1489 scoped_refptr<FakePicturePileImpl> pending_pile =
1490 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1491 tile_size, layer_bounds);
1492 scoped_refptr<FakePicturePileImpl> active_pile =
1493 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1494 tile_size, layer_bounds);
1496 pending_pile->set_is_solid_color(false);
1497 active_pile->set_is_solid_color(true);
1498 SetupTrees(pending_pile, active_pile);
1499 // Solid color layer should not have tilings.
1500 ASSERT_FALSE(active_layer_->CanHaveTilings());
1502 // Update properties with solid color pile should not allow tilings at any
1504 host_impl_.active_tree()->UpdateDrawProperties();
1505 EXPECT_FALSE(active_layer_->CanHaveTilings());
1506 EXPECT_EQ(0.f, active_layer_->ideal_contents_scale());
1508 // Push non-solid-color pending pile makes active layer can have tilings.
1509 active_layer_->UpdatePile(pending_pile);
1510 ASSERT_TRUE(active_layer_->CanHaveTilings());
1512 // Update properties with non-solid color pile should allow tilings.
1513 host_impl_.active_tree()->UpdateDrawProperties();
1514 EXPECT_TRUE(active_layer_->CanHaveTilings());
1515 EXPECT_GT(active_layer_->ideal_contents_scale(), 0.f);
1518 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1519 gfx::Size tile_size(100, 100);
1520 gfx::Size layer_bounds(200, 200);
1522 scoped_refptr<FakePicturePileImpl> pending_pile =
1523 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1524 SetupPendingTree(pending_pile);
1526 gfx::Transform transform;
1527 gfx::Transform transform_for_tile_priority;
1528 bool resourceless_software_draw = false;
1529 gfx::Rect viewport(0, 0, 100, 200);
1530 host_impl_.SetExternalDrawConstraints(transform,
1535 resourceless_software_draw);
1537 pending_layer_->set_fixed_tile_size(tile_size);
1538 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1539 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1540 host_impl_.pending_tree()->UpdateDrawProperties();
1541 EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1543 base::TimeTicks time_ticks;
1544 time_ticks += base::TimeDelta::FromMilliseconds(1);
1545 host_impl_.SetCurrentBeginFrameArgs(
1546 CreateBeginFrameArgsForTesting(time_ticks));
1547 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
1548 EXPECT_EQ(HIGH_RESOLUTION, tiling->resolution());
1550 int num_visible = 0;
1551 int num_offscreen = 0;
1553 for (PictureLayerTiling::TilingRasterTileIterator iter(tiling); iter;
1555 const Tile* tile = *iter;
1557 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1558 EXPECT_TRUE(tile->required_for_activation());
1561 EXPECT_FALSE(tile->required_for_activation());
1566 EXPECT_GT(num_visible, 0);
1567 EXPECT_GT(num_offscreen, 0);
1570 TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1571 base::TimeTicks time_ticks;
1572 time_ticks += base::TimeDelta::FromMilliseconds(1);
1573 host_impl_.SetCurrentBeginFrameArgs(
1574 CreateBeginFrameArgsForTesting(time_ticks));
1576 gfx::Size tile_size(100, 100);
1577 gfx::Size layer_bounds(400, 400);
1578 gfx::Rect external_viewport_for_tile_priority(400, 200);
1579 gfx::Rect visible_content_rect(200, 400);
1581 scoped_refptr<FakePicturePileImpl> active_pile =
1582 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1583 scoped_refptr<FakePicturePileImpl> pending_pile =
1584 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1585 SetupTrees(pending_pile, active_pile);
1587 active_layer_->set_fixed_tile_size(tile_size);
1588 pending_layer_->set_fixed_tile_size(tile_size);
1589 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1590 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1592 // Set external viewport for tile priority.
1593 gfx::Rect viewport = gfx::Rect(layer_bounds);
1594 gfx::Transform transform;
1595 gfx::Transform transform_for_tile_priority;
1596 bool resourceless_software_draw = false;
1597 host_impl_.SetExternalDrawConstraints(transform,
1600 external_viewport_for_tile_priority,
1601 transform_for_tile_priority,
1602 resourceless_software_draw);
1603 host_impl_.pending_tree()->UpdateDrawProperties();
1605 // Set visible content rect that is different from
1606 // external_viewport_for_tile_priority.
1607 pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1608 time_ticks += base::TimeDelta::FromMilliseconds(200);
1609 host_impl_.SetCurrentBeginFrameArgs(
1610 CreateBeginFrameArgsForTesting(time_ticks));
1611 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
1613 // Intersect the two rects. Any tile outside should not be required for
1615 gfx::Rect viewport_for_tile_priority =
1616 pending_layer_->GetViewportForTilePriorityInContentSpace();
1617 viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1620 int num_outside = 0;
1621 for (PictureLayerTiling::CoverageIterator iter(
1622 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1628 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1630 // Mark everything in viewport for tile priority as ready to draw.
1631 ManagedTileState::DrawInfo& draw_info = tile->draw_info();
1632 draw_info.SetSolidColorForTesting(SK_ColorRED);
1635 EXPECT_FALSE(tile->required_for_activation());
1639 EXPECT_GT(num_inside, 0);
1640 EXPECT_GT(num_outside, 0);
1642 // Activate and draw active layer.
1643 host_impl_.ActivateSyncTree();
1644 host_impl_.active_tree()->UpdateDrawProperties();
1645 active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1647 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1648 AppendQuadsData data;
1649 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1650 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1651 active_layer_->DidDraw(nullptr);
1653 // All tiles in activation rect is ready to draw.
1654 EXPECT_EQ(0u, data.num_missing_tiles);
1655 EXPECT_EQ(0u, data.num_incomplete_tiles);
1656 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1659 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1660 base::TimeTicks time_ticks;
1661 time_ticks += base::TimeDelta::FromMilliseconds(1);
1662 host_impl_.SetCurrentBeginFrameArgs(
1663 CreateBeginFrameArgsForTesting(time_ticks));
1665 gfx::Size tile_size(100, 100);
1666 gfx::Size layer_bounds(200, 200);
1668 host_impl_.SetViewportSize(layer_bounds);
1670 scoped_refptr<FakePicturePileImpl> pending_pile =
1671 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1672 SetupPendingTree(pending_pile);
1675 // All high res tiles have resources.
1676 active_layer_->set_fixed_tile_size(tile_size);
1677 host_impl_.active_tree()->UpdateDrawProperties();
1678 std::vector<Tile*> tiles =
1679 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1680 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1682 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1683 AppendQuadsData data;
1684 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1685 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1686 active_layer_->DidDraw(nullptr);
1688 // All high res tiles drew, nothing was incomplete.
1689 EXPECT_EQ(9u, render_pass->quad_list.size());
1690 EXPECT_EQ(0u, data.num_missing_tiles);
1691 EXPECT_EQ(0u, data.num_incomplete_tiles);
1692 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1695 TEST_F(PictureLayerImplTest, HighResTileIsIncomplete) {
1696 base::TimeTicks time_ticks;
1697 time_ticks += base::TimeDelta::FromMilliseconds(1);
1698 host_impl_.SetCurrentBeginFrameArgs(
1699 CreateBeginFrameArgsForTesting(time_ticks));
1701 gfx::Size tile_size(100, 100);
1702 gfx::Size layer_bounds(200, 200);
1704 host_impl_.SetViewportSize(layer_bounds);
1706 scoped_refptr<FakePicturePileImpl> pending_pile =
1707 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1708 SetupPendingTree(pending_pile);
1711 active_layer_->set_fixed_tile_size(tile_size);
1712 host_impl_.active_tree()->UpdateDrawProperties();
1714 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1715 AppendQuadsData data;
1716 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1717 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1718 active_layer_->DidDraw(nullptr);
1720 EXPECT_EQ(1u, render_pass->quad_list.size());
1721 EXPECT_EQ(1u, data.num_missing_tiles);
1722 EXPECT_EQ(0u, data.num_incomplete_tiles);
1723 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1726 TEST_F(PictureLayerImplTest, HighResTileIsIncompleteLowResComplete) {
1727 base::TimeTicks time_ticks;
1728 time_ticks += base::TimeDelta::FromMilliseconds(1);
1729 host_impl_.SetCurrentBeginFrameArgs(
1730 CreateBeginFrameArgsForTesting(time_ticks));
1732 gfx::Size tile_size(100, 100);
1733 gfx::Size layer_bounds(200, 200);
1735 host_impl_.SetViewportSize(layer_bounds);
1737 scoped_refptr<FakePicturePileImpl> pending_pile =
1738 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1739 SetupPendingTree(pending_pile);
1742 active_layer_->set_fixed_tile_size(tile_size);
1743 host_impl_.active_tree()->UpdateDrawProperties();
1744 std::vector<Tile*> low_tiles =
1745 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1746 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1748 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1749 AppendQuadsData data;
1750 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1751 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1752 active_layer_->DidDraw(nullptr);
1754 EXPECT_EQ(1u, render_pass->quad_list.size());
1755 EXPECT_EQ(0u, data.num_missing_tiles);
1756 EXPECT_EQ(1u, data.num_incomplete_tiles);
1757 EXPECT_TRUE(active_layer_->only_used_low_res_last_append_quads());
1760 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1761 base::TimeTicks time_ticks;
1762 time_ticks += base::TimeDelta::FromMilliseconds(1);
1763 host_impl_.SetCurrentBeginFrameArgs(
1764 CreateBeginFrameArgsForTesting(time_ticks));
1766 gfx::Size tile_size(100, 100);
1767 gfx::Size layer_bounds(200, 200);
1769 host_impl_.SetViewportSize(layer_bounds);
1771 scoped_refptr<FakePicturePileImpl> pending_pile =
1772 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1773 SetupPendingTree(pending_pile);
1776 // All high res tiles have resources except one.
1777 active_layer_->set_fixed_tile_size(tile_size);
1778 host_impl_.active_tree()->UpdateDrawProperties();
1779 std::vector<Tile*> high_tiles =
1780 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1781 high_tiles.erase(high_tiles.begin());
1782 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1784 // All low res tiles have resources.
1785 std::vector<Tile*> low_tiles =
1786 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1787 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1789 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1790 AppendQuadsData data;
1791 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1792 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1793 active_layer_->DidDraw(nullptr);
1795 // The missing high res tile was replaced by a low res tile.
1796 EXPECT_EQ(9u, render_pass->quad_list.size());
1797 EXPECT_EQ(0u, data.num_missing_tiles);
1798 EXPECT_EQ(1u, data.num_incomplete_tiles);
1799 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1802 TEST_F(PictureLayerImplTest,
1803 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1804 base::TimeTicks time_ticks;
1805 time_ticks += base::TimeDelta::FromMilliseconds(1);
1806 host_impl_.SetCurrentBeginFrameArgs(
1807 CreateBeginFrameArgsForTesting(time_ticks));
1809 gfx::Size tile_size(100, 100);
1810 gfx::Size layer_bounds(200, 200);
1812 host_impl_.SetViewportSize(layer_bounds);
1814 scoped_refptr<FakePicturePileImpl> pending_pile =
1815 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1816 scoped_refptr<FakePicturePileImpl> active_pile =
1817 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1818 SetupTrees(pending_pile, active_pile);
1820 active_layer_->set_fixed_tile_size(tile_size);
1822 active_layer_->draw_properties().visible_content_rect =
1823 gfx::Rect(layer_bounds);
1824 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1826 // One ideal tile exists, this will get used when drawing.
1827 std::vector<Tile*> ideal_tiles;
1828 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1829 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1830 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1833 // Due to layer scale throttling, the raster contents scale is changed to 1,
1834 // while the ideal is still 2.
1835 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1836 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1838 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1839 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1840 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1842 // Both tilings still exist.
1843 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1844 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1846 // All high res tiles have resources.
1847 std::vector<Tile*> high_tiles =
1848 active_layer_->HighResTiling()->AllTilesForTesting();
1849 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1851 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1852 AppendQuadsData data;
1853 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
1854 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
1855 active_layer_->DidDraw(nullptr);
1857 // All high res tiles drew, and the one ideal res tile drew.
1858 ASSERT_GT(render_pass->quad_list.size(), 9u);
1859 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1860 TileDrawQuad::MaterialCast(render_pass->quad_list.front())
1861 ->tex_coord_rect.size());
1862 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1863 TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
1864 ->tex_coord_rect.size());
1866 // Neither the high res nor the ideal tiles were considered as incomplete.
1867 EXPECT_EQ(0u, data.num_missing_tiles);
1868 EXPECT_EQ(0u, data.num_incomplete_tiles);
1869 EXPECT_FALSE(active_layer_->only_used_low_res_last_append_quads());
1872 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1873 gfx::Size layer_bounds(400, 400);
1874 gfx::Size tile_size(100, 100);
1875 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1878 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1880 CreateHighLowResAndSetAllTilesVisible();
1882 active_layer_->SetAllTilesReady();
1884 // No shared tiles and all active tiles ready, so pending can only
1885 // activate with all high res tiles.
1886 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
1887 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
1889 AssertAllTilesRequired(pending_layer_->HighResTiling());
1890 AssertNoTilesRequired(pending_layer_->LowResTiling());
1893 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1894 gfx::Size layer_bounds(400, 400);
1895 gfx::Size tile_size(100, 100);
1896 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1898 // All tiles shared (no invalidation).
1899 CreateHighLowResAndSetAllTilesVisible();
1901 // Verify active tree not ready.
1902 Tile* some_active_tile =
1903 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1904 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1906 // When high res are required, even if the active tree is not ready,
1907 // the high res tiles must be ready.
1908 host_impl_.SetRequiresHighResToDraw();
1910 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
1911 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
1913 AssertAllTilesRequired(pending_layer_->HighResTiling());
1914 AssertNoTilesRequired(pending_layer_->LowResTiling());
1917 TEST_F(PictureLayerImplTest, AllHighResRequiredEvenIfShared) {
1918 gfx::Size layer_bounds(400, 400);
1919 gfx::Size tile_size(100, 100);
1920 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1922 CreateHighLowResAndSetAllTilesVisible();
1924 Tile* some_active_tile =
1925 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1926 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1928 // All tiles shared (no invalidation), so even though the active tree's
1929 // tiles aren't ready, the high res tiles are required for activation.
1930 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
1931 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
1933 AssertAllTilesRequired(pending_layer_->HighResTiling());
1934 AssertNoTilesRequired(pending_layer_->LowResTiling());
1937 TEST_F(PictureLayerImplTest, DisallowRequiredForActivation) {
1938 gfx::Size layer_bounds(400, 400);
1939 gfx::Size tile_size(100, 100);
1940 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1942 CreateHighLowResAndSetAllTilesVisible();
1944 Tile* some_active_tile =
1945 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1946 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1948 pending_layer_->HighResTiling()->set_can_require_tiles_for_activation(false);
1949 pending_layer_->LowResTiling()->set_can_require_tiles_for_activation(false);
1951 // If we disallow required for activation, no tiles can be required.
1952 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
1953 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
1955 AssertNoTilesRequired(pending_layer_->HighResTiling());
1956 AssertNoTilesRequired(pending_layer_->LowResTiling());
1959 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1960 gfx::Size layer_bounds(400, 400);
1961 gfx::Size tile_size(100, 100);
1962 scoped_refptr<FakePicturePileImpl> pending_pile =
1963 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1964 // This pile will create tilings, but has no recordings so will not create any
1965 // tiles. This is attempting to simulate scrolling past the end of recorded
1966 // content on the active layer, where the recordings are so far away that
1967 // no tiles are created.
1968 scoped_refptr<FakePicturePileImpl> active_pile =
1969 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1970 tile_size, layer_bounds);
1971 SetupTrees(pending_pile, active_pile);
1972 pending_layer_->set_fixed_tile_size(tile_size);
1973 active_layer_->set_fixed_tile_size(tile_size);
1975 CreateHighLowResAndSetAllTilesVisible();
1977 // Active layer has tilings, but no tiles due to missing recordings.
1978 EXPECT_TRUE(active_layer_->CanHaveTilings());
1979 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1980 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1982 // Since the active layer has no tiles at all, the pending layer doesn't
1983 // need content in order to activate.
1984 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
1985 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
1987 AssertNoTilesRequired(pending_layer_->HighResTiling());
1988 AssertNoTilesRequired(pending_layer_->LowResTiling());
1991 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1992 gfx::Size layer_bounds(400, 400);
1993 gfx::Size tile_size(100, 100);
1994 scoped_refptr<FakePicturePileImpl> pending_pile =
1995 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1996 scoped_refptr<FakePicturePileImpl> active_pile =
1997 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1998 SetupTrees(pending_pile, active_pile);
1999 pending_layer_->set_fixed_tile_size(tile_size);
2000 active_layer_->set_fixed_tile_size(tile_size);
2002 CreateHighLowResAndSetAllTilesVisible();
2004 // Active layer can't have tiles.
2005 EXPECT_FALSE(active_layer_->CanHaveTilings());
2007 // All high res tiles required. This should be considered identical
2008 // to the case where there is no active layer, to avoid flashing content.
2009 // This can happen if a layer exists for a while and switches from
2010 // not being able to have content to having content.
2011 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2012 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2014 AssertAllTilesRequired(pending_layer_->HighResTiling());
2015 AssertNoTilesRequired(pending_layer_->LowResTiling());
2018 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
2019 gfx::Size layer_bounds(200, 200);
2020 gfx::Size tile_size(100, 100);
2021 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2023 gfx::Size pending_layer_bounds(400, 400);
2024 pending_layer_->SetBounds(pending_layer_bounds);
2026 CreateHighLowResAndSetAllTilesVisible();
2027 // TODO(vmpstr): This is confusing. Rework the test to create different bounds
2028 // on different trees instead of fudging tilings.
2029 pending_layer_->HighResTiling()->ComputeTilePriorityRects(
2030 PENDING_TREE, gfx::Rect(pending_layer_bounds), 1.f, 1.f, Occlusion());
2032 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2033 active_layer_->SetAllTilesReady();
2035 // Since the active layer has different bounds, the pending layer needs all
2036 // high res tiles in order to activate.
2037 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
2038 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
2040 AssertAllTilesRequired(pending_layer_->HighResTiling());
2041 AssertNoTilesRequired(pending_layer_->LowResTiling());
2044 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
2045 gfx::Size tile_size(100, 100);
2046 gfx::Size layer_bounds(400, 400);
2047 scoped_refptr<FakePicturePileImpl> pending_pile =
2048 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2050 host_impl_.CreatePendingTree();
2051 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
2053 scoped_ptr<FakePictureLayerImpl> pending_layer =
2054 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
2055 pending_layer->SetDrawsContent(true);
2056 pending_tree->SetRootLayer(pending_layer.Pass());
2058 pending_layer_ = static_cast<FakePictureLayerImpl*>(
2059 host_impl_.pending_tree()->LayerById(id_));
2061 // Set some state on the pending layer, make sure it is not clobbered
2062 // by a sync from the active layer. This could happen because if the
2063 // pending layer has not been post-commit initialized it will attempt
2064 // to sync from the active layer.
2065 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
2066 pending_layer_->set_raster_page_scale(raster_page_scale);
2067 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2069 host_impl_.ActivateSyncTree();
2071 active_layer_ = static_cast<FakePictureLayerImpl*>(
2072 host_impl_.active_tree()->LayerById(id_));
2074 EXPECT_EQ(0u, active_layer_->num_tilings());
2075 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
2076 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
2079 TEST_F(PictureLayerImplTest, ShareTilesOnNextFrame) {
2080 SetupDefaultTrees(gfx::Size(1500, 1500));
2082 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
2083 gfx::Rect first_invalidate = tiling->TilingDataForTesting().TileBounds(0, 0);
2084 first_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2085 tiling->TilingDataForTesting().border_texels());
2086 gfx::Rect second_invalidate = tiling->TilingDataForTesting().TileBounds(1, 1);
2087 second_invalidate.Inset(tiling->TilingDataForTesting().border_texels(),
2088 tiling->TilingDataForTesting().border_texels());
2090 // Make a pending tree with an invalidated raster tile 0,0.
2091 tiling->CreateAllTilesForTesting();
2092 pending_layer_->set_invalidation(first_invalidate);
2094 // Activate and make a pending tree with an invalidated raster tile 1,1.
2097 host_impl_.CreatePendingTree();
2098 pending_layer_ = static_cast<FakePictureLayerImpl*>(
2099 host_impl_.pending_tree()->root_layer());
2100 pending_layer_->set_invalidation(second_invalidate);
2102 PictureLayerTiling* pending_tiling = pending_layer_->tilings()->tiling_at(0);
2103 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(0);
2105 pending_tiling->CreateAllTilesForTesting();
2107 // Tile 0,0 should be shared, but tile 1,1 should not be.
2108 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2109 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2110 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2111 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2112 EXPECT_TRUE(pending_tiling->TileAt(0, 0)->is_shared());
2113 EXPECT_TRUE(pending_tiling->TileAt(1, 0)->is_shared());
2114 EXPECT_TRUE(pending_tiling->TileAt(0, 1)->is_shared());
2115 EXPECT_FALSE(pending_tiling->TileAt(1, 1)->is_shared());
2117 // Drop the tiles on the active tree and recreate them. The same tiles
2118 // should be shared or not.
2119 active_tiling->ComputeTilePriorityRects(
2120 ACTIVE_TREE, gfx::Rect(), 1.f, 1.0, Occlusion());
2121 EXPECT_TRUE(active_tiling->AllTilesForTesting().empty());
2122 active_tiling->CreateAllTilesForTesting();
2124 // Tile 0,0 should be shared, but tile 1,1 should not be.
2125 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2126 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2127 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2128 EXPECT_NE(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2129 EXPECT_TRUE(pending_tiling->TileAt(0, 0)->is_shared());
2130 EXPECT_TRUE(pending_tiling->TileAt(1, 0)->is_shared());
2131 EXPECT_TRUE(pending_tiling->TileAt(0, 1)->is_shared());
2132 EXPECT_FALSE(pending_tiling->TileAt(1, 1)->is_shared());
2135 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
2136 SetupDefaultTrees(gfx::Size(1500, 1500));
2137 AddDefaultTilingsWithInvalidation(gfx::Rect());
2139 host_impl_.ActivateSyncTree();
2140 host_impl_.CreatePendingTree();
2141 active_layer_ = static_cast<FakePictureLayerImpl*>(
2142 host_impl_.active_tree()->LayerById(id_));
2144 // Force the active tree to sync to the pending tree "post-commit".
2145 pending_layer_->DoPostCommitInitializationIfNeeded();
2147 // Both invalidations should drop tiles from the pending tree.
2148 EXPECT_EQ(3u, active_layer_->num_tilings());
2149 EXPECT_EQ(3u, pending_layer_->num_tilings());
2150 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2151 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2152 PictureLayerTiling* pending_tiling =
2153 pending_layer_->tilings()->tiling_at(i);
2155 ASSERT_TRUE(active_tiling);
2156 ASSERT_TRUE(pending_tiling);
2158 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2159 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2160 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2161 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2163 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2164 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2165 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2166 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2168 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2169 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2170 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2171 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2172 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2173 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2174 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2175 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2179 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
2180 SetupDefaultTrees(gfx::Size(1500, 1500));
2181 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
2183 // This activates the 0,0,1,1 invalidation.
2184 host_impl_.ActivateSyncTree();
2185 host_impl_.CreatePendingTree();
2186 active_layer_ = static_cast<FakePictureLayerImpl*>(
2187 host_impl_.active_tree()->LayerById(id_));
2189 // Force the active tree to sync to the pending tree "post-commit".
2190 pending_layer_->DoPostCommitInitializationIfNeeded();
2192 // The active tree invalidation was handled by the active tiles, so they
2193 // can be shared with the pending tree.
2194 EXPECT_EQ(3u, active_layer_->num_tilings());
2195 EXPECT_EQ(3u, pending_layer_->num_tilings());
2196 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2197 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2198 PictureLayerTiling* pending_tiling =
2199 pending_layer_->tilings()->tiling_at(i);
2201 ASSERT_TRUE(active_tiling);
2202 ASSERT_TRUE(pending_tiling);
2204 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2205 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2206 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2207 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2209 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2210 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2211 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2212 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2214 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2215 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2216 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2217 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2218 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2219 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2220 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2221 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2225 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
2226 SetupDefaultTrees(gfx::Size(1500, 1500));
2227 AddDefaultTilingsWithInvalidation(gfx::Rect());
2229 host_impl_.ActivateSyncTree();
2230 host_impl_.CreatePendingTree();
2231 active_layer_ = static_cast<FakePictureLayerImpl*>(
2232 host_impl_.active_tree()->LayerById(id_));
2234 // Set some invalidation on the pending tree "during commit". We should
2235 // replace raster tiles that touch this.
2236 pending_layer_->set_invalidation(gfx::Rect(1, 1));
2238 // Force the active tree to sync to the pending tree "post-commit".
2239 pending_layer_->DoPostCommitInitializationIfNeeded();
2241 // The pending tree invalidation means tiles can not be shared with the
2243 EXPECT_EQ(3u, active_layer_->num_tilings());
2244 EXPECT_EQ(3u, pending_layer_->num_tilings());
2245 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2246 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2247 PictureLayerTiling* pending_tiling =
2248 pending_layer_->tilings()->tiling_at(i);
2250 ASSERT_TRUE(active_tiling);
2251 ASSERT_TRUE(pending_tiling);
2253 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2254 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2255 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2256 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2258 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2259 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2260 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2261 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2263 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2264 EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2265 EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2266 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2267 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2268 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2269 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2270 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2271 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2275 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
2276 SetupDefaultTrees(gfx::Size(10, 10));
2277 host_impl_.active_tree()->UpdateDrawProperties();
2278 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2280 // Contrived unit test of a real crash. A layer is transparent during a
2281 // context loss, and later becomes opaque, causing active layer SyncTiling to
2283 float new_scale = 1.f;
2284 active_layer_->ReleaseResources();
2285 pending_layer_->ReleaseResources();
2286 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
2287 pending_layer_->AddTiling(new_scale);
2288 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
2290 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
2291 // responsible for calling ManageTilings. These checks verify that
2292 // ReleaseResources has set needs update draw properties so that the
2293 // new tiling gets the appropriate resolution set in ManageTilings.
2294 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2295 host_impl_.active_tree()->UpdateDrawProperties();
2296 PictureLayerTiling* high_res =
2297 active_layer_->tilings()->TilingAtScale(new_scale);
2298 ASSERT_TRUE(!!high_res);
2299 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
2302 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2303 SetupDefaultTrees(gfx::Size(10, 10));
2305 const float kScale = 1.f;
2306 pending_layer_->AddTiling(kScale);
2307 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2308 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2310 // Gpu rasterization is disabled by default.
2311 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2312 // Toggling the gpu rasterization clears all tilings on both trees.
2313 host_impl_.SetUseGpuRasterization(true);
2314 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2315 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2317 // Make sure that we can still add tiling to the pending layer,
2318 // that gets synced to the active layer.
2319 pending_layer_->AddTiling(kScale);
2320 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2321 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2323 // Toggling the gpu rasterization clears all tilings on both trees.
2324 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2325 host_impl_.SetUseGpuRasterization(false);
2326 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2327 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2330 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2331 gfx::Size tile_size(100, 100);
2333 scoped_refptr<FakePicturePileImpl> active_pile =
2334 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(10, 10));
2335 SetupPendingTree(active_pile);
2337 host_impl_.active_tree()->UpdateDrawProperties();
2338 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2340 SetupDrawPropertiesAndUpdateTiles(
2341 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2342 active_layer_->tilings()->RemoveAllTilings();
2343 PictureLayerTiling* tiling = active_layer_->AddTiling(0.5f);
2344 active_layer_->AddTiling(1.5f);
2345 active_layer_->AddTiling(0.25f);
2346 tiling->set_resolution(HIGH_RESOLUTION);
2349 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2350 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2352 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2353 // 1.0f). Note that we should also ensure that the pending layer needs post
2354 // commit initialization, since this is what would happen during commit. In
2355 // other words we want the pending layer to sync from the active layer.
2356 scoped_refptr<FakePicturePileImpl> pending_pile =
2357 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1, 1));
2358 SetupPendingTree(pending_pile);
2360 // Update the draw properties: sync from active tree should happen here.
2361 host_impl_.pending_tree()->UpdateDrawProperties();
2362 EXPECT_FALSE(pending_layer_->needs_post_commit_initialization());
2364 // Another sanity check.
2365 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2367 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2368 // violate minimum contents scale. At the same time, we should've created a
2369 // new high res tiling at scale 1.0f.
2370 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2371 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2372 EXPECT_EQ(HIGH_RESOLUTION,
2373 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2374 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2375 EXPECT_EQ(NON_IDEAL_RESOLUTION,
2376 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2379 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2380 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2381 gfx::Size layer_bounds(default_tile_size.width() * 4,
2382 default_tile_size.height() * 4);
2384 SetupDefaultTrees(layer_bounds);
2385 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2386 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2387 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2388 // Should have a low-res and a high-res tiling.
2389 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2391 ResetTilingsAndRasterScales();
2393 host_impl_.SetUseGpuRasterization(true);
2394 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2395 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2397 // Should only have the high-res tiling.
2398 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2401 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2402 // Set up layers with tilings.
2403 SetupDefaultTrees(gfx::Size(10, 10));
2404 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2405 pending_layer_->PushPropertiesTo(active_layer_);
2406 EXPECT_TRUE(pending_layer_->DrawsContent());
2407 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2408 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2409 EXPECT_GE(active_layer_->num_tilings(), 0u);
2411 // Set content to false, which should make CanHaveTilings return false.
2412 pending_layer_->SetDrawsContent(false);
2413 EXPECT_FALSE(pending_layer_->DrawsContent());
2414 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2416 // No tilings should be pushed to active layer.
2417 pending_layer_->PushPropertiesTo(active_layer_);
2418 EXPECT_EQ(0u, active_layer_->num_tilings());
2421 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2422 SetupDefaultTrees(gfx::Size(10, 10));
2423 host_impl_.PinchGestureBegin();
2424 float high_res_scale = 2.3f;
2425 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2427 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2428 EXPECT_FLOAT_EQ(high_res_scale,
2429 pending_layer_->HighResTiling()->contents_scale());
2432 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2433 SetupDefaultTrees(gfx::Size(10, 10));
2434 host_impl_.PinchGestureBegin();
2435 float high_res_scale = 0.0001f;
2436 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2438 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2440 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2441 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2442 pending_layer_->HighResTiling()->contents_scale());
2445 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2446 SetupDefaultTrees(gfx::Size(10, 10));
2448 float contents_scale = 0.15f;
2449 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2451 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2452 EXPECT_FLOAT_EQ(contents_scale,
2453 pending_layer_->HighResTiling()->contents_scale());
2455 host_impl_.PinchGestureBegin();
2457 float page_scale = 0.0001f;
2458 EXPECT_LT(page_scale * contents_scale,
2459 pending_layer_->MinimumContentsScale());
2461 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2462 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2463 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2464 pending_layer_->HighResTiling()->contents_scale());
2467 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2469 void InitializeRenderer() override {
2470 bool delegated_rendering = false;
2471 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL(
2472 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2473 delegated_rendering));
2476 virtual void SetUp() override {
2477 PictureLayerImplTest::SetUp();
2479 // Create some default active and pending trees.
2480 gfx::Size tile_size(100, 100);
2481 gfx::Size layer_bounds(400, 400);
2483 scoped_refptr<FakePicturePileImpl> pending_pile =
2484 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2485 scoped_refptr<FakePicturePileImpl> active_pile =
2486 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2488 SetupTrees(pending_pile, active_pile);
2492 // This test is really a LayerTreeHostImpl test, in that it makes sure
2493 // that trees need update draw properties after deferred initialization.
2494 // However, this is also a regression test for PictureLayerImpl in that
2495 // not having this update will cause a crash.
2496 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2497 host_impl_.pending_tree()->UpdateDrawProperties();
2498 host_impl_.active_tree()->UpdateDrawProperties();
2499 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2500 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2502 FakeOutputSurface* fake_output_surface =
2503 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2504 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2505 TestContextProvider::Create()));
2507 // These will crash PictureLayerImpl if this is not true.
2508 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2509 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2510 host_impl_.active_tree()->UpdateDrawProperties();
2513 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2514 gfx::Size layer_bounds(100, 100);
2515 gfx::Size viewport_size(1000, 1000);
2516 SetupDefaultTrees(layer_bounds);
2517 host_impl_.SetViewportSize(viewport_size);
2519 float contents_scale = 1.f;
2520 float device_scale = 1.3f;
2521 float page_scale = 1.4f;
2522 float maximum_animation_scale = 1.f;
2523 bool animating_transform = false;
2525 SetContentsScaleOnBothLayers(contents_scale,
2528 maximum_animation_scale,
2529 animating_transform);
2530 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2532 // Since we're CPU-rasterizing, starting an animation should cause tiling
2533 // resolution to get set to the maximum animation scale factor.
2534 animating_transform = true;
2535 maximum_animation_scale = 3.f;
2536 contents_scale = 2.f;
2538 SetContentsScaleOnBothLayers(contents_scale,
2541 maximum_animation_scale,
2542 animating_transform);
2543 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2545 // Further changes to scale during the animation should not cause a new
2546 // high-res tiling to get created.
2547 contents_scale = 4.f;
2548 maximum_animation_scale = 5.f;
2550 SetContentsScaleOnBothLayers(contents_scale,
2553 maximum_animation_scale,
2554 animating_transform);
2555 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2557 // Once we stop animating, a new high-res tiling should be created.
2558 animating_transform = false;
2560 SetContentsScaleOnBothLayers(contents_scale,
2563 maximum_animation_scale,
2564 animating_transform);
2565 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2567 // When animating with an unknown maximum animation scale factor, a new
2568 // high-res tiling should be created at a source scale of 1.
2569 animating_transform = true;
2570 contents_scale = 2.f;
2571 maximum_animation_scale = 0.f;
2573 SetContentsScaleOnBothLayers(contents_scale,
2576 maximum_animation_scale,
2577 animating_transform);
2578 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2580 // Further changes to scale during the animation should not cause a new
2581 // high-res tiling to get created.
2582 contents_scale = 3.f;
2584 SetContentsScaleOnBothLayers(contents_scale,
2587 maximum_animation_scale,
2588 animating_transform);
2589 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2591 // Once we stop animating, a new high-res tiling should be created.
2592 animating_transform = false;
2593 contents_scale = 4.f;
2595 SetContentsScaleOnBothLayers(contents_scale,
2598 maximum_animation_scale,
2599 animating_transform);
2600 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2602 // When animating with a maxmium animation scale factor that is so large
2603 // that the layer grows larger than the viewport at this scale, a new
2604 // high-res tiling should get created at a source scale of 1, not at its
2606 animating_transform = true;
2607 contents_scale = 2.f;
2608 maximum_animation_scale = 11.f;
2610 SetContentsScaleOnBothLayers(contents_scale,
2613 maximum_animation_scale,
2614 animating_transform);
2615 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2617 // Once we stop animating, a new high-res tiling should be created.
2618 animating_transform = false;
2619 contents_scale = 11.f;
2621 SetContentsScaleOnBothLayers(contents_scale,
2624 maximum_animation_scale,
2625 animating_transform);
2626 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2628 // When animating with a maxmium animation scale factor that is so large
2629 // that the layer grows larger than the viewport at this scale, and where
2630 // the intial source scale is < 1, a new high-res tiling should get created
2631 // at source scale 1.
2632 animating_transform = true;
2633 contents_scale = 0.1f;
2634 maximum_animation_scale = 11.f;
2636 SetContentsScaleOnBothLayers(contents_scale,
2639 maximum_animation_scale,
2640 animating_transform);
2641 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2643 // Once we stop animating, a new high-res tiling should be created.
2644 animating_transform = false;
2645 contents_scale = 12.f;
2647 SetContentsScaleOnBothLayers(contents_scale,
2650 maximum_animation_scale,
2651 animating_transform);
2652 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 12.f);
2654 // When animating toward a smaller scale, but that is still so large that the
2655 // layer grows larger than the viewport at this scale, a new high-res tiling
2656 // should get created at source scale 1.
2657 animating_transform = true;
2658 contents_scale = 11.f;
2659 maximum_animation_scale = 11.f;
2661 SetContentsScaleOnBothLayers(contents_scale,
2664 maximum_animation_scale,
2665 animating_transform);
2666 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2668 // Once we stop animating, a new high-res tiling should be created.
2669 animating_transform = false;
2670 contents_scale = 11.f;
2672 SetContentsScaleOnBothLayers(contents_scale,
2675 maximum_animation_scale,
2676 animating_transform);
2677 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2680 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2681 gfx::Size layer_bounds(100, 100);
2682 gfx::Size viewport_size(1000, 1000);
2683 SetupDefaultTrees(layer_bounds);
2684 host_impl_.SetViewportSize(viewport_size);
2685 host_impl_.SetUseGpuRasterization(true);
2687 float contents_scale = 1.f;
2688 float device_scale = 1.3f;
2689 float page_scale = 1.4f;
2690 float maximum_animation_scale = 1.f;
2691 bool animating_transform = false;
2693 SetContentsScaleOnBothLayers(contents_scale,
2696 maximum_animation_scale,
2697 animating_transform);
2698 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2700 // Since we're GPU-rasterizing, starting an animation should cause tiling
2701 // resolution to get set to the current contents scale.
2702 animating_transform = true;
2703 contents_scale = 2.f;
2704 maximum_animation_scale = 4.f;
2706 SetContentsScaleOnBothLayers(contents_scale,
2709 maximum_animation_scale,
2710 animating_transform);
2711 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2713 // Further changes to scale during the animation should cause a new high-res
2714 // tiling to get created.
2715 contents_scale = 3.f;
2717 SetContentsScaleOnBothLayers(contents_scale,
2720 maximum_animation_scale,
2721 animating_transform);
2722 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2724 // Since we're re-rasterizing during the animation, scales smaller than 1
2725 // should be respected.
2726 contents_scale = 0.25f;
2728 SetContentsScaleOnBothLayers(contents_scale,
2731 maximum_animation_scale,
2732 animating_transform);
2733 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2735 // Once we stop animating, a new high-res tiling should be created.
2736 contents_scale = 4.f;
2737 animating_transform = false;
2739 SetContentsScaleOnBothLayers(contents_scale,
2742 maximum_animation_scale,
2743 animating_transform);
2744 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2747 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2748 base::TimeTicks time_ticks;
2749 time_ticks += base::TimeDelta::FromMilliseconds(1);
2750 host_impl_.SetCurrentBeginFrameArgs(
2751 CreateBeginFrameArgsForTesting(time_ticks));
2753 gfx::Size tile_size(100, 100);
2754 gfx::Size layer_bounds(1000, 1000);
2756 scoped_refptr<FakePicturePileImpl> pending_pile =
2757 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2759 SetupPendingTree(pending_pile);
2761 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2763 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2766 PictureLayerImpl::LayerRasterTileIterator it;
2770 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2773 pending_layer_->AddTiling(low_res_factor);
2774 pending_layer_->AddTiling(0.3f);
2775 pending_layer_->AddTiling(0.7f);
2776 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2777 pending_layer_->AddTiling(2.0f);
2779 host_impl_.SetViewportSize(gfx::Size(500, 500));
2780 host_impl_.pending_tree()->UpdateDrawProperties();
2782 std::set<Tile*> unique_tiles;
2783 bool reached_prepaint = false;
2784 size_t non_ideal_tile_count = 0u;
2785 size_t low_res_tile_count = 0u;
2786 size_t high_res_tile_count = 0u;
2787 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2791 TilePriority priority = tile->priority(PENDING_TREE);
2795 // Non-high res tiles only get visible tiles. Also, prepaint should only
2796 // come at the end of the iteration.
2797 if (priority.resolution != HIGH_RESOLUTION)
2798 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2799 else if (reached_prepaint)
2800 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2802 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2804 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2805 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2806 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2808 unique_tiles.insert(tile);
2811 EXPECT_TRUE(reached_prepaint);
2812 EXPECT_EQ(0u, non_ideal_tile_count);
2813 EXPECT_EQ(0u, low_res_tile_count);
2814 EXPECT_EQ(16u, high_res_tile_count);
2815 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2816 unique_tiles.size());
2819 time_ticks += base::TimeDelta::FromMilliseconds(200);
2820 host_impl_.SetCurrentBeginFrameArgs(
2821 CreateBeginFrameArgsForTesting(time_ticks));
2823 pending_layer_->draw_properties().visible_content_rect =
2824 gfx::Rect(1100, 1100, 500, 500);
2825 bool resourceless_software_draw = false;
2826 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2828 unique_tiles.clear();
2829 high_res_tile_count = 0u;
2830 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2834 TilePriority priority = tile->priority(PENDING_TREE);
2838 // Non-high res tiles only get visible tiles.
2839 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2840 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2842 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2844 unique_tiles.insert(tile);
2847 EXPECT_EQ(16u, high_res_tile_count);
2848 EXPECT_EQ(high_res_tile_count, unique_tiles.size());
2850 time_ticks += base::TimeDelta::FromMilliseconds(200);
2851 host_impl_.SetCurrentBeginFrameArgs(
2852 CreateBeginFrameArgsForTesting(time_ticks));
2854 pending_layer_->draw_properties().visible_content_rect =
2855 gfx::Rect(0, 0, 500, 500);
2856 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2858 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2859 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2860 tile_it != high_res_tiles.end();
2862 Tile* tile = *tile_it;
2863 ManagedTileState::DrawInfo& draw_info = tile->draw_info();
2864 draw_info.SetSolidColorForTesting(SK_ColorRED);
2867 non_ideal_tile_count = 0;
2868 low_res_tile_count = 0;
2869 high_res_tile_count = 0;
2870 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, true); it;
2873 TilePriority priority = tile->priority(PENDING_TREE);
2877 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2878 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2879 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2882 EXPECT_EQ(0u, non_ideal_tile_count);
2883 EXPECT_EQ(1u, low_res_tile_count);
2884 EXPECT_EQ(0u, high_res_tile_count);
2887 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2888 gfx::Size tile_size(100, 100);
2889 gfx::Size layer_bounds(1000, 1000);
2891 scoped_refptr<FakePicturePileImpl> pending_pile =
2892 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2894 SetupPendingTree(pending_pile);
2896 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2898 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2900 std::vector<PictureLayerTiling*> tilings;
2901 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2902 tilings.push_back(pending_layer_->AddTiling(0.3f));
2903 tilings.push_back(pending_layer_->AddTiling(0.7f));
2904 tilings.push_back(pending_layer_->AddTiling(1.0f));
2905 tilings.push_back(pending_layer_->AddTiling(2.0f));
2907 host_impl_.SetViewportSize(gfx::Size(500, 500));
2908 host_impl_.pending_tree()->UpdateDrawProperties();
2910 std::vector<Tile*> all_tiles;
2911 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2913 tiling_iterator != tilings.end();
2914 ++tiling_iterator) {
2915 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2916 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2919 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2921 bool mark_required = false;
2922 size_t number_of_marked_tiles = 0u;
2923 size_t number_of_unmarked_tiles = 0u;
2924 for (size_t i = 0; i < tilings.size(); ++i) {
2925 PictureLayerTiling* tiling = tilings.at(i);
2926 for (PictureLayerTiling::CoverageIterator iter(
2928 pending_layer_->contents_scale_x(),
2929 pending_layer_->visible_content_rect());
2932 if (mark_required) {
2933 number_of_marked_tiles++;
2934 iter->set_required_for_activation(true);
2936 number_of_unmarked_tiles++;
2938 mark_required = !mark_required;
2943 EXPECT_EQ(91u, all_tiles.size());
2944 EXPECT_EQ(91u, all_tiles_set.size());
2945 EXPECT_GT(number_of_marked_tiles, 1u);
2946 EXPECT_GT(number_of_unmarked_tiles, 1u);
2949 PictureLayerImpl::LayerEvictionTileIterator it;
2952 // Tiles don't have resources yet.
2953 it = PictureLayerImpl::LayerEvictionTileIterator(
2954 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2957 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2959 std::set<Tile*> unique_tiles;
2960 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2961 size_t scale_index = 0;
2962 bool reached_visible = false;
2963 Tile* last_tile = nullptr;
2964 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2965 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2974 TilePriority priority = tile->priority(PENDING_TREE);
2976 if (priority.priority_bin == TilePriority::NOW) {
2977 reached_visible = true;
2982 EXPECT_FALSE(tile->required_for_activation());
2984 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2985 std::numeric_limits<float>::epsilon()) {
2987 ASSERT_LT(scale_index, arraysize(expected_scales));
2990 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2991 unique_tiles.insert(tile);
2993 // If the tile is the same rough bin as last tile (same activation, bin, and
2994 // scale), then distance should be decreasing.
2995 if (tile->required_for_activation() ==
2996 last_tile->required_for_activation() &&
2997 priority.priority_bin ==
2998 last_tile->priority(PENDING_TREE).priority_bin &&
2999 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
3000 std::numeric_limits<float>::epsilon()) {
3001 EXPECT_LE(priority.distance_to_visible,
3002 last_tile->priority(PENDING_TREE).distance_to_visible);
3008 EXPECT_TRUE(reached_visible);
3009 EXPECT_EQ(65u, unique_tiles.size());
3012 bool reached_required = false;
3017 TilePriority priority = tile->priority(PENDING_TREE);
3018 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
3020 if (reached_required) {
3021 EXPECT_TRUE(tile->required_for_activation());
3022 } else if (tile->required_for_activation()) {
3023 reached_required = true;
3027 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
3028 std::numeric_limits<float>::epsilon()) {
3030 ASSERT_LT(scale_index, arraysize(expected_scales));
3033 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
3034 unique_tiles.insert(tile);
3037 EXPECT_TRUE(reached_required);
3038 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
3041 TEST_F(PictureLayerImplTest, Occlusion) {
3042 gfx::Size tile_size(102, 102);
3043 gfx::Size layer_bounds(1000, 1000);
3044 gfx::Size viewport_size(1000, 1000);
3046 LayerTestCommon::LayerImplTest impl;
3048 scoped_refptr<FakePicturePileImpl> pending_pile =
3049 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
3050 SetupPendingTree(pending_pile);
3051 pending_layer_->SetBounds(layer_bounds);
3053 active_layer_->set_fixed_tile_size(tile_size);
3055 host_impl_.SetViewportSize(viewport_size);
3056 host_impl_.active_tree()->UpdateDrawProperties();
3058 std::vector<Tile*> tiles =
3059 active_layer_->HighResTiling()->AllTilesForTesting();
3060 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3063 SCOPED_TRACE("No occlusion");
3065 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3067 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
3068 gfx::Rect(layer_bounds));
3069 EXPECT_EQ(100u, impl.quad_list().size());
3073 SCOPED_TRACE("Full occlusion");
3074 gfx::Rect occluded(active_layer_->visible_content_rect());
3075 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3077 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
3078 EXPECT_EQ(impl.quad_list().size(), 0u);
3082 SCOPED_TRACE("Partial occlusion");
3083 gfx::Rect occluded(150, 0, 200, 1000);
3084 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
3086 size_t partially_occluded_count = 0;
3087 LayerTestCommon::VerifyQuadsAreOccluded(
3088 impl.quad_list(), occluded, &partially_occluded_count);
3089 // The layer outputs one quad, which is partially occluded.
3090 EXPECT_EQ(100u - 10u, impl.quad_list().size());
3091 EXPECT_EQ(10u + 10u, partially_occluded_count);
3095 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
3096 gfx::Size tile_size(host_impl_.settings().default_tile_size);
3097 SetupDefaultTrees(tile_size);
3099 float contents_scale = 2.f;
3100 float device_scale = 1.f;
3101 float page_scale = 1.f;
3102 float maximum_animation_scale = 1.f;
3103 bool animating_transform = false;
3105 SetContentsScaleOnBothLayers(contents_scale,
3108 maximum_animation_scale,
3109 animating_transform);
3110 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
3112 // Changing the source scale without being in an animation will cause
3113 // the layer to reset its source scale to 1.f.
3114 contents_scale = 3.f;
3116 SetContentsScaleOnBothLayers(contents_scale,
3119 maximum_animation_scale,
3120 animating_transform);
3121 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3123 // Further changes to the source scale will no longer be reflected in the
3125 contents_scale = 0.5f;
3127 SetContentsScaleOnBothLayers(contents_scale,
3130 maximum_animation_scale,
3131 animating_transform);
3132 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
3135 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
3136 gfx::Size tile_size(100, 100);
3137 gfx::Size layer_bounds(1000, 1000);
3139 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3141 // Make sure some tiles are not shared.
3142 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3144 CreateHighLowResAndSetAllTilesVisible();
3145 active_layer_->SetAllTilesReady();
3147 // All pending layer tiles required are not ready.
3148 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3150 // Initialize all low-res tiles.
3151 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3153 // Low-res tiles should not be enough.
3154 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3156 // Initialize remaining tiles.
3157 pending_layer_->SetAllTilesReady();
3159 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3162 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3163 gfx::Size tile_size(100, 100);
3164 gfx::Size layer_bounds(1000, 1000);
3166 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3168 // Make sure some tiles are not shared.
3169 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3171 CreateHighLowResAndSetAllTilesVisible();
3172 active_layer_->SetAllTilesReady();
3174 // All pending layer tiles required are not ready.
3175 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3177 // Initialize all high-res tiles.
3178 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3180 // High-res tiles should be enough, since they cover everything visible.
3181 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3184 TEST_F(PictureLayerImplTest,
3185 SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
3186 gfx::Size tile_size(100, 100);
3187 gfx::Size layer_bounds(1000, 1000);
3189 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3191 // Make sure some tiles are not shared.
3192 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3194 CreateHighLowResAndSetAllTilesVisible();
3196 // Initialize all high-res tiles in the active layer.
3197 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3198 // And all the low-res tiles in the pending layer.
3199 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3201 // The unshared high-res tiles are not ready, so we cannot activate.
3202 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3204 // When the unshared pending high-res tiles are ready, we can activate.
3205 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3206 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3209 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
3210 gfx::Size tile_size(100, 100);
3211 gfx::Size layer_bounds(1000, 1000);
3213 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3215 // Make sure some tiles are not shared.
3216 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3218 CreateHighLowResAndSetAllTilesVisible();
3220 // Initialize all high-res tiles in the active layer.
3221 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3223 // The unshared high-res tiles are not ready, so we cannot activate.
3224 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3226 // When the unshared pending high-res tiles are ready, we can activate.
3227 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3228 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3231 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
3233 NoLowResPictureLayerImplTest()
3234 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
3237 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3238 gfx::Size tile_size(400, 400);
3239 gfx::Size layer_bounds(1300, 1900);
3241 scoped_refptr<FakePicturePileImpl> pending_pile =
3242 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3243 scoped_refptr<FakePicturePileImpl> active_pile =
3244 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3246 SetupTrees(pending_pile, active_pile);
3247 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3249 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3250 EXPECT_LT(low_res_factor, 1.f);
3252 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3253 6.f, // ideal contents scale
3254 3.f, // device scale
3256 1.f, // maximum animation scale
3258 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3259 EXPECT_FLOAT_EQ(6.f,
3260 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3262 // If we change the page scale factor, then we should get new tilings.
3263 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3264 6.6f, // ideal contents scale
3265 3.f, // device scale
3267 1.f, // maximum animation scale
3269 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
3270 EXPECT_FLOAT_EQ(6.6f,
3271 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3273 // If we change the device scale factor, then we should get new tilings.
3274 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3275 7.26f, // ideal contents scale
3276 3.3f, // device scale
3278 1.f, // maximum animation scale
3280 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3281 EXPECT_FLOAT_EQ(7.26f,
3282 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3284 // If we change the device scale factor, but end up at the same total scale
3285 // factor somehow, then we don't get new tilings.
3286 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3287 7.26f, // ideal contents scale
3288 2.2f, // device scale
3290 1.f, // maximum animation scale
3292 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3293 EXPECT_FLOAT_EQ(7.26f,
3294 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3297 TEST_F(NoLowResPictureLayerImplTest, AllHighResRequiredEvenIfShared) {
3298 gfx::Size layer_bounds(400, 400);
3299 gfx::Size tile_size(100, 100);
3300 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3302 CreateHighLowResAndSetAllTilesVisible();
3304 Tile* some_active_tile =
3305 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3306 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3308 // All tiles shared (no invalidation), so even though the active tree's
3309 // tiles aren't ready, there is nothing required.
3310 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3311 if (host_impl_.settings().create_low_res_tiling)
3312 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3314 AssertAllTilesRequired(pending_layer_->HighResTiling());
3315 if (host_impl_.settings().create_low_res_tiling)
3316 AssertNoTilesRequired(pending_layer_->LowResTiling());
3319 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3320 gfx::Size layer_bounds(400, 400);
3321 gfx::Size tile_size(100, 100);
3322 scoped_refptr<FakePicturePileImpl> pending_pile =
3323 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3324 // This pile will create tilings, but has no recordings so will not create any
3325 // tiles. This is attempting to simulate scrolling past the end of recorded
3326 // content on the active layer, where the recordings are so far away that
3327 // no tiles are created.
3328 scoped_refptr<FakePicturePileImpl> active_pile =
3329 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3330 tile_size, layer_bounds);
3331 SetupTrees(pending_pile, active_pile);
3332 pending_layer_->set_fixed_tile_size(tile_size);
3333 active_layer_->set_fixed_tile_size(tile_size);
3335 CreateHighLowResAndSetAllTilesVisible();
3337 // Active layer has tilings, but no tiles due to missing recordings.
3338 EXPECT_TRUE(active_layer_->CanHaveTilings());
3339 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3340 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3341 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3343 // Since the active layer has no tiles at all, the pending layer doesn't
3344 // need content in order to activate.
3345 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
3346 if (host_impl_.settings().create_low_res_tiling)
3347 pending_layer_->LowResTiling()->UpdateAllTilePrioritiesForTesting();
3349 AssertNoTilesRequired(pending_layer_->HighResTiling());
3350 if (host_impl_.settings().create_low_res_tiling)
3351 AssertNoTilesRequired(pending_layer_->LowResTiling());
3354 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3355 base::TimeTicks time_ticks;
3356 time_ticks += base::TimeDelta::FromMilliseconds(1);
3357 host_impl_.SetCurrentBeginFrameArgs(
3358 CreateBeginFrameArgsForTesting(time_ticks));
3360 gfx::Size tile_size(100, 100);
3361 gfx::Size layer_bounds(400, 400);
3363 scoped_refptr<FakePicturePileImpl> pending_pile =
3364 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3365 scoped_refptr<FakePicturePileImpl> active_pile =
3366 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3368 SetupTrees(pending_pile, active_pile);
3370 Region invalidation;
3371 AddDefaultTilingsWithInvalidation(invalidation);
3372 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3374 // UpdateTiles with valid viewport. Should update tile viewport.
3375 // Note viewport is considered invalid if and only if in resourceless
3377 bool resourceless_software_draw = false;
3378 gfx::Rect viewport = gfx::Rect(layer_bounds);
3379 gfx::Transform transform;
3380 host_impl_.SetExternalDrawConstraints(transform,
3385 resourceless_software_draw);
3386 active_layer_->draw_properties().visible_content_rect = viewport;
3387 active_layer_->draw_properties().screen_space_transform = transform;
3388 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3390 gfx::Rect visible_rect_for_tile_priority =
3391 active_layer_->visible_rect_for_tile_priority();
3392 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3393 gfx::Transform screen_space_transform_for_tile_priority =
3394 active_layer_->screen_space_transform();
3396 // Expand viewport and set it as invalid for prioritizing tiles.
3397 // Should update viewport and transform, but not update visible rect.
3398 time_ticks += base::TimeDelta::FromMilliseconds(200);
3399 host_impl_.SetCurrentBeginFrameArgs(
3400 CreateBeginFrameArgsForTesting(time_ticks));
3401 resourceless_software_draw = true;
3402 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3403 transform.Translate(1.f, 1.f);
3404 active_layer_->draw_properties().visible_content_rect = viewport;
3405 active_layer_->draw_properties().screen_space_transform = transform;
3406 host_impl_.SetExternalDrawConstraints(transform,
3411 resourceless_software_draw);
3412 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3414 // Transform for tile priority is updated.
3415 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3416 active_layer_->screen_space_transform());
3417 // Visible rect for tile priority retains old value.
3418 EXPECT_EQ(visible_rect_for_tile_priority,
3419 active_layer_->visible_rect_for_tile_priority());
3421 // Keep expanded viewport but mark it valid. Should update tile viewport.
3422 time_ticks += base::TimeDelta::FromMilliseconds(200);
3423 host_impl_.SetCurrentBeginFrameArgs(
3424 CreateBeginFrameArgsForTesting(time_ticks));
3425 resourceless_software_draw = false;
3426 host_impl_.SetExternalDrawConstraints(transform,
3431 resourceless_software_draw);
3432 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3434 EXPECT_TRANSFORMATION_MATRIX_EQ(transform,
3435 active_layer_->screen_space_transform());
3436 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3439 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3440 gfx::Size tile_size(400, 400);
3441 gfx::Size layer_bounds(1300, 1900);
3443 scoped_refptr<FakePicturePileImpl> pending_pile =
3444 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3445 scoped_refptr<FakePicturePileImpl> active_pile =
3446 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3448 std::vector<PictureLayerTiling*> used_tilings;
3450 SetupTrees(pending_pile, active_pile);
3451 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3453 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3454 EXPECT_LT(low_res_factor, 1.f);
3456 float device_scale = 1.7f;
3457 float page_scale = 3.2f;
3460 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3461 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3463 // We only have ideal tilings, so they aren't removed.
3464 used_tilings.clear();
3465 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3466 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3468 host_impl_.PinchGestureBegin();
3470 // Changing the ideal but not creating new tilings.
3473 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3474 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3476 // The tilings are still our target scale, so they aren't removed.
3477 used_tilings.clear();
3478 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3479 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3481 host_impl_.PinchGestureEnd();
3483 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3486 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3487 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3488 EXPECT_FLOAT_EQ(1.f,
3489 active_layer_->tilings()->tiling_at(1)->contents_scale());
3491 // Mark the non-ideal tilings as used. They won't be removed.
3492 used_tilings.clear();
3493 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3494 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3495 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3497 // Now move the ideal scale to 0.5. Our target stays 1.2.
3498 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3500 // The high resolution tiling is between target and ideal, so is not
3501 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3502 used_tilings.clear();
3503 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3504 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3506 // Now move the ideal scale to 1.0. Our target stays 1.2.
3507 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3509 // All the tilings are between are target and the ideal, so they are not
3511 used_tilings.clear();
3512 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3513 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3515 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3516 SetupDrawPropertiesAndUpdateTiles(
3517 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3519 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3520 // in the range [1.0,1.2] and are kept.
3521 used_tilings.clear();
3522 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3523 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3525 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3527 SetupDrawPropertiesAndUpdateTiles(
3528 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3530 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3531 // target raster scale. But it is in our used tilings set, so nothing is
3533 used_tilings.clear();
3534 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3535 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3536 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3538 // If we remove it from our used tilings set, it is outside the range to keep
3539 // so it is deleted.
3540 used_tilings.clear();
3541 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3542 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3545 TEST_F(PictureLayerImplTest, ScaleCollision) {
3546 gfx::Size tile_size(400, 400);
3547 gfx::Size layer_bounds(1300, 1900);
3549 scoped_refptr<FakePicturePileImpl> pending_pile =
3550 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3551 scoped_refptr<FakePicturePileImpl> active_pile =
3552 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3554 std::vector<PictureLayerTiling*> used_tilings;
3556 SetupTrees(pending_pile, active_pile);
3558 float pending_contents_scale = 1.f;
3559 float active_contents_scale = 2.f;
3560 float device_scale_factor = 1.f;
3561 float page_scale_factor = 1.f;
3562 float maximum_animation_contents_scale = 1.f;
3563 bool animating_transform = false;
3565 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3566 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3567 EXPECT_LT(low_res_factor, 1.f);
3569 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3570 pending_contents_scale,
3571 device_scale_factor,
3573 maximum_animation_contents_scale,
3574 animating_transform);
3575 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3576 active_contents_scale,
3577 device_scale_factor,
3579 maximum_animation_contents_scale,
3580 animating_transform);
3582 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3583 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3585 EXPECT_EQ(active_contents_scale,
3586 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3587 EXPECT_EQ(pending_contents_scale,
3588 pending_layer_->tilings()->tiling_at(1)->contents_scale());
3589 EXPECT_EQ(active_contents_scale * low_res_factor,
3590 pending_layer_->tilings()->tiling_at(2)->contents_scale());
3591 EXPECT_EQ(pending_contents_scale * low_res_factor,
3592 pending_layer_->tilings()->tiling_at(3)->contents_scale());
3594 EXPECT_EQ(active_contents_scale,
3595 active_layer_->tilings()->tiling_at(0)->contents_scale());
3596 EXPECT_EQ(pending_contents_scale,
3597 active_layer_->tilings()->tiling_at(1)->contents_scale());
3598 EXPECT_EQ(active_contents_scale * low_res_factor,
3599 active_layer_->tilings()->tiling_at(2)->contents_scale());
3600 EXPECT_EQ(pending_contents_scale * low_res_factor,
3601 active_layer_->tilings()->tiling_at(3)->contents_scale());
3603 // The unused low res tiling from the pending tree must be kept or we may add
3604 // it again on the active tree and collide with the pending tree.
3605 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3606 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3607 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3609 EXPECT_EQ(active_contents_scale,
3610 active_layer_->tilings()->tiling_at(0)->contents_scale());
3611 EXPECT_EQ(pending_contents_scale,
3612 active_layer_->tilings()->tiling_at(1)->contents_scale());
3613 EXPECT_EQ(active_contents_scale * low_res_factor,
3614 active_layer_->tilings()->tiling_at(2)->contents_scale());
3615 EXPECT_EQ(pending_contents_scale * low_res_factor,
3616 active_layer_->tilings()->tiling_at(3)->contents_scale());
3619 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3620 gfx::Size tile_size(400, 400);
3621 gfx::Size layer_bounds(1300, 1900);
3623 scoped_refptr<FakePicturePileImpl> pending_pile =
3624 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3625 scoped_refptr<FakePicturePileImpl> active_pile =
3626 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3628 SetupTrees(pending_pile, active_pile);
3629 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3631 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3632 1.3f, // ideal contents scale
3633 2.7f, // device scale
3635 1.f, // maximum animation scale
3637 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3639 // All tilings should be removed when losing output surface.
3640 active_layer_->ReleaseResources();
3641 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3642 pending_layer_->ReleaseResources();
3643 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3645 // This should create new tilings.
3646 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3647 1.3f, // ideal contents scale
3648 2.7f, // device scale
3650 1.f, // maximum animation scale
3652 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3655 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3656 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3658 gfx::Size tile_size(400, 400);
3659 gfx::Size layer_bounds(1000, 2000);
3661 scoped_refptr<FakePicturePileImpl> pending_pile =
3662 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3663 scoped_refptr<FakePicturePileImpl> active_pile =
3664 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3666 SetupTrees(pending_pile, active_pile);
3668 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3669 host_impl_.pending_tree()->UpdateDrawProperties();
3671 active_layer_->draw_properties().visible_content_rect =
3672 gfx::Rect(layer_bounds);
3673 host_impl_.active_tree()->UpdateDrawProperties();
3675 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3676 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3677 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3678 SK_MScalar1 / max_contents_scale);
3680 AppendQuadsData data;
3681 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
3683 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3684 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3685 // The content_to_target_transform should be scaled by the
3686 // MaximumTilingContentsScale on the layer.
3687 EXPECT_EQ(scaled_draw_transform.ToString(),
3688 render_pass->shared_quad_state_list.front()
3689 ->content_to_target_transform.ToString());
3690 // The content_bounds should be scaled by the
3691 // MaximumTilingContentsScale on the layer.
3693 gfx::Size(2500u, 5000u).ToString(),
3694 render_pass->shared_quad_state_list.front()->content_bounds.ToString());
3695 // The visible_content_rect should be scaled by the
3696 // MaximumTilingContentsScale on the layer.
3697 EXPECT_EQ(gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3698 render_pass->shared_quad_state_list.front()
3699 ->visible_content_rect.ToString());
3702 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3703 gfx::Size tile_size(400, 400);
3704 gfx::Size bounds(100000, 100);
3706 host_impl_.CreatePendingTree();
3708 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3710 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3711 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3713 layer_with_mask->SetBounds(bounds);
3714 layer_with_mask->SetContentBounds(bounds);
3716 scoped_refptr<FakePicturePileImpl> pending_pile =
3717 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3718 pending_pile->SetIsMask(true);
3719 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3720 host_impl_.pending_tree(), 3, pending_pile);
3722 mask->SetBounds(bounds);
3723 mask->SetContentBounds(bounds);
3724 mask->SetDrawsContent(true);
3726 FakePictureLayerImpl* pending_mask_content = mask.get();
3727 layer_with_mask->SetMaskLayer(mask.Pass());
3729 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3730 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3732 child_of_layer_with_mask->SetBounds(bounds);
3733 child_of_layer_with_mask->SetContentBounds(bounds);
3734 child_of_layer_with_mask->SetDrawsContent(true);
3736 layer_with_mask->AddChild(child_of_layer_with_mask.Pass());
3738 root->AddChild(layer_with_mask.Pass());
3740 host_impl_.pending_tree()->SetRootLayer(root.Pass());
3742 EXPECT_FALSE(pending_mask_content->tilings());
3743 host_impl_.pending_tree()->UpdateDrawProperties();
3744 EXPECT_NE(0u, pending_mask_content->num_tilings());
3747 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3749 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3751 void InitializeRenderer() override {
3752 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDelegating3d());
3756 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3757 DelegatingRendererWithTileOOM) {
3758 // This test is added for crbug.com/402321, where quad should be produced when
3759 // raster on demand is not allowed and tile is OOM.
3760 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3761 gfx::Size layer_bounds(1000, 1000);
3764 scoped_refptr<FakePicturePileImpl> pending_pile =
3765 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3766 SetupPendingTree(pending_pile);
3767 pending_layer_->SetBounds(layer_bounds);
3768 host_impl_.SetViewportSize(layer_bounds);
3770 host_impl_.active_tree()->UpdateDrawProperties();
3771 std::vector<Tile*> tiles =
3772 active_layer_->HighResTiling()->AllTilesForTesting();
3773 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3775 // Force tiles after max_tiles to be OOM. TileManager uses
3776 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3777 // directly set state to host_impl_, so we set policy that would change the
3778 // state. We also need to update tree priority separately.
3779 GlobalStateThatImpactsTilePriority state;
3780 size_t max_tiles = 1;
3781 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3782 size_t resource_limit = max_tiles;
3783 ManagedMemoryPolicy policy(memory_limit,
3784 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3786 host_impl_.SetMemoryPolicy(policy);
3787 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3788 host_impl_.ManageTiles();
3790 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3791 AppendQuadsData data;
3792 active_layer_->WillDraw(DRAW_MODE_HARDWARE, nullptr);
3793 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
3794 active_layer_->DidDraw(nullptr);
3796 // Even when OOM, quads should be produced, and should be different material
3797 // from quads with resource.
3798 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3799 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3800 render_pass->quad_list.front()->material);
3801 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3802 render_pass->quad_list.back()->material);
3805 class OcclusionTrackingSettings : public LowResTilingsSettings {
3807 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3810 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3812 OcclusionTrackingPictureLayerImplTest()
3813 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3815 void VerifyEvictionConsidersOcclusion(
3816 PictureLayerImpl* layer,
3817 size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3818 for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3820 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3821 size_t occluded_tile_count = 0u;
3822 Tile* last_tile = nullptr;
3824 for (PictureLayerImpl::LayerEvictionTileIterator it =
3825 PictureLayerImpl::LayerEvictionTileIterator(layer,
3833 // The only way we will encounter an occluded tile after an unoccluded
3834 // tile is if the priorty bin decreased, the tile is required for
3835 // activation, or the scale changed.
3836 bool tile_is_occluded =
3837 tile->is_occluded_for_tree_priority(tree_priority);
3838 if (tile_is_occluded) {
3839 occluded_tile_count++;
3841 bool last_tile_is_occluded =
3842 last_tile->is_occluded_for_tree_priority(tree_priority);
3843 if (!last_tile_is_occluded) {
3844 TilePriority::PriorityBin tile_priority_bin =
3845 tile->priority_for_tree_priority(tree_priority).priority_bin;
3846 TilePriority::PriorityBin last_tile_priority_bin =
3847 last_tile->priority_for_tree_priority(tree_priority)
3851 (tile_priority_bin < last_tile_priority_bin) ||
3852 tile->required_for_activation() ||
3853 (tile->contents_scale() != last_tile->contents_scale()));
3858 EXPECT_EQ(expected_occluded_tile_count[priority_count],
3859 occluded_tile_count);
3864 TEST_F(OcclusionTrackingPictureLayerImplTest,
3865 OccludedTilesSkippedDuringRasterization) {
3866 base::TimeTicks time_ticks;
3867 time_ticks += base::TimeDelta::FromMilliseconds(1);
3868 host_impl_.SetCurrentBeginFrameArgs(
3869 CreateBeginFrameArgsForTesting(time_ticks));
3871 gfx::Size tile_size(102, 102);
3872 gfx::Size layer_bounds(1000, 1000);
3873 gfx::Size viewport_size(500, 500);
3874 gfx::Point occluding_layer_position(310, 0);
3876 scoped_refptr<FakePicturePileImpl> pending_pile =
3877 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3878 SetupPendingTree(pending_pile);
3879 pending_layer_->set_fixed_tile_size(tile_size);
3881 host_impl_.SetViewportSize(viewport_size);
3882 host_impl_.pending_tree()->UpdateDrawProperties();
3885 int unoccluded_tile_count = 0;
3886 for (PictureLayerImpl::LayerRasterTileIterator it =
3887 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3892 // Occluded tiles should not be iterated over.
3893 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3895 // Some tiles may not be visible (i.e. outside the viewport). The rest are
3896 // visible and at least partially unoccluded, verified by the above expect.
3897 bool tile_is_visible =
3898 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3899 if (tile_is_visible)
3900 unoccluded_tile_count++;
3902 EXPECT_EQ(unoccluded_tile_count, 25);
3904 // Partial occlusion.
3905 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3906 LayerImpl* layer1 = pending_layer_->children()[0];
3907 layer1->SetBounds(layer_bounds);
3908 layer1->SetContentBounds(layer_bounds);
3909 layer1->SetDrawsContent(true);
3910 layer1->SetContentsOpaque(true);
3911 layer1->SetPosition(occluding_layer_position);
3913 time_ticks += base::TimeDelta::FromMilliseconds(200);
3914 host_impl_.SetCurrentBeginFrameArgs(
3915 CreateBeginFrameArgsForTesting(time_ticks));
3916 host_impl_.pending_tree()->UpdateDrawProperties();
3918 unoccluded_tile_count = 0;
3919 for (PictureLayerImpl::LayerRasterTileIterator it =
3920 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3925 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3927 bool tile_is_visible =
3928 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3929 if (tile_is_visible)
3930 unoccluded_tile_count++;
3932 EXPECT_EQ(20, unoccluded_tile_count);
3935 layer1->SetPosition(gfx::Point(0, 0));
3937 time_ticks += base::TimeDelta::FromMilliseconds(200);
3938 host_impl_.SetCurrentBeginFrameArgs(
3939 CreateBeginFrameArgsForTesting(time_ticks));
3940 host_impl_.pending_tree()->UpdateDrawProperties();
3942 unoccluded_tile_count = 0;
3943 for (PictureLayerImpl::LayerRasterTileIterator it =
3944 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3949 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3951 bool tile_is_visible =
3952 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3953 if (tile_is_visible)
3954 unoccluded_tile_count++;
3956 EXPECT_EQ(unoccluded_tile_count, 0);
3959 TEST_F(OcclusionTrackingPictureLayerImplTest,
3960 OccludedTilesNotMarkedAsRequired) {
3961 base::TimeTicks time_ticks;
3962 time_ticks += base::TimeDelta::FromMilliseconds(1);
3963 host_impl_.SetCurrentBeginFrameArgs(
3964 CreateBeginFrameArgsForTesting(time_ticks));
3966 gfx::Size tile_size(102, 102);
3967 gfx::Size layer_bounds(1000, 1000);
3968 gfx::Size viewport_size(500, 500);
3969 gfx::Point occluding_layer_position(310, 0);
3971 scoped_refptr<FakePicturePileImpl> pending_pile =
3972 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3973 SetupPendingTree(pending_pile);
3974 pending_layer_->set_fixed_tile_size(tile_size);
3976 host_impl_.SetViewportSize(viewport_size);
3977 host_impl_.pending_tree()->UpdateDrawProperties();
3980 int occluded_tile_count = 0;
3981 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3982 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3984 occluded_tile_count = 0;
3985 for (PictureLayerTiling::CoverageIterator iter(
3987 pending_layer_->contents_scale_x(),
3988 gfx::Rect(layer_bounds));
3993 const Tile* tile = *iter;
3995 // Fully occluded tiles are not required for activation.
3996 if (tile->is_occluded(PENDING_TREE)) {
3997 EXPECT_FALSE(tile->required_for_activation());
3998 occluded_tile_count++;
4001 EXPECT_EQ(occluded_tile_count, 0);
4004 // Partial occlusion.
4005 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4006 LayerImpl* layer1 = pending_layer_->children()[0];
4007 layer1->SetBounds(layer_bounds);
4008 layer1->SetContentBounds(layer_bounds);
4009 layer1->SetDrawsContent(true);
4010 layer1->SetContentsOpaque(true);
4011 layer1->SetPosition(occluding_layer_position);
4013 time_ticks += base::TimeDelta::FromMilliseconds(200);
4014 host_impl_.SetCurrentBeginFrameArgs(
4015 CreateBeginFrameArgsForTesting(time_ticks));
4016 host_impl_.pending_tree()->UpdateDrawProperties();
4018 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4019 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4020 tiling->UpdateAllTilePrioritiesForTesting();
4022 occluded_tile_count = 0;
4023 for (PictureLayerTiling::CoverageIterator iter(
4025 pending_layer_->contents_scale_x(),
4026 gfx::Rect(layer_bounds));
4031 const Tile* tile = *iter;
4033 if (tile->is_occluded(PENDING_TREE)) {
4034 EXPECT_FALSE(tile->required_for_activation());
4035 occluded_tile_count++;
4040 EXPECT_EQ(occluded_tile_count, 5);
4043 EXPECT_EQ(occluded_tile_count, 2);
4051 layer1->SetPosition(gfx::PointF(0, 0));
4053 time_ticks += base::TimeDelta::FromMilliseconds(200);
4054 host_impl_.SetCurrentBeginFrameArgs(
4055 CreateBeginFrameArgsForTesting(time_ticks));
4056 host_impl_.pending_tree()->UpdateDrawProperties();
4058 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4059 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4060 tiling->UpdateAllTilePrioritiesForTesting();
4062 occluded_tile_count = 0;
4063 for (PictureLayerTiling::CoverageIterator iter(
4065 pending_layer_->contents_scale_x(),
4066 gfx::Rect(layer_bounds));
4071 const Tile* tile = *iter;
4073 if (tile->is_occluded(PENDING_TREE)) {
4074 EXPECT_FALSE(tile->required_for_activation());
4075 occluded_tile_count++;
4080 EXPECT_EQ(25, occluded_tile_count);
4083 EXPECT_EQ(4, occluded_tile_count);
4091 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
4092 gfx::Size tile_size(102, 102);
4093 gfx::Size layer_bounds(1000, 1000);
4094 gfx::Size viewport_size(500, 500);
4095 gfx::Point occluding_layer_position(310, 0);
4097 scoped_refptr<FakePicturePileImpl> pending_pile =
4098 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4099 SetupPendingTree(pending_pile);
4100 pending_layer_->set_fixed_tile_size(tile_size);
4102 ASSERT_TRUE(pending_layer_->CanHaveTilings());
4104 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4106 std::vector<PictureLayerTiling*> tilings;
4107 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4108 tilings.push_back(pending_layer_->AddTiling(0.3f));
4109 tilings.push_back(pending_layer_->AddTiling(0.7f));
4110 tilings.push_back(pending_layer_->AddTiling(1.0f));
4111 tilings.push_back(pending_layer_->AddTiling(2.0f));
4113 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4114 LayerImpl* layer1 = pending_layer_->children()[0];
4115 layer1->SetBounds(layer_bounds);
4116 layer1->SetContentBounds(layer_bounds);
4117 layer1->SetDrawsContent(true);
4118 layer1->SetContentsOpaque(true);
4119 layer1->SetPosition(occluding_layer_position);
4121 host_impl_.SetViewportSize(viewport_size);
4122 host_impl_.pending_tree()->UpdateDrawProperties();
4124 int tiling_count = 0;
4125 int occluded_tile_count = 0;
4126 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4128 tiling_iterator != tilings.end();
4129 ++tiling_iterator) {
4130 (*tiling_iterator)->UpdateAllTilePrioritiesForTesting();
4131 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4133 occluded_tile_count = 0;
4134 for (size_t i = 0; i < tiles.size(); ++i) {
4135 if (tiles[i]->is_occluded(PENDING_TREE)) {
4136 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4137 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
4138 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4139 occluded_tile_count++;
4142 switch (tiling_count) {
4145 EXPECT_EQ(occluded_tile_count, 2);
4148 EXPECT_EQ(occluded_tile_count, 4);
4151 EXPECT_EQ(occluded_tile_count, 5);
4154 EXPECT_EQ(occluded_tile_count, 30);
4163 EXPECT_EQ(tiling_count, 5);
4166 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4167 gfx::Size tile_size(102, 102);
4168 gfx::Size layer_bounds(1000, 1000);
4169 gfx::Size viewport_size(1000, 1000);
4170 gfx::Point occluding_layer_position(310, 0);
4171 gfx::Rect invalidation_rect(230, 230, 102, 102);
4173 scoped_refptr<FakePicturePileImpl> pending_pile =
4174 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4175 scoped_refptr<FakePicturePileImpl> active_pile =
4176 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4177 SetupTrees(pending_pile, active_pile);
4179 // Partially occlude the active layer.
4180 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4181 LayerImpl* layer1 = active_layer_->children()[0];
4182 layer1->SetBounds(layer_bounds);
4183 layer1->SetContentBounds(layer_bounds);
4184 layer1->SetDrawsContent(true);
4185 layer1->SetContentsOpaque(true);
4186 layer1->SetPosition(occluding_layer_position);
4188 // Partially invalidate the pending layer.
4189 pending_layer_->set_invalidation(invalidation_rect);
4191 host_impl_.SetViewportSize(viewport_size);
4193 active_layer_->CreateDefaultTilingsAndTiles();
4194 pending_layer_->CreateDefaultTilingsAndTiles();
4196 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4197 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4198 tiling->UpdateAllTilePrioritiesForTesting();
4200 for (PictureLayerTiling::CoverageIterator iter(
4202 pending_layer_->contents_scale_x(),
4203 gfx::Rect(layer_bounds));
4208 const Tile* tile = *iter;
4210 // All tiles are unoccluded on the pending tree.
4211 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4213 Tile* twin_tile = pending_layer_->GetPendingOrActiveTwinTiling(tiling)
4214 ->TileAt(iter.i(), iter.j());
4215 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4216 tile->content_rect(), 1.0f / tile->contents_scale());
4218 if (scaled_content_rect.Intersects(invalidation_rect)) {
4219 // Tiles inside the invalidation rect are only on the pending tree.
4220 EXPECT_NE(tile, twin_tile);
4222 // Unshared tiles should be unoccluded on the active tree by default.
4223 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4225 // Tiles outside the invalidation rect are shared between both trees.
4226 EXPECT_EQ(tile, twin_tile);
4227 // Shared tiles are occluded on the active tree iff they lie beneath the
4229 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4230 scaled_content_rect.x() >= occluding_layer_position.x());
4235 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4236 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4238 for (PictureLayerTiling::CoverageIterator iter(
4240 active_layer_->contents_scale_x(),
4241 gfx::Rect(layer_bounds));
4246 const Tile* tile = *iter;
4248 Tile* twin_tile = active_layer_->GetPendingOrActiveTwinTiling(tiling)
4249 ->TileAt(iter.i(), iter.j());
4250 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4251 tile->content_rect(), 1.0f / tile->contents_scale());
4253 // Since we've already checked the shared tiles, only consider tiles in
4254 // the invalidation rect.
4255 if (scaled_content_rect.Intersects(invalidation_rect)) {
4256 // Tiles inside the invalidation rect are only on the active tree.
4257 EXPECT_NE(tile, twin_tile);
4259 // Unshared tiles should be unoccluded on the pending tree by default.
4260 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4262 // Unshared tiles are occluded on the active tree iff they lie beneath
4263 // the occluding layer.
4264 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4265 scaled_content_rect.x() >= occluding_layer_position.x());
4271 TEST_F(OcclusionTrackingPictureLayerImplTest,
4272 OccludedTilesConsideredDuringEviction) {
4273 gfx::Size tile_size(102, 102);
4274 gfx::Size layer_bounds(1000, 1000);
4275 gfx::Size viewport_size(500, 500);
4276 gfx::Point pending_occluding_layer_position(310, 0);
4277 gfx::Point active_occluding_layer_position(0, 310);
4278 gfx::Rect invalidation_rect(230, 230, 102, 102);
4280 scoped_refptr<FakePicturePileImpl> pending_pile =
4281 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4282 scoped_refptr<FakePicturePileImpl> active_pile =
4283 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4284 SetupTrees(pending_pile, active_pile);
4286 pending_layer_->set_fixed_tile_size(tile_size);
4287 active_layer_->set_fixed_tile_size(tile_size);
4289 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4291 std::vector<PictureLayerTiling*> tilings;
4292 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4293 tilings.push_back(pending_layer_->AddTiling(0.3f));
4294 tilings.push_back(pending_layer_->AddTiling(0.7f));
4295 tilings.push_back(pending_layer_->AddTiling(1.0f));
4296 tilings.push_back(pending_layer_->AddTiling(2.0f));
4298 EXPECT_EQ(5u, pending_layer_->num_tilings());
4299 EXPECT_EQ(5u, active_layer_->num_tilings());
4301 // Partially occlude the pending layer.
4302 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4303 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4304 pending_occluding_layer->SetBounds(layer_bounds);
4305 pending_occluding_layer->SetContentBounds(layer_bounds);
4306 pending_occluding_layer->SetDrawsContent(true);
4307 pending_occluding_layer->SetContentsOpaque(true);
4308 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4310 // Partially occlude the active layer.
4311 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4312 LayerImpl* active_occluding_layer = active_layer_->children()[0];
4313 active_occluding_layer->SetBounds(layer_bounds);
4314 active_occluding_layer->SetContentBounds(layer_bounds);
4315 active_occluding_layer->SetDrawsContent(true);
4316 active_occluding_layer->SetContentsOpaque(true);
4317 active_occluding_layer->SetPosition(active_occluding_layer_position);
4319 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4320 // are not shared between trees.
4321 pending_layer_->set_invalidation(invalidation_rect);
4323 host_impl_.SetViewportSize(viewport_size);
4324 host_impl_.active_tree()->UpdateDrawProperties();
4325 host_impl_.pending_tree()->UpdateDrawProperties();
4327 // The expected number of occluded tiles on each of the 5 tilings for each of
4328 // the 3 tree priorities.
4329 size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
4330 size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
4331 size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
4333 // The total expected number of occluded tiles on all tilings for each of the
4334 // 3 tree priorities.
4335 size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
4337 ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
4339 // Verify number of occluded tiles on the pending layer for each tiling.
4340 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4341 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4342 tiling->CreateAllTilesForTesting();
4343 tiling->UpdateAllTilePrioritiesForTesting();
4345 size_t occluded_tile_count_on_pending = 0u;
4346 size_t occluded_tile_count_on_active = 0u;
4347 size_t occluded_tile_count_on_both = 0u;
4348 for (PictureLayerTiling::CoverageIterator iter(
4350 pending_layer_->contents_scale_x(),
4351 gfx::Rect(layer_bounds));
4356 if (tile->is_occluded(PENDING_TREE))
4357 occluded_tile_count_on_pending++;
4358 if (tile->is_occluded(ACTIVE_TREE))
4359 occluded_tile_count_on_active++;
4360 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4361 occluded_tile_count_on_both++;
4363 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4364 occluded_tile_count_on_pending)
4366 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4367 occluded_tile_count_on_active)
4369 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4370 occluded_tile_count_on_both)
4374 // Verify number of occluded tiles on the active layer for each tiling.
4375 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4376 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4377 tiling->CreateAllTilesForTesting();
4378 tiling->UpdateAllTilePrioritiesForTesting();
4380 size_t occluded_tile_count_on_pending = 0u;
4381 size_t occluded_tile_count_on_active = 0u;
4382 size_t occluded_tile_count_on_both = 0u;
4383 for (PictureLayerTiling::CoverageIterator iter(
4385 pending_layer_->contents_scale_x(),
4386 gfx::Rect(layer_bounds));
4391 if (tile->is_occluded(PENDING_TREE))
4392 occluded_tile_count_on_pending++;
4393 if (tile->is_occluded(ACTIVE_TREE))
4394 occluded_tile_count_on_active++;
4395 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4396 occluded_tile_count_on_both++;
4398 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4399 occluded_tile_count_on_pending)
4401 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4402 occluded_tile_count_on_active)
4404 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4405 occluded_tile_count_on_both)
4409 std::vector<Tile*> all_tiles;
4410 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4412 tiling_iterator != tilings.end();
4413 ++tiling_iterator) {
4414 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4415 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4418 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4420 VerifyEvictionConsidersOcclusion(pending_layer_,
4421 total_expected_occluded_tile_count);
4422 VerifyEvictionConsidersOcclusion(active_layer_,
4423 total_expected_occluded_tile_count);
4426 TEST_F(PictureLayerImplTest, PendingOrActiveTwinLayer) {
4427 gfx::Size tile_size(102, 102);
4428 gfx::Size layer_bounds(1000, 1000);
4430 scoped_refptr<FakePicturePileImpl> pile =
4431 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4432 SetupPendingTree(pile);
4433 EXPECT_FALSE(pending_layer_->GetPendingOrActiveTwinLayer());
4436 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4438 SetupPendingTree(pile);
4439 EXPECT_TRUE(pending_layer_->GetPendingOrActiveTwinLayer());
4440 EXPECT_TRUE(active_layer_->GetPendingOrActiveTwinLayer());
4441 EXPECT_EQ(pending_layer_, active_layer_->GetPendingOrActiveTwinLayer());
4442 EXPECT_EQ(active_layer_, pending_layer_->GetPendingOrActiveTwinLayer());
4445 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4447 // Make an empty pending tree.
4448 host_impl_.CreatePendingTree();
4449 host_impl_.pending_tree()->DetachLayerTree();
4450 EXPECT_FALSE(active_layer_->GetPendingOrActiveTwinLayer());
4453 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4454 gfx::Size tile_size(102, 102);
4455 gfx::Size layer_bounds(1000, 1000);
4457 scoped_refptr<FakePicturePileImpl> pile =
4458 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4459 SetupPendingTree(pile);
4460 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4463 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4464 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4466 SetupPendingTree(pile);
4467 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4468 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4471 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4472 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4474 // Make an empty pending tree.
4475 host_impl_.CreatePendingTree();
4476 host_impl_.pending_tree()->DetachLayerTree();
4477 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4480 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4481 base::TimeTicks time_ticks;
4482 time_ticks += base::TimeDelta::FromMilliseconds(1);
4483 host_impl_.SetCurrentBeginFrameArgs(
4484 CreateBeginFrameArgsForTesting(time_ticks));
4486 gfx::Size tile_size(100, 100);
4487 gfx::Size layer_bounds(200, 200);
4488 gfx::Rect layer_rect(layer_bounds);
4490 FakeContentLayerClient client;
4491 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4492 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4493 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4494 host->SetRootLayer(layer);
4495 PicturePile* pile = layer->GetPicturePileForTesting();
4497 host_impl_.SetViewportSize(layer_bounds);
4499 int frame_number = 0;
4500 FakeRenderingStatsInstrumentation stats_instrumentation;
4502 client.set_fill_with_nonsolid_color(!test_for_solid);
4504 Region invalidation(layer_rect);
4505 pile->UpdateAndExpandInvalidation(&client,
4513 Picture::RECORD_NORMALLY,
4514 &stats_instrumentation);
4516 scoped_refptr<PicturePileImpl> pending_pile =
4517 PicturePileImpl::CreateFromOther(pile);
4519 SetupPendingTree(pending_pile);
4522 active_layer_->set_fixed_tile_size(tile_size);
4523 host_impl_.active_tree()->UpdateDrawProperties();
4524 if (test_for_solid) {
4525 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4527 ASSERT_TRUE(active_layer_->tilings());
4528 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4529 std::vector<Tile*> tiles =
4530 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4531 EXPECT_FALSE(tiles.empty());
4532 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4535 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4536 AppendQuadsData data;
4537 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, nullptr);
4538 active_layer_->AppendQuads(render_pass.get(), Occlusion(), &data);
4539 active_layer_->DidDraw(nullptr);
4541 DrawQuad::Material expected = test_for_solid
4542 ? DrawQuad::Material::SOLID_COLOR
4543 : DrawQuad::Material::TILED_CONTENT;
4544 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4547 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4548 TestQuadsForSolidColor(true);
4551 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4552 TestQuadsForSolidColor(false);
4555 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4556 base::TimeTicks time_ticks;
4557 time_ticks += base::TimeDelta::FromMilliseconds(1);
4558 host_impl_.SetCurrentBeginFrameArgs(
4559 CreateBeginFrameArgsForTesting(time_ticks));
4561 gfx::Size tile_size(100, 100);
4562 gfx::Size layer_bounds(200, 200);
4563 gfx::Rect layer_rect(layer_bounds);
4565 FakeContentLayerClient client;
4566 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4567 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4568 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4569 host->SetRootLayer(layer);
4570 PicturePile* pile = layer->GetPicturePileForTesting();
4572 host_impl_.SetViewportSize(layer_bounds);
4574 int frame_number = 0;
4575 FakeRenderingStatsInstrumentation stats_instrumentation;
4577 client.set_fill_with_nonsolid_color(true);
4579 Region invalidation1(layer_rect);
4580 pile->UpdateAndExpandInvalidation(&client,
4588 Picture::RECORD_NORMALLY,
4589 &stats_instrumentation);
4591 scoped_refptr<PicturePileImpl> pending_pile1 =
4592 PicturePileImpl::CreateFromOther(pile);
4594 SetupPendingTree(pending_pile1);
4596 host_impl_.active_tree()->UpdateDrawProperties();
4598 // We've started with a solid layer that contains some tilings.
4599 ASSERT_TRUE(active_layer_->tilings());
4600 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4602 client.set_fill_with_nonsolid_color(false);
4604 Region invalidation2(layer_rect);
4605 pile->UpdateAndExpandInvalidation(&client,
4613 Picture::RECORD_NORMALLY,
4614 &stats_instrumentation);
4616 scoped_refptr<PicturePileImpl> pending_pile2 =
4617 PicturePileImpl::CreateFromOther(pile);
4619 SetupPendingTree(pending_pile2);
4622 // We've switched to a solid color, so we should end up with no tilings.
4623 ASSERT_TRUE(active_layer_->tilings());
4624 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4627 TEST_F(PictureLayerImplTest, ChangeInViewportAllowsTilingUpdates) {
4628 base::TimeTicks time_ticks;
4629 time_ticks += base::TimeDelta::FromMilliseconds(1);
4630 host_impl_.SetCurrentBeginFrameArgs(
4631 CreateBeginFrameArgsForTesting(time_ticks));
4633 gfx::Size tile_size(100, 100);
4634 gfx::Size layer_bounds(400, 4000);
4636 scoped_refptr<FakePicturePileImpl> pending_pile =
4637 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4638 scoped_refptr<FakePicturePileImpl> active_pile =
4639 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4641 SetupTrees(pending_pile, active_pile);
4643 Region invalidation;
4644 gfx::Rect viewport = gfx::Rect(0, 0, 100, 100);
4645 gfx::Transform transform;
4647 host_impl_.SetRequiresHighResToDraw();
4650 pending_layer_->draw_properties().visible_content_rect = viewport;
4651 pending_layer_->draw_properties().screen_space_transform = transform;
4652 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4653 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4655 // Ensure we can't activate.
4656 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
4658 // Now in the same frame, move the viewport (this can happen during
4660 viewport = gfx::Rect(0, 2000, 100, 100);
4663 pending_layer_->draw_properties().visible_content_rect = viewport;
4664 pending_layer_->draw_properties().screen_space_transform = transform;
4665 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
4666 pending_layer_->HighResTiling()->UpdateAllTilePrioritiesForTesting();
4668 // Make sure all viewport tiles (viewport from the tiling) are ready to draw.
4669 std::vector<Tile*> tiles;
4670 for (PictureLayerTiling::CoverageIterator iter(
4671 pending_layer_->HighResTiling(),
4673 pending_layer_->HighResTiling()->GetCurrentVisibleRectForTesting());
4677 tiles.push_back(*iter);
4680 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4682 // Ensure we can activate.
4683 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
4686 class TileSizeSettings : public ImplSidePaintingSettings {
4688 TileSizeSettings() {
4689 default_tile_size = gfx::Size(100, 100);
4690 max_untiled_layer_size = gfx::Size(200, 200);
4694 class TileSizeTest : public PictureLayerImplTest {
4696 TileSizeTest() : PictureLayerImplTest(TileSizeSettings()) {}
4699 TEST_F(TileSizeTest, TileSizes) {
4700 host_impl_.CreatePendingTree();
4702 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
4703 scoped_ptr<FakePictureLayerImpl> layer =
4704 FakePictureLayerImpl::Create(pending_tree, id_);
4706 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4709 host_impl_.SetUseGpuRasterization(false);
4711 // Default tile-size for large layers.
4712 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4713 EXPECT_EQ(result.width(), 100);
4714 EXPECT_EQ(result.height(), 100);
4715 // Don't tile and round-up, when under max_untiled_layer_size.
4716 result = layer->CalculateTileSize(gfx::Size(42, 42));
4717 EXPECT_EQ(result.width(), 64);
4718 EXPECT_EQ(result.height(), 64);
4719 result = layer->CalculateTileSize(gfx::Size(191, 191));
4720 EXPECT_EQ(result.width(), 192);
4721 EXPECT_EQ(result.height(), 192);
4722 result = layer->CalculateTileSize(gfx::Size(199, 199));
4723 EXPECT_EQ(result.width(), 200);
4724 EXPECT_EQ(result.height(), 200);
4726 // Gpu-rasterization uses 25% viewport-height tiles.
4727 // The +2's below are for border texels.
4728 host_impl_.SetUseGpuRasterization(true);
4729 host_impl_.SetViewportSize(gfx::Size(2000, 2000));
4730 result = layer->CalculateTileSize(gfx::Size(10000, 10000));
4731 EXPECT_EQ(result.width(), 2000);
4732 EXPECT_EQ(result.height(), 500 + 2);
4734 // Clamp and round-up, when smaller than viewport.
4735 // Tile-height doubles to 50% when width shrinks to <= 50%.
4736 host_impl_.SetViewportSize(gfx::Size(1000, 1000));
4737 result = layer->CalculateTileSize(gfx::Size(447, 10000));
4738 EXPECT_EQ(result.width(), 448);
4739 EXPECT_EQ(result.height(), 500 + 2);
4741 // Largest layer is 50% of viewport width (rounded up), and
4742 // 50% of viewport in height.
4743 result = layer->CalculateTileSize(gfx::Size(447, 400));
4744 EXPECT_EQ(result.width(), 448);
4745 EXPECT_EQ(result.height(), 448);
4746 result = layer->CalculateTileSize(gfx::Size(500, 499));
4747 EXPECT_EQ(result.width(), 512);
4748 EXPECT_EQ(result.height(), 500 + 2);