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