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/rect_conversions.h"
32 #include "ui/gfx/size_conversions.h"
37 class MockCanvas : public SkCanvas {
39 explicit MockCanvas(int w, int h) : SkCanvas(w, h) {}
41 virtual 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_),
63 old_pending_layer_(NULL),
64 active_layer_(NULL) {}
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(
80 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
83 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
84 gfx::Size tile_size(100, 100);
86 scoped_refptr<FakePicturePileImpl> pending_pile =
87 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
88 scoped_refptr<FakePicturePileImpl> active_pile =
89 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
91 SetupTrees(pending_pile, active_pile);
95 host_impl_.ActivateSyncTree();
96 CHECK(!host_impl_.pending_tree());
97 CHECK(host_impl_.recycle_tree());
98 old_pending_layer_ = pending_layer_;
99 pending_layer_ = NULL;
100 active_layer_ = static_cast<FakePictureLayerImpl*>(
101 host_impl_.active_tree()->LayerById(id_));
104 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
105 const gfx::Size& tile_size) {
106 SetupDefaultTrees(layer_bounds);
107 pending_layer_->set_fixed_tile_size(tile_size);
108 active_layer_->set_fixed_tile_size(tile_size);
112 scoped_refptr<PicturePileImpl> pending_pile,
113 scoped_refptr<PicturePileImpl> active_pile) {
114 SetupPendingTree(active_pile);
116 SetupPendingTree(pending_pile);
119 void CreateHighLowResAndSetAllTilesVisible() {
120 // Active layer must get updated first so pending layer can share from it.
121 active_layer_->CreateDefaultTilingsAndTiles();
122 active_layer_->SetAllTilesVisible();
123 pending_layer_->CreateDefaultTilingsAndTiles();
124 pending_layer_->SetAllTilesVisible();
127 void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
128 active_layer_->AddTiling(2.3f);
129 active_layer_->AddTiling(1.0f);
130 active_layer_->AddTiling(0.5f);
131 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
132 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
133 pending_layer_->set_invalidation(invalidation);
134 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
135 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
138 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
139 host_impl_.CreatePendingTree();
140 host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
141 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
142 // Clear recycled tree.
143 pending_tree->DetachLayerTree();
145 scoped_ptr<FakePictureLayerImpl> pending_layer =
146 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
147 pending_layer->SetDrawsContent(true);
148 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
150 pending_layer_ = static_cast<FakePictureLayerImpl*>(
151 host_impl_.pending_tree()->LayerById(id_));
152 pending_layer_->DoPostCommitInitializationIfNeeded();
155 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
156 float ideal_contents_scale,
157 float device_scale_factor,
158 float page_scale_factor,
159 float maximum_animation_contents_scale,
160 bool animating_transform_to_screen) {
161 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
162 layer->draw_properties().device_scale_factor = device_scale_factor;
163 layer->draw_properties().page_scale_factor = page_scale_factor;
164 layer->draw_properties().maximum_animation_contents_scale =
165 maximum_animation_contents_scale;
166 layer->draw_properties().screen_space_transform_is_animating =
167 animating_transform_to_screen;
168 bool resourceless_software_draw = false;
169 layer->UpdateTiles(Occlusion(), resourceless_software_draw);
171 static void VerifyAllTilesExistAndHavePile(
172 const PictureLayerTiling* tiling,
173 PicturePileImpl* pile) {
174 for (PictureLayerTiling::CoverageIterator iter(
176 tiling->contents_scale(),
177 gfx::Rect(tiling->tiling_size()));
181 EXPECT_EQ(pile, iter->picture_pile());
185 void SetContentsScaleOnBothLayers(float contents_scale,
186 float device_scale_factor,
187 float page_scale_factor,
188 float maximum_animation_contents_scale,
189 bool animating_transform) {
190 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
194 maximum_animation_contents_scale,
195 animating_transform);
197 SetupDrawPropertiesAndUpdateTiles(active_layer_,
201 maximum_animation_contents_scale,
202 animating_transform);
205 void ResetTilingsAndRasterScales() {
206 pending_layer_->ReleaseResources();
207 active_layer_->ReleaseResources();
210 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
211 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
212 for (size_t i = 0; i < tiles.size(); ++i)
213 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
214 EXPECT_GT(tiles.size(), 0u);
217 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
218 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
219 for (size_t i = 0; i < tiles.size(); ++i)
220 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
221 EXPECT_GT(tiles.size(), 0u);
225 void TestTileGridAlignmentCommon() {
226 // Layer to span 4 raster tiles in x and in y
227 ImplSidePaintingSettings settings;
228 gfx::Size layer_size(
229 settings.default_tile_size.width() * 7 / 2,
230 settings.default_tile_size.height() * 7 / 2);
232 scoped_refptr<FakePicturePileImpl> pending_pile =
233 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
234 scoped_refptr<FakePicturePileImpl> active_pile =
235 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
237 SetupTrees(pending_pile, active_pile);
239 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
241 // Add 1x1 rects at the centers of each tile, then re-record pile contents
242 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
243 std::vector<Tile*> tiles =
244 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
245 EXPECT_EQ(16u, tiles.size());
246 std::vector<SkRect> rects;
247 std::vector<Tile*>::const_iterator tile_iter;
248 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
249 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
250 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
251 active_pile->add_draw_rect(rect);
252 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
254 // Force re-record with newly injected content
255 active_pile->RemoveRecordingAt(0, 0);
256 active_pile->AddRecordingAt(0, 0);
258 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
259 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
260 MockCanvas mock_canvas(1000, 1000);
261 active_pile->RasterDirect(
262 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
264 // This test verifies that when drawing the contents of a specific tile
265 // at content scale 1.0, the playback canvas never receives content from
266 // neighboring tiles which indicates that the tile grid embedded in
267 // SkPicture is perfectly aligned with the compositor's tiles.
268 EXPECT_EQ(1u, mock_canvas.rects_.size());
269 EXPECT_EQ(*rect_iter, mock_canvas.rects_[0]);
274 void TestQuadsForSolidColor(bool test_for_solid);
276 FakeImplProxy proxy_;
277 TestSharedBitmapManager shared_bitmap_manager_;
278 FakeLayerTreeHostImpl host_impl_;
280 FakePictureLayerImpl* pending_layer_;
281 FakePictureLayerImpl* old_pending_layer_;
282 FakePictureLayerImpl* active_layer_;
285 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
288 TEST_F(PictureLayerImplTest, TileGridAlignment) {
289 host_impl_.SetDeviceScaleFactor(1.f);
290 TestTileGridAlignmentCommon();
293 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
294 host_impl_.SetDeviceScaleFactor(2.f);
295 TestTileGridAlignmentCommon();
298 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
299 gfx::Size tile_size(100, 100);
300 gfx::Size layer_bounds(400, 400);
302 scoped_refptr<FakePicturePileImpl> pending_pile =
303 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
304 scoped_refptr<FakePicturePileImpl> active_pile =
305 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
307 SetupTrees(pending_pile, active_pile);
310 AddDefaultTilingsWithInvalidation(invalidation);
312 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
313 active_layer_->tilings()->num_tilings());
315 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
316 EXPECT_GT(tilings->num_tilings(), 0u);
317 for (size_t i = 0; i < tilings->num_tilings(); ++i)
318 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
321 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
322 base::TimeTicks time_ticks;
323 time_ticks += base::TimeDelta::FromMilliseconds(1);
324 host_impl_.SetCurrentBeginFrameArgs(
325 CreateBeginFrameArgsForTesting(time_ticks));
326 gfx::Size tile_size(100, 100);
327 gfx::Size layer_bounds(400, 400);
329 scoped_refptr<FakePicturePileImpl> pending_pile =
330 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
331 scoped_refptr<FakePicturePileImpl> active_pile =
332 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
334 SetupTrees(pending_pile, active_pile);
337 AddDefaultTilingsWithInvalidation(invalidation);
338 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
340 time_ticks += base::TimeDelta::FromMilliseconds(200);
341 host_impl_.SetCurrentBeginFrameArgs(
342 CreateBeginFrameArgsForTesting(time_ticks));
344 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
345 // identify transform for tile priority.
346 bool resourceless_software_draw = false;
347 gfx::Rect viewport = gfx::Rect(layer_bounds),
348 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
349 gfx::Transform transform, transform_for_tile_priority;
351 host_impl_.SetExternalDrawConstraints(transform,
354 viewport_rect_for_tile_priority,
355 transform_for_tile_priority,
356 resourceless_software_draw);
357 active_layer_->draw_properties().visible_content_rect = viewport;
358 active_layer_->draw_properties().screen_space_transform = transform;
359 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
361 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
362 viewport_rect_for_tile_priority;
364 // Verify the viewport rect for tile priority is used in picture layer impl.
365 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
366 viewport_rect_for_tile_priority_in_view_space);
368 // Verify the viewport rect for tile priority is used in picture layer tiling.
369 PictureLayerTilingSet* tilings = active_layer_->tilings();
370 for (size_t i = 0; i < tilings->num_tilings(); i++) {
371 PictureLayerTiling* tiling = tilings->tiling_at(i);
373 tiling->GetCurrentVisibleRectForTesting(),
374 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
375 tiling->contents_scale()));
378 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
379 // screen space and the transform for tile priority is translated and
380 // rotated. The actual viewport for tile priority used by PictureLayerImpl
381 // should be (200, 200, 100, 100) applied with the said transform.
382 time_ticks += base::TimeDelta::FromMilliseconds(200);
383 host_impl_.SetCurrentBeginFrameArgs(
384 CreateBeginFrameArgsForTesting(time_ticks));
386 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
387 transform_for_tile_priority.Translate(100, 100);
388 transform_for_tile_priority.Rotate(45);
389 host_impl_.SetExternalDrawConstraints(transform,
392 viewport_rect_for_tile_priority,
393 transform_for_tile_priority,
394 resourceless_software_draw);
395 active_layer_->draw_properties().visible_content_rect = viewport;
396 active_layer_->draw_properties().screen_space_transform = transform;
397 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
399 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
400 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
401 EXPECT_TRUE(success);
403 // Note that we don't clip this to the layer bounds, since it is expected that
404 // the rect will sometimes be outside of the layer bounds. If we clip to
405 // bounds, then tile priorities will end up being incorrect in cases of fully
407 viewport_rect_for_tile_priority_in_view_space =
408 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
409 screen_to_view, viewport_rect_for_tile_priority));
411 // Verify the viewport rect for tile priority is used in PictureLayerImpl.
412 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
413 viewport_rect_for_tile_priority_in_view_space);
415 tilings = active_layer_->tilings();
416 for (size_t i = 0; i < tilings->num_tilings(); i++) {
417 PictureLayerTiling* tiling = tilings->tiling_at(i);
419 tiling->GetCurrentVisibleRectForTesting(),
420 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
421 tiling->contents_scale()));
425 TEST_F(PictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
426 base::TimeTicks time_ticks;
427 time_ticks += base::TimeDelta::FromMilliseconds(1);
428 host_impl_.SetCurrentBeginFrameArgs(
429 CreateBeginFrameArgsForTesting(time_ticks));
431 gfx::Size tile_size(100, 100);
432 gfx::Size layer_bounds(400, 400);
434 scoped_refptr<FakePicturePileImpl> pending_pile =
435 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
436 scoped_refptr<FakePicturePileImpl> active_pile =
437 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
439 SetupTrees(pending_pile, active_pile);
442 AddDefaultTilingsWithInvalidation(invalidation);
443 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
445 // UpdateTiles with valid viewport. Should update tile viewport.
446 // Note viewport is considered invalid if and only if in resourceless
448 bool resourceless_software_draw = false;
449 gfx::Rect viewport = gfx::Rect(layer_bounds);
450 gfx::Transform transform;
451 host_impl_.SetExternalDrawConstraints(transform,
456 resourceless_software_draw);
457 active_layer_->draw_properties().visible_content_rect = viewport;
458 active_layer_->draw_properties().screen_space_transform = transform;
459 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
461 gfx::Rect visible_rect_for_tile_priority =
462 active_layer_->visible_rect_for_tile_priority();
463 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
464 gfx::Rect viewport_rect_for_tile_priority =
465 active_layer_->viewport_rect_for_tile_priority();
466 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
467 gfx::Transform screen_space_transform_for_tile_priority =
468 active_layer_->screen_space_transform_for_tile_priority();
470 // Expand viewport and set it as invalid for prioritizing tiles.
471 // Should update viewport and transform, but not update visible rect.
472 time_ticks += base::TimeDelta::FromMilliseconds(200);
473 host_impl_.SetCurrentBeginFrameArgs(
474 CreateBeginFrameArgsForTesting(time_ticks));
475 resourceless_software_draw = true;
476 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
477 transform.Translate(1.f, 1.f);
478 active_layer_->draw_properties().visible_content_rect = viewport;
479 active_layer_->draw_properties().screen_space_transform = transform;
480 host_impl_.SetExternalDrawConstraints(transform,
485 resourceless_software_draw);
486 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
488 // Viewport and transform for tile priority are updated.
489 EXPECT_EQ(viewport, active_layer_->viewport_rect_for_tile_priority());
490 EXPECT_TRANSFORMATION_MATRIX_EQ(
491 transform, active_layer_->screen_space_transform_for_tile_priority());
492 // Visible rect for tile priority retains old value.
493 EXPECT_EQ(visible_rect_for_tile_priority,
494 active_layer_->visible_rect_for_tile_priority());
496 // Keep expanded viewport but mark it valid. Should update tile viewport.
497 time_ticks += base::TimeDelta::FromMilliseconds(200);
498 host_impl_.SetCurrentBeginFrameArgs(
499 CreateBeginFrameArgsForTesting(time_ticks));
500 resourceless_software_draw = false;
501 host_impl_.SetExternalDrawConstraints(transform,
506 resourceless_software_draw);
507 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
509 EXPECT_TRANSFORMATION_MATRIX_EQ(
510 transform, active_layer_->screen_space_transform_for_tile_priority());
511 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
513 // Match the reverse translate in |transform|.
514 EXPECT_EQ(viewport - gfx::Vector2d(1, 1),
515 active_layer_->viewport_rect_for_tile_priority());
518 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
519 gfx::Size tile_size(100, 100);
520 gfx::Size layer_bounds(400, 400);
521 gfx::Rect layer_invalidation(150, 200, 30, 180);
523 scoped_refptr<FakePicturePileImpl> pending_pile =
524 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
525 scoped_refptr<FakePicturePileImpl> active_pile =
526 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
528 SetupTrees(pending_pile, active_pile);
530 Region invalidation(layer_invalidation);
531 AddDefaultTilingsWithInvalidation(invalidation);
533 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
534 EXPECT_GT(tilings->num_tilings(), 0u);
535 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
536 const PictureLayerTiling* tiling = tilings->tiling_at(i);
537 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
539 tiling->contents_scale());
540 for (PictureLayerTiling::CoverageIterator iter(
542 tiling->contents_scale(),
543 gfx::Rect(tiling->tiling_size()));
547 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
548 if (iter.geometry_rect().Intersects(content_invalidation))
549 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
551 EXPECT_EQ(active_pile.get(), iter->picture_pile());
556 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
557 gfx::Size tile_size(90, 80);
558 gfx::Size layer_bounds(300, 500);
560 scoped_refptr<FakePicturePileImpl> pending_pile =
561 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
562 scoped_refptr<FakePicturePileImpl> active_pile =
563 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
565 SetupTrees(pending_pile, active_pile);
567 Region invalidation((gfx::Rect(layer_bounds)));
568 AddDefaultTilingsWithInvalidation(invalidation);
570 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
571 active_layer_->tilings()->num_tilings());
573 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
574 EXPECT_GT(tilings->num_tilings(), 0u);
575 for (size_t i = 0; i < tilings->num_tilings(); ++i)
576 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
579 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
580 gfx::Size tile_size(90, 80);
581 gfx::Size active_layer_bounds(300, 500);
582 gfx::Size pending_layer_bounds(400, 800);
584 scoped_refptr<FakePicturePileImpl> pending_pile =
585 FakePicturePileImpl::CreateFilledPile(tile_size,
586 pending_layer_bounds);
587 scoped_refptr<FakePicturePileImpl> active_pile =
588 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
590 SetupTrees(pending_pile, active_pile);
591 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
594 AddDefaultTilingsWithInvalidation(invalidation);
596 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
597 EXPECT_GT(tilings->num_tilings(), 0u);
598 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
599 const PictureLayerTiling* tiling = tilings->tiling_at(i);
600 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
601 gfx::Rect(active_layer_bounds),
602 tiling->contents_scale());
603 for (PictureLayerTiling::CoverageIterator iter(
605 tiling->contents_scale(),
606 gfx::Rect(tiling->tiling_size()));
610 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
611 std::vector<Tile*> active_tiles =
612 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
613 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
614 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
615 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
616 active_tiles[0]->content_rect().size() !=
617 pending_tiles[0]->content_rect().size()) {
618 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
620 EXPECT_EQ(active_pile.get(), iter->picture_pile());
626 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
627 gfx::Size tile_size(400, 400);
628 gfx::Size layer_bounds(1300, 1900);
630 scoped_refptr<FakePicturePileImpl> pending_pile =
631 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
632 scoped_refptr<FakePicturePileImpl> active_pile =
633 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
635 // Fill in some of active pile, but more of pending pile.
637 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
638 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
640 pending_pile->AddRecordingAt(x, y);
641 active_pile->AddRecordingAt(x, y);
645 pending_pile->AddRecordingAt(x, y);
650 SetupTrees(pending_pile, active_pile);
652 AddDefaultTilingsWithInvalidation(invalidation);
654 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
655 EXPECT_GT(tilings->num_tilings(), 0u);
656 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
657 const PictureLayerTiling* tiling = tilings->tiling_at(i);
659 for (PictureLayerTiling::CoverageIterator iter(
661 tiling->contents_scale(),
662 gfx::Rect(tiling->tiling_size()));
665 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
666 // Ensure there is a recording for this tile.
667 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
668 iter.full_tile_geometry_rect());
669 bool in_active = active_pile->CanRaster(tiling->contents_scale(),
670 iter.full_tile_geometry_rect());
672 if (in_pending && !in_active)
673 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
675 EXPECT_EQ(active_pile.get(), iter->picture_pile());
682 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
683 gfx::Size tile_size(400, 400);
684 gfx::Size layer_bounds(1300, 1900);
686 scoped_refptr<FakePicturePileImpl> pending_pile =
687 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
688 scoped_refptr<FakePicturePileImpl> active_pile =
689 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
691 SetupTrees(pending_pile, active_pile);
693 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
695 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
698 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
699 gfx::Size tile_size(400, 400);
700 gfx::Size layer_bounds(1300, 1900);
702 scoped_refptr<FakePicturePileImpl> pending_pile =
703 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
704 scoped_refptr<FakePicturePileImpl> active_pile =
705 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
707 SetupTrees(pending_pile, active_pile);
708 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
710 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
711 EXPECT_LT(low_res_factor, 1.f);
713 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
714 6.f, // ideal contents scale
717 1.f, // maximum animation scale
719 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
721 pending_layer_->tilings()->tiling_at(0)->contents_scale());
722 EXPECT_FLOAT_EQ(6.f * low_res_factor,
723 pending_layer_->tilings()->tiling_at(1)->contents_scale());
725 // If we change the page scale factor, then we should get new tilings.
726 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
727 6.6f, // ideal contents scale
730 1.f, // maximum animation scale
732 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
733 EXPECT_FLOAT_EQ(6.6f,
734 pending_layer_->tilings()->tiling_at(0)->contents_scale());
735 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
736 pending_layer_->tilings()->tiling_at(2)->contents_scale());
738 // If we change the device scale factor, then we should get new tilings.
739 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
740 7.26f, // ideal contents scale
741 3.3f, // device scale
743 1.f, // maximum animation scale
745 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
746 EXPECT_FLOAT_EQ(7.26f,
747 pending_layer_->tilings()->tiling_at(0)->contents_scale());
748 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
749 pending_layer_->tilings()->tiling_at(3)->contents_scale());
751 // If we change the device scale factor, but end up at the same total scale
752 // factor somehow, then we don't get new tilings.
753 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
754 7.26f, // ideal contents scale
755 2.2f, // device scale
757 1.f, // maximum animation scale
759 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
760 EXPECT_FLOAT_EQ(7.26f,
761 pending_layer_->tilings()->tiling_at(0)->contents_scale());
762 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
763 pending_layer_->tilings()->tiling_at(3)->contents_scale());
766 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
767 // This test makes sure that if a layer can have tilings, then a commit makes
768 // it not able to have tilings (empty size), and then a future commit that
769 // makes it valid again should be able to create tilings.
770 gfx::Size tile_size(400, 400);
771 gfx::Size layer_bounds(1300, 1900);
773 scoped_refptr<FakePicturePileImpl> empty_pile =
774 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
775 scoped_refptr<FakePicturePileImpl> valid_pile =
776 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
778 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
779 EXPECT_LT(low_res_factor, 1.f);
781 float high_res_scale = 1.3f;
782 float low_res_scale = high_res_scale * low_res_factor;
783 float device_scale = 1.7f;
784 float page_scale = 3.2f;
785 float maximum_animation_scale = 1.f;
787 SetupPendingTree(valid_pile);
788 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
792 maximum_animation_scale,
794 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
795 EXPECT_FLOAT_EQ(high_res_scale,
796 pending_layer_->HighResTiling()->contents_scale());
797 EXPECT_FLOAT_EQ(low_res_scale,
798 pending_layer_->LowResTiling()->contents_scale());
801 SetupPendingTree(empty_pile);
802 EXPECT_FALSE(pending_layer_->CanHaveTilings());
803 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
807 maximum_animation_scale,
809 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
810 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
813 EXPECT_FALSE(active_layer_->CanHaveTilings());
814 SetupDrawPropertiesAndUpdateTiles(active_layer_,
818 maximum_animation_scale,
820 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
822 SetupPendingTree(valid_pile);
823 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
827 maximum_animation_scale,
829 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
830 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
831 EXPECT_FLOAT_EQ(high_res_scale,
832 pending_layer_->HighResTiling()->contents_scale());
833 EXPECT_FLOAT_EQ(low_res_scale,
834 pending_layer_->LowResTiling()->contents_scale());
837 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
838 gfx::Size tile_size(400, 400);
839 gfx::Size layer_bounds(1300, 1900);
841 // Set up the high and low res tilings before pinch zoom.
842 scoped_refptr<FakePicturePileImpl> pending_pile =
843 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
844 scoped_refptr<FakePicturePileImpl> active_pile =
845 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
847 SetupTrees(pending_pile, active_pile);
848 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
849 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
850 host_impl_.PinchGestureBegin();
851 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
852 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
853 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
856 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
857 gfx::Size tile_size(400, 400);
858 gfx::Size layer_bounds(1300, 1900);
860 scoped_refptr<FakePicturePileImpl> pending_pile =
861 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
862 scoped_refptr<FakePicturePileImpl> active_pile =
863 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
865 // Set up the high and low res tilings before pinch zoom.
866 SetupTrees(pending_pile, active_pile);
867 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
868 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
869 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
870 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
871 EXPECT_FLOAT_EQ(2.0f,
872 active_layer_->tilings()->tiling_at(0)->contents_scale());
873 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
874 active_layer_->tilings()->tiling_at(1)->contents_scale());
876 // Start a pinch gesture.
877 host_impl_.PinchGestureBegin();
879 // Zoom out by a small amount. We should create a tiling at half
880 // the scale (2/kMaxScaleRatioDuringPinch).
881 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
882 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
883 EXPECT_FLOAT_EQ(2.0f,
884 active_layer_->tilings()->tiling_at(0)->contents_scale());
885 EXPECT_FLOAT_EQ(1.0f,
886 active_layer_->tilings()->tiling_at(1)->contents_scale());
887 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
888 active_layer_->tilings()->tiling_at(2)->contents_scale());
890 // Zoom out further, close to our low-res scale factor. We should
891 // use that tiling as high-res, and not create a new tiling.
892 SetContentsScaleOnBothLayers(
893 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
894 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
896 // Zoom in a lot now. Since we increase by increments of
897 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
898 // and then finally create a new tiling at 4.0.
899 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
900 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
901 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
902 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
903 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
904 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
905 EXPECT_FLOAT_EQ(4.0f,
906 active_layer_->tilings()->tiling_at(0)->contents_scale());
909 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
910 gfx::Size tile_size(300, 300);
911 gfx::Size layer_bounds(2600, 3800);
913 scoped_refptr<FakePicturePileImpl> pending_pile =
914 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
915 scoped_refptr<FakePicturePileImpl> active_pile =
916 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
918 // Set up the high and low res tilings before pinch zoom.
919 SetupTrees(pending_pile, active_pile);
920 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
921 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
922 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
923 EXPECT_FLOAT_EQ(0.24f,
924 active_layer_->tilings()->tiling_at(0)->contents_scale());
925 EXPECT_FLOAT_EQ(0.0625f,
926 active_layer_->tilings()->tiling_at(1)->contents_scale());
928 // Start a pinch gesture.
929 host_impl_.PinchGestureBegin();
931 // Zoom out by a small amount. We should create a tiling at half
932 // the scale (1/kMaxScaleRatioDuringPinch).
933 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
934 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
935 EXPECT_FLOAT_EQ(0.24f,
936 active_layer_->tilings()->tiling_at(0)->contents_scale());
937 EXPECT_FLOAT_EQ(0.12f,
938 active_layer_->tilings()->tiling_at(1)->contents_scale());
939 EXPECT_FLOAT_EQ(0.0625,
940 active_layer_->tilings()->tiling_at(2)->contents_scale());
942 // Zoom out further, close to our low-res scale factor. We should
943 // use that tiling as high-res, and not create a new tiling.
944 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
945 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
947 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
948 // because 0.125(desired_scale) is within the ratio(1.2)
949 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
950 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
953 TEST_F(PictureLayerImplTest, CleanUpTilings) {
954 gfx::Size tile_size(400, 400);
955 gfx::Size layer_bounds(1300, 1900);
957 scoped_refptr<FakePicturePileImpl> pending_pile =
958 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
959 scoped_refptr<FakePicturePileImpl> active_pile =
960 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
962 std::vector<PictureLayerTiling*> used_tilings;
964 SetupTrees(pending_pile, active_pile);
965 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
967 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
968 EXPECT_LT(low_res_factor, 1.f);
970 float device_scale = 1.7f;
971 float page_scale = 3.2f;
974 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
975 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
977 // We only have ideal tilings, so they aren't removed.
978 used_tilings.clear();
979 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
980 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
982 host_impl_.PinchGestureBegin();
984 // Changing the ideal but not creating new tilings.
987 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
988 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
990 // The tilings are still our target scale, so they aren't removed.
991 used_tilings.clear();
992 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
993 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
995 host_impl_.PinchGestureEnd();
997 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
1000 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
1001 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1004 active_layer_->tilings()->tiling_at(1)->contents_scale());
1006 1.f * low_res_factor,
1007 active_layer_->tilings()->tiling_at(3)->contents_scale());
1009 // Mark the non-ideal tilings as used. They won't be removed.
1010 used_tilings.clear();
1011 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1012 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
1013 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1014 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
1016 // Now move the ideal scale to 0.5. Our target stays 1.2.
1017 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1019 // The high resolution tiling is between target and ideal, so is not
1020 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1021 used_tilings.clear();
1022 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1023 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1025 // Now move the ideal scale to 1.0. Our target stays 1.2.
1026 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1028 // All the tilings are between are target and the ideal, so they are not
1030 used_tilings.clear();
1031 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1032 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1034 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1035 SetupDrawPropertiesAndUpdateTiles(
1036 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1038 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1039 // in the range [1.0,1.2] and are kept.
1040 used_tilings.clear();
1041 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1042 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1044 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1046 SetupDrawPropertiesAndUpdateTiles(
1047 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1049 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1050 // target raster scale. But it is in our used tilings set, so nothing is
1052 used_tilings.clear();
1053 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1054 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1055 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1057 // If we remove it from our used tilings set, it is outside the range to keep
1058 // so it is deleted.
1059 used_tilings.clear();
1060 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1061 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1064 #define EXPECT_BOTH_EQ(expression, x) \
1066 EXPECT_EQ(x, pending_layer_->expression); \
1067 EXPECT_EQ(x, active_layer_->expression); \
1070 #define EXPECT_BOTH_NE(expression, x) \
1072 EXPECT_NE(x, pending_layer_->expression); \
1073 EXPECT_NE(x, active_layer_->expression); \
1076 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1077 // Make sure this layer covers multiple tiles, since otherwise low
1078 // res won't get created because it is too small.
1079 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1080 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1081 // Avoid max untiled layer size heuristics via fixed tile size.
1082 pending_layer_->set_fixed_tile_size(tile_size);
1083 active_layer_->set_fixed_tile_size(tile_size);
1085 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1086 float contents_scale = 1.f;
1087 float device_scale = 1.f;
1088 float page_scale = 1.f;
1089 float maximum_animation_scale = 1.f;
1090 bool animating_transform = true;
1092 // Animating, so don't create low res even if there isn't one already.
1093 SetContentsScaleOnBothLayers(contents_scale,
1096 maximum_animation_scale,
1097 animating_transform);
1098 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1099 EXPECT_BOTH_EQ(num_tilings(), 1u);
1101 // Stop animating, low res gets created.
1102 animating_transform = false;
1103 SetContentsScaleOnBothLayers(contents_scale,
1106 maximum_animation_scale,
1107 animating_transform);
1108 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1109 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1110 EXPECT_BOTH_EQ(num_tilings(), 2u);
1112 // Page scale animation, new high res, but not new low res because animating.
1113 contents_scale = 2.f;
1115 maximum_animation_scale = 2.f;
1116 animating_transform = true;
1117 SetContentsScaleOnBothLayers(contents_scale,
1120 maximum_animation_scale,
1121 animating_transform);
1122 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1123 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1124 EXPECT_BOTH_EQ(num_tilings(), 3u);
1126 // Stop animating, new low res gets created for final page scale.
1127 animating_transform = false;
1128 SetContentsScaleOnBothLayers(contents_scale,
1131 maximum_animation_scale,
1132 animating_transform);
1133 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1134 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1135 EXPECT_BOTH_EQ(num_tilings(), 4u);
1138 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1139 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1140 SetupDefaultTrees(tile_size);
1142 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1143 float device_scale = 1.f;
1144 float page_scale = 1.f;
1145 float maximum_animation_scale = 1.f;
1146 bool animating_transform = false;
1148 // Contents exactly fit on one tile at scale 1, no low res.
1149 float contents_scale = 1.f;
1150 SetContentsScaleOnBothLayers(contents_scale,
1153 maximum_animation_scale,
1154 animating_transform);
1155 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1156 EXPECT_BOTH_EQ(num_tilings(), 1u);
1158 ResetTilingsAndRasterScales();
1160 // Contents that are smaller than one tile, no low res.
1161 contents_scale = 0.123f;
1162 SetContentsScaleOnBothLayers(contents_scale,
1165 maximum_animation_scale,
1166 animating_transform);
1167 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1168 EXPECT_BOTH_EQ(num_tilings(), 1u);
1170 ResetTilingsAndRasterScales();
1172 // Any content bounds that would create more than one tile will
1173 // generate a low res tiling.
1174 contents_scale = 2.5f;
1175 SetContentsScaleOnBothLayers(contents_scale,
1178 maximum_animation_scale,
1179 animating_transform);
1180 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1181 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1182 contents_scale * low_res_factor);
1183 EXPECT_BOTH_EQ(num_tilings(), 2u);
1185 ResetTilingsAndRasterScales();
1187 // Mask layers dont create low res since they always fit on one tile.
1188 pending_layer_->pile()->set_is_mask(true);
1189 active_layer_->pile()->set_is_mask(true);
1190 SetContentsScaleOnBothLayers(contents_scale,
1193 maximum_animation_scale,
1194 animating_transform);
1195 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1196 EXPECT_BOTH_EQ(num_tilings(), 1u);
1199 TEST_F(PictureLayerImplTest, HugeMasksDontGetTiles) {
1200 gfx::Size tile_size(100, 100);
1202 scoped_refptr<FakePicturePileImpl> valid_pile =
1203 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1204 valid_pile->set_is_mask(true);
1205 SetupPendingTree(valid_pile);
1207 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1208 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1209 EXPECT_EQ(1u, pending_layer_->num_tilings());
1211 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1212 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1213 pending_layer_->HighResTiling()->AllTilesForTesting());
1217 // Mask layers have a tiling with a single tile in it.
1218 EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1219 // The mask resource exists.
1220 EXPECT_NE(0u, active_layer_->ContentsResourceId());
1222 // Resize larger than the max texture size.
1223 int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1224 scoped_refptr<FakePicturePileImpl> huge_pile =
1225 FakePicturePileImpl::CreateFilledPile(
1226 tile_size, gfx::Size(max_texture_size + 1, 10));
1227 huge_pile->set_is_mask(true);
1228 SetupPendingTree(huge_pile);
1230 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1231 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1232 EXPECT_EQ(1u, pending_layer_->num_tilings());
1234 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1235 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1236 pending_layer_->HighResTiling()->AllTilesForTesting());
1240 // Mask layers have a tiling, but there should be no tiles in it.
1241 EXPECT_EQ(0u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1242 // The mask resource is empty.
1243 EXPECT_EQ(0u, active_layer_->ContentsResourceId());
1246 TEST_F(PictureLayerImplTest, ReleaseResources) {
1247 gfx::Size tile_size(400, 400);
1248 gfx::Size layer_bounds(1300, 1900);
1250 scoped_refptr<FakePicturePileImpl> pending_pile =
1251 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1252 scoped_refptr<FakePicturePileImpl> active_pile =
1253 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1255 SetupTrees(pending_pile, active_pile);
1256 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1258 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1259 1.3f, // ideal contents scale
1260 2.7f, // device scale
1262 1.f, // maximum animation scale
1264 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1266 // All tilings should be removed when losing output surface.
1267 active_layer_->ReleaseResources();
1268 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1269 pending_layer_->ReleaseResources();
1270 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1272 // This should create new tilings.
1273 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1274 1.3f, // ideal contents scale
1275 2.7f, // device scale
1277 1.f, // maximum animation scale
1279 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1282 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1283 // The default max tile size is larger than 400x400.
1284 gfx::Size tile_size(400, 400);
1285 gfx::Size layer_bounds(5000, 5000);
1287 scoped_refptr<FakePicturePileImpl> pending_pile =
1288 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1289 scoped_refptr<FakePicturePileImpl> active_pile =
1290 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1292 SetupTrees(pending_pile, active_pile);
1293 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1295 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1296 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1298 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1300 // The default value.
1301 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1302 host_impl_.settings().default_tile_size.ToString());
1304 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1305 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1306 tile->content_rect().size().ToString());
1308 pending_layer_->ReleaseResources();
1310 // Change the max texture size on the output surface context.
1311 scoped_ptr<TestWebGraphicsContext3D> context =
1312 TestWebGraphicsContext3D::Create();
1313 context->set_max_texture_size(140);
1314 host_impl_.DidLoseOutputSurface();
1315 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1316 context.Pass()).PassAs<OutputSurface>());
1318 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1319 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1321 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1323 // Verify the tiles are not larger than the context's max texture size.
1324 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1325 EXPECT_GE(140, tile->content_rect().width());
1326 EXPECT_GE(140, tile->content_rect().height());
1329 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1330 // The default max tile size is larger than 400x400.
1331 gfx::Size tile_size(400, 400);
1332 gfx::Size layer_bounds(500, 500);
1334 scoped_refptr<FakePicturePileImpl> pending_pile =
1335 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1336 scoped_refptr<FakePicturePileImpl> active_pile =
1337 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1339 SetupTrees(pending_pile, active_pile);
1340 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1342 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1343 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1345 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1347 // The default value. The layer is smaller than this.
1348 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1349 host_impl_.settings().max_untiled_layer_size.ToString());
1351 // There should be a single tile since the layer is small.
1352 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1353 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1355 pending_layer_->ReleaseResources();
1357 // Change the max texture size on the output surface context.
1358 scoped_ptr<TestWebGraphicsContext3D> context =
1359 TestWebGraphicsContext3D::Create();
1360 context->set_max_texture_size(140);
1361 host_impl_.DidLoseOutputSurface();
1362 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1363 context.Pass()).PassAs<OutputSurface>());
1365 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1366 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1368 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1370 // There should be more than one tile since the max texture size won't cover
1372 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1373 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1375 // Verify the tiles are not larger than the context's max texture size.
1376 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1377 EXPECT_GE(140, tile->content_rect().width());
1378 EXPECT_GE(140, tile->content_rect().height());
1381 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1382 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1383 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1385 gfx::Size tile_size(400, 400);
1386 gfx::Size layer_bounds(1300, 1900);
1388 scoped_refptr<FakePicturePileImpl> pending_pile =
1389 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1390 scoped_refptr<FakePicturePileImpl> active_pile =
1391 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1393 SetupTrees(pending_pile, active_pile);
1395 active_layer_->draw_properties().visible_content_rect =
1396 gfx::Rect(layer_bounds);
1398 gfx::Rect layer_invalidation(150, 200, 30, 180);
1399 Region invalidation(layer_invalidation);
1400 AddDefaultTilingsWithInvalidation(invalidation);
1402 AppendQuadsData data;
1403 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1404 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1405 active_layer_->DidDraw(NULL);
1407 ASSERT_EQ(1U, render_pass->quad_list.size());
1408 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1409 render_pass->quad_list.front()->material);
1412 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1413 gfx::Size tile_size(100, 100);
1414 gfx::Size layer_bounds(1000, 1000);
1416 scoped_refptr<FakePicturePileImpl> pending_pile =
1417 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1418 // Layers with entirely empty piles can't get tilings.
1419 pending_pile->AddRecordingAt(0, 0);
1421 SetupPendingTree(pending_pile);
1423 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1424 pending_layer_->AddTiling(1.0f);
1425 pending_layer_->AddTiling(2.0f);
1427 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1428 // on a layer with no recordings.
1429 host_impl_.pending_tree()->UpdateDrawProperties();
1430 pending_layer_->MarkVisibleResourcesAsRequired();
1433 TEST_F(PictureLayerImplTest, TileScalesWithSolidColorPile) {
1434 gfx::Size layer_bounds(200, 200);
1435 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1436 scoped_refptr<FakePicturePileImpl> pending_pile =
1437 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1438 tile_size, layer_bounds);
1439 scoped_refptr<FakePicturePileImpl> active_pile =
1440 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1441 tile_size, layer_bounds);
1443 pending_pile->set_is_solid_color(false);
1444 active_pile->set_is_solid_color(true);
1445 SetupTrees(pending_pile, active_pile);
1446 // Solid color layer should not have tilings.
1447 ASSERT_FALSE(active_layer_->CanHaveTilings());
1449 // Update properties with solid color pile should not allow tilings at any
1451 host_impl_.active_tree()->UpdateDrawProperties();
1452 EXPECT_FALSE(active_layer_->CanHaveTilings());
1453 EXPECT_EQ(0.f, active_layer_->ideal_contents_scale());
1455 // Push non-solid-color pending pile makes active layer can have tilings.
1456 active_layer_->UpdatePile(pending_pile);
1457 ASSERT_TRUE(active_layer_->CanHaveTilings());
1459 // Update properties with non-solid color pile should allow tilings.
1460 host_impl_.active_tree()->UpdateDrawProperties();
1461 EXPECT_TRUE(active_layer_->CanHaveTilings());
1462 EXPECT_GT(active_layer_->ideal_contents_scale(), 0.f);
1465 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1466 gfx::Size tile_size(100, 100);
1467 gfx::Size layer_bounds(200, 200);
1469 scoped_refptr<FakePicturePileImpl> pending_pile =
1470 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1471 SetupPendingTree(pending_pile);
1473 gfx::Transform transform;
1474 gfx::Transform transform_for_tile_priority;
1475 bool resourceless_software_draw = false;
1476 gfx::Rect viewport(0, 0, 100, 200);
1477 host_impl_.SetExternalDrawConstraints(transform,
1482 resourceless_software_draw);
1484 pending_layer_->set_fixed_tile_size(tile_size);
1485 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1486 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1487 host_impl_.pending_tree()->UpdateDrawProperties();
1488 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1489 EXPECT_EQ(viewport, pending_layer_->visible_rect_for_tile_priority());
1491 // Fake set priorities.
1492 for (PictureLayerTiling::CoverageIterator iter(
1493 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1499 TilePriority priority;
1500 priority.resolution = HIGH_RESOLUTION;
1501 gfx::Rect tile_bounds = iter.geometry_rect();
1502 if (pending_layer_->visible_rect_for_tile_priority().Intersects(
1504 priority.priority_bin = TilePriority::NOW;
1505 priority.distance_to_visible = 0.f;
1507 priority.priority_bin = TilePriority::SOON;
1508 priority.distance_to_visible = 1.f;
1510 tile->SetPriority(PENDING_TREE, priority);
1513 pending_layer_->MarkVisibleResourcesAsRequired();
1515 int num_visible = 0;
1516 int num_offscreen = 0;
1518 for (PictureLayerTiling::CoverageIterator iter(
1519 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1524 const Tile* tile = *iter;
1525 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1526 EXPECT_TRUE(tile->required_for_activation());
1529 EXPECT_FALSE(tile->required_for_activation());
1534 EXPECT_GT(num_visible, 0);
1535 EXPECT_GT(num_offscreen, 0);
1538 TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1539 base::TimeTicks time_ticks;
1540 time_ticks += base::TimeDelta::FromMilliseconds(1);
1541 host_impl_.SetCurrentBeginFrameArgs(
1542 CreateBeginFrameArgsForTesting(time_ticks));
1544 gfx::Size tile_size(100, 100);
1545 gfx::Size layer_bounds(400, 400);
1546 gfx::Rect external_viewport_for_tile_priority(400, 200);
1547 gfx::Rect visible_content_rect(200, 400);
1549 scoped_refptr<FakePicturePileImpl> active_pile =
1550 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1551 scoped_refptr<FakePicturePileImpl> pending_pile =
1552 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1553 SetupTrees(pending_pile, active_pile);
1555 active_layer_->set_fixed_tile_size(tile_size);
1556 pending_layer_->set_fixed_tile_size(tile_size);
1557 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1558 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1560 // Set external viewport for tile priority.
1561 gfx::Rect viewport = gfx::Rect(layer_bounds);
1562 gfx::Transform transform;
1563 gfx::Transform transform_for_tile_priority;
1564 bool resourceless_software_draw = false;
1565 host_impl_.SetExternalDrawConstraints(transform,
1568 external_viewport_for_tile_priority,
1569 transform_for_tile_priority,
1570 resourceless_software_draw);
1571 host_impl_.pending_tree()->UpdateDrawProperties();
1573 // Set visible content rect that is different from
1574 // external_viewport_for_tile_priority.
1575 pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1576 time_ticks += base::TimeDelta::FromMilliseconds(200);
1577 host_impl_.SetCurrentBeginFrameArgs(
1578 CreateBeginFrameArgsForTesting(time_ticks));
1579 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
1581 pending_layer_->MarkVisibleResourcesAsRequired();
1583 // Intersect the two rects. Any tile outside should not be required for
1585 gfx::Rect viewport_for_tile_priority =
1586 pending_layer_->GetViewportForTilePriorityInContentSpace();
1587 viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1590 int num_outside = 0;
1591 for (PictureLayerTiling::CoverageIterator iter(
1592 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1598 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1600 // Mark everything in viewport for tile priority as ready to draw.
1601 ManagedTileState::TileVersion& tile_version =
1602 tile->GetTileVersionForTesting(
1603 tile->DetermineRasterModeForTree(PENDING_TREE));
1604 tile_version.SetSolidColorForTesting(SK_ColorRED);
1607 EXPECT_FALSE(tile->required_for_activation());
1611 EXPECT_GT(num_inside, 0);
1612 EXPECT_GT(num_outside, 0);
1614 // Activate and draw active layer.
1615 host_impl_.ActivateSyncTree();
1616 host_impl_.active_tree()->UpdateDrawProperties();
1617 active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1619 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1620 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1621 AppendQuadsData data;
1622 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1623 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1624 active_layer_->DidDraw(NULL);
1626 // All tiles in activation rect is ready to draw.
1627 EXPECT_EQ(0u, data.num_missing_tiles);
1628 EXPECT_EQ(0u, data.num_incomplete_tiles);
1631 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1632 base::TimeTicks time_ticks;
1633 time_ticks += base::TimeDelta::FromMilliseconds(1);
1634 host_impl_.SetCurrentBeginFrameArgs(
1635 CreateBeginFrameArgsForTesting(time_ticks));
1637 gfx::Size tile_size(100, 100);
1638 gfx::Size layer_bounds(200, 200);
1640 host_impl_.SetViewportSize(layer_bounds);
1642 scoped_refptr<FakePicturePileImpl> pending_pile =
1643 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1644 SetupPendingTree(pending_pile);
1647 // All high res tiles have resources.
1648 active_layer_->set_fixed_tile_size(tile_size);
1649 host_impl_.active_tree()->UpdateDrawProperties();
1650 std::vector<Tile*> tiles =
1651 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1652 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1654 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1655 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1656 AppendQuadsData data;
1657 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1658 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1659 active_layer_->DidDraw(NULL);
1661 // All high res tiles drew, nothing was incomplete.
1662 EXPECT_EQ(9u, render_pass->quad_list.size());
1663 EXPECT_EQ(0u, data.num_missing_tiles);
1664 EXPECT_EQ(0u, data.num_incomplete_tiles);
1667 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1668 base::TimeTicks time_ticks;
1669 time_ticks += base::TimeDelta::FromMilliseconds(1);
1670 host_impl_.SetCurrentBeginFrameArgs(
1671 CreateBeginFrameArgsForTesting(time_ticks));
1673 gfx::Size tile_size(100, 100);
1674 gfx::Size layer_bounds(200, 200);
1676 host_impl_.SetViewportSize(layer_bounds);
1678 scoped_refptr<FakePicturePileImpl> pending_pile =
1679 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1680 SetupPendingTree(pending_pile);
1683 // All high res tiles have resources except one.
1684 active_layer_->set_fixed_tile_size(tile_size);
1685 host_impl_.active_tree()->UpdateDrawProperties();
1686 std::vector<Tile*> high_tiles =
1687 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1688 high_tiles.erase(high_tiles.begin());
1689 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1691 // All low res tiles have resources.
1692 std::vector<Tile*> low_tiles =
1693 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1694 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1696 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1697 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1698 AppendQuadsData data;
1699 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1700 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1701 active_layer_->DidDraw(NULL);
1703 // The missing high res tile was replaced by a low res tile.
1704 EXPECT_EQ(9u, render_pass->quad_list.size());
1705 EXPECT_EQ(0u, data.num_missing_tiles);
1706 EXPECT_EQ(1u, data.num_incomplete_tiles);
1709 TEST_F(PictureLayerImplTest,
1710 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1711 base::TimeTicks time_ticks;
1712 time_ticks += base::TimeDelta::FromMilliseconds(1);
1713 host_impl_.SetCurrentBeginFrameArgs(
1714 CreateBeginFrameArgsForTesting(time_ticks));
1716 gfx::Size tile_size(100, 100);
1717 gfx::Size layer_bounds(200, 200);
1719 host_impl_.SetViewportSize(layer_bounds);
1721 scoped_refptr<FakePicturePileImpl> pending_pile =
1722 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1723 scoped_refptr<FakePicturePileImpl> active_pile =
1724 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1725 SetupTrees(pending_pile, active_pile);
1727 active_layer_->set_fixed_tile_size(tile_size);
1729 active_layer_->draw_properties().visible_content_rect =
1730 gfx::Rect(layer_bounds);
1731 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1733 // One ideal tile exists, this will get used when drawing.
1734 std::vector<Tile*> ideal_tiles;
1735 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1736 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1737 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1740 // Due to layer scale throttling, the raster contents scale is changed to 1,
1741 // while the ideal is still 2.
1742 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1743 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1745 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1746 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1747 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1749 // Both tilings still exist.
1750 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1751 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1753 // All high res tiles have resources.
1754 std::vector<Tile*> high_tiles =
1755 active_layer_->HighResTiling()->AllTilesForTesting();
1756 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1758 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1759 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1760 AppendQuadsData data;
1761 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1762 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1763 active_layer_->DidDraw(NULL);
1765 // All high res tiles drew, and the one ideal res tile drew.
1766 ASSERT_GT(render_pass->quad_list.size(), 9u);
1767 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1768 TileDrawQuad::MaterialCast(render_pass->quad_list.front())
1769 ->tex_coord_rect.size());
1770 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1771 TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
1772 ->tex_coord_rect.size());
1774 // Neither the high res nor the ideal tiles were considered as incomplete.
1775 EXPECT_EQ(0u, data.num_missing_tiles);
1776 EXPECT_EQ(0u, data.num_incomplete_tiles);
1779 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1780 gfx::Size layer_bounds(400, 400);
1781 gfx::Size tile_size(100, 100);
1782 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1785 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1787 CreateHighLowResAndSetAllTilesVisible();
1789 active_layer_->SetAllTilesReady();
1791 // No shared tiles and all active tiles ready, so pending can only
1792 // activate with all high res tiles.
1793 pending_layer_->MarkVisibleResourcesAsRequired();
1794 AssertAllTilesRequired(pending_layer_->HighResTiling());
1795 AssertNoTilesRequired(pending_layer_->LowResTiling());
1798 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1799 gfx::Size layer_bounds(400, 400);
1800 gfx::Size tile_size(100, 100);
1801 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1803 // All tiles shared (no invalidation).
1804 CreateHighLowResAndSetAllTilesVisible();
1806 // Verify active tree not ready.
1807 Tile* some_active_tile =
1808 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1809 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1811 // When high res are required, even if the active tree is not ready,
1812 // the high res tiles must be ready.
1813 host_impl_.active_tree()->SetRequiresHighResToDraw();
1814 pending_layer_->MarkVisibleResourcesAsRequired();
1815 AssertAllTilesRequired(pending_layer_->HighResTiling());
1816 AssertNoTilesRequired(pending_layer_->LowResTiling());
1819 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1820 gfx::Size layer_bounds(400, 400);
1821 gfx::Size tile_size(100, 100);
1822 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1824 CreateHighLowResAndSetAllTilesVisible();
1826 Tile* some_active_tile =
1827 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1828 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1830 // All tiles shared (no invalidation), so even though the active tree's
1831 // tiles aren't ready, there is nothing required.
1832 pending_layer_->MarkVisibleResourcesAsRequired();
1833 AssertNoTilesRequired(pending_layer_->HighResTiling());
1834 AssertNoTilesRequired(pending_layer_->LowResTiling());
1837 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1838 gfx::Size layer_bounds(400, 400);
1839 gfx::Size tile_size(100, 100);
1840 scoped_refptr<FakePicturePileImpl> pending_pile =
1841 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1842 // This pile will create tilings, but has no recordings so will not create any
1843 // tiles. This is attempting to simulate scrolling past the end of recorded
1844 // content on the active layer, where the recordings are so far away that
1845 // no tiles are created.
1846 scoped_refptr<FakePicturePileImpl> active_pile =
1847 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1848 tile_size, layer_bounds);
1849 SetupTrees(pending_pile, active_pile);
1850 pending_layer_->set_fixed_tile_size(tile_size);
1851 active_layer_->set_fixed_tile_size(tile_size);
1853 CreateHighLowResAndSetAllTilesVisible();
1855 // Active layer has tilings, but no tiles due to missing recordings.
1856 EXPECT_TRUE(active_layer_->CanHaveTilings());
1857 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1858 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1860 // Since the active layer has no tiles at all, the pending layer doesn't
1861 // need content in order to activate.
1862 pending_layer_->MarkVisibleResourcesAsRequired();
1863 AssertNoTilesRequired(pending_layer_->HighResTiling());
1864 AssertNoTilesRequired(pending_layer_->LowResTiling());
1867 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1868 gfx::Size layer_bounds(400, 400);
1869 gfx::Size tile_size(100, 100);
1870 scoped_refptr<FakePicturePileImpl> pending_pile =
1871 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1872 scoped_refptr<FakePicturePileImpl> active_pile =
1873 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1874 SetupTrees(pending_pile, active_pile);
1875 pending_layer_->set_fixed_tile_size(tile_size);
1876 active_layer_->set_fixed_tile_size(tile_size);
1878 CreateHighLowResAndSetAllTilesVisible();
1880 // Active layer can't have tiles.
1881 EXPECT_FALSE(active_layer_->CanHaveTilings());
1883 // All high res tiles required. This should be considered identical
1884 // to the case where there is no active layer, to avoid flashing content.
1885 // This can happen if a layer exists for a while and switches from
1886 // not being able to have content to having content.
1887 pending_layer_->MarkVisibleResourcesAsRequired();
1888 AssertAllTilesRequired(pending_layer_->HighResTiling());
1889 AssertNoTilesRequired(pending_layer_->LowResTiling());
1892 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1893 gfx::Size layer_bounds(200, 200);
1894 gfx::Size tile_size(100, 100);
1895 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1897 gfx::Size pending_layer_bounds(400, 400);
1898 pending_layer_->SetBounds(pending_layer_bounds);
1900 CreateHighLowResAndSetAllTilesVisible();
1902 active_layer_->SetAllTilesReady();
1904 // Since the active layer has different bounds, the pending layer needs all
1905 // high res tiles in order to activate.
1906 pending_layer_->MarkVisibleResourcesAsRequired();
1907 AssertAllTilesRequired(pending_layer_->HighResTiling());
1908 AssertNoTilesRequired(pending_layer_->LowResTiling());
1911 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1912 gfx::Size tile_size(100, 100);
1913 gfx::Size layer_bounds(400, 400);
1914 scoped_refptr<FakePicturePileImpl> pending_pile =
1915 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1917 host_impl_.CreatePendingTree();
1918 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1920 scoped_ptr<FakePictureLayerImpl> pending_layer =
1921 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1922 pending_layer->SetDrawsContent(true);
1923 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1925 pending_layer_ = static_cast<FakePictureLayerImpl*>(
1926 host_impl_.pending_tree()->LayerById(id_));
1928 // Set some state on the pending layer, make sure it is not clobbered
1929 // by a sync from the active layer. This could happen because if the
1930 // pending layer has not been post-commit initialized it will attempt
1931 // to sync from the active layer.
1932 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1933 pending_layer_->set_raster_page_scale(raster_page_scale);
1934 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1936 host_impl_.ActivateSyncTree();
1938 active_layer_ = static_cast<FakePictureLayerImpl*>(
1939 host_impl_.active_tree()->LayerById(id_));
1941 EXPECT_EQ(0u, active_layer_->num_tilings());
1942 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1943 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1946 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1947 SetupDefaultTrees(gfx::Size(1500, 1500));
1948 AddDefaultTilingsWithInvalidation(gfx::Rect());
1950 host_impl_.ActivateSyncTree();
1951 host_impl_.CreatePendingTree();
1952 active_layer_ = static_cast<FakePictureLayerImpl*>(
1953 host_impl_.active_tree()->LayerById(id_));
1955 // Force the active tree to sync to the pending tree "post-commit".
1956 pending_layer_->DoPostCommitInitializationIfNeeded();
1958 // Both invalidations should drop tiles from the pending tree.
1959 EXPECT_EQ(3u, active_layer_->num_tilings());
1960 EXPECT_EQ(3u, pending_layer_->num_tilings());
1961 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1962 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1963 PictureLayerTiling* pending_tiling =
1964 pending_layer_->tilings()->tiling_at(i);
1966 ASSERT_TRUE(active_tiling);
1967 ASSERT_TRUE(pending_tiling);
1969 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1970 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1971 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1972 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1974 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1975 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1976 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1977 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1979 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1980 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1981 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1982 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1983 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1984 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1985 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1986 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1990 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1991 SetupDefaultTrees(gfx::Size(1500, 1500));
1992 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1994 // This activates the 0,0,1,1 invalidation.
1995 host_impl_.ActivateSyncTree();
1996 host_impl_.CreatePendingTree();
1997 active_layer_ = static_cast<FakePictureLayerImpl*>(
1998 host_impl_.active_tree()->LayerById(id_));
2000 // Force the active tree to sync to the pending tree "post-commit".
2001 pending_layer_->DoPostCommitInitializationIfNeeded();
2003 // The active tree invalidation was handled by the active tiles, so they
2004 // can be shared with the pending tree.
2005 EXPECT_EQ(3u, active_layer_->num_tilings());
2006 EXPECT_EQ(3u, pending_layer_->num_tilings());
2007 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2008 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2009 PictureLayerTiling* pending_tiling =
2010 pending_layer_->tilings()->tiling_at(i);
2012 ASSERT_TRUE(active_tiling);
2013 ASSERT_TRUE(pending_tiling);
2015 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2016 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2017 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2018 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2020 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2021 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2022 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2023 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2025 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2026 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
2027 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2028 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2029 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2030 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
2031 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2032 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2036 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
2037 SetupDefaultTrees(gfx::Size(1500, 1500));
2038 AddDefaultTilingsWithInvalidation(gfx::Rect());
2040 host_impl_.ActivateSyncTree();
2041 host_impl_.CreatePendingTree();
2042 active_layer_ = static_cast<FakePictureLayerImpl*>(
2043 host_impl_.active_tree()->LayerById(id_));
2045 // Set some invalidation on the pending tree "during commit". We should
2046 // replace raster tiles that touch this.
2047 pending_layer_->set_invalidation(gfx::Rect(1, 1));
2049 // Force the active tree to sync to the pending tree "post-commit".
2050 pending_layer_->DoPostCommitInitializationIfNeeded();
2052 // The pending tree invalidation means tiles can not be shared with the
2054 EXPECT_EQ(3u, active_layer_->num_tilings());
2055 EXPECT_EQ(3u, pending_layer_->num_tilings());
2056 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
2057 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
2058 PictureLayerTiling* pending_tiling =
2059 pending_layer_->tilings()->tiling_at(i);
2061 ASSERT_TRUE(active_tiling);
2062 ASSERT_TRUE(pending_tiling);
2064 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2065 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2066 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2067 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2069 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2070 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2071 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2072 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2074 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2075 EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2076 EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2077 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2078 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2079 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2080 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2081 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2082 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2086 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
2087 SetupDefaultTrees(gfx::Size(10, 10));
2088 host_impl_.active_tree()->UpdateDrawProperties();
2089 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2091 // Contrived unit test of a real crash. A layer is transparent during a
2092 // context loss, and later becomes opaque, causing active layer SyncTiling to
2094 float new_scale = 1.f;
2095 active_layer_->ReleaseResources();
2096 pending_layer_->ReleaseResources();
2097 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
2098 pending_layer_->AddTiling(new_scale);
2099 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
2101 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
2102 // responsible for calling ManageTilings. These checks verify that
2103 // ReleaseResources has set needs update draw properties so that the
2104 // new tiling gets the appropriate resolution set in ManageTilings.
2105 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2106 host_impl_.active_tree()->UpdateDrawProperties();
2107 PictureLayerTiling* high_res =
2108 active_layer_->tilings()->TilingAtScale(new_scale);
2109 ASSERT_TRUE(!!high_res);
2110 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
2113 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2114 SetupDefaultTrees(gfx::Size(10, 10));
2116 const float kScale = 1.f;
2117 pending_layer_->AddTiling(kScale);
2118 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2119 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2121 // Gpu rasterization is disabled by default.
2122 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2123 // Toggling the gpu rasterization clears all tilings on both trees.
2124 host_impl_.SetUseGpuRasterization(true);
2125 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2126 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2128 // Make sure that we can still add tiling to the pending layer,
2129 // that gets synced to the active layer.
2130 pending_layer_->AddTiling(kScale);
2131 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2132 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2134 // Toggling the gpu rasterization clears all tilings on both trees.
2135 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2136 host_impl_.SetUseGpuRasterization(false);
2137 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2138 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2141 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2142 SetupDefaultTrees(gfx::Size(10, 10));
2143 host_impl_.active_tree()->UpdateDrawProperties();
2144 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2146 SetupDrawPropertiesAndUpdateTiles(
2147 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2148 active_layer_->tilings()->RemoveAllTilings();
2149 PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
2150 active_layer_->tilings()->AddTiling(1.5f);
2151 active_layer_->tilings()->AddTiling(0.25f);
2152 tiling->set_resolution(HIGH_RESOLUTION);
2155 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2156 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2158 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2159 // 1.0f). Note that we should also ensure that the pending layer needs post
2160 // commit initialization, since this is what would happen during commit. In
2161 // other words we want the pending layer to sync from the active layer.
2162 pending_layer_->SetBounds(gfx::Size(1, 1));
2163 pending_layer_->SetNeedsPostCommitInitialization();
2164 pending_layer_->set_twin_layer(NULL);
2165 active_layer_->set_twin_layer(NULL);
2166 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2168 // Update the draw properties: sync from active tree should happen here.
2169 host_impl_.pending_tree()->UpdateDrawProperties();
2171 // Another sanity check.
2172 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2174 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2175 // violate minimum contents scale. At the same time, we should've created a
2176 // new high res tiling at scale 1.0f.
2177 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2178 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2179 EXPECT_EQ(HIGH_RESOLUTION,
2180 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2181 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2182 EXPECT_EQ(NON_IDEAL_RESOLUTION,
2183 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2186 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2187 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2188 gfx::Size layer_bounds(default_tile_size.width() * 4,
2189 default_tile_size.height() * 4);
2191 SetupDefaultTrees(layer_bounds);
2192 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2193 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2194 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2195 // Should have a low-res and a high-res tiling.
2196 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2198 ResetTilingsAndRasterScales();
2200 host_impl_.SetUseGpuRasterization(true);
2201 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2202 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2204 // Should only have the high-res tiling.
2205 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2208 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2209 // Set up layers with tilings.
2210 SetupDefaultTrees(gfx::Size(10, 10));
2211 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2212 pending_layer_->PushPropertiesTo(active_layer_);
2213 EXPECT_TRUE(pending_layer_->DrawsContent());
2214 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2215 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2216 EXPECT_GE(active_layer_->num_tilings(), 0u);
2218 // Set content to false, which should make CanHaveTilings return false.
2219 pending_layer_->SetDrawsContent(false);
2220 EXPECT_FALSE(pending_layer_->DrawsContent());
2221 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2223 // No tilings should be pushed to active layer.
2224 pending_layer_->PushPropertiesTo(active_layer_);
2225 EXPECT_EQ(0u, active_layer_->num_tilings());
2228 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2229 SetupDefaultTrees(gfx::Size(10, 10));
2230 host_impl_.PinchGestureBegin();
2231 float high_res_scale = 2.3f;
2232 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2234 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2235 EXPECT_FLOAT_EQ(high_res_scale,
2236 pending_layer_->HighResTiling()->contents_scale());
2239 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2240 SetupDefaultTrees(gfx::Size(10, 10));
2241 host_impl_.PinchGestureBegin();
2242 float high_res_scale = 0.0001f;
2243 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2245 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2247 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2248 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2249 pending_layer_->HighResTiling()->contents_scale());
2252 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2253 SetupDefaultTrees(gfx::Size(10, 10));
2255 float contents_scale = 0.15f;
2256 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2258 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2259 EXPECT_FLOAT_EQ(contents_scale,
2260 pending_layer_->HighResTiling()->contents_scale());
2262 host_impl_.PinchGestureBegin();
2264 float page_scale = 0.0001f;
2265 EXPECT_LT(page_scale * contents_scale,
2266 pending_layer_->MinimumContentsScale());
2268 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2269 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2270 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2271 pending_layer_->HighResTiling()->contents_scale());
2274 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2276 virtual void InitializeRenderer() OVERRIDE {
2277 bool delegated_rendering = false;
2278 host_impl_.InitializeRenderer(
2279 FakeOutputSurface::CreateDeferredGL(
2280 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2281 delegated_rendering).PassAs<OutputSurface>());
2284 virtual void SetUp() OVERRIDE {
2285 PictureLayerImplTest::SetUp();
2287 // Create some default active and pending trees.
2288 gfx::Size tile_size(100, 100);
2289 gfx::Size layer_bounds(400, 400);
2291 scoped_refptr<FakePicturePileImpl> pending_pile =
2292 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2293 scoped_refptr<FakePicturePileImpl> active_pile =
2294 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2296 SetupTrees(pending_pile, active_pile);
2300 // This test is really a LayerTreeHostImpl test, in that it makes sure
2301 // that trees need update draw properties after deferred initialization.
2302 // However, this is also a regression test for PictureLayerImpl in that
2303 // not having this update will cause a crash.
2304 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2305 host_impl_.pending_tree()->UpdateDrawProperties();
2306 host_impl_.active_tree()->UpdateDrawProperties();
2307 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2308 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2310 FakeOutputSurface* fake_output_surface =
2311 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2312 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2313 TestContextProvider::Create()));
2315 // These will crash PictureLayerImpl if this is not true.
2316 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2317 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2318 host_impl_.active_tree()->UpdateDrawProperties();
2321 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2322 gfx::Size layer_bounds(100, 100);
2323 gfx::Size viewport_size(1000, 1000);
2324 SetupDefaultTrees(layer_bounds);
2325 host_impl_.SetViewportSize(viewport_size);
2327 float contents_scale = 1.f;
2328 float device_scale = 1.3f;
2329 float page_scale = 1.4f;
2330 float maximum_animation_scale = 1.f;
2331 bool animating_transform = false;
2333 SetContentsScaleOnBothLayers(contents_scale,
2336 maximum_animation_scale,
2337 animating_transform);
2338 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2340 // Since we're CPU-rasterizing, starting an animation should cause tiling
2341 // resolution to get set to the maximum animation scale factor.
2342 animating_transform = true;
2343 maximum_animation_scale = 3.f;
2344 contents_scale = 2.f;
2346 SetContentsScaleOnBothLayers(contents_scale,
2349 maximum_animation_scale,
2350 animating_transform);
2351 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2353 // Further changes to scale during the animation should not cause a new
2354 // high-res tiling to get created.
2355 contents_scale = 4.f;
2356 maximum_animation_scale = 5.f;
2358 SetContentsScaleOnBothLayers(contents_scale,
2361 maximum_animation_scale,
2362 animating_transform);
2363 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2365 // Once we stop animating, a new high-res tiling should be created.
2366 animating_transform = false;
2368 SetContentsScaleOnBothLayers(contents_scale,
2371 maximum_animation_scale,
2372 animating_transform);
2373 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2375 // When animating with an unknown maximum animation scale factor, a new
2376 // high-res tiling should be created at a source scale of 1.
2377 animating_transform = true;
2378 contents_scale = 2.f;
2379 maximum_animation_scale = 0.f;
2381 SetContentsScaleOnBothLayers(contents_scale,
2384 maximum_animation_scale,
2385 animating_transform);
2386 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2388 // Further changes to scale during the animation should not cause a new
2389 // high-res tiling to get created.
2390 contents_scale = 3.f;
2392 SetContentsScaleOnBothLayers(contents_scale,
2395 maximum_animation_scale,
2396 animating_transform);
2397 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2399 // Once we stop animating, a new high-res tiling should be created.
2400 animating_transform = false;
2401 contents_scale = 4.f;
2403 SetContentsScaleOnBothLayers(contents_scale,
2406 maximum_animation_scale,
2407 animating_transform);
2408 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2410 // When animating with a maxmium animation scale factor that is so large
2411 // that the layer grows larger than the viewport at this scale, a new
2412 // high-res tiling should get created at a source scale of 1, not at its
2414 animating_transform = true;
2415 contents_scale = 2.f;
2416 maximum_animation_scale = 11.f;
2418 SetContentsScaleOnBothLayers(contents_scale,
2421 maximum_animation_scale,
2422 animating_transform);
2423 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), page_scale * device_scale);
2425 // Once we stop animating, a new high-res tiling should be created.
2426 animating_transform = false;
2427 contents_scale = 11.f;
2429 SetContentsScaleOnBothLayers(contents_scale,
2432 maximum_animation_scale,
2433 animating_transform);
2434 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2436 // When animating with a maxmium animation scale factor that is so large
2437 // that the layer grows larger than the viewport at this scale, and where
2438 // the intial source scale is < 1, a new high-res tiling should get created
2439 // at source scale 1.
2440 animating_transform = true;
2441 contents_scale = 0.1f;
2442 maximum_animation_scale = 11.f;
2444 SetContentsScaleOnBothLayers(contents_scale,
2447 maximum_animation_scale,
2448 animating_transform);
2449 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2451 // Once we stop animating, a new high-res tiling should be created.
2452 animating_transform = false;
2453 contents_scale = 12.f;
2455 SetContentsScaleOnBothLayers(contents_scale,
2458 maximum_animation_scale,
2459 animating_transform);
2460 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 12.f);
2462 // When animating toward a smaller scale, but that is still so large that the
2463 // layer grows larger than the viewport at this scale, a new high-res tiling
2464 // should get created at source scale 1.
2465 animating_transform = true;
2466 contents_scale = 11.f;
2467 maximum_animation_scale = 11.f;
2469 SetContentsScaleOnBothLayers(contents_scale,
2472 maximum_animation_scale,
2473 animating_transform);
2474 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2476 // Once we stop animating, a new high-res tiling should be created.
2477 animating_transform = false;
2478 contents_scale = 11.f;
2480 SetContentsScaleOnBothLayers(contents_scale,
2483 maximum_animation_scale,
2484 animating_transform);
2485 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2488 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2489 gfx::Size layer_bounds(100, 100);
2490 gfx::Size viewport_size(1000, 1000);
2491 SetupDefaultTrees(layer_bounds);
2492 host_impl_.SetViewportSize(viewport_size);
2493 host_impl_.SetUseGpuRasterization(true);
2495 float contents_scale = 1.f;
2496 float device_scale = 1.3f;
2497 float page_scale = 1.4f;
2498 float maximum_animation_scale = 1.f;
2499 bool animating_transform = false;
2501 SetContentsScaleOnBothLayers(contents_scale,
2504 maximum_animation_scale,
2505 animating_transform);
2506 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2508 // Since we're GPU-rasterizing, starting an animation should cause tiling
2509 // resolution to get set to the current contents scale.
2510 animating_transform = true;
2511 contents_scale = 2.f;
2512 maximum_animation_scale = 4.f;
2514 SetContentsScaleOnBothLayers(contents_scale,
2517 maximum_animation_scale,
2518 animating_transform);
2519 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2521 // Further changes to scale during the animation should cause a new high-res
2522 // tiling to get created.
2523 contents_scale = 3.f;
2525 SetContentsScaleOnBothLayers(contents_scale,
2528 maximum_animation_scale,
2529 animating_transform);
2530 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2532 // Since we're re-rasterizing during the animation, scales smaller than 1
2533 // should be respected.
2534 contents_scale = 0.25f;
2536 SetContentsScaleOnBothLayers(contents_scale,
2539 maximum_animation_scale,
2540 animating_transform);
2541 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2543 // Once we stop animating, a new high-res tiling should be created.
2544 contents_scale = 4.f;
2545 animating_transform = false;
2547 SetContentsScaleOnBothLayers(contents_scale,
2550 maximum_animation_scale,
2551 animating_transform);
2552 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2554 static_cast<FakePicturePileImpl*>(pending_layer_->pile())->set_has_text(true);
2555 static_cast<FakePicturePileImpl*>(active_layer_->pile())->set_has_text(true);
2557 // Since we're GPU-rasterizing but have text, starting an animation should
2558 // cause tiling resolution to get set to the maximum animation scale.
2559 animating_transform = true;
2560 contents_scale = 2.f;
2561 maximum_animation_scale = 3.f;
2563 SetContentsScaleOnBothLayers(contents_scale,
2566 maximum_animation_scale,
2567 animating_transform);
2568 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2570 // Further changes to scale during the animation should not cause a new
2571 // high-res tiling to get created.
2572 contents_scale = 4.f;
2573 maximum_animation_scale = 5.f;
2575 SetContentsScaleOnBothLayers(contents_scale,
2578 maximum_animation_scale,
2579 animating_transform);
2580 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2582 // Once we stop animating, a new high-res tiling should be created.
2583 animating_transform = false;
2585 SetContentsScaleOnBothLayers(contents_scale,
2588 maximum_animation_scale,
2589 animating_transform);
2590 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2593 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2594 base::TimeTicks time_ticks;
2595 time_ticks += base::TimeDelta::FromMilliseconds(1);
2596 host_impl_.SetCurrentBeginFrameArgs(
2597 CreateBeginFrameArgsForTesting(time_ticks));
2599 gfx::Size tile_size(100, 100);
2600 gfx::Size layer_bounds(1000, 1000);
2602 scoped_refptr<FakePicturePileImpl> pending_pile =
2603 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2605 SetupPendingTree(pending_pile);
2607 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2609 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2612 PictureLayerImpl::LayerRasterTileIterator it;
2616 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2619 pending_layer_->AddTiling(low_res_factor);
2620 pending_layer_->AddTiling(0.3f);
2621 pending_layer_->AddTiling(0.7f);
2622 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2623 pending_layer_->AddTiling(2.0f);
2625 host_impl_.SetViewportSize(gfx::Size(500, 500));
2626 host_impl_.pending_tree()->UpdateDrawProperties();
2628 std::set<Tile*> unique_tiles;
2629 bool reached_prepaint = false;
2630 size_t non_ideal_tile_count = 0u;
2631 size_t low_res_tile_count = 0u;
2632 size_t high_res_tile_count = 0u;
2633 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2637 TilePriority priority = tile->priority(PENDING_TREE);
2641 // Non-high res tiles only get visible tiles. Also, prepaint should only
2642 // come at the end of the iteration.
2643 if (priority.resolution != HIGH_RESOLUTION)
2644 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2645 else if (reached_prepaint)
2646 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2648 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2650 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2651 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2652 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2654 unique_tiles.insert(tile);
2657 EXPECT_TRUE(reached_prepaint);
2658 EXPECT_EQ(0u, non_ideal_tile_count);
2659 EXPECT_EQ(1u, low_res_tile_count);
2660 EXPECT_EQ(16u, high_res_tile_count);
2661 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2662 unique_tiles.size());
2665 time_ticks += base::TimeDelta::FromMilliseconds(200);
2666 host_impl_.SetCurrentBeginFrameArgs(
2667 CreateBeginFrameArgsForTesting(time_ticks));
2669 pending_layer_->draw_properties().visible_content_rect =
2670 gfx::Rect(1100, 1100, 500, 500);
2671 bool resourceless_software_draw = false;
2672 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2674 unique_tiles.clear();
2675 high_res_tile_count = 0u;
2676 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2680 TilePriority priority = tile->priority(PENDING_TREE);
2684 // Non-high res tiles only get visible tiles.
2685 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2686 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2688 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2690 unique_tiles.insert(tile);
2693 EXPECT_EQ(16u, high_res_tile_count);
2694 EXPECT_EQ(high_res_tile_count, unique_tiles.size());
2696 time_ticks += base::TimeDelta::FromMilliseconds(200);
2697 host_impl_.SetCurrentBeginFrameArgs(
2698 CreateBeginFrameArgsForTesting(time_ticks));
2700 pending_layer_->draw_properties().visible_content_rect =
2701 gfx::Rect(0, 0, 500, 500);
2702 pending_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
2704 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2705 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2706 tile_it != high_res_tiles.end();
2708 Tile* tile = *tile_it;
2709 ManagedTileState::TileVersion& tile_version =
2710 tile->GetTileVersionForTesting(
2711 tile->DetermineRasterModeForTree(ACTIVE_TREE));
2712 tile_version.SetSolidColorForTesting(SK_ColorRED);
2715 non_ideal_tile_count = 0;
2716 low_res_tile_count = 0;
2717 high_res_tile_count = 0;
2718 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2722 TilePriority priority = tile->priority(PENDING_TREE);
2726 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2727 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2728 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2731 EXPECT_EQ(0u, non_ideal_tile_count);
2732 EXPECT_EQ(1u, low_res_tile_count);
2733 EXPECT_EQ(0u, high_res_tile_count);
2736 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2737 gfx::Size tile_size(100, 100);
2738 gfx::Size layer_bounds(1000, 1000);
2740 scoped_refptr<FakePicturePileImpl> pending_pile =
2741 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2743 SetupPendingTree(pending_pile);
2745 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2747 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2749 std::vector<PictureLayerTiling*> tilings;
2750 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2751 tilings.push_back(pending_layer_->AddTiling(0.3f));
2752 tilings.push_back(pending_layer_->AddTiling(0.7f));
2753 tilings.push_back(pending_layer_->AddTiling(1.0f));
2754 tilings.push_back(pending_layer_->AddTiling(2.0f));
2756 host_impl_.SetViewportSize(gfx::Size(500, 500));
2757 host_impl_.pending_tree()->UpdateDrawProperties();
2759 std::vector<Tile*> all_tiles;
2760 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2762 tiling_iterator != tilings.end();
2763 ++tiling_iterator) {
2764 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2765 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2768 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2770 bool mark_required = false;
2771 size_t number_of_marked_tiles = 0u;
2772 size_t number_of_unmarked_tiles = 0u;
2773 for (size_t i = 0; i < tilings.size(); ++i) {
2774 PictureLayerTiling* tiling = tilings.at(i);
2775 for (PictureLayerTiling::CoverageIterator iter(
2777 pending_layer_->contents_scale_x(),
2778 pending_layer_->visible_content_rect());
2781 if (mark_required) {
2782 number_of_marked_tiles++;
2783 iter->MarkRequiredForActivation();
2785 number_of_unmarked_tiles++;
2787 mark_required = !mark_required;
2792 EXPECT_EQ(91u, all_tiles.size());
2793 EXPECT_EQ(91u, all_tiles_set.size());
2794 EXPECT_GT(number_of_marked_tiles, 1u);
2795 EXPECT_GT(number_of_unmarked_tiles, 1u);
2798 PictureLayerImpl::LayerEvictionTileIterator it;
2801 // Tiles don't have resources yet.
2802 it = PictureLayerImpl::LayerEvictionTileIterator(
2803 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2806 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2808 std::set<Tile*> unique_tiles;
2809 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2810 size_t scale_index = 0;
2811 bool reached_visible = false;
2812 Tile* last_tile = NULL;
2813 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2814 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2823 TilePriority priority = tile->priority(PENDING_TREE);
2825 if (priority.priority_bin == TilePriority::NOW) {
2826 reached_visible = true;
2831 EXPECT_FALSE(tile->required_for_activation());
2833 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2834 std::numeric_limits<float>::epsilon()) {
2836 ASSERT_LT(scale_index, arraysize(expected_scales));
2839 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2840 unique_tiles.insert(tile);
2842 // If the tile is the same rough bin as last tile (same activation, bin, and
2843 // scale), then distance should be decreasing.
2844 if (tile->required_for_activation() ==
2845 last_tile->required_for_activation() &&
2846 priority.priority_bin ==
2847 last_tile->priority(PENDING_TREE).priority_bin &&
2848 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2849 std::numeric_limits<float>::epsilon()) {
2850 EXPECT_LE(priority.distance_to_visible,
2851 last_tile->priority(PENDING_TREE).distance_to_visible);
2857 EXPECT_TRUE(reached_visible);
2858 EXPECT_EQ(65u, unique_tiles.size());
2861 bool reached_required = false;
2866 TilePriority priority = tile->priority(PENDING_TREE);
2867 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2869 if (reached_required) {
2870 EXPECT_TRUE(tile->required_for_activation());
2871 } else if (tile->required_for_activation()) {
2872 reached_required = true;
2876 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2877 std::numeric_limits<float>::epsilon()) {
2879 ASSERT_LT(scale_index, arraysize(expected_scales));
2882 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2883 unique_tiles.insert(tile);
2886 EXPECT_TRUE(reached_required);
2887 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2890 TEST_F(PictureLayerImplTest, Occlusion) {
2891 gfx::Size tile_size(102, 102);
2892 gfx::Size layer_bounds(1000, 1000);
2893 gfx::Size viewport_size(1000, 1000);
2895 LayerTestCommon::LayerImplTest impl;
2897 scoped_refptr<FakePicturePileImpl> pending_pile =
2898 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2899 SetupPendingTree(pending_pile);
2900 pending_layer_->SetBounds(layer_bounds);
2902 active_layer_->set_fixed_tile_size(tile_size);
2904 host_impl_.SetViewportSize(viewport_size);
2905 host_impl_.active_tree()->UpdateDrawProperties();
2907 std::vector<Tile*> tiles =
2908 active_layer_->HighResTiling()->AllTilesForTesting();
2909 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2912 SCOPED_TRACE("No occlusion");
2914 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2916 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2917 gfx::Rect(layer_bounds));
2918 EXPECT_EQ(100u, impl.quad_list().size());
2922 SCOPED_TRACE("Full occlusion");
2923 gfx::Rect occluded(active_layer_->visible_content_rect());
2924 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2926 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2927 EXPECT_EQ(impl.quad_list().size(), 0u);
2931 SCOPED_TRACE("Partial occlusion");
2932 gfx::Rect occluded(150, 0, 200, 1000);
2933 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2935 size_t partially_occluded_count = 0;
2936 LayerTestCommon::VerifyQuadsAreOccluded(
2937 impl.quad_list(), occluded, &partially_occluded_count);
2938 // The layer outputs one quad, which is partially occluded.
2939 EXPECT_EQ(100u - 10u, impl.quad_list().size());
2940 EXPECT_EQ(10u + 10u, partially_occluded_count);
2944 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2945 gfx::Size tile_size(host_impl_.settings().default_tile_size);
2946 SetupDefaultTrees(tile_size);
2948 float contents_scale = 2.f;
2949 float device_scale = 1.f;
2950 float page_scale = 1.f;
2951 float maximum_animation_scale = 1.f;
2952 bool animating_transform = false;
2954 SetContentsScaleOnBothLayers(contents_scale,
2957 maximum_animation_scale,
2958 animating_transform);
2959 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2961 // Changing the source scale without being in an animation will cause
2962 // the layer to reset its source scale to 1.f.
2963 contents_scale = 3.f;
2965 SetContentsScaleOnBothLayers(contents_scale,
2968 maximum_animation_scale,
2969 animating_transform);
2970 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2972 // Further changes to the source scale will no longer be reflected in the
2974 contents_scale = 0.5f;
2976 SetContentsScaleOnBothLayers(contents_scale,
2979 maximum_animation_scale,
2980 animating_transform);
2981 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2984 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2985 gfx::Size tile_size(100, 100);
2986 gfx::Size layer_bounds(1000, 1000);
2988 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2990 // Make sure some tiles are not shared.
2991 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2993 CreateHighLowResAndSetAllTilesVisible();
2994 active_layer_->SetAllTilesReady();
2995 pending_layer_->MarkVisibleResourcesAsRequired();
2997 // All pending layer tiles required are not ready.
2998 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3000 // Initialize all low-res tiles.
3001 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3003 // Low-res tiles should not be enough.
3004 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3006 // Initialize remaining tiles.
3007 pending_layer_->SetAllTilesReady();
3009 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3012 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
3013 gfx::Size tile_size(100, 100);
3014 gfx::Size layer_bounds(1000, 1000);
3016 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3018 // Make sure some tiles are not shared.
3019 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3021 CreateHighLowResAndSetAllTilesVisible();
3022 active_layer_->SetAllTilesReady();
3023 pending_layer_->MarkVisibleResourcesAsRequired();
3025 // All pending layer tiles required are not ready.
3026 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3028 // Initialize all high-res tiles.
3029 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3031 // High-res tiles should be enough, since they cover everything visible.
3032 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3035 TEST_F(PictureLayerImplTest,
3036 SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
3037 gfx::Size tile_size(100, 100);
3038 gfx::Size layer_bounds(1000, 1000);
3040 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3042 // Make sure some tiles are not shared.
3043 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3045 CreateHighLowResAndSetAllTilesVisible();
3047 // Initialize all high-res tiles in the active layer.
3048 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3049 // And all the low-res tiles in the pending layer.
3050 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
3052 pending_layer_->MarkVisibleResourcesAsRequired();
3054 // The unshared high-res tiles are not ready, so we cannot activate.
3055 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3057 // When the unshared pending high-res tiles are ready, we can activate.
3058 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3059 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3062 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
3063 gfx::Size tile_size(100, 100);
3064 gfx::Size layer_bounds(1000, 1000);
3066 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3068 // Make sure some tiles are not shared.
3069 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
3071 CreateHighLowResAndSetAllTilesVisible();
3073 // Initialize all high-res tiles in the active layer.
3074 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
3076 pending_layer_->MarkVisibleResourcesAsRequired();
3078 // The unshared high-res tiles are not ready, so we cannot activate.
3079 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3081 // When the unshared pending high-res tiles are ready, we can activate.
3082 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
3083 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3086 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
3088 NoLowResPictureLayerImplTest()
3089 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
3092 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3093 gfx::Size tile_size(400, 400);
3094 gfx::Size layer_bounds(1300, 1900);
3096 scoped_refptr<FakePicturePileImpl> pending_pile =
3097 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3098 scoped_refptr<FakePicturePileImpl> active_pile =
3099 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3101 SetupTrees(pending_pile, active_pile);
3102 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3104 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3105 EXPECT_LT(low_res_factor, 1.f);
3107 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3108 6.f, // ideal contents scale
3109 3.f, // device scale
3111 1.f, // maximum animation scale
3113 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3114 EXPECT_FLOAT_EQ(6.f,
3115 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3117 // If we change the page scale factor, then we should get new tilings.
3118 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3119 6.6f, // ideal contents scale
3120 3.f, // device scale
3122 1.f, // maximum animation scale
3124 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
3125 EXPECT_FLOAT_EQ(6.6f,
3126 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3128 // If we change the device scale factor, then we should get new tilings.
3129 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3130 7.26f, // ideal contents scale
3131 3.3f, // device scale
3133 1.f, // maximum animation scale
3135 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3136 EXPECT_FLOAT_EQ(7.26f,
3137 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3139 // If we change the device scale factor, but end up at the same total scale
3140 // factor somehow, then we don't get new tilings.
3141 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3142 7.26f, // ideal contents scale
3143 2.2f, // device scale
3145 1.f, // maximum animation scale
3147 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3148 EXPECT_FLOAT_EQ(7.26f,
3149 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3152 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
3153 gfx::Size tile_size(100, 100);
3154 gfx::Size layer_bounds(1000, 1000);
3156 scoped_refptr<FakePicturePileImpl> pending_pile =
3157 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
3158 // Layers with entirely empty piles can't get tilings.
3159 pending_pile->AddRecordingAt(0, 0);
3161 SetupPendingTree(pending_pile);
3163 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3164 pending_layer_->AddTiling(1.0f);
3165 pending_layer_->AddTiling(2.0f);
3167 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
3168 // on a layer with no recordings.
3169 host_impl_.pending_tree()->UpdateDrawProperties();
3170 pending_layer_->MarkVisibleResourcesAsRequired();
3173 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
3174 gfx::Size layer_bounds(400, 400);
3175 gfx::Size tile_size(100, 100);
3176 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3178 CreateHighLowResAndSetAllTilesVisible();
3180 Tile* some_active_tile =
3181 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3182 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3184 // All tiles shared (no invalidation), so even though the active tree's
3185 // tiles aren't ready, there is nothing required.
3186 pending_layer_->MarkVisibleResourcesAsRequired();
3187 AssertNoTilesRequired(pending_layer_->HighResTiling());
3188 if (host_impl_.settings().create_low_res_tiling) {
3189 AssertNoTilesRequired(pending_layer_->LowResTiling());
3193 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3194 gfx::Size layer_bounds(400, 400);
3195 gfx::Size tile_size(100, 100);
3196 scoped_refptr<FakePicturePileImpl> pending_pile =
3197 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3198 // This pile will create tilings, but has no recordings so will not create any
3199 // tiles. This is attempting to simulate scrolling past the end of recorded
3200 // content on the active layer, where the recordings are so far away that
3201 // no tiles are created.
3202 scoped_refptr<FakePicturePileImpl> active_pile =
3203 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3204 tile_size, layer_bounds);
3205 SetupTrees(pending_pile, active_pile);
3206 pending_layer_->set_fixed_tile_size(tile_size);
3207 active_layer_->set_fixed_tile_size(tile_size);
3209 CreateHighLowResAndSetAllTilesVisible();
3211 // Active layer has tilings, but no tiles due to missing recordings.
3212 EXPECT_TRUE(active_layer_->CanHaveTilings());
3213 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3214 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3215 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3217 // Since the active layer has no tiles at all, the pending layer doesn't
3218 // need content in order to activate.
3219 pending_layer_->MarkVisibleResourcesAsRequired();
3220 AssertNoTilesRequired(pending_layer_->HighResTiling());
3221 if (host_impl_.settings().create_low_res_tiling)
3222 AssertNoTilesRequired(pending_layer_->LowResTiling());
3225 TEST_F(NoLowResPictureLayerImplTest, InvalidViewportForPrioritizingTiles) {
3226 base::TimeTicks time_ticks;
3227 time_ticks += base::TimeDelta::FromMilliseconds(1);
3228 host_impl_.SetCurrentBeginFrameArgs(
3229 CreateBeginFrameArgsForTesting(time_ticks));
3231 gfx::Size tile_size(100, 100);
3232 gfx::Size layer_bounds(400, 400);
3234 scoped_refptr<FakePicturePileImpl> pending_pile =
3235 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3236 scoped_refptr<FakePicturePileImpl> active_pile =
3237 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3239 SetupTrees(pending_pile, active_pile);
3241 Region invalidation;
3242 AddDefaultTilingsWithInvalidation(invalidation);
3243 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3245 // UpdateTiles with valid viewport. Should update tile viewport.
3246 // Note viewport is considered invalid if and only if in resourceless
3248 bool resourceless_software_draw = false;
3249 gfx::Rect viewport = gfx::Rect(layer_bounds);
3250 gfx::Transform transform;
3251 host_impl_.SetExternalDrawConstraints(transform,
3256 resourceless_software_draw);
3257 active_layer_->draw_properties().visible_content_rect = viewport;
3258 active_layer_->draw_properties().screen_space_transform = transform;
3259 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3261 gfx::Rect visible_rect_for_tile_priority =
3262 active_layer_->visible_rect_for_tile_priority();
3263 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3264 gfx::Rect viewport_rect_for_tile_priority =
3265 active_layer_->viewport_rect_for_tile_priority();
3266 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
3267 gfx::Transform screen_space_transform_for_tile_priority =
3268 active_layer_->screen_space_transform_for_tile_priority();
3270 // Expand viewport and set it as invalid for prioritizing tiles.
3271 // Should update viewport and transform, but not update visible rect.
3272 time_ticks += base::TimeDelta::FromMilliseconds(200);
3273 host_impl_.SetCurrentBeginFrameArgs(
3274 CreateBeginFrameArgsForTesting(time_ticks));
3275 resourceless_software_draw = true;
3276 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3277 transform.Translate(1.f, 1.f);
3278 active_layer_->draw_properties().visible_content_rect = viewport;
3279 active_layer_->draw_properties().screen_space_transform = transform;
3280 host_impl_.SetExternalDrawConstraints(transform,
3285 resourceless_software_draw);
3286 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3288 // Viewport and transform for tile priority are updated.
3289 EXPECT_EQ(viewport, active_layer_->viewport_rect_for_tile_priority());
3290 EXPECT_TRANSFORMATION_MATRIX_EQ(
3291 transform, active_layer_->screen_space_transform_for_tile_priority());
3292 // Visible rect for tile priority retains old value.
3293 EXPECT_EQ(visible_rect_for_tile_priority,
3294 active_layer_->visible_rect_for_tile_priority());
3296 // Keep expanded viewport but mark it valid. Should update tile viewport.
3297 time_ticks += base::TimeDelta::FromMilliseconds(200);
3298 host_impl_.SetCurrentBeginFrameArgs(
3299 CreateBeginFrameArgsForTesting(time_ticks));
3300 resourceless_software_draw = false;
3301 host_impl_.SetExternalDrawConstraints(transform,
3306 resourceless_software_draw);
3307 active_layer_->UpdateTiles(Occlusion(), resourceless_software_draw);
3309 EXPECT_TRANSFORMATION_MATRIX_EQ(
3310 transform, active_layer_->screen_space_transform_for_tile_priority());
3311 EXPECT_EQ(viewport, active_layer_->visible_rect_for_tile_priority());
3313 // Match the reverse translate in |transform|.
3314 EXPECT_EQ(viewport - gfx::Vector2d(1, 1),
3315 active_layer_->viewport_rect_for_tile_priority());
3318 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3319 gfx::Size tile_size(400, 400);
3320 gfx::Size layer_bounds(1300, 1900);
3322 scoped_refptr<FakePicturePileImpl> pending_pile =
3323 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3324 scoped_refptr<FakePicturePileImpl> active_pile =
3325 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3327 std::vector<PictureLayerTiling*> used_tilings;
3329 SetupTrees(pending_pile, active_pile);
3330 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3332 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3333 EXPECT_LT(low_res_factor, 1.f);
3335 float device_scale = 1.7f;
3336 float page_scale = 3.2f;
3339 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3340 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3342 // We only have ideal tilings, so they aren't removed.
3343 used_tilings.clear();
3344 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3345 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3347 host_impl_.PinchGestureBegin();
3349 // Changing the ideal but not creating new tilings.
3352 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3353 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3355 // The tilings are still our target scale, so they aren't removed.
3356 used_tilings.clear();
3357 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3358 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3360 host_impl_.PinchGestureEnd();
3362 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3365 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3366 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3367 EXPECT_FLOAT_EQ(1.f,
3368 active_layer_->tilings()->tiling_at(1)->contents_scale());
3370 // Mark the non-ideal tilings as used. They won't be removed.
3371 used_tilings.clear();
3372 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3373 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3374 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3376 // Now move the ideal scale to 0.5. Our target stays 1.2.
3377 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3379 // The high resolution tiling is between target and ideal, so is not
3380 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3381 used_tilings.clear();
3382 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3383 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3385 // Now move the ideal scale to 1.0. Our target stays 1.2.
3386 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3388 // All the tilings are between are target and the ideal, so they are not
3390 used_tilings.clear();
3391 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3392 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3394 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3395 SetupDrawPropertiesAndUpdateTiles(
3396 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3398 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3399 // in the range [1.0,1.2] and are kept.
3400 used_tilings.clear();
3401 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3402 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3404 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3406 SetupDrawPropertiesAndUpdateTiles(
3407 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3409 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3410 // target raster scale. But it is in our used tilings set, so nothing is
3412 used_tilings.clear();
3413 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3414 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3415 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3417 // If we remove it from our used tilings set, it is outside the range to keep
3418 // so it is deleted.
3419 used_tilings.clear();
3420 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3421 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3424 TEST_F(PictureLayerImplTest, ScaleCollision) {
3425 gfx::Size tile_size(400, 400);
3426 gfx::Size layer_bounds(1300, 1900);
3428 scoped_refptr<FakePicturePileImpl> pending_pile =
3429 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3430 scoped_refptr<FakePicturePileImpl> active_pile =
3431 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3433 std::vector<PictureLayerTiling*> used_tilings;
3435 SetupTrees(pending_pile, active_pile);
3437 float pending_contents_scale = 1.f;
3438 float active_contents_scale = 2.f;
3439 float device_scale_factor = 1.f;
3440 float page_scale_factor = 1.f;
3441 float maximum_animation_contents_scale = 1.f;
3442 bool animating_transform = false;
3444 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3445 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3446 EXPECT_LT(low_res_factor, 1.f);
3448 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3449 pending_contents_scale,
3450 device_scale_factor,
3452 maximum_animation_contents_scale,
3453 animating_transform);
3454 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3455 active_contents_scale,
3456 device_scale_factor,
3458 maximum_animation_contents_scale,
3459 animating_transform);
3461 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3462 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3464 EXPECT_EQ(active_contents_scale,
3465 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3466 EXPECT_EQ(pending_contents_scale,
3467 pending_layer_->tilings()->tiling_at(1)->contents_scale());
3468 EXPECT_EQ(active_contents_scale * low_res_factor,
3469 pending_layer_->tilings()->tiling_at(2)->contents_scale());
3470 EXPECT_EQ(pending_contents_scale * low_res_factor,
3471 pending_layer_->tilings()->tiling_at(3)->contents_scale());
3473 EXPECT_EQ(active_contents_scale,
3474 active_layer_->tilings()->tiling_at(0)->contents_scale());
3475 EXPECT_EQ(pending_contents_scale,
3476 active_layer_->tilings()->tiling_at(1)->contents_scale());
3477 EXPECT_EQ(active_contents_scale * low_res_factor,
3478 active_layer_->tilings()->tiling_at(2)->contents_scale());
3479 EXPECT_EQ(pending_contents_scale * low_res_factor,
3480 active_layer_->tilings()->tiling_at(3)->contents_scale());
3482 // The unused low res tiling from the pending tree must be kept or we may add
3483 // it again on the active tree and collide with the pending tree.
3484 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3485 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3486 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3488 EXPECT_EQ(active_contents_scale,
3489 active_layer_->tilings()->tiling_at(0)->contents_scale());
3490 EXPECT_EQ(pending_contents_scale,
3491 active_layer_->tilings()->tiling_at(1)->contents_scale());
3492 EXPECT_EQ(active_contents_scale * low_res_factor,
3493 active_layer_->tilings()->tiling_at(2)->contents_scale());
3494 EXPECT_EQ(pending_contents_scale * low_res_factor,
3495 active_layer_->tilings()->tiling_at(3)->contents_scale());
3498 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3499 gfx::Size tile_size(400, 400);
3500 gfx::Size layer_bounds(1300, 1900);
3502 scoped_refptr<FakePicturePileImpl> pending_pile =
3503 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3504 scoped_refptr<FakePicturePileImpl> active_pile =
3505 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3507 SetupTrees(pending_pile, active_pile);
3508 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3510 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3511 1.3f, // ideal contents scale
3512 2.7f, // device scale
3514 1.f, // maximum animation scale
3516 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3518 // All tilings should be removed when losing output surface.
3519 active_layer_->ReleaseResources();
3520 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3521 pending_layer_->ReleaseResources();
3522 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3524 // This should create new tilings.
3525 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3526 1.3f, // ideal contents scale
3527 2.7f, // device scale
3529 1.f, // maximum animation scale
3531 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3534 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3535 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3536 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3538 gfx::Size tile_size(400, 400);
3539 gfx::Size layer_bounds(1000, 2000);
3541 scoped_refptr<FakePicturePileImpl> pending_pile =
3542 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3543 scoped_refptr<FakePicturePileImpl> active_pile =
3544 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3546 SetupTrees(pending_pile, active_pile);
3548 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3549 host_impl_.pending_tree()->UpdateDrawProperties();
3551 active_layer_->draw_properties().visible_content_rect =
3552 gfx::Rect(layer_bounds);
3553 host_impl_.active_tree()->UpdateDrawProperties();
3555 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3556 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3557 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3558 SK_MScalar1 / max_contents_scale);
3560 AppendQuadsData data;
3561 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3563 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3564 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3565 // The content_to_target_transform should be scaled by the
3566 // MaximumTilingContentsScale on the layer.
3567 EXPECT_EQ(scaled_draw_transform.ToString(),
3568 render_pass->shared_quad_state_list[0]
3569 ->content_to_target_transform.ToString());
3570 // The content_bounds should be scaled by the
3571 // MaximumTilingContentsScale on the layer.
3572 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3573 render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3574 // The visible_content_rect should be scaled by the
3575 // MaximumTilingContentsScale on the layer.
3577 gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3578 render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3581 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3582 gfx::Size tile_size(400, 400);
3583 gfx::Size bounds(100000, 100);
3585 host_impl_.CreatePendingTree();
3587 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3589 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3590 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3592 layer_with_mask->SetBounds(bounds);
3593 layer_with_mask->SetContentBounds(bounds);
3595 scoped_refptr<FakePicturePileImpl> pending_pile =
3596 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3597 pending_pile->set_is_mask(true);
3598 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3599 host_impl_.pending_tree(), 3, pending_pile);
3601 mask->SetBounds(bounds);
3602 mask->SetContentBounds(bounds);
3603 mask->SetDrawsContent(true);
3605 FakePictureLayerImpl* pending_mask_content = mask.get();
3606 layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3608 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3609 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3611 child_of_layer_with_mask->SetBounds(bounds);
3612 child_of_layer_with_mask->SetContentBounds(bounds);
3613 child_of_layer_with_mask->SetDrawsContent(true);
3615 layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3617 root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3619 host_impl_.pending_tree()->SetRootLayer(root.Pass());
3621 EXPECT_FALSE(pending_mask_content->tilings());
3622 host_impl_.pending_tree()->UpdateDrawProperties();
3623 EXPECT_NE(0u, pending_mask_content->num_tilings());
3626 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3628 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3630 virtual void InitializeRenderer() OVERRIDE {
3631 host_impl_.InitializeRenderer(
3632 FakeOutputSurface::CreateDelegating3d().PassAs<OutputSurface>());
3636 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3637 DelegatingRendererWithTileOOM) {
3638 // This test is added for crbug.com/402321, where quad should be produced when
3639 // raster on demand is not allowed and tile is OOM.
3640 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3641 gfx::Size layer_bounds(1000, 1000);
3644 scoped_refptr<FakePicturePileImpl> pending_pile =
3645 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3646 SetupPendingTree(pending_pile);
3647 pending_layer_->SetBounds(layer_bounds);
3648 host_impl_.SetViewportSize(layer_bounds);
3650 host_impl_.active_tree()->UpdateDrawProperties();
3651 std::vector<Tile*> tiles =
3652 active_layer_->HighResTiling()->AllTilesForTesting();
3653 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3655 // Force tiles after max_tiles to be OOM. TileManager uses
3656 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3657 // directly set state to host_impl_, so we set policy that would change the
3658 // state. We also need to update tree priority separately.
3659 GlobalStateThatImpactsTilePriority state;
3660 size_t max_tiles = 1;
3661 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3662 size_t resource_limit = max_tiles;
3663 ManagedMemoryPolicy policy(memory_limit,
3664 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3666 host_impl_.SetMemoryPolicy(policy);
3667 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3668 host_impl_.ManageTiles();
3670 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3671 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3672 AppendQuadsData data;
3673 active_layer_->WillDraw(DRAW_MODE_HARDWARE, NULL);
3674 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3675 active_layer_->DidDraw(NULL);
3677 // Even when OOM, quads should be produced, and should be different material
3678 // from quads with resource.
3679 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3680 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3681 render_pass->quad_list.front()->material);
3682 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3683 render_pass->quad_list.back()->material);
3686 class OcclusionTrackingSettings : public LowResTilingsSettings {
3688 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3691 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3693 OcclusionTrackingPictureLayerImplTest()
3694 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3696 void VerifyEvictionConsidersOcclusion(
3697 PictureLayerImpl* layer,
3698 size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3699 for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3701 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3702 size_t occluded_tile_count = 0u;
3703 Tile* last_tile = NULL;
3705 for (PictureLayerImpl::LayerEvictionTileIterator it =
3706 PictureLayerImpl::LayerEvictionTileIterator(layer,
3714 // The only way we will encounter an occluded tile after an unoccluded
3715 // tile is if the priorty bin decreased, the tile is required for
3716 // activation, or the scale changed.
3717 bool tile_is_occluded =
3718 tile->is_occluded_for_tree_priority(tree_priority);
3719 if (tile_is_occluded) {
3720 occluded_tile_count++;
3722 bool last_tile_is_occluded =
3723 last_tile->is_occluded_for_tree_priority(tree_priority);
3724 if (!last_tile_is_occluded) {
3725 TilePriority::PriorityBin tile_priority_bin =
3726 tile->priority_for_tree_priority(tree_priority).priority_bin;
3727 TilePriority::PriorityBin last_tile_priority_bin =
3728 last_tile->priority_for_tree_priority(tree_priority)
3732 (tile_priority_bin < last_tile_priority_bin) ||
3733 tile->required_for_activation() ||
3734 (tile->contents_scale() != last_tile->contents_scale()));
3739 EXPECT_EQ(expected_occluded_tile_count[priority_count],
3740 occluded_tile_count);
3745 TEST_F(OcclusionTrackingPictureLayerImplTest,
3746 OccludedTilesSkippedDuringRasterization) {
3747 base::TimeTicks time_ticks;
3748 time_ticks += base::TimeDelta::FromMilliseconds(1);
3749 host_impl_.SetCurrentBeginFrameArgs(
3750 CreateBeginFrameArgsForTesting(time_ticks));
3752 gfx::Size tile_size(102, 102);
3753 gfx::Size layer_bounds(1000, 1000);
3754 gfx::Size viewport_size(500, 500);
3755 gfx::Point occluding_layer_position(310, 0);
3757 scoped_refptr<FakePicturePileImpl> pending_pile =
3758 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3759 SetupPendingTree(pending_pile);
3760 pending_layer_->set_fixed_tile_size(tile_size);
3762 host_impl_.SetViewportSize(viewport_size);
3763 host_impl_.pending_tree()->UpdateDrawProperties();
3766 int unoccluded_tile_count = 0;
3767 for (PictureLayerImpl::LayerRasterTileIterator it =
3768 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3773 // Occluded tiles should not be iterated over.
3774 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3776 // Some tiles may not be visible (i.e. outside the viewport). The rest are
3777 // visible and at least partially unoccluded, verified by the above expect.
3778 bool tile_is_visible =
3779 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3780 if (tile_is_visible)
3781 unoccluded_tile_count++;
3783 EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3785 // Partial occlusion.
3786 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3787 LayerImpl* layer1 = pending_layer_->children()[0];
3788 layer1->SetBounds(layer_bounds);
3789 layer1->SetContentBounds(layer_bounds);
3790 layer1->SetDrawsContent(true);
3791 layer1->SetContentsOpaque(true);
3792 layer1->SetPosition(occluding_layer_position);
3794 time_ticks += base::TimeDelta::FromMilliseconds(200);
3795 host_impl_.SetCurrentBeginFrameArgs(
3796 CreateBeginFrameArgsForTesting(time_ticks));
3797 host_impl_.pending_tree()->UpdateDrawProperties();
3799 unoccluded_tile_count = 0;
3800 for (PictureLayerImpl::LayerRasterTileIterator it =
3801 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3806 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3808 bool tile_is_visible =
3809 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3810 if (tile_is_visible)
3811 unoccluded_tile_count++;
3813 EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3816 layer1->SetPosition(gfx::Point(0, 0));
3818 time_ticks += base::TimeDelta::FromMilliseconds(200);
3819 host_impl_.SetCurrentBeginFrameArgs(
3820 CreateBeginFrameArgsForTesting(time_ticks));
3821 host_impl_.pending_tree()->UpdateDrawProperties();
3823 unoccluded_tile_count = 0;
3824 for (PictureLayerImpl::LayerRasterTileIterator it =
3825 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3830 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3832 bool tile_is_visible =
3833 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3834 if (tile_is_visible)
3835 unoccluded_tile_count++;
3837 EXPECT_EQ(unoccluded_tile_count, 0);
3840 TEST_F(OcclusionTrackingPictureLayerImplTest,
3841 OccludedTilesNotMarkedAsRequired) {
3842 base::TimeTicks time_ticks;
3843 time_ticks += base::TimeDelta::FromMilliseconds(1);
3844 host_impl_.SetCurrentBeginFrameArgs(
3845 CreateBeginFrameArgsForTesting(time_ticks));
3847 gfx::Size tile_size(102, 102);
3848 gfx::Size layer_bounds(1000, 1000);
3849 gfx::Size viewport_size(500, 500);
3850 gfx::Point occluding_layer_position(310, 0);
3852 scoped_refptr<FakePicturePileImpl> pending_pile =
3853 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3854 SetupPendingTree(pending_pile);
3855 pending_layer_->set_fixed_tile_size(tile_size);
3857 host_impl_.SetViewportSize(viewport_size);
3858 host_impl_.pending_tree()->UpdateDrawProperties();
3861 int occluded_tile_count = 0;
3862 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3863 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3865 occluded_tile_count = 0;
3866 for (PictureLayerTiling::CoverageIterator iter(
3868 pending_layer_->contents_scale_x(),
3869 gfx::Rect(layer_bounds));
3874 const Tile* tile = *iter;
3876 // Fully occluded tiles are not required for activation.
3877 if (tile->is_occluded(PENDING_TREE)) {
3878 EXPECT_FALSE(tile->required_for_activation());
3879 occluded_tile_count++;
3882 EXPECT_EQ(occluded_tile_count, 0);
3885 // Partial occlusion.
3886 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3887 LayerImpl* layer1 = pending_layer_->children()[0];
3888 layer1->SetBounds(layer_bounds);
3889 layer1->SetContentBounds(layer_bounds);
3890 layer1->SetDrawsContent(true);
3891 layer1->SetContentsOpaque(true);
3892 layer1->SetPosition(occluding_layer_position);
3894 time_ticks += base::TimeDelta::FromMilliseconds(200);
3895 host_impl_.SetCurrentBeginFrameArgs(
3896 CreateBeginFrameArgsForTesting(time_ticks));
3897 host_impl_.pending_tree()->UpdateDrawProperties();
3899 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3900 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3902 occluded_tile_count = 0;
3903 for (PictureLayerTiling::CoverageIterator iter(
3905 pending_layer_->contents_scale_x(),
3906 gfx::Rect(layer_bounds));
3911 const Tile* tile = *iter;
3913 if (tile->is_occluded(PENDING_TREE)) {
3914 EXPECT_FALSE(tile->required_for_activation());
3915 occluded_tile_count++;
3920 EXPECT_EQ(occluded_tile_count, 5);
3923 EXPECT_EQ(occluded_tile_count, 2);
3931 layer1->SetPosition(gfx::PointF(0, 0));
3933 time_ticks += base::TimeDelta::FromMilliseconds(200);
3934 host_impl_.SetCurrentBeginFrameArgs(
3935 CreateBeginFrameArgsForTesting(time_ticks));
3936 host_impl_.pending_tree()->UpdateDrawProperties();
3938 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3939 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3941 occluded_tile_count = 0;
3942 for (PictureLayerTiling::CoverageIterator iter(
3944 pending_layer_->contents_scale_x(),
3945 gfx::Rect(layer_bounds));
3950 const Tile* tile = *iter;
3952 if (tile->is_occluded(PENDING_TREE)) {
3953 EXPECT_FALSE(tile->required_for_activation());
3954 occluded_tile_count++;
3959 EXPECT_EQ(occluded_tile_count, 25);
3962 EXPECT_EQ(occluded_tile_count, 4);
3970 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3971 gfx::Size tile_size(102, 102);
3972 gfx::Size layer_bounds(1000, 1000);
3973 gfx::Size viewport_size(500, 500);
3974 gfx::Point occluding_layer_position(310, 0);
3976 scoped_refptr<FakePicturePileImpl> pending_pile =
3977 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3978 SetupPendingTree(pending_pile);
3979 pending_layer_->set_fixed_tile_size(tile_size);
3981 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3983 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3985 std::vector<PictureLayerTiling*> tilings;
3986 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3987 tilings.push_back(pending_layer_->AddTiling(0.3f));
3988 tilings.push_back(pending_layer_->AddTiling(0.7f));
3989 tilings.push_back(pending_layer_->AddTiling(1.0f));
3990 tilings.push_back(pending_layer_->AddTiling(2.0f));
3992 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3993 LayerImpl* layer1 = pending_layer_->children()[0];
3994 layer1->SetBounds(layer_bounds);
3995 layer1->SetContentBounds(layer_bounds);
3996 layer1->SetDrawsContent(true);
3997 layer1->SetContentsOpaque(true);
3998 layer1->SetPosition(occluding_layer_position);
4000 host_impl_.SetViewportSize(viewport_size);
4001 host_impl_.pending_tree()->UpdateDrawProperties();
4003 int tiling_count = 0;
4004 int occluded_tile_count = 0;
4005 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4007 tiling_iterator != tilings.end();
4008 ++tiling_iterator) {
4009 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4011 occluded_tile_count = 0;
4012 for (size_t i = 0; i < tiles.size(); ++i) {
4013 if (tiles[i]->is_occluded(PENDING_TREE)) {
4014 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4015 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
4016 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
4017 occluded_tile_count++;
4020 switch (tiling_count) {
4023 EXPECT_EQ(occluded_tile_count, 2);
4026 EXPECT_EQ(occluded_tile_count, 4);
4029 EXPECT_EQ(occluded_tile_count, 5);
4032 EXPECT_EQ(occluded_tile_count, 30);
4041 EXPECT_EQ(tiling_count, 5);
4044 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
4045 gfx::Size tile_size(102, 102);
4046 gfx::Size layer_bounds(1000, 1000);
4047 gfx::Size viewport_size(1000, 1000);
4048 gfx::Point occluding_layer_position(310, 0);
4049 gfx::Rect invalidation_rect(230, 230, 102, 102);
4051 scoped_refptr<FakePicturePileImpl> pending_pile =
4052 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4053 scoped_refptr<FakePicturePileImpl> active_pile =
4054 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4055 SetupTrees(pending_pile, active_pile);
4057 // Partially occlude the active layer.
4058 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4059 LayerImpl* layer1 = active_layer_->children()[0];
4060 layer1->SetBounds(layer_bounds);
4061 layer1->SetContentBounds(layer_bounds);
4062 layer1->SetDrawsContent(true);
4063 layer1->SetContentsOpaque(true);
4064 layer1->SetPosition(occluding_layer_position);
4066 // Partially invalidate the pending layer.
4067 pending_layer_->set_invalidation(invalidation_rect);
4069 host_impl_.SetViewportSize(viewport_size);
4071 active_layer_->CreateDefaultTilingsAndTiles();
4072 pending_layer_->CreateDefaultTilingsAndTiles();
4074 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4075 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4077 for (PictureLayerTiling::CoverageIterator iter(
4079 pending_layer_->contents_scale_x(),
4080 gfx::Rect(layer_bounds));
4085 const Tile* tile = *iter;
4087 // All tiles are unoccluded on the pending tree.
4088 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4091 pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4092 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4093 tile->content_rect(), 1.0f / tile->contents_scale());
4095 if (scaled_content_rect.Intersects(invalidation_rect)) {
4096 // Tiles inside the invalidation rect are only on the pending tree.
4097 EXPECT_NE(tile, twin_tile);
4099 // Unshared tiles should be unoccluded on the active tree by default.
4100 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4102 // Tiles outside the invalidation rect are shared between both trees.
4103 EXPECT_EQ(tile, twin_tile);
4104 // Shared tiles are occluded on the active tree iff they lie beneath the
4106 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4107 scaled_content_rect.x() >= occluding_layer_position.x());
4112 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4113 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4115 for (PictureLayerTiling::CoverageIterator iter(
4117 active_layer_->contents_scale_x(),
4118 gfx::Rect(layer_bounds));
4123 const Tile* tile = *iter;
4126 active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4127 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4128 tile->content_rect(), 1.0f / tile->contents_scale());
4130 // Since we've already checked the shared tiles, only consider tiles in
4131 // the invalidation rect.
4132 if (scaled_content_rect.Intersects(invalidation_rect)) {
4133 // Tiles inside the invalidation rect are only on the active tree.
4134 EXPECT_NE(tile, twin_tile);
4136 // Unshared tiles should be unoccluded on the pending tree by default.
4137 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4139 // Unshared tiles are occluded on the active tree iff they lie beneath
4140 // the occluding layer.
4141 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4142 scaled_content_rect.x() >= occluding_layer_position.x());
4148 TEST_F(OcclusionTrackingPictureLayerImplTest,
4149 OccludedTilesConsideredDuringEviction) {
4150 gfx::Size tile_size(102, 102);
4151 gfx::Size layer_bounds(1000, 1000);
4152 gfx::Size viewport_size(500, 500);
4153 gfx::Point pending_occluding_layer_position(310, 0);
4154 gfx::Point active_occluding_layer_position(0, 310);
4155 gfx::Rect invalidation_rect(230, 230, 102, 102);
4157 scoped_refptr<FakePicturePileImpl> pending_pile =
4158 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4159 scoped_refptr<FakePicturePileImpl> active_pile =
4160 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4161 SetupTrees(pending_pile, active_pile);
4163 pending_layer_->set_fixed_tile_size(tile_size);
4164 active_layer_->set_fixed_tile_size(tile_size);
4166 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4168 std::vector<PictureLayerTiling*> tilings;
4169 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4170 tilings.push_back(pending_layer_->AddTiling(0.3f));
4171 tilings.push_back(pending_layer_->AddTiling(0.7f));
4172 tilings.push_back(pending_layer_->AddTiling(1.0f));
4173 tilings.push_back(pending_layer_->AddTiling(2.0f));
4175 EXPECT_EQ(5u, pending_layer_->num_tilings());
4176 EXPECT_EQ(5u, active_layer_->num_tilings());
4178 // Partially occlude the pending layer.
4179 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4180 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4181 pending_occluding_layer->SetBounds(layer_bounds);
4182 pending_occluding_layer->SetContentBounds(layer_bounds);
4183 pending_occluding_layer->SetDrawsContent(true);
4184 pending_occluding_layer->SetContentsOpaque(true);
4185 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4187 // Partially occlude the active layer.
4188 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4189 LayerImpl* active_occluding_layer = active_layer_->children()[0];
4190 active_occluding_layer->SetBounds(layer_bounds);
4191 active_occluding_layer->SetContentBounds(layer_bounds);
4192 active_occluding_layer->SetDrawsContent(true);
4193 active_occluding_layer->SetContentsOpaque(true);
4194 active_occluding_layer->SetPosition(active_occluding_layer_position);
4196 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4197 // are not shared between trees.
4198 pending_layer_->set_invalidation(invalidation_rect);
4200 host_impl_.SetViewportSize(viewport_size);
4201 host_impl_.active_tree()->UpdateDrawProperties();
4202 host_impl_.pending_tree()->UpdateDrawProperties();
4204 // The expected number of occluded tiles on each of the 5 tilings for each of
4205 // the 3 tree priorities.
4206 size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
4207 size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
4208 size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
4210 // The total expected number of occluded tiles on all tilings for each of the
4211 // 3 tree priorities.
4212 size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
4214 ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
4216 // Verify number of occluded tiles on the pending layer for each tiling.
4217 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4218 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4219 tiling->CreateAllTilesForTesting();
4221 size_t occluded_tile_count_on_pending = 0u;
4222 size_t occluded_tile_count_on_active = 0u;
4223 size_t occluded_tile_count_on_both = 0u;
4224 for (PictureLayerTiling::CoverageIterator iter(
4226 pending_layer_->contents_scale_x(),
4227 gfx::Rect(layer_bounds));
4232 if (tile->is_occluded(PENDING_TREE))
4233 occluded_tile_count_on_pending++;
4234 if (tile->is_occluded(ACTIVE_TREE))
4235 occluded_tile_count_on_active++;
4236 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4237 occluded_tile_count_on_both++;
4239 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4240 occluded_tile_count_on_pending)
4242 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4243 occluded_tile_count_on_active)
4245 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4246 occluded_tile_count_on_both)
4250 // Verify number of occluded tiles on the active layer for each tiling.
4251 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4252 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4253 tiling->CreateAllTilesForTesting();
4255 size_t occluded_tile_count_on_pending = 0u;
4256 size_t occluded_tile_count_on_active = 0u;
4257 size_t occluded_tile_count_on_both = 0u;
4258 for (PictureLayerTiling::CoverageIterator iter(
4260 pending_layer_->contents_scale_x(),
4261 gfx::Rect(layer_bounds));
4266 if (tile->is_occluded(PENDING_TREE))
4267 occluded_tile_count_on_pending++;
4268 if (tile->is_occluded(ACTIVE_TREE))
4269 occluded_tile_count_on_active++;
4270 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4271 occluded_tile_count_on_both++;
4273 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4274 occluded_tile_count_on_pending)
4276 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4277 occluded_tile_count_on_active)
4279 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4280 occluded_tile_count_on_both)
4284 std::vector<Tile*> all_tiles;
4285 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4287 tiling_iterator != tilings.end();
4288 ++tiling_iterator) {
4289 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4290 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4293 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4295 VerifyEvictionConsidersOcclusion(pending_layer_,
4296 total_expected_occluded_tile_count);
4297 VerifyEvictionConsidersOcclusion(active_layer_,
4298 total_expected_occluded_tile_count);
4301 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4302 gfx::Size tile_size(102, 102);
4303 gfx::Size layer_bounds(1000, 1000);
4305 scoped_refptr<FakePicturePileImpl> pile =
4306 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4307 SetupPendingTree(pile);
4308 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4311 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4312 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4314 SetupPendingTree(pile);
4315 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4316 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4319 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4320 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4322 host_impl_.ResetRecycleTreeForTesting();
4323 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4326 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4327 base::TimeTicks time_ticks;
4328 time_ticks += base::TimeDelta::FromMilliseconds(1);
4329 host_impl_.SetCurrentBeginFrameArgs(
4330 CreateBeginFrameArgsForTesting(time_ticks));
4332 gfx::Size tile_size(100, 100);
4333 gfx::Size layer_bounds(200, 200);
4334 gfx::Rect layer_rect(layer_bounds);
4336 FakeContentLayerClient client;
4337 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4338 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4339 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4340 host->SetRootLayer(layer);
4341 PicturePile* pile = layer->GetPicturePileForTesting();
4343 host_impl_.SetViewportSize(layer_bounds);
4345 int frame_number = 0;
4346 FakeRenderingStatsInstrumentation stats_instrumentation;
4348 client.set_fill_with_nonsolid_color(!test_for_solid);
4350 Region invalidation(layer_rect);
4351 pile->UpdateAndExpandInvalidation(&client,
4359 Picture::RECORD_NORMALLY,
4360 &stats_instrumentation);
4362 scoped_refptr<PicturePileImpl> pending_pile =
4363 PicturePileImpl::CreateFromOther(pile);
4365 SetupPendingTree(pending_pile);
4368 active_layer_->set_fixed_tile_size(tile_size);
4369 host_impl_.active_tree()->UpdateDrawProperties();
4370 if (test_for_solid) {
4371 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4373 ASSERT_TRUE(active_layer_->tilings());
4374 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4375 std::vector<Tile*> tiles =
4376 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4377 EXPECT_FALSE(tiles.empty());
4378 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4381 MockOcclusionTracker<LayerImpl> occlusion_tracker;
4382 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4383 AppendQuadsData data;
4384 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
4385 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
4386 active_layer_->DidDraw(NULL);
4388 DrawQuad::Material expected = test_for_solid
4389 ? DrawQuad::Material::SOLID_COLOR
4390 : DrawQuad::Material::TILED_CONTENT;
4391 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4394 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4395 TestQuadsForSolidColor(true);
4398 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4399 TestQuadsForSolidColor(false);
4402 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4403 base::TimeTicks time_ticks;
4404 time_ticks += base::TimeDelta::FromMilliseconds(1);
4405 host_impl_.SetCurrentBeginFrameArgs(
4406 CreateBeginFrameArgsForTesting(time_ticks));
4408 gfx::Size tile_size(100, 100);
4409 gfx::Size layer_bounds(200, 200);
4410 gfx::Rect layer_rect(layer_bounds);
4412 FakeContentLayerClient client;
4413 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4414 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4415 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4416 host->SetRootLayer(layer);
4417 PicturePile* pile = layer->GetPicturePileForTesting();
4419 host_impl_.SetViewportSize(layer_bounds);
4421 int frame_number = 0;
4422 FakeRenderingStatsInstrumentation stats_instrumentation;
4424 client.set_fill_with_nonsolid_color(true);
4426 Region invalidation1(layer_rect);
4427 pile->UpdateAndExpandInvalidation(&client,
4435 Picture::RECORD_NORMALLY,
4436 &stats_instrumentation);
4438 scoped_refptr<PicturePileImpl> pending_pile1 =
4439 PicturePileImpl::CreateFromOther(pile);
4441 SetupPendingTree(pending_pile1);
4443 host_impl_.active_tree()->UpdateDrawProperties();
4445 // We've started with a solid layer that contains some tilings.
4446 ASSERT_TRUE(active_layer_->tilings());
4447 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4449 client.set_fill_with_nonsolid_color(false);
4451 Region invalidation2(layer_rect);
4452 pile->UpdateAndExpandInvalidation(&client,
4460 Picture::RECORD_NORMALLY,
4461 &stats_instrumentation);
4463 scoped_refptr<PicturePileImpl> pending_pile2 =
4464 PicturePileImpl::CreateFromOther(pile);
4466 SetupPendingTree(pending_pile2);
4469 // We've switched to a solid color, so we should end up with no tilings.
4470 ASSERT_TRUE(active_layer_->tilings());
4471 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());