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

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: fix failing tests 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();
piman 2017/03/17 19:12:37 nit: can we mention that the public API of this cl
sunnyps 2017/03/17 21:48:23 Done.
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.
150 // be called. 161 bool Wait(const SyncToken& sync_token, const base::Closure& callback);
151 bool WaitForRelease(uint64_t release,
152 uint32_t wait_order_num,
153 const base::Closure& callback);
154 162
155 // Releases a fence sync and all fence syncs below. 163 // Like Wait but runs the callback on the given task runner's thread.
164 bool WaitNonThreadSafe(
165 const SyncToken& sync_token,
166 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
167 const base::Closure& callback);
168
169 // Release fence sync and run queued callbacks.
156 void ReleaseFenceSync(uint64_t release); 170 void ReleaseFenceSync(uint64_t release);
157 171
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: 172 private:
163 friend class base::RefCountedThreadSafe<SyncPointClientState>; 173 friend class base::RefCountedThreadSafe<SyncPointClientState>;
174 friend class SyncPointManager;
175 friend class SyncPointOrderData;
164 176
165 struct ReleaseCallback { 177 struct ReleaseCallback {
166 uint64_t release_count; 178 uint64_t release_count;
167 base::Closure callback_closure; 179 base::Closure callback_closure;
168 180
169 ReleaseCallback(uint64_t release, const base::Closure& callback); 181 ReleaseCallback(uint64_t release, const base::Closure& callback);
170 ReleaseCallback(const ReleaseCallback& other); 182 ReleaseCallback(const ReleaseCallback& other);
171 ~ReleaseCallback(); 183 ~ReleaseCallback();
172 184
173 bool operator>(const ReleaseCallback& rhs) const { 185 bool operator>(const ReleaseCallback& rhs) const {
174 return release_count > rhs.release_count; 186 return release_count > rhs.release_count;
175 } 187 }
176 }; 188 };
177 typedef std::priority_queue<ReleaseCallback, 189 typedef std::priority_queue<ReleaseCallback,
178 std::vector<ReleaseCallback>, 190 std::vector<ReleaseCallback>,
179 std::greater<ReleaseCallback>> 191 std::greater<ReleaseCallback>>
180 ReleaseCallbackQueue; 192 ReleaseCallbackQueue;
181 193
194 SyncPointClientState(SyncPointManager* sync_point_manager,
195 scoped_refptr<SyncPointOrderData> order_data,
196 CommandBufferNamespace namespace_id,
197 CommandBufferId command_buffer_id);
198
182 ~SyncPointClientState(); 199 ~SyncPointClientState();
183 200
201 // Returns true if fence sync has been released.
202 bool IsFenceSyncReleased(uint64_t release);
203
204 // Queues the callback to be called if the release is valid. If the release
205 // is invalid this function will return False and the callback will never
206 // be called.
207 bool WaitForRelease(uint64_t release,
208 uint32_t wait_order_num,
209 const base::Closure& callback);
210
211 // Does not release the fence sync, but releases callbacks waiting on that
212 // fence sync.
213 void EnsureWaitReleased(uint64_t release, const base::Closure& callback);
214
215 void ReleaseFenceSyncHelper(uint64_t release);
216
217 // Sync point manager is guaranteed to exist in the lifetime of the client.
218 SyncPointManager* const sync_point_manager_;
219
184 // Global order data where releases will originate from. 220 // Global order data where releases will originate from.
185 scoped_refptr<SyncPointOrderData> order_data_; 221 scoped_refptr<SyncPointOrderData> order_data_;
186 222
223 // Unique namespace/client id pair for this sync point client.
224 const CommandBufferNamespace namespace_id_;
225 const CommandBufferId command_buffer_id_;
226
187 // Protects fence_sync_release_, fence_callback_queue_. 227 // Protects fence_sync_release_, fence_callback_queue_.
188 base::Lock fence_sync_lock_; 228 base::Lock fence_sync_lock_;
189 229
190 // Current fence sync release that has been signaled. 230 // Current fence sync release that has been signaled.
191 uint64_t fence_sync_release_ = 0; 231 uint64_t fence_sync_release_ = 0;
192 232
193 // In well defined fence sync operations, fence syncs are released in order 233 // In well defined fence sync operations, fence syncs are released in order
194 // so simply having a priority queue for callbacks is enough. 234 // so simply having a priority queue for callbacks is enough.
195 ReleaseCallbackQueue release_callback_queue_; 235 ReleaseCallbackQueue release_callback_queue_;
196 236
197 DISALLOW_COPY_AND_ASSIGN(SyncPointClientState); 237 DISALLOW_COPY_AND_ASSIGN(SyncPointClientState);
198 }; 238 };
199 239
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 240 // This class manages the sync points, which allow cross-channel
237 // synchronization. 241 // synchronization.
238 class GPU_EXPORT SyncPointManager { 242 class GPU_EXPORT SyncPointManager {
239 public: 243 public:
240 SyncPointManager(); 244 SyncPointManager();
241 ~SyncPointManager(); 245 ~SyncPointManager();
242 246
243 // Returns true if the sync token has been released or if the command buffer 247 scoped_refptr<SyncPointOrderData> CreateSyncPointOrderData();
244 // does not exist. 248
249 scoped_refptr<SyncPointClientState> CreateSyncPointClientState(
250 CommandBufferNamespace namespace_id,
251 CommandBufferId command_buffer_id,
252 SequenceId sequence_id);
253
254 // Returns true if the sync token has been released or if the command
255 // buffer does not exist.
245 bool IsSyncTokenReleased(const SyncToken& sync_token); 256 bool IsSyncTokenReleased(const SyncToken& sync_token);
246 257
258 // Returns the sequence ID that will release this sync token.
259 SequenceId GetSyncTokenReleaseSequenceId(const SyncToken& sync_token);
260
261 // Returns the global last processed order number.
262 uint32_t GetProcessedOrderNum() const;
263
264 // // Returns the global last unprocessed order number.
265 uint32_t GetUnprocessedOrderNum() const;
266
247 // If the wait is valid (sync token hasn't been processed or command buffer 267 // 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 268 // 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 269 // 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 270 // runs on the thread the sync point is released. Clients should use
251 // SyncPointClient::Wait because that uses order data to prevent deadlocks. 271 // SyncPointClient::Wait because that uses order data to prevent deadlocks.
252 bool Wait(const SyncToken& sync_token, 272 bool Wait(const SyncToken& sync_token,
253 uint32_t wait_order_num, 273 uint32_t wait_order_num,
254 const base::Closure& callback); 274 const base::Closure& callback);
255 275
256 // Like Wait but runs the callback on the given task runner's thread. 276 // Like Wait but runs the callback on the given task runner's thread.
257 bool WaitNonThreadSafe( 277 bool WaitNonThreadSafe(
258 const SyncToken& sync_token, 278 const SyncToken& sync_token,
259 uint32_t wait_order_num, 279 uint32_t wait_order_num,
260 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 280 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
261 const base::Closure& callback); 281 const base::Closure& callback);
262 282
263 // WaitOutOfOrder allows waiting for a sync token indefinitely, so it 283 // WaitOutOfOrder allows waiting for a sync token indefinitely, so it
264 // should be used with trusted sync tokens only. 284 // should be used with trusted sync tokens only.
265 bool WaitOutOfOrder(const SyncToken& trusted_sync_token, 285 bool WaitOutOfOrder(const SyncToken& trusted_sync_token,
266 const base::Closure& callback); 286 const base::Closure& callback);
267 287
268 // Like WaitOutOfOrder but runs the callback on the given task runner's 288 // Like WaitOutOfOrder but runs the callback on the given task runner's
269 // thread. 289 // thread.
270 bool WaitOutOfOrderNonThreadSafe( 290 bool WaitOutOfOrderNonThreadSafe(
271 const SyncToken& trusted_sync_token, 291 const SyncToken& trusted_sync_token,
272 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 292 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
273 const base::Closure& callback); 293 const base::Closure& callback);
274 294
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. 295 // Used by SyncPointOrderData.
284 uint32_t GenerateOrderNumber(); 296 uint32_t GenerateOrderNumber();
285 297
298 void DestroyedSyncPointOrderData(SequenceId sequence_id);
299
300 void DestroyedSyncPointClientState(CommandBufferNamespace namespace_id,
301 CommandBufferId command_buffer_id);
302
286 private: 303 private:
287 using ClientStateMap = std::unordered_map<CommandBufferId, 304 using ClientStateMap = std::unordered_map<CommandBufferId,
288 scoped_refptr<SyncPointClientState>, 305 scoped_refptr<SyncPointClientState>,
289 CommandBufferId::Hasher>; 306 CommandBufferId::Hasher>;
290 307
308 using OrderDataMap = std::unordered_map<SequenceId,
309 scoped_refptr<SyncPointOrderData>,
310 SequenceId::Hasher>;
311
312 scoped_refptr<SyncPointOrderData> GetSyncPointOrderData(
313 SequenceId sequence_id);
314
291 scoped_refptr<SyncPointClientState> GetSyncPointClientState( 315 scoped_refptr<SyncPointClientState> GetSyncPointClientState(
292 CommandBufferNamespace namespace_id, 316 CommandBufferNamespace namespace_id,
293 CommandBufferId command_buffer_id); 317 CommandBufferId command_buffer_id);
294 318
295 // Order number is global for all clients. 319 // Order number is global for all clients.
296 base::AtomicSequenceNumber global_order_num_; 320 base::AtomicSequenceNumber order_num_generator_;
297 321
298 // Client map holds a map of clients id to client for each namespace. 322 // The following are protected by |lock_|.
299 base::Lock client_state_maps_lock_; 323 // Map of command buffer id to client state for each namespace.
300 ClientStateMap client_state_maps_[NUM_COMMAND_BUFFER_NAMESPACES]; 324 ClientStateMap client_state_maps_[NUM_COMMAND_BUFFER_NAMESPACES];
301 325
326 // Map of sequence id to order data.
327 OrderDataMap order_data_map_;
328
329 uint32_t next_sequence_id_ = 1;
330
331 mutable base::Lock lock_;
332
302 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); 333 DISALLOW_COPY_AND_ASSIGN(SyncPointManager);
303 }; 334 };
304 335
305 } // namespace gpu 336 } // namespace gpu
306 337
307 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ 338 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698