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->SubmitCompositorFrame(LocalFrameId(0, 0), CompositorFrame(), |
| 483 SurfaceFactory::DrawCallback()); |
532 | 484 |
533 CompositorFrame frame; | 485 CompositorFrame frame; |
534 frame.metadata.satisfies_sequences.push_back(6); | 486 frame.metadata.satisfies_sequences.push_back(6); |
535 frame.metadata.satisfies_sequences.push_back(4); | 487 frame.metadata.satisfies_sequences.push_back(4); |
536 DCHECK(manager_.GetSurfaceForId(id2)); | 488 DCHECK(manager_.GetSurfaceForId(id2)); |
537 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), | 489 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), |
538 SurfaceFactory::DrawCallback()); | 490 SurfaceFactory::DrawCallback()); |
539 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id_); | |
540 DCHECK(!manager_.GetSurfaceForId(id2)); | 491 DCHECK(!manager_.GetSurfaceForId(id2)); |
541 | 492 |
542 // Check that waiting after the sequence is satisfied works. | 493 // Check that waiting after the sequence is satisfied works. |
543 factory_->Create(local_frame_id2); | 494 factory2->SubmitCompositorFrame(local_frame_id2, CompositorFrame(), |
| 495 SurfaceFactory::DrawCallback()); |
544 DCHECK(manager_.GetSurfaceForId(id2)); | 496 DCHECK(manager_.GetSurfaceForId(id2)); |
545 manager_.GetSurfaceForId(id2)->AddDestructionDependency( | 497 manager_.GetSurfaceForId(id2)->AddDestructionDependency( |
546 SurfaceSequence(kAnotherArbitraryFrameSinkId, 6)); | 498 SurfaceSequence(kAnotherArbitraryFrameSinkId, 6)); |
547 factory_->Destroy(local_frame_id2); | 499 factory2->SubmitCompositorFrame(LocalFrameId(0, 1), CompositorFrame(), |
| 500 SurfaceFactory::DrawCallback()); |
548 DCHECK(!manager_.GetSurfaceForId(id2)); | 501 DCHECK(!manager_.GetSurfaceForId(id2)); |
549 } | 502 } |
550 | 503 |
551 // Tests that Surface ID namespace invalidation correctly allows | 504 // Tests that Surface ID namespace invalidation correctly allows |
552 // Sequences to be ignored. | 505 // Sequences to be ignored. |
553 TEST_F(SurfaceFactoryTest, InvalidFrameSinkId) { | 506 TEST_F(SurfaceFactoryTest, InvalidFrameSinkId) { |
554 FrameSinkId frame_sink_id(1234, 5678); | 507 FrameSinkId frame_sink_id(1234, 5678); |
555 | 508 |
556 LocalFrameId local_frame_id(5, kArbitraryToken); | 509 LocalFrameId local_frame_id(5, kArbitraryToken); |
557 SurfaceId id(factory_->frame_sink_id(), local_frame_id); | 510 SurfaceId id(factory_->frame_sink_id(), local_frame_id); |
558 factory_->Create(local_frame_id); | 511 factory_->SubmitCompositorFrame(local_frame_id, CompositorFrame(), |
| 512 SurfaceFactory::DrawCallback()); |
559 | 513 |
560 manager_.RegisterFrameSinkId(frame_sink_id); | 514 manager_.RegisterFrameSinkId(frame_sink_id); |
561 manager_.GetSurfaceForId(id)->AddDestructionDependency( | 515 manager_.GetSurfaceForId(id)->AddDestructionDependency( |
562 SurfaceSequence(frame_sink_id, 4)); | 516 SurfaceSequence(frame_sink_id, 4)); |
563 factory_->Destroy(local_frame_id); | 517 |
| 518 LocalFrameId local_frame_id_2(6, 0); |
| 519 factory_->SubmitCompositorFrame(local_frame_id_2, CompositorFrame(), |
| 520 SurfaceFactory::DrawCallback()); |
564 | 521 |
565 // Verify the dependency has prevented the surface from getting destroyed. | 522 // Verify the dependency has prevented the surface from getting destroyed. |
566 EXPECT_TRUE(manager_.GetSurfaceForId(id)); | 523 EXPECT_TRUE(manager_.GetSurfaceForId(id)); |
567 | 524 |
568 manager_.InvalidateFrameSinkId(frame_sink_id); | 525 manager_.InvalidateFrameSinkId(frame_sink_id); |
569 | 526 |
570 // Verify that the invalidated namespace caused the unsatisfied sequence | 527 // Verify that the invalidated namespace caused the unsatisfied sequence |
571 // to be ignored. | 528 // to be ignored. |
572 EXPECT_FALSE(manager_.GetSurfaceForId(id)); | 529 EXPECT_FALSE(manager_.GetSurfaceForId(id)); |
573 } | 530 } |
574 | 531 |
575 TEST_F(SurfaceFactoryTest, DestroyCycle) { | 532 TEST_F(SurfaceFactoryTest, DestroyCycle) { |
576 LocalFrameId local_frame_id2(5, kArbitraryToken); | 533 LocalFrameId local_frame_id2(5, kArbitraryToken); |
577 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); | 534 SurfaceId id2(kArbitraryFrameSinkId, local_frame_id2); |
578 factory_->Create(local_frame_id2); | 535 std::unique_ptr<SurfaceFactory> factory2( |
579 | 536 new SurfaceFactory(kArbitraryFrameSinkId, &manager_, &client_)); |
580 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId); | 537 manager_.RegisterFrameSinkId(kAnotherArbitraryFrameSinkId); |
581 | |
582 manager_.GetSurfaceForId(id2)->AddDestructionDependency( | |
583 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4)); | |
584 | |
585 // Give id2 a frame that references local_frame_id_. | 538 // Give id2 a frame that references local_frame_id_. |
586 { | 539 { |
587 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); | 540 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); |
588 CompositorFrame frame; | 541 CompositorFrame frame; |
589 frame.render_pass_list.push_back(std::move(render_pass)); | 542 frame.render_pass_list.push_back(std::move(render_pass)); |
590 frame.metadata.referenced_surfaces.push_back( | 543 frame.metadata.referenced_surfaces.push_back( |
591 SurfaceId(factory_->frame_sink_id(), local_frame_id_)); | 544 SurfaceId(factory_->frame_sink_id(), local_frame_id_)); |
592 factory_->SubmitCompositorFrame(local_frame_id2, std::move(frame), | 545 factory2->SubmitCompositorFrame(local_frame_id2, std::move(frame), |
593 SurfaceFactory::DrawCallback()); | 546 SurfaceFactory::DrawCallback()); |
594 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id2); | 547 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id2); |
595 } | 548 } |
596 factory_->Destroy(local_frame_id2); | 549 manager_.GetSurfaceForId(id2)->AddDestructionDependency( |
597 | 550 SurfaceSequence(kAnotherArbitraryFrameSinkId, 4)); |
| 551 factory2->SubmitCompositorFrame(LocalFrameId(0, 0), CompositorFrame(), |
| 552 SurfaceFactory::DrawCallback()); |
598 // Give local_frame_id_ a frame that references id2. | 553 // Give local_frame_id_ a frame that references id2. |
599 { | 554 { |
600 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); | 555 std::unique_ptr<RenderPass> render_pass(RenderPass::Create()); |
601 CompositorFrame frame; | 556 CompositorFrame frame; |
602 frame.render_pass_list.push_back(std::move(render_pass)); | 557 frame.render_pass_list.push_back(std::move(render_pass)); |
603 frame.metadata.referenced_surfaces.push_back(id2); | 558 frame.metadata.referenced_surfaces.push_back(id2); |
604 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), | 559 factory_->SubmitCompositorFrame(local_frame_id_, std::move(frame), |
605 SurfaceFactory::DrawCallback()); | 560 SurfaceFactory::DrawCallback()); |
606 EXPECT_EQ(last_created_surface_id().local_frame_id(), local_frame_id_); | |
607 } | 561 } |
608 factory_->Destroy(local_frame_id_); | 562 factory_->SubmitCompositorFrame(LocalFrameId(0, 1), CompositorFrame(), |
| 563 SurfaceFactory::DrawCallback()); |
609 EXPECT_TRUE(manager_.GetSurfaceForId(id2)); | 564 EXPECT_TRUE(manager_.GetSurfaceForId(id2)); |
610 // local_frame_id_ should be retained by reference from id2. | 565 // local_frame_id_ should be retained by reference from id2. |
611 EXPECT_TRUE(manager_.GetSurfaceForId( | 566 EXPECT_TRUE(manager_.GetSurfaceForId( |
612 SurfaceId(factory_->frame_sink_id(), local_frame_id_))); | 567 SurfaceId(factory_->frame_sink_id(), local_frame_id_))); |
613 | 568 |
614 // Satisfy last destruction dependency for id2. | 569 // Satisfy last destruction dependency for id2. |
615 std::vector<uint32_t> to_satisfy; | 570 std::vector<uint32_t> to_satisfy; |
616 to_satisfy.push_back(4); | 571 to_satisfy.push_back(4); |
617 manager_.DidSatisfySequences(kAnotherArbitraryFrameSinkId, &to_satisfy); | 572 manager_.DidSatisfySequences(kAnotherArbitraryFrameSinkId, &to_satisfy); |
618 | 573 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
667 request = CopyOutputRequest::CreateRequest( | 622 request = CopyOutputRequest::CreateRequest( |
668 base::Bind(&CopyRequestTestCallback, &called3)); | 623 base::Bind(&CopyRequestTestCallback, &called3)); |
669 request->set_source(source1); | 624 request->set_source(source1); |
670 | 625 |
671 factory_->RequestCopyOfSurface(local_frame_id_, std::move(request)); | 626 factory_->RequestCopyOfSurface(local_frame_id_, std::move(request)); |
672 // Two callbacks are from source1, so the first should be called. | 627 // Two callbacks are from source1, so the first should be called. |
673 EXPECT_TRUE(called1); | 628 EXPECT_TRUE(called1); |
674 EXPECT_FALSE(called2); | 629 EXPECT_FALSE(called2); |
675 EXPECT_FALSE(called3); | 630 EXPECT_FALSE(called3); |
676 | 631 |
677 factory_->Destroy(local_frame_id_); | |
678 local_frame_id_ = LocalFrameId(); | 632 local_frame_id_ = LocalFrameId(); |
| 633 factory_->SubmitCompositorFrame(local_frame_id_, CompositorFrame(), |
| 634 SurfaceFactory::DrawCallback()); |
679 EXPECT_TRUE(called1); | 635 EXPECT_TRUE(called1); |
680 EXPECT_TRUE(called2); | 636 EXPECT_TRUE(called2); |
681 EXPECT_TRUE(called3); | 637 EXPECT_TRUE(called3); |
682 } | 638 } |
683 | 639 |
684 } // namespace | 640 } // namespace |
685 } // namespace cc | 641 } // namespace cc |
OLD | NEW |