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" |
15 #include "gpu/gpu_export.h" | 18 #include "gpu/gpu_export.h" |
16 | 19 |
17 namespace gpu { | 20 namespace gpu { |
18 | 21 |
22 class SyncPointManager; | |
23 | |
24 enum SyncPointNamespace { | |
25 kSyncPointNamespace_GpuIO, | |
26 kSyncPointNamespace_InProcess, | |
27 | |
28 NUM_SYNC_POINT_NAMESPACES | |
29 }; | |
30 | |
31 class GPU_EXPORT SyncPointClient | |
32 : public base::RefCountedThreadSafe<SyncPointClient> { | |
33 public: | |
34 void BeginProcessingOrderNumber(uint32_t order_num) { | |
35 DCHECK_GE(current_order_num_, order_num); | |
36 current_order_num_ = order_num; | |
37 } | |
38 | |
39 void FinishProcessingOrderNumber(uint32_t order_num) { | |
40 DCHECK_EQ(current_order_num_, order_num); | |
41 DCHECK_GT(processed_order_num(), order_num); | |
42 base::subtle::Release_Store(&processed_order_num_, order_num); | |
43 } | |
44 | |
45 uint32_t processed_order_num() const { | |
46 return base::subtle::Acquire_Load(&processed_order_num_); | |
47 } | |
48 | |
49 private: | |
50 friend class base::RefCountedThreadSafe<SyncPointClient>; | |
51 friend class SyncPointManager; | |
52 | |
53 SyncPointClient(SyncPointManager* sync_point_manager, | |
54 SyncPointNamespace namespace_id, uint64_t client_id); | |
55 | |
56 protected: | |
57 virtual ~SyncPointClient(); | |
58 | |
59 // Sync Point Manager is guaranteed to outlive the sync point client. | |
piman
2015/09/14 23:43:53
This seems hard to guarantee if SyncPointClient is
David Yen
2015/09/15 18:09:31
Hmm, later functions probably do need a pointer to
piman
2015/09/15 23:47:15
One way to enforce proper behavior is to split the
David Yen
2015/09/18 18:43:59
Done. Since SyncPointClient is no longer ref count
| |
60 SyncPointManager* sync_point_manager_; | |
61 | |
62 // Unique namespace/client id pair for this sync point client. | |
63 SyncPointNamespace namespace_id_; | |
64 uint64_t client_id_; | |
65 | |
66 // Current IPC order number being processed (only used on processing thread). | |
67 uint32_t current_order_num_; | |
68 | |
69 // Last finished IPC order number. | |
70 base::subtle::Atomic32 processed_order_num_; | |
71 | |
72 DISALLOW_COPY_AND_ASSIGN(SyncPointClient); | |
73 }; | |
74 | |
19 // This class manages the sync points, which allow cross-channel | 75 // This class manages the sync points, which allow cross-channel |
20 // synchronization. | 76 // synchronization. |
21 class GPU_EXPORT SyncPointManager { | 77 class GPU_EXPORT SyncPointManager { |
22 public: | 78 public: |
23 explicit SyncPointManager(bool allow_threaded_wait); | 79 explicit SyncPointManager(bool allow_threaded_wait); |
24 ~SyncPointManager(); | 80 ~SyncPointManager(); |
25 | 81 |
82 // Creates a sync point client which message processors should hold. | |
83 scoped_refptr<SyncPointClient> CreateSyncPointClient( | |
84 SyncPointNamespace namespace_id, uint64_t client_id); | |
85 | |
86 // Finds an already created sync point client. | |
87 scoped_refptr<SyncPointClient> GetSyncPointClient( | |
88 SyncPointNamespace namespace_id, uint64_t client_id); | |
89 | |
90 // Generate a global order number, messages need to be ordered by flush order. | |
91 uint32_t GenerateOrderNumber(); | |
92 | |
26 // Generates a sync point, returning its ID. This can me called on any thread. | 93 // Generates a sync point, returning its ID. This can me called on any thread. |
27 // IDs start at a random number. Never return 0. | 94 // IDs start at a random number. Never return 0. |
28 uint32 GenerateSyncPoint(); | 95 uint32 GenerateSyncPoint(); |
29 | 96 |
30 // Retires a sync point. This will call all the registered callbacks for this | 97 // 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. | 98 // sync point. This can only be called on the main thread. |
32 void RetireSyncPoint(uint32 sync_point); | 99 void RetireSyncPoint(uint32 sync_point); |
33 | 100 |
34 // Adds a callback to the sync point. The callback will be called when the | 101 // 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 | 102 // 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 | 103 // sync point was already retired (or not created yet). This can only be |
37 // called on the main thread. | 104 // called on the main thread. |
38 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); | 105 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); |
39 | 106 |
40 bool IsSyncPointRetired(uint32 sync_point); | 107 bool IsSyncPointRetired(uint32 sync_point); |
41 | 108 |
42 // Block and wait until a sync point is signaled. This is only useful when | 109 // Block and wait until a sync point is signaled. This is only useful when |
43 // the sync point is signaled on another thread. | 110 // the sync point is signaled on another thread. |
44 void WaitSyncPoint(uint32 sync_point); | 111 void WaitSyncPoint(uint32 sync_point); |
45 | 112 |
46 private: | 113 private: |
114 friend class SyncPointClient; | |
115 | |
47 typedef std::vector<base::Closure> ClosureList; | 116 typedef std::vector<base::Closure> ClosureList; |
48 typedef base::hash_map<uint32, ClosureList> SyncPointMap; | 117 typedef base::hash_map<uint32, ClosureList> SyncPointMap; |
49 | 118 typedef base::hash_map<uint64_t, SyncPointClient*> ClientMap; |
50 | 119 |
51 bool IsSyncPointRetiredLocked(uint32 sync_point); | 120 bool IsSyncPointRetiredLocked(uint32 sync_point); |
121 void RemoveSyncPointClient(SyncPointNamespace namespace_id, | |
122 uint64_t client_id); | |
52 | 123 |
53 const bool allow_threaded_wait_; | 124 const bool allow_threaded_wait_; |
54 | 125 |
126 // Order number is global for all clients. | |
127 base::AtomicSequenceNumber global_order_num_; | |
128 | |
129 // Client map holds a map of clients id to client for each namespace. | |
130 base::Lock client_maps_lock_[NUM_SYNC_POINT_NAMESPACES]; | |
piman
2015/09/14 23:43:53
nit: it seems slightly overkill to have separate l
David Yen
2015/09/15 18:09:31
Done.
| |
131 ClientMap client_maps_[NUM_SYNC_POINT_NAMESPACES]; | |
132 | |
55 // Protects the 2 fields below. Note: callbacks shouldn't be called with this | 133 // Protects the 2 fields below. Note: callbacks shouldn't be called with this |
56 // held. | 134 // held. |
57 base::Lock lock_; | 135 base::Lock lock_; |
58 SyncPointMap sync_point_map_; | 136 SyncPointMap sync_point_map_; |
59 uint32 next_sync_point_; | 137 uint32 next_sync_point_; |
60 base::ConditionVariable retire_cond_var_; | 138 base::ConditionVariable retire_cond_var_; |
61 | 139 |
62 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); | 140 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); |
63 }; | 141 }; |
64 | 142 |
65 } // namespace gpu | 143 } // namespace gpu |
66 | 144 |
67 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ | 145 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ |
OLD | NEW |