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