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. 4 5 #include "cc/layers/picture_layer_impl.h" 6 7 #include <utility> 8 9 #include "cc/layers/append_quads_data.h" 10 #include "cc/layers/picture_layer.h" 11 #include "cc/test/fake_content_layer_client.h" 12 #include "cc/test/fake_impl_proxy.h" 13 #include "cc/test/fake_layer_tree_host_impl.h" 14 #include "cc/test/fake_output_surface.h" 15 #include "cc/test/fake_picture_layer_impl.h" 16 #include "cc/test/fake_picture_pile_impl.h" 17 #include "cc/test/geometry_test_utils.h" 18 #include "cc/test/impl_side_painting_settings.h" 19 #include "cc/test/mock_quad_culler.h" 20 #include "cc/test/test_web_graphics_context_3d.h" 21 #include "cc/trees/layer_tree_impl.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "third_party/skia/include/core/SkBitmapDevice.h" 24 #include "ui/gfx/rect_conversions.h" 25 26 namespace cc { 27 namespace { 28 29 class MockCanvas : public SkCanvas { 30 public: 31 explicit MockCanvas(SkBaseDevice* device) : SkCanvas(device) {} 32 33 virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE { 34 // Capture calls before SkCanvas quickReject() kicks in. 35 rects_.push_back(rect); 36 } 37 38 std::vector<SkRect> rects_; 39 }; 40 41 class PictureLayerImplTest : public testing::Test { 42 public: 43 PictureLayerImplTest() 44 : host_impl_(ImplSidePaintingSettings(), &proxy_), id_(7) {} 45 46 explicit PictureLayerImplTest(const LayerTreeSettings& settings) 47 : host_impl_(settings, &proxy_), id_(7) {} 48 49 virtual ~PictureLayerImplTest() { 50 } 51 52 virtual void SetUp() OVERRIDE { 53 InitializeRenderer(); 54 } 55 56 virtual void InitializeRenderer() { 57 host_impl_.InitializeRenderer(CreateFakeOutputSurface()); 58 } 59 60 void SetupDefaultTrees(gfx::Size layer_bounds) { 61 gfx::Size tile_size(100, 100); 62 63 scoped_refptr<FakePicturePileImpl> pending_pile = 64 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 65 scoped_refptr<FakePicturePileImpl> active_pile = 66 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 67 68 SetupTrees(pending_pile, active_pile); 69 } 70 71 void ActivateTree() { 72 host_impl_.ActivatePendingTree(); 73 CHECK(!host_impl_.pending_tree()); 74 pending_layer_ = NULL; 75 active_layer_ = static_cast<FakePictureLayerImpl*>( 76 host_impl_.active_tree()->LayerById(id_)); 77 } 78 79 void SetupDefaultTreesWithFixedTileSize(gfx::Size layer_bounds, 80 gfx::Size tile_size) { 81 SetupDefaultTrees(layer_bounds); 82 pending_layer_->set_fixed_tile_size(tile_size); 83 active_layer_->set_fixed_tile_size(tile_size); 84 } 85 86 void SetupTrees( 87 scoped_refptr<PicturePileImpl> pending_pile, 88 scoped_refptr<PicturePileImpl> active_pile) { 89 SetupPendingTree(active_pile); 90 ActivateTree(); 91 SetupPendingTree(pending_pile); 92 } 93 94 void CreateHighLowResAndSetAllTilesVisible() { 95 // Active layer must get updated first so pending layer can share from it. 96 active_layer_->CreateDefaultTilingsAndTiles(); 97 active_layer_->SetAllTilesVisible(); 98 pending_layer_->CreateDefaultTilingsAndTiles(); 99 pending_layer_->SetAllTilesVisible(); 100 } 101 102 void AddDefaultTilingsWithInvalidation(const Region& invalidation) { 103 active_layer_->AddTiling(2.3f); 104 active_layer_->AddTiling(1.0f); 105 active_layer_->AddTiling(0.5f); 106 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i) 107 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting(); 108 pending_layer_->set_invalidation(invalidation); 109 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i) 110 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting(); 111 } 112 113 void SetupPendingTree( 114 scoped_refptr<PicturePileImpl> pile) { 115 host_impl_.CreatePendingTree(); 116 LayerTreeImpl* pending_tree = host_impl_.pending_tree(); 117 // Clear recycled tree. 118 pending_tree->DetachLayerTree(); 119 120 scoped_ptr<FakePictureLayerImpl> pending_layer = 121 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile); 122 pending_layer->SetDrawsContent(true); 123 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>()); 124 125 pending_layer_ = static_cast<FakePictureLayerImpl*>( 126 host_impl_.pending_tree()->LayerById(id_)); 127 pending_layer_->DoPostCommitInitializationIfNeeded(); 128 } 129 130 static void VerifyAllTilesExistAndHavePile( 131 const PictureLayerTiling* tiling, 132 PicturePileImpl* pile) { 133 for (PictureLayerTiling::CoverageIterator 134 iter(tiling, tiling->contents_scale(), tiling->ContentRect()); 135 iter; 136 ++iter) { 137 EXPECT_TRUE(*iter); 138 EXPECT_EQ(pile, iter->picture_pile()); 139 } 140 } 141 142 void SetContentsScaleOnBothLayers(float contents_scale, 143 float device_scale_factor, 144 float page_scale_factor, 145 bool animating_transform) { 146 float result_scale_x, result_scale_y; 147 gfx::Size result_bounds; 148 pending_layer_->CalculateContentsScale( 149 contents_scale, 150 device_scale_factor, 151 page_scale_factor, 152 animating_transform, 153 &result_scale_x, 154 &result_scale_y, 155 &result_bounds); 156 active_layer_->CalculateContentsScale( 157 contents_scale, 158 device_scale_factor, 159 page_scale_factor, 160 animating_transform, 161 &result_scale_x, 162 &result_scale_y, 163 &result_bounds); 164 } 165 166 void ResetTilingsAndRasterScales() { 167 pending_layer_->DidLoseOutputSurface(); 168 active_layer_->DidLoseOutputSurface(); 169 } 170 171 void AssertAllTilesRequired(PictureLayerTiling* tiling) { 172 std::vector<Tile*> tiles = tiling->AllTilesForTesting(); 173 for (size_t i = 0; i < tiles.size(); ++i) 174 EXPECT_TRUE(tiles[i]->required_for_activation()) << "i: " << i; 175 EXPECT_GT(tiles.size(), 0u); 176 } 177 178 void AssertNoTilesRequired(PictureLayerTiling* tiling) { 179 std::vector<Tile*> tiles = tiling->AllTilesForTesting(); 180 for (size_t i = 0; i < tiles.size(); ++i) 181 EXPECT_FALSE(tiles[i]->required_for_activation()) << "i: " << i; 182 EXPECT_GT(tiles.size(), 0u); 183 } 184 185 protected: 186 void TestTileGridAlignmentCommon() { 187 // Layer to span 4 raster tiles in x and in y 188 ImplSidePaintingSettings settings; 189 gfx::Size layer_size( 190 settings.default_tile_size.width() * 7 / 2, 191 settings.default_tile_size.height() * 7 / 2); 192 193 scoped_refptr<FakePicturePileImpl> pending_pile = 194 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size); 195 scoped_refptr<FakePicturePileImpl> active_pile = 196 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size); 197 198 SetupTrees(pending_pile, active_pile); 199 200 float result_scale_x, result_scale_y; 201 gfx::Size result_bounds; 202 active_layer_->CalculateContentsScale( 203 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 204 205 // Add 1x1 rects at the centers of each tile, then re-record pile contents 206 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 207 std::vector<Tile*> tiles = 208 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting(); 209 EXPECT_EQ(16u, tiles.size()); 210 std::vector<SkRect> rects; 211 std::vector<Tile*>::const_iterator tile_iter; 212 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) { 213 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint(); 214 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1); 215 active_pile->add_draw_rect(rect); 216 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1)); 217 } 218 // Force re-record with newly injected content 219 active_pile->RemoveRecordingAt(0, 0); 220 active_pile->AddRecordingAt(0, 0); 221 222 SkBitmap store; 223 store.setConfig(SkBitmap::kNo_Config, 1000, 1000); 224 SkBitmapDevice device(store); 225 226 std::vector<SkRect>::const_iterator rect_iter = rects.begin(); 227 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) { 228 MockCanvas mock_canvas(&device); 229 active_pile->RasterDirect( 230 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL); 231 232 // This test verifies that when drawing the contents of a specific tile 233 // at content scale 1.0, the playback canvas never receives content from 234 // neighboring tiles which indicates that the tile grid embedded in 235 // SkPicture is perfectly aligned with the compositor's tiles. 236 EXPECT_EQ(1u, mock_canvas.rects_.size()); 237 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]); 238 rect_iter++; 239 } 240 } 241 242 FakeImplProxy proxy_; 243 FakeLayerTreeHostImpl host_impl_; 244 int id_; 245 FakePictureLayerImpl* pending_layer_; 246 FakePictureLayerImpl* active_layer_; 247 248 private: 249 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest); 250 }; 251 252 TEST_F(PictureLayerImplTest, TileGridAlignment) { 253 host_impl_.SetDeviceScaleFactor(1.f); 254 TestTileGridAlignmentCommon(); 255 } 256 257 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) { 258 host_impl_.SetDeviceScaleFactor(2.f); 259 TestTileGridAlignmentCommon(); 260 } 261 262 TEST_F(PictureLayerImplTest, CloneNoInvalidation) { 263 gfx::Size tile_size(100, 100); 264 gfx::Size layer_bounds(400, 400); 265 266 scoped_refptr<FakePicturePileImpl> pending_pile = 267 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 268 scoped_refptr<FakePicturePileImpl> active_pile = 269 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 270 271 SetupTrees(pending_pile, active_pile); 272 273 Region invalidation; 274 AddDefaultTilingsWithInvalidation(invalidation); 275 276 EXPECT_EQ(pending_layer_->tilings()->num_tilings(), 277 active_layer_->tilings()->num_tilings()); 278 279 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 280 EXPECT_GT(tilings->num_tilings(), 0u); 281 for (size_t i = 0; i < tilings->num_tilings(); ++i) 282 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get()); 283 } 284 285 TEST_F(PictureLayerImplTest, SuppressUpdateTilePriorities) { 286 base::TimeTicks time_ticks; 287 host_impl_.SetCurrentFrameTimeTicks(time_ticks); 288 289 gfx::Size tile_size(100, 100); 290 gfx::Size layer_bounds(400, 400); 291 292 scoped_refptr<FakePicturePileImpl> pending_pile = 293 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 294 scoped_refptr<FakePicturePileImpl> active_pile = 295 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 296 297 SetupTrees(pending_pile, active_pile); 298 299 Region invalidation; 300 AddDefaultTilingsWithInvalidation(invalidation); 301 float dummy_contents_scale_x; 302 float dummy_contents_scale_y; 303 gfx::Size dummy_content_bounds; 304 active_layer_->CalculateContentsScale(1.f, 305 1.f, 306 1.f, 307 false, 308 &dummy_contents_scale_x, 309 &dummy_contents_scale_y, 310 &dummy_content_bounds); 311 312 EXPECT_TRUE(host_impl_.manage_tiles_needed()); 313 active_layer_->UpdateTilePriorities(); 314 host_impl_.ManageTiles(); 315 EXPECT_FALSE(host_impl_.manage_tiles_needed()); 316 317 time_ticks += base::TimeDelta::FromMilliseconds(200); 318 host_impl_.SetCurrentFrameTimeTicks(time_ticks); 319 320 // Setting this boolean should cause an early out in UpdateTilePriorities. 321 bool valid_for_tile_management = false; 322 host_impl_.SetExternalDrawConstraints(gfx::Transform(), 323 gfx::Rect(layer_bounds), 324 gfx::Rect(layer_bounds), 325 valid_for_tile_management); 326 active_layer_->UpdateTilePriorities(); 327 EXPECT_FALSE(host_impl_.manage_tiles_needed()); 328 329 time_ticks += base::TimeDelta::FromMilliseconds(200); 330 host_impl_.SetCurrentFrameTimeTicks(time_ticks); 331 332 valid_for_tile_management = true; 333 host_impl_.SetExternalDrawConstraints(gfx::Transform(), 334 gfx::Rect(layer_bounds), 335 gfx::Rect(layer_bounds), 336 valid_for_tile_management); 337 active_layer_->UpdateTilePriorities(); 338 EXPECT_TRUE(host_impl_.manage_tiles_needed()); 339 } 340 341 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) { 342 gfx::Size tile_size(100, 100); 343 gfx::Size layer_bounds(400, 400); 344 gfx::Rect layer_invalidation(150, 200, 30, 180); 345 346 scoped_refptr<FakePicturePileImpl> pending_pile = 347 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 348 scoped_refptr<FakePicturePileImpl> active_pile = 349 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 350 351 SetupTrees(pending_pile, active_pile); 352 353 Region invalidation(layer_invalidation); 354 AddDefaultTilingsWithInvalidation(invalidation); 355 356 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 357 EXPECT_GT(tilings->num_tilings(), 0u); 358 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 359 const PictureLayerTiling* tiling = tilings->tiling_at(i); 360 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect( 361 layer_invalidation, 362 tiling->contents_scale()); 363 for (PictureLayerTiling::CoverageIterator 364 iter(tiling, 365 tiling->contents_scale(), 366 tiling->ContentRect()); 367 iter; 368 ++iter) { 369 EXPECT_TRUE(*iter); 370 EXPECT_FALSE(iter.geometry_rect().IsEmpty()); 371 if (iter.geometry_rect().Intersects(content_invalidation)) 372 EXPECT_EQ(pending_pile, iter->picture_pile()); 373 else 374 EXPECT_EQ(active_pile, iter->picture_pile()); 375 } 376 } 377 } 378 379 TEST_F(PictureLayerImplTest, CloneFullInvalidation) { 380 gfx::Size tile_size(90, 80); 381 gfx::Size layer_bounds(300, 500); 382 383 scoped_refptr<FakePicturePileImpl> pending_pile = 384 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 385 scoped_refptr<FakePicturePileImpl> active_pile = 386 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 387 388 SetupTrees(pending_pile, active_pile); 389 390 Region invalidation((gfx::Rect(layer_bounds))); 391 AddDefaultTilingsWithInvalidation(invalidation); 392 393 EXPECT_EQ(pending_layer_->tilings()->num_tilings(), 394 active_layer_->tilings()->num_tilings()); 395 396 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 397 EXPECT_GT(tilings->num_tilings(), 0u); 398 for (size_t i = 0; i < tilings->num_tilings(); ++i) 399 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get()); 400 } 401 402 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) { 403 gfx::Size tile_size(90, 80); 404 gfx::Size active_layer_bounds(300, 500); 405 gfx::Size pending_layer_bounds(400, 800); 406 407 scoped_refptr<FakePicturePileImpl> pending_pile = 408 FakePicturePileImpl::CreateFilledPile(tile_size, 409 pending_layer_bounds); 410 scoped_refptr<FakePicturePileImpl> active_pile = 411 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds); 412 413 SetupTrees(pending_pile, active_pile); 414 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100)); 415 416 Region invalidation; 417 AddDefaultTilingsWithInvalidation(invalidation); 418 419 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 420 EXPECT_GT(tilings->num_tilings(), 0u); 421 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 422 const PictureLayerTiling* tiling = tilings->tiling_at(i); 423 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect( 424 gfx::Rect(active_layer_bounds), 425 tiling->contents_scale()); 426 for (PictureLayerTiling::CoverageIterator 427 iter(tiling, 428 tiling->contents_scale(), 429 tiling->ContentRect()); 430 iter; 431 ++iter) { 432 EXPECT_TRUE(*iter); 433 EXPECT_FALSE(iter.geometry_rect().IsEmpty()); 434 std::vector<Tile*> active_tiles = 435 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting(); 436 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting(); 437 if (iter.geometry_rect().right() >= active_content_bounds.width() || 438 iter.geometry_rect().bottom() >= active_content_bounds.height() || 439 active_tiles[0]->content_rect().size() != 440 pending_tiles[0]->content_rect().size()) { 441 EXPECT_EQ(pending_pile, iter->picture_pile()); 442 } else { 443 EXPECT_EQ(active_pile, iter->picture_pile()); 444 } 445 } 446 } 447 } 448 449 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) { 450 gfx::Size tile_size(400, 400); 451 gfx::Size layer_bounds(1300, 1900); 452 453 scoped_refptr<FakePicturePileImpl> pending_pile = 454 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 455 scoped_refptr<FakePicturePileImpl> active_pile = 456 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 457 458 // Fill in some of active pile, but more of pending pile. 459 int hole_count = 0; 460 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) { 461 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) { 462 if ((x + y) % 2) { 463 pending_pile->AddRecordingAt(x, y); 464 active_pile->AddRecordingAt(x, y); 465 } else { 466 hole_count++; 467 if (hole_count % 2) 468 pending_pile->AddRecordingAt(x, y); 469 } 470 } 471 } 472 473 SetupTrees(pending_pile, active_pile); 474 Region invalidation; 475 AddDefaultTilingsWithInvalidation(invalidation); 476 477 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 478 EXPECT_GT(tilings->num_tilings(), 0u); 479 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 480 const PictureLayerTiling* tiling = tilings->tiling_at(i); 481 482 for (PictureLayerTiling::CoverageIterator 483 iter(tiling, 484 tiling->contents_scale(), 485 tiling->ContentRect()); 486 iter; 487 ++iter) { 488 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty()); 489 // Ensure there is a recording for this tile. 490 bool in_pending = pending_pile->CanRaster(tiling->contents_scale(), 491 iter.full_tile_geometry_rect()); 492 bool in_active = active_pile->CanRaster(tiling->contents_scale(), 493 iter.full_tile_geometry_rect()); 494 495 if (in_pending && !in_active) 496 EXPECT_EQ(pending_pile, iter->picture_pile()); 497 else if (in_active) 498 EXPECT_EQ(active_pile, iter->picture_pile()); 499 else 500 EXPECT_FALSE(*iter); 501 } 502 } 503 } 504 505 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) { 506 gfx::Size tile_size(400, 400); 507 gfx::Size layer_bounds(1300, 1900); 508 509 scoped_refptr<FakePicturePileImpl> pending_pile = 510 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 511 scoped_refptr<FakePicturePileImpl> active_pile = 512 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 513 514 float result_scale_x, result_scale_y; 515 gfx::Size result_bounds; 516 517 SetupTrees(pending_pile, active_pile); 518 519 pending_layer_->CalculateContentsScale( 520 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 521 522 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 523 } 524 525 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) { 526 gfx::Size tile_size(400, 400); 527 gfx::Size layer_bounds(1300, 1900); 528 529 scoped_refptr<FakePicturePileImpl> pending_pile = 530 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 531 scoped_refptr<FakePicturePileImpl> active_pile = 532 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 533 534 float result_scale_x, result_scale_y; 535 gfx::Size result_bounds; 536 537 SetupTrees(pending_pile, active_pile); 538 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 539 540 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 541 EXPECT_LT(low_res_factor, 1.f); 542 543 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 544 1.7f, // device scale 545 3.2f, // page cale 546 false, 547 &result_scale_x, 548 &result_scale_y, 549 &result_bounds); 550 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 551 EXPECT_FLOAT_EQ( 552 1.3f, 553 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 554 EXPECT_FLOAT_EQ( 555 1.3f * low_res_factor, 556 pending_layer_->tilings()->tiling_at(1)->contents_scale()); 557 558 // If we change the layer's CSS scale factor, then we should not get new 559 // tilings. 560 pending_layer_->CalculateContentsScale(1.8f, // ideal contents scale 561 1.7f, // device scale 562 3.2f, // page cale 563 false, 564 &result_scale_x, 565 &result_scale_y, 566 &result_bounds); 567 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 568 EXPECT_FLOAT_EQ( 569 1.3f, 570 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 571 EXPECT_FLOAT_EQ( 572 1.3f * low_res_factor, 573 pending_layer_->tilings()->tiling_at(1)->contents_scale()); 574 575 // If we change the page scale factor, then we should get new tilings. 576 pending_layer_->CalculateContentsScale(1.8f, // ideal contents scale 577 1.7f, // device scale 578 2.2f, // page cale 579 false, 580 &result_scale_x, 581 &result_scale_y, 582 &result_bounds); 583 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings()); 584 EXPECT_FLOAT_EQ( 585 1.8f, 586 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 587 EXPECT_FLOAT_EQ( 588 1.8f * low_res_factor, 589 pending_layer_->tilings()->tiling_at(2)->contents_scale()); 590 591 // If we change the device scale factor, then we should get new tilings. 592 pending_layer_->CalculateContentsScale(1.9f, // ideal contents scale 593 1.4f, // device scale 594 2.2f, // page cale 595 false, 596 &result_scale_x, 597 &result_scale_y, 598 &result_bounds); 599 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings()); 600 EXPECT_FLOAT_EQ( 601 1.9f, 602 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 603 EXPECT_FLOAT_EQ( 604 1.9f * low_res_factor, 605 pending_layer_->tilings()->tiling_at(3)->contents_scale()); 606 607 // If we change the device scale factor, but end up at the same total scale 608 // factor somehow, then we don't get new tilings. 609 pending_layer_->CalculateContentsScale(1.9f, // ideal contents scale 610 2.2f, // device scale 611 1.4f, // page cale 612 false, 613 &result_scale_x, 614 &result_scale_y, 615 &result_bounds); 616 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings()); 617 EXPECT_FLOAT_EQ( 618 1.9f, 619 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 620 EXPECT_FLOAT_EQ( 621 1.9f * low_res_factor, 622 pending_layer_->tilings()->tiling_at(3)->contents_scale()); 623 } 624 625 TEST_F(PictureLayerImplTest, CreateTilingsEvenIfTwinHasNone) { 626 // This test makes sure that if a layer can have tilings, then a commit makes 627 // it not able to have tilings (empty size), and then a future commit that 628 // makes it valid again should be able to create tilings. 629 gfx::Size tile_size(400, 400); 630 gfx::Size layer_bounds(1300, 1900); 631 632 scoped_refptr<FakePicturePileImpl> empty_pile = 633 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 634 scoped_refptr<FakePicturePileImpl> valid_pile = 635 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 636 637 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 638 EXPECT_LT(low_res_factor, 1.f); 639 640 float high_res_scale = 1.3f; 641 float low_res_scale = high_res_scale * low_res_factor; 642 float device_scale = 1.7f; 643 float page_scale = 3.2f; 644 float result_scale_x, result_scale_y; 645 gfx::Size result_bounds; 646 647 SetupPendingTree(valid_pile); 648 pending_layer_->CalculateContentsScale(high_res_scale, 649 device_scale, 650 page_scale, 651 false, 652 &result_scale_x, 653 &result_scale_y, 654 &result_bounds); 655 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 656 EXPECT_FLOAT_EQ(high_res_scale, 657 pending_layer_->HighResTiling()->contents_scale()); 658 EXPECT_FLOAT_EQ(low_res_scale, 659 pending_layer_->LowResTiling()->contents_scale()); 660 661 ActivateTree(); 662 SetupPendingTree(empty_pile); 663 pending_layer_->CalculateContentsScale(high_res_scale, 664 device_scale, 665 page_scale, 666 false, 667 &result_scale_x, 668 &result_scale_y, 669 &result_bounds); 670 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 671 ASSERT_EQ(0u, pending_layer_->tilings()->num_tilings()); 672 673 ActivateTree(); 674 active_layer_->CalculateContentsScale(high_res_scale, 675 device_scale, 676 page_scale, 677 false, 678 &result_scale_x, 679 &result_scale_y, 680 &result_bounds); 681 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings()); 682 683 SetupPendingTree(valid_pile); 684 pending_layer_->CalculateContentsScale(high_res_scale, 685 device_scale, 686 page_scale, 687 false, 688 &result_scale_x, 689 &result_scale_y, 690 &result_bounds); 691 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 692 ASSERT_EQ(0u, active_layer_->tilings()->num_tilings()); 693 EXPECT_FLOAT_EQ(high_res_scale, 694 pending_layer_->HighResTiling()->contents_scale()); 695 EXPECT_FLOAT_EQ(low_res_scale, 696 pending_layer_->LowResTiling()->contents_scale()); 697 } 698 699 TEST_F(PictureLayerImplTest, ZoomOutCrash) { 700 gfx::Size tile_size(400, 400); 701 gfx::Size layer_bounds(1300, 1900); 702 703 // Set up the high and low res tilings before pinch zoom. 704 scoped_refptr<FakePicturePileImpl> pending_pile = 705 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 706 scoped_refptr<FakePicturePileImpl> active_pile = 707 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 708 709 SetupTrees(pending_pile, active_pile); 710 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings()); 711 SetContentsScaleOnBothLayers(32.0f, 1.0f, 32.0f, false); 712 host_impl_.PinchGestureBegin(); 713 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false); 714 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false); 715 EXPECT_EQ(active_layer_->tilings()->NumHighResTilings(), 1); 716 } 717 718 TEST_F(PictureLayerImplTest, PinchGestureTilings) { 719 gfx::Size tile_size(400, 400); 720 gfx::Size layer_bounds(1300, 1900); 721 722 scoped_refptr<FakePicturePileImpl> pending_pile = 723 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 724 scoped_refptr<FakePicturePileImpl> active_pile = 725 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 726 727 // Set up the high and low res tilings before pinch zoom. 728 SetupTrees(pending_pile, active_pile); 729 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings()); 730 SetContentsScaleOnBothLayers(1.0f, 1.0f, 1.0f, false); 731 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 732 EXPECT_EQ(2u, active_layer_->tilings()->num_tilings()); 733 EXPECT_FLOAT_EQ( 734 1.0f, 735 active_layer_->tilings()->tiling_at(0)->contents_scale()); 736 EXPECT_FLOAT_EQ( 737 1.0f * low_res_factor, 738 active_layer_->tilings()->tiling_at(1)->contents_scale()); 739 740 // Start a pinch gesture. 741 host_impl_.PinchGestureBegin(); 742 743 // Zoom out by a small amount. We should create a tiling at half 744 // the scale (1/kMaxScaleRatioDuringPinch). 745 SetContentsScaleOnBothLayers(0.90f, 1.0f, 0.9f, false); 746 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings()); 747 EXPECT_FLOAT_EQ( 748 1.0f, 749 active_layer_->tilings()->tiling_at(0)->contents_scale()); 750 EXPECT_FLOAT_EQ( 751 0.5f, 752 active_layer_->tilings()->tiling_at(1)->contents_scale()); 753 EXPECT_FLOAT_EQ( 754 1.0f * low_res_factor, 755 active_layer_->tilings()->tiling_at(2)->contents_scale()); 756 757 // Zoom out further, close to our low-res scale factor. We should 758 // use that tiling as high-res, and not create a new tiling. 759 SetContentsScaleOnBothLayers(low_res_factor, 1.0f, low_res_factor, false); 760 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings()); 761 762 // Zoom in a lot now. Since we increase by increments of 763 // kMaxScaleRatioDuringPinch, this will first use 0.5, then 1.0 764 // and then finally create a new tiling at 2.0. 765 SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false); 766 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings()); 767 SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false); 768 EXPECT_EQ(3u, active_layer_->tilings()->num_tilings()); 769 SetContentsScaleOnBothLayers(2.1f, 1.0f, 2.1f, false); 770 EXPECT_EQ(4u, active_layer_->tilings()->num_tilings()); 771 EXPECT_FLOAT_EQ( 772 2.0f, 773 active_layer_->tilings()->tiling_at(0)->contents_scale()); 774 } 775 776 TEST_F(PictureLayerImplTest, CleanUpTilings) { 777 gfx::Size tile_size(400, 400); 778 gfx::Size layer_bounds(1300, 1900); 779 780 scoped_refptr<FakePicturePileImpl> pending_pile = 781 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 782 scoped_refptr<FakePicturePileImpl> active_pile = 783 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 784 785 float result_scale_x, result_scale_y; 786 gfx::Size result_bounds; 787 std::vector<PictureLayerTiling*> used_tilings; 788 789 SetupTrees(pending_pile, active_pile); 790 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 791 792 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 793 EXPECT_LT(low_res_factor, 1.f); 794 795 float device_scale = 1.7f; 796 float page_scale = 3.2f; 797 798 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false); 799 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 800 801 // We only have ideal tilings, so they aren't removed. 802 used_tilings.clear(); 803 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 804 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 805 806 // Changing the ideal but not creating new tilings. 807 SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false); 808 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 809 810 // The tilings are still our target scale, so they aren't removed. 811 used_tilings.clear(); 812 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 813 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 814 815 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2. 816 page_scale = 1.2f; 817 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false); 818 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings()); 819 EXPECT_FLOAT_EQ( 820 1.f, 821 active_layer_->tilings()->tiling_at(1)->contents_scale()); 822 EXPECT_FLOAT_EQ( 823 1.f * low_res_factor, 824 active_layer_->tilings()->tiling_at(3)->contents_scale()); 825 826 // Mark the non-ideal tilings as used. They won't be removed. 827 used_tilings.clear(); 828 used_tilings.push_back(active_layer_->tilings()->tiling_at(1)); 829 used_tilings.push_back(active_layer_->tilings()->tiling_at(3)); 830 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 831 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings()); 832 833 // Now move the ideal scale to 0.5. Our target stays 1.2. 834 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false); 835 836 // The high resolution tiling is between target and ideal, so is not 837 // removed. The low res tiling for the old ideal=1.0 scale is removed. 838 used_tilings.clear(); 839 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 840 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 841 842 // Now move the ideal scale to 1.0. Our target stays 1.2. 843 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false); 844 845 // All the tilings are between are target and the ideal, so they are not 846 // removed. 847 used_tilings.clear(); 848 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 849 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 850 851 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2. 852 active_layer_->CalculateContentsScale(1.1f, 853 device_scale, 854 page_scale, 855 false, 856 &result_scale_x, 857 &result_scale_y, 858 &result_bounds); 859 860 // Because the pending layer's ideal scale is still 1.0, our tilings fall 861 // in the range [1.0,1.2] and are kept. 862 used_tilings.clear(); 863 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 864 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 865 866 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays 867 // 1.2 still. 868 pending_layer_->CalculateContentsScale(1.1f, 869 device_scale, 870 page_scale, 871 false, 872 &result_scale_x, 873 &result_scale_y, 874 &result_bounds); 875 876 // Our 1.0 tiling now falls outside the range between our ideal scale and our 877 // target raster scale. But it is in our used tilings set, so nothing is 878 // deleted. 879 used_tilings.clear(); 880 used_tilings.push_back(active_layer_->tilings()->tiling_at(1)); 881 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 882 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 883 884 // If we remove it from our used tilings set, it is outside the range to keep 885 // so it is deleted. 886 used_tilings.clear(); 887 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 888 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 889 } 890 891 #define EXPECT_BOTH_EQ(expression, x) \ 892 do { \ 893 EXPECT_EQ(pending_layer_->expression, x); \ 894 EXPECT_EQ(active_layer_->expression, x); \ 895 } while (false) 896 897 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) { 898 // Make sure this layer covers multiple tiles, since otherwise low 899 // res won't get created because it is too small. 900 gfx::Size tile_size(host_impl_.settings().default_tile_size); 901 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1)); 902 // Avoid max untiled layer size heuristics via fixed tile size. 903 pending_layer_->set_fixed_tile_size(tile_size); 904 active_layer_->set_fixed_tile_size(tile_size); 905 906 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 907 float contents_scale = 1.f; 908 float device_scale = 1.f; 909 float page_scale = 1.f; 910 bool animating_transform = true; 911 912 // Animating, so don't create low res even if there isn't one already. 913 SetContentsScaleOnBothLayers( 914 contents_scale, device_scale, page_scale, animating_transform); 915 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f); 916 EXPECT_BOTH_EQ(num_tilings(), 1u); 917 918 // Stop animating, low res gets created. 919 animating_transform = false; 920 SetContentsScaleOnBothLayers( 921 contents_scale, device_scale, page_scale, animating_transform); 922 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f); 923 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor); 924 EXPECT_BOTH_EQ(num_tilings(), 2u); 925 926 // Page scale animation, new high res, but not new low res because animating. 927 contents_scale = 2.f; 928 page_scale = 2.f; 929 animating_transform = true; 930 SetContentsScaleOnBothLayers( 931 contents_scale, device_scale, page_scale, animating_transform); 932 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f); 933 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor); 934 EXPECT_BOTH_EQ(num_tilings(), 3u); 935 936 // Stop animating, new low res gets created for final page scale. 937 animating_transform = false; 938 SetContentsScaleOnBothLayers( 939 contents_scale, device_scale, page_scale, animating_transform); 940 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 2.f); 941 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 2.f * low_res_factor); 942 EXPECT_BOTH_EQ(num_tilings(), 4u); 943 } 944 945 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) { 946 gfx::Size tile_size(host_impl_.settings().default_tile_size); 947 SetupDefaultTrees(tile_size); 948 949 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 950 float device_scale = 1.f; 951 float page_scale = 1.f; 952 bool animating_transform = false; 953 954 // Contents exactly fit on one tile at scale 1, no low res. 955 float contents_scale = 1.f; 956 SetContentsScaleOnBothLayers( 957 contents_scale, device_scale, page_scale, animating_transform); 958 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 959 EXPECT_BOTH_EQ(num_tilings(), 1u); 960 961 ResetTilingsAndRasterScales(); 962 963 // Contents that are smaller than one tile, no low res. 964 contents_scale = 0.123f; 965 SetContentsScaleOnBothLayers( 966 contents_scale, device_scale, page_scale, animating_transform); 967 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 968 EXPECT_BOTH_EQ(num_tilings(), 1u); 969 970 ResetTilingsAndRasterScales(); 971 972 // Any content bounds that would create more than one tile will 973 // generate a low res tiling. 974 contents_scale = 2.5f; 975 SetContentsScaleOnBothLayers( 976 contents_scale, device_scale, page_scale, animating_transform); 977 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 978 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 979 contents_scale * low_res_factor); 980 EXPECT_BOTH_EQ(num_tilings(), 2u); 981 982 ResetTilingsAndRasterScales(); 983 984 // Mask layers dont create low res since they always fit on one tile. 985 pending_layer_->SetIsMask(true); 986 active_layer_->SetIsMask(true); 987 SetContentsScaleOnBothLayers( 988 contents_scale, device_scale, page_scale, animating_transform); 989 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 990 EXPECT_BOTH_EQ(num_tilings(), 1u); 991 } 992 993 TEST_F(PictureLayerImplTest, DidLoseOutputSurface) { 994 gfx::Size tile_size(400, 400); 995 gfx::Size layer_bounds(1300, 1900); 996 997 scoped_refptr<FakePicturePileImpl> pending_pile = 998 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 999 scoped_refptr<FakePicturePileImpl> active_pile = 1000 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1001 1002 float result_scale_x, result_scale_y; 1003 gfx::Size result_bounds; 1004 1005 SetupTrees(pending_pile, active_pile); 1006 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 1007 1008 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 1009 2.7f, // device scale 1010 3.2f, // page cale 1011 false, 1012 &result_scale_x, 1013 &result_scale_y, 1014 &result_bounds); 1015 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings()); 1016 1017 // All tilings should be removed when losing output surface. 1018 active_layer_->DidLoseOutputSurface(); 1019 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings()); 1020 pending_layer_->DidLoseOutputSurface(); 1021 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 1022 1023 // This should create new tilings. 1024 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 1025 2.7f, // device scale 1026 3.2f, // page cale 1027 false, 1028 &result_scale_x, 1029 &result_scale_y, 1030 &result_bounds); 1031 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings()); 1032 } 1033 1034 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) { 1035 // The default max tile size is larger than 400x400. 1036 gfx::Size tile_size(400, 400); 1037 gfx::Size layer_bounds(5000, 5000); 1038 1039 scoped_refptr<FakePicturePileImpl> pending_pile = 1040 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1041 scoped_refptr<FakePicturePileImpl> active_pile = 1042 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1043 1044 float result_scale_x, result_scale_y; 1045 gfx::Size result_bounds; 1046 1047 SetupTrees(pending_pile, active_pile); 1048 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 1049 1050 pending_layer_->CalculateContentsScale( 1051 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 1052 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 1053 1054 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 1055 1056 // The default value. 1057 EXPECT_EQ(gfx::Size(256, 256).ToString(), 1058 host_impl_.settings().default_tile_size.ToString()); 1059 1060 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 1061 EXPECT_EQ(gfx::Size(256, 256).ToString(), 1062 tile->content_rect().size().ToString()); 1063 1064 pending_layer_->DidLoseOutputSurface(); 1065 1066 // Change the max texture size on the output surface context. 1067 scoped_ptr<TestWebGraphicsContext3D> context = 1068 TestWebGraphicsContext3D::Create(); 1069 context->set_max_texture_size(140); 1070 host_impl_.DidLoseOutputSurface(); 1071 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d( 1072 context.Pass()).PassAs<OutputSurface>()); 1073 1074 pending_layer_->CalculateContentsScale( 1075 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 1076 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 1077 1078 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 1079 1080 // Verify the tiles are not larger than the context's max texture size. 1081 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 1082 EXPECT_GE(140, tile->content_rect().width()); 1083 EXPECT_GE(140, tile->content_rect().height()); 1084 } 1085 1086 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) { 1087 // The default max tile size is larger than 400x400. 1088 gfx::Size tile_size(400, 400); 1089 gfx::Size layer_bounds(500, 500); 1090 1091 scoped_refptr<FakePicturePileImpl> pending_pile = 1092 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1093 scoped_refptr<FakePicturePileImpl> active_pile = 1094 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1095 1096 float result_scale_x, result_scale_y; 1097 gfx::Size result_bounds; 1098 1099 SetupTrees(pending_pile, active_pile); 1100 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 1101 1102 pending_layer_->CalculateContentsScale( 1103 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 1104 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings()); 1105 1106 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 1107 1108 // The default value. The layer is smaller than this. 1109 EXPECT_EQ(gfx::Size(512, 512).ToString(), 1110 host_impl_.settings().max_untiled_layer_size.ToString()); 1111 1112 // There should be a single tile since the layer is small. 1113 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0); 1114 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size()); 1115 1116 pending_layer_->DidLoseOutputSurface(); 1117 1118 // Change the max texture size on the output surface context. 1119 scoped_ptr<TestWebGraphicsContext3D> context = 1120 TestWebGraphicsContext3D::Create(); 1121 context->set_max_texture_size(140); 1122 host_impl_.DidLoseOutputSurface(); 1123 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d( 1124 context.Pass()).PassAs<OutputSurface>()); 1125 1126 pending_layer_->CalculateContentsScale( 1127 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 1128 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings()); 1129 1130 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 1131 1132 // There should be more than one tile since the max texture size won't cover 1133 // the layer. 1134 high_res_tiling = pending_layer_->tilings()->tiling_at(0); 1135 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size()); 1136 1137 // Verify the tiles are not larger than the context's max texture size. 1138 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 1139 EXPECT_GE(140, tile->content_rect().width()); 1140 EXPECT_GE(140, tile->content_rect().height()); 1141 } 1142 1143 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) { 1144 MockQuadCuller quad_culler; 1145 1146 gfx::Size tile_size(400, 400); 1147 gfx::Size layer_bounds(1300, 1900); 1148 1149 scoped_refptr<FakePicturePileImpl> pending_pile = 1150 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1151 scoped_refptr<FakePicturePileImpl> active_pile = 1152 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1153 1154 SetupTrees(pending_pile, active_pile); 1155 1156 active_layer_->SetContentBounds(layer_bounds); 1157 active_layer_->draw_properties().visible_content_rect = 1158 gfx::Rect(layer_bounds); 1159 1160 gfx::Rect layer_invalidation(150, 200, 30, 180); 1161 Region invalidation(layer_invalidation); 1162 AddDefaultTilingsWithInvalidation(invalidation); 1163 1164 AppendQuadsData data; 1165 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL); 1166 active_layer_->AppendQuads(&quad_culler, &data); 1167 active_layer_->DidDraw(NULL); 1168 1169 ASSERT_EQ(1U, quad_culler.quad_list().size()); 1170 EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material); 1171 } 1172 1173 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) { 1174 gfx::Size tile_size(100, 100); 1175 gfx::Size layer_bounds(1000, 1000); 1176 1177 scoped_refptr<FakePicturePileImpl> pending_pile = 1178 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 1179 // Layers with entirely empty piles can't get tilings. 1180 pending_pile->AddRecordingAt(0, 0); 1181 1182 SetupPendingTree(pending_pile); 1183 1184 ASSERT_TRUE(pending_layer_->CanHaveTilings()); 1185 pending_layer_->AddTiling(1.0f); 1186 pending_layer_->AddTiling(2.0f); 1187 1188 // It should be safe to call this (and MarkVisibleResourcesAsRequired) 1189 // on a layer with no recordings. 1190 host_impl_.pending_tree()->UpdateDrawProperties(); 1191 pending_layer_->MarkVisibleResourcesAsRequired(); 1192 } 1193 1194 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) { 1195 gfx::Size tile_size(100, 100); 1196 gfx::Size layer_bounds(200, 100); 1197 1198 scoped_refptr<FakePicturePileImpl> pending_pile = 1199 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1200 SetupPendingTree(pending_pile); 1201 1202 pending_layer_->set_fixed_tile_size(tile_size); 1203 ASSERT_TRUE(pending_layer_->CanHaveTilings()); 1204 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f); 1205 host_impl_.pending_tree()->UpdateDrawProperties(); 1206 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION); 1207 1208 // Fake set priorities. 1209 int tile_count = 0; 1210 for (PictureLayerTiling::CoverageIterator iter( 1211 tiling, 1212 pending_layer_->contents_scale_x(), 1213 gfx::Rect(pending_layer_->visible_content_rect())); 1214 iter; 1215 ++iter) { 1216 if (!*iter) 1217 continue; 1218 Tile* tile = *iter; 1219 TilePriority priority; 1220 priority.resolution = HIGH_RESOLUTION; 1221 if (++tile_count % 2) { 1222 priority.time_to_visible_in_seconds = 0.f; 1223 priority.distance_to_visible_in_pixels = 0.f; 1224 } else { 1225 priority.time_to_visible_in_seconds = 1.f; 1226 priority.distance_to_visible_in_pixels = 1.f; 1227 } 1228 tile->SetPriority(PENDING_TREE, priority); 1229 } 1230 1231 pending_layer_->MarkVisibleResourcesAsRequired(); 1232 1233 int num_visible = 0; 1234 int num_offscreen = 0; 1235 1236 for (PictureLayerTiling::CoverageIterator iter( 1237 tiling, 1238 pending_layer_->contents_scale_x(), 1239 gfx::Rect(pending_layer_->visible_content_rect())); 1240 iter; 1241 ++iter) { 1242 if (!*iter) 1243 continue; 1244 const Tile* tile = *iter; 1245 if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) { 1246 EXPECT_TRUE(tile->required_for_activation()); 1247 num_visible++; 1248 } else { 1249 EXPECT_FALSE(tile->required_for_activation()); 1250 num_offscreen++; 1251 } 1252 } 1253 1254 EXPECT_GT(num_visible, 0); 1255 EXPECT_GT(num_offscreen, 0); 1256 } 1257 1258 TEST_F(PictureLayerImplTest, HighResRequiredWhenUnsharedActiveAllReady) { 1259 gfx::Size layer_bounds(400, 400); 1260 gfx::Size tile_size(100, 100); 1261 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size); 1262 1263 // No tiles shared. 1264 pending_layer_->set_invalidation(gfx::Rect(layer_bounds)); 1265 1266 CreateHighLowResAndSetAllTilesVisible(); 1267 1268 active_layer_->SetAllTilesReady(); 1269 1270 // No shared tiles and all active tiles ready, so pending can only 1271 // activate with all high res tiles. 1272 pending_layer_->MarkVisibleResourcesAsRequired(); 1273 AssertAllTilesRequired(pending_layer_->HighResTiling()); 1274 AssertNoTilesRequired(pending_layer_->LowResTiling()); 1275 } 1276 1277 // TODO(enne): temporarily disabled: http://crbug.com/335289 1278 TEST_F(PictureLayerImplTest, DISABLED_NothingRequiredIfAllHighResTilesShared) { 1279 gfx::Size layer_bounds(400, 400); 1280 gfx::Size tile_size(100, 100); 1281 SetupDefaultTreesWithFixedTileSize(layer_bounds, tile_size); 1282 1283 CreateHighLowResAndSetAllTilesVisible(); 1284 1285 Tile* some_active_tile = 1286 active_layer_->HighResTiling()->AllTilesForTesting()[0]; 1287 EXPECT_FALSE(some_active_tile->IsReadyToDraw()); 1288 1289 // All tiles shared (no invalidation), so even though the active tree's 1290 // tiles aren't ready, there is nothing required. 1291 pending_layer_->MarkVisibleResourcesAsRequired(); 1292 AssertNoTilesRequired(pending_layer_->HighResTiling()); 1293 AssertNoTilesRequired(pending_layer_->LowResTiling()); 1294 } 1295 1296 // TODO(enne): temporarily disabled: http://crbug.com/335289 1297 TEST_F(PictureLayerImplTest, DISABLED_NothingRequiredIfActiveMissingTiles) { 1298 gfx::Size layer_bounds(400, 400); 1299 gfx::Size tile_size(100, 100); 1300 scoped_refptr<FakePicturePileImpl> pending_pile = 1301 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1302 // This pile will create tilings, but has no recordings so will not create any 1303 // tiles. This is attempting to simulate scrolling past the end of recorded 1304 // content on the active layer, where the recordings are so far away that 1305 // no tiles are created. 1306 scoped_refptr<FakePicturePileImpl> active_pile = 1307 FakePicturePileImpl::CreateEmptyPileThatThinksItHasRecordings( 1308 tile_size, layer_bounds); 1309 SetupTrees(pending_pile, active_pile); 1310 pending_layer_->set_fixed_tile_size(tile_size); 1311 active_layer_->set_fixed_tile_size(tile_size); 1312 1313 CreateHighLowResAndSetAllTilesVisible(); 1314 1315 // Active layer has tilings, but no tiles due to missing recordings. 1316 EXPECT_TRUE(active_layer_->CanHaveTilings()); 1317 EXPECT_EQ(active_layer_->tilings()->num_tilings(), 2u); 1318 EXPECT_EQ(active_layer_->HighResTiling()->AllTilesForTesting().size(), 0u); 1319 1320 // Since the active layer has no tiles at all, the pending layer doesn't 1321 // need content in order to activate. 1322 pending_layer_->MarkVisibleResourcesAsRequired(); 1323 AssertNoTilesRequired(pending_layer_->HighResTiling()); 1324 AssertNoTilesRequired(pending_layer_->LowResTiling()); 1325 } 1326 1327 TEST_F(PictureLayerImplTest, HighResRequiredIfActiveCantHaveTiles) { 1328 gfx::Size layer_bounds(400, 400); 1329 gfx::Size tile_size(100, 100); 1330 scoped_refptr<FakePicturePileImpl> pending_pile = 1331 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1332 scoped_refptr<FakePicturePileImpl> active_pile = 1333 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 1334 SetupTrees(pending_pile, active_pile); 1335 pending_layer_->set_fixed_tile_size(tile_size); 1336 active_layer_->set_fixed_tile_size(tile_size); 1337 1338 CreateHighLowResAndSetAllTilesVisible(); 1339 1340 // Active layer can't have tiles. 1341 EXPECT_FALSE(active_layer_->CanHaveTilings()); 1342 1343 // All high res tiles required. This should be considered identical 1344 // to the case where there is no active layer, to avoid flashing content. 1345 // This can happen if a layer exists for a while and switches from 1346 // not being able to have content to having content. 1347 pending_layer_->MarkVisibleResourcesAsRequired(); 1348 AssertAllTilesRequired(pending_layer_->HighResTiling()); 1349 AssertNoTilesRequired(pending_layer_->LowResTiling()); 1350 } 1351 1352 TEST_F(PictureLayerImplTest, ActivateUninitializedLayer) { 1353 gfx::Size tile_size(100, 100); 1354 gfx::Size layer_bounds(400, 400); 1355 scoped_refptr<FakePicturePileImpl> pending_pile = 1356 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1357 1358 host_impl_.CreatePendingTree(); 1359 LayerTreeImpl* pending_tree = host_impl_.pending_tree(); 1360 1361 scoped_ptr<FakePictureLayerImpl> pending_layer = 1362 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pending_pile); 1363 pending_layer->SetDrawsContent(true); 1364 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>()); 1365 1366 pending_layer_ = static_cast<FakePictureLayerImpl*>( 1367 host_impl_.pending_tree()->LayerById(id_)); 1368 1369 // Set some state on the pending layer, make sure it is not clobbered 1370 // by a sync from the active layer. This could happen because if the 1371 // pending layer has not been post-commit initialized it will attempt 1372 // to sync from the active layer. 1373 bool default_lcd_text_setting = pending_layer_->is_using_lcd_text(); 1374 pending_layer_->force_set_lcd_text(!default_lcd_text_setting); 1375 EXPECT_TRUE(pending_layer_->needs_post_commit_initialization()); 1376 1377 host_impl_.ActivatePendingTree(); 1378 1379 active_layer_ = static_cast<FakePictureLayerImpl*>( 1380 host_impl_.active_tree()->LayerById(id_)); 1381 1382 EXPECT_EQ(0u, active_layer_->num_tilings()); 1383 EXPECT_EQ(!default_lcd_text_setting, active_layer_->is_using_lcd_text()); 1384 EXPECT_FALSE(active_layer_->needs_post_commit_initialization()); 1385 } 1386 1387 TEST_F(PictureLayerImplTest, SyncTilingAfterReleaseResource) { 1388 SetupDefaultTrees(gfx::Size(10, 10)); 1389 host_impl_.active_tree()->UpdateDrawProperties(); 1390 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties()); 1391 1392 // Contrived unit test of a real crash. A layer is transparent during a 1393 // context loss, and later becomes opaque, causing active layer SyncTiling to 1394 // be called. 1395 const float tile_scale = 2.f; 1396 active_layer_->DidLoseOutputSurface(); 1397 EXPECT_FALSE(active_layer_->tilings()->TilingAtScale(tile_scale)); 1398 pending_layer_->AddTiling(2.f); 1399 EXPECT_TRUE(active_layer_->tilings()->TilingAtScale(tile_scale)); 1400 } 1401 1402 TEST_F(PictureLayerImplTest, NoTilingIfDoesNotDrawContent) { 1403 // Set up layers with tilings. 1404 SetupDefaultTrees(gfx::Size(10, 10)); 1405 SetContentsScaleOnBothLayers(1.f, 1.f, 1.f, false); 1406 pending_layer_->PushPropertiesTo(active_layer_); 1407 EXPECT_TRUE(pending_layer_->DrawsContent()); 1408 EXPECT_TRUE(pending_layer_->CanHaveTilings()); 1409 EXPECT_GE(pending_layer_->num_tilings(), 0u); 1410 EXPECT_GE(active_layer_->num_tilings(), 0u); 1411 1412 // Set content to false, which should make CanHaveTilings return false. 1413 pending_layer_->SetDrawsContent(false); 1414 EXPECT_FALSE(pending_layer_->DrawsContent()); 1415 EXPECT_FALSE(pending_layer_->CanHaveTilings()); 1416 1417 // No tilings should be pushed to active layer. 1418 pending_layer_->PushPropertiesTo(active_layer_); 1419 EXPECT_EQ(0u, active_layer_->num_tilings()); 1420 } 1421 1422 TEST_F(PictureLayerImplTest, FirstTilingDuringPinch) { 1423 SetupDefaultTrees(gfx::Size(10, 10)); 1424 host_impl_.PinchGestureBegin(); 1425 float high_res_scale = 2.3f; 1426 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, false); 1427 1428 ASSERT_GE(pending_layer_->num_tilings(), 0u); 1429 EXPECT_FLOAT_EQ(high_res_scale, 1430 pending_layer_->HighResTiling()->contents_scale()); 1431 } 1432 1433 TEST_F(PictureLayerImplTest, FirstTilingTooSmall) { 1434 SetupDefaultTrees(gfx::Size(10, 10)); 1435 host_impl_.PinchGestureBegin(); 1436 float high_res_scale = 0.0001f; 1437 EXPECT_GT(pending_layer_->MinimumContentsScale(), high_res_scale); 1438 1439 SetContentsScaleOnBothLayers(high_res_scale, 1.f, 1.f, false); 1440 1441 ASSERT_GE(pending_layer_->num_tilings(), 0u); 1442 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(), 1443 pending_layer_->HighResTiling()->contents_scale()); 1444 } 1445 1446 TEST_F(PictureLayerImplTest, PinchingTooSmall) { 1447 SetupDefaultTrees(gfx::Size(10, 10)); 1448 1449 float contents_scale = 0.15f; 1450 SetContentsScaleOnBothLayers(contents_scale, 1.f, 1.f, false); 1451 1452 ASSERT_GE(pending_layer_->num_tilings(), 0u); 1453 EXPECT_FLOAT_EQ(contents_scale, 1454 pending_layer_->HighResTiling()->contents_scale()); 1455 1456 host_impl_.PinchGestureBegin(); 1457 1458 float page_scale = 0.0001f; 1459 EXPECT_LT(page_scale * contents_scale, 1460 pending_layer_->MinimumContentsScale()); 1461 1462 1463 SetContentsScaleOnBothLayers(contents_scale, 1.f, page_scale, false); 1464 ASSERT_GE(pending_layer_->num_tilings(), 0u); 1465 EXPECT_FLOAT_EQ(pending_layer_->MinimumContentsScale(), 1466 pending_layer_->HighResTiling()->contents_scale()); 1467 } 1468 1469 class DeferredInitPictureLayerImplTest : public PictureLayerImplTest { 1470 public: 1471 DeferredInitPictureLayerImplTest() 1472 : PictureLayerImplTest(ImplSidePaintingSettings()) {} 1473 1474 virtual void InitializeRenderer() OVERRIDE { 1475 host_impl_.InitializeRenderer(FakeOutputSurface::CreateDeferredGL( 1476 scoped_ptr<SoftwareOutputDevice>(new SoftwareOutputDevice)) 1477 .PassAs<OutputSurface>()); 1478 } 1479 1480 virtual void SetUp() OVERRIDE { 1481 PictureLayerImplTest::SetUp(); 1482 1483 // Create some default active and pending trees. 1484 gfx::Size tile_size(100, 100); 1485 gfx::Size layer_bounds(400, 400); 1486 1487 scoped_refptr<FakePicturePileImpl> pending_pile = 1488 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1489 scoped_refptr<FakePicturePileImpl> active_pile = 1490 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 1491 1492 SetupTrees(pending_pile, active_pile); 1493 } 1494 }; 1495 1496 // This test is really a LayerTreeHostImpl test, in that it makes sure 1497 // that trees need update draw properties after deferred initialization. 1498 // However, this is also a regression test for PictureLayerImpl in that 1499 // not having this update will cause a crash. 1500 TEST_F(DeferredInitPictureLayerImplTest, 1501 PreventUpdateTilePrioritiesDuringLostContext) { 1502 host_impl_.pending_tree()->UpdateDrawProperties(); 1503 host_impl_.active_tree()->UpdateDrawProperties(); 1504 EXPECT_FALSE(host_impl_.pending_tree()->needs_update_draw_properties()); 1505 EXPECT_FALSE(host_impl_.active_tree()->needs_update_draw_properties()); 1506 1507 FakeOutputSurface* fake_output_surface = 1508 static_cast<FakeOutputSurface*>(host_impl_.output_surface()); 1509 ASSERT_TRUE(fake_output_surface->InitializeAndSetContext3d( 1510 TestContextProvider::Create(), NULL)); 1511 1512 // These will crash PictureLayerImpl if this is not true. 1513 ASSERT_TRUE(host_impl_.pending_tree()->needs_update_draw_properties()); 1514 ASSERT_TRUE(host_impl_.active_tree()->needs_update_draw_properties()); 1515 host_impl_.active_tree()->UpdateDrawProperties(); 1516 } 1517 1518 } // namespace 1519 } // namespace cc 1520