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 layer->UpdateTiles(Occlusion());
170 static void VerifyAllTilesExistAndHavePile(
171 const PictureLayerTiling* tiling,
172 PicturePileImpl* pile) {
173 for (PictureLayerTiling::CoverageIterator iter(
175 tiling->contents_scale(),
176 gfx::Rect(tiling->tiling_size()));
180 EXPECT_EQ(pile, iter->picture_pile());
184 void SetContentsScaleOnBothLayers(float contents_scale,
185 float device_scale_factor,
186 float page_scale_factor,
187 float maximum_animation_contents_scale,
188 bool animating_transform) {
189 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
193 maximum_animation_contents_scale,
194 animating_transform);
196 SetupDrawPropertiesAndUpdateTiles(active_layer_,
200 maximum_animation_contents_scale,
201 animating_transform);
204 void ResetTilingsAndRasterScales() {
205 pending_layer_->ReleaseResources();
206 active_layer_->ReleaseResources();
209 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
210 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
211 for (size_t i = 0; i < tiles.size(); ++i)
212 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
213 EXPECT_GT(tiles.size(), 0u);
216 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
217 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
218 for (size_t i = 0; i < tiles.size(); ++i)
219 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
220 EXPECT_GT(tiles.size(), 0u);
224 void TestTileGridAlignmentCommon() {
225 // Layer to span 4 raster tiles in x and in y
226 ImplSidePaintingSettings settings;
227 gfx::Size layer_size(
228 settings.default_tile_size.width() * 7 / 2,
229 settings.default_tile_size.height() * 7 / 2);
231 scoped_refptr<FakePicturePileImpl> pending_pile =
232 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
233 scoped_refptr<FakePicturePileImpl> active_pile =
234 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
236 SetupTrees(pending_pile, active_pile);
238 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
240 // Add 1x1 rects at the centers of each tile, then re-record pile contents
241 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
242 std::vector<Tile*> tiles =
243 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
244 EXPECT_EQ(16u, tiles.size());
245 std::vector<SkRect> rects;
246 std::vector<Tile*>::const_iterator tile_iter;
247 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
248 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
249 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
250 active_pile->add_draw_rect(rect);
251 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
253 // Force re-record with newly injected content
254 active_pile->RemoveRecordingAt(0, 0);
255 active_pile->AddRecordingAt(0, 0);
257 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
258 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
259 MockCanvas mock_canvas(1000, 1000);
260 active_pile->RasterDirect(
261 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
263 // This test verifies that when drawing the contents of a specific tile
264 // at content scale 1.0, the playback canvas never receives content from
265 // neighboring tiles which indicates that the tile grid embedded in
266 // SkPicture is perfectly aligned with the compositor's tiles.
267 EXPECT_EQ(1u, mock_canvas.rects_.size());
268 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
273 void TestQuadsForSolidColor(bool test_for_solid);
275 FakeImplProxy proxy_;
276 TestSharedBitmapManager shared_bitmap_manager_;
277 FakeLayerTreeHostImpl host_impl_;
279 FakePictureLayerImpl* pending_layer_;
280 FakePictureLayerImpl* old_pending_layer_;
281 FakePictureLayerImpl* active_layer_;
284 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
287 TEST_F(PictureLayerImplTest, TileGridAlignment) {
288 host_impl_.SetDeviceScaleFactor(1.f);
289 TestTileGridAlignmentCommon();
292 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
293 host_impl_.SetDeviceScaleFactor(2.f);
294 TestTileGridAlignmentCommon();
297 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
298 gfx::Size tile_size(100, 100);
299 gfx::Size layer_bounds(400, 400);
301 scoped_refptr<FakePicturePileImpl> pending_pile =
302 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
303 scoped_refptr<FakePicturePileImpl> active_pile =
304 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
306 SetupTrees(pending_pile, active_pile);
309 AddDefaultTilingsWithInvalidation(invalidation);
311 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
312 active_layer_->tilings()->num_tilings());
314 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
315 EXPECT_GT(tilings->num_tilings(), 0u);
316 for (size_t i = 0; i < tilings->num_tilings(); ++i)
317 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
320 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
321 base::TimeTicks time_ticks;
322 time_ticks += base::TimeDelta::FromMilliseconds(1);
323 host_impl_.SetCurrentBeginFrameArgs(
324 CreateBeginFrameArgsForTesting(time_ticks));
325 gfx::Size tile_size(100, 100);
326 gfx::Size layer_bounds(400, 400);
328 scoped_refptr<FakePicturePileImpl> pending_pile =
329 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
330 scoped_refptr<FakePicturePileImpl> active_pile =
331 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
333 SetupTrees(pending_pile, active_pile);
336 AddDefaultTilingsWithInvalidation(invalidation);
337 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
339 time_ticks += base::TimeDelta::FromMilliseconds(200);
340 host_impl_.SetCurrentBeginFrameArgs(
341 CreateBeginFrameArgsForTesting(time_ticks));
343 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
344 // identify transform for tile priority.
345 bool resourceless_software_draw = false;
346 gfx::Rect viewport = gfx::Rect(layer_bounds),
347 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
348 gfx::Transform transform, transform_for_tile_priority;
350 host_impl_.SetExternalDrawConstraints(transform,
353 viewport_rect_for_tile_priority,
354 transform_for_tile_priority,
355 resourceless_software_draw);
356 active_layer_->draw_properties().visible_content_rect = viewport;
357 active_layer_->draw_properties().screen_space_transform = transform;
358 active_layer_->UpdateTiles(Occlusion());
360 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
361 viewport_rect_for_tile_priority;
363 // Verify the viewport rect for tile priority is used in picture layer impl.
364 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
365 viewport_rect_for_tile_priority_in_view_space);
367 // Verify the viewport rect for tile priority is used in picture layer tiling.
368 PictureLayerTilingSet* tilings = active_layer_->tilings();
369 for (size_t i = 0; i < tilings->num_tilings(); i++) {
370 PictureLayerTiling* tiling = tilings->tiling_at(i);
372 tiling->GetCurrentVisibleRectForTesting(),
373 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
374 tiling->contents_scale()));
377 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
378 // screen space and the transform for tile priority is translated and
379 // rotated. The actual viewport for tile priority used by PictureLayerImpl
380 // should be (200, 200, 100, 100) applied with the said transform.
381 time_ticks += base::TimeDelta::FromMilliseconds(200);
382 host_impl_.SetCurrentBeginFrameArgs(
383 CreateBeginFrameArgsForTesting(time_ticks));
385 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
386 transform_for_tile_priority.Translate(100, 100);
387 transform_for_tile_priority.Rotate(45);
388 host_impl_.SetExternalDrawConstraints(transform,
391 viewport_rect_for_tile_priority,
392 transform_for_tile_priority,
393 resourceless_software_draw);
394 active_layer_->draw_properties().visible_content_rect = viewport;
395 active_layer_->draw_properties().screen_space_transform = transform;
396 active_layer_->UpdateTiles(Occlusion());
398 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
399 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
400 EXPECT_TRUE(success);
402 // Note that we don't clip this to the layer bounds, since it is expected that
403 // the rect will sometimes be outside of the layer bounds. If we clip to
404 // bounds, then tile priorities will end up being incorrect in cases of fully
406 viewport_rect_for_tile_priority_in_view_space =
407 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
408 screen_to_view, viewport_rect_for_tile_priority));
410 // Verify the viewport rect for tile priority is used in PictureLayerImpl.
411 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
412 viewport_rect_for_tile_priority_in_view_space);
414 tilings = active_layer_->tilings();
415 for (size_t i = 0; i < tilings->num_tilings(); i++) {
416 PictureLayerTiling* tiling = tilings->tiling_at(i);
418 tiling->GetCurrentVisibleRectForTesting(),
419 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
420 tiling->contents_scale()));
424 TEST_F(PictureLayerImplTest,
425 ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
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 bool resourceless_software_draw = false;
447 gfx::Rect viewport = gfx::Rect(layer_bounds);
448 gfx::Transform transform;
449 host_impl_.SetExternalDrawConstraints(transform,
454 resourceless_software_draw);
455 active_layer_->draw_properties().visible_content_rect = viewport;
456 active_layer_->draw_properties().screen_space_transform = transform;
457 active_layer_->UpdateTiles(Occlusion());
459 gfx::Rect visible_rect_for_tile_priority =
460 active_layer_->visible_rect_for_tile_priority();
461 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
462 gfx::Rect viewport_rect_for_tile_priority =
463 active_layer_->viewport_rect_for_tile_priority();
464 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
465 gfx::Transform screen_space_transform_for_tile_priority =
466 active_layer_->screen_space_transform_for_tile_priority();
468 // PictureLayerImpl does not make a special case for
469 // resource_less_software_draw, so the tile viewport and matrix should be
471 time_ticks += base::TimeDelta::FromMilliseconds(200);
472 host_impl_.SetCurrentBeginFrameArgs(
473 CreateBeginFrameArgsForTesting(time_ticks));
474 resourceless_software_draw = true;
475 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
476 transform.Translate(1.f, 1.f);
477 active_layer_->draw_properties().visible_content_rect = viewport;
478 active_layer_->draw_properties().screen_space_transform = transform;
479 host_impl_.SetExternalDrawConstraints(transform,
484 resourceless_software_draw);
485 active_layer_->UpdateTiles(Occlusion());
487 visible_rect_for_tile_priority =
488 gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
489 viewport_rect_for_tile_priority =
490 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
491 screen_space_transform_for_tile_priority = transform;
492 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
493 active_layer_->visible_rect_for_tile_priority());
494 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
495 active_layer_->viewport_rect_for_tile_priority());
496 EXPECT_TRANSFORMATION_MATRIX_EQ(
497 screen_space_transform_for_tile_priority,
498 active_layer_->screen_space_transform_for_tile_priority());
501 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
502 gfx::Size tile_size(100, 100);
503 gfx::Size layer_bounds(400, 400);
504 gfx::Rect layer_invalidation(150, 200, 30, 180);
506 scoped_refptr<FakePicturePileImpl> pending_pile =
507 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
508 scoped_refptr<FakePicturePileImpl> active_pile =
509 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
511 SetupTrees(pending_pile, active_pile);
513 Region invalidation(layer_invalidation);
514 AddDefaultTilingsWithInvalidation(invalidation);
516 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
517 EXPECT_GT(tilings->num_tilings(), 0u);
518 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
519 const PictureLayerTiling* tiling = tilings->tiling_at(i);
520 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
522 tiling->contents_scale());
523 for (PictureLayerTiling::CoverageIterator iter(
525 tiling->contents_scale(),
526 gfx::Rect(tiling->tiling_size()));
530 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
531 if (iter.geometry_rect().Intersects(content_invalidation))
532 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
534 EXPECT_EQ(active_pile.get(), iter->picture_pile());
539 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
540 gfx::Size tile_size(90, 80);
541 gfx::Size layer_bounds(300, 500);
543 scoped_refptr<FakePicturePileImpl> pending_pile =
544 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
545 scoped_refptr<FakePicturePileImpl> active_pile =
546 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
548 SetupTrees(pending_pile, active_pile);
550 Region invalidation((gfx::Rect(layer_bounds)));
551 AddDefaultTilingsWithInvalidation(invalidation);
553 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
554 active_layer_->tilings()->num_tilings());
556 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
557 EXPECT_GT(tilings->num_tilings(), 0u);
558 for (size_t i = 0; i < tilings->num_tilings(); ++i)
559 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
562 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
563 gfx::Size tile_size(90, 80);
564 gfx::Size active_layer_bounds(300, 500);
565 gfx::Size pending_layer_bounds(400, 800);
567 scoped_refptr<FakePicturePileImpl> pending_pile =
568 FakePicturePileImpl::CreateFilledPile(tile_size,
569 pending_layer_bounds);
570 scoped_refptr<FakePicturePileImpl> active_pile =
571 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
573 SetupTrees(pending_pile, active_pile);
574 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
577 AddDefaultTilingsWithInvalidation(invalidation);
579 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
580 EXPECT_GT(tilings->num_tilings(), 0u);
581 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
582 const PictureLayerTiling* tiling = tilings->tiling_at(i);
583 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
584 gfx::Rect(active_layer_bounds),
585 tiling->contents_scale());
586 for (PictureLayerTiling::CoverageIterator iter(
588 tiling->contents_scale(),
589 gfx::Rect(tiling->tiling_size()));
593 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
594 std::vector<Tile*> active_tiles =
595 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
596 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
597 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
598 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
599 active_tiles[0]->content_rect().size() !=
600 pending_tiles[0]->content_rect().size()) {
601 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
603 EXPECT_EQ(active_pile.get(), iter->picture_pile());
609 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
610 gfx::Size tile_size(400, 400);
611 gfx::Size layer_bounds(1300, 1900);
613 scoped_refptr<FakePicturePileImpl> pending_pile =
614 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
615 scoped_refptr<FakePicturePileImpl> active_pile =
616 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
618 // Fill in some of active pile, but more of pending pile.
620 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
621 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
623 pending_pile->AddRecordingAt(x, y);
624 active_pile->AddRecordingAt(x, y);
628 pending_pile->AddRecordingAt(x, y);
633 SetupTrees(pending_pile, active_pile);
635 AddDefaultTilingsWithInvalidation(invalidation);
637 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
638 EXPECT_GT(tilings->num_tilings(), 0u);
639 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
640 const PictureLayerTiling* tiling = tilings->tiling_at(i);
642 for (PictureLayerTiling::CoverageIterator iter(
644 tiling->contents_scale(),
645 gfx::Rect(tiling->tiling_size()));
648 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
649 // Ensure there is a recording for this tile.
650 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
651 iter.full_tile_geometry_rect());
652 bool in_active = active_pile->CanRaster(tiling->contents_scale(),
653 iter.full_tile_geometry_rect());
655 if (in_pending && !in_active)
656 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
658 EXPECT_EQ(active_pile.get(), iter->picture_pile());
665 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
666 gfx::Size tile_size(400, 400);
667 gfx::Size layer_bounds(1300, 1900);
669 scoped_refptr<FakePicturePileImpl> pending_pile =
670 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
671 scoped_refptr<FakePicturePileImpl> active_pile =
672 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
674 SetupTrees(pending_pile, active_pile);
676 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
678 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
681 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
682 gfx::Size tile_size(400, 400);
683 gfx::Size layer_bounds(1300, 1900);
685 scoped_refptr<FakePicturePileImpl> pending_pile =
686 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
687 scoped_refptr<FakePicturePileImpl> active_pile =
688 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
690 SetupTrees(pending_pile, active_pile);
691 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
693 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
694 EXPECT_LT(low_res_factor, 1.f);
696 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
697 6.f, // ideal contents scale
700 1.f, // maximum animation scale
702 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
704 pending_layer_->tilings()->tiling_at(0)->contents_scale());
705 EXPECT_FLOAT_EQ(6.f * low_res_factor,
706 pending_layer_->tilings()->tiling_at(1)->contents_scale());
708 // If we change the page scale factor, then we should get new tilings.
709 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
710 6.6f, // ideal contents scale
713 1.f, // maximum animation scale
715 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
716 EXPECT_FLOAT_EQ(6.6f,
717 pending_layer_->tilings()->tiling_at(0)->contents_scale());
718 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
719 pending_layer_->tilings()->tiling_at(2)->contents_scale());
721 // If we change the device scale factor, then we should get new tilings.
722 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
723 7.26f, // ideal contents scale
724 3.3f, // device scale
726 1.f, // maximum animation scale
728 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
729 EXPECT_FLOAT_EQ(7.26f,
730 pending_layer_->tilings()->tiling_at(0)->contents_scale());
731 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
732 pending_layer_->tilings()->tiling_at(3)->contents_scale());
734 // If we change the device scale factor, but end up at the same total scale
735 // factor somehow, then we don't get new tilings.
736 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
737 7.26f, // ideal contents scale
738 2.2f, // device scale
740 1.f, // maximum animation scale
742 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
743 EXPECT_FLOAT_EQ(7.26f,
744 pending_layer_->tilings()->tiling_at(0)->contents_scale());
745 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
746 pending_layer_->tilings()->tiling_at(3)->contents_scale());
749 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
750 // This test makes sure that if a layer can have tilings, then a commit makes
751 // it not able to have tilings (empty size), and then a future commit that
752 // makes it valid again should be able to create tilings.
753 gfx::Size tile_size(400, 400);
754 gfx::Size layer_bounds(1300, 1900);
756 scoped_refptr<FakePicturePileImpl> empty_pile =
757 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
758 scoped_refptr<FakePicturePileImpl> valid_pile =
759 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
761 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
762 EXPECT_LT(low_res_factor, 1.f);
764 float high_res_scale = 1.3f;
765 float low_res_scale = high_res_scale * low_res_factor;
766 float device_scale = 1.7f;
767 float page_scale = 3.2f;
768 float maximum_animation_scale = 1.f;
770 SetupPendingTree(valid_pile);
771 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
775 maximum_animation_scale,
777 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
778 EXPECT_FLOAT_EQ(high_res_scale,
779 pending_layer_->HighResTiling()->contents_scale());
780 EXPECT_FLOAT_EQ(low_res_scale,
781 pending_layer_->LowResTiling()->contents_scale());
784 SetupPendingTree(empty_pile);
785 EXPECT_FALSE(pending_layer_->CanHaveTilings());
786 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
790 maximum_animation_scale,
792 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
793 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
796 EXPECT_FALSE(active_layer_->CanHaveTilings());
797 SetupDrawPropertiesAndUpdateTiles(active_layer_,
801 maximum_animation_scale,
803 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
805 SetupPendingTree(valid_pile);
806 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
810 maximum_animation_scale,
812 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
813 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
814 EXPECT_FLOAT_EQ(high_res_scale,
815 pending_layer_->HighResTiling()->contents_scale());
816 EXPECT_FLOAT_EQ(low_res_scale,
817 pending_layer_->LowResTiling()->contents_scale());
820 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
821 gfx::Size tile_size(400, 400);
822 gfx::Size layer_bounds(1300, 1900);
824 // Set up the high and low res tilings before pinch zoom.
825 scoped_refptr<FakePicturePileImpl> pending_pile =
826 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
827 scoped_refptr<FakePicturePileImpl> active_pile =
828 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
830 SetupTrees(pending_pile, active_pile);
831 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
832 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
833 host_impl_.PinchGestureBegin();
834 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
835 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
836 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
839 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
840 gfx::Size tile_size(400, 400);
841 gfx::Size layer_bounds(1300, 1900);
843 scoped_refptr<FakePicturePileImpl> pending_pile =
844 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
845 scoped_refptr<FakePicturePileImpl> active_pile =
846 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
848 // Set up the high and low res tilings before pinch zoom.
849 SetupTrees(pending_pile, active_pile);
850 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
851 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
852 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
853 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
854 EXPECT_FLOAT_EQ(2.0f,
855 active_layer_->tilings()->tiling_at(0)->contents_scale());
856 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
857 active_layer_->tilings()->tiling_at(1)->contents_scale());
859 // Start a pinch gesture.
860 host_impl_.PinchGestureBegin();
862 // Zoom out by a small amount. We should create a tiling at half
863 // the scale (2/kMaxScaleRatioDuringPinch).
864 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
865 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
866 EXPECT_FLOAT_EQ(2.0f,
867 active_layer_->tilings()->tiling_at(0)->contents_scale());
868 EXPECT_FLOAT_EQ(1.0f,
869 active_layer_->tilings()->tiling_at(1)->contents_scale());
870 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
871 active_layer_->tilings()->tiling_at(2)->contents_scale());
873 // Zoom out further, close to our low-res scale factor. We should
874 // use that tiling as high-res, and not create a new tiling.
875 SetContentsScaleOnBothLayers(
876 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
877 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
879 // Zoom in a lot now. Since we increase by increments of
880 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
881 // and then finally create a new tiling at 4.0.
882 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
883 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
884 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
885 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
886 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
887 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
888 EXPECT_FLOAT_EQ(4.0f,
889 active_layer_->tilings()->tiling_at(0)->contents_scale());
892 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
893 gfx::Size tile_size(300, 300);
894 gfx::Size layer_bounds(2600, 3800);
896 scoped_refptr<FakePicturePileImpl> pending_pile =
897 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
898 scoped_refptr<FakePicturePileImpl> active_pile =
899 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
901 // Set up the high and low res tilings before pinch zoom.
902 SetupTrees(pending_pile, active_pile);
903 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
904 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
905 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
906 EXPECT_FLOAT_EQ(0.24f,
907 active_layer_->tilings()->tiling_at(0)->contents_scale());
908 EXPECT_FLOAT_EQ(0.0625f,
909 active_layer_->tilings()->tiling_at(1)->contents_scale());
911 // Start a pinch gesture.
912 host_impl_.PinchGestureBegin();
914 // Zoom out by a small amount. We should create a tiling at half
915 // the scale (1/kMaxScaleRatioDuringPinch).
916 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
917 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
918 EXPECT_FLOAT_EQ(0.24f,
919 active_layer_->tilings()->tiling_at(0)->contents_scale());
920 EXPECT_FLOAT_EQ(0.12f,
921 active_layer_->tilings()->tiling_at(1)->contents_scale());
922 EXPECT_FLOAT_EQ(0.0625,
923 active_layer_->tilings()->tiling_at(2)->contents_scale());
925 // Zoom out further, close to our low-res scale factor. We should
926 // use that tiling as high-res, and not create a new tiling.
927 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
928 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
930 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
931 // because 0.125(desired_scale) is within the ratio(1.2)
932 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
933 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
936 TEST_F(PictureLayerImplTest, CleanUpTilings) {
937 gfx::Size tile_size(400, 400);
938 gfx::Size layer_bounds(1300, 1900);
940 scoped_refptr<FakePicturePileImpl> pending_pile =
941 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
942 scoped_refptr<FakePicturePileImpl> active_pile =
943 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
945 std::vector<PictureLayerTiling*> used_tilings;
947 SetupTrees(pending_pile, active_pile);
948 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
950 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
951 EXPECT_LT(low_res_factor, 1.f);
953 float device_scale = 1.7f;
954 float page_scale = 3.2f;
957 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
958 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
960 // We only have ideal tilings, so they aren't removed.
961 used_tilings.clear();
962 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
963 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
965 host_impl_.PinchGestureBegin();
967 // Changing the ideal but not creating new tilings.
970 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
971 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
973 // The tilings are still our target scale, so they aren't removed.
974 used_tilings.clear();
975 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
976 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
978 host_impl_.PinchGestureEnd();
980 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
983 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
984 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
987 active_layer_->tilings()->tiling_at(1)->contents_scale());
989 1.f * low_res_factor,
990 active_layer_->tilings()->tiling_at(3)->contents_scale());
992 // Mark the non-ideal tilings as used. They won't be removed.
993 used_tilings.clear();
994 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
995 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
996 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
997 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
999 // Now move the ideal scale to 0.5. Our target stays 1.2.
1000 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
1002 // The high resolution tiling is between target and ideal, so is not
1003 // removed. The low res tiling for the old ideal=1.0 scale is removed.
1004 used_tilings.clear();
1005 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1006 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1008 // Now move the ideal scale to 1.0. Our target stays 1.2.
1009 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1011 // All the tilings are between are target and the ideal, so they are not
1013 used_tilings.clear();
1014 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1015 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1017 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1018 SetupDrawPropertiesAndUpdateTiles(
1019 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1021 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1022 // in the range [1.0,1.2] and are kept.
1023 used_tilings.clear();
1024 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1025 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1027 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1029 SetupDrawPropertiesAndUpdateTiles(
1030 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1032 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1033 // target raster scale. But it is in our used tilings set, so nothing is
1035 used_tilings.clear();
1036 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1037 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1038 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1040 // If we remove it from our used tilings set, it is outside the range to keep
1041 // so it is deleted.
1042 used_tilings.clear();
1043 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1044 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1047 #define EXPECT_BOTH_EQ(expression, x) \
1049 EXPECT_EQ(x, pending_layer_->expression); \
1050 EXPECT_EQ(x, active_layer_->expression); \
1053 #define EXPECT_BOTH_NE(expression, x) \
1055 EXPECT_NE(x, pending_layer_->expression); \
1056 EXPECT_NE(x, active_layer_->expression); \
1059 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1060 // Make sure this layer covers multiple tiles, since otherwise low
1061 // res won't get created because it is too small.
1062 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1063 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1064 // Avoid max untiled layer size heuristics via fixed tile size.
1065 pending_layer_->set_fixed_tile_size(tile_size);
1066 active_layer_->set_fixed_tile_size(tile_size);
1068 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1069 float contents_scale = 1.f;
1070 float device_scale = 1.f;
1071 float page_scale = 1.f;
1072 float maximum_animation_scale = 1.f;
1073 bool animating_transform = true;
1075 // Animating, so don't create low res even if there isn't one already.
1076 SetContentsScaleOnBothLayers(contents_scale,
1079 maximum_animation_scale,
1080 animating_transform);
1081 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1082 EXPECT_BOTH_EQ(num_tilings(), 1u);
1084 // Stop animating, low res gets created.
1085 animating_transform = false;
1086 SetContentsScaleOnBothLayers(contents_scale,
1089 maximum_animation_scale,
1090 animating_transform);
1091 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1092 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1093 EXPECT_BOTH_EQ(num_tilings(), 2u);
1095 // Page scale animation, new high res, but not new low res because animating.
1096 contents_scale = 2.f;
1098 animating_transform = true;
1099 SetContentsScaleOnBothLayers(contents_scale,
1102 maximum_animation_scale,
1103 animating_transform);
1104 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1105 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1106 EXPECT_BOTH_EQ(num_tilings(), 3u);
1108 // Stop animating, new low res gets created for final page scale.
1109 animating_transform = false;
1110 SetContentsScaleOnBothLayers(contents_scale,
1113 maximum_animation_scale,
1114 animating_transform);
1115 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1116 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1117 EXPECT_BOTH_EQ(num_tilings(), 4u);
1120 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1121 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1122 SetupDefaultTrees(tile_size);
1124 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1125 float device_scale = 1.f;
1126 float page_scale = 1.f;
1127 float maximum_animation_scale = 1.f;
1128 bool animating_transform = false;
1130 // Contents exactly fit on one tile at scale 1, no low res.
1131 float contents_scale = 1.f;
1132 SetContentsScaleOnBothLayers(contents_scale,
1135 maximum_animation_scale,
1136 animating_transform);
1137 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1138 EXPECT_BOTH_EQ(num_tilings(), 1u);
1140 ResetTilingsAndRasterScales();
1142 // Contents that are smaller than one tile, no low res.
1143 contents_scale = 0.123f;
1144 SetContentsScaleOnBothLayers(contents_scale,
1147 maximum_animation_scale,
1148 animating_transform);
1149 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1150 EXPECT_BOTH_EQ(num_tilings(), 1u);
1152 ResetTilingsAndRasterScales();
1154 // Any content bounds that would create more than one tile will
1155 // generate a low res tiling.
1156 contents_scale = 2.5f;
1157 SetContentsScaleOnBothLayers(contents_scale,
1160 maximum_animation_scale,
1161 animating_transform);
1162 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1163 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1164 contents_scale * low_res_factor);
1165 EXPECT_BOTH_EQ(num_tilings(), 2u);
1167 ResetTilingsAndRasterScales();
1169 // Mask layers dont create low res since they always fit on one tile.
1170 pending_layer_->pile()->set_is_mask(true);
1171 active_layer_->pile()->set_is_mask(true);
1172 SetContentsScaleOnBothLayers(contents_scale,
1175 maximum_animation_scale,
1176 animating_transform);
1177 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1178 EXPECT_BOTH_EQ(num_tilings(), 1u);
1181 TEST_F(PictureLayerImplTest, HugeMasksDontGetTiles) {
1182 gfx::Size tile_size(100, 100);
1184 scoped_refptr<FakePicturePileImpl> valid_pile =
1185 FakePicturePileImpl::CreateFilledPile(tile_size, gfx::Size(1000, 1000));
1186 valid_pile->set_is_mask(true);
1187 SetupPendingTree(valid_pile);
1189 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1190 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1191 EXPECT_EQ(1u, pending_layer_->num_tilings());
1193 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1194 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1195 pending_layer_->HighResTiling()->AllTilesForTesting());
1199 // Mask layers have a tiling with a single tile in it.
1200 EXPECT_EQ(1u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1201 // The mask resource exists.
1202 EXPECT_NE(0u, active_layer_->ContentsResourceId());
1204 // Resize larger than the max texture size.
1205 int max_texture_size = host_impl_.GetRendererCapabilities().max_texture_size;
1206 scoped_refptr<FakePicturePileImpl> huge_pile =
1207 FakePicturePileImpl::CreateFilledPile(
1208 tile_size, gfx::Size(max_texture_size + 1, 10));
1209 huge_pile->set_is_mask(true);
1210 SetupPendingTree(huge_pile);
1212 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1213 EXPECT_EQ(1.f, pending_layer_->HighResTiling()->contents_scale());
1214 EXPECT_EQ(1u, pending_layer_->num_tilings());
1216 pending_layer_->HighResTiling()->CreateAllTilesForTesting();
1217 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1218 pending_layer_->HighResTiling()->AllTilesForTesting());
1222 // Mask layers have a tiling, but there should be no tiles in it.
1223 EXPECT_EQ(0u, active_layer_->HighResTiling()->AllTilesForTesting().size());
1224 // The mask resource is empty.
1225 EXPECT_EQ(0u, active_layer_->ContentsResourceId());
1228 TEST_F(PictureLayerImplTest, ReleaseResources) {
1229 gfx::Size tile_size(400, 400);
1230 gfx::Size layer_bounds(1300, 1900);
1232 scoped_refptr<FakePicturePileImpl> pending_pile =
1233 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1234 scoped_refptr<FakePicturePileImpl> active_pile =
1235 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1237 SetupTrees(pending_pile, active_pile);
1238 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1240 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1241 1.3f, // ideal contents scale
1242 2.7f, // device scale
1244 1.f, // maximum animation scale
1246 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1248 // All tilings should be removed when losing output surface.
1249 active_layer_->ReleaseResources();
1250 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1251 pending_layer_->ReleaseResources();
1252 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1254 // This should create new tilings.
1255 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1256 1.3f, // ideal contents scale
1257 2.7f, // device scale
1259 1.f, // maximum animation scale
1261 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1264 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1265 // The default max tile size is larger than 400x400.
1266 gfx::Size tile_size(400, 400);
1267 gfx::Size layer_bounds(5000, 5000);
1269 scoped_refptr<FakePicturePileImpl> pending_pile =
1270 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1271 scoped_refptr<FakePicturePileImpl> active_pile =
1272 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1274 SetupTrees(pending_pile, active_pile);
1275 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1277 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1278 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1280 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1282 // The default value.
1283 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1284 host_impl_.settings().default_tile_size.ToString());
1286 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1287 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1288 tile->content_rect().size().ToString());
1290 pending_layer_->ReleaseResources();
1292 // Change the max texture size on the output surface context.
1293 scoped_ptr<TestWebGraphicsContext3D> context =
1294 TestWebGraphicsContext3D::Create();
1295 context->set_max_texture_size(140);
1296 host_impl_.DidLoseOutputSurface();
1297 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1298 context.Pass()).PassAs<OutputSurface>());
1300 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1301 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1303 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1305 // Verify the tiles are not larger than the context's max texture size.
1306 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1307 EXPECT_GE(140, tile->content_rect().width());
1308 EXPECT_GE(140, tile->content_rect().height());
1311 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1312 // The default max tile size is larger than 400x400.
1313 gfx::Size tile_size(400, 400);
1314 gfx::Size layer_bounds(500, 500);
1316 scoped_refptr<FakePicturePileImpl> pending_pile =
1317 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1318 scoped_refptr<FakePicturePileImpl> active_pile =
1319 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1321 SetupTrees(pending_pile, active_pile);
1322 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1324 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1325 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1327 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1329 // The default value. The layer is smaller than this.
1330 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1331 host_impl_.settings().max_untiled_layer_size.ToString());
1333 // There should be a single tile since the layer is small.
1334 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1335 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1337 pending_layer_->ReleaseResources();
1339 // Change the max texture size on the output surface context.
1340 scoped_ptr<TestWebGraphicsContext3D> context =
1341 TestWebGraphicsContext3D::Create();
1342 context->set_max_texture_size(140);
1343 host_impl_.DidLoseOutputSurface();
1344 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1345 context.Pass()).PassAs<OutputSurface>());
1347 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1348 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1350 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1352 // There should be more than one tile since the max texture size won't cover
1354 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1355 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1357 // Verify the tiles are not larger than the context's max texture size.
1358 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1359 EXPECT_GE(140, tile->content_rect().width());
1360 EXPECT_GE(140, tile->content_rect().height());
1363 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1364 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1365 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1367 gfx::Size tile_size(400, 400);
1368 gfx::Size layer_bounds(1300, 1900);
1370 scoped_refptr<FakePicturePileImpl> pending_pile =
1371 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1372 scoped_refptr<FakePicturePileImpl> active_pile =
1373 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1375 SetupTrees(pending_pile, active_pile);
1377 active_layer_->draw_properties().visible_content_rect =
1378 gfx::Rect(layer_bounds);
1380 gfx::Rect layer_invalidation(150, 200, 30, 180);
1381 Region invalidation(layer_invalidation);
1382 AddDefaultTilingsWithInvalidation(invalidation);
1384 AppendQuadsData data;
1385 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1386 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1387 active_layer_->DidDraw(NULL);
1389 ASSERT_EQ(1U, render_pass->quad_list.size());
1390 EXPECT_EQ(DrawQuad::PICTURE_CONTENT,
1391 render_pass->quad_list.front()->material);
1394 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1395 gfx::Size tile_size(100, 100);
1396 gfx::Size layer_bounds(1000, 1000);
1398 scoped_refptr<FakePicturePileImpl> pending_pile =
1399 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1400 // Layers with entirely empty piles can't get tilings.
1401 pending_pile->AddRecordingAt(0, 0);
1403 SetupPendingTree(pending_pile);
1405 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1406 pending_layer_->AddTiling(1.0f);
1407 pending_layer_->AddTiling(2.0f);
1409 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1410 // on a layer with no recordings.
1411 host_impl_.pending_tree()->UpdateDrawProperties();
1412 pending_layer_->MarkVisibleResourcesAsRequired();
1415 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1416 gfx::Size tile_size(100, 100);
1417 gfx::Size layer_bounds(200, 200);
1419 scoped_refptr<FakePicturePileImpl> pending_pile =
1420 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1421 SetupPendingTree(pending_pile);
1423 pending_layer_->set_fixed_tile_size(tile_size);
1424 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1425 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1426 host_impl_.pending_tree()->UpdateDrawProperties();
1427 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1429 pending_layer_->draw_properties().visible_content_rect =
1430 gfx::Rect(0, 0, 100, 200);
1432 // Fake set priorities.
1433 for (PictureLayerTiling::CoverageIterator iter(
1434 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1440 TilePriority priority;
1441 priority.resolution = HIGH_RESOLUTION;
1442 gfx::Rect tile_bounds = iter.geometry_rect();
1443 if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1444 priority.priority_bin = TilePriority::NOW;
1445 priority.distance_to_visible = 0.f;
1447 priority.priority_bin = TilePriority::SOON;
1448 priority.distance_to_visible = 1.f;
1450 tile->SetPriority(PENDING_TREE, priority);
1453 pending_layer_->MarkVisibleResourcesAsRequired();
1455 int num_visible = 0;
1456 int num_offscreen = 0;
1458 for (PictureLayerTiling::CoverageIterator iter(
1459 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1464 const Tile* tile = *iter;
1465 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1466 EXPECT_TRUE(tile->required_for_activation());
1469 EXPECT_FALSE(tile->required_for_activation());
1474 EXPECT_GT(num_visible, 0);
1475 EXPECT_GT(num_offscreen, 0);
1478 TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1479 base::TimeTicks time_ticks;
1480 time_ticks += base::TimeDelta::FromMilliseconds(1);
1481 host_impl_.SetCurrentBeginFrameArgs(
1482 CreateBeginFrameArgsForTesting(time_ticks));
1484 gfx::Size tile_size(100, 100);
1485 gfx::Size layer_bounds(400, 400);
1486 gfx::Rect external_viewport_for_tile_priority(400, 200);
1487 gfx::Rect visible_content_rect(200, 400);
1489 scoped_refptr<FakePicturePileImpl> active_pile =
1490 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1491 scoped_refptr<FakePicturePileImpl> pending_pile =
1492 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1493 SetupTrees(pending_pile, active_pile);
1495 active_layer_->set_fixed_tile_size(tile_size);
1496 pending_layer_->set_fixed_tile_size(tile_size);
1497 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1498 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1500 // Set external viewport for tile priority.
1501 gfx::Rect viewport = gfx::Rect(layer_bounds);
1502 gfx::Transform transform;
1503 gfx::Transform transform_for_tile_priority;
1504 bool resourceless_software_draw = false;
1505 host_impl_.SetExternalDrawConstraints(transform,
1508 external_viewport_for_tile_priority,
1509 transform_for_tile_priority,
1510 resourceless_software_draw);
1511 host_impl_.pending_tree()->UpdateDrawProperties();
1513 // Set visible content rect that is different from
1514 // external_viewport_for_tile_priority.
1515 pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1516 time_ticks += base::TimeDelta::FromMilliseconds(200);
1517 host_impl_.SetCurrentBeginFrameArgs(
1518 CreateBeginFrameArgsForTesting(time_ticks));
1519 pending_layer_->UpdateTiles(Occlusion());
1521 pending_layer_->MarkVisibleResourcesAsRequired();
1523 // Intersect the two rects. Any tile outside should not be required for
1525 gfx::Rect viewport_for_tile_priority =
1526 pending_layer_->GetViewportForTilePriorityInContentSpace();
1527 viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1530 int num_outside = 0;
1531 for (PictureLayerTiling::CoverageIterator iter(
1532 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1538 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1540 // Mark everything in viewport for tile priority as ready to draw.
1541 ManagedTileState::TileVersion& tile_version =
1542 tile->GetTileVersionForTesting(
1543 tile->DetermineRasterModeForTree(PENDING_TREE));
1544 tile_version.SetSolidColorForTesting(SK_ColorRED);
1547 EXPECT_FALSE(tile->required_for_activation());
1551 EXPECT_GT(num_inside, 0);
1552 EXPECT_GT(num_outside, 0);
1554 // Activate and draw active layer.
1555 host_impl_.ActivateSyncTree();
1556 host_impl_.active_tree()->UpdateDrawProperties();
1557 active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1559 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1560 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1561 AppendQuadsData data;
1562 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1563 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1564 active_layer_->DidDraw(NULL);
1566 // All tiles in activation rect is ready to draw.
1567 EXPECT_EQ(0u, data.num_missing_tiles);
1568 EXPECT_EQ(0u, data.num_incomplete_tiles);
1571 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1572 base::TimeTicks time_ticks;
1573 time_ticks += base::TimeDelta::FromMilliseconds(1);
1574 host_impl_.SetCurrentBeginFrameArgs(
1575 CreateBeginFrameArgsForTesting(time_ticks));
1577 gfx::Size tile_size(100, 100);
1578 gfx::Size layer_bounds(200, 200);
1580 host_impl_.SetViewportSize(layer_bounds);
1582 scoped_refptr<FakePicturePileImpl> pending_pile =
1583 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1584 SetupPendingTree(pending_pile);
1587 // All high res tiles have resources.
1588 active_layer_->set_fixed_tile_size(tile_size);
1589 host_impl_.active_tree()->UpdateDrawProperties();
1590 std::vector<Tile*> tiles =
1591 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1592 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1594 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1595 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1596 AppendQuadsData data;
1597 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1598 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1599 active_layer_->DidDraw(NULL);
1601 // All high res tiles drew, nothing was incomplete.
1602 EXPECT_EQ(9u, render_pass->quad_list.size());
1603 EXPECT_EQ(0u, data.num_missing_tiles);
1604 EXPECT_EQ(0u, data.num_incomplete_tiles);
1607 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1608 base::TimeTicks time_ticks;
1609 time_ticks += base::TimeDelta::FromMilliseconds(1);
1610 host_impl_.SetCurrentBeginFrameArgs(
1611 CreateBeginFrameArgsForTesting(time_ticks));
1613 gfx::Size tile_size(100, 100);
1614 gfx::Size layer_bounds(200, 200);
1616 host_impl_.SetViewportSize(layer_bounds);
1618 scoped_refptr<FakePicturePileImpl> pending_pile =
1619 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1620 SetupPendingTree(pending_pile);
1623 // All high res tiles have resources except one.
1624 active_layer_->set_fixed_tile_size(tile_size);
1625 host_impl_.active_tree()->UpdateDrawProperties();
1626 std::vector<Tile*> high_tiles =
1627 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1628 high_tiles.erase(high_tiles.begin());
1629 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1631 // All low res tiles have resources.
1632 std::vector<Tile*> low_tiles =
1633 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1634 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1636 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1637 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1638 AppendQuadsData data;
1639 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1640 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1641 active_layer_->DidDraw(NULL);
1643 // The missing high res tile was replaced by a low res tile.
1644 EXPECT_EQ(9u, render_pass->quad_list.size());
1645 EXPECT_EQ(0u, data.num_missing_tiles);
1646 EXPECT_EQ(1u, data.num_incomplete_tiles);
1649 TEST_F(PictureLayerImplTest,
1650 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1651 base::TimeTicks time_ticks;
1652 time_ticks += base::TimeDelta::FromMilliseconds(1);
1653 host_impl_.SetCurrentBeginFrameArgs(
1654 CreateBeginFrameArgsForTesting(time_ticks));
1656 gfx::Size tile_size(100, 100);
1657 gfx::Size layer_bounds(200, 200);
1659 host_impl_.SetViewportSize(layer_bounds);
1661 scoped_refptr<FakePicturePileImpl> pending_pile =
1662 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1663 scoped_refptr<FakePicturePileImpl> active_pile =
1664 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1665 SetupTrees(pending_pile, active_pile);
1667 active_layer_->set_fixed_tile_size(tile_size);
1669 active_layer_->draw_properties().visible_content_rect =
1670 gfx::Rect(layer_bounds);
1671 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1673 // One ideal tile exists, this will get used when drawing.
1674 std::vector<Tile*> ideal_tiles;
1675 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1676 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1677 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1680 // Due to layer scale throttling, the raster contents scale is changed to 1,
1681 // while the ideal is still 2.
1682 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1683 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1685 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1686 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1687 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1689 // Both tilings still exist.
1690 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1691 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1693 // All high res tiles have resources.
1694 std::vector<Tile*> high_tiles =
1695 active_layer_->HighResTiling()->AllTilesForTesting();
1696 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1698 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1699 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1700 AppendQuadsData data;
1701 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1702 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1703 active_layer_->DidDraw(NULL);
1705 // All high res tiles drew, and the one ideal res tile drew.
1706 ASSERT_GT(render_pass->quad_list.size(), 9u);
1707 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1708 TileDrawQuad::MaterialCast(render_pass->quad_list.front())
1709 ->tex_coord_rect.size());
1710 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1711 TileDrawQuad::MaterialCast(render_pass->quad_list.ElementAt(1))
1712 ->tex_coord_rect.size());
1714 // Neither the high res nor the ideal tiles were considered as incomplete.
1715 EXPECT_EQ(0u, data.num_missing_tiles);
1716 EXPECT_EQ(0u, data.num_incomplete_tiles);
1719 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1720 gfx::Size layer_bounds(400, 400);
1721 gfx::Size tile_size(100, 100);
1722 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1725 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1727 CreateHighLowResAndSetAllTilesVisible();
1729 active_layer_->SetAllTilesReady();
1731 // No shared tiles and all active tiles ready, so pending can only
1732 // activate with all high res tiles.
1733 pending_layer_->MarkVisibleResourcesAsRequired();
1734 AssertAllTilesRequired(pending_layer_->HighResTiling());
1735 AssertNoTilesRequired(pending_layer_->LowResTiling());
1738 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1739 gfx::Size layer_bounds(400, 400);
1740 gfx::Size tile_size(100, 100);
1741 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1743 // All tiles shared (no invalidation).
1744 CreateHighLowResAndSetAllTilesVisible();
1746 // Verify active tree not ready.
1747 Tile* some_active_tile =
1748 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1749 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1751 // When high res are required, even if the active tree is not ready,
1752 // the high res tiles must be ready.
1753 host_impl_.active_tree()->SetRequiresHighResToDraw();
1754 pending_layer_->MarkVisibleResourcesAsRequired();
1755 AssertAllTilesRequired(pending_layer_->HighResTiling());
1756 AssertNoTilesRequired(pending_layer_->LowResTiling());
1759 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1760 gfx::Size layer_bounds(400, 400);
1761 gfx::Size tile_size(100, 100);
1762 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1764 CreateHighLowResAndSetAllTilesVisible();
1766 Tile* some_active_tile =
1767 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1768 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1770 // All tiles shared (no invalidation), so even though the active tree's
1771 // tiles aren't ready, there is nothing required.
1772 pending_layer_->MarkVisibleResourcesAsRequired();
1773 AssertNoTilesRequired(pending_layer_->HighResTiling());
1774 AssertNoTilesRequired(pending_layer_->LowResTiling());
1777 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1778 gfx::Size layer_bounds(400, 400);
1779 gfx::Size tile_size(100, 100);
1780 scoped_refptr<FakePicturePileImpl> pending_pile =
1781 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1782 // This pile will create tilings, but has no recordings so will not create any
1783 // tiles. This is attempting to simulate scrolling past the end of recorded
1784 // content on the active layer, where the recordings are so far away that
1785 // no tiles are created.
1786 scoped_refptr<FakePicturePileImpl> active_pile =
1787 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1788 tile_size, layer_bounds);
1789 SetupTrees(pending_pile, active_pile);
1790 pending_layer_->set_fixed_tile_size(tile_size);
1791 active_layer_->set_fixed_tile_size(tile_size);
1793 CreateHighLowResAndSetAllTilesVisible();
1795 // Active layer has tilings, but no tiles due to missing recordings.
1796 EXPECT_TRUE(active_layer_->CanHaveTilings());
1797 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1798 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1800 // Since the active layer has no tiles at all, the pending layer doesn't
1801 // need content in order to activate.
1802 pending_layer_->MarkVisibleResourcesAsRequired();
1803 AssertNoTilesRequired(pending_layer_->HighResTiling());
1804 AssertNoTilesRequired(pending_layer_->LowResTiling());
1807 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1808 gfx::Size layer_bounds(400, 400);
1809 gfx::Size tile_size(100, 100);
1810 scoped_refptr<FakePicturePileImpl> pending_pile =
1811 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1812 scoped_refptr<FakePicturePileImpl> active_pile =
1813 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1814 SetupTrees(pending_pile, active_pile);
1815 pending_layer_->set_fixed_tile_size(tile_size);
1816 active_layer_->set_fixed_tile_size(tile_size);
1818 CreateHighLowResAndSetAllTilesVisible();
1820 // Active layer can't have tiles.
1821 EXPECT_FALSE(active_layer_->CanHaveTilings());
1823 // All high res tiles required. This should be considered identical
1824 // to the case where there is no active layer, to avoid flashing content.
1825 // This can happen if a layer exists for a while and switches from
1826 // not being able to have content to having content.
1827 pending_layer_->MarkVisibleResourcesAsRequired();
1828 AssertAllTilesRequired(pending_layer_->HighResTiling());
1829 AssertNoTilesRequired(pending_layer_->LowResTiling());
1832 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1833 gfx::Size layer_bounds(200, 200);
1834 gfx::Size tile_size(100, 100);
1835 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1837 gfx::Size pending_layer_bounds(400, 400);
1838 pending_layer_->SetBounds(pending_layer_bounds);
1840 CreateHighLowResAndSetAllTilesVisible();
1842 active_layer_->SetAllTilesReady();
1844 // Since the active layer has different bounds, the pending layer needs all
1845 // high res tiles in order to activate.
1846 pending_layer_->MarkVisibleResourcesAsRequired();
1847 AssertAllTilesRequired(pending_layer_->HighResTiling());
1848 AssertNoTilesRequired(pending_layer_->LowResTiling());
1851 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1852 gfx::Size tile_size(100, 100);
1853 gfx::Size layer_bounds(400, 400);
1854 scoped_refptr<FakePicturePileImpl> pending_pile =
1855 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1857 host_impl_.CreatePendingTree();
1858 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1860 scoped_ptr<FakePictureLayerImpl> pending_layer =
1861 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1862 pending_layer->SetDrawsContent(true);
1863 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1865 pending_layer_ = static_cast<FakePictureLayerImpl*>(
1866 host_impl_.pending_tree()->LayerById(id_));
1868 // Set some state on the pending layer, make sure it is not clobbered
1869 // by a sync from the active layer. This could happen because if the
1870 // pending layer has not been post-commit initialized it will attempt
1871 // to sync from the active layer.
1872 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1873 pending_layer_->set_raster_page_scale(raster_page_scale);
1874 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1876 host_impl_.ActivateSyncTree();
1878 active_layer_ = static_cast<FakePictureLayerImpl*>(
1879 host_impl_.active_tree()->LayerById(id_));
1881 EXPECT_EQ(0u, active_layer_->num_tilings());
1882 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1883 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1886 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1887 SetupDefaultTrees(gfx::Size(1500, 1500));
1888 AddDefaultTilingsWithInvalidation(gfx::Rect());
1890 host_impl_.ActivateSyncTree();
1891 host_impl_.CreatePendingTree();
1892 active_layer_ = static_cast<FakePictureLayerImpl*>(
1893 host_impl_.active_tree()->LayerById(id_));
1895 // Force the active tree to sync to the pending tree "post-commit".
1896 pending_layer_->DoPostCommitInitializationIfNeeded();
1898 // Both invalidations should drop tiles from the pending tree.
1899 EXPECT_EQ(3u, active_layer_->num_tilings());
1900 EXPECT_EQ(3u, pending_layer_->num_tilings());
1901 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1902 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1903 PictureLayerTiling* pending_tiling =
1904 pending_layer_->tilings()->tiling_at(i);
1906 ASSERT_TRUE(active_tiling);
1907 ASSERT_TRUE(pending_tiling);
1909 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1910 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1911 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1912 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1914 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1915 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1916 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1917 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1919 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1920 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1921 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1922 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1923 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1924 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1925 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1926 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1930 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1931 SetupDefaultTrees(gfx::Size(1500, 1500));
1932 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1934 // This activates the 0,0,1,1 invalidation.
1935 host_impl_.ActivateSyncTree();
1936 host_impl_.CreatePendingTree();
1937 active_layer_ = static_cast<FakePictureLayerImpl*>(
1938 host_impl_.active_tree()->LayerById(id_));
1940 // Force the active tree to sync to the pending tree "post-commit".
1941 pending_layer_->DoPostCommitInitializationIfNeeded();
1943 // The active tree invalidation was handled by the active tiles, so they
1944 // can be shared with the pending tree.
1945 EXPECT_EQ(3u, active_layer_->num_tilings());
1946 EXPECT_EQ(3u, pending_layer_->num_tilings());
1947 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1948 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1949 PictureLayerTiling* pending_tiling =
1950 pending_layer_->tilings()->tiling_at(i);
1952 ASSERT_TRUE(active_tiling);
1953 ASSERT_TRUE(pending_tiling);
1955 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1956 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1957 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1958 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1960 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1961 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1962 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1963 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1965 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1966 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1967 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1968 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1969 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1970 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1971 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1972 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1976 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
1977 SetupDefaultTrees(gfx::Size(1500, 1500));
1978 AddDefaultTilingsWithInvalidation(gfx::Rect());
1980 host_impl_.ActivateSyncTree();
1981 host_impl_.CreatePendingTree();
1982 active_layer_ = static_cast<FakePictureLayerImpl*>(
1983 host_impl_.active_tree()->LayerById(id_));
1985 // Set some invalidation on the pending tree "during commit". We should
1986 // replace raster tiles that touch this.
1987 pending_layer_->set_invalidation(gfx::Rect(1, 1));
1989 // Force the active tree to sync to the pending tree "post-commit".
1990 pending_layer_->DoPostCommitInitializationIfNeeded();
1992 // The pending tree invalidation means tiles can not be shared with the
1994 EXPECT_EQ(3u, active_layer_->num_tilings());
1995 EXPECT_EQ(3u, pending_layer_->num_tilings());
1996 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1997 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1998 PictureLayerTiling* pending_tiling =
1999 pending_layer_->tilings()->tiling_at(i);
2001 ASSERT_TRUE(active_tiling);
2002 ASSERT_TRUE(pending_tiling);
2004 EXPECT_TRUE(active_tiling->TileAt(0, 0));
2005 EXPECT_TRUE(active_tiling->TileAt(1, 0));
2006 EXPECT_TRUE(active_tiling->TileAt(0, 1));
2007 EXPECT_TRUE(active_tiling->TileAt(1, 1));
2009 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
2010 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
2011 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
2012 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
2014 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
2015 EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
2016 EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
2017 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
2018 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
2019 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
2020 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2021 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
2022 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
2026 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
2027 SetupDefaultTrees(gfx::Size(10, 10));
2028 host_impl_.active_tree()->UpdateDrawProperties();
2029 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2031 // Contrived unit test of a real crash. A layer is transparent during a
2032 // context loss, and later becomes opaque, causing active layer SyncTiling to
2034 float new_scale = 1.f;
2035 active_layer_->ReleaseResources();
2036 pending_layer_->ReleaseResources();
2037 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
2038 pending_layer_->AddTiling(new_scale);
2039 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
2041 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
2042 // responsible for calling ManageTilings. These checks verify that
2043 // ReleaseResources has set needs update draw properties so that the
2044 // new tiling gets the appropriate resolution set in ManageTilings.
2045 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2046 host_impl_.active_tree()->UpdateDrawProperties();
2047 PictureLayerTiling* high_res =
2048 active_layer_->tilings()->TilingAtScale(new_scale);
2049 ASSERT_TRUE(!!high_res);
2050 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
2053 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
2054 SetupDefaultTrees(gfx::Size(10, 10));
2056 const float kScale = 1.f;
2057 pending_layer_->AddTiling(kScale);
2058 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2059 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2061 // Gpu rasterization is disabled by default.
2062 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2063 // Toggling the gpu rasterization clears all tilings on both trees.
2064 host_impl_.SetUseGpuRasterization(true);
2065 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2066 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2068 // Make sure that we can still add tiling to the pending layer,
2069 // that gets synced to the active layer.
2070 pending_layer_->AddTiling(kScale);
2071 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2072 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2074 // Toggling the gpu rasterization clears all tilings on both trees.
2075 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2076 host_impl_.SetUseGpuRasterization(false);
2077 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2078 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2081 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2082 SetupDefaultTrees(gfx::Size(10, 10));
2083 host_impl_.active_tree()->UpdateDrawProperties();
2084 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2086 SetupDrawPropertiesAndUpdateTiles(
2087 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2088 active_layer_->tilings()->RemoveAllTilings();
2089 PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
2090 active_layer_->tilings()->AddTiling(1.5f);
2091 active_layer_->tilings()->AddTiling(0.25f);
2092 tiling->set_resolution(HIGH_RESOLUTION);
2095 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2096 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2098 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2099 // 1.0f). Note that we should also ensure that the pending layer needs post
2100 // commit initialization, since this is what would happen during commit. In
2101 // other words we want the pending layer to sync from the active layer.
2102 pending_layer_->SetBounds(gfx::Size(1, 1));
2103 pending_layer_->SetNeedsPostCommitInitialization();
2104 pending_layer_->set_twin_layer(NULL);
2105 active_layer_->set_twin_layer(NULL);
2106 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2108 // Update the draw properties: sync from active tree should happen here.
2109 host_impl_.pending_tree()->UpdateDrawProperties();
2111 // Another sanity check.
2112 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2114 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2115 // violate minimum contents scale. At the same time, we should've created a
2116 // new high res tiling at scale 1.0f.
2117 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2118 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2119 EXPECT_EQ(HIGH_RESOLUTION,
2120 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2121 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2122 EXPECT_EQ(NON_IDEAL_RESOLUTION,
2123 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2126 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2127 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2128 gfx::Size layer_bounds(default_tile_size.width() * 4,
2129 default_tile_size.height() * 4);
2131 SetupDefaultTrees(layer_bounds);
2132 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2133 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2134 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2135 // Should have a low-res and a high-res tiling.
2136 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2138 ResetTilingsAndRasterScales();
2140 host_impl_.SetUseGpuRasterization(true);
2141 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2142 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2144 // Should only have the high-res tiling.
2145 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2148 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2149 // Set up layers with tilings.
2150 SetupDefaultTrees(gfx::Size(10, 10));
2151 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2152 pending_layer_->PushPropertiesTo(active_layer_);
2153 EXPECT_TRUE(pending_layer_->DrawsContent());
2154 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2155 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2156 EXPECT_GE(active_layer_->num_tilings(), 0u);
2158 // Set content to false, which should make CanHaveTilings return false.
2159 pending_layer_->SetDrawsContent(false);
2160 EXPECT_FALSE(pending_layer_->DrawsContent());
2161 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2163 // No tilings should be pushed to active layer.
2164 pending_layer_->PushPropertiesTo(active_layer_);
2165 EXPECT_EQ(0u, active_layer_->num_tilings());
2168 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2169 SetupDefaultTrees(gfx::Size(10, 10));
2170 host_impl_.PinchGestureBegin();
2171 float high_res_scale = 2.3f;
2172 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2174 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2175 EXPECT_FLOAT_EQ(high_res_scale,
2176 pending_layer_->HighResTiling()->contents_scale());
2179 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2180 SetupDefaultTrees(gfx::Size(10, 10));
2181 host_impl_.PinchGestureBegin();
2182 float high_res_scale = 0.0001f;
2183 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2185 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2187 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2188 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2189 pending_layer_->HighResTiling()->contents_scale());
2192 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2193 SetupDefaultTrees(gfx::Size(10, 10));
2195 float contents_scale = 0.15f;
2196 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2198 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2199 EXPECT_FLOAT_EQ(contents_scale,
2200 pending_layer_->HighResTiling()->contents_scale());
2202 host_impl_.PinchGestureBegin();
2204 float page_scale = 0.0001f;
2205 EXPECT_LT(page_scale * contents_scale,
2206 pending_layer_->MinimumContentsScale());
2208 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2209 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2210 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2211 pending_layer_->HighResTiling()->contents_scale());
2214 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2216 virtual void InitializeRenderer() OVERRIDE {
2217 bool delegated_rendering = false;
2218 host_impl_.InitializeRenderer(
2219 FakeOutputSurface::CreateDeferredGL(
2220 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2221 delegated_rendering).PassAs<OutputSurface>());
2224 virtual void SetUp() OVERRIDE {
2225 PictureLayerImplTest::SetUp();
2227 // Create some default active and pending trees.
2228 gfx::Size tile_size(100, 100);
2229 gfx::Size layer_bounds(400, 400);
2231 scoped_refptr<FakePicturePileImpl> pending_pile =
2232 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2233 scoped_refptr<FakePicturePileImpl> active_pile =
2234 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2236 SetupTrees(pending_pile, active_pile);
2240 // This test is really a LayerTreeHostImpl test, in that it makes sure
2241 // that trees need update draw properties after deferred initialization.
2242 // However, this is also a regression test for PictureLayerImpl in that
2243 // not having this update will cause a crash.
2244 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2245 host_impl_.pending_tree()->UpdateDrawProperties();
2246 host_impl_.active_tree()->UpdateDrawProperties();
2247 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2248 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2250 FakeOutputSurface* fake_output_surface =
2251 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2252 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2253 TestContextProvider::Create()));
2255 // These will crash PictureLayerImpl if this is not true.
2256 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2257 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2258 host_impl_.active_tree()->UpdateDrawProperties();
2261 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2262 gfx::Size layer_bounds(100, 100);
2263 gfx::Size viewport_size(1000, 1000);
2264 SetupDefaultTrees(layer_bounds);
2265 host_impl_.SetViewportSize(viewport_size);
2267 float contents_scale = 1.f;
2268 float device_scale = 1.3f;
2269 float page_scale = 1.4f;
2270 float maximum_animation_scale = 1.f;
2271 bool animating_transform = false;
2273 SetContentsScaleOnBothLayers(contents_scale,
2276 maximum_animation_scale,
2277 animating_transform);
2278 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2280 // Since we're CPU-rasterizing, starting an animation should cause tiling
2281 // resolution to get set to the maximum animation scale factor.
2282 animating_transform = true;
2283 maximum_animation_scale = 3.f;
2284 contents_scale = 2.f;
2286 SetContentsScaleOnBothLayers(contents_scale,
2289 maximum_animation_scale,
2290 animating_transform);
2291 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2293 // Further changes to scale during the animation should not cause a new
2294 // high-res tiling to get created.
2295 contents_scale = 4.f;
2296 maximum_animation_scale = 5.f;
2298 SetContentsScaleOnBothLayers(contents_scale,
2301 maximum_animation_scale,
2302 animating_transform);
2303 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2305 // Once we stop animating, a new high-res tiling should be created.
2306 animating_transform = false;
2308 SetContentsScaleOnBothLayers(contents_scale,
2311 maximum_animation_scale,
2312 animating_transform);
2313 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2315 // When animating with an unknown maximum animation scale factor, a new
2316 // high-res tiling should be created at the animation's initial scale.
2317 animating_transform = true;
2318 contents_scale = 2.f;
2319 maximum_animation_scale = 0.f;
2321 SetContentsScaleOnBothLayers(contents_scale,
2324 maximum_animation_scale,
2325 animating_transform);
2326 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2328 // Further changes to scale during the animation should not cause a new
2329 // high-res tiling to get created.
2330 contents_scale = 3.f;
2332 SetContentsScaleOnBothLayers(contents_scale,
2335 maximum_animation_scale,
2336 animating_transform);
2337 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2339 // Once we stop animating, a new high-res tiling should be created.
2340 animating_transform = false;
2341 contents_scale = 4.f;
2343 SetContentsScaleOnBothLayers(contents_scale,
2346 maximum_animation_scale,
2347 animating_transform);
2348 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2350 // When animating with a maxmium animation scale factor that is so large
2351 // that the layer grows larger than the viewport at this scale, a new
2352 // high-res tiling should get created at the animation's initial scale, not
2353 // at its maximum scale.
2354 animating_transform = true;
2355 contents_scale = 2.f;
2356 maximum_animation_scale = 11.f;
2358 SetContentsScaleOnBothLayers(contents_scale,
2361 maximum_animation_scale,
2362 animating_transform);
2363 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2365 // Once we stop animating, a new high-res tiling should be created.
2366 animating_transform = false;
2367 contents_scale = 11.f;
2369 SetContentsScaleOnBothLayers(contents_scale,
2372 maximum_animation_scale,
2373 animating_transform);
2374 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2376 // When animating with a maxmium animation scale factor that is so large
2377 // that the layer grows larger than the viewport at this scale, and where
2378 // the intial source scale is < 1, a new high-res tiling should get created
2379 // at source scale 1.
2380 animating_transform = true;
2381 contents_scale = 0.1f;
2382 maximum_animation_scale = 11.f;
2384 SetContentsScaleOnBothLayers(contents_scale,
2387 maximum_animation_scale,
2388 animating_transform);
2389 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2391 // Once we stop animating, a new high-res tiling should be created.
2392 animating_transform = false;
2393 contents_scale = 11.f;
2395 SetContentsScaleOnBothLayers(contents_scale,
2398 maximum_animation_scale,
2399 animating_transform);
2400 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2403 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2404 gfx::Size layer_bounds(100, 100);
2405 gfx::Size viewport_size(1000, 1000);
2406 SetupDefaultTrees(layer_bounds);
2407 host_impl_.SetViewportSize(viewport_size);
2408 host_impl_.SetUseGpuRasterization(true);
2410 float contents_scale = 1.f;
2411 float device_scale = 1.3f;
2412 float page_scale = 1.4f;
2413 float maximum_animation_scale = 1.f;
2414 bool animating_transform = false;
2416 SetContentsScaleOnBothLayers(contents_scale,
2419 maximum_animation_scale,
2420 animating_transform);
2421 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2423 // Since we're GPU-rasterizing, starting an animation should cause tiling
2424 // resolution to get set to the current contents scale.
2425 animating_transform = true;
2426 contents_scale = 2.f;
2427 maximum_animation_scale = 4.f;
2429 SetContentsScaleOnBothLayers(contents_scale,
2432 maximum_animation_scale,
2433 animating_transform);
2434 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2436 // Further changes to scale during the animation should cause a new high-res
2437 // tiling to get created.
2438 contents_scale = 3.f;
2440 SetContentsScaleOnBothLayers(contents_scale,
2443 maximum_animation_scale,
2444 animating_transform);
2445 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2447 // Since we're re-rasterizing during the animation, scales smaller than 1
2448 // should be respected.
2449 contents_scale = 0.25f;
2451 SetContentsScaleOnBothLayers(contents_scale,
2454 maximum_animation_scale,
2455 animating_transform);
2456 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2458 // Once we stop animating, a new high-res tiling should be created.
2459 contents_scale = 4.f;
2460 animating_transform = false;
2462 SetContentsScaleOnBothLayers(contents_scale,
2465 maximum_animation_scale,
2466 animating_transform);
2467 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2469 static_cast<FakePicturePileImpl*>(pending_layer_->pile())->set_has_text(true);
2470 static_cast<FakePicturePileImpl*>(active_layer_->pile())->set_has_text(true);
2472 // Since we're GPU-rasterizing but have text, starting an animation should
2473 // cause tiling resolution to get set to the maximum animation scale.
2474 animating_transform = true;
2475 contents_scale = 2.f;
2476 maximum_animation_scale = 3.f;
2478 SetContentsScaleOnBothLayers(contents_scale,
2481 maximum_animation_scale,
2482 animating_transform);
2483 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2485 // Further changes to scale during the animation should not cause a new
2486 // high-res tiling to get created.
2487 contents_scale = 4.f;
2488 maximum_animation_scale = 5.f;
2490 SetContentsScaleOnBothLayers(contents_scale,
2493 maximum_animation_scale,
2494 animating_transform);
2495 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2497 // Once we stop animating, a new high-res tiling should be created.
2498 animating_transform = false;
2500 SetContentsScaleOnBothLayers(contents_scale,
2503 maximum_animation_scale,
2504 animating_transform);
2505 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2508 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2509 base::TimeTicks time_ticks;
2510 time_ticks += base::TimeDelta::FromMilliseconds(1);
2511 host_impl_.SetCurrentBeginFrameArgs(
2512 CreateBeginFrameArgsForTesting(time_ticks));
2514 gfx::Size tile_size(100, 100);
2515 gfx::Size layer_bounds(1000, 1000);
2517 scoped_refptr<FakePicturePileImpl> pending_pile =
2518 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2520 SetupPendingTree(pending_pile);
2522 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2524 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2527 PictureLayerImpl::LayerRasterTileIterator it;
2531 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2534 pending_layer_->AddTiling(low_res_factor);
2535 pending_layer_->AddTiling(0.3f);
2536 pending_layer_->AddTiling(0.7f);
2537 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2538 pending_layer_->AddTiling(2.0f);
2540 host_impl_.SetViewportSize(gfx::Size(500, 500));
2541 host_impl_.pending_tree()->UpdateDrawProperties();
2543 std::set<Tile*> unique_tiles;
2544 bool reached_prepaint = false;
2545 size_t non_ideal_tile_count = 0u;
2546 size_t low_res_tile_count = 0u;
2547 size_t high_res_tile_count = 0u;
2548 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2552 TilePriority priority = tile->priority(PENDING_TREE);
2556 // Non-high res tiles only get visible tiles. Also, prepaint should only
2557 // come at the end of the iteration.
2558 if (priority.resolution != HIGH_RESOLUTION)
2559 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2560 else if (reached_prepaint)
2561 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2563 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2565 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2566 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2567 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2569 unique_tiles.insert(tile);
2572 EXPECT_TRUE(reached_prepaint);
2573 EXPECT_EQ(0u, non_ideal_tile_count);
2574 EXPECT_EQ(1u, low_res_tile_count);
2575 EXPECT_EQ(16u, high_res_tile_count);
2576 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2577 unique_tiles.size());
2580 time_ticks += base::TimeDelta::FromMilliseconds(200);
2581 host_impl_.SetCurrentBeginFrameArgs(
2582 CreateBeginFrameArgsForTesting(time_ticks));
2584 pending_layer_->draw_properties().visible_content_rect =
2585 gfx::Rect(1100, 1100, 500, 500);
2586 pending_layer_->UpdateTiles(Occlusion());
2588 unique_tiles.clear();
2589 high_res_tile_count = 0u;
2590 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2594 TilePriority priority = tile->priority(PENDING_TREE);
2598 // Non-high res tiles only get visible tiles.
2599 EXPECT_EQ(HIGH_RESOLUTION, priority.resolution);
2600 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2602 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2604 unique_tiles.insert(tile);
2607 EXPECT_EQ(16u, high_res_tile_count);
2608 EXPECT_EQ(high_res_tile_count, unique_tiles.size());
2610 time_ticks += base::TimeDelta::FromMilliseconds(200);
2611 host_impl_.SetCurrentBeginFrameArgs(
2612 CreateBeginFrameArgsForTesting(time_ticks));
2614 pending_layer_->draw_properties().visible_content_rect =
2615 gfx::Rect(0, 0, 500, 500);
2616 pending_layer_->UpdateTiles(Occlusion());
2618 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2619 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2620 tile_it != high_res_tiles.end();
2622 Tile* tile = *tile_it;
2623 ManagedTileState::TileVersion& tile_version =
2624 tile->GetTileVersionForTesting(
2625 tile->DetermineRasterModeForTree(ACTIVE_TREE));
2626 tile_version.SetSolidColorForTesting(SK_ColorRED);
2629 non_ideal_tile_count = 0;
2630 low_res_tile_count = 0;
2631 high_res_tile_count = 0;
2632 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2636 TilePriority priority = tile->priority(PENDING_TREE);
2640 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2641 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2642 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2645 EXPECT_EQ(0u, non_ideal_tile_count);
2646 EXPECT_EQ(1u, low_res_tile_count);
2647 EXPECT_EQ(0u, high_res_tile_count);
2650 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2651 gfx::Size tile_size(100, 100);
2652 gfx::Size layer_bounds(1000, 1000);
2654 scoped_refptr<FakePicturePileImpl> pending_pile =
2655 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2657 SetupPendingTree(pending_pile);
2659 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2661 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2663 std::vector<PictureLayerTiling*> tilings;
2664 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2665 tilings.push_back(pending_layer_->AddTiling(0.3f));
2666 tilings.push_back(pending_layer_->AddTiling(0.7f));
2667 tilings.push_back(pending_layer_->AddTiling(1.0f));
2668 tilings.push_back(pending_layer_->AddTiling(2.0f));
2670 host_impl_.SetViewportSize(gfx::Size(500, 500));
2671 host_impl_.pending_tree()->UpdateDrawProperties();
2673 std::vector<Tile*> all_tiles;
2674 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2676 tiling_iterator != tilings.end();
2677 ++tiling_iterator) {
2678 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2679 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2682 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2684 bool mark_required = false;
2685 size_t number_of_marked_tiles = 0u;
2686 size_t number_of_unmarked_tiles = 0u;
2687 for (size_t i = 0; i < tilings.size(); ++i) {
2688 PictureLayerTiling* tiling = tilings.at(i);
2689 for (PictureLayerTiling::CoverageIterator iter(
2691 pending_layer_->contents_scale_x(),
2692 pending_layer_->visible_content_rect());
2695 if (mark_required) {
2696 number_of_marked_tiles++;
2697 iter->MarkRequiredForActivation();
2699 number_of_unmarked_tiles++;
2701 mark_required = !mark_required;
2706 EXPECT_EQ(91u, all_tiles.size());
2707 EXPECT_EQ(91u, all_tiles_set.size());
2708 EXPECT_GT(number_of_marked_tiles, 1u);
2709 EXPECT_GT(number_of_unmarked_tiles, 1u);
2712 PictureLayerImpl::LayerEvictionTileIterator it;
2715 // Tiles don't have resources yet.
2716 it = PictureLayerImpl::LayerEvictionTileIterator(
2717 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2720 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2722 std::set<Tile*> unique_tiles;
2723 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2724 size_t scale_index = 0;
2725 bool reached_visible = false;
2726 Tile* last_tile = NULL;
2727 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2728 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2737 TilePriority priority = tile->priority(PENDING_TREE);
2739 if (priority.priority_bin == TilePriority::NOW) {
2740 reached_visible = true;
2745 EXPECT_FALSE(tile->required_for_activation());
2747 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2748 std::numeric_limits<float>::epsilon()) {
2750 ASSERT_LT(scale_index, arraysize(expected_scales));
2753 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2754 unique_tiles.insert(tile);
2756 // If the tile is the same rough bin as last tile (same activation, bin, and
2757 // scale), then distance should be decreasing.
2758 if (tile->required_for_activation() ==
2759 last_tile->required_for_activation() &&
2760 priority.priority_bin ==
2761 last_tile->priority(PENDING_TREE).priority_bin &&
2762 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2763 std::numeric_limits<float>::epsilon()) {
2764 EXPECT_LE(priority.distance_to_visible,
2765 last_tile->priority(PENDING_TREE).distance_to_visible);
2771 EXPECT_TRUE(reached_visible);
2772 EXPECT_EQ(65u, unique_tiles.size());
2775 bool reached_required = false;
2780 TilePriority priority = tile->priority(PENDING_TREE);
2781 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2783 if (reached_required) {
2784 EXPECT_TRUE(tile->required_for_activation());
2785 } else if (tile->required_for_activation()) {
2786 reached_required = true;
2790 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2791 std::numeric_limits<float>::epsilon()) {
2793 ASSERT_LT(scale_index, arraysize(expected_scales));
2796 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2797 unique_tiles.insert(tile);
2800 EXPECT_TRUE(reached_required);
2801 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2804 TEST_F(PictureLayerImplTest, Occlusion) {
2805 gfx::Size tile_size(102, 102);
2806 gfx::Size layer_bounds(1000, 1000);
2807 gfx::Size viewport_size(1000, 1000);
2809 LayerTestCommon::LayerImplTest impl;
2811 scoped_refptr<FakePicturePileImpl> pending_pile =
2812 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2813 SetupPendingTree(pending_pile);
2814 pending_layer_->SetBounds(layer_bounds);
2816 active_layer_->set_fixed_tile_size(tile_size);
2818 host_impl_.SetViewportSize(viewport_size);
2819 host_impl_.active_tree()->UpdateDrawProperties();
2821 std::vector<Tile*> tiles =
2822 active_layer_->HighResTiling()->AllTilesForTesting();
2823 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2826 SCOPED_TRACE("No occlusion");
2828 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2830 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2831 gfx::Rect(layer_bounds));
2832 EXPECT_EQ(100u, impl.quad_list().size());
2836 SCOPED_TRACE("Full occlusion");
2837 gfx::Rect occluded(active_layer_->visible_content_rect());
2838 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2840 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2841 EXPECT_EQ(impl.quad_list().size(), 0u);
2845 SCOPED_TRACE("Partial occlusion");
2846 gfx::Rect occluded(150, 0, 200, 1000);
2847 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2849 size_t partially_occluded_count = 0;
2850 LayerTestCommon::VerifyQuadsAreOccluded(
2851 impl.quad_list(), occluded, &partially_occluded_count);
2852 // The layer outputs one quad, which is partially occluded.
2853 EXPECT_EQ(100u - 10u, impl.quad_list().size());
2854 EXPECT_EQ(10u + 10u, partially_occluded_count);
2858 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2859 gfx::Size tile_size(host_impl_.settings().default_tile_size);
2860 SetupDefaultTrees(tile_size);
2862 float contents_scale = 2.f;
2863 float device_scale = 1.f;
2864 float page_scale = 1.f;
2865 float maximum_animation_scale = 1.f;
2866 bool animating_transform = false;
2868 SetContentsScaleOnBothLayers(contents_scale,
2871 maximum_animation_scale,
2872 animating_transform);
2873 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2875 // Changing the source scale without being in an animation will cause
2876 // the layer to reset its source scale to 1.f.
2877 contents_scale = 3.f;
2879 SetContentsScaleOnBothLayers(contents_scale,
2882 maximum_animation_scale,
2883 animating_transform);
2884 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2886 // Further changes to the source scale will no longer be reflected in the
2888 contents_scale = 0.5f;
2890 SetContentsScaleOnBothLayers(contents_scale,
2893 maximum_animation_scale,
2894 animating_transform);
2895 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2898 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2899 gfx::Size tile_size(100, 100);
2900 gfx::Size layer_bounds(1000, 1000);
2902 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2904 // Make sure some tiles are not shared.
2905 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2907 CreateHighLowResAndSetAllTilesVisible();
2908 active_layer_->SetAllTilesReady();
2909 pending_layer_->MarkVisibleResourcesAsRequired();
2911 // All pending layer tiles required are not ready.
2912 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2914 // Initialize all low-res tiles.
2915 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2917 // Low-res tiles should not be enough.
2918 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2920 // Initialize remaining tiles.
2921 pending_layer_->SetAllTilesReady();
2923 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2926 TEST_F(PictureLayerImplTest, HighResReadyToDrawEnoughToActivate) {
2927 gfx::Size tile_size(100, 100);
2928 gfx::Size layer_bounds(1000, 1000);
2930 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2932 // Make sure some tiles are not shared.
2933 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2935 CreateHighLowResAndSetAllTilesVisible();
2936 active_layer_->SetAllTilesReady();
2937 pending_layer_->MarkVisibleResourcesAsRequired();
2939 // All pending layer tiles required are not ready.
2940 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2942 // Initialize all high-res tiles.
2943 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2945 // High-res tiles should be enough, since they cover everything visible.
2946 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2949 TEST_F(PictureLayerImplTest,
2950 SharedActiveHighResReadyAndPendingLowResReadyNotEnoughToActivate) {
2951 gfx::Size tile_size(100, 100);
2952 gfx::Size layer_bounds(1000, 1000);
2954 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2956 // Make sure some tiles are not shared.
2957 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2959 CreateHighLowResAndSetAllTilesVisible();
2961 // Initialize all high-res tiles in the active layer.
2962 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
2963 // And all the low-res tiles in the pending layer.
2964 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2966 pending_layer_->MarkVisibleResourcesAsRequired();
2968 // The unshared high-res tiles are not ready, so we cannot activate.
2969 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2971 // When the unshared pending high-res tiles are ready, we can activate.
2972 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2973 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2976 TEST_F(PictureLayerImplTest, SharedActiveHighResReadyNotEnoughToActivate) {
2977 gfx::Size tile_size(100, 100);
2978 gfx::Size layer_bounds(1000, 1000);
2980 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2982 // Make sure some tiles are not shared.
2983 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2985 CreateHighLowResAndSetAllTilesVisible();
2987 // Initialize all high-res tiles in the active layer.
2988 active_layer_->SetAllTilesReadyInTiling(active_layer_->HighResTiling());
2990 pending_layer_->MarkVisibleResourcesAsRequired();
2992 // The unshared high-res tiles are not ready, so we cannot activate.
2993 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2995 // When the unshared pending high-res tiles are ready, we can activate.
2996 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2997 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
3000 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
3002 NoLowResPictureLayerImplTest()
3003 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
3006 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
3007 gfx::Size tile_size(400, 400);
3008 gfx::Size layer_bounds(1300, 1900);
3010 scoped_refptr<FakePicturePileImpl> pending_pile =
3011 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3012 scoped_refptr<FakePicturePileImpl> active_pile =
3013 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3015 SetupTrees(pending_pile, active_pile);
3016 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3018 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3019 EXPECT_LT(low_res_factor, 1.f);
3021 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3022 6.f, // ideal contents scale
3023 3.f, // device scale
3025 1.f, // maximum animation scale
3027 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
3028 EXPECT_FLOAT_EQ(6.f,
3029 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3031 // If we change the page scale factor, then we should get new tilings.
3032 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3033 6.6f, // ideal contents scale
3034 3.f, // device scale
3036 1.f, // maximum animation scale
3038 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
3039 EXPECT_FLOAT_EQ(6.6f,
3040 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3042 // If we change the device scale factor, then we should get new tilings.
3043 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3044 7.26f, // ideal contents scale
3045 3.3f, // device scale
3047 1.f, // maximum animation scale
3049 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3050 EXPECT_FLOAT_EQ(7.26f,
3051 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3053 // If we change the device scale factor, but end up at the same total scale
3054 // factor somehow, then we don't get new tilings.
3055 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3056 7.26f, // ideal contents scale
3057 2.2f, // device scale
3059 1.f, // maximum animation scale
3061 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
3062 EXPECT_FLOAT_EQ(7.26f,
3063 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3066 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
3067 gfx::Size tile_size(100, 100);
3068 gfx::Size layer_bounds(1000, 1000);
3070 scoped_refptr<FakePicturePileImpl> pending_pile =
3071 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
3072 // Layers with entirely empty piles can't get tilings.
3073 pending_pile->AddRecordingAt(0, 0);
3075 SetupPendingTree(pending_pile);
3077 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3078 pending_layer_->AddTiling(1.0f);
3079 pending_layer_->AddTiling(2.0f);
3081 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
3082 // on a layer with no recordings.
3083 host_impl_.pending_tree()->UpdateDrawProperties();
3084 pending_layer_->MarkVisibleResourcesAsRequired();
3087 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
3088 gfx::Size layer_bounds(400, 400);
3089 gfx::Size tile_size(100, 100);
3090 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
3092 CreateHighLowResAndSetAllTilesVisible();
3094 Tile* some_active_tile =
3095 active_layer_->HighResTiling()->AllTilesForTesting()[0];
3096 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
3098 // All tiles shared (no invalidation), so even though the active tree's
3099 // tiles aren't ready, there is nothing required.
3100 pending_layer_->MarkVisibleResourcesAsRequired();
3101 AssertNoTilesRequired(pending_layer_->HighResTiling());
3102 if (host_impl_.settings().create_low_res_tiling) {
3103 AssertNoTilesRequired(pending_layer_->LowResTiling());
3107 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
3108 gfx::Size layer_bounds(400, 400);
3109 gfx::Size tile_size(100, 100);
3110 scoped_refptr<FakePicturePileImpl> pending_pile =
3111 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3112 // This pile will create tilings, but has no recordings so will not create any
3113 // tiles. This is attempting to simulate scrolling past the end of recorded
3114 // content on the active layer, where the recordings are so far away that
3115 // no tiles are created.
3116 scoped_refptr<FakePicturePileImpl> active_pile =
3117 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
3118 tile_size, layer_bounds);
3119 SetupTrees(pending_pile, active_pile);
3120 pending_layer_->set_fixed_tile_size(tile_size);
3121 active_layer_->set_fixed_tile_size(tile_size);
3123 CreateHighLowResAndSetAllTilesVisible();
3125 // Active layer has tilings, but no tiles due to missing recordings.
3126 EXPECT_TRUE(active_layer_->CanHaveTilings());
3127 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
3128 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
3129 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
3131 // Since the active layer has no tiles at all, the pending layer doesn't
3132 // need content in order to activate.
3133 pending_layer_->MarkVisibleResourcesAsRequired();
3134 AssertNoTilesRequired(pending_layer_->HighResTiling());
3135 if (host_impl_.settings().create_low_res_tiling)
3136 AssertNoTilesRequired(pending_layer_->LowResTiling());
3139 TEST_F(NoLowResPictureLayerImplTest,
3140 ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
3141 base::TimeTicks time_ticks;
3142 time_ticks += base::TimeDelta::FromMilliseconds(1);
3143 host_impl_.SetCurrentBeginFrameArgs(
3144 CreateBeginFrameArgsForTesting(time_ticks));
3146 gfx::Size tile_size(100, 100);
3147 gfx::Size layer_bounds(400, 400);
3149 scoped_refptr<FakePicturePileImpl> pending_pile =
3150 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3151 scoped_refptr<FakePicturePileImpl> active_pile =
3152 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3154 SetupTrees(pending_pile, active_pile);
3156 Region invalidation;
3157 AddDefaultTilingsWithInvalidation(invalidation);
3158 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3160 // UpdateTiles with valid viewport. Should update tile viewport.
3161 bool resourceless_software_draw = false;
3162 gfx::Rect viewport = gfx::Rect(layer_bounds);
3163 gfx::Transform transform;
3164 host_impl_.SetExternalDrawConstraints(transform,
3169 resourceless_software_draw);
3170 active_layer_->draw_properties().visible_content_rect = viewport;
3171 active_layer_->draw_properties().screen_space_transform = transform;
3172 active_layer_->UpdateTiles(Occlusion());
3174 gfx::Rect visible_rect_for_tile_priority =
3175 active_layer_->visible_rect_for_tile_priority();
3176 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3177 gfx::Rect viewport_rect_for_tile_priority =
3178 active_layer_->viewport_rect_for_tile_priority();
3179 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
3180 gfx::Transform screen_space_transform_for_tile_priority =
3181 active_layer_->screen_space_transform_for_tile_priority();
3183 // PictureLayerImpl does not make a special case for
3184 // resource_less_software_draw, so the tile viewport and matrix should be
3186 time_ticks += base::TimeDelta::FromMilliseconds(200);
3187 host_impl_.SetCurrentBeginFrameArgs(
3188 CreateBeginFrameArgsForTesting(time_ticks));
3189 resourceless_software_draw = true;
3190 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3191 transform.Translate(1.f, 1.f);
3192 active_layer_->draw_properties().visible_content_rect = viewport;
3193 active_layer_->draw_properties().screen_space_transform = transform;
3194 host_impl_.SetExternalDrawConstraints(transform,
3199 resourceless_software_draw);
3200 active_layer_->UpdateTiles(Occlusion());
3202 visible_rect_for_tile_priority =
3203 gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
3204 viewport_rect_for_tile_priority =
3205 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
3206 screen_space_transform_for_tile_priority = transform;
3208 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
3209 active_layer_->visible_rect_for_tile_priority());
3210 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
3211 active_layer_->viewport_rect_for_tile_priority());
3212 EXPECT_TRANSFORMATION_MATRIX_EQ(
3213 screen_space_transform_for_tile_priority,
3214 active_layer_->screen_space_transform_for_tile_priority());
3217 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3218 gfx::Size tile_size(400, 400);
3219 gfx::Size layer_bounds(1300, 1900);
3221 scoped_refptr<FakePicturePileImpl> pending_pile =
3222 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3223 scoped_refptr<FakePicturePileImpl> active_pile =
3224 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3226 std::vector<PictureLayerTiling*> used_tilings;
3228 SetupTrees(pending_pile, active_pile);
3229 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3231 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3232 EXPECT_LT(low_res_factor, 1.f);
3234 float device_scale = 1.7f;
3235 float page_scale = 3.2f;
3238 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3239 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3241 // We only have ideal tilings, so they aren't removed.
3242 used_tilings.clear();
3243 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3244 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3246 host_impl_.PinchGestureBegin();
3248 // Changing the ideal but not creating new tilings.
3251 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3252 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3254 // The tilings are still our target scale, so they aren't removed.
3255 used_tilings.clear();
3256 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3257 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3259 host_impl_.PinchGestureEnd();
3261 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3264 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3265 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3266 EXPECT_FLOAT_EQ(1.f,
3267 active_layer_->tilings()->tiling_at(1)->contents_scale());
3269 // Mark the non-ideal tilings as used. They won't be removed.
3270 used_tilings.clear();
3271 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3272 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3273 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3275 // Now move the ideal scale to 0.5. Our target stays 1.2.
3276 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3278 // The high resolution tiling is between target and ideal, so is not
3279 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3280 used_tilings.clear();
3281 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3282 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3284 // Now move the ideal scale to 1.0. Our target stays 1.2.
3285 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3287 // All the tilings are between are target and the ideal, so they are not
3289 used_tilings.clear();
3290 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3291 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3293 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3294 SetupDrawPropertiesAndUpdateTiles(
3295 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3297 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3298 // in the range [1.0,1.2] and are kept.
3299 used_tilings.clear();
3300 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3301 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3303 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3305 SetupDrawPropertiesAndUpdateTiles(
3306 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3308 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3309 // target raster scale. But it is in our used tilings set, so nothing is
3311 used_tilings.clear();
3312 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3313 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3314 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3316 // If we remove it from our used tilings set, it is outside the range to keep
3317 // so it is deleted.
3318 used_tilings.clear();
3319 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3320 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3323 TEST_F(PictureLayerImplTest, ScaleCollision) {
3324 gfx::Size tile_size(400, 400);
3325 gfx::Size layer_bounds(1300, 1900);
3327 scoped_refptr<FakePicturePileImpl> pending_pile =
3328 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3329 scoped_refptr<FakePicturePileImpl> active_pile =
3330 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3332 std::vector<PictureLayerTiling*> used_tilings;
3334 SetupTrees(pending_pile, active_pile);
3336 float pending_contents_scale = 1.f;
3337 float active_contents_scale = 2.f;
3338 float device_scale_factor = 1.f;
3339 float page_scale_factor = 1.f;
3340 float maximum_animation_contents_scale = 1.f;
3341 bool animating_transform = false;
3343 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3344 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3345 EXPECT_LT(low_res_factor, 1.f);
3347 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3348 pending_contents_scale,
3349 device_scale_factor,
3351 maximum_animation_contents_scale,
3352 animating_transform);
3353 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3354 active_contents_scale,
3355 device_scale_factor,
3357 maximum_animation_contents_scale,
3358 animating_transform);
3360 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3361 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3363 EXPECT_EQ(active_contents_scale,
3364 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3365 EXPECT_EQ(pending_contents_scale,
3366 pending_layer_->tilings()->tiling_at(1)->contents_scale());
3367 EXPECT_EQ(active_contents_scale * low_res_factor,
3368 pending_layer_->tilings()->tiling_at(2)->contents_scale());
3369 EXPECT_EQ(pending_contents_scale * low_res_factor,
3370 pending_layer_->tilings()->tiling_at(3)->contents_scale());
3372 EXPECT_EQ(active_contents_scale,
3373 active_layer_->tilings()->tiling_at(0)->contents_scale());
3374 EXPECT_EQ(pending_contents_scale,
3375 active_layer_->tilings()->tiling_at(1)->contents_scale());
3376 EXPECT_EQ(active_contents_scale * low_res_factor,
3377 active_layer_->tilings()->tiling_at(2)->contents_scale());
3378 EXPECT_EQ(pending_contents_scale * low_res_factor,
3379 active_layer_->tilings()->tiling_at(3)->contents_scale());
3381 // The unused low res tiling from the pending tree must be kept or we may add
3382 // it again on the active tree and collide with the pending tree.
3383 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3384 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3385 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3387 EXPECT_EQ(active_contents_scale,
3388 active_layer_->tilings()->tiling_at(0)->contents_scale());
3389 EXPECT_EQ(pending_contents_scale,
3390 active_layer_->tilings()->tiling_at(1)->contents_scale());
3391 EXPECT_EQ(active_contents_scale * low_res_factor,
3392 active_layer_->tilings()->tiling_at(2)->contents_scale());
3393 EXPECT_EQ(pending_contents_scale * low_res_factor,
3394 active_layer_->tilings()->tiling_at(3)->contents_scale());
3397 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3398 gfx::Size tile_size(400, 400);
3399 gfx::Size layer_bounds(1300, 1900);
3401 scoped_refptr<FakePicturePileImpl> pending_pile =
3402 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3403 scoped_refptr<FakePicturePileImpl> active_pile =
3404 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3406 SetupTrees(pending_pile, active_pile);
3407 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3409 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3410 1.3f, // ideal contents scale
3411 2.7f, // device scale
3413 1.f, // maximum animation scale
3415 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3417 // All tilings should be removed when losing output surface.
3418 active_layer_->ReleaseResources();
3419 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3420 pending_layer_->ReleaseResources();
3421 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3423 // This should create new tilings.
3424 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3425 1.3f, // ideal contents scale
3426 2.7f, // device scale
3428 1.f, // maximum animation scale
3430 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3433 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3434 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3435 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3437 gfx::Size tile_size(400, 400);
3438 gfx::Size layer_bounds(1000, 2000);
3440 scoped_refptr<FakePicturePileImpl> pending_pile =
3441 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3442 scoped_refptr<FakePicturePileImpl> active_pile =
3443 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3445 SetupTrees(pending_pile, active_pile);
3447 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3448 host_impl_.pending_tree()->UpdateDrawProperties();
3450 active_layer_->draw_properties().visible_content_rect =
3451 gfx::Rect(layer_bounds);
3452 host_impl_.active_tree()->UpdateDrawProperties();
3454 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3455 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3456 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3457 SK_MScalar1 / max_contents_scale);
3459 AppendQuadsData data;
3460 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3462 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3463 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3464 // The content_to_target_transform should be scaled by the
3465 // MaximumTilingContentsScale on the layer.
3466 EXPECT_EQ(scaled_draw_transform.ToString(),
3467 render_pass->shared_quad_state_list[0]
3468 ->content_to_target_transform.ToString());
3469 // The content_bounds should be scaled by the
3470 // MaximumTilingContentsScale on the layer.
3471 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3472 render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3473 // The visible_content_rect should be scaled by the
3474 // MaximumTilingContentsScale on the layer.
3476 gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3477 render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3480 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3481 gfx::Size tile_size(400, 400);
3482 gfx::Size bounds(100000, 100);
3484 host_impl_.CreatePendingTree();
3486 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3488 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3489 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3491 layer_with_mask->SetBounds(bounds);
3492 layer_with_mask->SetContentBounds(bounds);
3494 scoped_refptr<FakePicturePileImpl> pending_pile =
3495 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3496 pending_pile->set_is_mask(true);
3497 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3498 host_impl_.pending_tree(), 3, pending_pile);
3500 mask->SetBounds(bounds);
3501 mask->SetContentBounds(bounds);
3502 mask->SetDrawsContent(true);
3504 FakePictureLayerImpl* pending_mask_content = mask.get();
3505 layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3507 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3508 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3510 child_of_layer_with_mask->SetBounds(bounds);
3511 child_of_layer_with_mask->SetContentBounds(bounds);
3512 child_of_layer_with_mask->SetDrawsContent(true);
3514 layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3516 root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3518 host_impl_.pending_tree()->SetRootLayer(root.Pass());
3520 EXPECT_FALSE(pending_mask_content->tilings());
3521 host_impl_.pending_tree()->UpdateDrawProperties();
3522 EXPECT_NE(0u, pending_mask_content->num_tilings());
3525 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3527 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3529 virtual void InitializeRenderer() OVERRIDE {
3530 host_impl_.InitializeRenderer(
3531 FakeOutputSurface::CreateDelegating3d().PassAs<OutputSurface>());
3535 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3536 DelegatingRendererWithTileOOM) {
3537 // This test is added for crbug.com/402321, where quad should be produced when
3538 // raster on demand is not allowed and tile is OOM.
3539 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3540 gfx::Size layer_bounds(1000, 1000);
3543 scoped_refptr<FakePicturePileImpl> pending_pile =
3544 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3545 SetupPendingTree(pending_pile);
3546 pending_layer_->SetBounds(layer_bounds);
3547 host_impl_.SetViewportSize(layer_bounds);
3549 host_impl_.active_tree()->UpdateDrawProperties();
3550 std::vector<Tile*> tiles =
3551 active_layer_->HighResTiling()->AllTilesForTesting();
3552 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3554 // Force tiles after max_tiles to be OOM. TileManager uses
3555 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3556 // directly set state to host_impl_, so we set policy that would change the
3557 // state. We also need to update tree priority separately.
3558 GlobalStateThatImpactsTilePriority state;
3559 size_t max_tiles = 1;
3560 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3561 size_t resource_limit = max_tiles;
3562 ManagedMemoryPolicy policy(memory_limit,
3563 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3565 host_impl_.SetMemoryPolicy(policy);
3566 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3567 host_impl_.ManageTiles();
3569 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3570 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3571 AppendQuadsData data;
3572 active_layer_->WillDraw(DRAW_MODE_HARDWARE, NULL);
3573 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3574 active_layer_->DidDraw(NULL);
3576 // Even when OOM, quads should be produced, and should be different material
3577 // from quads with resource.
3578 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3579 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3580 render_pass->quad_list.front()->material);
3581 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3582 render_pass->quad_list.back()->material);
3585 class OcclusionTrackingSettings : public LowResTilingsSettings {
3587 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3590 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3592 OcclusionTrackingPictureLayerImplTest()
3593 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3595 void VerifyEvictionConsidersOcclusion(
3596 PictureLayerImpl* layer,
3597 size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3598 for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3600 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3601 size_t occluded_tile_count = 0u;
3602 Tile* last_tile = NULL;
3604 for (PictureLayerImpl::LayerEvictionTileIterator it =
3605 PictureLayerImpl::LayerEvictionTileIterator(layer,
3613 // The only way we will encounter an occluded tile after an unoccluded
3614 // tile is if the priorty bin decreased, the tile is required for
3615 // activation, or the scale changed.
3616 bool tile_is_occluded =
3617 tile->is_occluded_for_tree_priority(tree_priority);
3618 if (tile_is_occluded) {
3619 occluded_tile_count++;
3621 bool last_tile_is_occluded =
3622 last_tile->is_occluded_for_tree_priority(tree_priority);
3623 if (!last_tile_is_occluded) {
3624 TilePriority::PriorityBin tile_priority_bin =
3625 tile->priority_for_tree_priority(tree_priority).priority_bin;
3626 TilePriority::PriorityBin last_tile_priority_bin =
3627 last_tile->priority_for_tree_priority(tree_priority)
3631 (tile_priority_bin < last_tile_priority_bin) ||
3632 tile->required_for_activation() ||
3633 (tile->contents_scale() != last_tile->contents_scale()));
3638 EXPECT_EQ(expected_occluded_tile_count[priority_count],
3639 occluded_tile_count);
3644 TEST_F(OcclusionTrackingPictureLayerImplTest,
3645 OccludedTilesSkippedDuringRasterization) {
3646 base::TimeTicks time_ticks;
3647 time_ticks += base::TimeDelta::FromMilliseconds(1);
3648 host_impl_.SetCurrentBeginFrameArgs(
3649 CreateBeginFrameArgsForTesting(time_ticks));
3651 gfx::Size tile_size(102, 102);
3652 gfx::Size layer_bounds(1000, 1000);
3653 gfx::Size viewport_size(500, 500);
3654 gfx::Point occluding_layer_position(310, 0);
3656 scoped_refptr<FakePicturePileImpl> pending_pile =
3657 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3658 SetupPendingTree(pending_pile);
3659 pending_layer_->set_fixed_tile_size(tile_size);
3661 host_impl_.SetViewportSize(viewport_size);
3662 host_impl_.pending_tree()->UpdateDrawProperties();
3665 int unoccluded_tile_count = 0;
3666 for (PictureLayerImpl::LayerRasterTileIterator it =
3667 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3672 // Occluded tiles should not be iterated over.
3673 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3675 // Some tiles may not be visible (i.e. outside the viewport). The rest are
3676 // visible and at least partially unoccluded, verified by the above expect.
3677 bool tile_is_visible =
3678 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3679 if (tile_is_visible)
3680 unoccluded_tile_count++;
3682 EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3684 // Partial occlusion.
3685 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3686 LayerImpl* layer1 = pending_layer_->children()[0];
3687 layer1->SetBounds(layer_bounds);
3688 layer1->SetContentBounds(layer_bounds);
3689 layer1->SetDrawsContent(true);
3690 layer1->SetContentsOpaque(true);
3691 layer1->SetPosition(occluding_layer_position);
3693 time_ticks += base::TimeDelta::FromMilliseconds(200);
3694 host_impl_.SetCurrentBeginFrameArgs(
3695 CreateBeginFrameArgsForTesting(time_ticks));
3696 host_impl_.pending_tree()->UpdateDrawProperties();
3698 unoccluded_tile_count = 0;
3699 for (PictureLayerImpl::LayerRasterTileIterator it =
3700 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3705 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3707 bool tile_is_visible =
3708 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3709 if (tile_is_visible)
3710 unoccluded_tile_count++;
3712 EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3715 layer1->SetPosition(gfx::Point(0, 0));
3717 time_ticks += base::TimeDelta::FromMilliseconds(200);
3718 host_impl_.SetCurrentBeginFrameArgs(
3719 CreateBeginFrameArgsForTesting(time_ticks));
3720 host_impl_.pending_tree()->UpdateDrawProperties();
3722 unoccluded_tile_count = 0;
3723 for (PictureLayerImpl::LayerRasterTileIterator it =
3724 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3729 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3731 bool tile_is_visible =
3732 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3733 if (tile_is_visible)
3734 unoccluded_tile_count++;
3736 EXPECT_EQ(unoccluded_tile_count, 0);
3739 TEST_F(OcclusionTrackingPictureLayerImplTest,
3740 OccludedTilesNotMarkedAsRequired) {
3741 base::TimeTicks time_ticks;
3742 time_ticks += base::TimeDelta::FromMilliseconds(1);
3743 host_impl_.SetCurrentBeginFrameArgs(
3744 CreateBeginFrameArgsForTesting(time_ticks));
3746 gfx::Size tile_size(102, 102);
3747 gfx::Size layer_bounds(1000, 1000);
3748 gfx::Size viewport_size(500, 500);
3749 gfx::Point occluding_layer_position(310, 0);
3751 scoped_refptr<FakePicturePileImpl> pending_pile =
3752 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3753 SetupPendingTree(pending_pile);
3754 pending_layer_->set_fixed_tile_size(tile_size);
3756 host_impl_.SetViewportSize(viewport_size);
3757 host_impl_.pending_tree()->UpdateDrawProperties();
3760 int occluded_tile_count = 0;
3761 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3762 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3764 occluded_tile_count = 0;
3765 for (PictureLayerTiling::CoverageIterator iter(
3767 pending_layer_->contents_scale_x(),
3768 gfx::Rect(layer_bounds));
3773 const Tile* tile = *iter;
3775 // Fully occluded tiles are not required for activation.
3776 if (tile->is_occluded(PENDING_TREE)) {
3777 EXPECT_FALSE(tile->required_for_activation());
3778 occluded_tile_count++;
3781 EXPECT_EQ(occluded_tile_count, 0);
3784 // Partial occlusion.
3785 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3786 LayerImpl* layer1 = pending_layer_->children()[0];
3787 layer1->SetBounds(layer_bounds);
3788 layer1->SetContentBounds(layer_bounds);
3789 layer1->SetDrawsContent(true);
3790 layer1->SetContentsOpaque(true);
3791 layer1->SetPosition(occluding_layer_position);
3793 time_ticks += base::TimeDelta::FromMilliseconds(200);
3794 host_impl_.SetCurrentBeginFrameArgs(
3795 CreateBeginFrameArgsForTesting(time_ticks));
3796 host_impl_.pending_tree()->UpdateDrawProperties();
3798 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3799 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3801 occluded_tile_count = 0;
3802 for (PictureLayerTiling::CoverageIterator iter(
3804 pending_layer_->contents_scale_x(),
3805 gfx::Rect(layer_bounds));
3810 const Tile* tile = *iter;
3812 if (tile->is_occluded(PENDING_TREE)) {
3813 EXPECT_FALSE(tile->required_for_activation());
3814 occluded_tile_count++;
3819 EXPECT_EQ(occluded_tile_count, 5);
3822 EXPECT_EQ(occluded_tile_count, 2);
3830 layer1->SetPosition(gfx::PointF(0, 0));
3832 time_ticks += base::TimeDelta::FromMilliseconds(200);
3833 host_impl_.SetCurrentBeginFrameArgs(
3834 CreateBeginFrameArgsForTesting(time_ticks));
3835 host_impl_.pending_tree()->UpdateDrawProperties();
3837 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3838 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3840 occluded_tile_count = 0;
3841 for (PictureLayerTiling::CoverageIterator iter(
3843 pending_layer_->contents_scale_x(),
3844 gfx::Rect(layer_bounds));
3849 const Tile* tile = *iter;
3851 if (tile->is_occluded(PENDING_TREE)) {
3852 EXPECT_FALSE(tile->required_for_activation());
3853 occluded_tile_count++;
3858 EXPECT_EQ(occluded_tile_count, 25);
3861 EXPECT_EQ(occluded_tile_count, 4);
3869 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3870 gfx::Size tile_size(102, 102);
3871 gfx::Size layer_bounds(1000, 1000);
3872 gfx::Size viewport_size(500, 500);
3873 gfx::Point occluding_layer_position(310, 0);
3875 scoped_refptr<FakePicturePileImpl> pending_pile =
3876 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3877 SetupPendingTree(pending_pile);
3878 pending_layer_->set_fixed_tile_size(tile_size);
3880 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3882 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3884 std::vector<PictureLayerTiling*> tilings;
3885 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3886 tilings.push_back(pending_layer_->AddTiling(0.3f));
3887 tilings.push_back(pending_layer_->AddTiling(0.7f));
3888 tilings.push_back(pending_layer_->AddTiling(1.0f));
3889 tilings.push_back(pending_layer_->AddTiling(2.0f));
3891 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3892 LayerImpl* layer1 = pending_layer_->children()[0];
3893 layer1->SetBounds(layer_bounds);
3894 layer1->SetContentBounds(layer_bounds);
3895 layer1->SetDrawsContent(true);
3896 layer1->SetContentsOpaque(true);
3897 layer1->SetPosition(occluding_layer_position);
3899 host_impl_.SetViewportSize(viewport_size);
3900 host_impl_.pending_tree()->UpdateDrawProperties();
3902 int tiling_count = 0;
3903 int occluded_tile_count = 0;
3904 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3906 tiling_iterator != tilings.end();
3907 ++tiling_iterator) {
3908 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3910 occluded_tile_count = 0;
3911 for (size_t i = 0; i < tiles.size(); ++i) {
3912 if (tiles[i]->is_occluded(PENDING_TREE)) {
3913 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3914 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
3915 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
3916 occluded_tile_count++;
3919 switch (tiling_count) {
3922 EXPECT_EQ(occluded_tile_count, 2);
3925 EXPECT_EQ(occluded_tile_count, 4);
3928 EXPECT_EQ(occluded_tile_count, 5);
3931 EXPECT_EQ(occluded_tile_count, 30);
3940 EXPECT_EQ(tiling_count, 5);
3943 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
3944 gfx::Size tile_size(102, 102);
3945 gfx::Size layer_bounds(1000, 1000);
3946 gfx::Size viewport_size(1000, 1000);
3947 gfx::Point occluding_layer_position(310, 0);
3948 gfx::Rect invalidation_rect(230, 230, 102, 102);
3950 scoped_refptr<FakePicturePileImpl> pending_pile =
3951 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3952 scoped_refptr<FakePicturePileImpl> active_pile =
3953 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3954 SetupTrees(pending_pile, active_pile);
3956 // Partially occlude the active layer.
3957 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3958 LayerImpl* layer1 = active_layer_->children()[0];
3959 layer1->SetBounds(layer_bounds);
3960 layer1->SetContentBounds(layer_bounds);
3961 layer1->SetDrawsContent(true);
3962 layer1->SetContentsOpaque(true);
3963 layer1->SetPosition(occluding_layer_position);
3965 // Partially invalidate the pending layer.
3966 pending_layer_->set_invalidation(invalidation_rect);
3968 host_impl_.SetViewportSize(viewport_size);
3970 active_layer_->CreateDefaultTilingsAndTiles();
3971 pending_layer_->CreateDefaultTilingsAndTiles();
3973 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3974 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3976 for (PictureLayerTiling::CoverageIterator iter(
3978 pending_layer_->contents_scale_x(),
3979 gfx::Rect(layer_bounds));
3984 const Tile* tile = *iter;
3986 // All tiles are unoccluded on the pending tree.
3987 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3990 pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3991 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3992 tile->content_rect(), 1.0f / tile->contents_scale());
3994 if (scaled_content_rect.Intersects(invalidation_rect)) {
3995 // Tiles inside the invalidation rect are only on the pending tree.
3996 EXPECT_NE(tile, twin_tile);
3998 // Unshared tiles should be unoccluded on the active tree by default.
3999 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
4001 // Tiles outside the invalidation rect are shared between both trees.
4002 EXPECT_EQ(tile, twin_tile);
4003 // Shared tiles are occluded on the active tree iff they lie beneath the
4005 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4006 scaled_content_rect.x() >= occluding_layer_position.x());
4011 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4012 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4014 for (PictureLayerTiling::CoverageIterator iter(
4016 active_layer_->contents_scale_x(),
4017 gfx::Rect(layer_bounds));
4022 const Tile* tile = *iter;
4025 active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
4026 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
4027 tile->content_rect(), 1.0f / tile->contents_scale());
4029 // Since we've already checked the shared tiles, only consider tiles in
4030 // the invalidation rect.
4031 if (scaled_content_rect.Intersects(invalidation_rect)) {
4032 // Tiles inside the invalidation rect are only on the active tree.
4033 EXPECT_NE(tile, twin_tile);
4035 // Unshared tiles should be unoccluded on the pending tree by default.
4036 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
4038 // Unshared tiles are occluded on the active tree iff they lie beneath
4039 // the occluding layer.
4040 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
4041 scaled_content_rect.x() >= occluding_layer_position.x());
4047 TEST_F(OcclusionTrackingPictureLayerImplTest,
4048 OccludedTilesConsideredDuringEviction) {
4049 gfx::Size tile_size(102, 102);
4050 gfx::Size layer_bounds(1000, 1000);
4051 gfx::Size viewport_size(500, 500);
4052 gfx::Point pending_occluding_layer_position(310, 0);
4053 gfx::Point active_occluding_layer_position(0, 310);
4054 gfx::Rect invalidation_rect(230, 230, 102, 102);
4056 scoped_refptr<FakePicturePileImpl> pending_pile =
4057 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4058 scoped_refptr<FakePicturePileImpl> active_pile =
4059 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4060 SetupTrees(pending_pile, active_pile);
4062 pending_layer_->set_fixed_tile_size(tile_size);
4063 active_layer_->set_fixed_tile_size(tile_size);
4065 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
4067 std::vector<PictureLayerTiling*> tilings;
4068 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
4069 tilings.push_back(pending_layer_->AddTiling(0.3f));
4070 tilings.push_back(pending_layer_->AddTiling(0.7f));
4071 tilings.push_back(pending_layer_->AddTiling(1.0f));
4072 tilings.push_back(pending_layer_->AddTiling(2.0f));
4074 EXPECT_EQ(5u, pending_layer_->num_tilings());
4075 EXPECT_EQ(5u, active_layer_->num_tilings());
4077 // Partially occlude the pending layer.
4078 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
4079 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
4080 pending_occluding_layer->SetBounds(layer_bounds);
4081 pending_occluding_layer->SetContentBounds(layer_bounds);
4082 pending_occluding_layer->SetDrawsContent(true);
4083 pending_occluding_layer->SetContentsOpaque(true);
4084 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
4086 // Partially occlude the active layer.
4087 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
4088 LayerImpl* active_occluding_layer = active_layer_->children()[0];
4089 active_occluding_layer->SetBounds(layer_bounds);
4090 active_occluding_layer->SetContentBounds(layer_bounds);
4091 active_occluding_layer->SetDrawsContent(true);
4092 active_occluding_layer->SetContentsOpaque(true);
4093 active_occluding_layer->SetPosition(active_occluding_layer_position);
4095 // Partially invalidate the pending layer. Tiles inside the invalidation rect
4096 // are not shared between trees.
4097 pending_layer_->set_invalidation(invalidation_rect);
4099 host_impl_.SetViewportSize(viewport_size);
4100 host_impl_.active_tree()->UpdateDrawProperties();
4101 host_impl_.pending_tree()->UpdateDrawProperties();
4103 // The expected number of occluded tiles on each of the 5 tilings for each of
4104 // the 3 tree priorities.
4105 size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
4106 size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
4107 size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
4109 // The total expected number of occluded tiles on all tilings for each of the
4110 // 3 tree priorities.
4111 size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
4113 ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
4115 // Verify number of occluded tiles on the pending layer for each tiling.
4116 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
4117 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
4118 tiling->CreateAllTilesForTesting();
4120 size_t occluded_tile_count_on_pending = 0u;
4121 size_t occluded_tile_count_on_active = 0u;
4122 size_t occluded_tile_count_on_both = 0u;
4123 for (PictureLayerTiling::CoverageIterator iter(
4125 pending_layer_->contents_scale_x(),
4126 gfx::Rect(layer_bounds));
4131 if (tile->is_occluded(PENDING_TREE))
4132 occluded_tile_count_on_pending++;
4133 if (tile->is_occluded(ACTIVE_TREE))
4134 occluded_tile_count_on_active++;
4135 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4136 occluded_tile_count_on_both++;
4138 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4139 occluded_tile_count_on_pending)
4141 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4142 occluded_tile_count_on_active)
4144 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4145 occluded_tile_count_on_both)
4149 // Verify number of occluded tiles on the active layer for each tiling.
4150 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4151 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4152 tiling->CreateAllTilesForTesting();
4154 size_t occluded_tile_count_on_pending = 0u;
4155 size_t occluded_tile_count_on_active = 0u;
4156 size_t occluded_tile_count_on_both = 0u;
4157 for (PictureLayerTiling::CoverageIterator iter(
4159 pending_layer_->contents_scale_x(),
4160 gfx::Rect(layer_bounds));
4165 if (tile->is_occluded(PENDING_TREE))
4166 occluded_tile_count_on_pending++;
4167 if (tile->is_occluded(ACTIVE_TREE))
4168 occluded_tile_count_on_active++;
4169 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4170 occluded_tile_count_on_both++;
4172 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4173 occluded_tile_count_on_pending)
4175 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4176 occluded_tile_count_on_active)
4178 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4179 occluded_tile_count_on_both)
4183 std::vector<Tile*> all_tiles;
4184 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4186 tiling_iterator != tilings.end();
4187 ++tiling_iterator) {
4188 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4189 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4192 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4194 VerifyEvictionConsidersOcclusion(pending_layer_,
4195 total_expected_occluded_tile_count);
4196 VerifyEvictionConsidersOcclusion(active_layer_,
4197 total_expected_occluded_tile_count);
4200 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4201 gfx::Size tile_size(102, 102);
4202 gfx::Size layer_bounds(1000, 1000);
4204 scoped_refptr<FakePicturePileImpl> pile =
4205 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4206 SetupPendingTree(pile);
4207 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4210 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4211 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4213 SetupPendingTree(pile);
4214 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4215 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4218 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4219 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4221 host_impl_.ResetRecycleTreeForTesting();
4222 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4225 void PictureLayerImplTest::TestQuadsForSolidColor(bool test_for_solid) {
4226 base::TimeTicks time_ticks;
4227 time_ticks += base::TimeDelta::FromMilliseconds(1);
4228 host_impl_.SetCurrentBeginFrameArgs(
4229 CreateBeginFrameArgsForTesting(time_ticks));
4231 gfx::Size tile_size(100, 100);
4232 gfx::Size layer_bounds(200, 200);
4233 gfx::Rect layer_rect(layer_bounds);
4235 FakeContentLayerClient client;
4236 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4237 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4238 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4239 host->SetRootLayer(layer);
4240 PicturePile* pile = layer->GetPicturePileForTesting();
4242 host_impl_.SetViewportSize(layer_bounds);
4244 int frame_number = 0;
4245 FakeRenderingStatsInstrumentation stats_instrumentation;
4247 client.set_fill_with_nonsolid_color(!test_for_solid);
4249 Region invalidation(layer_rect);
4250 pile->UpdateAndExpandInvalidation(&client,
4258 Picture::RECORD_NORMALLY,
4259 &stats_instrumentation);
4261 scoped_refptr<PicturePileImpl> pending_pile =
4262 PicturePileImpl::CreateFromOther(pile);
4264 SetupPendingTree(pending_pile);
4267 if (test_for_solid) {
4268 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
4270 ASSERT_TRUE(active_layer_->tilings());
4271 active_layer_->set_fixed_tile_size(tile_size);
4272 host_impl_.active_tree()->UpdateDrawProperties();
4273 ASSERT_GT(active_layer_->tilings()->num_tilings(), 0u);
4274 std::vector<Tile*> tiles =
4275 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
4276 EXPECT_FALSE(tiles.empty());
4277 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
4280 MockOcclusionTracker<LayerImpl> occlusion_tracker;
4281 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
4282 AppendQuadsData data;
4283 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
4284 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
4285 active_layer_->DidDraw(NULL);
4287 DrawQuad::Material expected = test_for_solid
4288 ? DrawQuad::Material::SOLID_COLOR
4289 : DrawQuad::Material::TILED_CONTENT;
4290 EXPECT_EQ(expected, render_pass->quad_list.front()->material);
4293 TEST_F(PictureLayerImplTest, DrawSolidQuads) {
4294 TestQuadsForSolidColor(true);
4297 TEST_F(PictureLayerImplTest, DrawNonSolidQuads) {
4298 TestQuadsForSolidColor(false);
4301 TEST_F(PictureLayerImplTest, NonSolidToSolidNoTilings) {
4302 base::TimeTicks time_ticks;
4303 time_ticks += base::TimeDelta::FromMilliseconds(1);
4304 host_impl_.SetCurrentBeginFrameArgs(
4305 CreateBeginFrameArgsForTesting(time_ticks));
4307 gfx::Size tile_size(100, 100);
4308 gfx::Size layer_bounds(200, 200);
4309 gfx::Rect layer_rect(layer_bounds);
4311 FakeContentLayerClient client;
4312 scoped_refptr<PictureLayer> layer = PictureLayer::Create(&client);
4313 FakeLayerTreeHostClient host_client(FakeLayerTreeHostClient::DIRECT_3D);
4314 scoped_ptr<FakeLayerTreeHost> host = FakeLayerTreeHost::Create(&host_client);
4315 host->SetRootLayer(layer);
4316 PicturePile* pile = layer->GetPicturePileForTesting();
4318 host_impl_.SetViewportSize(layer_bounds);
4320 int frame_number = 0;
4321 FakeRenderingStatsInstrumentation stats_instrumentation;
4323 client.set_fill_with_nonsolid_color(true);
4325 Region invalidation1(layer_rect);
4326 pile->UpdateAndExpandInvalidation(&client,
4334 Picture::RECORD_NORMALLY,
4335 &stats_instrumentation);
4337 scoped_refptr<PicturePileImpl> pending_pile1 =
4338 PicturePileImpl::CreateFromOther(pile);
4340 SetupPendingTree(pending_pile1);
4342 host_impl_.active_tree()->UpdateDrawProperties();
4344 // We've started with a solid layer that contains some tilings.
4345 ASSERT_TRUE(active_layer_->tilings());
4346 EXPECT_NE(0u, active_layer_->tilings()->num_tilings());
4348 client.set_fill_with_nonsolid_color(false);
4350 Region invalidation2(layer_rect);
4351 pile->UpdateAndExpandInvalidation(&client,
4359 Picture::RECORD_NORMALLY,
4360 &stats_instrumentation);
4362 scoped_refptr<PicturePileImpl> pending_pile2 =
4363 PicturePileImpl::CreateFromOther(pile);
4365 SetupPendingTree(pending_pile2);
4368 // We've switched to a solid color, so we should end up with no tilings.
4369 ASSERT_TRUE(active_layer_->tilings());
4370 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());