| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/surfaces/surface_factory.h" | 5 #include "cc/surfaces/surface_factory.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 class SurfaceFactoryTest : public testing::Test, public SurfaceObserver { | 69 class SurfaceFactoryTest : public testing::Test, public SurfaceObserver { |
| 70 public: | 70 public: |
| 71 SurfaceFactoryTest() | 71 SurfaceFactoryTest() |
| 72 : factory_( | 72 : factory_( |
| 73 new SurfaceFactory(kArbitraryFrameSinkId, &manager_, &client_)), | 73 new SurfaceFactory(kArbitraryFrameSinkId, &manager_, &client_)), |
| 74 local_frame_id_(3, kArbitraryToken), | 74 local_frame_id_(3, kArbitraryToken), |
| 75 frame_sync_token_(GenTestSyncToken(4)), | 75 frame_sync_token_(GenTestSyncToken(4)), |
| 76 consumer_sync_token_(GenTestSyncToken(5)) { | 76 consumer_sync_token_(GenTestSyncToken(5)) { |
| 77 manager_.AddObserver(this); | 77 manager_.AddObserver(this); |
| 78 factory_->Create(local_frame_id_); | 78 factory_->SubmitCompositorFrame(local_frame_id_, CompositorFrame(), |
| 79 SurfaceFactory::DrawCallback()); |
| 79 } | 80 } |
| 80 | 81 |
| 81 const SurfaceId& last_created_surface_id() const { | 82 const SurfaceId& last_created_surface_id() const { |
| 82 return last_created_surface_id_; | 83 return last_created_surface_id_; |
| 83 } | 84 } |
| 84 | 85 |
| 85 // SurfaceObserver implementation. | 86 // SurfaceObserver implementation. |
| 86 void OnSurfaceCreated(const SurfaceId& surface_id, | 87 void OnSurfaceCreated(const SurfaceId& surface_id, |
| 87 const gfx::Size& frame, | 88 const gfx::Size& frame, |
| 88 float device_scale_factor) override { | 89 float device_scale_factor) override { |
| 89 EXPECT_EQ(kArbitraryFrameSinkId, surface_id.frame_sink_id()); | 90 EXPECT_EQ(kArbitraryFrameSinkId, surface_id.frame_sink_id()); |
| 90 last_created_surface_id_ = surface_id; | 91 last_created_surface_id_ = surface_id; |
| 91 } | 92 } |
| 92 | 93 |
| 93 void OnSurfaceDamaged(const SurfaceId& id, bool* changed) override { | 94 void OnSurfaceDamaged(const SurfaceId& id, bool* changed) override { |
| 94 *changed = true; | 95 *changed = true; |
| 95 } | 96 } |
| 96 | 97 |
| 97 ~SurfaceFactoryTest() override { | 98 ~SurfaceFactoryTest() override { |
| 98 if (local_frame_id_.is_valid()) | |
| 99 factory_->Destroy(local_frame_id_); | |
| 100 manager_.RemoveObserver(this); | 99 manager_.RemoveObserver(this); |
| 101 } | 100 } |
| 102 | 101 |
| 103 void SubmitCompositorFrameWithResources(ResourceId* resource_ids, | 102 void SubmitCompositorFrameWithResources(ResourceId* resource_ids, |
| 104 size_t num_resource_ids) { | 103 size_t num_resource_ids) { |
| 105 CompositorFrame frame; | 104 CompositorFrame frame; |
| 106 for (size_t i = 0u; i < num_resource_ids; ++i) { | 105 for (size_t i = 0u; i < num_resource_ids; ++i) { |
| 107 TransferableResource resource; | 106 TransferableResource resource; |
| 108 resource.id = resource_ids[i]; | 107 resource.id = resource_ids[i]; |
| 109 resource.mailbox_holder.texture_target = GL_TEXTURE_2D; | 108 resource.mailbox_holder.texture_target = GL_TEXTURE_2D; |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 int expected_returned_counts[] = {2, 2}; | 429 int expected_returned_counts[] = {2, 2}; |
| 431 CheckReturnedResourcesMatchExpected( | 430 CheckReturnedResourcesMatchExpected( |
| 432 expected_returned_ids, expected_returned_counts, | 431 expected_returned_ids, expected_returned_counts, |
| 433 arraysize(expected_returned_counts), consumer_sync_token_); | 432 arraysize(expected_returned_counts), consumer_sync_token_); |
| 434 } | 433 } |
| 435 } | 434 } |
| 436 | 435 |
| 437 TEST_F(SurfaceFactoryTest, BlankNoIndexIncrement) { | 436 TEST_F(SurfaceFactoryTest, BlankNoIndexIncrement) { |
| 438 LocalFrameId local_frame_id(6, kArbitraryToken); | 437 LocalFrameId local_frame_id(6, kArbitraryToken); |
| 439 SurfaceId surface_id(kArbitraryFrameSinkId, local_frame_id); | 438 SurfaceId surface_id(kArbitraryFrameSinkId, local_frame_id); |
| 440 factory_->Create(local_frame_id); | 439 factory_->SubmitCompositorFrame(local_frame_id, CompositorFrame(), |
| 440 SurfaceFactory::DrawCallback()); |
| 441 Surface* surface = manager_.GetSurfaceForId(surface_id); | 441 Surface* surface = manager_.GetSurfaceForId(surface_id); |
| 442 ASSERT_NE(nullptr, surface); | 442 ASSERT_NE(nullptr, surface); |
| 443 EXPECT_EQ(2, surface->frame_index()); | 443 EXPECT_EQ(2, surface->frame_index()); |
| 444 | |
| 445 factory_->SubmitCompositorFrame(local_frame_id, CompositorFrame(), | |
| 446 SurfaceFactory::DrawCallback()); | |
| 447 EXPECT_EQ(2, surface->frame_index()); | |
| 448 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id); | 444 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id); |
| 449 factory_->Destroy(local_frame_id); | |
| 450 } | 445 } |
| 451 | 446 |
| 452 void CreateSurfaceDrawCallback(SurfaceFactory* factory, | 447 // Tests that SurfaceFactory doesn't return resources after Reset(). |
| 453 uint32_t* execute_count) { | 448 TEST_F(SurfaceFactoryTest, Reset) { |
| 454 LocalFrameId new_id(7, base::UnguessableToken::Create()); | |
| 455 factory->Create(new_id); | |
| 456 factory->Destroy(new_id); | |
| 457 *execute_count += 1; | |
| 458 } | |
| 459 | |
| 460 TEST_F(SurfaceFactoryTest, AddDuringDestroy) { | |
| 461 LocalFrameId local_frame_id(6, kArbitraryToken); | |
| 462 factory_->Create(local_frame_id); | |
| 463 | |
| 464 uint32_t execute_count = 0; | |
| 465 factory_->SubmitCompositorFrame( | |
| 466 local_frame_id, CompositorFrame(), | |
| 467 base::Bind(&CreateSurfaceDrawCallback, base::Unretained(factory_.get()), | |
| 468 &execute_count)); | |
| 469 EXPECT_EQ(0u, execute_count); | |
| 470 factory_->Destroy(local_frame_id); | |
| 471 EXPECT_EQ(1u, execute_count); | |
| 472 } | |
| 473 | |
| 474 void DrawCallback(uint32_t* execute_count) { | |
| 475 *execute_count += 1; | |
| 476 } | |
| 477 | |
| 478 // Tests doing a DestroyAll before shutting down the factory; | |
| 479 TEST_F(SurfaceFactoryTest, DestroyAll) { | |
| 480 LocalFrameId id(7, kArbitraryToken); | 449 LocalFrameId id(7, kArbitraryToken); |
| 481 factory_->Create(id); | |
| 482 | 450 |
| 483 TransferableResource resource; | 451 TransferableResource resource; |
| 484 resource.id = 1; | 452 resource.id = 1; |
| 485 resource.mailbox_holder.texture_target = GL_TEXTURE_2D; | 453 resource.mailbox_holder.texture_target = GL_TEXTURE_2D; |
| 486 CompositorFrame frame; | 454 CompositorFrame frame; |
| 487 frame.resource_list.push_back(resource); | 455 frame.resource_list.push_back(resource); |
| 488 uint32_t execute_count = 0; | |
| 489 factory_->SubmitCompositorFrame(id, std::move(frame), | 456 factory_->SubmitCompositorFrame(id, std::move(frame), |
| 490 base::Bind(&DrawCallback, &execute_count)); | 457 SurfaceFactory::DrawCallback()); |
| 491 EXPECT_EQ(last_created_surface_id().local_frame_id(), id); | |
| 492 local_frame_id_ = LocalFrameId(); | |
| 493 factory_->DestroyAll(); | |
| 494 EXPECT_EQ(1u, execute_count); | |
| 495 } | |
| 496 | |
| 497 // Tests that SurfaceFactory doesn't return resources after Reset(). | |
| 498 TEST_F(SurfaceFactoryTest, Reset) { | |
| 499 LocalFrameId id(7, kArbitraryToken); | |
| 500 factory_->Create(id); | |
| 501 | |
| 502 TransferableResource resource; | |
| 503 resource.id = 1; | |
| 504 resource.mailbox_holder.texture_target = GL_TEXTURE_2D; | |
| 505 CompositorFrame frame; | |
| 506 frame.resource_list.push_back(resource); | |
| 507 uint32_t execute_count = 0; | |
| 508 factory_->SubmitCompositorFrame(id, std::move(frame), | |
| 509 base::Bind(&DrawCallback, &execute_count)); | |
| 510 EXPECT_EQ(last_created_surface_id().local_frame_id(), id); | 458 EXPECT_EQ(last_created_surface_id().local_frame_id(), id); |
| 511 | 459 |
| 512 SurfaceId surface_id(kArbitraryFrameSinkId, id); | 460 SurfaceId surface_id(kArbitraryFrameSinkId, id); |
| 513 manager_.AddSurfaceReference(manager_.GetRootSurfaceId(), surface_id); | 461 manager_.AddSurfaceReference(manager_.GetRootSurfaceId(), surface_id); |
| 514 factory_->Reset(); | 462 factory_->Reset(); |
| 515 EXPECT_TRUE(client_.returned_resources().empty()); | 463 EXPECT_TRUE(client_.returned_resources().empty()); |
| 516 manager_.RemoveSurfaceReference(manager_.GetRootSurfaceId(), surface_id); | 464 manager_.RemoveSurfaceReference(manager_.GetRootSurfaceId(), surface_id); |
| 517 EXPECT_TRUE(client_.returned_resources().empty()); | 465 EXPECT_TRUE(client_.returned_resources().empty()); |
| 518 local_frame_id_ = LocalFrameId(); | 466 local_frame_id_ = LocalFrameId(); |
| 519 } | 467 } |
| 520 | 468 |
| 521 TEST_F(SurfaceFactoryTest, DestroySequence) { | 469 TEST_F(SurfaceFactoryTest, DestroySequence) { |
| 522 LocalFrameId local_frame_id2(5, kArbitraryToken); | 470 LocalFrameId local_frame_id2(5, kArbitraryToken); |
| 471 std::unique_ptr<SurfaceFactory> factory2( |
| 472 new SurfaceFactory(kArbitraryFrameSinkId, &manager_, &client_)); |
| 523 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); | 473 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); |
| 524 factory_->Create(local_frame_id2); | 474 factory2->SubmitCompositorFrame(local_frame_id2, CompositorFrame(), |
| 475 SurfaceFactory::DrawCallback()); |
| 525 | 476 |
| 526 manager_.RegisterFrameSinkId(kArbitraryFrameSinkId); | 477 manager_.RegisterFrameSinkId(kArbitraryFrameSinkId); |
| 527 | 478 |
| 528 // Check that waiting before the sequence is satisfied works. | 479 // Check that waiting before the sequence is satisfied works. |
| 529 manager_.GetSurfaceForId(id2)->AddDestructionDependency( | 480 manager_.GetSurfaceForId(id2)->AddDestructionDependency( |
| 530 SurfaceSequence(kArbitraryFrameSinkId, 4)); | 481 SurfaceSequence(kArbitraryFrameSinkId, 4)); |
| 531 factory_->Destroy(local_frame_id2); | 482 factory2->Reset(); |
| 532 | 483 |
| 533 CompositorFrame frame; | 484 CompositorFrame frame; |
| 534 frame.metadata.satisfies_sequences.push_back(6); | 485 frame.metadata.satisfies_sequences.push_back(6); |
| 535 frame.metadata.satisfies_sequences.push_back(4); | 486 frame.metadata.satisfies_sequences.push_back(4); |
| 536 DCHECK(manager_.GetSurfaceForId(id2)); | 487 DCHECK(manager_.GetSurfaceForId(id2)); |
| 537 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), | 488 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), |
| 538 SurfaceFactory::DrawCallback()); | 489 SurfaceFactory::DrawCallback()); |
| 539 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id_); | |
| 540 DCHECK(!manager_.GetSurfaceForId(id2)); | 490 DCHECK(!manager_.GetSurfaceForId(id2)); |
| 541 | 491 |
| 542 // Check that waiting after the sequence is satisfied works. | 492 // Check that waiting after the sequence is satisfied works. |
| 543 factory_->Create(local_frame_id2); | 493 factory2->SubmitCompositorFrame(local_frame_id2, CompositorFrame(), |
| 494 SurfaceFactory::DrawCallback()); |
| 544 DCHECK(manager_.GetSurfaceForId(id2)); | 495 DCHECK(manager_.GetSurfaceForId(id2)); |
| 545 manager_.GetSurfaceForId(id2)->AddDestructionDependency( | 496 manager_.GetSurfaceForId(id2)->AddDestructionDependency( |
| 546 SurfaceSequence(kAnotherArbitraryFrameSinkId, 6)); | 497 SurfaceSequence(kAnotherArbitraryFrameSinkId, 6)); |
| 547 factory_->Destroy(local_frame_id2); | 498 factory2->Reset(); |
| 548 DCHECK(!manager_.GetSurfaceForId(id2)); | 499 DCHECK(!manager_.GetSurfaceForId(id2)); |
| 549 } | 500 } |
| 550 | 501 |
| 551 // Tests that Surface ID namespace invalidation correctly allows | 502 // Tests that Surface ID namespace invalidation correctly allows |
| 552 // Sequences to be ignored. | 503 // Sequences to be ignored. |
| 553 TEST_F(SurfaceFactoryTest, InvalidFrameSinkId) { | 504 TEST_F(SurfaceFactoryTest, InvalidFrameSinkId) { |
| 554 FrameSinkId frame_sink_id(1234, 5678); | 505 FrameSinkId frame_sink_id(1234, 5678); |
| 555 | 506 |
| 556 LocalFrameId local_frame_id(5, kArbitraryToken); | 507 LocalFrameId local_frame_id(5, kArbitraryToken); |
| 557 SurfaceId id(factory_->frame_sink_id(), local_frame_id); | 508 SurfaceId id(factory_->frame_sink_id(), local_frame_id); |
| 558 factory_->Create(local_frame_id); | 509 factory_->SubmitCompositorFrame(local_frame_id, CompositorFrame(), |
| 510 SurfaceFactory::DrawCallback()); |
| 559 | 511 |
| 560 manager_.RegisterFrameSinkId(frame_sink_id); | 512 manager_.RegisterFrameSinkId(frame_sink_id); |
| 561 manager_.GetSurfaceForId(id)->AddDestructionDependency( | 513 manager_.GetSurfaceForId(id)->AddDestructionDependency( |
| 562 SurfaceSequence(frame_sink_id, 4)); | 514 SurfaceSequence(frame_sink_id, 4)); |
| 563 factory_->Destroy(local_frame_id); | 515 |
| 516 factory_->Reset(); |
| 564 | 517 |
| 565 // Verify the dependency has prevented the surface from getting destroyed. | 518 // Verify the dependency has prevented the surface from getting destroyed. |
| 566 EXPECT_TRUE(manager_.GetSurfaceForId(id)); | 519 EXPECT_TRUE(manager_.GetSurfaceForId(id)); |
| 567 | 520 |
| 568 manager_.InvalidateFrameSinkId(frame_sink_id); | 521 manager_.InvalidateFrameSinkId(frame_sink_id); |
| 569 | 522 |
| 570 // Verify that the invalidated namespace caused the unsatisfied sequence | 523 // Verify that the invalidated namespace caused the unsatisfied sequence |
| 571 // to be ignored. | 524 // to be ignored. |
| 572 EXPECT_FALSE(manager_.GetSurfaceForId(id)); | 525 EXPECT_FALSE(manager_.GetSurfaceForId(id)); |
| 573 } | 526 } |
| 574 | 527 |
| 575 TEST_F(SurfaceFactoryTest, DestroyCycle) { | 528 TEST_F(SurfaceFactoryTest, DestroyCycle) { |
| 576 LocalFrameId local_frame_id2(5, kArbitraryToken); | 529 LocalFrameId local_frame_id2(5, kArbitraryToken); |
| 577 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); | 530 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); |
| 578 factory_->Create(local_frame_id2); | 531 std::unique_ptr<SurfaceFactory> factory2( |
| 579 | 532 new SurfaceFactory(kArbitraryFrameSinkId, &manager_, &client_)); |
| 580 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId); | 533 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId); |
| 581 | |
| 582 manager_.GetSurfaceForId(id2)->AddDestructionDependency( | |
| 583 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4)); | |
| 584 | |
| 585 // Give id2 a frame that references local_frame_id_. | 534 // Give id2 a frame that references local_frame_id_. |
| 586 { | 535 { |
| 587 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); | 536 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); |
| 588 CompositorFrame frame; | 537 CompositorFrame frame; |
| 589 frame.render_pass_list.push_back(std::move(render_pass)); | 538 frame.render_pass_list.push_back(std::move(render_pass)); |
| 590 frame.metadata.referenced_surfaces.push_back( | 539 frame.metadata.referenced_surfaces.push_back( |
| 591 SurfaceId(factory_->frame_sink_id(), local_frame_id_)); | 540 SurfaceId(factory_->frame_sink_id(), local_frame_id_)); |
| 592 factory_->SubmitCompositorFrame(local_frame_id2, std::move(frame), | 541 factory2->SubmitCompositorFrame(local_frame_id2, std::move(frame), |
| 593 SurfaceFactory::DrawCallback()); | 542 SurfaceFactory::DrawCallback()); |
| 594 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id2); | 543 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id2); |
| 595 } | 544 } |
| 596 factory_->Destroy(local_frame_id2); | 545 manager_.GetSurfaceForId(id2)->AddDestructionDependency( |
| 597 | 546 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4)); |
| 547 factory2->Reset(); |
| 598 // Give local_frame_id_ a frame that references id2. | 548 // Give local_frame_id_ a frame that references id2. |
| 599 { | 549 { |
| 600 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); | 550 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); |
| 601 CompositorFrame frame; | 551 CompositorFrame frame; |
| 602 frame.render_pass_list.push_back(std::move(render_pass)); | 552 frame.render_pass_list.push_back(std::move(render_pass)); |
| 603 frame.metadata.referenced_surfaces.push_back(id2); | 553 frame.metadata.referenced_surfaces.push_back(id2); |
| 604 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), | 554 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), |
| 605 SurfaceFactory::DrawCallback()); | 555 SurfaceFactory::DrawCallback()); |
| 606 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id_); | |
| 607 } | 556 } |
| 608 factory_->Destroy(local_frame_id_); | 557 factory_->Reset(); |
| 609 EXPECT_TRUE(manager_.GetSurfaceForId(id2)); | 558 EXPECT_TRUE(manager_.GetSurfaceForId(id2)); |
| 610 // local_frame_id_ should be retained by reference from id2. | 559 // local_frame_id_ should be retained by reference from id2. |
| 611 EXPECT_TRUE(manager_.GetSurfaceForId( | 560 EXPECT_TRUE(manager_.GetSurfaceForId( |
| 612 SurfaceId(factory_->frame_sink_id(), local_frame_id_))); | 561 SurfaceId(factory_->frame_sink_id(), local_frame_id_))); |
| 613 | 562 |
| 614 // Satisfy last destruction dependency for id2. | 563 // Satisfy last destruction dependency for id2. |
| 615 std::vector<uint32_t> to_satisfy; | 564 std::vector<uint32_t> to_satisfy; |
| 616 to_satisfy.push_back(4); | 565 to_satisfy.push_back(4); |
| 617 manager_.DidSatisfySequences(kAnotherArbitraryFrameSinkId, &to_satisfy); | 566 manager_.DidSatisfySequences(kAnotherArbitraryFrameSinkId, &to_satisfy); |
| 618 | 567 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 667 request = CopyOutputRequest::CreateRequest( | 616 request = CopyOutputRequest::CreateRequest( |
| 668 base::Bind(&CopyRequestTestCallback, &called3)); | 617 base::Bind(&CopyRequestTestCallback, &called3)); |
| 669 request->set_source(source1); | 618 request->set_source(source1); |
| 670 | 619 |
| 671 factory_->RequestCopyOfSurface(local_frame_id_, std::move(request)); | 620 factory_->RequestCopyOfSurface(local_frame_id_, std::move(request)); |
| 672 // Two callbacks are from source1, so the first should be called. | 621 // Two callbacks are from source1, so the first should be called. |
| 673 EXPECT_TRUE(called1); | 622 EXPECT_TRUE(called1); |
| 674 EXPECT_FALSE(called2); | 623 EXPECT_FALSE(called2); |
| 675 EXPECT_FALSE(called3); | 624 EXPECT_FALSE(called3); |
| 676 | 625 |
| 677 factory_->Destroy(local_frame_id_); | 626 factory_->Reset(); |
| 678 local_frame_id_ = LocalFrameId(); | 627 local_frame_id_ = LocalFrameId(); |
| 628 |
| 679 EXPECT_TRUE(called1); | 629 EXPECT_TRUE(called1); |
| 680 EXPECT_TRUE(called2); | 630 EXPECT_TRUE(called2); |
| 681 EXPECT_TRUE(called3); | 631 EXPECT_TRUE(called3); |
| 682 } | 632 } |
| 683 | 633 |
| 684 } // namespace | 634 } // namespace |
| 685 } // namespace cc | 635 } // namespace cc |
| OLD | NEW |