OLD | NEW |
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 <vector> | 8 #include <vector> |
9 | 9 |
| 10 #include "base/atomic_sequence_num.h" |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
13 #include "base/synchronization/condition_variable.h" | 16 #include "base/synchronization/condition_variable.h" |
14 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "base/threading/thread_checker.h" |
| 19 #include "gpu/command_buffer/common/constants.h" |
15 #include "gpu/gpu_export.h" | 20 #include "gpu/gpu_export.h" |
16 | 21 |
17 namespace gpu { | 22 namespace gpu { |
18 | 23 |
| 24 class SyncPointClient; |
| 25 class SyncPointManager; |
| 26 |
| 27 class GPU_EXPORT SyncPointClientState |
| 28 : public base::RefCountedThreadSafe<SyncPointClientState> { |
| 29 public: |
| 30 static scoped_refptr<SyncPointClientState> Create(); |
| 31 uint32_t GenerateUnprocessedOrderNumber(SyncPointManager* sync_point_manager); |
| 32 |
| 33 void BeginProcessingOrderNumber(uint32_t order_num) { |
| 34 DCHECK(processing_thread_checker_.CalledOnValidThread()); |
| 35 DCHECK_GE(order_num, current_order_num_); |
| 36 current_order_num_ = order_num; |
| 37 } |
| 38 |
| 39 void FinishProcessingOrderNumber(uint32_t order_num) { |
| 40 DCHECK(processing_thread_checker_.CalledOnValidThread()); |
| 41 DCHECK_EQ(current_order_num_, order_num); |
| 42 DCHECK_GT(order_num, processed_order_num()); |
| 43 base::subtle::Release_Store(&processed_order_num_, order_num); |
| 44 } |
| 45 |
| 46 uint32_t processed_order_num() const { |
| 47 return base::subtle::Acquire_Load(&processed_order_num_); |
| 48 } |
| 49 |
| 50 uint32_t unprocessed_order_num() const { |
| 51 return base::subtle::Acquire_Load(&unprocessed_order_num_); |
| 52 } |
| 53 |
| 54 uint32_t current_order_num() const { |
| 55 DCHECK(processing_thread_checker_.CalledOnValidThread()); |
| 56 return current_order_num_; |
| 57 } |
| 58 |
| 59 protected: |
| 60 friend class base::RefCountedThreadSafe<SyncPointClientState>; |
| 61 friend class SyncPointClient; |
| 62 |
| 63 SyncPointClientState(); |
| 64 virtual ~SyncPointClientState(); |
| 65 |
| 66 // Last finished IPC order number. |
| 67 base::subtle::Atomic32 processed_order_num_; |
| 68 |
| 69 // Unprocessed order number expected to be processed under normal execution. |
| 70 base::subtle::Atomic32 unprocessed_order_num_; |
| 71 |
| 72 // Non thread-safe functions need to be called from a single thread. |
| 73 base::ThreadChecker processing_thread_checker_; |
| 74 |
| 75 // Current IPC order number being processed (only used on processing thread). |
| 76 uint32_t current_order_num_; |
| 77 |
| 78 DISALLOW_COPY_AND_ASSIGN(SyncPointClientState); |
| 79 }; |
| 80 |
| 81 class GPU_EXPORT SyncPointClient { |
| 82 public: |
| 83 ~SyncPointClient(); |
| 84 |
| 85 scoped_refptr<SyncPointClientState> client_state() { return client_state_; } |
| 86 |
| 87 private: |
| 88 friend class SyncPointManager; |
| 89 |
| 90 SyncPointClient(SyncPointManager* sync_point_manager, |
| 91 scoped_refptr<SyncPointClientState> state, |
| 92 CommandBufferNamespace namespace_id, uint64_t client_id); |
| 93 |
| 94 // Sync point manager is guaranteed to exist in the lifetime of the client. |
| 95 SyncPointManager* sync_point_manager_; |
| 96 |
| 97 // Keep the state that is sharable across multiple threads. |
| 98 scoped_refptr<SyncPointClientState> client_state_; |
| 99 |
| 100 // Unique namespace/client id pair for this sync point client. |
| 101 CommandBufferNamespace namespace_id_; |
| 102 uint64_t client_id_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(SyncPointClient); |
| 105 }; |
| 106 |
19 // This class manages the sync points, which allow cross-channel | 107 // This class manages the sync points, which allow cross-channel |
20 // synchronization. | 108 // synchronization. |
21 class GPU_EXPORT SyncPointManager { | 109 class GPU_EXPORT SyncPointManager { |
22 public: | 110 public: |
23 explicit SyncPointManager(bool allow_threaded_wait); | 111 explicit SyncPointManager(bool allow_threaded_wait); |
24 ~SyncPointManager(); | 112 ~SyncPointManager(); |
25 | 113 |
| 114 // Creates/Destroy a sync point client which message processors should hold. |
| 115 scoped_ptr<SyncPointClient> CreateSyncPointClient( |
| 116 scoped_refptr<SyncPointClientState> client_state, |
| 117 CommandBufferNamespace namespace_id, uint64_t client_id); |
| 118 |
| 119 // Finds the state of an already created sync point client. |
| 120 scoped_refptr<SyncPointClientState> GetSyncPointClientState( |
| 121 CommandBufferNamespace namespace_id, uint64_t client_id); |
| 122 |
26 // Generates a sync point, returning its ID. This can me called on any thread. | 123 // Generates a sync point, returning its ID. This can me called on any thread. |
27 // IDs start at a random number. Never return 0. | 124 // IDs start at a random number. Never return 0. |
28 uint32 GenerateSyncPoint(); | 125 uint32 GenerateSyncPoint(); |
29 | 126 |
30 // Retires a sync point. This will call all the registered callbacks for this | 127 // Retires a sync point. This will call all the registered callbacks for this |
31 // sync point. This can only be called on the main thread. | 128 // sync point. This can only be called on the main thread. |
32 void RetireSyncPoint(uint32 sync_point); | 129 void RetireSyncPoint(uint32 sync_point); |
33 | 130 |
34 // Adds a callback to the sync point. The callback will be called when the | 131 // Adds a callback to the sync point. The callback will be called when the |
35 // sync point is retired, or immediately (from within that function) if the | 132 // sync point is retired, or immediately (from within that function) if the |
36 // sync point was already retired (or not created yet). This can only be | 133 // sync point was already retired (or not created yet). This can only be |
37 // called on the main thread. | 134 // called on the main thread. |
38 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); | 135 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); |
39 | 136 |
40 bool IsSyncPointRetired(uint32 sync_point); | 137 bool IsSyncPointRetired(uint32 sync_point); |
41 | 138 |
42 // Block and wait until a sync point is signaled. This is only useful when | 139 // Block and wait until a sync point is signaled. This is only useful when |
43 // the sync point is signaled on another thread. | 140 // the sync point is signaled on another thread. |
44 void WaitSyncPoint(uint32 sync_point); | 141 void WaitSyncPoint(uint32 sync_point); |
45 | 142 |
46 private: | 143 private: |
| 144 friend class SyncPointClient; |
| 145 friend class SyncPointClientState; |
| 146 |
47 typedef std::vector<base::Closure> ClosureList; | 147 typedef std::vector<base::Closure> ClosureList; |
48 typedef base::hash_map<uint32, ClosureList> SyncPointMap; | 148 typedef base::hash_map<uint32, ClosureList> SyncPointMap; |
49 | 149 typedef base::hash_map<uint64_t, SyncPointClient*> ClientMap; |
50 | 150 |
51 bool IsSyncPointRetiredLocked(uint32 sync_point); | 151 bool IsSyncPointRetiredLocked(uint32 sync_point); |
| 152 uint32_t GenerateOrderNumber(); |
| 153 void DestroySyncPointClient(CommandBufferNamespace namespace_id, |
| 154 uint64_t client_id); |
52 | 155 |
53 const bool allow_threaded_wait_; | 156 const bool allow_threaded_wait_; |
54 | 157 |
| 158 // Order number is global for all clients. |
| 159 base::AtomicSequenceNumber global_order_num_; |
| 160 |
| 161 // Client map holds a map of clients id to client for each namespace. |
| 162 base::Lock client_maps_lock_; |
| 163 ClientMap client_maps_[NUM_COMMAND_BUFFER_NAMESPACES]; |
| 164 |
55 // Protects the 2 fields below. Note: callbacks shouldn't be called with this | 165 // Protects the 2 fields below. Note: callbacks shouldn't be called with this |
56 // held. | 166 // held. |
57 base::Lock lock_; | 167 base::Lock lock_; |
58 SyncPointMap sync_point_map_; | 168 SyncPointMap sync_point_map_; |
59 uint32 next_sync_point_; | 169 uint32 next_sync_point_; |
60 base::ConditionVariable retire_cond_var_; | 170 base::ConditionVariable retire_cond_var_; |
61 | 171 |
62 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); | 172 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); |
63 }; | 173 }; |
64 | 174 |
65 } // namespace gpu | 175 } // namespace gpu |
66 | 176 |
67 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ | 177 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ |
OLD | NEW |