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/callback.h" | 10 #include "base/callback.h" |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
14 #include "base/synchronization/condition_variable.h" | 13 #include "base/synchronization/condition_variable.h" |
15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
16 #include "gpu/gpu_export.h" | 15 #include "gpu/gpu_export.h" |
17 | 16 |
18 namespace base { | |
19 class SequenceChecker; | |
20 } | |
21 | |
22 namespace gpu { | 17 namespace gpu { |
23 | 18 |
24 // This class manages the sync points, which allow cross-channel | 19 // This class manages the sync points, which allow cross-channel |
25 // synchronization. | 20 // synchronization. |
26 class GPU_EXPORT SyncPointManager | 21 class GPU_EXPORT SyncPointManager { |
27 : public base::RefCountedThreadSafe<SyncPointManager> { | |
28 public: | 22 public: |
29 // InProcessCommandBuffer allows threaded calls since it can call into | 23 explicit SyncPointManager(bool allow_threaded_wait); |
30 // SyncPointManager from client threads, or from multiple service threads | 24 ~SyncPointManager(); |
31 // used in Android WebView. | |
32 static SyncPointManager* Create(bool allow_threaded_calls_and_wait); | |
33 | 25 |
34 // Generates a sync point, returning its ID. This can me called on any thread. | 26 // Generates a sync point, returning its ID. This can me called on any thread. |
35 // IDs start at a random number. Never return 0. | 27 // IDs start at a random number. Never return 0. |
36 uint32 GenerateSyncPoint(); | 28 uint32 GenerateSyncPoint(); |
37 | 29 |
38 // Retires a sync point. This will call all the registered callbacks for this | 30 // Retires a sync point. This will call all the registered callbacks for this |
39 // sync point. This can only be called on the main thread. | 31 // sync point. This can only be called on the main thread. |
40 void RetireSyncPoint(uint32 sync_point); | 32 void RetireSyncPoint(uint32 sync_point); |
41 | 33 |
42 // Adds a callback to the sync point. The callback will be called when the | 34 // Adds a callback to the sync point. The callback will be called when the |
43 // sync point is retired, or immediately (from within that function) if the | 35 // sync point is retired, or immediately (from within that function) if the |
44 // sync point was already retired (or not created yet). This can only be | 36 // sync point was already retired (or not created yet). This can only be |
45 // called on the main thread. | 37 // called on the main thread. |
46 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); | 38 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); |
47 | 39 |
48 bool IsSyncPointRetired(uint32 sync_point); | 40 bool IsSyncPointRetired(uint32 sync_point); |
49 | 41 |
50 // Block and wait until a sync point is signaled. This is only useful when | 42 // Block and wait until a sync point is signaled. This is only useful when |
51 // the sync point is signaled on another thread. | 43 // the sync point is signaled on another thread. |
52 void ThreadedWaitSyncPoint(uint32 sync_point); | 44 void WaitSyncPoint(uint32 sync_point); |
53 | 45 |
54 private: | 46 private: |
55 friend class base::RefCountedThreadSafe<SyncPointManager>; | |
56 typedef std::vector<base::Closure> ClosureList; | 47 typedef std::vector<base::Closure> ClosureList; |
57 typedef base::hash_map<uint32, ClosureList> SyncPointMap; | 48 typedef base::hash_map<uint32, ClosureList> SyncPointMap; |
58 | 49 |
59 explicit SyncPointManager(bool allow_threaded_calls); | |
60 ~SyncPointManager(); | |
61 void CheckSequencedThread(); | |
62 | 50 |
63 const bool allow_threaded_calls_and_wait_; | 51 bool IsSyncPointRetiredLocked(uint32 sync_point); |
64 scoped_ptr<base::SequenceChecker> sequence_checker_; | 52 |
| 53 const bool allow_threaded_wait_; |
65 | 54 |
66 // Protects the 2 fields below. Note: callbacks shouldn't be called with this | 55 // Protects the 2 fields below. Note: callbacks shouldn't be called with this |
67 // held. | 56 // held. |
68 base::Lock lock_; | 57 base::Lock lock_; |
69 SyncPointMap sync_point_map_; | 58 SyncPointMap sync_point_map_; |
70 uint32 next_sync_point_; | 59 uint32 next_sync_point_; |
71 | |
72 // Used to handle condition variable based wait. | |
73 base::Lock retire_lock_; | |
74 base::ConditionVariable retire_cond_var_; | 60 base::ConditionVariable retire_cond_var_; |
75 | 61 |
76 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); | 62 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); |
77 }; | 63 }; |
78 | 64 |
79 } // namespace gpu | 65 } // namespace gpu |
80 | 66 |
81 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ | 67 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ |
OLD | NEW |