Remove NavigationRequestInfo::is_showing.
[chromium-blink-merge.git] / cc / layers / picture_layer_impl_unittest.cc
blob249dcf02f14fa61f4267325475bf4f9f76b41554
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/picture_layer_impl.h"
7 #include <algorithm>
8 #include <limits>
9 #include <set>
10 #include <utility>
12 #include "cc/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"
34 namespace cc {
35 namespace {
37 class MockCanvas : public SkCanvas {
38 public:
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 PictureLayerImplTest : public testing::Test {
50 public:
51 PictureLayerImplTest()
52 : proxy_(base::MessageLoopProxy::current()),
53 host_impl_(ImplSidePaintingSettings(),
54 &proxy_,
55 &shared_bitmap_manager_),
56 id_(7),
57 pending_layer_(NULL),
58 old_pending_layer_(NULL),
59 active_layer_(NULL) {}
61 explicit PictureLayerImplTest(const LayerTreeSettings& settings)
62 : proxy_(base::MessageLoopProxy::current()),
63 host_impl_(settings, &proxy_, &shared_bitmap_manager_),
64 id_(7) {}
66 virtual ~PictureLayerImplTest() {
69 virtual void SetUp() OVERRIDE {
70 InitializeRenderer();
73 virtual void InitializeRenderer() {
74 host_impl_.InitializeRenderer(
75 FakeOutputSurface::Create3d().PassAs<OutputSurface>());
78 void SetupDefaultTrees(const gfx::Size& layer_bounds) {
79 gfx::Size tile_size(100, 100);
81 scoped_refptr<FakePicturePileImpl> pending_pile =
82 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
83 scoped_refptr<FakePicturePileImpl> active_pile =
84 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
86 SetupTrees(pending_pile, active_pile);
89 void ActivateTree() {
90 host_impl_.ActivateSyncTree();
91 CHECK(!host_impl_.pending_tree());
92 CHECK(host_impl_.recycle_tree());
93 old_pending_layer_ = pending_layer_;
94 pending_layer_ = NULL;
95 active_layer_ = static_cast<FakePictureLayerImpl*>(
96 host_impl_.active_tree()->LayerById(id_));
99 void SetupDefaultTreesWithFixedTileSize(const gfx::Size& layer_bounds,
100 const gfx::Size& tile_size) {
101 SetupDefaultTrees(layer_bounds);
102 pending_layer_->set_fixed_tile_size(tile_size);
103 active_layer_->set_fixed_tile_size(tile_size);
106 void SetupTrees(
107 scoped_refptr<PicturePileImpl> pending_pile,
108 scoped_refptr<PicturePileImpl> active_pile) {
109 SetupPendingTree(active_pile);
110 ActivateTree();
111 SetupPendingTree(pending_pile);
112 host_impl_.pending_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
113 host_impl_.active_tree()->SetPageScaleFactorAndLimits(1.f, 0.25f, 100.f);
116 void CreateHighLowResAndSetAllTilesVisible() {
117 // Active layer must get updated first so pending layer can share from it.
118 active_layer_->CreateDefaultTilingsAndTiles();
119 active_layer_->SetAllTilesVisible();
120 pending_layer_->CreateDefaultTilingsAndTiles();
121 pending_layer_->SetAllTilesVisible();
124 void AddDefaultTilingsWithInvalidation(const Region& invalidation) {
125 active_layer_->AddTiling(2.3f);
126 active_layer_->AddTiling(1.0f);
127 active_layer_->AddTiling(0.5f);
128 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i)
129 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
130 pending_layer_->set_invalidation(invalidation);
131 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i)
132 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting();
135 void SetupPendingTree(scoped_refptr<PicturePileImpl> pile) {
136 host_impl_.CreatePendingTree();
137 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
138 // Clear recycled tree.
139 pending_tree->DetachLayerTree();
141 scoped_ptr<FakePictureLayerImpl> pending_layer =
142 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile);
143 pending_layer->SetDrawsContent(true);
144 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
146 pending_layer_ = static_cast<FakePictureLayerImpl*>(
147 host_impl_.pending_tree()->LayerById(id_));
148 pending_layer_->DoPostCommitInitializationIfNeeded();
151 void SetupDrawPropertiesAndUpdateTiles(FakePictureLayerImpl* layer,
152 float ideal_contents_scale,
153 float device_scale_factor,
154 float page_scale_factor,
155 float maximum_animation_contents_scale,
156 bool animating_transform_to_screen) {
157 layer->draw_properties().ideal_contents_scale = ideal_contents_scale;
158 layer->draw_properties().device_scale_factor = device_scale_factor;
159 layer->draw_properties().page_scale_factor = page_scale_factor;
160 layer->draw_properties().maximum_animation_contents_scale =
161 maximum_animation_contents_scale;
162 layer->draw_properties().screen_space_transform_is_animating =
163 animating_transform_to_screen;
164 layer->UpdateTiles(NULL);
166 static void VerifyAllTilesExistAndHavePile(
167 const PictureLayerTiling* tiling,
168 PicturePileImpl* pile) {
169 for (PictureLayerTiling::CoverageIterator iter(
170 tiling,
171 tiling->contents_scale(),
172 gfx::Rect(tiling->tiling_size()));
173 iter;
174 ++iter) {
175 EXPECT_TRUE(*iter);
176 EXPECT_EQ(pile, iter->picture_pile());
180 void SetContentsScaleOnBothLayers(float contents_scale,
181 float device_scale_factor,
182 float page_scale_factor,
183 float maximum_animation_contents_scale,
184 bool animating_transform) {
185 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
186 contents_scale,
187 device_scale_factor,
188 page_scale_factor,
189 maximum_animation_contents_scale,
190 animating_transform);
192 SetupDrawPropertiesAndUpdateTiles(active_layer_,
193 contents_scale,
194 device_scale_factor,
195 page_scale_factor,
196 maximum_animation_contents_scale,
197 animating_transform);
200 void ResetTilingsAndRasterScales() {
201 pending_layer_->ReleaseResources();
202 active_layer_->ReleaseResources();
205 void AssertAllTilesRequired(PictureLayerTiling* tiling) {
206 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
207 for (size_t i = 0; i < tiles.size(); ++i)
208 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i;
209 EXPECT_GT(tiles.size(), 0u);
212 void AssertNoTilesRequired(PictureLayerTiling* tiling) {
213 std::vector<Tile*> tiles = tiling->AllTilesForTesting();
214 for (size_t i = 0; i < tiles.size(); ++i)
215 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i;
216 EXPECT_GT(tiles.size(), 0u);
219 protected:
220 void TestTileGridAlignmentCommon() {
221 // Layer to span 4 raster tiles in x and in y
222 ImplSidePaintingSettings settings;
223 gfx::Size layer_size(
224 settings.default_tile_size.width() * 7 / 2,
225 settings.default_tile_size.height() * 7 / 2);
227 scoped_refptr<FakePicturePileImpl> pending_pile =
228 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
229 scoped_refptr<FakePicturePileImpl> active_pile =
230 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size);
232 SetupTrees(pending_pile, active_pile);
234 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
236 // Add 1x1 rects at the centers of each tile, then re-record pile contents
237 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
238 std::vector<Tile*> tiles =
239 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
240 EXPECT_EQ(16u, tiles.size());
241 std::vector<SkRect> rects;
242 std::vector<Tile*>::const_iterator tile_iter;
243 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
244 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint();
245 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1);
246 active_pile->add_draw_rect(rect);
247 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1));
249 // Force re-record with newly injected content
250 active_pile->RemoveRecordingAt(0, 0);
251 active_pile->AddRecordingAt(0, 0);
253 std::vector<SkRect>::const_iterator rect_iter = rects.begin();
254 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) {
255 MockCanvas mock_canvas(1000, 1000);
256 active_pile->RasterDirect(
257 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL);
259 // This test verifies that when drawing the contents of a specific tile
260 // at content scale 1.0, the playback canvas never receives content from
261 // neighboring tiles which indicates that the tile grid embedded in
262 // SkPicture is perfectly aligned with the compositor's tiles.
263 EXPECT_EQ(1u, mock_canvas.rects_.size());
264 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]);
265 rect_iter++;
269 FakeImplProxy proxy_;
270 TestSharedBitmapManager shared_bitmap_manager_;
271 FakeLayerTreeHostImpl host_impl_;
272 int id_;
273 FakePictureLayerImpl* pending_layer_;
274 FakePictureLayerImpl* old_pending_layer_;
275 FakePictureLayerImpl* active_layer_;
277 private:
278 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest);
281 TEST_F(PictureLayerImplTest, TileGridAlignment) {
282 host_impl_.SetDeviceScaleFactor(1.f);
283 TestTileGridAlignmentCommon();
286 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) {
287 host_impl_.SetDeviceScaleFactor(2.f);
288 TestTileGridAlignmentCommon();
291 TEST_F(PictureLayerImplTest, CloneNoInvalidation) {
292 gfx::Size tile_size(100, 100);
293 gfx::Size layer_bounds(400, 400);
295 scoped_refptr<FakePicturePileImpl> pending_pile =
296 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
297 scoped_refptr<FakePicturePileImpl> active_pile =
298 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
300 SetupTrees(pending_pile, active_pile);
302 Region invalidation;
303 AddDefaultTilingsWithInvalidation(invalidation);
305 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
306 active_layer_->tilings()->num_tilings());
308 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
309 EXPECT_GT(tilings->num_tilings(), 0u);
310 for (size_t i = 0; i < tilings->num_tilings(); ++i)
311 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get());
314 TEST_F(PictureLayerImplTest, ExternalViewportRectForPrioritizingTiles) {
315 base::TimeTicks time_ticks;
316 time_ticks += base::TimeDelta::FromMilliseconds(1);
317 host_impl_.SetCurrentBeginFrameArgs(
318 CreateBeginFrameArgsForTesting(time_ticks));
319 gfx::Size tile_size(100, 100);
320 gfx::Size layer_bounds(400, 400);
322 scoped_refptr<FakePicturePileImpl> pending_pile =
323 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
324 scoped_refptr<FakePicturePileImpl> active_pile =
325 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
327 SetupTrees(pending_pile, active_pile);
329 Region invalidation;
330 AddDefaultTilingsWithInvalidation(invalidation);
331 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
333 time_ticks += base::TimeDelta::FromMilliseconds(200);
334 host_impl_.SetCurrentBeginFrameArgs(
335 CreateBeginFrameArgsForTesting(time_ticks));
337 // Update tiles with viewport for tile priority as (0, 0, 100, 100) and the
338 // identify transform for tile priority.
339 bool resourceless_software_draw = false;
340 gfx::Rect viewport = gfx::Rect(layer_bounds),
341 viewport_rect_for_tile_priority = gfx::Rect(0, 0, 100, 100);
342 gfx::Transform transform, transform_for_tile_priority;
344 host_impl_.SetExternalDrawConstraints(transform,
345 viewport,
346 viewport,
347 viewport_rect_for_tile_priority,
348 transform_for_tile_priority,
349 resourceless_software_draw);
350 active_layer_->draw_properties().visible_content_rect = viewport;
351 active_layer_->draw_properties().screen_space_transform = transform;
352 active_layer_->UpdateTiles(NULL);
354 gfx::Rect viewport_rect_for_tile_priority_in_view_space =
355 viewport_rect_for_tile_priority;
357 // Verify the viewport rect for tile priority is used in picture layer impl.
358 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
359 viewport_rect_for_tile_priority_in_view_space);
361 // Verify the viewport rect for tile priority is used in picture layer tiling.
362 PictureLayerTilingSet* tilings = active_layer_->tilings();
363 for (size_t i = 0; i < tilings->num_tilings(); i++) {
364 PictureLayerTiling* tiling = tilings->tiling_at(i);
365 EXPECT_EQ(
366 tiling->GetCurrentVisibleRectForTesting(),
367 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
368 tiling->contents_scale()));
371 // Update tiles with viewport for tile priority as (200, 200, 100, 100) in
372 // screen space and the transform for tile priority is translated and
373 // rotated. The actual viewport for tile priority used by PictureLayerImpl
374 // should be (200, 200, 100, 100) applied with the said transform.
375 time_ticks += base::TimeDelta::FromMilliseconds(200);
376 host_impl_.SetCurrentBeginFrameArgs(
377 CreateBeginFrameArgsForTesting(time_ticks));
379 viewport_rect_for_tile_priority = gfx::Rect(200, 200, 100, 100);
380 transform_for_tile_priority.Translate(100, 100);
381 transform_for_tile_priority.Rotate(45);
382 host_impl_.SetExternalDrawConstraints(transform,
383 viewport,
384 viewport,
385 viewport_rect_for_tile_priority,
386 transform_for_tile_priority,
387 resourceless_software_draw);
388 active_layer_->draw_properties().visible_content_rect = viewport;
389 active_layer_->draw_properties().screen_space_transform = transform;
390 active_layer_->UpdateTiles(NULL);
392 gfx::Transform screen_to_view(gfx::Transform::kSkipInitialization);
393 bool success = transform_for_tile_priority.GetInverse(&screen_to_view);
394 EXPECT_TRUE(success);
396 viewport_rect_for_tile_priority_in_view_space =
397 gfx::ToEnclosingRect(MathUtil::ProjectClippedRect(
398 screen_to_view, viewport_rect_for_tile_priority));
400 // Verify the viewport rect for tile priority is used in PictureLayerImpl.
401 EXPECT_EQ(active_layer_->viewport_rect_for_tile_priority(),
402 viewport_rect_for_tile_priority_in_view_space);
404 // Interset viewport_rect_for_tile_priority_in_view_space with the layer
405 // bounds and the result should be used in PictureLayerTiling.
406 viewport_rect_for_tile_priority_in_view_space.Intersect(
407 gfx::Rect(layer_bounds));
408 tilings = active_layer_->tilings();
409 for (size_t i = 0; i < tilings->num_tilings(); i++) {
410 PictureLayerTiling* tiling = tilings->tiling_at(i);
411 EXPECT_EQ(
412 tiling->GetCurrentVisibleRectForTesting(),
413 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority_in_view_space,
414 tiling->contents_scale()));
418 TEST_F(PictureLayerImplTest,
419 ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
420 base::TimeTicks time_ticks;
421 time_ticks += base::TimeDelta::FromMilliseconds(1);
422 host_impl_.SetCurrentBeginFrameArgs(
423 CreateBeginFrameArgsForTesting(time_ticks));
425 gfx::Size tile_size(100, 100);
426 gfx::Size layer_bounds(400, 400);
428 scoped_refptr<FakePicturePileImpl> pending_pile =
429 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
430 scoped_refptr<FakePicturePileImpl> active_pile =
431 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
433 SetupTrees(pending_pile, active_pile);
435 Region invalidation;
436 AddDefaultTilingsWithInvalidation(invalidation);
437 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
439 // UpdateTiles with valid viewport. Should update tile viewport.
440 bool resourceless_software_draw = false;
441 gfx::Rect viewport = gfx::Rect(layer_bounds);
442 gfx::Transform transform;
443 host_impl_.SetExternalDrawConstraints(transform,
444 viewport,
445 viewport,
446 viewport,
447 transform,
448 resourceless_software_draw);
449 active_layer_->draw_properties().visible_content_rect = viewport;
450 active_layer_->draw_properties().screen_space_transform = transform;
451 active_layer_->UpdateTiles(NULL);
453 gfx::Rect visible_rect_for_tile_priority =
454 active_layer_->visible_rect_for_tile_priority();
455 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
456 gfx::Rect viewport_rect_for_tile_priority =
457 active_layer_->viewport_rect_for_tile_priority();
458 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
459 gfx::Transform screen_space_transform_for_tile_priority =
460 active_layer_->screen_space_transform_for_tile_priority();
462 // PictureLayerImpl does not make a special case for
463 // resource_less_software_draw, so the tile viewport and matrix should be
464 // respected.
465 time_ticks += base::TimeDelta::FromMilliseconds(200);
466 host_impl_.SetCurrentBeginFrameArgs(
467 CreateBeginFrameArgsForTesting(time_ticks));
468 resourceless_software_draw = true;
469 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
470 transform.Translate(1.f, 1.f);
471 active_layer_->draw_properties().visible_content_rect = viewport;
472 active_layer_->draw_properties().screen_space_transform = transform;
473 host_impl_.SetExternalDrawConstraints(transform,
474 viewport,
475 viewport,
476 viewport,
477 transform,
478 resourceless_software_draw);
479 active_layer_->UpdateTiles(NULL);
481 visible_rect_for_tile_priority =
482 gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
483 viewport_rect_for_tile_priority =
484 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
485 screen_space_transform_for_tile_priority = transform;
486 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
487 active_layer_->visible_rect_for_tile_priority());
488 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
489 active_layer_->viewport_rect_for_tile_priority());
490 EXPECT_TRANSFORMATION_MATRIX_EQ(
491 screen_space_transform_for_tile_priority,
492 active_layer_->screen_space_transform_for_tile_priority());
495 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) {
496 gfx::Size tile_size(100, 100);
497 gfx::Size layer_bounds(400, 400);
498 gfx::Rect layer_invalidation(150, 200, 30, 180);
500 scoped_refptr<FakePicturePileImpl> pending_pile =
501 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
502 scoped_refptr<FakePicturePileImpl> active_pile =
503 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
505 SetupTrees(pending_pile, active_pile);
507 Region invalidation(layer_invalidation);
508 AddDefaultTilingsWithInvalidation(invalidation);
510 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
511 EXPECT_GT(tilings->num_tilings(), 0u);
512 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
513 const PictureLayerTiling* tiling = tilings->tiling_at(i);
514 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect(
515 layer_invalidation,
516 tiling->contents_scale());
517 for (PictureLayerTiling::CoverageIterator iter(
518 tiling,
519 tiling->contents_scale(),
520 gfx::Rect(tiling->tiling_size()));
521 iter;
522 ++iter) {
523 EXPECT_TRUE(*iter);
524 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
525 if (iter.geometry_rect().Intersects(content_invalidation))
526 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
527 else
528 EXPECT_EQ(active_pile.get(), iter->picture_pile());
533 TEST_F(PictureLayerImplTest, CloneFullInvalidation) {
534 gfx::Size tile_size(90, 80);
535 gfx::Size layer_bounds(300, 500);
537 scoped_refptr<FakePicturePileImpl> pending_pile =
538 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
539 scoped_refptr<FakePicturePileImpl> active_pile =
540 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
542 SetupTrees(pending_pile, active_pile);
544 Region invalidation((gfx::Rect(layer_bounds)));
545 AddDefaultTilingsWithInvalidation(invalidation);
547 EXPECT_EQ(pending_layer_->tilings()->num_tilings(),
548 active_layer_->tilings()->num_tilings());
550 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
551 EXPECT_GT(tilings->num_tilings(), 0u);
552 for (size_t i = 0; i < tilings->num_tilings(); ++i)
553 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get());
556 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) {
557 gfx::Size tile_size(90, 80);
558 gfx::Size active_layer_bounds(300, 500);
559 gfx::Size pending_layer_bounds(400, 800);
561 scoped_refptr<FakePicturePileImpl> pending_pile =
562 FakePicturePileImpl::CreateFilledPile(tile_size,
563 pending_layer_bounds);
564 scoped_refptr<FakePicturePileImpl> active_pile =
565 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds);
567 SetupTrees(pending_pile, active_pile);
568 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100));
570 Region invalidation;
571 AddDefaultTilingsWithInvalidation(invalidation);
573 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
574 EXPECT_GT(tilings->num_tilings(), 0u);
575 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
576 const PictureLayerTiling* tiling = tilings->tiling_at(i);
577 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect(
578 gfx::Rect(active_layer_bounds),
579 tiling->contents_scale());
580 for (PictureLayerTiling::CoverageIterator iter(
581 tiling,
582 tiling->contents_scale(),
583 gfx::Rect(tiling->tiling_size()));
584 iter;
585 ++iter) {
586 EXPECT_TRUE(*iter);
587 EXPECT_FALSE(iter.geometry_rect().IsEmpty());
588 std::vector<Tile*> active_tiles =
589 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting();
590 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting();
591 if (iter.geometry_rect().right() >= active_content_bounds.width() ||
592 iter.geometry_rect().bottom() >= active_content_bounds.height() ||
593 active_tiles[0]->content_rect().size() !=
594 pending_tiles[0]->content_rect().size()) {
595 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
596 } else {
597 EXPECT_EQ(active_pile.get(), iter->picture_pile());
603 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) {
604 gfx::Size tile_size(400, 400);
605 gfx::Size layer_bounds(1300, 1900);
607 scoped_refptr<FakePicturePileImpl> pending_pile =
608 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
609 scoped_refptr<FakePicturePileImpl> active_pile =
610 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
612 // Fill in some of active pile, but more of pending pile.
613 int hole_count = 0;
614 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) {
615 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) {
616 if ((x + y) % 2) {
617 pending_pile->AddRecordingAt(x, y);
618 active_pile->AddRecordingAt(x, y);
619 } else {
620 hole_count++;
621 if (hole_count % 2)
622 pending_pile->AddRecordingAt(x, y);
627 SetupTrees(pending_pile, active_pile);
628 Region invalidation;
629 AddDefaultTilingsWithInvalidation(invalidation);
631 const PictureLayerTilingSet* tilings = pending_layer_->tilings();
632 EXPECT_GT(tilings->num_tilings(), 0u);
633 for (size_t i = 0; i < tilings->num_tilings(); ++i) {
634 const PictureLayerTiling* tiling = tilings->tiling_at(i);
636 for (PictureLayerTiling::CoverageIterator iter(
637 tiling,
638 tiling->contents_scale(),
639 gfx::Rect(tiling->tiling_size()));
640 iter;
641 ++iter) {
642 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty());
643 // Ensure there is a recording for this tile.
644 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(),
645 iter.full_tile_geometry_rect());
646 bool in_active = active_pile->CanRaster(tiling->contents_scale(),
647 iter.full_tile_geometry_rect());
649 if (in_pending && !in_active)
650 EXPECT_EQ(pending_pile.get(), iter->picture_pile());
651 else if (in_active)
652 EXPECT_EQ(active_pile.get(), iter->picture_pile());
653 else
654 EXPECT_FALSE(*iter);
659 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) {
660 gfx::Size tile_size(400, 400);
661 gfx::Size layer_bounds(1300, 1900);
663 scoped_refptr<FakePicturePileImpl> pending_pile =
664 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
665 scoped_refptr<FakePicturePileImpl> active_pile =
666 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
668 SetupTrees(pending_pile, active_pile);
670 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
672 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
675 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) {
676 gfx::Size tile_size(400, 400);
677 gfx::Size layer_bounds(1300, 1900);
679 scoped_refptr<FakePicturePileImpl> pending_pile =
680 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
681 scoped_refptr<FakePicturePileImpl> active_pile =
682 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
684 SetupTrees(pending_pile, active_pile);
685 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
687 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
688 EXPECT_LT(low_res_factor, 1.f);
690 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
691 6.f, // ideal contents scale
692 3.f, // device scale
693 2.f, // page scale
694 1.f, // maximum animation scale
695 false);
696 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
697 EXPECT_FLOAT_EQ(6.f,
698 pending_layer_->tilings()->tiling_at(0)->contents_scale());
699 EXPECT_FLOAT_EQ(6.f * low_res_factor,
700 pending_layer_->tilings()->tiling_at(1)->contents_scale());
702 // If we change the page scale factor, then we should get new tilings.
703 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
704 6.6f, // ideal contents scale
705 3.f, // device scale
706 2.2f, // page scale
707 1.f, // maximum animation scale
708 false);
709 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
710 EXPECT_FLOAT_EQ(6.6f,
711 pending_layer_->tilings()->tiling_at(0)->contents_scale());
712 EXPECT_FLOAT_EQ(6.6f * low_res_factor,
713 pending_layer_->tilings()->tiling_at(2)->contents_scale());
715 // If we change the device scale factor, then we should get new tilings.
716 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
717 7.26f, // ideal contents scale
718 3.3f, // device scale
719 2.2f, // page scale
720 1.f, // maximum animation scale
721 false);
722 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
723 EXPECT_FLOAT_EQ(7.26f,
724 pending_layer_->tilings()->tiling_at(0)->contents_scale());
725 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
726 pending_layer_->tilings()->tiling_at(3)->contents_scale());
728 // If we change the device scale factor, but end up at the same total scale
729 // factor somehow, then we don't get new tilings.
730 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
731 7.26f, // ideal contents scale
732 2.2f, // device scale
733 3.3f, // page scale
734 1.f, // maximum animation scale
735 false);
736 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings());
737 EXPECT_FLOAT_EQ(7.26f,
738 pending_layer_->tilings()->tiling_at(0)->contents_scale());
739 EXPECT_FLOAT_EQ(7.26f * low_res_factor,
740 pending_layer_->tilings()->tiling_at(3)->contents_scale());
743 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) {
744 // This test makes sure that if a layer can have tilings, then a commit makes
745 // it not able to have tilings (empty size), and then a future commit that
746 // makes it valid again should be able to create tilings.
747 gfx::Size tile_size(400, 400);
748 gfx::Size layer_bounds(1300, 1900);
750 scoped_refptr<FakePicturePileImpl> empty_pile =
751 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
752 scoped_refptr<FakePicturePileImpl> valid_pile =
753 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
755 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
756 EXPECT_LT(low_res_factor, 1.f);
758 float high_res_scale = 1.3f;
759 float low_res_scale = high_res_scale * low_res_factor;
760 float device_scale = 1.7f;
761 float page_scale = 3.2f;
762 float maximum_animation_scale = 1.f;
764 SetupPendingTree(valid_pile);
765 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
766 high_res_scale,
767 device_scale,
768 page_scale,
769 maximum_animation_scale,
770 false);
771 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
772 EXPECT_FLOAT_EQ(high_res_scale,
773 pending_layer_->HighResTiling()->contents_scale());
774 EXPECT_FLOAT_EQ(low_res_scale,
775 pending_layer_->LowResTiling()->contents_scale());
777 ActivateTree();
778 SetupPendingTree(empty_pile);
779 EXPECT_FALSE(pending_layer_->CanHaveTilings());
780 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
781 high_res_scale,
782 device_scale,
783 page_scale,
784 maximum_animation_scale,
785 false);
786 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
787 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings());
789 ActivateTree();
790 EXPECT_FALSE(active_layer_->CanHaveTilings());
791 SetupDrawPropertiesAndUpdateTiles(active_layer_,
792 high_res_scale,
793 device_scale,
794 page_scale,
795 maximum_animation_scale,
796 false);
797 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
799 SetupPendingTree(valid_pile);
800 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
801 high_res_scale,
802 device_scale,
803 page_scale,
804 maximum_animation_scale,
805 false);
806 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
807 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings());
808 EXPECT_FLOAT_EQ(high_res_scale,
809 pending_layer_->HighResTiling()->contents_scale());
810 EXPECT_FLOAT_EQ(low_res_scale,
811 pending_layer_->LowResTiling()->contents_scale());
814 TEST_F(PictureLayerImplTest, ZoomOutCrash) {
815 gfx::Size tile_size(400, 400);
816 gfx::Size layer_bounds(1300, 1900);
818 // Set up the high and low res tilings before pinch zoom.
819 scoped_refptr<FakePicturePileImpl> pending_pile =
820 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
821 scoped_refptr<FakePicturePileImpl> active_pile =
822 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
824 SetupTrees(pending_pile, active_pile);
825 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
826 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, 1.0f, false);
827 host_impl_.PinchGestureBegin();
828 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
829 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, 1.0f, false);
830 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1);
833 TEST_F(PictureLayerImplTest, PinchGestureTilings) {
834 gfx::Size tile_size(400, 400);
835 gfx::Size layer_bounds(1300, 1900);
837 scoped_refptr<FakePicturePileImpl> pending_pile =
838 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
839 scoped_refptr<FakePicturePileImpl> active_pile =
840 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
842 // Set up the high and low res tilings before pinch zoom.
843 SetupTrees(pending_pile, active_pile);
844 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
845 SetContentsScaleOnBothLayers(2.0f, 1.0f, 1.0f, 1.0f, false);
846 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
847 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
848 EXPECT_FLOAT_EQ(2.0f,
849 active_layer_->tilings()->tiling_at(0)->contents_scale());
850 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
851 active_layer_->tilings()->tiling_at(1)->contents_scale());
853 // Start a pinch gesture.
854 host_impl_.PinchGestureBegin();
856 // Zoom out by a small amount. We should create a tiling at half
857 // the scale (2/kMaxScaleRatioDuringPinch).
858 SetContentsScaleOnBothLayers(1.8f, 1.0f, 0.9f, 1.0f, false);
859 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
860 EXPECT_FLOAT_EQ(2.0f,
861 active_layer_->tilings()->tiling_at(0)->contents_scale());
862 EXPECT_FLOAT_EQ(1.0f,
863 active_layer_->tilings()->tiling_at(1)->contents_scale());
864 EXPECT_FLOAT_EQ(2.0f * low_res_factor,
865 active_layer_->tilings()->tiling_at(2)->contents_scale());
867 // Zoom out further, close to our low-res scale factor. We should
868 // use that tiling as high-res, and not create a new tiling.
869 SetContentsScaleOnBothLayers(
870 low_res_factor, 1.0f, low_res_factor / 2.0f, 1.0f, false);
871 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
873 // Zoom in a lot now. Since we increase by increments of
874 // kMaxScaleRatioDuringPinch, this will first use 1.0, then 2.0
875 // and then finally create a new tiling at 4.0.
876 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
877 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
878 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
879 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
880 SetContentsScaleOnBothLayers(4.2f, 1.0f, 2.1f, 1.f, false);
881 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings());
882 EXPECT_FLOAT_EQ(4.0f,
883 active_layer_->tilings()->tiling_at(0)->contents_scale());
886 TEST_F(PictureLayerImplTest, SnappedTilingDuringZoom) {
887 gfx::Size tile_size(300, 300);
888 gfx::Size layer_bounds(2600, 3800);
890 scoped_refptr<FakePicturePileImpl> pending_pile =
891 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
892 scoped_refptr<FakePicturePileImpl> active_pile =
893 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
895 // Set up the high and low res tilings before pinch zoom.
896 SetupTrees(pending_pile, active_pile);
897 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
898 SetContentsScaleOnBothLayers(0.24f, 1.0f, 0.24f, 1.0f, false);
899 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings());
900 EXPECT_FLOAT_EQ(0.24f,
901 active_layer_->tilings()->tiling_at(0)->contents_scale());
902 EXPECT_FLOAT_EQ(0.0625f,
903 active_layer_->tilings()->tiling_at(1)->contents_scale());
905 // Start a pinch gesture.
906 host_impl_.PinchGestureBegin();
908 // Zoom out by a small amount. We should create a tiling at half
909 // the scale (1/kMaxScaleRatioDuringPinch).
910 SetContentsScaleOnBothLayers(0.2f, 1.0f, 0.2f, 1.0f, false);
911 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
912 EXPECT_FLOAT_EQ(0.24f,
913 active_layer_->tilings()->tiling_at(0)->contents_scale());
914 EXPECT_FLOAT_EQ(0.12f,
915 active_layer_->tilings()->tiling_at(1)->contents_scale());
916 EXPECT_FLOAT_EQ(0.0625,
917 active_layer_->tilings()->tiling_at(2)->contents_scale());
919 // Zoom out further, close to our low-res scale factor. We should
920 // use that tiling as high-res, and not create a new tiling.
921 SetContentsScaleOnBothLayers(0.1f, 1.0f, 0.1f, 1.0f, false);
922 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
924 // Zoom in. 0.125(desired_scale) should be snapped to 0.12 during zoom-in
925 // because 0.125(desired_scale) is within the ratio(1.2)
926 SetContentsScaleOnBothLayers(0.5f, 1.0f, 0.5f, 1.0f, false);
927 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings());
930 TEST_F(PictureLayerImplTest, CleanUpTilings) {
931 gfx::Size tile_size(400, 400);
932 gfx::Size layer_bounds(1300, 1900);
934 scoped_refptr<FakePicturePileImpl> pending_pile =
935 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
936 scoped_refptr<FakePicturePileImpl> active_pile =
937 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
939 std::vector<PictureLayerTiling*> used_tilings;
941 SetupTrees(pending_pile, active_pile);
942 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
944 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
945 EXPECT_LT(low_res_factor, 1.f);
947 float device_scale = 1.7f;
948 float page_scale = 3.2f;
949 float scale = 1.f;
951 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
952 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
954 // We only have ideal tilings, so they aren't removed.
955 used_tilings.clear();
956 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
957 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
959 host_impl_.PinchGestureBegin();
961 // Changing the ideal but not creating new tilings.
962 scale *= 1.5f;
963 page_scale *= 1.5f;
964 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
965 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
967 // The tilings are still our target scale, so they aren't removed.
968 used_tilings.clear();
969 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
970 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
972 host_impl_.PinchGestureEnd();
974 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
975 scale /= 4.f;
976 page_scale /= 4.f;
977 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
978 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
979 EXPECT_FLOAT_EQ(
980 1.f,
981 active_layer_->tilings()->tiling_at(1)->contents_scale());
982 EXPECT_FLOAT_EQ(
983 1.f * low_res_factor,
984 active_layer_->tilings()->tiling_at(3)->contents_scale());
986 // Mark the non-ideal tilings as used. They won't be removed.
987 used_tilings.clear();
988 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
989 used_tilings.push_back(active_layer_->tilings()->tiling_at(3));
990 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
991 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
993 // Now move the ideal scale to 0.5. Our target stays 1.2.
994 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
996 // The high resolution tiling is between target and ideal, so is not
997 // removed. The low res tiling for the old ideal=1.0 scale is removed.
998 used_tilings.clear();
999 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1000 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1002 // Now move the ideal scale to 1.0. Our target stays 1.2.
1003 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
1005 // All the tilings are between are target and the ideal, so they are not
1006 // removed.
1007 used_tilings.clear();
1008 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1009 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1011 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
1012 SetupDrawPropertiesAndUpdateTiles(
1013 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1015 // Because the pending layer's ideal scale is still 1.0, our tilings fall
1016 // in the range [1.0,1.2] and are kept.
1017 used_tilings.clear();
1018 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1019 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1021 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
1022 // 1.2 still.
1023 SetupDrawPropertiesAndUpdateTiles(
1024 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
1026 // Our 1.0 tiling now falls outside the range between our ideal scale and our
1027 // target raster scale. But it is in our used tilings set, so nothing is
1028 // deleted.
1029 used_tilings.clear();
1030 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
1031 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1032 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
1034 // If we remove it from our used tilings set, it is outside the range to keep
1035 // so it is deleted.
1036 used_tilings.clear();
1037 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
1038 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
1041 #define EXPECT_BOTH_EQ(expression, x) \
1042 do { \
1043 EXPECT_EQ(x, pending_layer_->expression); \
1044 EXPECT_EQ(x, active_layer_->expression); \
1045 } while (false)
1047 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) {
1048 // Make sure this layer covers multiple tiles, since otherwise low
1049 // res won't get created because it is too small.
1050 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1051 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1));
1052 // Avoid max untiled layer size heuristics via fixed tile size.
1053 pending_layer_->set_fixed_tile_size(tile_size);
1054 active_layer_->set_fixed_tile_size(tile_size);
1056 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1057 float contents_scale = 1.f;
1058 float device_scale = 1.f;
1059 float page_scale = 1.f;
1060 float maximum_animation_scale = 1.f;
1061 bool animating_transform = true;
1063 // Animating, so don't create low res even if there isn't one already.
1064 SetContentsScaleOnBothLayers(contents_scale,
1065 device_scale,
1066 page_scale,
1067 maximum_animation_scale,
1068 animating_transform);
1069 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1070 EXPECT_BOTH_EQ(num_tilings(), 1u);
1072 // Stop animating, low res gets created.
1073 animating_transform = false;
1074 SetContentsScaleOnBothLayers(contents_scale,
1075 device_scale,
1076 page_scale,
1077 maximum_animation_scale,
1078 animating_transform);
1079 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
1080 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1081 EXPECT_BOTH_EQ(num_tilings(), 2u);
1083 // Page scale animation, new high res, but not new low res because animating.
1084 contents_scale = 2.f;
1085 page_scale = 2.f;
1086 animating_transform = true;
1087 SetContentsScaleOnBothLayers(contents_scale,
1088 device_scale,
1089 page_scale,
1090 maximum_animation_scale,
1091 animating_transform);
1092 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1093 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor);
1094 EXPECT_BOTH_EQ(num_tilings(), 3u);
1096 // Stop animating, new low res gets created for final page scale.
1097 animating_transform = false;
1098 SetContentsScaleOnBothLayers(contents_scale,
1099 device_scale,
1100 page_scale,
1101 maximum_animation_scale,
1102 animating_transform);
1103 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
1104 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor);
1105 EXPECT_BOTH_EQ(num_tilings(), 4u);
1108 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) {
1109 gfx::Size tile_size(host_impl_.settings().default_tile_size);
1110 SetupDefaultTrees(tile_size);
1112 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
1113 float device_scale = 1.f;
1114 float page_scale = 1.f;
1115 float maximum_animation_scale = 1.f;
1116 bool animating_transform = false;
1118 // Contents exactly fit on one tile at scale 1, no low res.
1119 float contents_scale = 1.f;
1120 SetContentsScaleOnBothLayers(contents_scale,
1121 device_scale,
1122 page_scale,
1123 maximum_animation_scale,
1124 animating_transform);
1125 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1126 EXPECT_BOTH_EQ(num_tilings(), 1u);
1128 ResetTilingsAndRasterScales();
1130 // Contents that are smaller than one tile, no low res.
1131 contents_scale = 0.123f;
1132 SetContentsScaleOnBothLayers(contents_scale,
1133 device_scale,
1134 page_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 // Any content bounds that would create more than one tile will
1143 // generate a low res tiling.
1144 contents_scale = 2.5f;
1145 SetContentsScaleOnBothLayers(contents_scale,
1146 device_scale,
1147 page_scale,
1148 maximum_animation_scale,
1149 animating_transform);
1150 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1151 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(),
1152 contents_scale * low_res_factor);
1153 EXPECT_BOTH_EQ(num_tilings(), 2u);
1155 ResetTilingsAndRasterScales();
1157 // Mask layers dont create low res since they always fit on one tile.
1158 pending_layer_->SetIsMask(true);
1159 active_layer_->SetIsMask(true);
1160 SetContentsScaleOnBothLayers(contents_scale,
1161 device_scale,
1162 page_scale,
1163 maximum_animation_scale,
1164 animating_transform);
1165 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale);
1166 EXPECT_BOTH_EQ(num_tilings(), 1u);
1169 TEST_F(PictureLayerImplTest, ReleaseResources) {
1170 gfx::Size tile_size(400, 400);
1171 gfx::Size layer_bounds(1300, 1900);
1173 scoped_refptr<FakePicturePileImpl> pending_pile =
1174 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1175 scoped_refptr<FakePicturePileImpl> active_pile =
1176 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1178 SetupTrees(pending_pile, active_pile);
1179 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1181 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1182 1.3f, // ideal contents scale
1183 2.7f, // device scale
1184 3.2f, // page scale
1185 1.f, // maximum animation scale
1186 false);
1187 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1189 // All tilings should be removed when losing output surface.
1190 active_layer_->ReleaseResources();
1191 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
1192 pending_layer_->ReleaseResources();
1193 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1195 // This should create new tilings.
1196 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
1197 1.3f, // ideal contents scale
1198 2.7f, // device scale
1199 3.2f, // page scale
1200 1.f, // maximum animation scale
1201 false);
1202 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
1205 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) {
1206 // The default max tile size is larger than 400x400.
1207 gfx::Size tile_size(400, 400);
1208 gfx::Size layer_bounds(5000, 5000);
1210 scoped_refptr<FakePicturePileImpl> pending_pile =
1211 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1212 scoped_refptr<FakePicturePileImpl> active_pile =
1213 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1215 SetupTrees(pending_pile, active_pile);
1216 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1218 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1219 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1221 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1223 // The default value.
1224 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1225 host_impl_.settings().default_tile_size.ToString());
1227 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1228 EXPECT_EQ(gfx::Size(256, 256).ToString(),
1229 tile->content_rect().size().ToString());
1231 pending_layer_->ReleaseResources();
1233 // Change the max texture size on the output surface context.
1234 scoped_ptr<TestWebGraphicsContext3D> context =
1235 TestWebGraphicsContext3D::Create();
1236 context->set_max_texture_size(140);
1237 host_impl_.DidLoseOutputSurface();
1238 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1239 context.Pass()).PassAs<OutputSurface>());
1241 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1242 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
1244 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1246 // Verify the tiles are not larger than the context's max texture size.
1247 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1248 EXPECT_GE(140, tile->content_rect().width());
1249 EXPECT_GE(140, tile->content_rect().height());
1252 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) {
1253 // The default max tile size is larger than 400x400.
1254 gfx::Size tile_size(400, 400);
1255 gfx::Size layer_bounds(500, 500);
1257 scoped_refptr<FakePicturePileImpl> pending_pile =
1258 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1259 scoped_refptr<FakePicturePileImpl> active_pile =
1260 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1262 SetupTrees(pending_pile, active_pile);
1263 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
1265 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1266 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1268 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1270 // The default value. The layer is smaller than this.
1271 EXPECT_EQ(gfx::Size(512, 512).ToString(),
1272 host_impl_.settings().max_untiled_layer_size.ToString());
1274 // There should be a single tile since the layer is small.
1275 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1276 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size());
1278 pending_layer_->ReleaseResources();
1280 // Change the max texture size on the output surface context.
1281 scoped_ptr<TestWebGraphicsContext3D> context =
1282 TestWebGraphicsContext3D::Create();
1283 context->set_max_texture_size(140);
1284 host_impl_.DidLoseOutputSurface();
1285 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d(
1286 context.Pass()).PassAs<OutputSurface>());
1288 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
1289 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings());
1291 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting();
1293 // There should be more than one tile since the max texture size won't cover
1294 // the layer.
1295 high_res_tiling = pending_layer_->tilings()->tiling_at(0);
1296 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size());
1298 // Verify the tiles are not larger than the context's max texture size.
1299 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0];
1300 EXPECT_GE(140, tile->content_rect().width());
1301 EXPECT_GE(140, tile->content_rect().height());
1304 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) {
1305 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1306 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1308 gfx::Size tile_size(400, 400);
1309 gfx::Size layer_bounds(1300, 1900);
1311 scoped_refptr<FakePicturePileImpl> pending_pile =
1312 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1313 scoped_refptr<FakePicturePileImpl> active_pile =
1314 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1316 SetupTrees(pending_pile, active_pile);
1318 active_layer_->draw_properties().visible_content_rect =
1319 gfx::Rect(layer_bounds);
1321 gfx::Rect layer_invalidation(150, 200, 30, 180);
1322 Region invalidation(layer_invalidation);
1323 AddDefaultTilingsWithInvalidation(invalidation);
1325 AppendQuadsData data;
1326 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL);
1327 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1328 active_layer_->DidDraw(NULL);
1330 ASSERT_EQ(1U, render_pass->quad_list.size());
1331 EXPECT_EQ(DrawQuad::PICTURE_CONTENT, render_pass->quad_list[0]->material);
1334 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) {
1335 gfx::Size tile_size(100, 100);
1336 gfx::Size layer_bounds(1000, 1000);
1338 scoped_refptr<FakePicturePileImpl> pending_pile =
1339 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1340 // Layers with entirely empty piles can't get tilings.
1341 pending_pile->AddRecordingAt(0, 0);
1343 SetupPendingTree(pending_pile);
1345 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1346 pending_layer_->AddTiling(1.0f);
1347 pending_layer_->AddTiling(2.0f);
1349 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
1350 // on a layer with no recordings.
1351 host_impl_.pending_tree()->UpdateDrawProperties();
1352 pending_layer_->MarkVisibleResourcesAsRequired();
1355 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) {
1356 gfx::Size tile_size(100, 100);
1357 gfx::Size layer_bounds(200, 200);
1359 scoped_refptr<FakePicturePileImpl> pending_pile =
1360 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1361 SetupPendingTree(pending_pile);
1363 pending_layer_->set_fixed_tile_size(tile_size);
1364 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1365 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1366 host_impl_.pending_tree()->UpdateDrawProperties();
1367 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION);
1369 pending_layer_->draw_properties().visible_content_rect =
1370 gfx::Rect(0, 0, 100, 200);
1372 // Fake set priorities.
1373 for (PictureLayerTiling::CoverageIterator iter(
1374 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1375 iter;
1376 ++iter) {
1377 if (!*iter)
1378 continue;
1379 Tile* tile = *iter;
1380 TilePriority priority;
1381 priority.resolution = HIGH_RESOLUTION;
1382 gfx::Rect tile_bounds = iter.geometry_rect();
1383 if (pending_layer_->visible_content_rect().Intersects(tile_bounds)) {
1384 priority.priority_bin = TilePriority::NOW;
1385 priority.distance_to_visible = 0.f;
1386 } else {
1387 priority.priority_bin = TilePriority::SOON;
1388 priority.distance_to_visible = 1.f;
1390 tile->SetPriority(PENDING_TREE, priority);
1393 pending_layer_->MarkVisibleResourcesAsRequired();
1395 int num_visible = 0;
1396 int num_offscreen = 0;
1398 for (PictureLayerTiling::CoverageIterator iter(
1399 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1400 iter;
1401 ++iter) {
1402 if (!*iter)
1403 continue;
1404 const Tile* tile = *iter;
1405 if (tile->priority(PENDING_TREE).distance_to_visible == 0.f) {
1406 EXPECT_TRUE(tile->required_for_activation());
1407 num_visible++;
1408 } else {
1409 EXPECT_FALSE(tile->required_for_activation());
1410 num_offscreen++;
1414 EXPECT_GT(num_visible, 0);
1415 EXPECT_GT(num_offscreen, 0);
1418 TEST_F(PictureLayerImplTest, TileOutsideOfViewportForTilePriorityNotRequired) {
1419 base::TimeTicks time_ticks;
1420 time_ticks += base::TimeDelta::FromMilliseconds(1);
1421 host_impl_.SetCurrentBeginFrameArgs(
1422 CreateBeginFrameArgsForTesting(time_ticks));
1424 gfx::Size tile_size(100, 100);
1425 gfx::Size layer_bounds(400, 400);
1426 gfx::Rect external_viewport_for_tile_priority(400, 200);
1427 gfx::Rect visible_content_rect(200, 400);
1429 scoped_refptr<FakePicturePileImpl> active_pile =
1430 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1431 scoped_refptr<FakePicturePileImpl> pending_pile =
1432 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1433 SetupTrees(pending_pile, active_pile);
1435 active_layer_->set_fixed_tile_size(tile_size);
1436 pending_layer_->set_fixed_tile_size(tile_size);
1437 ASSERT_TRUE(pending_layer_->CanHaveTilings());
1438 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f);
1440 // Set external viewport for tile priority.
1441 gfx::Rect viewport = gfx::Rect(layer_bounds);
1442 gfx::Transform transform;
1443 gfx::Transform transform_for_tile_priority;
1444 bool resourceless_software_draw = false;
1445 host_impl_.SetExternalDrawConstraints(transform,
1446 viewport,
1447 viewport,
1448 external_viewport_for_tile_priority,
1449 transform_for_tile_priority,
1450 resourceless_software_draw);
1451 host_impl_.pending_tree()->UpdateDrawProperties();
1453 // Set visible content rect that is different from
1454 // external_viewport_for_tile_priority.
1455 pending_layer_->draw_properties().visible_content_rect = visible_content_rect;
1456 time_ticks += base::TimeDelta::FromMilliseconds(200);
1457 host_impl_.SetCurrentBeginFrameArgs(
1458 CreateBeginFrameArgsForTesting(time_ticks));
1459 pending_layer_->UpdateTiles(NULL);
1461 pending_layer_->MarkVisibleResourcesAsRequired();
1463 // Intersect the two rects. Any tile outside should not be required for
1464 // activation.
1465 gfx::Rect viewport_for_tile_priority =
1466 pending_layer_->GetViewportForTilePriorityInContentSpace();
1467 viewport_for_tile_priority.Intersect(pending_layer_->visible_content_rect());
1469 int num_inside = 0;
1470 int num_outside = 0;
1471 for (PictureLayerTiling::CoverageIterator iter(
1472 tiling, pending_layer_->contents_scale_x(), gfx::Rect(layer_bounds));
1473 iter;
1474 ++iter) {
1475 if (!*iter)
1476 continue;
1477 Tile* tile = *iter;
1478 if (viewport_for_tile_priority.Intersects(iter.geometry_rect())) {
1479 num_inside++;
1480 // Mark everything in viewport for tile priority as ready to draw.
1481 ManagedTileState::TileVersion& tile_version =
1482 tile->GetTileVersionForTesting(
1483 tile->DetermineRasterModeForTree(PENDING_TREE));
1484 tile_version.SetSolidColorForTesting(SK_ColorRED);
1485 } else {
1486 num_outside++;
1487 EXPECT_FALSE(tile->required_for_activation());
1491 EXPECT_GT(num_inside, 0);
1492 EXPECT_GT(num_outside, 0);
1494 // Activate and draw active layer.
1495 host_impl_.ActivateSyncTree();
1496 host_impl_.active_tree()->UpdateDrawProperties();
1497 active_layer_->draw_properties().visible_content_rect = visible_content_rect;
1499 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1500 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1501 AppendQuadsData data;
1502 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1503 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1504 active_layer_->DidDraw(NULL);
1506 // All tiles in activation rect is ready to draw.
1507 EXPECT_EQ(0u, data.num_missing_tiles);
1508 EXPECT_EQ(0u, data.num_incomplete_tiles);
1511 TEST_F(PictureLayerImplTest, HighResTileIsComplete) {
1512 base::TimeTicks time_ticks;
1513 time_ticks += base::TimeDelta::FromMilliseconds(1);
1514 host_impl_.SetCurrentBeginFrameArgs(
1515 CreateBeginFrameArgsForTesting(time_ticks));
1517 gfx::Size tile_size(100, 100);
1518 gfx::Size layer_bounds(200, 200);
1520 host_impl_.SetViewportSize(layer_bounds);
1522 scoped_refptr<FakePicturePileImpl> pending_pile =
1523 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1524 SetupPendingTree(pending_pile);
1525 ActivateTree();
1527 // All high res tiles have resources.
1528 active_layer_->set_fixed_tile_size(tile_size);
1529 host_impl_.active_tree()->UpdateDrawProperties();
1530 std::vector<Tile*> tiles =
1531 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1532 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
1534 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1535 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1536 AppendQuadsData data;
1537 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1538 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1539 active_layer_->DidDraw(NULL);
1541 // All high res tiles drew, nothing was incomplete.
1542 EXPECT_EQ(9u, render_pass->quad_list.size());
1543 EXPECT_EQ(0u, data.num_missing_tiles);
1544 EXPECT_EQ(0u, data.num_incomplete_tiles);
1547 TEST_F(PictureLayerImplTest, LowResTileIsIncomplete) {
1548 base::TimeTicks time_ticks;
1549 time_ticks += base::TimeDelta::FromMilliseconds(1);
1550 host_impl_.SetCurrentBeginFrameArgs(
1551 CreateBeginFrameArgsForTesting(time_ticks));
1553 gfx::Size tile_size(100, 100);
1554 gfx::Size layer_bounds(200, 200);
1556 host_impl_.SetViewportSize(layer_bounds);
1558 scoped_refptr<FakePicturePileImpl> pending_pile =
1559 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1560 SetupPendingTree(pending_pile);
1561 ActivateTree();
1563 // All high res tiles have resources except one.
1564 active_layer_->set_fixed_tile_size(tile_size);
1565 host_impl_.active_tree()->UpdateDrawProperties();
1566 std::vector<Tile*> high_tiles =
1567 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting();
1568 high_tiles.erase(high_tiles.begin());
1569 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1571 // All low res tiles have resources.
1572 std::vector<Tile*> low_tiles =
1573 active_layer_->tilings()->tiling_at(1)->AllTilesForTesting();
1574 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(low_tiles);
1576 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1577 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1578 AppendQuadsData data;
1579 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1580 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1581 active_layer_->DidDraw(NULL);
1583 // The missing high res tile was replaced by a low res tile.
1584 EXPECT_EQ(9u, render_pass->quad_list.size());
1585 EXPECT_EQ(0u, data.num_missing_tiles);
1586 EXPECT_EQ(1u, data.num_incomplete_tiles);
1589 TEST_F(PictureLayerImplTest,
1590 HighResAndIdealResTileIsCompleteWhenRasterScaleIsNotIdeal) {
1591 base::TimeTicks time_ticks;
1592 time_ticks += base::TimeDelta::FromMilliseconds(1);
1593 host_impl_.SetCurrentBeginFrameArgs(
1594 CreateBeginFrameArgsForTesting(time_ticks));
1596 gfx::Size tile_size(100, 100);
1597 gfx::Size layer_bounds(200, 200);
1599 host_impl_.SetViewportSize(layer_bounds);
1601 scoped_refptr<FakePicturePileImpl> pending_pile =
1602 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1603 scoped_refptr<FakePicturePileImpl> active_pile =
1604 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1605 SetupTrees(pending_pile, active_pile);
1607 active_layer_->set_fixed_tile_size(tile_size);
1609 active_layer_->draw_properties().visible_content_rect =
1610 gfx::Rect(layer_bounds);
1611 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1613 // One ideal tile exists, this will get used when drawing.
1614 std::vector<Tile*> ideal_tiles;
1615 EXPECT_EQ(2.f, active_layer_->HighResTiling()->contents_scale());
1616 ideal_tiles.push_back(active_layer_->HighResTiling()->TileAt(0, 0));
1617 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(
1618 ideal_tiles);
1620 // Due to layer scale throttling, the raster contents scale is changed to 1,
1621 // while the ideal is still 2.
1622 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
1623 SetupDrawPropertiesAndUpdateTiles(active_layer_, 2.f, 1.f, 1.f, 1.f, false);
1625 EXPECT_EQ(1.f, active_layer_->HighResTiling()->contents_scale());
1626 EXPECT_EQ(1.f, active_layer_->raster_contents_scale());
1627 EXPECT_EQ(2.f, active_layer_->ideal_contents_scale());
1629 // Both tilings still exist.
1630 EXPECT_EQ(2.f, active_layer_->tilings()->tiling_at(0)->contents_scale());
1631 EXPECT_EQ(1.f, active_layer_->tilings()->tiling_at(1)->contents_scale());
1633 // All high res tiles have resources.
1634 std::vector<Tile*> high_tiles =
1635 active_layer_->HighResTiling()->AllTilesForTesting();
1636 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(high_tiles);
1638 MockOcclusionTracker<LayerImpl> occlusion_tracker;
1639 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
1640 AppendQuadsData data;
1641 active_layer_->WillDraw(DRAW_MODE_SOFTWARE, NULL);
1642 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
1643 active_layer_->DidDraw(NULL);
1645 // All high res tiles drew, and the one ideal res tile drew.
1646 ASSERT_GT(render_pass->quad_list.size(), 9u);
1647 EXPECT_EQ(gfx::SizeF(99.f, 99.f),
1648 TileDrawQuad::MaterialCast(render_pass->quad_list[0])
1649 ->tex_coord_rect.size());
1650 EXPECT_EQ(gfx::SizeF(49.5f, 49.5f),
1651 TileDrawQuad::MaterialCast(render_pass->quad_list[1])
1652 ->tex_coord_rect.size());
1654 // Neither the high res nor the ideal tiles were considered as incomplete.
1655 EXPECT_EQ(0u, data.num_missing_tiles);
1656 EXPECT_EQ(0u, data.num_incomplete_tiles);
1659 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) {
1660 gfx::Size layer_bounds(400, 400);
1661 gfx::Size tile_size(100, 100);
1662 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1664 // No tiles shared.
1665 pending_layer_->set_invalidation(gfx::Rect(layer_bounds));
1667 CreateHighLowResAndSetAllTilesVisible();
1669 active_layer_->SetAllTilesReady();
1671 // No shared tiles and all active tiles ready, so pending can only
1672 // activate with all high res tiles.
1673 pending_layer_->MarkVisibleResourcesAsRequired();
1674 AssertAllTilesRequired(pending_layer_->HighResTiling());
1675 AssertNoTilesRequired(pending_layer_->LowResTiling());
1678 TEST_F(PictureLayerImplTest, HighResRequiredWhenMissingHighResFlagOn) {
1679 gfx::Size layer_bounds(400, 400);
1680 gfx::Size tile_size(100, 100);
1681 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1683 // All tiles shared (no invalidation).
1684 CreateHighLowResAndSetAllTilesVisible();
1686 // Verify active tree not ready.
1687 Tile* some_active_tile =
1688 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1689 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1691 // When high res are required, even if the active tree is not ready,
1692 // the high res tiles must be ready.
1693 host_impl_.active_tree()->SetRequiresHighResToDraw();
1694 pending_layer_->MarkVisibleResourcesAsRequired();
1695 AssertAllTilesRequired(pending_layer_->HighResTiling());
1696 AssertNoTilesRequired(pending_layer_->LowResTiling());
1699 TEST_F(PictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
1700 gfx::Size layer_bounds(400, 400);
1701 gfx::Size tile_size(100, 100);
1702 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1704 CreateHighLowResAndSetAllTilesVisible();
1706 Tile* some_active_tile =
1707 active_layer_->HighResTiling()->AllTilesForTesting()[0];
1708 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
1710 // All tiles shared (no invalidation), so even though the active tree's
1711 // tiles aren't ready, there is nothing required.
1712 pending_layer_->MarkVisibleResourcesAsRequired();
1713 AssertNoTilesRequired(pending_layer_->HighResTiling());
1714 AssertNoTilesRequired(pending_layer_->LowResTiling());
1717 TEST_F(PictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
1718 gfx::Size layer_bounds(400, 400);
1719 gfx::Size tile_size(100, 100);
1720 scoped_refptr<FakePicturePileImpl> pending_pile =
1721 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1722 // This pile will create tilings, but has no recordings so will not create any
1723 // tiles. This is attempting to simulate scrolling past the end of recorded
1724 // content on the active layer, where the recordings are so far away that
1725 // no tiles are created.
1726 scoped_refptr<FakePicturePileImpl> active_pile =
1727 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
1728 tile_size, layer_bounds);
1729 SetupTrees(pending_pile, active_pile);
1730 pending_layer_->set_fixed_tile_size(tile_size);
1731 active_layer_->set_fixed_tile_size(tile_size);
1733 CreateHighLowResAndSetAllTilesVisible();
1735 // Active layer has tilings, but no tiles due to missing recordings.
1736 EXPECT_TRUE(active_layer_->CanHaveTilings());
1737 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u);
1738 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
1740 // Since the active layer has no tiles at all, the pending layer doesn't
1741 // need content in order to activate.
1742 pending_layer_->MarkVisibleResourcesAsRequired();
1743 AssertNoTilesRequired(pending_layer_->HighResTiling());
1744 AssertNoTilesRequired(pending_layer_->LowResTiling());
1747 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) {
1748 gfx::Size layer_bounds(400, 400);
1749 gfx::Size tile_size(100, 100);
1750 scoped_refptr<FakePicturePileImpl> pending_pile =
1751 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1752 scoped_refptr<FakePicturePileImpl> active_pile =
1753 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
1754 SetupTrees(pending_pile, active_pile);
1755 pending_layer_->set_fixed_tile_size(tile_size);
1756 active_layer_->set_fixed_tile_size(tile_size);
1758 CreateHighLowResAndSetAllTilesVisible();
1760 // Active layer can't have tiles.
1761 EXPECT_FALSE(active_layer_->CanHaveTilings());
1763 // All high res tiles required. This should be considered identical
1764 // to the case where there is no active layer, to avoid flashing content.
1765 // This can happen if a layer exists for a while and switches from
1766 // not being able to have content to having content.
1767 pending_layer_->MarkVisibleResourcesAsRequired();
1768 AssertAllTilesRequired(pending_layer_->HighResTiling());
1769 AssertNoTilesRequired(pending_layer_->LowResTiling());
1772 TEST_F(PictureLayerImplTest, HighResRequiredWhenActiveHasDifferentBounds) {
1773 gfx::Size layer_bounds(200, 200);
1774 gfx::Size tile_size(100, 100);
1775 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
1777 gfx::Size pending_layer_bounds(400, 400);
1778 pending_layer_->SetBounds(pending_layer_bounds);
1780 CreateHighLowResAndSetAllTilesVisible();
1782 active_layer_->SetAllTilesReady();
1784 // Since the active layer has different bounds, the pending layer needs all
1785 // high res tiles in order to activate.
1786 pending_layer_->MarkVisibleResourcesAsRequired();
1787 AssertAllTilesRequired(pending_layer_->HighResTiling());
1788 AssertNoTilesRequired(pending_layer_->LowResTiling());
1791 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) {
1792 gfx::Size tile_size(100, 100);
1793 gfx::Size layer_bounds(400, 400);
1794 scoped_refptr<FakePicturePileImpl> pending_pile =
1795 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
1797 host_impl_.CreatePendingTree();
1798 LayerTreeImpl* pending_tree = host_impl_.pending_tree();
1800 scoped_ptr<FakePictureLayerImpl> pending_layer =
1801 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile);
1802 pending_layer->SetDrawsContent(true);
1803 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>());
1805 pending_layer_ = static_cast<FakePictureLayerImpl*>(
1806 host_impl_.pending_tree()->LayerById(id_));
1808 // Set some state on the pending layer, make sure it is not clobbered
1809 // by a sync from the active layer. This could happen because if the
1810 // pending layer has not been post-commit initialized it will attempt
1811 // to sync from the active layer.
1812 float raster_page_scale = 10.f * pending_layer_->raster_page_scale();
1813 pending_layer_->set_raster_page_scale(raster_page_scale);
1814 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
1816 host_impl_.ActivateSyncTree();
1818 active_layer_ = static_cast<FakePictureLayerImpl*>(
1819 host_impl_.active_tree()->LayerById(id_));
1821 EXPECT_EQ(0u, active_layer_->num_tilings());
1822 EXPECT_EQ(raster_page_scale, active_layer_->raster_page_scale());
1823 EXPECT_FALSE(active_layer_->needs_post_commit_initialization());
1826 TEST_F(PictureLayerImplTest, ShareTilesOnSync) {
1827 SetupDefaultTrees(gfx::Size(1500, 1500));
1828 AddDefaultTilingsWithInvalidation(gfx::Rect());
1830 host_impl_.ActivateSyncTree();
1831 host_impl_.CreatePendingTree();
1832 active_layer_ = static_cast<FakePictureLayerImpl*>(
1833 host_impl_.active_tree()->LayerById(id_));
1835 // Force the active tree to sync to the pending tree "post-commit".
1836 pending_layer_->DoPostCommitInitializationIfNeeded();
1838 // Both invalidations should drop tiles from the pending tree.
1839 EXPECT_EQ(3u, active_layer_->num_tilings());
1840 EXPECT_EQ(3u, pending_layer_->num_tilings());
1841 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1842 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1843 PictureLayerTiling* pending_tiling =
1844 pending_layer_->tilings()->tiling_at(i);
1846 ASSERT_TRUE(active_tiling);
1847 ASSERT_TRUE(pending_tiling);
1849 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1850 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1851 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1852 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1854 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1855 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1856 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1857 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1859 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1860 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1861 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1862 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1863 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1864 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1865 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1866 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1870 TEST_F(PictureLayerImplTest, ShareInvalidActiveTreeTilesOnSync) {
1871 SetupDefaultTrees(gfx::Size(1500, 1500));
1872 AddDefaultTilingsWithInvalidation(gfx::Rect(0, 0, 1, 1));
1874 // This activates the 0,0,1,1 invalidation.
1875 host_impl_.ActivateSyncTree();
1876 host_impl_.CreatePendingTree();
1877 active_layer_ = static_cast<FakePictureLayerImpl*>(
1878 host_impl_.active_tree()->LayerById(id_));
1880 // Force the active tree to sync to the pending tree "post-commit".
1881 pending_layer_->DoPostCommitInitializationIfNeeded();
1883 // The active tree invalidation was handled by the active tiles, so they
1884 // can be shared with the pending tree.
1885 EXPECT_EQ(3u, active_layer_->num_tilings());
1886 EXPECT_EQ(3u, pending_layer_->num_tilings());
1887 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1888 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1889 PictureLayerTiling* pending_tiling =
1890 pending_layer_->tilings()->tiling_at(i);
1892 ASSERT_TRUE(active_tiling);
1893 ASSERT_TRUE(pending_tiling);
1895 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1896 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1897 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1898 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1900 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1901 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1902 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1903 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1905 EXPECT_EQ(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1906 EXPECT_TRUE(active_tiling->TileAt(0, 0)->is_shared());
1907 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1908 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1909 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1910 EXPECT_TRUE(active_tiling->TileAt(0, 1)->is_shared());
1911 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1912 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1916 TEST_F(PictureLayerImplTest, RemoveInvalidPendingTreeTilesOnSync) {
1917 SetupDefaultTrees(gfx::Size(1500, 1500));
1918 AddDefaultTilingsWithInvalidation(gfx::Rect());
1920 host_impl_.ActivateSyncTree();
1921 host_impl_.CreatePendingTree();
1922 active_layer_ = static_cast<FakePictureLayerImpl*>(
1923 host_impl_.active_tree()->LayerById(id_));
1925 // Set some invalidation on the pending tree "during commit". We should
1926 // replace raster tiles that touch this.
1927 pending_layer_->set_invalidation(gfx::Rect(1, 1));
1929 // Force the active tree to sync to the pending tree "post-commit".
1930 pending_layer_->DoPostCommitInitializationIfNeeded();
1932 // The pending tree invalidation means tiles can not be shared with the
1933 // active tree.
1934 EXPECT_EQ(3u, active_layer_->num_tilings());
1935 EXPECT_EQ(3u, pending_layer_->num_tilings());
1936 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
1937 PictureLayerTiling* active_tiling = active_layer_->tilings()->tiling_at(i);
1938 PictureLayerTiling* pending_tiling =
1939 pending_layer_->tilings()->tiling_at(i);
1941 ASSERT_TRUE(active_tiling);
1942 ASSERT_TRUE(pending_tiling);
1944 EXPECT_TRUE(active_tiling->TileAt(0, 0));
1945 EXPECT_TRUE(active_tiling->TileAt(1, 0));
1946 EXPECT_TRUE(active_tiling->TileAt(0, 1));
1947 EXPECT_TRUE(active_tiling->TileAt(1, 1));
1949 EXPECT_TRUE(pending_tiling->TileAt(0, 0));
1950 EXPECT_TRUE(pending_tiling->TileAt(1, 0));
1951 EXPECT_TRUE(pending_tiling->TileAt(0, 1));
1952 EXPECT_TRUE(pending_tiling->TileAt(1, 1));
1954 EXPECT_NE(active_tiling->TileAt(0, 0), pending_tiling->TileAt(0, 0));
1955 EXPECT_FALSE(active_tiling->TileAt(0, 0)->is_shared());
1956 EXPECT_FALSE(pending_tiling->TileAt(0, 0)->is_shared());
1957 EXPECT_EQ(active_tiling->TileAt(1, 0), pending_tiling->TileAt(1, 0));
1958 EXPECT_TRUE(active_tiling->TileAt(1, 0)->is_shared());
1959 EXPECT_EQ(active_tiling->TileAt(0, 1), pending_tiling->TileAt(0, 1));
1960 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1961 EXPECT_EQ(active_tiling->TileAt(1, 1), pending_tiling->TileAt(1, 1));
1962 EXPECT_TRUE(active_tiling->TileAt(1, 1)->is_shared());
1966 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) {
1967 SetupDefaultTrees(gfx::Size(10, 10));
1968 host_impl_.active_tree()->UpdateDrawProperties();
1969 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
1971 // Contrived unit test of a real crash. A layer is transparent during a
1972 // context loss, and later becomes opaque, causing active layer SyncTiling to
1973 // be called.
1974 float new_scale = 1.f;
1975 active_layer_->ReleaseResources();
1976 pending_layer_->ReleaseResources();
1977 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(new_scale));
1978 pending_layer_->AddTiling(new_scale);
1979 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(new_scale));
1981 // UpdateDrawProperties early-outs if the tree doesn't need it. It is also
1982 // responsible for calling ManageTilings. These checks verify that
1983 // ReleaseResources has set needs update draw properties so that the
1984 // new tiling gets the appropriate resolution set in ManageTilings.
1985 EXPECT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
1986 host_impl_.active_tree()->UpdateDrawProperties();
1987 PictureLayerTiling* high_res =
1988 active_layer_->tilings()->TilingAtScale(new_scale);
1989 ASSERT_TRUE(!!high_res);
1990 EXPECT_EQ(HIGH_RESOLUTION, high_res->resolution());
1993 TEST_F(PictureLayerImplTest, SyncTilingAfterGpuRasterizationToggles) {
1994 SetupDefaultTrees(gfx::Size(10, 10));
1996 const float kScale = 1.f;
1997 pending_layer_->AddTiling(kScale);
1998 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
1999 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2001 // Gpu rasterization is disabled by default.
2002 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2003 // Toggling the gpu rasterization clears all tilings on both trees.
2004 host_impl_.SetUseGpuRasterization(true);
2005 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2006 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2008 // Make sure that we can still add tiling to the pending layer,
2009 // that gets synced to the active layer.
2010 pending_layer_->AddTiling(kScale);
2011 EXPECT_TRUE(pending_layer_->tilings()->TilingAtScale(kScale));
2012 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(kScale));
2014 // Toggling the gpu rasterization clears all tilings on both trees.
2015 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2016 host_impl_.SetUseGpuRasterization(false);
2017 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2018 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
2021 TEST_F(PictureLayerImplTest, HighResCreatedWhenBoundsShrink) {
2022 SetupDefaultTrees(gfx::Size(10, 10));
2023 host_impl_.active_tree()->UpdateDrawProperties();
2024 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2026 SetupDrawPropertiesAndUpdateTiles(
2027 active_layer_, 0.5f, 0.5f, 0.5f, 0.5f, false);
2028 active_layer_->tilings()->RemoveAllTilings();
2029 PictureLayerTiling* tiling = active_layer_->tilings()->AddTiling(0.5f);
2030 active_layer_->tilings()->AddTiling(1.5f);
2031 active_layer_->tilings()->AddTiling(0.25f);
2032 tiling->set_resolution(HIGH_RESOLUTION);
2034 // Sanity checks.
2035 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings());
2036 ASSERT_EQ(tiling, active_layer_->tilings()->TilingAtScale(0.5f));
2038 // Now, set the bounds to be 1x1 (so that minimum contents scale becomes
2039 // 1.0f). Note that we should also ensure that the pending layer needs post
2040 // commit initialization, since this is what would happen during commit. In
2041 // other words we want the pending layer to sync from the active layer.
2042 pending_layer_->SetBounds(gfx::Size(1, 1));
2043 pending_layer_->SetNeedsPostCommitInitialization();
2044 pending_layer_->set_twin_layer(NULL);
2045 active_layer_->set_twin_layer(NULL);
2046 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization());
2048 // Update the draw properties: sync from active tree should happen here.
2049 host_impl_.pending_tree()->UpdateDrawProperties();
2051 // Another sanity check.
2052 ASSERT_EQ(1.f, pending_layer_->MinimumContentsScale());
2054 // Now we should've synced 1.5f tiling, since that's the only one that doesn't
2055 // violate minimum contents scale. At the same time, we should've created a
2056 // new high res tiling at scale 1.0f.
2057 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings());
2058 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.0f));
2059 EXPECT_EQ(HIGH_RESOLUTION,
2060 pending_layer_->tilings()->TilingAtScale(1.0f)->resolution());
2061 ASSERT_TRUE(pending_layer_->tilings()->TilingAtScale(1.5f));
2062 EXPECT_EQ(NON_IDEAL_RESOLUTION,
2063 pending_layer_->tilings()->TilingAtScale(1.5f)->resolution());
2066 TEST_F(PictureLayerImplTest, NoLowResTilingWithGpuRasterization) {
2067 gfx::Size default_tile_size(host_impl_.settings().default_tile_size);
2068 gfx::Size layer_bounds(default_tile_size.width() * 4,
2069 default_tile_size.height() * 4);
2071 SetupDefaultTrees(layer_bounds);
2072 EXPECT_FALSE(host_impl_.use_gpu_rasterization());
2073 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2074 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2075 // Should have a low-res and a high-res tiling.
2076 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2078 ResetTilingsAndRasterScales();
2080 host_impl_.SetUseGpuRasterization(true);
2081 EXPECT_TRUE(host_impl_.use_gpu_rasterization());
2082 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 1.f, 1.f, 1.f, 1.f, false);
2084 // Should only have the high-res tiling.
2085 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2088 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) {
2089 // Set up layers with tilings.
2090 SetupDefaultTrees(gfx::Size(10, 10));
2091 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, 1.f, false);
2092 pending_layer_->PushPropertiesTo(active_layer_);
2093 EXPECT_TRUE(pending_layer_->DrawsContent());
2094 EXPECT_TRUE(pending_layer_->CanHaveTilings());
2095 EXPECT_GE(pending_layer_->num_tilings(), 0u);
2096 EXPECT_GE(active_layer_->num_tilings(), 0u);
2098 // Set content to false, which should make CanHaveTilings return false.
2099 pending_layer_->SetDrawsContent(false);
2100 EXPECT_FALSE(pending_layer_->DrawsContent());
2101 EXPECT_FALSE(pending_layer_->CanHaveTilings());
2103 // No tilings should be pushed to active layer.
2104 pending_layer_->PushPropertiesTo(active_layer_);
2105 EXPECT_EQ(0u, active_layer_->num_tilings());
2108 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) {
2109 SetupDefaultTrees(gfx::Size(10, 10));
2110 host_impl_.PinchGestureBegin();
2111 float high_res_scale = 2.3f;
2112 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2114 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2115 EXPECT_FLOAT_EQ(high_res_scale,
2116 pending_layer_->HighResTiling()->contents_scale());
2119 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) {
2120 SetupDefaultTrees(gfx::Size(10, 10));
2121 host_impl_.PinchGestureBegin();
2122 float high_res_scale = 0.0001f;
2123 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale);
2125 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, 1.f, false);
2127 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2128 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2129 pending_layer_->HighResTiling()->contents_scale());
2132 TEST_F(PictureLayerImplTest, PinchingTooSmall) {
2133 SetupDefaultTrees(gfx::Size(10, 10));
2135 float contents_scale = 0.15f;
2136 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, 1.f, false);
2138 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2139 EXPECT_FLOAT_EQ(contents_scale,
2140 pending_layer_->HighResTiling()->contents_scale());
2142 host_impl_.PinchGestureBegin();
2144 float page_scale = 0.0001f;
2145 EXPECT_LT(page_scale * contents_scale,
2146 pending_layer_->MinimumContentsScale());
2148 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, 1.f, false);
2149 ASSERT_GE(pending_layer_->num_tilings(), 0u);
2150 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(),
2151 pending_layer_->HighResTiling()->contents_scale());
2154 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest {
2155 public:
2156 virtual void InitializeRenderer() OVERRIDE {
2157 bool delegated_rendering = false;
2158 host_impl_.InitializeRenderer(
2159 FakeOutputSurface::CreateDeferredGL(
2160 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice),
2161 delegated_rendering).PassAs<OutputSurface>());
2164 virtual void SetUp() OVERRIDE {
2165 PictureLayerImplTest::SetUp();
2167 // Create some default active and pending trees.
2168 gfx::Size tile_size(100, 100);
2169 gfx::Size layer_bounds(400, 400);
2171 scoped_refptr<FakePicturePileImpl> pending_pile =
2172 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2173 scoped_refptr<FakePicturePileImpl> active_pile =
2174 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2176 SetupTrees(pending_pile, active_pile);
2180 // This test is really a LayerTreeHostImpl test, in that it makes sure
2181 // that trees need update draw properties after deferred initialization.
2182 // However, this is also a regression test for PictureLayerImpl in that
2183 // not having this update will cause a crash.
2184 TEST_F(DeferredInitPictureLayerImplTest, PreventUpdateTilesDuringLostContext) {
2185 host_impl_.pending_tree()->UpdateDrawProperties();
2186 host_impl_.active_tree()->UpdateDrawProperties();
2187 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties());
2188 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties());
2190 FakeOutputSurface* fake_output_surface =
2191 static_cast<FakeOutputSurface*>(host_impl_.output_surface());
2192 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d(
2193 TestContextProvider::Create()));
2195 // These will crash PictureLayerImpl if this is not true.
2196 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties());
2197 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties());
2198 host_impl_.active_tree()->UpdateDrawProperties();
2201 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForCpuRasterization) {
2202 gfx::Size layer_bounds(100, 100);
2203 gfx::Size viewport_size(1000, 1000);
2204 SetupDefaultTrees(layer_bounds);
2205 host_impl_.SetViewportSize(viewport_size);
2207 float contents_scale = 1.f;
2208 float device_scale = 1.3f;
2209 float page_scale = 1.4f;
2210 float maximum_animation_scale = 1.f;
2211 bool animating_transform = false;
2213 SetContentsScaleOnBothLayers(contents_scale,
2214 device_scale,
2215 page_scale,
2216 maximum_animation_scale,
2217 animating_transform);
2218 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2220 // Since we're CPU-rasterizing, starting an animation should cause tiling
2221 // resolution to get set to the maximum animation scale factor.
2222 animating_transform = true;
2223 maximum_animation_scale = 3.f;
2224 contents_scale = 2.f;
2226 SetContentsScaleOnBothLayers(contents_scale,
2227 device_scale,
2228 page_scale,
2229 maximum_animation_scale,
2230 animating_transform);
2231 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2233 // Further changes to scale during the animation should not cause a new
2234 // high-res tiling to get created.
2235 contents_scale = 4.f;
2236 maximum_animation_scale = 5.f;
2238 SetContentsScaleOnBothLayers(contents_scale,
2239 device_scale,
2240 page_scale,
2241 maximum_animation_scale,
2242 animating_transform);
2243 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2245 // Once we stop animating, a new high-res tiling should be created.
2246 animating_transform = false;
2248 SetContentsScaleOnBothLayers(contents_scale,
2249 device_scale,
2250 page_scale,
2251 maximum_animation_scale,
2252 animating_transform);
2253 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2255 // When animating with an unknown maximum animation scale factor, a new
2256 // high-res tiling should be created at the animation's initial scale.
2257 animating_transform = true;
2258 contents_scale = 2.f;
2259 maximum_animation_scale = 0.f;
2261 SetContentsScaleOnBothLayers(contents_scale,
2262 device_scale,
2263 page_scale,
2264 maximum_animation_scale,
2265 animating_transform);
2266 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2268 // Further changes to scale during the animation should not cause a new
2269 // high-res tiling to get created.
2270 contents_scale = 3.f;
2272 SetContentsScaleOnBothLayers(contents_scale,
2273 device_scale,
2274 page_scale,
2275 maximum_animation_scale,
2276 animating_transform);
2277 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2279 // Once we stop animating, a new high-res tiling should be created.
2280 animating_transform = false;
2281 contents_scale = 4.f;
2283 SetContentsScaleOnBothLayers(contents_scale,
2284 device_scale,
2285 page_scale,
2286 maximum_animation_scale,
2287 animating_transform);
2288 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2290 // When animating with a maxmium animation scale factor that is so large
2291 // that the layer grows larger than the viewport at this scale, a new
2292 // high-res tiling should get created at the animation's initial scale, not
2293 // at its maximum scale.
2294 animating_transform = true;
2295 contents_scale = 2.f;
2296 maximum_animation_scale = 11.f;
2298 SetContentsScaleOnBothLayers(contents_scale,
2299 device_scale,
2300 page_scale,
2301 maximum_animation_scale,
2302 animating_transform);
2303 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2305 // Once we stop animating, a new high-res tiling should be created.
2306 animating_transform = false;
2307 contents_scale = 11.f;
2309 SetContentsScaleOnBothLayers(contents_scale,
2310 device_scale,
2311 page_scale,
2312 maximum_animation_scale,
2313 animating_transform);
2314 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2316 // When animating with a maxmium animation scale factor that is so large
2317 // that the layer grows larger than the viewport at this scale, and where
2318 // the intial source scale is < 1, a new high-res tiling should get created
2319 // at source scale 1.
2320 animating_transform = true;
2321 contents_scale = 0.1f;
2322 maximum_animation_scale = 11.f;
2324 SetContentsScaleOnBothLayers(contents_scale,
2325 device_scale,
2326 page_scale,
2327 maximum_animation_scale,
2328 animating_transform);
2329 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), device_scale * page_scale);
2331 // Once we stop animating, a new high-res tiling should be created.
2332 animating_transform = false;
2333 contents_scale = 11.f;
2335 SetContentsScaleOnBothLayers(contents_scale,
2336 device_scale,
2337 page_scale,
2338 maximum_animation_scale,
2339 animating_transform);
2340 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 11.f);
2343 TEST_F(PictureLayerImplTest, HighResTilingDuringAnimationForGpuRasterization) {
2344 gfx::Size layer_bounds(100, 100);
2345 gfx::Size viewport_size(1000, 1000);
2346 SetupDefaultTrees(layer_bounds);
2347 host_impl_.SetViewportSize(viewport_size);
2348 host_impl_.SetUseGpuRasterization(true);
2350 float contents_scale = 1.f;
2351 float device_scale = 1.3f;
2352 float page_scale = 1.4f;
2353 float maximum_animation_scale = 1.f;
2354 bool animating_transform = false;
2356 SetContentsScaleOnBothLayers(contents_scale,
2357 device_scale,
2358 page_scale,
2359 maximum_animation_scale,
2360 animating_transform);
2361 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2363 // Since we're GPU-rasterizing, starting an animation should cause tiling
2364 // resolution to get set to the current contents scale.
2365 animating_transform = true;
2366 contents_scale = 2.f;
2367 maximum_animation_scale = 4.f;
2369 SetContentsScaleOnBothLayers(contents_scale,
2370 device_scale,
2371 page_scale,
2372 maximum_animation_scale,
2373 animating_transform);
2374 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2376 // Further changes to scale during the animation should cause a new high-res
2377 // tiling to get created.
2378 contents_scale = 3.f;
2380 SetContentsScaleOnBothLayers(contents_scale,
2381 device_scale,
2382 page_scale,
2383 maximum_animation_scale,
2384 animating_transform);
2385 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2387 // Since we're re-rasterizing during the animation, scales smaller than 1
2388 // should be respected.
2389 contents_scale = 0.25f;
2391 SetContentsScaleOnBothLayers(contents_scale,
2392 device_scale,
2393 page_scale,
2394 maximum_animation_scale,
2395 animating_transform);
2396 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 0.25f);
2398 // Once we stop animating, a new high-res tiling should be created.
2399 contents_scale = 4.f;
2400 animating_transform = false;
2402 SetContentsScaleOnBothLayers(contents_scale,
2403 device_scale,
2404 page_scale,
2405 maximum_animation_scale,
2406 animating_transform);
2407 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2409 static_cast<FakePicturePileImpl*>(pending_layer_->pile())->set_has_text(true);
2410 static_cast<FakePicturePileImpl*>(active_layer_->pile())->set_has_text(true);
2412 // Since we're GPU-rasterizing but have text, starting an animation should
2413 // cause tiling resolution to get set to the maximum animation scale.
2414 animating_transform = true;
2415 contents_scale = 2.f;
2416 maximum_animation_scale = 3.f;
2418 SetContentsScaleOnBothLayers(contents_scale,
2419 device_scale,
2420 page_scale,
2421 maximum_animation_scale,
2422 animating_transform);
2423 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2425 // Further changes to scale during the animation should not cause a new
2426 // high-res tiling to get created.
2427 contents_scale = 4.f;
2428 maximum_animation_scale = 5.f;
2430 SetContentsScaleOnBothLayers(contents_scale,
2431 device_scale,
2432 page_scale,
2433 maximum_animation_scale,
2434 animating_transform);
2435 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 3.f);
2437 // Once we stop animating, a new high-res tiling should be created.
2438 animating_transform = false;
2440 SetContentsScaleOnBothLayers(contents_scale,
2441 device_scale,
2442 page_scale,
2443 maximum_animation_scale,
2444 animating_transform);
2445 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f);
2448 TEST_F(PictureLayerImplTest, LayerRasterTileIterator) {
2449 gfx::Size tile_size(100, 100);
2450 gfx::Size layer_bounds(1000, 1000);
2452 scoped_refptr<FakePicturePileImpl> pending_pile =
2453 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2455 SetupPendingTree(pending_pile);
2457 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2459 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2461 // Empty iterator
2462 PictureLayerImpl::LayerRasterTileIterator it;
2463 EXPECT_FALSE(it);
2465 // No tilings.
2466 it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2467 EXPECT_FALSE(it);
2469 pending_layer_->AddTiling(low_res_factor);
2470 pending_layer_->AddTiling(0.3f);
2471 pending_layer_->AddTiling(0.7f);
2472 PictureLayerTiling* high_res_tiling = pending_layer_->AddTiling(1.0f);
2473 pending_layer_->AddTiling(2.0f);
2475 host_impl_.SetViewportSize(gfx::Size(500, 500));
2476 host_impl_.pending_tree()->UpdateDrawProperties();
2478 std::set<Tile*> unique_tiles;
2479 bool reached_prepaint = false;
2480 size_t non_ideal_tile_count = 0u;
2481 size_t low_res_tile_count = 0u;
2482 size_t high_res_tile_count = 0u;
2483 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2485 ++it) {
2486 Tile* tile = *it;
2487 TilePriority priority = tile->priority(PENDING_TREE);
2489 EXPECT_TRUE(tile);
2491 // Non-high res tiles only get visible tiles. Also, prepaint should only
2492 // come at the end of the iteration.
2493 if (priority.resolution != HIGH_RESOLUTION)
2494 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2495 else if (reached_prepaint)
2496 EXPECT_NE(TilePriority::NOW, priority.priority_bin);
2497 else
2498 reached_prepaint = priority.priority_bin != TilePriority::NOW;
2500 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2501 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2502 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2504 unique_tiles.insert(tile);
2507 EXPECT_TRUE(reached_prepaint);
2508 EXPECT_EQ(0u, non_ideal_tile_count);
2509 EXPECT_EQ(1u, low_res_tile_count);
2510 EXPECT_EQ(16u, high_res_tile_count);
2511 EXPECT_EQ(low_res_tile_count + high_res_tile_count + non_ideal_tile_count,
2512 unique_tiles.size());
2514 std::vector<Tile*> high_res_tiles = high_res_tiling->AllTilesForTesting();
2515 for (std::vector<Tile*>::iterator tile_it = high_res_tiles.begin();
2516 tile_it != high_res_tiles.end();
2517 ++tile_it) {
2518 Tile* tile = *tile_it;
2519 ManagedTileState::TileVersion& tile_version =
2520 tile->GetTileVersionForTesting(
2521 tile->DetermineRasterModeForTree(ACTIVE_TREE));
2522 tile_version.SetSolidColorForTesting(SK_ColorRED);
2525 non_ideal_tile_count = 0;
2526 low_res_tile_count = 0;
2527 high_res_tile_count = 0;
2528 for (it = PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
2530 ++it) {
2531 Tile* tile = *it;
2532 TilePriority priority = tile->priority(PENDING_TREE);
2534 EXPECT_TRUE(tile);
2536 non_ideal_tile_count += priority.resolution == NON_IDEAL_RESOLUTION;
2537 low_res_tile_count += priority.resolution == LOW_RESOLUTION;
2538 high_res_tile_count += priority.resolution == HIGH_RESOLUTION;
2541 EXPECT_EQ(0u, non_ideal_tile_count);
2542 EXPECT_EQ(1u, low_res_tile_count);
2543 EXPECT_EQ(0u, high_res_tile_count);
2546 TEST_F(PictureLayerImplTest, LayerEvictionTileIterator) {
2547 gfx::Size tile_size(100, 100);
2548 gfx::Size layer_bounds(1000, 1000);
2550 scoped_refptr<FakePicturePileImpl> pending_pile =
2551 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2553 SetupPendingTree(pending_pile);
2555 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2557 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2559 std::vector<PictureLayerTiling*> tilings;
2560 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
2561 tilings.push_back(pending_layer_->AddTiling(0.3f));
2562 tilings.push_back(pending_layer_->AddTiling(0.7f));
2563 tilings.push_back(pending_layer_->AddTiling(1.0f));
2564 tilings.push_back(pending_layer_->AddTiling(2.0f));
2566 host_impl_.SetViewportSize(gfx::Size(500, 500));
2567 host_impl_.pending_tree()->UpdateDrawProperties();
2569 std::vector<Tile*> all_tiles;
2570 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
2571 tilings.begin();
2572 tiling_iterator != tilings.end();
2573 ++tiling_iterator) {
2574 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
2575 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
2578 std::set<Tile*> all_tiles_set(all_tiles.begin(), all_tiles.end());
2580 bool mark_required = false;
2581 size_t number_of_marked_tiles = 0u;
2582 size_t number_of_unmarked_tiles = 0u;
2583 for (size_t i = 0; i < tilings.size(); ++i) {
2584 PictureLayerTiling* tiling = tilings.at(i);
2585 for (PictureLayerTiling::CoverageIterator iter(
2586 tiling,
2587 pending_layer_->contents_scale_x(),
2588 pending_layer_->visible_content_rect());
2589 iter;
2590 ++iter) {
2591 if (mark_required) {
2592 number_of_marked_tiles++;
2593 iter->MarkRequiredForActivation();
2594 } else {
2595 number_of_unmarked_tiles++;
2597 mark_required = !mark_required;
2601 // Sanity checks.
2602 EXPECT_EQ(91u, all_tiles.size());
2603 EXPECT_EQ(91u, all_tiles_set.size());
2604 EXPECT_GT(number_of_marked_tiles, 1u);
2605 EXPECT_GT(number_of_unmarked_tiles, 1u);
2607 // Empty iterator.
2608 PictureLayerImpl::LayerEvictionTileIterator it;
2609 EXPECT_FALSE(it);
2611 // Tiles don't have resources yet.
2612 it = PictureLayerImpl::LayerEvictionTileIterator(
2613 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2614 EXPECT_FALSE(it);
2616 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
2618 std::set<Tile*> unique_tiles;
2619 float expected_scales[] = {2.0f, 0.3f, 0.7f, low_res_factor, 1.0f};
2620 size_t scale_index = 0;
2621 bool reached_visible = false;
2622 Tile* last_tile = NULL;
2623 for (it = PictureLayerImpl::LayerEvictionTileIterator(
2624 pending_layer_, SAME_PRIORITY_FOR_BOTH_TREES);
2626 ++it) {
2627 Tile* tile = *it;
2628 if (!last_tile)
2629 last_tile = tile;
2631 EXPECT_TRUE(tile);
2633 TilePriority priority = tile->priority(PENDING_TREE);
2635 if (priority.priority_bin == TilePriority::NOW) {
2636 reached_visible = true;
2637 last_tile = tile;
2638 break;
2641 EXPECT_FALSE(tile->required_for_activation());
2643 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2644 std::numeric_limits<float>::epsilon()) {
2645 ++scale_index;
2646 ASSERT_LT(scale_index, arraysize(expected_scales));
2649 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2650 unique_tiles.insert(tile);
2652 // If the tile is the same rough bin as last tile (same activation, bin, and
2653 // scale), then distance should be decreasing.
2654 if (tile->required_for_activation() ==
2655 last_tile->required_for_activation() &&
2656 priority.priority_bin ==
2657 last_tile->priority(PENDING_TREE).priority_bin &&
2658 std::abs(tile->contents_scale() - last_tile->contents_scale()) <
2659 std::numeric_limits<float>::epsilon()) {
2660 EXPECT_LE(priority.distance_to_visible,
2661 last_tile->priority(PENDING_TREE).distance_to_visible);
2664 last_tile = tile;
2667 EXPECT_TRUE(reached_visible);
2668 EXPECT_EQ(65u, unique_tiles.size());
2670 scale_index = 0;
2671 bool reached_required = false;
2672 for (; it; ++it) {
2673 Tile* tile = *it;
2674 EXPECT_TRUE(tile);
2676 TilePriority priority = tile->priority(PENDING_TREE);
2677 EXPECT_EQ(TilePriority::NOW, priority.priority_bin);
2679 if (reached_required) {
2680 EXPECT_TRUE(tile->required_for_activation());
2681 } else if (tile->required_for_activation()) {
2682 reached_required = true;
2683 scale_index = 0;
2686 while (std::abs(tile->contents_scale() - expected_scales[scale_index]) >
2687 std::numeric_limits<float>::epsilon()) {
2688 ++scale_index;
2689 ASSERT_LT(scale_index, arraysize(expected_scales));
2692 EXPECT_FLOAT_EQ(tile->contents_scale(), expected_scales[scale_index]);
2693 unique_tiles.insert(tile);
2696 EXPECT_TRUE(reached_required);
2697 EXPECT_EQ(all_tiles_set.size(), unique_tiles.size());
2700 TEST_F(PictureLayerImplTest, Occlusion) {
2701 gfx::Size tile_size(102, 102);
2702 gfx::Size layer_bounds(1000, 1000);
2703 gfx::Size viewport_size(1000, 1000);
2705 LayerTestCommon::LayerImplTest impl;
2707 scoped_refptr<FakePicturePileImpl> pending_pile =
2708 FakePicturePileImpl::CreateFilledPile(layer_bounds, layer_bounds);
2709 SetupPendingTree(pending_pile);
2710 pending_layer_->SetBounds(layer_bounds);
2711 ActivateTree();
2712 active_layer_->set_fixed_tile_size(tile_size);
2714 host_impl_.SetViewportSize(viewport_size);
2715 host_impl_.active_tree()->UpdateDrawProperties();
2717 std::vector<Tile*> tiles =
2718 active_layer_->HighResTiling()->AllTilesForTesting();
2719 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
2722 SCOPED_TRACE("No occlusion");
2723 gfx::Rect occluded;
2724 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2726 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(),
2727 gfx::Rect(layer_bounds));
2728 EXPECT_EQ(100u, impl.quad_list().size());
2732 SCOPED_TRACE("Full occlusion");
2733 gfx::Rect occluded(active_layer_->visible_content_rect());
2734 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2736 LayerTestCommon::VerifyQuadsExactlyCoverRect(impl.quad_list(), gfx::Rect());
2737 EXPECT_EQ(impl.quad_list().size(), 0u);
2741 SCOPED_TRACE("Partial occlusion");
2742 gfx::Rect occluded(150, 0, 200, 1000);
2743 impl.AppendQuadsWithOcclusion(active_layer_, occluded);
2745 size_t partially_occluded_count = 0;
2746 LayerTestCommon::VerifyQuadsCoverRectWithOcclusion(
2747 impl.quad_list(),
2748 gfx::Rect(layer_bounds),
2749 occluded,
2750 &partially_occluded_count);
2751 // The layer outputs one quad, which is partially occluded.
2752 EXPECT_EQ(100u - 10u, impl.quad_list().size());
2753 EXPECT_EQ(10u + 10u, partially_occluded_count);
2757 TEST_F(PictureLayerImplTest, RasterScaleChangeWithoutAnimation) {
2758 gfx::Size tile_size(host_impl_.settings().default_tile_size);
2759 SetupDefaultTrees(tile_size);
2761 float contents_scale = 2.f;
2762 float device_scale = 1.f;
2763 float page_scale = 1.f;
2764 float maximum_animation_scale = 1.f;
2765 bool animating_transform = false;
2767 SetContentsScaleOnBothLayers(contents_scale,
2768 device_scale,
2769 page_scale,
2770 maximum_animation_scale,
2771 animating_transform);
2772 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f);
2774 // Changing the source scale without being in an animation will cause
2775 // the layer to reset its source scale to 1.f.
2776 contents_scale = 3.f;
2778 SetContentsScaleOnBothLayers(contents_scale,
2779 device_scale,
2780 page_scale,
2781 maximum_animation_scale,
2782 animating_transform);
2783 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2785 // Further changes to the source scale will no longer be reflected in the
2786 // contents scale.
2787 contents_scale = 0.5f;
2789 SetContentsScaleOnBothLayers(contents_scale,
2790 device_scale,
2791 page_scale,
2792 maximum_animation_scale,
2793 animating_transform);
2794 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f);
2797 TEST_F(PictureLayerImplTest, LowResReadyToDrawNotEnoughToActivate) {
2798 gfx::Size tile_size(100, 100);
2799 gfx::Size layer_bounds(1000, 1000);
2801 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2803 // Make sure some tiles are not shared.
2804 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2806 CreateHighLowResAndSetAllTilesVisible();
2807 active_layer_->SetAllTilesReady();
2808 pending_layer_->MarkVisibleResourcesAsRequired();
2810 // All pending layer tiles required are not ready.
2811 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2813 // Initialize all low-res tiles.
2814 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->LowResTiling());
2816 // Low-res tiles should not be enough.
2817 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2819 // Initialize remaining tiles.
2820 pending_layer_->SetAllTilesReady();
2822 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2825 TEST_F(PictureLayerImplTest, HighResReadyToDrawNotEnoughToActivate) {
2826 gfx::Size tile_size(100, 100);
2827 gfx::Size layer_bounds(1000, 1000);
2829 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2831 // Make sure some tiles are not shared.
2832 pending_layer_->set_invalidation(gfx::Rect(gfx::Point(50, 50), tile_size));
2834 CreateHighLowResAndSetAllTilesVisible();
2835 active_layer_->SetAllTilesReady();
2836 pending_layer_->MarkVisibleResourcesAsRequired();
2838 // All pending layer tiles required are not ready.
2839 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2841 // Initialize all high-res tiles.
2842 pending_layer_->SetAllTilesReadyInTiling(pending_layer_->HighResTiling());
2844 // High-res tiles should not be enough.
2845 EXPECT_FALSE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2847 // Initialize remaining tiles.
2848 pending_layer_->SetAllTilesReady();
2850 EXPECT_TRUE(pending_layer_->AllTilesRequiredForActivationAreReadyToDraw());
2853 class NoLowResTilingsSettings : public ImplSidePaintingSettings {
2854 public:
2855 NoLowResTilingsSettings() { create_low_res_tiling = false; }
2858 class NoLowResPictureLayerImplTest : public PictureLayerImplTest {
2859 public:
2860 NoLowResPictureLayerImplTest()
2861 : PictureLayerImplTest(NoLowResTilingsSettings()) {}
2864 TEST_F(NoLowResPictureLayerImplTest, ManageTilingsCreatesTilings) {
2865 gfx::Size tile_size(400, 400);
2866 gfx::Size layer_bounds(1300, 1900);
2868 scoped_refptr<FakePicturePileImpl> pending_pile =
2869 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2870 scoped_refptr<FakePicturePileImpl> active_pile =
2871 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2873 SetupTrees(pending_pile, active_pile);
2874 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
2876 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
2877 EXPECT_LT(low_res_factor, 1.f);
2879 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2880 6.f, // ideal contents scale
2881 3.f, // device scale
2882 2.f, // page scale
2883 1.f, // maximum animation scale
2884 false);
2885 ASSERT_EQ(1u, pending_layer_->tilings()->num_tilings());
2886 EXPECT_FLOAT_EQ(6.f,
2887 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2889 // If we change the page scale factor, then we should get new tilings.
2890 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2891 6.6f, // ideal contents scale
2892 3.f, // device scale
2893 2.2f, // page scale
2894 1.f, // maximum animation scale
2895 false);
2896 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings());
2897 EXPECT_FLOAT_EQ(6.6f,
2898 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2900 // If we change the device scale factor, then we should get new tilings.
2901 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2902 7.26f, // ideal contents scale
2903 3.3f, // device scale
2904 2.2f, // page scale
2905 1.f, // maximum animation scale
2906 false);
2907 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2908 EXPECT_FLOAT_EQ(7.26f,
2909 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2911 // If we change the device scale factor, but end up at the same total scale
2912 // factor somehow, then we don't get new tilings.
2913 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
2914 7.26f, // ideal contents scale
2915 2.2f, // device scale
2916 3.3f, // page scale
2917 1.f, // maximum animation scale
2918 false);
2919 ASSERT_EQ(3u, pending_layer_->tilings()->num_tilings());
2920 EXPECT_FLOAT_EQ(7.26f,
2921 pending_layer_->tilings()->tiling_at(0)->contents_scale());
2924 TEST_F(NoLowResPictureLayerImplTest, MarkRequiredNullTiles) {
2925 gfx::Size tile_size(100, 100);
2926 gfx::Size layer_bounds(1000, 1000);
2928 scoped_refptr<FakePicturePileImpl> pending_pile =
2929 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds);
2930 // Layers with entirely empty piles can't get tilings.
2931 pending_pile->AddRecordingAt(0, 0);
2933 SetupPendingTree(pending_pile);
2935 ASSERT_TRUE(pending_layer_->CanHaveTilings());
2936 pending_layer_->AddTiling(1.0f);
2937 pending_layer_->AddTiling(2.0f);
2939 // It should be safe to call this (and MarkVisibleResourcesAsRequired)
2940 // on a layer with no recordings.
2941 host_impl_.pending_tree()->UpdateDrawProperties();
2942 pending_layer_->MarkVisibleResourcesAsRequired();
2945 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfAllHighResTilesShared) {
2946 gfx::Size layer_bounds(400, 400);
2947 gfx::Size tile_size(100, 100);
2948 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size);
2950 CreateHighLowResAndSetAllTilesVisible();
2952 Tile* some_active_tile =
2953 active_layer_->HighResTiling()->AllTilesForTesting()[0];
2954 EXPECT_FALSE(some_active_tile->IsReadyToDraw());
2956 // All tiles shared (no invalidation), so even though the active tree's
2957 // tiles aren't ready, there is nothing required.
2958 pending_layer_->MarkVisibleResourcesAsRequired();
2959 AssertNoTilesRequired(pending_layer_->HighResTiling());
2960 if (host_impl_.settings().create_low_res_tiling) {
2961 AssertNoTilesRequired(pending_layer_->LowResTiling());
2965 TEST_F(NoLowResPictureLayerImplTest, NothingRequiredIfActiveMissingTiles) {
2966 gfx::Size layer_bounds(400, 400);
2967 gfx::Size tile_size(100, 100);
2968 scoped_refptr<FakePicturePileImpl> pending_pile =
2969 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
2970 // This pile will create tilings, but has no recordings so will not create any
2971 // tiles. This is attempting to simulate scrolling past the end of recorded
2972 // content on the active layer, where the recordings are so far away that
2973 // no tiles are created.
2974 scoped_refptr<FakePicturePileImpl> active_pile =
2975 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings(
2976 tile_size, layer_bounds);
2977 SetupTrees(pending_pile, active_pile);
2978 pending_layer_->set_fixed_tile_size(tile_size);
2979 active_layer_->set_fixed_tile_size(tile_size);
2981 CreateHighLowResAndSetAllTilesVisible();
2983 // Active layer has tilings, but no tiles due to missing recordings.
2984 EXPECT_TRUE(active_layer_->CanHaveTilings());
2985 EXPECT_EQ(active_layer_->tilings()->num_tilings(),
2986 host_impl_.settings().create_low_res_tiling ? 2u : 1u);
2987 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u);
2989 // Since the active layer has no tiles at all, the pending layer doesn't
2990 // need content in order to activate.
2991 pending_layer_->MarkVisibleResourcesAsRequired();
2992 AssertNoTilesRequired(pending_layer_->HighResTiling());
2993 if (host_impl_.settings().create_low_res_tiling)
2994 AssertNoTilesRequired(pending_layer_->LowResTiling());
2997 TEST_F(NoLowResPictureLayerImplTest,
2998 ResourcelessSoftwareDrawHasValidViewportForTilePriority) {
2999 base::TimeTicks time_ticks;
3000 time_ticks += base::TimeDelta::FromMilliseconds(1);
3001 host_impl_.SetCurrentBeginFrameArgs(
3002 CreateBeginFrameArgsForTesting(time_ticks));
3004 gfx::Size tile_size(100, 100);
3005 gfx::Size layer_bounds(400, 400);
3007 scoped_refptr<FakePicturePileImpl> pending_pile =
3008 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3009 scoped_refptr<FakePicturePileImpl> active_pile =
3010 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3012 SetupTrees(pending_pile, active_pile);
3014 Region invalidation;
3015 AddDefaultTilingsWithInvalidation(invalidation);
3016 SetupDrawPropertiesAndUpdateTiles(active_layer_, 1.f, 1.f, 1.f, 1.f, false);
3018 // UpdateTiles with valid viewport. Should update tile viewport.
3019 bool resourceless_software_draw = false;
3020 gfx::Rect viewport = gfx::Rect(layer_bounds);
3021 gfx::Transform transform;
3022 host_impl_.SetExternalDrawConstraints(transform,
3023 viewport,
3024 viewport,
3025 viewport,
3026 transform,
3027 resourceless_software_draw);
3028 active_layer_->draw_properties().visible_content_rect = viewport;
3029 active_layer_->draw_properties().screen_space_transform = transform;
3030 active_layer_->UpdateTiles(NULL);
3032 gfx::Rect visible_rect_for_tile_priority =
3033 active_layer_->visible_rect_for_tile_priority();
3034 EXPECT_FALSE(visible_rect_for_tile_priority.IsEmpty());
3035 gfx::Rect viewport_rect_for_tile_priority =
3036 active_layer_->viewport_rect_for_tile_priority();
3037 EXPECT_FALSE(viewport_rect_for_tile_priority.IsEmpty());
3038 gfx::Transform screen_space_transform_for_tile_priority =
3039 active_layer_->screen_space_transform_for_tile_priority();
3041 // PictureLayerImpl does not make a special case for
3042 // resource_less_software_draw, so the tile viewport and matrix should be
3043 // respected.
3044 time_ticks += base::TimeDelta::FromMilliseconds(200);
3045 host_impl_.SetCurrentBeginFrameArgs(
3046 CreateBeginFrameArgsForTesting(time_ticks));
3047 resourceless_software_draw = true;
3048 viewport = gfx::ScaleToEnclosingRect(viewport, 2);
3049 transform.Translate(1.f, 1.f);
3050 active_layer_->draw_properties().visible_content_rect = viewport;
3051 active_layer_->draw_properties().screen_space_transform = transform;
3052 host_impl_.SetExternalDrawConstraints(transform,
3053 viewport,
3054 viewport,
3055 viewport,
3056 transform,
3057 resourceless_software_draw);
3058 active_layer_->UpdateTiles(NULL);
3060 visible_rect_for_tile_priority =
3061 gfx::ScaleToEnclosingRect(visible_rect_for_tile_priority, 2);
3062 viewport_rect_for_tile_priority =
3063 gfx::ScaleToEnclosingRect(viewport_rect_for_tile_priority, 2);
3064 screen_space_transform_for_tile_priority = transform;
3066 EXPECT_RECT_EQ(visible_rect_for_tile_priority,
3067 active_layer_->visible_rect_for_tile_priority());
3068 EXPECT_RECT_EQ(viewport_rect_for_tile_priority,
3069 active_layer_->viewport_rect_for_tile_priority());
3070 EXPECT_TRANSFORMATION_MATRIX_EQ(
3071 screen_space_transform_for_tile_priority,
3072 active_layer_->screen_space_transform_for_tile_priority());
3075 TEST_F(NoLowResPictureLayerImplTest, CleanUpTilings) {
3076 gfx::Size tile_size(400, 400);
3077 gfx::Size layer_bounds(1300, 1900);
3079 scoped_refptr<FakePicturePileImpl> pending_pile =
3080 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3081 scoped_refptr<FakePicturePileImpl> active_pile =
3082 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3084 std::vector<PictureLayerTiling*> used_tilings;
3086 SetupTrees(pending_pile, active_pile);
3087 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3089 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3090 EXPECT_LT(low_res_factor, 1.f);
3092 float device_scale = 1.7f;
3093 float page_scale = 3.2f;
3094 float scale = 1.f;
3096 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3097 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3099 // We only have ideal tilings, so they aren't removed.
3100 used_tilings.clear();
3101 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3102 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3104 host_impl_.PinchGestureBegin();
3106 // Changing the ideal but not creating new tilings.
3107 scale *= 1.5f;
3108 page_scale *= 1.5f;
3109 SetContentsScaleOnBothLayers(scale, device_scale, page_scale, 1.f, false);
3110 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3112 // The tilings are still our target scale, so they aren't removed.
3113 used_tilings.clear();
3114 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3115 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3117 host_impl_.PinchGestureEnd();
3119 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2.
3120 scale /= 4.f;
3121 page_scale /= 4.f;
3122 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, 1.f, false);
3123 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3124 EXPECT_FLOAT_EQ(1.f,
3125 active_layer_->tilings()->tiling_at(1)->contents_scale());
3127 // Mark the non-ideal tilings as used. They won't be removed.
3128 used_tilings.clear();
3129 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3130 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3131 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3133 // Now move the ideal scale to 0.5. Our target stays 1.2.
3134 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, 1.f, false);
3136 // The high resolution tiling is between target and ideal, so is not
3137 // removed. The low res tiling for the old ideal=1.0 scale is removed.
3138 used_tilings.clear();
3139 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3140 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3142 // Now move the ideal scale to 1.0. Our target stays 1.2.
3143 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, 1.f, false);
3145 // All the tilings are between are target and the ideal, so they are not
3146 // removed.
3147 used_tilings.clear();
3148 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3149 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3151 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2.
3152 SetupDrawPropertiesAndUpdateTiles(
3153 active_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3155 // Because the pending layer's ideal scale is still 1.0, our tilings fall
3156 // in the range [1.0,1.2] and are kept.
3157 used_tilings.clear();
3158 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3159 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3161 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays
3162 // 1.2 still.
3163 SetupDrawPropertiesAndUpdateTiles(
3164 pending_layer_, 1.1f, device_scale, page_scale, 1.f, false);
3166 // Our 1.0 tiling now falls outside the range between our ideal scale and our
3167 // target raster scale. But it is in our used tilings set, so nothing is
3168 // deleted.
3169 used_tilings.clear();
3170 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3171 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3172 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings());
3174 // If we remove it from our used tilings set, it is outside the range to keep
3175 // so it is deleted.
3176 used_tilings.clear();
3177 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3178 ASSERT_EQ(1u, active_layer_->tilings()->num_tilings());
3181 TEST_F(PictureLayerImplTest, ScaleCollision) {
3182 gfx::Size tile_size(400, 400);
3183 gfx::Size layer_bounds(1300, 1900);
3185 scoped_refptr<FakePicturePileImpl> pending_pile =
3186 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3187 scoped_refptr<FakePicturePileImpl> active_pile =
3188 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3190 std::vector<PictureLayerTiling*> used_tilings;
3192 SetupTrees(pending_pile, active_pile);
3194 float pending_contents_scale = 1.f;
3195 float active_contents_scale = 2.f;
3196 float device_scale_factor = 1.f;
3197 float page_scale_factor = 1.f;
3198 float maximum_animation_contents_scale = 1.f;
3199 bool animating_transform = false;
3201 EXPECT_TRUE(host_impl_.settings().create_low_res_tiling);
3202 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3203 EXPECT_LT(low_res_factor, 1.f);
3205 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3206 pending_contents_scale,
3207 device_scale_factor,
3208 page_scale_factor,
3209 maximum_animation_contents_scale,
3210 animating_transform);
3211 SetupDrawPropertiesAndUpdateTiles(active_layer_,
3212 active_contents_scale,
3213 device_scale_factor,
3214 page_scale_factor,
3215 maximum_animation_contents_scale,
3216 animating_transform);
3218 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings());
3219 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3221 EXPECT_EQ(active_contents_scale,
3222 pending_layer_->tilings()->tiling_at(0)->contents_scale());
3223 EXPECT_EQ(pending_contents_scale,
3224 pending_layer_->tilings()->tiling_at(1)->contents_scale());
3225 EXPECT_EQ(active_contents_scale * low_res_factor,
3226 pending_layer_->tilings()->tiling_at(2)->contents_scale());
3227 EXPECT_EQ(pending_contents_scale * low_res_factor,
3228 pending_layer_->tilings()->tiling_at(3)->contents_scale());
3230 EXPECT_EQ(active_contents_scale,
3231 active_layer_->tilings()->tiling_at(0)->contents_scale());
3232 EXPECT_EQ(pending_contents_scale,
3233 active_layer_->tilings()->tiling_at(1)->contents_scale());
3234 EXPECT_EQ(active_contents_scale * low_res_factor,
3235 active_layer_->tilings()->tiling_at(2)->contents_scale());
3236 EXPECT_EQ(pending_contents_scale * low_res_factor,
3237 active_layer_->tilings()->tiling_at(3)->contents_scale());
3239 // The unused low res tiling from the pending tree must be kept or we may add
3240 // it again on the active tree and collide with the pending tree.
3241 used_tilings.push_back(active_layer_->tilings()->tiling_at(1));
3242 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings);
3243 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings());
3245 EXPECT_EQ(active_contents_scale,
3246 active_layer_->tilings()->tiling_at(0)->contents_scale());
3247 EXPECT_EQ(pending_contents_scale,
3248 active_layer_->tilings()->tiling_at(1)->contents_scale());
3249 EXPECT_EQ(active_contents_scale * low_res_factor,
3250 active_layer_->tilings()->tiling_at(2)->contents_scale());
3251 EXPECT_EQ(pending_contents_scale * low_res_factor,
3252 active_layer_->tilings()->tiling_at(3)->contents_scale());
3255 TEST_F(NoLowResPictureLayerImplTest, ReleaseResources) {
3256 gfx::Size tile_size(400, 400);
3257 gfx::Size layer_bounds(1300, 1900);
3259 scoped_refptr<FakePicturePileImpl> pending_pile =
3260 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3261 scoped_refptr<FakePicturePileImpl> active_pile =
3262 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3264 SetupTrees(pending_pile, active_pile);
3265 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3267 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3268 1.3f, // ideal contents scale
3269 2.7f, // device scale
3270 3.2f, // page scale
3271 1.f, // maximum animation scale
3272 false);
3273 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3275 // All tilings should be removed when losing output surface.
3276 active_layer_->ReleaseResources();
3277 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings());
3278 pending_layer_->ReleaseResources();
3279 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings());
3281 // This should create new tilings.
3282 SetupDrawPropertiesAndUpdateTiles(pending_layer_,
3283 1.3f, // ideal contents scale
3284 2.7f, // device scale
3285 3.2f, // page scale
3286 1.f, // maximum animation scale
3287 false);
3288 EXPECT_EQ(1u, pending_layer_->tilings()->num_tilings());
3291 TEST_F(PictureLayerImplTest, SharedQuadStateContainsMaxTilingScale) {
3292 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3293 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3295 gfx::Size tile_size(400, 400);
3296 gfx::Size layer_bounds(1000, 2000);
3298 scoped_refptr<FakePicturePileImpl> pending_pile =
3299 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3300 scoped_refptr<FakePicturePileImpl> active_pile =
3301 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3303 SetupTrees(pending_pile, active_pile);
3305 SetupDrawPropertiesAndUpdateTiles(pending_layer_, 2.5f, 1.f, 1.f, 1.f, false);
3306 host_impl_.pending_tree()->UpdateDrawProperties();
3308 active_layer_->draw_properties().visible_content_rect =
3309 gfx::Rect(layer_bounds);
3310 host_impl_.active_tree()->UpdateDrawProperties();
3312 float max_contents_scale = active_layer_->MaximumTilingContentsScale();
3313 gfx::Transform scaled_draw_transform = active_layer_->draw_transform();
3314 scaled_draw_transform.Scale(SK_MScalar1 / max_contents_scale,
3315 SK_MScalar1 / max_contents_scale);
3317 AppendQuadsData data;
3318 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3320 // SharedQuadState should have be of size 1, as we are doing AppenQuad once.
3321 EXPECT_EQ(1u, render_pass->shared_quad_state_list.size());
3322 // The content_to_target_transform should be scaled by the
3323 // MaximumTilingContentsScale on the layer.
3324 EXPECT_EQ(scaled_draw_transform.ToString(),
3325 render_pass->shared_quad_state_list[0]
3326 ->content_to_target_transform.ToString());
3327 // The content_bounds should be scaled by the
3328 // MaximumTilingContentsScale on the layer.
3329 EXPECT_EQ(gfx::Size(2500u, 5000u).ToString(),
3330 render_pass->shared_quad_state_list[0]->content_bounds.ToString());
3331 // The visible_content_rect should be scaled by the
3332 // MaximumTilingContentsScale on the layer.
3333 EXPECT_EQ(
3334 gfx::Rect(0u, 0u, 2500u, 5000u).ToString(),
3335 render_pass->shared_quad_state_list[0]->visible_content_rect.ToString());
3338 TEST_F(PictureLayerImplTest, UpdateTilesForMasksWithNoVisibleContent) {
3339 gfx::Size tile_size(400, 400);
3340 gfx::Size bounds(100000, 100);
3342 host_impl_.CreatePendingTree();
3344 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl_.pending_tree(), 1);
3346 scoped_ptr<FakePictureLayerImpl> layer_with_mask =
3347 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 2);
3349 layer_with_mask->SetBounds(bounds);
3350 layer_with_mask->SetContentBounds(bounds);
3352 scoped_refptr<FakePicturePileImpl> pending_pile =
3353 FakePicturePileImpl::CreateFilledPile(tile_size, bounds);
3354 scoped_ptr<FakePictureLayerImpl> mask = FakePictureLayerImpl::CreateWithPile(
3355 host_impl_.pending_tree(), 3, pending_pile);
3357 mask->SetIsMask(true);
3358 mask->SetBounds(bounds);
3359 mask->SetContentBounds(bounds);
3360 mask->SetDrawsContent(true);
3362 FakePictureLayerImpl* pending_mask_content = mask.get();
3363 layer_with_mask->SetMaskLayer(mask.PassAs<LayerImpl>());
3365 scoped_ptr<FakePictureLayerImpl> child_of_layer_with_mask =
3366 FakePictureLayerImpl::Create(host_impl_.pending_tree(), 4);
3368 child_of_layer_with_mask->SetBounds(bounds);
3369 child_of_layer_with_mask->SetContentBounds(bounds);
3370 child_of_layer_with_mask->SetDrawsContent(true);
3372 layer_with_mask->AddChild(child_of_layer_with_mask.PassAs<LayerImpl>());
3374 root->AddChild(layer_with_mask.PassAs<LayerImpl>());
3376 host_impl_.pending_tree()->SetRootLayer(root.Pass());
3378 EXPECT_FALSE(pending_mask_content->tilings());
3379 host_impl_.pending_tree()->UpdateDrawProperties();
3380 EXPECT_NE(0u, pending_mask_content->num_tilings());
3383 class PictureLayerImplTestWithDelegatingRenderer : public PictureLayerImplTest {
3384 public:
3385 PictureLayerImplTestWithDelegatingRenderer() : PictureLayerImplTest() {}
3387 virtual void InitializeRenderer() OVERRIDE {
3388 host_impl_.InitializeRenderer(
3389 FakeOutputSurface::CreateDelegating3d().PassAs<OutputSurface>());
3393 TEST_F(PictureLayerImplTestWithDelegatingRenderer,
3394 DelegatingRendererWithTileOOM) {
3395 // This test is added for crbug.com/402321, where quad should be produced when
3396 // raster on demand is not allowed and tile is OOM.
3397 gfx::Size tile_size = host_impl_.settings().default_tile_size;
3398 gfx::Size layer_bounds(1000, 1000);
3400 // Create tiles.
3401 scoped_refptr<FakePicturePileImpl> pending_pile =
3402 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3403 SetupPendingTree(pending_pile);
3404 pending_layer_->SetBounds(layer_bounds);
3405 host_impl_.SetViewportSize(layer_bounds);
3406 ActivateTree();
3407 host_impl_.active_tree()->UpdateDrawProperties();
3408 std::vector<Tile*> tiles =
3409 active_layer_->HighResTiling()->AllTilesForTesting();
3410 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(tiles);
3412 // Force tiles after max_tiles to be OOM. TileManager uses
3413 // GlobalStateThatImpactsTilesPriority from LayerTreeHostImpl, and we cannot
3414 // directly set state to host_impl_, so we set policy that would change the
3415 // state. We also need to update tree priority separately.
3416 GlobalStateThatImpactsTilePriority state;
3417 size_t max_tiles = 1;
3418 size_t memory_limit = max_tiles * 4 * tile_size.width() * tile_size.height();
3419 size_t resource_limit = max_tiles;
3420 ManagedMemoryPolicy policy(memory_limit,
3421 gpu::MemoryAllocation::CUTOFF_ALLOW_EVERYTHING,
3422 resource_limit);
3423 host_impl_.SetMemoryPolicy(policy);
3424 host_impl_.SetTreePriority(SAME_PRIORITY_FOR_BOTH_TREES);
3425 host_impl_.ManageTiles();
3427 MockOcclusionTracker<LayerImpl> occlusion_tracker;
3428 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
3429 AppendQuadsData data;
3430 active_layer_->WillDraw(DRAW_MODE_HARDWARE, NULL);
3431 active_layer_->AppendQuads(render_pass.get(), occlusion_tracker, &data);
3432 active_layer_->DidDraw(NULL);
3434 // Even when OOM, quads should be produced, and should be different material
3435 // from quads with resource.
3436 EXPECT_LT(max_tiles, render_pass->quad_list.size());
3437 EXPECT_EQ(DrawQuad::Material::TILED_CONTENT,
3438 render_pass->quad_list.front()->material);
3439 EXPECT_EQ(DrawQuad::Material::SOLID_COLOR,
3440 render_pass->quad_list.back()->material);
3443 class OcclusionTrackingSettings : public ImplSidePaintingSettings {
3444 public:
3445 OcclusionTrackingSettings() { use_occlusion_for_tile_prioritization = true; }
3448 class OcclusionTrackingPictureLayerImplTest : public PictureLayerImplTest {
3449 public:
3450 OcclusionTrackingPictureLayerImplTest()
3451 : PictureLayerImplTest(OcclusionTrackingSettings()) {}
3453 void VerifyEvictionConsidersOcclusion(
3454 PictureLayerImpl* layer,
3455 size_t expected_occluded_tile_count[NUM_TREE_PRIORITIES]) {
3456 for (int priority_count = 0; priority_count < NUM_TREE_PRIORITIES;
3457 ++priority_count) {
3458 TreePriority tree_priority = static_cast<TreePriority>(priority_count);
3459 size_t occluded_tile_count = 0u;
3460 Tile* last_tile = NULL;
3462 for (PictureLayerImpl::LayerEvictionTileIterator it =
3463 PictureLayerImpl::LayerEvictionTileIterator(layer,
3464 tree_priority);
3466 ++it) {
3467 Tile* tile = *it;
3468 if (!last_tile)
3469 last_tile = tile;
3471 // The only way we will encounter an occluded tile after an unoccluded
3472 // tile is if the priorty bin decreased, the tile is required for
3473 // activation, or the scale changed.
3474 bool tile_is_occluded =
3475 tile->is_occluded_for_tree_priority(tree_priority);
3476 if (tile_is_occluded) {
3477 occluded_tile_count++;
3479 bool last_tile_is_occluded =
3480 last_tile->is_occluded_for_tree_priority(tree_priority);
3481 if (!last_tile_is_occluded) {
3482 TilePriority::PriorityBin tile_priority_bin =
3483 tile->priority_for_tree_priority(tree_priority).priority_bin;
3484 TilePriority::PriorityBin last_tile_priority_bin =
3485 last_tile->priority_for_tree_priority(tree_priority)
3486 .priority_bin;
3488 EXPECT_TRUE(
3489 (tile_priority_bin < last_tile_priority_bin) ||
3490 tile->required_for_activation() ||
3491 (tile->contents_scale() != last_tile->contents_scale()));
3494 last_tile = tile;
3496 EXPECT_EQ(expected_occluded_tile_count[priority_count],
3497 occluded_tile_count);
3502 TEST_F(OcclusionTrackingPictureLayerImplTest,
3503 OccludedTilesSkippedDuringRasterization) {
3504 base::TimeTicks time_ticks;
3505 time_ticks += base::TimeDelta::FromMilliseconds(1);
3506 host_impl_.SetCurrentBeginFrameArgs(
3507 CreateBeginFrameArgsForTesting(time_ticks));
3509 gfx::Size tile_size(102, 102);
3510 gfx::Size layer_bounds(1000, 1000);
3511 gfx::Size viewport_size(500, 500);
3512 gfx::Point occluding_layer_position(310, 0);
3514 scoped_refptr<FakePicturePileImpl> pending_pile =
3515 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3516 SetupPendingTree(pending_pile);
3517 pending_layer_->set_fixed_tile_size(tile_size);
3519 host_impl_.SetViewportSize(viewport_size);
3520 host_impl_.pending_tree()->UpdateDrawProperties();
3522 // No occlusion.
3523 int unoccluded_tile_count = 0;
3524 for (PictureLayerImpl::LayerRasterTileIterator it =
3525 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3527 ++it) {
3528 Tile* tile = *it;
3530 // Occluded tiles should not be iterated over.
3531 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3533 // Some tiles may not be visible (i.e. outside the viewport). The rest are
3534 // visible and at least partially unoccluded, verified by the above expect.
3535 bool tile_is_visible =
3536 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3537 if (tile_is_visible)
3538 unoccluded_tile_count++;
3540 EXPECT_EQ(unoccluded_tile_count, 25 + 4);
3542 // Partial occlusion.
3543 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3544 LayerImpl* layer1 = pending_layer_->children()[0];
3545 layer1->SetBounds(layer_bounds);
3546 layer1->SetContentBounds(layer_bounds);
3547 layer1->SetDrawsContent(true);
3548 layer1->SetContentsOpaque(true);
3549 layer1->SetPosition(occluding_layer_position);
3551 time_ticks += base::TimeDelta::FromMilliseconds(200);
3552 host_impl_.SetCurrentBeginFrameArgs(
3553 CreateBeginFrameArgsForTesting(time_ticks));
3554 host_impl_.pending_tree()->UpdateDrawProperties();
3556 unoccluded_tile_count = 0;
3557 for (PictureLayerImpl::LayerRasterTileIterator it =
3558 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3560 ++it) {
3561 Tile* tile = *it;
3563 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3565 bool tile_is_visible =
3566 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3567 if (tile_is_visible)
3568 unoccluded_tile_count++;
3570 EXPECT_EQ(unoccluded_tile_count, 20 + 2);
3572 // Full occlusion.
3573 layer1->SetPosition(gfx::Point(0, 0));
3575 time_ticks += base::TimeDelta::FromMilliseconds(200);
3576 host_impl_.SetCurrentBeginFrameArgs(
3577 CreateBeginFrameArgsForTesting(time_ticks));
3578 host_impl_.pending_tree()->UpdateDrawProperties();
3580 unoccluded_tile_count = 0;
3581 for (PictureLayerImpl::LayerRasterTileIterator it =
3582 PictureLayerImpl::LayerRasterTileIterator(pending_layer_, false);
3584 ++it) {
3585 Tile* tile = *it;
3587 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3589 bool tile_is_visible =
3590 tile->content_rect().Intersects(pending_layer_->visible_content_rect());
3591 if (tile_is_visible)
3592 unoccluded_tile_count++;
3594 EXPECT_EQ(unoccluded_tile_count, 0);
3597 TEST_F(OcclusionTrackingPictureLayerImplTest,
3598 OccludedTilesNotMarkedAsRequired) {
3599 base::TimeTicks time_ticks;
3600 time_ticks += base::TimeDelta::FromMilliseconds(1);
3601 host_impl_.SetCurrentBeginFrameArgs(
3602 CreateBeginFrameArgsForTesting(time_ticks));
3604 gfx::Size tile_size(102, 102);
3605 gfx::Size layer_bounds(1000, 1000);
3606 gfx::Size viewport_size(500, 500);
3607 gfx::Point occluding_layer_position(310, 0);
3609 scoped_refptr<FakePicturePileImpl> pending_pile =
3610 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3611 SetupPendingTree(pending_pile);
3612 pending_layer_->set_fixed_tile_size(tile_size);
3614 host_impl_.SetViewportSize(viewport_size);
3615 host_impl_.pending_tree()->UpdateDrawProperties();
3617 // No occlusion.
3618 int occluded_tile_count = 0;
3619 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3620 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3622 occluded_tile_count = 0;
3623 for (PictureLayerTiling::CoverageIterator iter(
3624 tiling,
3625 pending_layer_->contents_scale_x(),
3626 gfx::Rect(layer_bounds));
3627 iter;
3628 ++iter) {
3629 if (!*iter)
3630 continue;
3631 const Tile* tile = *iter;
3633 // Fully occluded tiles are not required for activation.
3634 if (tile->is_occluded(PENDING_TREE)) {
3635 EXPECT_FALSE(tile->required_for_activation());
3636 occluded_tile_count++;
3639 EXPECT_EQ(occluded_tile_count, 0);
3642 // Partial occlusion.
3643 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3644 LayerImpl* layer1 = pending_layer_->children()[0];
3645 layer1->SetBounds(layer_bounds);
3646 layer1->SetContentBounds(layer_bounds);
3647 layer1->SetDrawsContent(true);
3648 layer1->SetContentsOpaque(true);
3649 layer1->SetPosition(occluding_layer_position);
3651 time_ticks += base::TimeDelta::FromMilliseconds(200);
3652 host_impl_.SetCurrentBeginFrameArgs(
3653 CreateBeginFrameArgsForTesting(time_ticks));
3654 host_impl_.pending_tree()->UpdateDrawProperties();
3656 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3657 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3659 occluded_tile_count = 0;
3660 for (PictureLayerTiling::CoverageIterator iter(
3661 tiling,
3662 pending_layer_->contents_scale_x(),
3663 gfx::Rect(layer_bounds));
3664 iter;
3665 ++iter) {
3666 if (!*iter)
3667 continue;
3668 const Tile* tile = *iter;
3670 if (tile->is_occluded(PENDING_TREE)) {
3671 EXPECT_FALSE(tile->required_for_activation());
3672 occluded_tile_count++;
3675 switch (i) {
3676 case 0:
3677 EXPECT_EQ(occluded_tile_count, 5);
3678 break;
3679 case 1:
3680 EXPECT_EQ(occluded_tile_count, 2);
3681 break;
3682 default:
3683 NOTREACHED();
3687 // Full occlusion.
3688 layer1->SetPosition(gfx::PointF(0, 0));
3690 time_ticks += base::TimeDelta::FromMilliseconds(200);
3691 host_impl_.SetCurrentBeginFrameArgs(
3692 CreateBeginFrameArgsForTesting(time_ticks));
3693 host_impl_.pending_tree()->UpdateDrawProperties();
3695 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3696 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3698 occluded_tile_count = 0;
3699 for (PictureLayerTiling::CoverageIterator iter(
3700 tiling,
3701 pending_layer_->contents_scale_x(),
3702 gfx::Rect(layer_bounds));
3703 iter;
3704 ++iter) {
3705 if (!*iter)
3706 continue;
3707 const Tile* tile = *iter;
3709 if (tile->is_occluded(PENDING_TREE)) {
3710 EXPECT_FALSE(tile->required_for_activation());
3711 occluded_tile_count++;
3714 switch (i) {
3715 case 0:
3716 EXPECT_EQ(occluded_tile_count, 25);
3717 break;
3718 case 1:
3719 EXPECT_EQ(occluded_tile_count, 4);
3720 break;
3721 default:
3722 NOTREACHED();
3727 TEST_F(OcclusionTrackingPictureLayerImplTest, OcclusionForDifferentScales) {
3728 gfx::Size tile_size(102, 102);
3729 gfx::Size layer_bounds(1000, 1000);
3730 gfx::Size viewport_size(500, 500);
3731 gfx::Point occluding_layer_position(310, 0);
3733 scoped_refptr<FakePicturePileImpl> pending_pile =
3734 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3735 SetupPendingTree(pending_pile);
3736 pending_layer_->set_fixed_tile_size(tile_size);
3738 ASSERT_TRUE(pending_layer_->CanHaveTilings());
3740 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3742 std::vector<PictureLayerTiling*> tilings;
3743 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3744 tilings.push_back(pending_layer_->AddTiling(0.3f));
3745 tilings.push_back(pending_layer_->AddTiling(0.7f));
3746 tilings.push_back(pending_layer_->AddTiling(1.0f));
3747 tilings.push_back(pending_layer_->AddTiling(2.0f));
3749 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3750 LayerImpl* layer1 = pending_layer_->children()[0];
3751 layer1->SetBounds(layer_bounds);
3752 layer1->SetContentBounds(layer_bounds);
3753 layer1->SetDrawsContent(true);
3754 layer1->SetContentsOpaque(true);
3755 layer1->SetPosition(occluding_layer_position);
3757 host_impl_.SetViewportSize(viewport_size);
3758 host_impl_.pending_tree()->UpdateDrawProperties();
3760 int tiling_count = 0;
3761 int occluded_tile_count = 0;
3762 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
3763 tilings.begin();
3764 tiling_iterator != tilings.end();
3765 ++tiling_iterator) {
3766 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
3768 occluded_tile_count = 0;
3769 for (size_t i = 0; i < tiles.size(); ++i) {
3770 if (tiles[i]->is_occluded(PENDING_TREE)) {
3771 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3772 tiles[i]->content_rect(), 1.0f / tiles[i]->contents_scale());
3773 EXPECT_GE(scaled_content_rect.x(), occluding_layer_position.x());
3774 occluded_tile_count++;
3777 switch (tiling_count) {
3778 case 0:
3779 case 1:
3780 EXPECT_EQ(occluded_tile_count, 2);
3781 break;
3782 case 2:
3783 EXPECT_EQ(occluded_tile_count, 4);
3784 break;
3785 case 3:
3786 EXPECT_EQ(occluded_tile_count, 5);
3787 break;
3788 case 4:
3789 EXPECT_EQ(occluded_tile_count, 30);
3790 break;
3791 default:
3792 NOTREACHED();
3795 tiling_count++;
3798 EXPECT_EQ(tiling_count, 5);
3801 TEST_F(OcclusionTrackingPictureLayerImplTest, DifferentOcclusionOnTrees) {
3802 gfx::Size tile_size(102, 102);
3803 gfx::Size layer_bounds(1000, 1000);
3804 gfx::Size viewport_size(1000, 1000);
3805 gfx::Point occluding_layer_position(310, 0);
3806 gfx::Rect invalidation_rect(230, 230, 102, 102);
3808 scoped_refptr<FakePicturePileImpl> pending_pile =
3809 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3810 scoped_refptr<FakePicturePileImpl> active_pile =
3811 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3812 SetupTrees(pending_pile, active_pile);
3814 // Partially occlude the active layer.
3815 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3816 LayerImpl* layer1 = active_layer_->children()[0];
3817 layer1->SetBounds(layer_bounds);
3818 layer1->SetContentBounds(layer_bounds);
3819 layer1->SetDrawsContent(true);
3820 layer1->SetContentsOpaque(true);
3821 layer1->SetPosition(occluding_layer_position);
3823 // Partially invalidate the pending layer.
3824 pending_layer_->set_invalidation(invalidation_rect);
3826 host_impl_.SetViewportSize(viewport_size);
3828 active_layer_->CreateDefaultTilingsAndTiles();
3829 pending_layer_->CreateDefaultTilingsAndTiles();
3831 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3832 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3834 for (PictureLayerTiling::CoverageIterator iter(
3835 tiling,
3836 pending_layer_->contents_scale_x(),
3837 gfx::Rect(layer_bounds));
3838 iter;
3839 ++iter) {
3840 if (!*iter)
3841 continue;
3842 const Tile* tile = *iter;
3844 // All tiles are unoccluded on the pending tree.
3845 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3847 Tile* twin_tile =
3848 pending_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3849 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3850 tile->content_rect(), 1.0f / tile->contents_scale());
3852 if (scaled_content_rect.Intersects(invalidation_rect)) {
3853 // Tiles inside the invalidation rect are only on the pending tree.
3854 EXPECT_NE(tile, twin_tile);
3856 // Unshared tiles should be unoccluded on the active tree by default.
3857 EXPECT_FALSE(tile->is_occluded(ACTIVE_TREE));
3858 } else {
3859 // Tiles outside the invalidation rect are shared between both trees.
3860 EXPECT_EQ(tile, twin_tile);
3861 // Shared tiles are occluded on the active tree iff they lie beneath the
3862 // occluding layer.
3863 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3864 scaled_content_rect.x() >= occluding_layer_position.x());
3869 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
3870 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
3872 for (PictureLayerTiling::CoverageIterator iter(
3873 tiling,
3874 active_layer_->contents_scale_x(),
3875 gfx::Rect(layer_bounds));
3876 iter;
3877 ++iter) {
3878 if (!*iter)
3879 continue;
3880 const Tile* tile = *iter;
3882 Tile* twin_tile =
3883 active_layer_->GetTwinTiling(tiling)->TileAt(iter.i(), iter.j());
3884 gfx::Rect scaled_content_rect = ScaleToEnclosingRect(
3885 tile->content_rect(), 1.0f / tile->contents_scale());
3887 // Since we've already checked the shared tiles, only consider tiles in
3888 // the invalidation rect.
3889 if (scaled_content_rect.Intersects(invalidation_rect)) {
3890 // Tiles inside the invalidation rect are only on the active tree.
3891 EXPECT_NE(tile, twin_tile);
3893 // Unshared tiles should be unoccluded on the pending tree by default.
3894 EXPECT_FALSE(tile->is_occluded(PENDING_TREE));
3896 // Unshared tiles are occluded on the active tree iff they lie beneath
3897 // the occluding layer.
3898 EXPECT_EQ(tile->is_occluded(ACTIVE_TREE),
3899 scaled_content_rect.x() >= occluding_layer_position.x());
3905 TEST_F(OcclusionTrackingPictureLayerImplTest,
3906 OccludedTilesConsideredDuringEviction) {
3907 gfx::Size tile_size(102, 102);
3908 gfx::Size layer_bounds(1000, 1000);
3909 gfx::Size viewport_size(500, 500);
3910 gfx::Point pending_occluding_layer_position(310, 0);
3911 gfx::Point active_occluding_layer_position(0, 310);
3912 gfx::Rect invalidation_rect(230, 230, 102, 102);
3914 scoped_refptr<FakePicturePileImpl> pending_pile =
3915 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3916 scoped_refptr<FakePicturePileImpl> active_pile =
3917 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
3918 SetupTrees(pending_pile, active_pile);
3920 pending_layer_->set_fixed_tile_size(tile_size);
3921 active_layer_->set_fixed_tile_size(tile_size);
3923 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor;
3925 std::vector<PictureLayerTiling*> tilings;
3926 tilings.push_back(pending_layer_->AddTiling(low_res_factor));
3927 tilings.push_back(pending_layer_->AddTiling(0.3f));
3928 tilings.push_back(pending_layer_->AddTiling(0.7f));
3929 tilings.push_back(pending_layer_->AddTiling(1.0f));
3930 tilings.push_back(pending_layer_->AddTiling(2.0f));
3932 EXPECT_EQ(5u, pending_layer_->num_tilings());
3933 EXPECT_EQ(5u, active_layer_->num_tilings());
3935 // Partially occlude the pending layer.
3936 pending_layer_->AddChild(LayerImpl::Create(host_impl_.pending_tree(), 1));
3937 LayerImpl* pending_occluding_layer = pending_layer_->children()[0];
3938 pending_occluding_layer->SetBounds(layer_bounds);
3939 pending_occluding_layer->SetContentBounds(layer_bounds);
3940 pending_occluding_layer->SetDrawsContent(true);
3941 pending_occluding_layer->SetContentsOpaque(true);
3942 pending_occluding_layer->SetPosition(pending_occluding_layer_position);
3944 // Partially occlude the active layer.
3945 active_layer_->AddChild(LayerImpl::Create(host_impl_.active_tree(), 2));
3946 LayerImpl* active_occluding_layer = active_layer_->children()[0];
3947 active_occluding_layer->SetBounds(layer_bounds);
3948 active_occluding_layer->SetContentBounds(layer_bounds);
3949 active_occluding_layer->SetDrawsContent(true);
3950 active_occluding_layer->SetContentsOpaque(true);
3951 active_occluding_layer->SetPosition(active_occluding_layer_position);
3953 // Partially invalidate the pending layer. Tiles inside the invalidation rect
3954 // are not shared between trees.
3955 pending_layer_->set_invalidation(invalidation_rect);
3957 host_impl_.SetViewportSize(viewport_size);
3958 host_impl_.active_tree()->UpdateDrawProperties();
3959 host_impl_.pending_tree()->UpdateDrawProperties();
3961 // The expected number of occluded tiles on each of the 5 tilings for each of
3962 // the 3 tree priorities.
3963 size_t expected_occluded_tile_count_on_both[] = {9u, 1u, 1u, 1u, 1u};
3964 size_t expected_occluded_tile_count_on_active[] = {30u, 5u, 4u, 2u, 2u};
3965 size_t expected_occluded_tile_count_on_pending[] = {30u, 5u, 4u, 2u, 2u};
3967 // The total expected number of occluded tiles on all tilings for each of the
3968 // 3 tree priorities.
3969 size_t total_expected_occluded_tile_count[] = {13u, 43u, 43u};
3971 ASSERT_EQ(arraysize(total_expected_occluded_tile_count), NUM_TREE_PRIORITIES);
3973 // Verify number of occluded tiles on the pending layer for each tiling.
3974 for (size_t i = 0; i < pending_layer_->num_tilings(); ++i) {
3975 PictureLayerTiling* tiling = pending_layer_->tilings()->tiling_at(i);
3976 tiling->CreateAllTilesForTesting();
3978 size_t occluded_tile_count_on_pending = 0u;
3979 size_t occluded_tile_count_on_active = 0u;
3980 size_t occluded_tile_count_on_both = 0u;
3981 for (PictureLayerTiling::CoverageIterator iter(
3982 tiling,
3983 pending_layer_->contents_scale_x(),
3984 gfx::Rect(layer_bounds));
3985 iter;
3986 ++iter) {
3987 Tile* tile = *iter;
3989 if (tile->is_occluded(PENDING_TREE))
3990 occluded_tile_count_on_pending++;
3991 if (tile->is_occluded(ACTIVE_TREE))
3992 occluded_tile_count_on_active++;
3993 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
3994 occluded_tile_count_on_both++;
3996 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
3997 occluded_tile_count_on_pending)
3998 << i;
3999 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4000 occluded_tile_count_on_active)
4001 << i;
4002 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4003 occluded_tile_count_on_both)
4004 << i;
4007 // Verify number of occluded tiles on the active layer for each tiling.
4008 for (size_t i = 0; i < active_layer_->num_tilings(); ++i) {
4009 PictureLayerTiling* tiling = active_layer_->tilings()->tiling_at(i);
4010 tiling->CreateAllTilesForTesting();
4012 size_t occluded_tile_count_on_pending = 0u;
4013 size_t occluded_tile_count_on_active = 0u;
4014 size_t occluded_tile_count_on_both = 0u;
4015 for (PictureLayerTiling::CoverageIterator iter(
4016 tiling,
4017 pending_layer_->contents_scale_x(),
4018 gfx::Rect(layer_bounds));
4019 iter;
4020 ++iter) {
4021 Tile* tile = *iter;
4023 if (tile->is_occluded(PENDING_TREE))
4024 occluded_tile_count_on_pending++;
4025 if (tile->is_occluded(ACTIVE_TREE))
4026 occluded_tile_count_on_active++;
4027 if (tile->is_occluded(PENDING_TREE) && tile->is_occluded(ACTIVE_TREE))
4028 occluded_tile_count_on_both++;
4030 EXPECT_EQ(expected_occluded_tile_count_on_pending[i],
4031 occluded_tile_count_on_pending)
4032 << i;
4033 EXPECT_EQ(expected_occluded_tile_count_on_active[i],
4034 occluded_tile_count_on_active)
4035 << i;
4036 EXPECT_EQ(expected_occluded_tile_count_on_both[i],
4037 occluded_tile_count_on_both)
4038 << i;
4041 std::vector<Tile*> all_tiles;
4042 for (std::vector<PictureLayerTiling*>::iterator tiling_iterator =
4043 tilings.begin();
4044 tiling_iterator != tilings.end();
4045 ++tiling_iterator) {
4046 std::vector<Tile*> tiles = (*tiling_iterator)->AllTilesForTesting();
4047 std::copy(tiles.begin(), tiles.end(), std::back_inserter(all_tiles));
4050 host_impl_.tile_manager()->InitializeTilesWithResourcesForTesting(all_tiles);
4052 VerifyEvictionConsidersOcclusion(pending_layer_,
4053 total_expected_occluded_tile_count);
4054 VerifyEvictionConsidersOcclusion(active_layer_,
4055 total_expected_occluded_tile_count);
4058 TEST_F(PictureLayerImplTest, RecycledTwinLayer) {
4059 gfx::Size tile_size(102, 102);
4060 gfx::Size layer_bounds(1000, 1000);
4062 scoped_refptr<FakePicturePileImpl> pile =
4063 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds);
4064 SetupPendingTree(pile);
4065 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4067 ActivateTree();
4068 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4069 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4071 SetupPendingTree(pile);
4072 EXPECT_FALSE(pending_layer_->GetRecycledTwinLayer());
4073 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4075 ActivateTree();
4076 EXPECT_TRUE(active_layer_->GetRecycledTwinLayer());
4077 EXPECT_EQ(old_pending_layer_, active_layer_->GetRecycledTwinLayer());
4079 host_impl_.ResetRecycleTreeForTesting();
4080 EXPECT_FALSE(active_layer_->GetRecycledTwinLayer());
4083 } // namespace
4084 } // namespace cc