Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(446)

Side by Side Diff: gpu/command_buffer/client/client_discardable_manager_unittest.cc

Issue 2751633002: ClientDiscardableManager Implementation
Patch Set: AllocationSize cleanup Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 UnlockAndDeleteClientHandleForTesting(
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 EXPECT_TRUE(service_handle.Delete());
58 }
59
60 } // namespace
61
62 TEST(ClientDiscardableManagerTest, BasicUsage) {
63 FakeCommandBuffer command_buffer;
64 ClientDiscardableManager manager;
65 {
66 ClientDiscardableHandle handle = manager.GetForTexture(&command_buffer, 1);
67 EXPECT_TRUE(handle.IsLockedForTesting());
68 EXPECT_EQ(handle.shm_id(), 1);
69 UnlockAndDeleteClientHandleForTesting(handle);
70 }
71 manager.FreeForTexture(1);
72 manager.CheckPendingForTesting(&command_buffer);
73 }
74
75 TEST(ClientDiscardableManagerTest, Reuse) {
76 FakeCommandBuffer command_buffer;
77 ClientDiscardableManager manager;
78 manager.SetElementCountForTesting(1024);
79 for (int i = 1; i <= 1024; ++i) {
80 ClientDiscardableHandle handle = manager.GetForTexture(&command_buffer, i);
81 EXPECT_TRUE(handle.IsLockedForTesting());
82 EXPECT_EQ(handle.shm_id(), 1);
83 UnlockAndDeleteClientHandleForTesting(handle);
84 }
85 // Delete every other entry.
86 for (int i = 1; i <= 1024; i += 2) {
87 manager.FreeForTexture(i);
88 }
89 // Allocate 512 more entries, ensure we re-use the original buffer.
90 for (int i = 1; i <= 512; ++i) {
91 ClientDiscardableHandle handle =
92 manager.GetForTexture(&command_buffer, 1024 + i);
93 EXPECT_TRUE(handle.IsLockedForTesting());
94 EXPECT_EQ(handle.shm_id(), 1);
95 UnlockAndDeleteClientHandleForTesting(handle);
96 }
97 // Delete the other half of the original allocations.
98 for (int i = 2; i <= 1024; i += 2) {
99 manager.FreeForTexture(i);
100 }
101 // And delete the second set of allocations.
102 for (int i = 1; i <= 512; ++i) {
103 manager.FreeForTexture(1024 + i);
104 }
105 manager.CheckPendingForTesting(&command_buffer);
106 }
107
108 TEST(ClientDiscardableManagerTest, MultipleAllocations) {
109 FakeCommandBuffer command_buffer;
110 ClientDiscardableManager manager;
111 manager.SetElementCountForTesting(1024);
112 for (int i = 1; i <= 1024; ++i) {
113 ClientDiscardableHandle handle = manager.GetForTexture(&command_buffer, i);
114 EXPECT_TRUE(handle.IsLockedForTesting());
115 EXPECT_EQ(handle.shm_id(), 1);
116 UnlockAndDeleteClientHandleForTesting(handle);
117 }
118 // Allocate and free one entry multiple times, this should cause the
119 // allocation and release of a new shm_id each time.
120 for (int i = 1; i < 10; ++i) {
121 {
122 ClientDiscardableHandle handle =
123 manager.GetForTexture(&command_buffer, 1024 + i);
124 EXPECT_TRUE(handle.IsLockedForTesting());
125 EXPECT_EQ(handle.shm_id(), i + 1);
126 UnlockAndDeleteClientHandleForTesting(handle);
127 }
128 manager.FreeForTexture(1024 + i);
129 }
130 // Delete every other entry.
131 for (int i = 1; i <= 1024; ++i) {
132 manager.FreeForTexture(i);
133 }
134 manager.CheckPendingForTesting(&command_buffer);
135 }
136
137 } // namespace gpu
OLDNEW
« no previous file with comments | « gpu/command_buffer/client/client_discardable_manager.cc ('k') | gpu/command_buffer/common/discardable_handle.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698