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 #include "gpu/command_buffer/service/transfer_buffer_manager.h" | 5 #include "gpu/command_buffer/service/transfer_buffer_manager.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 | 10 |
11 using base::SharedMemory; | 11 using base::SharedMemory; |
12 | 12 |
13 namespace gpu { | 13 namespace gpu { |
14 | 14 |
15 const static size_t kBufferSize = 1024; | 15 const static size_t kBufferSize = 1024; |
16 | 16 |
17 class TransferBufferManagerTest : public testing::Test { | 17 class TransferBufferManagerTest : public testing::Test { |
18 protected: | 18 protected: |
19 virtual void SetUp() { | 19 virtual void SetUp() { |
20 for (size_t i = 0; i < arraysize(buffers_); ++i) { | |
21 buffers_[i].CreateAnonymous(kBufferSize); | |
22 buffers_[i].Map(kBufferSize); | |
23 } | |
24 | |
25 TransferBufferManager* manager = new TransferBufferManager(); | 20 TransferBufferManager* manager = new TransferBufferManager(); |
26 transfer_buffer_manager_.reset(manager); | 21 transfer_buffer_manager_.reset(manager); |
27 ASSERT_TRUE(manager->Initialize()); | 22 ASSERT_TRUE(manager->Initialize()); |
28 } | 23 } |
29 | 24 |
30 base::SharedMemory buffers_[3]; | |
31 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; | 25 scoped_ptr<TransferBufferManagerInterface> transfer_buffer_manager_; |
32 }; | 26 }; |
33 | 27 |
34 TEST_F(TransferBufferManagerTest, ZeroHandleMapsToNull) { | 28 TEST_F(TransferBufferManagerTest, ZeroHandleMapsToNull) { |
35 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(0)); | 29 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(0)); |
36 } | 30 } |
37 | 31 |
38 TEST_F(TransferBufferManagerTest, NegativeHandleMapsToNull) { | 32 TEST_F(TransferBufferManagerTest, NegativeHandleMapsToNull) { |
39 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(-1)); | 33 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(-1)); |
40 } | 34 } |
41 | 35 |
42 TEST_F(TransferBufferManagerTest, OutOfRangeHandleMapsToNull) { | 36 TEST_F(TransferBufferManagerTest, OutOfRangeHandleMapsToNull) { |
43 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(1)); | 37 EXPECT_TRUE(NULL == transfer_buffer_manager_->GetTransferBuffer(1)); |
44 } | 38 } |
45 | 39 |
46 TEST_F(TransferBufferManagerTest, CanRegisterTransferBuffer) { | 40 TEST_F(TransferBufferManagerTest, CanRegisterTransferBuffer) { |
47 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 41 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
48 &buffers_[0], | 42 shm->CreateAndMapAnonymous(kBufferSize); |
49 kBufferSize)); | 43 base::SharedMemory* shm_raw_pointer = shm.get(); |
| 44 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 45 1, shm.Pass(), kBufferSize)); |
50 scoped_refptr<Buffer> registered = | 46 scoped_refptr<Buffer> registered = |
51 transfer_buffer_manager_->GetTransferBuffer(1); | 47 transfer_buffer_manager_->GetTransferBuffer(1); |
52 | 48 |
53 // Distinct memory range and shared memory handle from that originally | 49 // Shared-memory ownership is transfered. It should be the same memory. |
54 // registered. | 50 EXPECT_EQ(shm_raw_pointer, registered->shared_memory()); |
55 scoped_refptr<Buffer> null_buffer; | |
56 EXPECT_NE(null_buffer, registered); | |
57 EXPECT_NE(buffers_[0].memory(), registered->memory()); | |
58 EXPECT_EQ(kBufferSize, registered->size()); | |
59 EXPECT_NE(&buffers_[0], registered->shared_memory()); | |
60 | |
61 // But maps to the same physical memory. | |
62 *static_cast<int*>(registered->memory()) = 7; | |
63 *static_cast<int*>(buffers_[0].memory()) = 8; | |
64 EXPECT_EQ(8, *static_cast<int*>(registered->memory())); | |
65 } | 51 } |
66 | 52 |
67 TEST_F(TransferBufferManagerTest, CanDestroyTransferBuffer) { | 53 TEST_F(TransferBufferManagerTest, CanDestroyTransferBuffer) { |
68 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 54 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
69 &buffers_[0], | 55 shm->CreateAndMapAnonymous(kBufferSize); |
70 kBufferSize)); | 56 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 57 1, shm.Pass(), kBufferSize)); |
71 transfer_buffer_manager_->DestroyTransferBuffer(1); | 58 transfer_buffer_manager_->DestroyTransferBuffer(1); |
72 scoped_refptr<Buffer> registered = | 59 scoped_refptr<Buffer> registered = |
73 transfer_buffer_manager_->GetTransferBuffer(1); | 60 transfer_buffer_manager_->GetTransferBuffer(1); |
74 | 61 |
75 scoped_refptr<Buffer> null_buffer; | 62 scoped_refptr<Buffer> null_buffer; |
76 EXPECT_EQ(null_buffer, registered); | 63 EXPECT_EQ(null_buffer, registered); |
77 } | 64 } |
78 | 65 |
79 TEST_F(TransferBufferManagerTest, CannotRegregisterTransferBufferId) { | 66 TEST_F(TransferBufferManagerTest, CannotRegregisterTransferBufferId) { |
80 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 67 scoped_ptr<base::SharedMemory> shm1(new base::SharedMemory()); |
81 &buffers_[0], | 68 scoped_ptr<base::SharedMemory> shm2(new base::SharedMemory()); |
82 kBufferSize)); | 69 scoped_ptr<base::SharedMemory> shm3(new base::SharedMemory()); |
83 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 70 shm1->CreateAndMapAnonymous(kBufferSize); |
84 &buffers_[0], | 71 shm2->CreateAndMapAnonymous(kBufferSize); |
85 kBufferSize)); | 72 shm3->CreateAndMapAnonymous(kBufferSize); |
86 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 73 |
87 &buffers_[1], | 74 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer( |
88 kBufferSize)); | 75 1, shm1.Pass(), kBufferSize)); |
| 76 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 77 1, shm2.Pass(), kBufferSize)); |
| 78 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 79 1, shm3.Pass(), kBufferSize)); |
89 } | 80 } |
90 | 81 |
91 TEST_F(TransferBufferManagerTest, CanReuseTransferBufferIdAfterDestroying) { | 82 TEST_F(TransferBufferManagerTest, CanReuseTransferBufferIdAfterDestroying) { |
92 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 83 scoped_ptr<base::SharedMemory> shm1(new base::SharedMemory()); |
93 &buffers_[0], | 84 scoped_ptr<base::SharedMemory> shm2(new base::SharedMemory()); |
94 kBufferSize)); | 85 shm1->CreateAndMapAnonymous(kBufferSize); |
| 86 shm2->CreateAndMapAnonymous(kBufferSize); |
| 87 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 88 1, shm1.Pass(), kBufferSize)); |
95 transfer_buffer_manager_->DestroyTransferBuffer(1); | 89 transfer_buffer_manager_->DestroyTransferBuffer(1); |
96 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer(1, | 90 EXPECT_TRUE(transfer_buffer_manager_->RegisterTransferBuffer( |
97 &buffers_[1], | 91 1, shm2.Pass(), kBufferSize)); |
98 kBufferSize)); | |
99 } | 92 } |
100 | 93 |
101 TEST_F(TransferBufferManagerTest, DestroyUnusedTransferBufferIdDoesNotCrash) { | 94 TEST_F(TransferBufferManagerTest, DestroyUnusedTransferBufferIdDoesNotCrash) { |
102 transfer_buffer_manager_->DestroyTransferBuffer(1); | 95 transfer_buffer_manager_->DestroyTransferBuffer(1); |
103 } | 96 } |
104 | 97 |
105 TEST_F(TransferBufferManagerTest, CannotRegisterNullTransferBuffer) { | 98 TEST_F(TransferBufferManagerTest, CannotRegisterNullTransferBuffer) { |
106 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer(0, | 99 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
107 &buffers_[0], | 100 shm->CreateAndMapAnonymous(kBufferSize); |
108 kBufferSize)); | 101 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 102 0, shm.Pass(), kBufferSize)); |
109 } | 103 } |
110 | 104 |
111 TEST_F(TransferBufferManagerTest, CannotRegisterNegativeTransferBufferId) { | 105 TEST_F(TransferBufferManagerTest, CannotRegisterNegativeTransferBufferId) { |
112 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer(-1, | 106 scoped_ptr<base::SharedMemory> shm(new base::SharedMemory()); |
113 &buffers_[0], | 107 shm->CreateAndMapAnonymous(kBufferSize); |
114 kBufferSize)); | 108 EXPECT_FALSE(transfer_buffer_manager_->RegisterTransferBuffer( |
| 109 -1, shm.Pass(), kBufferSize)); |
115 } | 110 } |
116 | 111 |
117 } // namespace gpu | 112 } // namespace gpu |
OLD | NEW |