OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017 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 "gpu/command_buffer/client/client_discardable_manager.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace gpu { |
| 9 namespace { |
| 10 class FakeCommandBuffer : public CommandBuffer { |
| 11 public: |
| 12 FakeCommandBuffer() = default; |
| 13 ~FakeCommandBuffer() override { EXPECT_TRUE(active_ids_.empty()); } |
| 14 // Overridden from CommandBuffer: |
| 15 State GetLastState() override { |
| 16 NOTREACHED(); |
| 17 return State(); |
| 18 }; |
| 19 void Flush(int32_t put_offset) override { NOTREACHED(); } |
| 20 void OrderingBarrier(int32_t put_offset) override { NOTREACHED(); } |
| 21 State WaitForTokenInRange(int32_t start, int32_t end) override { |
| 22 NOTREACHED(); |
| 23 |
| 24 return State(); |
| 25 } |
| 26 State WaitForGetOffsetInRange(int32_t start, int32_t end) override { |
| 27 NOTREACHED(); |
| 28 return State(); |
| 29 } |
| 30 void SetGetBuffer(int32_t transfer_buffer_id) override { NOTREACHED(); } |
| 31 scoped_refptr<gpu::Buffer> CreateTransferBuffer(size_t size, |
| 32 int32_t* id) override { |
| 33 EXPECT_EQ(4096u, size); |
| 34 *id = next_id_++; |
| 35 active_ids_.insert(*id); |
| 36 std::unique_ptr<base::SharedMemory> shared_mem(new base::SharedMemory); |
| 37 shared_mem->CreateAndMapAnonymous(size); |
| 38 return MakeBufferFromSharedMemory(std::move(shared_mem), size); |
| 39 } |
| 40 void DestroyTransferBuffer(int32_t id) override { |
| 41 auto found = active_ids_.find(id); |
| 42 EXPECT_TRUE(found != active_ids_.end()); |
| 43 active_ids_.erase(found); |
| 44 } |
| 45 |
| 46 private: |
| 47 int32_t next_id_ = 1; |
| 48 std::set<int32_t> active_ids_; |
| 49 }; |
| 50 |
| 51 void UnlockClientHandleForTesting( |
| 52 const ClientDiscardableHandle& client_handle) { |
| 53 ServiceDiscardableHandle service_handle(client_handle.BufferForTesting(), |
| 54 client_handle.byte_offset(), |
| 55 client_handle.shm_id()); |
| 56 service_handle.Unlock(); |
| 57 } |
| 58 |
| 59 bool DeleteClientHandleForTesting( |
| 60 const ClientDiscardableHandle& client_handle) { |
| 61 ServiceDiscardableHandle service_handle(client_handle.BufferForTesting(), |
| 62 client_handle.byte_offset(), |
| 63 client_handle.shm_id()); |
| 64 return service_handle.Delete(); |
| 65 } |
| 66 |
| 67 void UnlockAndDeleteClientHandleForTesting( |
| 68 const ClientDiscardableHandle& client_handle) { |
| 69 UnlockClientHandleForTesting(client_handle); |
| 70 EXPECT_TRUE(DeleteClientHandleForTesting(client_handle)); |
| 71 } |
| 72 |
| 73 } // namespace |
| 74 |
| 75 TEST(ClientDiscardableManagerTest, BasicUsage) { |
| 76 FakeCommandBuffer command_buffer; |
| 77 ClientDiscardableManager manager; |
| 78 { |
| 79 ClientDiscardableHandle handle = |
| 80 manager.InitializeTexture(&command_buffer, 1); |
| 81 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 82 EXPECT_EQ(handle.shm_id(), 1); |
| 83 EXPECT_FALSE(DeleteClientHandleForTesting(handle)); |
| 84 UnlockClientHandleForTesting(handle); |
| 85 manager.LockTexture(1); |
| 86 EXPECT_FALSE(DeleteClientHandleForTesting(handle)); |
| 87 UnlockAndDeleteClientHandleForTesting(handle); |
| 88 } |
| 89 manager.FreeTexture(1); |
| 90 manager.CheckPendingForTesting(&command_buffer); |
| 91 } |
| 92 |
| 93 TEST(ClientDiscardableManagerTest, Reuse) { |
| 94 FakeCommandBuffer command_buffer; |
| 95 ClientDiscardableManager manager; |
| 96 manager.SetElementCountForTesting(1024); |
| 97 for (int i = 1; i <= 1024; ++i) { |
| 98 ClientDiscardableHandle handle = |
| 99 manager.InitializeTexture(&command_buffer, i); |
| 100 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 101 EXPECT_EQ(handle.shm_id(), 1); |
| 102 UnlockAndDeleteClientHandleForTesting(handle); |
| 103 } |
| 104 // Delete every other entry. |
| 105 for (int i = 1; i <= 1024; i += 2) { |
| 106 manager.FreeTexture(i); |
| 107 } |
| 108 // Allocate 512 more entries, ensure we re-use the original buffer. |
| 109 for (int i = 1; i <= 512; ++i) { |
| 110 ClientDiscardableHandle handle = |
| 111 manager.InitializeTexture(&command_buffer, 1024 + i); |
| 112 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 113 EXPECT_EQ(handle.shm_id(), 1); |
| 114 UnlockAndDeleteClientHandleForTesting(handle); |
| 115 } |
| 116 // Delete the other half of the original allocations. |
| 117 for (int i = 2; i <= 1024; i += 2) { |
| 118 manager.FreeTexture(i); |
| 119 } |
| 120 // And delete the second set of allocations. |
| 121 for (int i = 1; i <= 512; ++i) { |
| 122 manager.FreeTexture(1024 + i); |
| 123 } |
| 124 manager.CheckPendingForTesting(&command_buffer); |
| 125 } |
| 126 |
| 127 TEST(ClientDiscardableManagerTest, MultipleAllocations) { |
| 128 FakeCommandBuffer command_buffer; |
| 129 ClientDiscardableManager manager; |
| 130 manager.SetElementCountForTesting(1024); |
| 131 for (int i = 1; i <= 1024; ++i) { |
| 132 ClientDiscardableHandle handle = |
| 133 manager.InitializeTexture(&command_buffer, i); |
| 134 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 135 EXPECT_EQ(handle.shm_id(), 1); |
| 136 UnlockAndDeleteClientHandleForTesting(handle); |
| 137 } |
| 138 // Allocate and free one entry multiple times, this should cause the |
| 139 // allocation and release of a new shm_id each time. |
| 140 for (int i = 1; i < 10; ++i) { |
| 141 { |
| 142 ClientDiscardableHandle handle = |
| 143 manager.InitializeTexture(&command_buffer, 1024 + i); |
| 144 EXPECT_TRUE(handle.IsLockedForTesting()); |
| 145 EXPECT_EQ(handle.shm_id(), i + 1); |
| 146 UnlockAndDeleteClientHandleForTesting(handle); |
| 147 } |
| 148 manager.FreeTexture(1024 + i); |
| 149 } |
| 150 // Delete every other entry. |
| 151 for (int i = 1; i <= 1024; ++i) { |
| 152 manager.FreeTexture(i); |
| 153 } |
| 154 manager.CheckPendingForTesting(&command_buffer); |
| 155 } |
| 156 |
| 157 } // namespace gpu |
OLD | NEW |