| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file contains the tests for the FencedAllocator class. | 5 // This file contains the tests for the FencedAllocator class. |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/memory/aligned_memory.h" | 9 #include "base/memory/aligned_memory.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 namespace gpu { | 24 namespace gpu { |
| 25 | 25 |
| 26 using testing::Return; | 26 using testing::Return; |
| 27 using testing::Mock; | 27 using testing::Mock; |
| 28 using testing::Truly; | 28 using testing::Truly; |
| 29 using testing::Sequence; | 29 using testing::Sequence; |
| 30 using testing::DoAll; | 30 using testing::DoAll; |
| 31 using testing::Invoke; | 31 using testing::Invoke; |
| 32 using testing::InvokeWithoutArgs; | |
| 33 using testing::_; | 32 using testing::_; |
| 34 | 33 |
| 35 class BaseFencedAllocatorTest : public testing::Test { | 34 class BaseFencedAllocatorTest : public testing::Test { |
| 36 protected: | 35 protected: |
| 37 static const unsigned int kBufferSize = 1024; | 36 static const unsigned int kBufferSize = 1024; |
| 38 static const int kAllocAlignment = 16; | 37 static const int kAllocAlignment = 16; |
| 39 | 38 |
| 40 virtual void SetUp() { | 39 virtual void SetUp() { |
| 41 api_mock_.reset(new AsyncAPIMock); | 40 api_mock_.reset(new AsyncAPIMock); |
| 42 // ignore noops in the mock - we don't want to inspect the internals of the | 41 // ignore noops in the mock - we don't want to inspect the internals of the |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; | 81 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; |
| 83 scoped_ptr<CommandBufferService> command_buffer_; | 82 scoped_ptr<CommandBufferService> command_buffer_; |
| 84 scoped_ptr<GpuScheduler> gpu_scheduler_; | 83 scoped_ptr<GpuScheduler> gpu_scheduler_; |
| 85 scoped_ptr<CommandBufferHelper> helper_; | 84 scoped_ptr<CommandBufferHelper> helper_; |
| 86 }; | 85 }; |
| 87 | 86 |
| 88 #ifndef _MSC_VER | 87 #ifndef _MSC_VER |
| 89 const unsigned int BaseFencedAllocatorTest::kBufferSize; | 88 const unsigned int BaseFencedAllocatorTest::kBufferSize; |
| 90 #endif | 89 #endif |
| 91 | 90 |
| 92 namespace { | |
| 93 void EmptyPoll() { | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 // Test fixture for FencedAllocator test - Creates a FencedAllocator, using a | 91 // Test fixture for FencedAllocator test - Creates a FencedAllocator, using a |
| 98 // CommandBufferHelper with a mock AsyncAPIInterface for its interface (calling | 92 // CommandBufferHelper with a mock AsyncAPIInterface for its interface (calling |
| 99 // it directly, not through the RPC mechanism), making sure Noops are ignored | 93 // it directly, not through the RPC mechanism), making sure Noops are ignored |
| 100 // and SetToken are properly forwarded to the engine. | 94 // and SetToken are properly forwarded to the engine. |
| 101 class FencedAllocatorTest : public BaseFencedAllocatorTest { | 95 class FencedAllocatorTest : public BaseFencedAllocatorTest { |
| 102 protected: | 96 protected: |
| 103 virtual void SetUp() { | 97 virtual void SetUp() { |
| 104 BaseFencedAllocatorTest::SetUp(); | 98 BaseFencedAllocatorTest::SetUp(); |
| 105 allocator_.reset(new FencedAllocator(kBufferSize, | 99 allocator_.reset(new FencedAllocator(kBufferSize, helper_.get())); |
| 106 helper_.get(), | |
| 107 base::Bind(&EmptyPoll))); | |
| 108 } | 100 } |
| 109 | 101 |
| 110 virtual void TearDown() { | 102 virtual void TearDown() { |
| 111 // If the GpuScheduler posts any tasks, this forces them to run. | 103 // If the GpuScheduler posts any tasks, this forces them to run. |
| 112 base::MessageLoop::current()->RunUntilIdle(); | 104 base::MessageLoop::current()->RunUntilIdle(); |
| 113 | 105 |
| 114 EXPECT_TRUE(allocator_->CheckConsistency()); | 106 EXPECT_TRUE(allocator_->CheckConsistency()); |
| 115 | 107 |
| 116 BaseFencedAllocatorTest::TearDown(); | 108 BaseFencedAllocatorTest::TearDown(); |
| 117 } | 109 } |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 // Check that the token has indeed passed. | 384 // Check that the token has indeed passed. |
| 393 EXPECT_LE(token, GetToken()); | 385 EXPECT_LE(token, GetToken()); |
| 394 allocator_->Free(offset); | 386 allocator_->Free(offset); |
| 395 | 387 |
| 396 // Everything now has been freed... | 388 // Everything now has been freed... |
| 397 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize()); | 389 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeOrPendingSize()); |
| 398 // ... for real. | 390 // ... for real. |
| 399 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSize()); | 391 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSize()); |
| 400 } | 392 } |
| 401 | 393 |
| 402 class FencedAllocatorPollTest : public BaseFencedAllocatorTest { | |
| 403 public: | |
| 404 static const unsigned int kAllocSize = 128; | |
| 405 | |
| 406 MOCK_METHOD0(MockedPoll, void()); | |
| 407 | |
| 408 protected: | |
| 409 virtual void TearDown() { | |
| 410 // If the GpuScheduler posts any tasks, this forces them to run. | |
| 411 base::MessageLoop::current()->RunUntilIdle(); | |
| 412 | |
| 413 BaseFencedAllocatorTest::TearDown(); | |
| 414 } | |
| 415 }; | |
| 416 | |
| 417 TEST_F(FencedAllocatorPollTest, TestPoll) { | |
| 418 scoped_ptr<FencedAllocator> allocator( | |
| 419 new FencedAllocator(kBufferSize, | |
| 420 helper_.get(), | |
| 421 base::Bind(&FencedAllocatorPollTest::MockedPoll, | |
| 422 base::Unretained(this)))); | |
| 423 | |
| 424 FencedAllocator::Offset mem1 = allocator->Alloc(kAllocSize); | |
| 425 FencedAllocator::Offset mem2 = allocator->Alloc(kAllocSize); | |
| 426 EXPECT_NE(mem1, FencedAllocator::kInvalidOffset); | |
| 427 EXPECT_NE(mem2, FencedAllocator::kInvalidOffset); | |
| 428 EXPECT_TRUE(allocator->CheckConsistency()); | |
| 429 EXPECT_EQ(allocator->bytes_in_use(), kAllocSize * 2); | |
| 430 | |
| 431 // Check that no-op Poll doesn't affect the state. | |
| 432 EXPECT_CALL(*this, MockedPoll()).RetiresOnSaturation(); | |
| 433 allocator->FreeUnused(); | |
| 434 EXPECT_TRUE(allocator->CheckConsistency()); | |
| 435 EXPECT_EQ(allocator->bytes_in_use(), kAllocSize * 2); | |
| 436 | |
| 437 // Check that freeing in Poll works. | |
| 438 base::Closure free_mem1_closure = | |
| 439 base::Bind(&FencedAllocator::Free, | |
| 440 base::Unretained(allocator.get()), | |
| 441 mem1); | |
| 442 EXPECT_CALL(*this, MockedPoll()) | |
| 443 .WillOnce(InvokeWithoutArgs(&free_mem1_closure, &base::Closure::Run)) | |
| 444 .RetiresOnSaturation(); | |
| 445 allocator->FreeUnused(); | |
| 446 EXPECT_TRUE(allocator->CheckConsistency()); | |
| 447 EXPECT_EQ(allocator->bytes_in_use(), kAllocSize * 1); | |
| 448 | |
| 449 // Check that freeing still works. | |
| 450 EXPECT_CALL(*this, MockedPoll()).RetiresOnSaturation(); | |
| 451 allocator->Free(mem2); | |
| 452 allocator->FreeUnused(); | |
| 453 EXPECT_TRUE(allocator->CheckConsistency()); | |
| 454 EXPECT_EQ(allocator->bytes_in_use(), 0u); | |
| 455 | |
| 456 allocator.reset(); | |
| 457 } | |
| 458 | |
| 459 // Test fixture for FencedAllocatorWrapper test - Creates a | 394 // Test fixture for FencedAllocatorWrapper test - Creates a |
| 460 // FencedAllocatorWrapper, using a CommandBufferHelper with a mock | 395 // FencedAllocatorWrapper, using a CommandBufferHelper with a mock |
| 461 // AsyncAPIInterface for its interface (calling it directly, not through the | 396 // AsyncAPIInterface for its interface (calling it directly, not through the |
| 462 // RPC mechanism), making sure Noops are ignored and SetToken are properly | 397 // RPC mechanism), making sure Noops are ignored and SetToken are properly |
| 463 // forwarded to the engine. | 398 // forwarded to the engine. |
| 464 class FencedAllocatorWrapperTest : public BaseFencedAllocatorTest { | 399 class FencedAllocatorWrapperTest : public BaseFencedAllocatorTest { |
| 465 protected: | 400 protected: |
| 466 virtual void SetUp() { | 401 virtual void SetUp() { |
| 467 BaseFencedAllocatorTest::SetUp(); | 402 BaseFencedAllocatorTest::SetUp(); |
| 468 | 403 |
| 469 // Though allocating this buffer isn't strictly necessary, it makes | 404 // Though allocating this buffer isn't strictly necessary, it makes |
| 470 // allocations point to valid addresses, so they could be used for | 405 // allocations point to valid addresses, so they could be used for |
| 471 // something. | 406 // something. |
| 472 buffer_.reset(static_cast<char*>(base::AlignedAlloc( | 407 buffer_.reset(static_cast<char*>(base::AlignedAlloc( |
| 473 kBufferSize, kAllocAlignment))); | 408 kBufferSize, kAllocAlignment))); |
| 474 allocator_.reset(new FencedAllocatorWrapper(kBufferSize, | 409 allocator_.reset(new FencedAllocatorWrapper(kBufferSize, helper_.get(), |
| 475 helper_.get(), | |
| 476 base::Bind(&EmptyPoll), | |
| 477 buffer_.get())); | 410 buffer_.get())); |
| 478 } | 411 } |
| 479 | 412 |
| 480 virtual void TearDown() { | 413 virtual void TearDown() { |
| 481 // If the GpuScheduler posts any tasks, this forces them to run. | 414 // If the GpuScheduler posts any tasks, this forces them to run. |
| 482 base::MessageLoop::current()->RunUntilIdle(); | 415 base::MessageLoop::current()->RunUntilIdle(); |
| 483 | 416 |
| 484 EXPECT_TRUE(allocator_->CheckConsistency()); | 417 EXPECT_TRUE(allocator_->CheckConsistency()); |
| 485 | 418 |
| 486 BaseFencedAllocatorTest::TearDown(); | 419 BaseFencedAllocatorTest::TearDown(); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 EXPECT_LE(token, GetToken()); | 569 EXPECT_LE(token, GetToken()); |
| 637 | 570 |
| 638 // Free up everything. | 571 // Free up everything. |
| 639 for (unsigned int i = 0; i < kAllocCount; ++i) { | 572 for (unsigned int i = 0; i < kAllocCount; ++i) { |
| 640 allocator_->Free(pointers[i]); | 573 allocator_->Free(pointers[i]); |
| 641 EXPECT_TRUE(allocator_->CheckConsistency()); | 574 EXPECT_TRUE(allocator_->CheckConsistency()); |
| 642 } | 575 } |
| 643 } | 576 } |
| 644 | 577 |
| 645 } // namespace gpu | 578 } // namespace gpu |
| OLD | NEW |