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

Side by Side Diff: gpu/command_buffer/service/sync_point_manager.h

Issue 2752393002: gpu: Add SequenceId for identifying sync point sequences. (Closed)
Patch Set: piman's review 3 Created 3 years, 9 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
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 #ifndef GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ 5 #ifndef GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ 6 #define GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <functional> 10 #include <functional>
11 #include <memory> 11 #include <memory>
12 #include <queue> 12 #include <queue>
13 #include <unordered_map> 13 #include <unordered_map>
14 #include <vector> 14 #include <vector>
15 15
16 #include "base/atomic_sequence_num.h" 16 #include "base/atomic_sequence_num.h"
17 #include "base/callback.h" 17 #include "base/callback.h"
18 #include "base/logging.h" 18 #include "base/logging.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
21 #include "base/synchronization/condition_variable.h" 21 #include "base/synchronization/condition_variable.h"
22 #include "base/synchronization/lock.h" 22 #include "base/synchronization/lock.h"
23 #include "base/threading/thread_checker.h" 23 #include "base/threading/thread_checker.h"
24 #include "gpu/command_buffer/common/command_buffer_id.h" 24 #include "gpu/command_buffer/common/command_buffer_id.h"
25 #include "gpu/command_buffer/common/constants.h" 25 #include "gpu/command_buffer/common/constants.h"
26 #include "gpu/command_buffer/common/sync_token.h" 26 #include "gpu/command_buffer/common/sync_token.h"
27 #include "gpu/command_buffer/service/sequence_id.h"
27 #include "gpu/gpu_export.h" 28 #include "gpu/gpu_export.h"
28 29
29 namespace base { 30 namespace base {
30 class SingleThreadTaskRunner; 31 class SingleThreadTaskRunner;
31 } // namespace base 32 } // namespace base
32 33
33 namespace gpu { 34 namespace gpu {
34 35
35 class SyncPointClient; 36 class SyncPointClient;
36 class SyncPointClientState; 37 class SyncPointClientState;
37 class SyncPointManager; 38 class SyncPointManager;
38 39
39 class GPU_EXPORT SyncPointOrderData 40 class GPU_EXPORT SyncPointOrderData
40 : public base::RefCountedThreadSafe<SyncPointOrderData> { 41 : public base::RefCountedThreadSafe<SyncPointOrderData> {
41 public: 42 public:
42 static scoped_refptr<SyncPointOrderData> Create();
43 void Destroy(); 43 void Destroy();
44 44
45 uint32_t GenerateUnprocessedOrderNumber(SyncPointManager* sync_point_manager); 45 SequenceId sequence_id() { return sequence_id_; }
46 void BeginProcessingOrderNumber(uint32_t order_num);
47 void PauseProcessingOrderNumber(uint32_t order_num);
48 void FinishProcessingOrderNumber(uint32_t order_num);
49 46
50 uint32_t processed_order_num() const { 47 uint32_t processed_order_num() const {
51 base::AutoLock auto_lock(lock_); 48 base::AutoLock auto_lock(lock_);
52 return processed_order_num_; 49 return processed_order_num_;
53 } 50 }
54 51
55 uint32_t unprocessed_order_num() const { 52 uint32_t unprocessed_order_num() const {
56 base::AutoLock auto_lock(lock_); 53 base::AutoLock auto_lock(lock_);
57 return unprocessed_order_num_; 54 return unprocessed_order_num_;
58 } 55 }
59 56
60 uint32_t current_order_num() const { 57 uint32_t current_order_num() const {
61 DCHECK(processing_thread_checker_.CalledOnValidThread()); 58 DCHECK(processing_thread_checker_.CalledOnValidThread());
62 return current_order_num_; 59 return current_order_num_;
63 } 60 }
64 61
65 bool IsProcessingOrderNumber() { 62 bool IsProcessingOrderNumber() {
66 DCHECK(processing_thread_checker_.CalledOnValidThread()); 63 DCHECK(processing_thread_checker_.CalledOnValidThread());
67 return !paused_ && current_order_num_ > processed_order_num(); 64 return !paused_ && current_order_num_ > processed_order_num();
68 } 65 }
69 66
70 bool ValidateReleaseOrderNumber( 67 uint32_t GenerateUnprocessedOrderNumber();
71 scoped_refptr<SyncPointClientState> client_state, 68 void BeginProcessingOrderNumber(uint32_t order_num);
72 uint32_t wait_order_num, 69 void PauseProcessingOrderNumber(uint32_t order_num);
73 uint64_t fence_release, 70 void FinishProcessingOrderNumber(uint32_t order_num);
74 const base::Closure& release_callback);
75 71
76 private: 72 private:
77 friend class base::RefCountedThreadSafe<SyncPointOrderData>; 73 friend class base::RefCountedThreadSafe<SyncPointOrderData>;
74 friend class SyncPointManager;
75 friend class SyncPointClientState;
78 76
79 struct OrderFence { 77 struct OrderFence {
80 uint32_t order_num; 78 uint32_t order_num;
81 uint64_t fence_release; 79 uint64_t fence_release;
82 base::Closure release_callback; 80 base::Closure release_callback;
83 scoped_refptr<SyncPointClientState> client_state; 81 scoped_refptr<SyncPointClientState> client_state;
84 82
85 OrderFence(uint32_t order, 83 OrderFence(uint32_t order,
86 uint64_t release, 84 uint64_t release,
87 const base::Closure& release_callback, 85 const base::Closure& release_callback,
88 scoped_refptr<SyncPointClientState> state); 86 scoped_refptr<SyncPointClientState> state);
89 OrderFence(const OrderFence& other); 87 OrderFence(const OrderFence& other);
90 ~OrderFence(); 88 ~OrderFence();
91 89
92 bool operator>(const OrderFence& rhs) const { 90 bool operator>(const OrderFence& rhs) const {
93 return std::tie(order_num, fence_release) > 91 return std::tie(order_num, fence_release) >
94 std::tie(rhs.order_num, rhs.fence_release); 92 std::tie(rhs.order_num, rhs.fence_release);
95 } 93 }
96 }; 94 };
97 typedef std::priority_queue<OrderFence, 95 typedef std::priority_queue<OrderFence,
98 std::vector<OrderFence>, 96 std::vector<OrderFence>,
99 std::greater<OrderFence>> 97 std::greater<OrderFence>>
100 OrderFenceQueue; 98 OrderFenceQueue;
101 99
102 SyncPointOrderData(); 100 SyncPointOrderData(SyncPointManager* sync_point_manager,
101 SequenceId seqeunce_id);
102
103 ~SyncPointOrderData(); 103 ~SyncPointOrderData();
104 104
105 bool ValidateReleaseOrderNumber(
106 scoped_refptr<SyncPointClientState> client_state,
107 uint32_t wait_order_num,
108 uint64_t fence_release,
109 const base::Closure& release_callback);
110
111 SyncPointManager* const sync_point_manager_;
112
113 const SequenceId sequence_id_;
114
105 // Non thread-safe functions need to be called from a single thread. 115 // Non thread-safe functions need to be called from a single thread.
106 base::ThreadChecker processing_thread_checker_; 116 base::ThreadChecker processing_thread_checker_;
107 117
108 // Current IPC order number being processed (only used on processing thread). 118 // Current IPC order number being processed (only used on processing thread).
109 uint32_t current_order_num_ = 0; 119 uint32_t current_order_num_ = 0;
110 120
111 // Whether or not the current order number is being processed or paused. 121 // Whether or not the current order number is being processed or paused.
112 bool paused_ = false; 122 bool paused_ = false;
113 123
114 // This lock protects destroyed_, processed_order_num_, 124 // This lock protects destroyed_, processed_order_num_,
(...skipping 15 matching lines...) Expand all
130 // which the wait command was issued. If the order number reaches the 140 // which the wait command was issued. If the order number reaches the
131 // wait command's, we should automatically release up to the expected 141 // wait command's, we should automatically release up to the expected
132 // release count. Note that this also releases other lower release counts, 142 // release count. Note that this also releases other lower release counts,
133 // so a single misbehaved fence sync is enough to invalidate/signal all 143 // so a single misbehaved fence sync is enough to invalidate/signal all
134 // previous fence syncs. 144 // previous fence syncs.
135 OrderFenceQueue order_fence_queue_; 145 OrderFenceQueue order_fence_queue_;
136 146
137 DISALLOW_COPY_AND_ASSIGN(SyncPointOrderData); 147 DISALLOW_COPY_AND_ASSIGN(SyncPointOrderData);
138 }; 148 };
139 149
140 // Internal state for sync point clients.
141 class GPU_EXPORT SyncPointClientState 150 class GPU_EXPORT SyncPointClientState
142 : public base::RefCountedThreadSafe<SyncPointClientState> { 151 : public base::RefCountedThreadSafe<SyncPointClientState> {
143 public: 152 public:
144 explicit SyncPointClientState(scoped_refptr<SyncPointOrderData> order_data); 153 void Destroy();
145 154
146 bool IsFenceSyncReleased(uint64_t release); 155 CommandBufferNamespace namespace_id() const { return namespace_id_; }
156 CommandBufferId command_buffer_id() const { return command_buffer_id_; }
157 SequenceId sequence_id() const { return order_data_->sequence_id(); }
147 158
148 // Queues the callback to be called if the release is valid. If the release 159 // This behaves similarly to SyncPointManager::Wait but uses the order data
149 // is invalid this function will return False and the callback will never 160 // to guarantee no deadlocks with other clients. Must be called on order
150 // be called. 161 // number processing thread.
151 bool WaitForRelease(uint64_t release, 162 bool Wait(const SyncToken& sync_token, const base::Closure& callback);
152 uint32_t wait_order_num,
153 const base::Closure& callback);
154 163
155 // Releases a fence sync and all fence syncs below. 164 // Like Wait but runs the callback on the given task runner's thread. Must be
165 // called on order number processing thread.
166 bool WaitNonThreadSafe(
167 const SyncToken& sync_token,
168 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
169 const base::Closure& callback);
170
171 // Release fence sync and run queued callbacks. Must be called on order number
172 // processing thread.
156 void ReleaseFenceSync(uint64_t release); 173 void ReleaseFenceSync(uint64_t release);
157 174
158 // Does not release the fence sync, but releases callbacks waiting on that
159 // fence sync.
160 void EnsureWaitReleased(uint64_t release, const base::Closure& callback);
161
162 private: 175 private:
163 friend class base::RefCountedThreadSafe<SyncPointClientState>; 176 friend class base::RefCountedThreadSafe<SyncPointClientState>;
177 friend class SyncPointManager;
178 friend class SyncPointOrderData;
164 179
165 struct ReleaseCallback { 180 struct ReleaseCallback {
166 uint64_t release_count; 181 uint64_t release_count;
167 base::Closure callback_closure; 182 base::Closure callback_closure;
168 183
169 ReleaseCallback(uint64_t release, const base::Closure& callback); 184 ReleaseCallback(uint64_t release, const base::Closure& callback);
170 ReleaseCallback(const ReleaseCallback& other); 185 ReleaseCallback(const ReleaseCallback& other);
171 ~ReleaseCallback(); 186 ~ReleaseCallback();
172 187
173 bool operator>(const ReleaseCallback& rhs) const { 188 bool operator>(const ReleaseCallback& rhs) const {
174 return release_count > rhs.release_count; 189 return release_count > rhs.release_count;
175 } 190 }
176 }; 191 };
177 typedef std::priority_queue<ReleaseCallback, 192 typedef std::priority_queue<ReleaseCallback,
178 std::vector<ReleaseCallback>, 193 std::vector<ReleaseCallback>,
179 std::greater<ReleaseCallback>> 194 std::greater<ReleaseCallback>>
180 ReleaseCallbackQueue; 195 ReleaseCallbackQueue;
181 196
197 SyncPointClientState(SyncPointManager* sync_point_manager,
198 scoped_refptr<SyncPointOrderData> order_data,
199 CommandBufferNamespace namespace_id,
200 CommandBufferId command_buffer_id);
201
182 ~SyncPointClientState(); 202 ~SyncPointClientState();
183 203
204 // Returns true if fence sync has been released.
205 bool IsFenceSyncReleased(uint64_t release);
206
207 // Queues the callback to be called if the release is valid. If the release
208 // is invalid this function will return False and the callback will never
209 // be called.
210 bool WaitForRelease(uint64_t release,
211 uint32_t wait_order_num,
212 const base::Closure& callback);
213
214 // Does not release the fence sync, but releases callbacks waiting on that
215 // fence sync.
216 void EnsureWaitReleased(uint64_t release, const base::Closure& callback);
217
218 void ReleaseFenceSyncHelper(uint64_t release);
219
220 // Sync point manager is guaranteed to exist in the lifetime of the client.
221 SyncPointManager* sync_point_manager_ = nullptr;
222
184 // Global order data where releases will originate from. 223 // Global order data where releases will originate from.
185 scoped_refptr<SyncPointOrderData> order_data_; 224 scoped_refptr<SyncPointOrderData> order_data_;
186 225
226 // Unique namespace/client id pair for this sync point client.
227 const CommandBufferNamespace namespace_id_;
228 const CommandBufferId command_buffer_id_;
229
187 // Protects fence_sync_release_, fence_callback_queue_. 230 // Protects fence_sync_release_, fence_callback_queue_.
188 base::Lock fence_sync_lock_; 231 base::Lock fence_sync_lock_;
189 232
190 // Current fence sync release that has been signaled. 233 // Current fence sync release that has been signaled.
191 uint64_t fence_sync_release_ = 0; 234 uint64_t fence_sync_release_ = 0;
192 235
193 // In well defined fence sync operations, fence syncs are released in order 236 // In well defined fence sync operations, fence syncs are released in order
194 // so simply having a priority queue for callbacks is enough. 237 // so simply having a priority queue for callbacks is enough.
195 ReleaseCallbackQueue release_callback_queue_; 238 ReleaseCallbackQueue release_callback_queue_;
196 239
197 DISALLOW_COPY_AND_ASSIGN(SyncPointClientState); 240 DISALLOW_COPY_AND_ASSIGN(SyncPointClientState);
198 }; 241 };
199 242
200 class GPU_EXPORT SyncPointClient {
201 public:
202 SyncPointClient(SyncPointManager* sync_point_manager,
203 scoped_refptr<SyncPointOrderData> order_data,
204 CommandBufferNamespace namespace_id,
205 CommandBufferId command_buffer_id);
206 ~SyncPointClient();
207
208 // This behaves similarly to SyncPointManager::Wait but uses the order data
209 // to guarantee no deadlocks with other clients.
210 bool Wait(const SyncToken& sync_token, const base::Closure& callback);
211
212 // Like Wait but runs the callback on the given task runner's thread.
213 bool WaitNonThreadSafe(
214 const SyncToken& sync_token,
215 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
216 const base::Closure& callback);
217
218 // Release fence sync and run queued callbacks.
219 void ReleaseFenceSync(uint64_t release);
220
221 private:
222 // Sync point manager is guaranteed to exist in the lifetime of the client.
223 SyncPointManager* const sync_point_manager_;
224
225 scoped_refptr<SyncPointOrderData> order_data_;
226
227 scoped_refptr<SyncPointClientState> client_state_;
228
229 // Unique namespace/client id pair for this sync point client.
230 const CommandBufferNamespace namespace_id_;
231 const CommandBufferId command_buffer_id_;
232
233 DISALLOW_COPY_AND_ASSIGN(SyncPointClient);
234 };
235
236 // This class manages the sync points, which allow cross-channel 243 // This class manages the sync points, which allow cross-channel
237 // synchronization. 244 // synchronization.
238 class GPU_EXPORT SyncPointManager { 245 class GPU_EXPORT SyncPointManager {
239 public: 246 public:
240 SyncPointManager(); 247 SyncPointManager();
241 ~SyncPointManager(); 248 ~SyncPointManager();
242 249
243 // Returns true if the sync token has been released or if the command buffer 250 scoped_refptr<SyncPointOrderData> CreateSyncPointOrderData();
244 // does not exist. 251
252 scoped_refptr<SyncPointClientState> CreateSyncPointClientState(
253 CommandBufferNamespace namespace_id,
254 CommandBufferId command_buffer_id,
255 SequenceId sequence_id);
256
257 // Returns true if the sync token has been released or if the command
258 // buffer does not exist.
245 bool IsSyncTokenReleased(const SyncToken& sync_token); 259 bool IsSyncTokenReleased(const SyncToken& sync_token);
246 260
261 // Returns the sequence ID that will release this sync token.
262 SequenceId GetSyncTokenReleaseSequenceId(const SyncToken& sync_token);
263
264 // Returns the global last processed order number.
265 uint32_t GetProcessedOrderNum() const;
266
267 // // Returns the global last unprocessed order number.
268 uint32_t GetUnprocessedOrderNum() const;
269
247 // If the wait is valid (sync token hasn't been processed or command buffer 270 // If the wait is valid (sync token hasn't been processed or command buffer
248 // does not exist), the callback is queued to run when the sync point is 271 // does not exist), the callback is queued to run when the sync point is
249 // released. If the wait is invalid, the callback is NOT run. The callback 272 // released. If the wait is invalid, the callback is NOT run. The callback
250 // runs on the thread the sync point is released. Clients should use 273 // runs on the thread the sync point is released. Clients should use
251 // SyncPointClient::Wait because that uses order data to prevent deadlocks. 274 // SyncPointClient::Wait because that uses order data to prevent deadlocks.
252 bool Wait(const SyncToken& sync_token, 275 bool Wait(const SyncToken& sync_token,
253 uint32_t wait_order_num, 276 uint32_t wait_order_num,
254 const base::Closure& callback); 277 const base::Closure& callback);
255 278
256 // Like Wait but runs the callback on the given task runner's thread. 279 // Like Wait but runs the callback on the given task runner's thread.
257 bool WaitNonThreadSafe( 280 bool WaitNonThreadSafe(
258 const SyncToken& sync_token, 281 const SyncToken& sync_token,
259 uint32_t wait_order_num, 282 uint32_t wait_order_num,
260 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 283 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
261 const base::Closure& callback); 284 const base::Closure& callback);
262 285
263 // WaitOutOfOrder allows waiting for a sync token indefinitely, so it 286 // WaitOutOfOrder allows waiting for a sync token indefinitely, so it
264 // should be used with trusted sync tokens only. 287 // should be used with trusted sync tokens only.
265 bool WaitOutOfOrder(const SyncToken& trusted_sync_token, 288 bool WaitOutOfOrder(const SyncToken& trusted_sync_token,
266 const base::Closure& callback); 289 const base::Closure& callback);
267 290
268 // Like WaitOutOfOrder but runs the callback on the given task runner's 291 // Like WaitOutOfOrder but runs the callback on the given task runner's
269 // thread. 292 // thread.
270 bool WaitOutOfOrderNonThreadSafe( 293 bool WaitOutOfOrderNonThreadSafe(
271 const SyncToken& trusted_sync_token, 294 const SyncToken& trusted_sync_token,
272 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 295 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
273 const base::Closure& callback); 296 const base::Closure& callback);
274 297
275 // Used by SyncPointClient.
276 void RegisterSyncPointClient(scoped_refptr<SyncPointClientState> client_state,
277 CommandBufferNamespace namespace_id,
278 CommandBufferId command_buffer_id);
279
280 void DeregisterSyncPointClient(CommandBufferNamespace namespace_id,
281 CommandBufferId command_buffer_id);
282
283 // Used by SyncPointOrderData. 298 // Used by SyncPointOrderData.
284 uint32_t GenerateOrderNumber(); 299 uint32_t GenerateOrderNumber();
285 300
301 void DestroyedSyncPointOrderData(SequenceId sequence_id);
302
303 void DestroyedSyncPointClientState(CommandBufferNamespace namespace_id,
304 CommandBufferId command_buffer_id);
305
286 private: 306 private:
287 using ClientStateMap = std::unordered_map<CommandBufferId, 307 using ClientStateMap = std::unordered_map<CommandBufferId,
288 scoped_refptr<SyncPointClientState>, 308 scoped_refptr<SyncPointClientState>,
289 CommandBufferId::Hasher>; 309 CommandBufferId::Hasher>;
290 310
311 using OrderDataMap = std::unordered_map<SequenceId,
312 scoped_refptr<SyncPointOrderData>,
313 SequenceId::Hasher>;
314
315 scoped_refptr<SyncPointOrderData> GetSyncPointOrderData(
316 SequenceId sequence_id);
317
291 scoped_refptr<SyncPointClientState> GetSyncPointClientState( 318 scoped_refptr<SyncPointClientState> GetSyncPointClientState(
292 CommandBufferNamespace namespace_id, 319 CommandBufferNamespace namespace_id,
293 CommandBufferId command_buffer_id); 320 CommandBufferId command_buffer_id);
294 321
295 // Order number is global for all clients. 322 // Order number is global for all clients.
296 base::AtomicSequenceNumber global_order_num_; 323 base::AtomicSequenceNumber order_num_generator_;
297 324
298 // Client map holds a map of clients id to client for each namespace. 325 // The following are protected by |lock_|.
299 base::Lock client_state_maps_lock_; 326 // Map of command buffer id to client state for each namespace.
300 ClientStateMap client_state_maps_[NUM_COMMAND_BUFFER_NAMESPACES]; 327 ClientStateMap client_state_maps_[NUM_COMMAND_BUFFER_NAMESPACES];
301 328
329 // Map of sequence id to order data.
330 OrderDataMap order_data_map_;
331
332 uint32_t next_sequence_id_ = 1;
333
334 mutable base::Lock lock_;
335
302 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); 336 DISALLOW_COPY_AND_ASSIGN(SyncPointManager);
303 }; 337 };
304 338
305 } // namespace gpu 339 } // namespace gpu
306 340
307 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ 341 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_
OLDNEW
« no previous file with comments | « gpu/command_buffer/service/sequence_id.h ('k') | gpu/command_buffer/service/sync_point_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698