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/trees/layer_tree_impl.h" 21 #include "testing/gtest/include/gtest/gtest.h" 22 #include "third_party/skia/include/core/SkDevice.h" 23 #include "ui/gfx/rect_conversions.h" 24 25 namespace cc { 26 namespace { 27 28 class MockCanvas : public SkCanvas { 29 public: 30 explicit MockCanvas(SkDevice* device) : SkCanvas(device) {} 31 32 virtual void drawRect(const SkRect& rect, const SkPaint& paint) OVERRIDE { 33 // Capture calls before SkCanvas quickReject() kicks in. 34 rects_.push_back(rect); 35 } 36 37 std::vector<SkRect> rects_; 38 }; 39 40 class PictureLayerImplTest : public testing::Test { 41 public: 42 PictureLayerImplTest() 43 : host_impl_(ImplSidePaintingSettings(), &proxy_), id_(7) {} 44 45 explicit PictureLayerImplTest(const LayerTreeSettings& settings) 46 : host_impl_(settings, &proxy_), id_(7) {} 47 48 virtual ~PictureLayerImplTest() { 49 } 50 51 virtual void SetUp() OVERRIDE { 52 InitializeRenderer(); 53 } 54 55 virtual void InitializeRenderer() { 56 host_impl_.InitializeRenderer(CreateFakeOutputSurface()); 57 } 58 59 void SetupDefaultTrees(gfx::Size layer_bounds) { 60 gfx::Size tile_size(100, 100); 61 62 scoped_refptr<FakePicturePileImpl> pending_pile = 63 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 64 scoped_refptr<FakePicturePileImpl> active_pile = 65 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 66 67 SetupTrees(pending_pile, active_pile); 68 } 69 70 void SetupTrees( 71 scoped_refptr<PicturePileImpl> pending_pile, 72 scoped_refptr<PicturePileImpl> active_pile) { 73 SetupPendingTree(active_pile); 74 host_impl_.ActivatePendingTree(); 75 76 active_layer_ = static_cast<FakePictureLayerImpl*>( 77 host_impl_.active_tree()->LayerById(id_)); 78 79 SetupPendingTree(pending_pile); 80 pending_layer_->UpdateTwinLayer(); 81 } 82 83 void AddDefaultTilingsWithInvalidation(const Region& invalidation) { 84 active_layer_->AddTiling(2.3f); 85 active_layer_->AddTiling(1.0f); 86 active_layer_->AddTiling(0.5f); 87 for (size_t i = 0; i < active_layer_->tilings()->num_tilings(); ++i) 88 active_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting(); 89 pending_layer_->set_invalidation(invalidation); 90 pending_layer_->SyncFromActiveLayer(); 91 for (size_t i = 0; i < pending_layer_->tilings()->num_tilings(); ++i) 92 pending_layer_->tilings()->tiling_at(i)->CreateAllTilesForTesting(); 93 } 94 95 void SetupPendingTree( 96 scoped_refptr<PicturePileImpl> pile) { 97 host_impl_.CreatePendingTree(); 98 LayerTreeImpl* pending_tree = host_impl_.pending_tree(); 99 // Clear recycled tree. 100 pending_tree->DetachLayerTree(); 101 102 scoped_ptr<FakePictureLayerImpl> pending_layer = 103 FakePictureLayerImpl::CreateWithPile(pending_tree, id_, pile); 104 pending_layer->SetDrawsContent(true); 105 pending_tree->SetRootLayer(pending_layer.PassAs<LayerImpl>()); 106 107 pending_layer_ = static_cast<FakePictureLayerImpl*>( 108 host_impl_.pending_tree()->LayerById(id_)); 109 } 110 111 static void VerifyAllTilesExistAndHavePile( 112 const PictureLayerTiling* tiling, 113 PicturePileImpl* pile) { 114 for (PictureLayerTiling::CoverageIterator 115 iter(tiling, tiling->contents_scale(), tiling->ContentRect()); 116 iter; 117 ++iter) { 118 EXPECT_TRUE(*iter); 119 EXPECT_EQ(pile, iter->picture_pile()); 120 } 121 } 122 123 void SetContentsScaleOnBothLayers(float contents_scale, 124 float device_scale_factor, 125 float page_scale_factor, 126 bool animating_transform) { 127 float result_scale_x, result_scale_y; 128 gfx::Size result_bounds; 129 pending_layer_->CalculateContentsScale( 130 contents_scale, 131 device_scale_factor, 132 page_scale_factor, 133 animating_transform, 134 &result_scale_x, 135 &result_scale_y, 136 &result_bounds); 137 active_layer_->CalculateContentsScale( 138 contents_scale, 139 device_scale_factor, 140 page_scale_factor, 141 animating_transform, 142 &result_scale_x, 143 &result_scale_y, 144 &result_bounds); 145 } 146 147 void ResetTilingsAndRasterScales() { 148 pending_layer_->DidLoseOutputSurface(); 149 active_layer_->DidLoseOutputSurface(); 150 } 151 152 protected: 153 void TestTileGridAlignmentCommon() { 154 // Layer to span 4 raster tiles in x and in y 155 ImplSidePaintingSettings settings; 156 gfx::Size layer_size( 157 settings.default_tile_size.width() * 7 / 2, 158 settings.default_tile_size.height() * 7 / 2); 159 160 scoped_refptr<FakePicturePileImpl> pending_pile = 161 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size); 162 scoped_refptr<FakePicturePileImpl> active_pile = 163 FakePicturePileImpl::CreateFilledPile(layer_size, layer_size); 164 165 SetupTrees(pending_pile, active_pile); 166 167 float result_scale_x, result_scale_y; 168 gfx::Size result_bounds; 169 active_layer_->CalculateContentsScale( 170 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 171 172 // Add 1x1 rects at the centers of each tile, then re-record pile contents 173 active_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 174 std::vector<Tile*> tiles = 175 active_layer_->tilings()->tiling_at(0)->AllTilesForTesting(); 176 EXPECT_EQ(16u, tiles.size()); 177 std::vector<SkRect> rects; 178 std::vector<Tile*>::const_iterator tile_iter; 179 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) { 180 gfx::Point tile_center = (*tile_iter)->content_rect().CenterPoint(); 181 gfx::Rect rect(tile_center.x(), tile_center.y(), 1, 1); 182 active_pile->add_draw_rect(rect); 183 rects.push_back(SkRect::MakeXYWH(rect.x(), rect.y(), 1, 1)); 184 } 185 // Force re-record with newly injected content 186 active_pile->RemoveRecordingAt(0, 0); 187 active_pile->AddRecordingAt(0, 0); 188 189 SkBitmap store; 190 store.setConfig(SkBitmap::kNo_Config, 1000, 1000); 191 SkDevice device(store); 192 193 std::vector<SkRect>::const_iterator rect_iter = rects.begin(); 194 for (tile_iter = tiles.begin(); tile_iter < tiles.end(); tile_iter++) { 195 MockCanvas mock_canvas(&device); 196 active_pile->RasterDirect( 197 &mock_canvas, (*tile_iter)->content_rect(), 1.0f, NULL); 198 199 // This test verifies that when drawing the contents of a specific tile 200 // at content scale 1.0, the playback canvas never receives content from 201 // neighboring tiles which indicates that the tile grid embedded in 202 // SkPicture is perfectly aligned with the compositor's tiles. 203 EXPECT_EQ(1u, mock_canvas.rects_.size()); 204 EXPECT_RECT_EQ(*rect_iter, mock_canvas.rects_[0]); 205 rect_iter++; 206 } 207 } 208 209 FakeImplProxy proxy_; 210 FakeLayerTreeHostImpl host_impl_; 211 int id_; 212 FakePictureLayerImpl* pending_layer_; 213 FakePictureLayerImpl* active_layer_; 214 215 private: 216 DISALLOW_COPY_AND_ASSIGN(PictureLayerImplTest); 217 }; 218 219 TEST_F(PictureLayerImplTest, TileGridAlignment) { 220 host_impl_.SetDeviceScaleFactor(1.f); 221 TestTileGridAlignmentCommon(); 222 } 223 224 TEST_F(PictureLayerImplTest, TileGridAlignmentHiDPI) { 225 host_impl_.SetDeviceScaleFactor(2.f); 226 TestTileGridAlignmentCommon(); 227 } 228 229 TEST_F(PictureLayerImplTest, CloneNoInvalidation) { 230 gfx::Size tile_size(100, 100); 231 gfx::Size layer_bounds(400, 400); 232 233 scoped_refptr<FakePicturePileImpl> pending_pile = 234 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 235 scoped_refptr<FakePicturePileImpl> active_pile = 236 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 237 238 SetupTrees(pending_pile, active_pile); 239 240 Region invalidation; 241 AddDefaultTilingsWithInvalidation(invalidation); 242 243 EXPECT_EQ(pending_layer_->tilings()->num_tilings(), 244 active_layer_->tilings()->num_tilings()); 245 246 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 247 EXPECT_GT(tilings->num_tilings(), 0u); 248 for (size_t i = 0; i < tilings->num_tilings(); ++i) 249 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), active_pile.get()); 250 } 251 252 TEST_F(PictureLayerImplTest, ClonePartialInvalidation) { 253 gfx::Size tile_size(100, 100); 254 gfx::Size layer_bounds(400, 400); 255 gfx::Rect layer_invalidation(150, 200, 30, 180); 256 257 scoped_refptr<FakePicturePileImpl> pending_pile = 258 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 259 scoped_refptr<FakePicturePileImpl> active_pile = 260 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 261 262 SetupTrees(pending_pile, active_pile); 263 264 Region invalidation(layer_invalidation); 265 AddDefaultTilingsWithInvalidation(invalidation); 266 267 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 268 EXPECT_GT(tilings->num_tilings(), 0u); 269 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 270 const PictureLayerTiling* tiling = tilings->tiling_at(i); 271 gfx::Rect content_invalidation = gfx::ScaleToEnclosingRect( 272 layer_invalidation, 273 tiling->contents_scale()); 274 for (PictureLayerTiling::CoverageIterator 275 iter(tiling, 276 tiling->contents_scale(), 277 tiling->ContentRect()); 278 iter; 279 ++iter) { 280 EXPECT_TRUE(*iter); 281 EXPECT_FALSE(iter.geometry_rect().IsEmpty()); 282 if (iter.geometry_rect().Intersects(content_invalidation)) 283 EXPECT_EQ(pending_pile, iter->picture_pile()); 284 else 285 EXPECT_EQ(active_pile, iter->picture_pile()); 286 } 287 } 288 } 289 290 TEST_F(PictureLayerImplTest, CloneFullInvalidation) { 291 gfx::Size tile_size(90, 80); 292 gfx::Size layer_bounds(300, 500); 293 294 scoped_refptr<FakePicturePileImpl> pending_pile = 295 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 296 scoped_refptr<FakePicturePileImpl> active_pile = 297 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 298 299 SetupTrees(pending_pile, active_pile); 300 301 Region invalidation((gfx::Rect(layer_bounds))); 302 AddDefaultTilingsWithInvalidation(invalidation); 303 304 EXPECT_EQ(pending_layer_->tilings()->num_tilings(), 305 active_layer_->tilings()->num_tilings()); 306 307 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 308 EXPECT_GT(tilings->num_tilings(), 0u); 309 for (size_t i = 0; i < tilings->num_tilings(); ++i) 310 VerifyAllTilesExistAndHavePile(tilings->tiling_at(i), pending_pile.get()); 311 } 312 313 TEST_F(PictureLayerImplTest, NoInvalidationBoundsChange) { 314 gfx::Size tile_size(90, 80); 315 gfx::Size active_layer_bounds(300, 500); 316 gfx::Size pending_layer_bounds(400, 800); 317 318 scoped_refptr<FakePicturePileImpl> pending_pile = 319 FakePicturePileImpl::CreateFilledPile(tile_size, 320 pending_layer_bounds); 321 scoped_refptr<FakePicturePileImpl> active_pile = 322 FakePicturePileImpl::CreateFilledPile(tile_size, active_layer_bounds); 323 324 SetupTrees(pending_pile, active_pile); 325 pending_layer_->set_fixed_tile_size(gfx::Size(100, 100)); 326 327 Region invalidation; 328 AddDefaultTilingsWithInvalidation(invalidation); 329 330 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 331 EXPECT_GT(tilings->num_tilings(), 0u); 332 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 333 const PictureLayerTiling* tiling = tilings->tiling_at(i); 334 gfx::Rect active_content_bounds = gfx::ScaleToEnclosingRect( 335 gfx::Rect(active_layer_bounds), 336 tiling->contents_scale()); 337 for (PictureLayerTiling::CoverageIterator 338 iter(tiling, 339 tiling->contents_scale(), 340 tiling->ContentRect()); 341 iter; 342 ++iter) { 343 EXPECT_TRUE(*iter); 344 EXPECT_FALSE(iter.geometry_rect().IsEmpty()); 345 std::vector<Tile*> active_tiles = 346 active_layer_->tilings()->tiling_at(i)->AllTilesForTesting(); 347 std::vector<Tile*> pending_tiles = tiling->AllTilesForTesting(); 348 if (iter.geometry_rect().right() >= active_content_bounds.width() || 349 iter.geometry_rect().bottom() >= active_content_bounds.height() || 350 active_tiles[0]->content_rect().size() != 351 pending_tiles[0]->content_rect().size()) { 352 EXPECT_EQ(pending_pile, iter->picture_pile()); 353 } else { 354 EXPECT_EQ(active_pile, iter->picture_pile()); 355 } 356 } 357 } 358 } 359 360 TEST_F(PictureLayerImplTest, AddTilesFromNewRecording) { 361 gfx::Size tile_size(400, 400); 362 gfx::Size layer_bounds(1300, 1900); 363 364 scoped_refptr<FakePicturePileImpl> pending_pile = 365 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 366 scoped_refptr<FakePicturePileImpl> active_pile = 367 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 368 369 // Fill in some of active pile, but more of pending pile. 370 int hole_count = 0; 371 for (int x = 0; x < active_pile->tiling().num_tiles_x(); ++x) { 372 for (int y = 0; y < active_pile->tiling().num_tiles_y(); ++y) { 373 if ((x + y) % 2) { 374 pending_pile->AddRecordingAt(x, y); 375 active_pile->AddRecordingAt(x, y); 376 } else { 377 hole_count++; 378 if (hole_count % 2) 379 pending_pile->AddRecordingAt(x, y); 380 } 381 } 382 } 383 384 SetupTrees(pending_pile, active_pile); 385 Region invalidation; 386 AddDefaultTilingsWithInvalidation(invalidation); 387 388 const PictureLayerTilingSet* tilings = pending_layer_->tilings(); 389 EXPECT_GT(tilings->num_tilings(), 0u); 390 for (size_t i = 0; i < tilings->num_tilings(); ++i) { 391 const PictureLayerTiling* tiling = tilings->tiling_at(i); 392 393 for (PictureLayerTiling::CoverageIterator 394 iter(tiling, 395 tiling->contents_scale(), 396 tiling->ContentRect()); 397 iter; 398 ++iter) { 399 EXPECT_FALSE(iter.full_tile_geometry_rect().IsEmpty()); 400 // Ensure there is a recording for this tile. 401 gfx::Rect layer_rect = gfx::ScaleToEnclosingRect( 402 iter.full_tile_geometry_rect(), 1.f / tiling->contents_scale()); 403 layer_rect.Intersect(gfx::Rect(layer_bounds)); 404 405 bool in_pending = pending_pile->recorded_region().Contains(layer_rect); 406 bool in_active = active_pile->recorded_region().Contains(layer_rect); 407 408 if (in_pending && !in_active) 409 EXPECT_EQ(pending_pile, iter->picture_pile()); 410 else if (in_active) 411 EXPECT_EQ(active_pile, iter->picture_pile()); 412 else 413 EXPECT_FALSE(*iter); 414 } 415 } 416 } 417 418 TEST_F(PictureLayerImplTest, ManageTilingsWithNoRecording) { 419 gfx::Size tile_size(400, 400); 420 gfx::Size layer_bounds(1300, 1900); 421 422 scoped_refptr<FakePicturePileImpl> pending_pile = 423 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 424 scoped_refptr<FakePicturePileImpl> active_pile = 425 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 426 427 float result_scale_x, result_scale_y; 428 gfx::Size result_bounds; 429 430 SetupTrees(pending_pile, active_pile); 431 432 pending_layer_->CalculateContentsScale( 433 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 434 435 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 436 } 437 438 TEST_F(PictureLayerImplTest, ManageTilingsCreatesTilings) { 439 gfx::Size tile_size(400, 400); 440 gfx::Size layer_bounds(1300, 1900); 441 442 scoped_refptr<FakePicturePileImpl> pending_pile = 443 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 444 scoped_refptr<FakePicturePileImpl> active_pile = 445 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 446 447 float result_scale_x, result_scale_y; 448 gfx::Size result_bounds; 449 450 SetupTrees(pending_pile, active_pile); 451 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 452 453 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 454 EXPECT_LT(low_res_factor, 1.f); 455 456 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 457 1.7f, // device scale 458 3.2f, // page cale 459 false, 460 &result_scale_x, 461 &result_scale_y, 462 &result_bounds); 463 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 464 EXPECT_FLOAT_EQ( 465 1.3f, 466 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 467 EXPECT_FLOAT_EQ( 468 1.3f * low_res_factor, 469 pending_layer_->tilings()->tiling_at(1)->contents_scale()); 470 471 // If we change the layer's CSS scale factor, then we should not get new 472 // tilings. 473 pending_layer_->CalculateContentsScale(1.8f, // ideal contents scale 474 1.7f, // device scale 475 3.2f, // page cale 476 false, 477 &result_scale_x, 478 &result_scale_y, 479 &result_bounds); 480 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 481 EXPECT_FLOAT_EQ( 482 1.3f, 483 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 484 EXPECT_FLOAT_EQ( 485 1.3f * low_res_factor, 486 pending_layer_->tilings()->tiling_at(1)->contents_scale()); 487 488 // If we change the page scale factor, then we should get new tilings. 489 pending_layer_->CalculateContentsScale(1.8f, // ideal contents scale 490 1.7f, // device scale 491 2.2f, // page cale 492 false, 493 &result_scale_x, 494 &result_scale_y, 495 &result_bounds); 496 ASSERT_EQ(4u, pending_layer_->tilings()->num_tilings()); 497 EXPECT_FLOAT_EQ( 498 1.8f, 499 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 500 EXPECT_FLOAT_EQ( 501 1.8f * low_res_factor, 502 pending_layer_->tilings()->tiling_at(2)->contents_scale()); 503 504 // If we change the device scale factor, then we should get new tilings. 505 pending_layer_->CalculateContentsScale(1.9f, // ideal contents scale 506 1.4f, // device scale 507 2.2f, // page cale 508 false, 509 &result_scale_x, 510 &result_scale_y, 511 &result_bounds); 512 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings()); 513 EXPECT_FLOAT_EQ( 514 1.9f, 515 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 516 EXPECT_FLOAT_EQ( 517 1.9f * low_res_factor, 518 pending_layer_->tilings()->tiling_at(3)->contents_scale()); 519 520 // If we change the device scale factor, but end up at the same total scale 521 // factor somehow, then we don't get new tilings. 522 pending_layer_->CalculateContentsScale(1.9f, // ideal contents scale 523 2.2f, // device scale 524 1.4f, // page cale 525 false, 526 &result_scale_x, 527 &result_scale_y, 528 &result_bounds); 529 ASSERT_EQ(6u, pending_layer_->tilings()->num_tilings()); 530 EXPECT_FLOAT_EQ( 531 1.9f, 532 pending_layer_->tilings()->tiling_at(0)->contents_scale()); 533 EXPECT_FLOAT_EQ( 534 1.9f * low_res_factor, 535 pending_layer_->tilings()->tiling_at(3)->contents_scale()); 536 } 537 538 TEST_F(PictureLayerImplTest, CleanUpTilings) { 539 gfx::Size tile_size(400, 400); 540 gfx::Size layer_bounds(1300, 1900); 541 542 scoped_refptr<FakePicturePileImpl> pending_pile = 543 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 544 scoped_refptr<FakePicturePileImpl> active_pile = 545 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 546 547 float result_scale_x, result_scale_y; 548 gfx::Size result_bounds; 549 std::vector<PictureLayerTiling*> used_tilings; 550 551 SetupTrees(pending_pile, active_pile); 552 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 553 554 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 555 EXPECT_LT(low_res_factor, 1.f); 556 557 float device_scale = 1.7f; 558 float page_scale = 3.2f; 559 560 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false); 561 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 562 563 // We only have ideal tilings, so they aren't removed. 564 used_tilings.clear(); 565 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 566 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 567 568 // Changing the ideal but not creating new tilings. 569 SetContentsScaleOnBothLayers(1.5f, device_scale, page_scale, false); 570 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 571 572 // The tilings are still our target scale, so they aren't removed. 573 used_tilings.clear(); 574 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 575 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 576 577 // Create a 1.2 scale tiling. Now we have 1.0 and 1.2 tilings. Ideal = 1.2. 578 page_scale = 1.2f; 579 SetContentsScaleOnBothLayers(1.2f, device_scale, page_scale, false); 580 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings()); 581 EXPECT_FLOAT_EQ( 582 1.f, 583 active_layer_->tilings()->tiling_at(1)->contents_scale()); 584 EXPECT_FLOAT_EQ( 585 1.f * low_res_factor, 586 active_layer_->tilings()->tiling_at(3)->contents_scale()); 587 588 // Mark the non-ideal tilings as used. They won't be removed. 589 used_tilings.clear(); 590 used_tilings.push_back(active_layer_->tilings()->tiling_at(1)); 591 used_tilings.push_back(active_layer_->tilings()->tiling_at(3)); 592 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 593 ASSERT_EQ(4u, active_layer_->tilings()->num_tilings()); 594 595 // Now move the ideal scale to 0.5. Our target stays 1.2. 596 SetContentsScaleOnBothLayers(0.5f, device_scale, page_scale, false); 597 598 // The high resolution tiling is between target and ideal, so is not 599 // removed. The low res tiling for the old ideal=1.0 scale is removed. 600 used_tilings.clear(); 601 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 602 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 603 604 // Now move the ideal scale to 1.0. Our target stays 1.2. 605 SetContentsScaleOnBothLayers(1.f, device_scale, page_scale, false); 606 607 // All the tilings are between are target and the ideal, so they are not 608 // removed. 609 used_tilings.clear(); 610 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 611 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 612 613 // Now move the ideal scale to 1.1 on the active layer. Our target stays 1.2. 614 active_layer_->CalculateContentsScale(1.1f, 615 device_scale, 616 page_scale, 617 false, 618 &result_scale_x, 619 &result_scale_y, 620 &result_bounds); 621 622 // Because the pending layer's ideal scale is still 1.0, our tilings fall 623 // in the range [1.0,1.2] and are kept. 624 used_tilings.clear(); 625 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 626 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 627 628 // Move the ideal scale on the pending layer to 1.1 as well. Our target stays 629 // 1.2 still. 630 pending_layer_->CalculateContentsScale(1.1f, 631 device_scale, 632 page_scale, 633 false, 634 &result_scale_x, 635 &result_scale_y, 636 &result_bounds); 637 638 // Our 1.0 tiling now falls outside the range between our ideal scale and our 639 // target raster scale. But it is in our used tilings set, so nothing is 640 // deleted. 641 used_tilings.clear(); 642 used_tilings.push_back(active_layer_->tilings()->tiling_at(1)); 643 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 644 ASSERT_EQ(3u, active_layer_->tilings()->num_tilings()); 645 646 // If we remove it from our used tilings set, it is outside the range to keep 647 // so it is deleted. 648 used_tilings.clear(); 649 active_layer_->CleanUpTilingsOnActiveLayer(used_tilings); 650 ASSERT_EQ(2u, active_layer_->tilings()->num_tilings()); 651 } 652 653 #define EXPECT_BOTH_EQ(expression, x) \ 654 do { \ 655 EXPECT_EQ(pending_layer_->expression, x); \ 656 EXPECT_EQ(active_layer_->expression, x); \ 657 } while (false) 658 659 TEST_F(PictureLayerImplTest, DontAddLowResDuringAnimation) { 660 // Make sure this layer covers multiple tiles, since otherwise low 661 // res won't get created because it is too small. 662 gfx::Size tile_size(host_impl_.settings().default_tile_size); 663 SetupDefaultTrees(gfx::Size(tile_size.width() + 1, tile_size.height() + 1)); 664 // Avoid max untiled layer size heuristics via fixed tile size. 665 pending_layer_->set_fixed_tile_size(tile_size); 666 active_layer_->set_fixed_tile_size(tile_size); 667 668 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 669 float contents_scale = 1.f; 670 float device_scale = 1.f; 671 float page_scale = 1.f; 672 bool animating_transform = true; 673 674 // Animating, so don't create low res even if there isn't one already. 675 SetContentsScaleOnBothLayers( 676 contents_scale, device_scale, page_scale, animating_transform); 677 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f); 678 EXPECT_BOTH_EQ(num_tilings(), 1u); 679 680 // Stop animating, low res gets created. 681 animating_transform = false; 682 SetContentsScaleOnBothLayers( 683 contents_scale, device_scale, page_scale, animating_transform); 684 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 1.f); 685 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor); 686 EXPECT_BOTH_EQ(num_tilings(), 2u); 687 688 // Page scale animation, new high res, but not new low res because animating. 689 contents_scale = 4.f; 690 page_scale = 4.f; 691 animating_transform = true; 692 SetContentsScaleOnBothLayers( 693 contents_scale, device_scale, page_scale, animating_transform); 694 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f); 695 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), low_res_factor); 696 EXPECT_BOTH_EQ(num_tilings(), 3u); 697 698 // Stop animating, new low res gets created for final page scale. 699 animating_transform = false; 700 SetContentsScaleOnBothLayers( 701 contents_scale, device_scale, page_scale, animating_transform); 702 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), 4.f); 703 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 4.f * low_res_factor); 704 EXPECT_BOTH_EQ(num_tilings(), 4u); 705 } 706 707 TEST_F(PictureLayerImplTest, DontAddLowResForSmallLayers) { 708 gfx::Size tile_size(host_impl_.settings().default_tile_size); 709 SetupDefaultTrees(tile_size); 710 711 float low_res_factor = host_impl_.settings().low_res_contents_scale_factor; 712 float device_scale = 1.f; 713 float page_scale = 1.f; 714 bool animating_transform = false; 715 716 // Contents exactly fit on one tile at scale 1, no low res. 717 float contents_scale = 1.f; 718 SetContentsScaleOnBothLayers( 719 contents_scale, device_scale, page_scale, animating_transform); 720 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 721 EXPECT_BOTH_EQ(num_tilings(), 1u); 722 723 ResetTilingsAndRasterScales(); 724 725 // Contents that are smaller than one tile, no low res. 726 contents_scale = 0.123f; 727 SetContentsScaleOnBothLayers( 728 contents_scale, device_scale, page_scale, animating_transform); 729 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 730 EXPECT_BOTH_EQ(num_tilings(), 1u); 731 732 ResetTilingsAndRasterScales(); 733 734 // Any content bounds that would create more than one tile will 735 // generate a low res tiling. 736 contents_scale = 2.5f; 737 SetContentsScaleOnBothLayers( 738 contents_scale, device_scale, page_scale, animating_transform); 739 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 740 EXPECT_BOTH_EQ(LowResTiling()->contents_scale(), 741 contents_scale * low_res_factor); 742 EXPECT_BOTH_EQ(num_tilings(), 2u); 743 744 ResetTilingsAndRasterScales(); 745 746 // Mask layers dont create low res since they always fit on one tile. 747 pending_layer_->SetIsMask(true); 748 active_layer_->SetIsMask(true); 749 SetContentsScaleOnBothLayers( 750 contents_scale, device_scale, page_scale, animating_transform); 751 EXPECT_BOTH_EQ(HighResTiling()->contents_scale(), contents_scale); 752 EXPECT_BOTH_EQ(num_tilings(), 1u); 753 } 754 755 TEST_F(PictureLayerImplTest, DidLoseOutputSurface) { 756 gfx::Size tile_size(400, 400); 757 gfx::Size layer_bounds(1300, 1900); 758 759 scoped_refptr<FakePicturePileImpl> pending_pile = 760 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 761 scoped_refptr<FakePicturePileImpl> active_pile = 762 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 763 764 float result_scale_x, result_scale_y; 765 gfx::Size result_bounds; 766 767 SetupTrees(pending_pile, active_pile); 768 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 769 770 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 771 2.7f, // device scale 772 3.2f, // page cale 773 false, 774 &result_scale_x, 775 &result_scale_y, 776 &result_bounds); 777 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings()); 778 779 // All tilings should be removed when losing output surface. 780 active_layer_->DidLoseOutputSurface(); 781 EXPECT_EQ(0u, active_layer_->tilings()->num_tilings()); 782 pending_layer_->DidLoseOutputSurface(); 783 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 784 785 // This should create new tilings. 786 pending_layer_->CalculateContentsScale(1.3f, // ideal contents scale 787 2.7f, // device scale 788 3.2f, // page cale 789 false, 790 &result_scale_x, 791 &result_scale_y, 792 &result_bounds); 793 EXPECT_EQ(2u, pending_layer_->tilings()->num_tilings()); 794 } 795 796 TEST_F(PictureLayerImplTest, ClampTilesToToMaxTileSize) { 797 // The default max tile size is larger than 400x400. 798 gfx::Size tile_size(400, 400); 799 gfx::Size layer_bounds(5000, 5000); 800 801 scoped_refptr<FakePicturePileImpl> pending_pile = 802 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 803 scoped_refptr<FakePicturePileImpl> active_pile = 804 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 805 806 float result_scale_x, result_scale_y; 807 gfx::Size result_bounds; 808 809 SetupTrees(pending_pile, active_pile); 810 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 811 812 pending_layer_->CalculateContentsScale( 813 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 814 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 815 816 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 817 818 // The default value. 819 EXPECT_EQ(gfx::Size(256, 256).ToString(), 820 host_impl_.settings().default_tile_size.ToString()); 821 822 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 823 EXPECT_EQ(gfx::Size(256, 256).ToString(), 824 tile->content_rect().size().ToString()); 825 826 pending_layer_->DidLoseOutputSurface(); 827 828 // Change the max texture size on the output surface context. 829 scoped_ptr<TestWebGraphicsContext3D> context = 830 TestWebGraphicsContext3D::Create(); 831 context->set_max_texture_size(140); 832 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d( 833 context.PassAs<WebKit::WebGraphicsContext3D>()).PassAs<OutputSurface>()); 834 835 pending_layer_->CalculateContentsScale( 836 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 837 ASSERT_EQ(2u, pending_layer_->tilings()->num_tilings()); 838 839 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 840 841 // Verify the tiles are not larger than the context's max texture size. 842 tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 843 EXPECT_GE(140, tile->content_rect().width()); 844 EXPECT_GE(140, tile->content_rect().height()); 845 } 846 847 TEST_F(PictureLayerImplTest, ClampSingleTileToToMaxTileSize) { 848 // The default max tile size is larger than 400x400. 849 gfx::Size tile_size(400, 400); 850 gfx::Size layer_bounds(500, 500); 851 852 scoped_refptr<FakePicturePileImpl> pending_pile = 853 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 854 scoped_refptr<FakePicturePileImpl> active_pile = 855 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 856 857 float result_scale_x, result_scale_y; 858 gfx::Size result_bounds; 859 860 SetupTrees(pending_pile, active_pile); 861 EXPECT_EQ(0u, pending_layer_->tilings()->num_tilings()); 862 863 pending_layer_->CalculateContentsScale( 864 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 865 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings()); 866 867 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 868 869 // The default value. The layer is smaller than this. 870 EXPECT_EQ(gfx::Size(512, 512).ToString(), 871 host_impl_.settings().max_untiled_layer_size.ToString()); 872 873 // There should be a single tile since the layer is small. 874 PictureLayerTiling* high_res_tiling = pending_layer_->tilings()->tiling_at(0); 875 EXPECT_EQ(1u, high_res_tiling->AllTilesForTesting().size()); 876 877 pending_layer_->DidLoseOutputSurface(); 878 879 // Change the max texture size on the output surface context. 880 scoped_ptr<TestWebGraphicsContext3D> context = 881 TestWebGraphicsContext3D::Create(); 882 context->set_max_texture_size(140); 883 host_impl_.InitializeRenderer(FakeOutputSurface::Create3d( 884 context.PassAs<WebKit::WebGraphicsContext3D>()).PassAs<OutputSurface>()); 885 886 pending_layer_->CalculateContentsScale( 887 1.f, 1.f, 1.f, false, &result_scale_x, &result_scale_y, &result_bounds); 888 ASSERT_LE(1u, pending_layer_->tilings()->num_tilings()); 889 890 pending_layer_->tilings()->tiling_at(0)->CreateAllTilesForTesting(); 891 892 // There should be more than one tile since the max texture size won't cover 893 // the layer. 894 high_res_tiling = pending_layer_->tilings()->tiling_at(0); 895 EXPECT_LT(1u, high_res_tiling->AllTilesForTesting().size()); 896 897 // Verify the tiles are not larger than the context's max texture size. 898 Tile* tile = pending_layer_->tilings()->tiling_at(0)->AllTilesForTesting()[0]; 899 EXPECT_GE(140, tile->content_rect().width()); 900 EXPECT_GE(140, tile->content_rect().height()); 901 } 902 903 TEST_F(PictureLayerImplTest, DisallowTileDrawQuads) { 904 MockQuadCuller quad_culler; 905 906 gfx::Size tile_size(400, 400); 907 gfx::Size layer_bounds(1300, 1900); 908 909 scoped_refptr<FakePicturePileImpl> pending_pile = 910 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 911 scoped_refptr<FakePicturePileImpl> active_pile = 912 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 913 914 SetupTrees(pending_pile, active_pile); 915 916 active_layer_->SetContentBounds(layer_bounds); 917 active_layer_->draw_properties().visible_content_rect = 918 gfx::Rect(layer_bounds); 919 920 gfx::Rect layer_invalidation(150, 200, 30, 180); 921 Region invalidation(layer_invalidation); 922 AddDefaultTilingsWithInvalidation(invalidation); 923 924 AppendQuadsData data; 925 active_layer_->WillDraw(DRAW_MODE_RESOURCELESS_SOFTWARE, NULL); 926 active_layer_->AppendQuads(&quad_culler, &data); 927 active_layer_->DidDraw(NULL); 928 929 ASSERT_EQ(1U, quad_culler.quad_list().size()); 930 EXPECT_EQ(DrawQuad::PICTURE_CONTENT, quad_culler.quad_list()[0]->material); 931 } 932 933 TEST_F(PictureLayerImplTest, MarkRequiredNullTiles) { 934 gfx::Size tile_size(100, 100); 935 gfx::Size layer_bounds(1000, 1000); 936 937 scoped_refptr<FakePicturePileImpl> pending_pile = 938 FakePicturePileImpl::CreateEmptyPile(tile_size, layer_bounds); 939 // Layers with entirely empty piles can't get tilings. 940 pending_pile->AddRecordingAt(0, 0); 941 942 SetupPendingTree(pending_pile); 943 944 ASSERT_TRUE(pending_layer_->CanHaveTilings()); 945 pending_layer_->AddTiling(1.0f); 946 pending_layer_->AddTiling(2.0f); 947 948 // It should be safe to call this (and MarkVisibleResourcesAsRequired) 949 // on a layer with no recordings. 950 host_impl_.pending_tree()->UpdateDrawProperties(); 951 pending_layer_->MarkVisibleResourcesAsRequired(); 952 } 953 954 TEST_F(PictureLayerImplTest, MarkRequiredOffscreenTiles) { 955 gfx::Size tile_size(100, 100); 956 gfx::Size layer_bounds(200, 100); 957 958 scoped_refptr<FakePicturePileImpl> pending_pile = 959 FakePicturePileImpl::CreateFilledPile(tile_size, layer_bounds); 960 SetupPendingTree(pending_pile); 961 962 pending_layer_->set_fixed_tile_size(tile_size); 963 ASSERT_TRUE(pending_layer_->CanHaveTilings()); 964 PictureLayerTiling* tiling = pending_layer_->AddTiling(1.f); 965 host_impl_.pending_tree()->UpdateDrawProperties(); 966 EXPECT_EQ(tiling->resolution(), HIGH_RESOLUTION); 967 968 // Fake set priorities. 969 int tile_count = 0; 970 for (PictureLayerTiling::CoverageIterator iter( 971 tiling, 972 pending_layer_->contents_scale_x(), 973 gfx::Rect(pending_layer_->visible_content_rect())); 974 iter; 975 ++iter) { 976 if (!*iter) 977 continue; 978 Tile* tile = *iter; 979 TilePriority priority; 980 priority.resolution = HIGH_RESOLUTION; 981 if (++tile_count % 2) { 982 priority.time_to_visible_in_seconds = 0.f; 983 priority.distance_to_visible_in_pixels = 0.f; 984 } else { 985 priority.time_to_visible_in_seconds = 1.f; 986 priority.distance_to_visible_in_pixels = 1.f; 987 } 988 tile->SetPriority(PENDING_TREE, priority); 989 } 990 991 pending_layer_->MarkVisibleResourcesAsRequired(); 992 993 int num_visible = 0; 994 int num_offscreen = 0; 995 996 for (PictureLayerTiling::CoverageIterator iter( 997 tiling, 998 pending_layer_->contents_scale_x(), 999 gfx::Rect(pending_layer_->visible_content_rect())); 1000 iter; 1001 ++iter) { 1002 if (!*iter) 1003 continue; 1004 const Tile* tile = *iter; 1005 if (tile->priority(PENDING_TREE).distance_to_visible_in_pixels == 0.f) { 1006 EXPECT_TRUE(tile->required_for_activation()); 1007 num_visible++; 1008 } else { 1009 EXPECT_FALSE(tile->required_for_activation()); 1010 num_offscreen++; 1011 } 1012 } 1013 1014 EXPECT_GT(num_visible, 0); 1015 EXPECT_GT(num_offscreen, 0); 1016 } 1017 1018 } // namespace 1019 } // namespace cc 1020