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

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

Issue 2814583002: Service/ClientDiscardableManager (Closed)
Patch Set: Feedback Created 3 years, 7 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
7 #include "base/containers/flat_set.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/sys_info.h"
10
11 namespace gpu {
12 namespace {
13
14 // Stores a set of offsets, initially 0 to |element_count_|. Allows callers to
15 // take and return offsets from the set. Internally stores the offsets as a set
16 // of ranges. This means that in the worst case (every other offset taken), the
17 // set will use |element_count_| uints, but should typically use fewer.
18 class FreeOffsetSet {
19 public:
20 // Creates a new set, containing 0 to |element_count|.
21 explicit FreeOffsetSet(uint32_t element_count);
22
23 // Returns true if the set contains at least one element.
24 bool HasFreeOffset() const;
25
26 // Returns true if any element from the set has been taken.
27 bool HasUsedOffset() const;
28
29 // Takes a free offset from the set. Should only be called if HasFreeOffset().
30 uint32_t TakeFreeOffset();
31
32 // Returns an offset to the set.
33 void ReturnFreeOffset(uint32_t offset);
34
35 private:
36 struct FreeRange {
37 uint32_t start;
38 uint32_t end;
39 };
40 struct CompareFreeRanges {
41 bool operator()(const FreeRange& a, const FreeRange& b) const {
42 return a.start < b.start;
43 }
44 };
45
46 const uint32_t element_count_;
47 base::flat_set<FreeRange, CompareFreeRanges> free_ranges_;
48
49 DISALLOW_COPY_AND_ASSIGN(FreeOffsetSet);
50 };
51
52 FreeOffsetSet::FreeOffsetSet(uint32_t element_count)
53 : element_count_(element_count) {
54 free_ranges_.insert({0, element_count_});
55 }
56
57 bool FreeOffsetSet::HasFreeOffset() const {
58 return !free_ranges_.empty();
59 }
60
61 bool FreeOffsetSet::HasUsedOffset() const {
62 if (free_ranges_.size() != 1 || free_ranges_.begin()->start != 0 ||
63 free_ranges_.begin()->end != element_count_)
64 return true;
65
66 return false;
67 }
68
69 uint32_t FreeOffsetSet::TakeFreeOffset() {
70 DCHECK(HasFreeOffset());
71
72 auto it = free_ranges_.begin();
73 uint32_t offset_to_return = it->start;
74
75 FreeRange new_range{it->start + 1, it->end};
76 free_ranges_.erase(it);
77 if (new_range.start != new_range.end)
78 free_ranges_.insert(new_range);
79
80 return offset_to_return;
81 }
82
83 void FreeOffsetSet::ReturnFreeOffset(uint32_t offset) {
84 FreeRange new_range{offset, offset + 1};
85
86 // Find the FreeRange directly before/after our new range.
87 auto next_range = free_ranges_.lower_bound(new_range);
88 auto prev_range = free_ranges_.end();
89 if (next_range != free_ranges_.begin()) {
90 prev_range = std::prev(next_range);
91 }
92
93 // Collapse ranges if possible.
94 if (next_range != free_ranges_.end() && next_range->start == new_range.end) {
95 new_range.end = next_range->end;
96 free_ranges_.erase(next_range);
97 }
98
99 if (prev_range != free_ranges_.end() && prev_range->end == new_range.start) {
100 new_range.start = prev_range->start;
101 free_ranges_.erase(prev_range);
102 }
103
104 free_ranges_.insert(new_range);
105 }
106
107 // Returns the size of the allocation which ClientDiscardableManager will
108 // sub-allocate from. This should be at least as big as the minimum shared
109 // memory allocation size.
110 size_t AllocationSize() {
111 size_t allocation_size = base::SysInfo::VMAllocationGranularity();
112 // If the allocation is small (less than 2K), round it up to at least 4K.
113 allocation_size = std::max(static_cast<size_t>(2048), allocation_size);
114 return allocation_size;
115 }
116
117 } // namespace
118
119 struct ClientDiscardableManager::Allocation {
120 Allocation(uint32_t element_count) : free_offsets(element_count) {}
121
122 scoped_refptr<Buffer> buffer;
123 int32_t shm_id = 0;
124 FreeOffsetSet free_offsets;
125 };
126
127 ClientDiscardableManager::ClientDiscardableManager()
128 : allocation_size_(AllocationSize()) {}
129 ClientDiscardableManager::~ClientDiscardableManager() = default;
130
131 ClientDiscardableHandle ClientDiscardableManager::InitializeTexture(
132 CommandBuffer* command_buffer,
133 uint32_t texture_id) {
134 DCHECK(texture_handles_.find(texture_id) == texture_handles_.end());
135
136 scoped_refptr<Buffer> buffer;
137 uint32_t offset = 0;
138 int32_t shm_id = 0;
139 FindAllocation(command_buffer, &buffer, &shm_id, &offset);
140 uint32_t byte_offset = offset * element_size_;
141 ClientDiscardableHandle handle(std::move(buffer), byte_offset, shm_id);
142 texture_handles_.emplace(texture_id, handle);
143 return handle;
144 }
145
146 bool ClientDiscardableManager::LockTexture(uint32_t texture_id) {
147 auto found = texture_handles_.find(texture_id);
148 DCHECK(found != texture_handles_.end());
149 return found->second.Lock();
150 }
151
152 void ClientDiscardableManager::FreeTexture(uint32_t texture_id) {
153 auto found = texture_handles_.find(texture_id);
154 if (found == texture_handles_.end())
155 return;
156 pending_handles_.push(found->second);
157 texture_handles_.erase(found);
158 }
159
160 bool ClientDiscardableManager::TextureIsValid(uint32_t texture_id) const {
161 return texture_handles_.find(texture_id) != texture_handles_.end();
162 }
163
164 void ClientDiscardableManager::FindAllocation(CommandBuffer* command_buffer,
165 scoped_refptr<Buffer>* buffer,
166 int32_t* shm_id,
167 uint32_t* offset) {
168 CheckPending(command_buffer);
169
170 for (auto& allocation : allocations_) {
171 if (!allocation->free_offsets.HasFreeOffset())
172 continue;
173
174 *offset = allocation->free_offsets.TakeFreeOffset();
175 *shm_id = allocation->shm_id;
176 *buffer = allocation->buffer;
177 return;
178 }
179
180 // We couldn't find an existing free entry. Allocate more space.
181 auto allocation = base::MakeUnique<Allocation>(elements_per_allocation_);
182 allocation->buffer = command_buffer->CreateTransferBuffer(
183 allocation_size_, &allocation->shm_id);
184
185 *offset = allocation->free_offsets.TakeFreeOffset();
186 *shm_id = allocation->shm_id;
187 *buffer = allocation->buffer;
188 allocations_.push_back(std::move(allocation));
189 }
190
191 void ClientDiscardableManager::ReturnAllocation(
192 CommandBuffer* command_buffer,
193 const ClientDiscardableHandle& handle) {
194 for (auto it = allocations_.begin(); it != allocations_.end(); ++it) {
195 Allocation* allocation = it->get();
196 if (allocation->shm_id != handle.shm_id())
197 continue;
198
199 allocation->free_offsets.ReturnFreeOffset(handle.byte_offset() /
200 element_size_);
201
202 if (!allocation->free_offsets.HasUsedOffset()) {
203 command_buffer->DestroyTransferBuffer(allocation->shm_id);
204 allocations_.erase(it);
205 return;
206 }
207 }
208 }
209
210 void ClientDiscardableManager::CheckPending(CommandBuffer* command_buffer) {
211 while (pending_handles_.size() > 0 &&
212 pending_handles_.front().CanBeReUsed()) {
213 ReturnAllocation(command_buffer, pending_handles_.front());
214 pending_handles_.pop();
215 }
216 }
217
218 ClientDiscardableHandle ClientDiscardableManager::GetHandleForTesting(
219 uint32_t texture_id) {
220 auto found = texture_handles_.find(texture_id);
221 DCHECK(found != texture_handles_.end());
222 return found->second;
223 }
224
225 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698