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" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/synchronization/condition_variable.h" |
13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
14 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
15 #include "gpu/gpu_export.h" | 16 #include "gpu/gpu_export.h" |
16 | 17 |
17 namespace gpu { | 18 namespace gpu { |
18 | 19 |
19 // This class manages the sync points, which allow cross-channel | 20 // This class manages the sync points, which allow cross-channel |
20 // synchronization. | 21 // synchronization. |
21 class GPU_EXPORT SyncPointManager | 22 class GPU_EXPORT ThreadedSyncPointManager { |
22 : public base::RefCountedThreadSafe<SyncPointManager> { | |
23 public: | 23 public: |
24 SyncPointManager(); | 24 ThreadedSyncPointManager(); |
| 25 ~ThreadedSyncPointManager(); |
25 | 26 |
26 // Generates a sync point, returning its ID. This can me called on any thread. | 27 // Generates a sync point, returning its ID. This can me called on any thread. |
27 // IDs start at a random number. Never return 0. | 28 // IDs start at a random number. Never return 0. |
28 uint32 GenerateSyncPoint(); | 29 uint32 GenerateSyncPoint(); |
29 | 30 |
30 // Retires a sync point. This will call all the registered callbacks for this | 31 // 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. | 32 // sync point. This can only be called on the main thread. |
32 void RetireSyncPoint(uint32 sync_point); | 33 void RetireSyncPoint(uint32 sync_point); |
33 | 34 |
34 // Adds a callback to the sync point. The callback will be called when the | 35 // 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 | 36 // 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 | 37 // sync point was already retired (or not created yet). This can only be |
37 // called on the main thread. | 38 // called on the main thread. |
38 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); | 39 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); |
39 | 40 |
40 bool IsSyncPointRetired(uint32 sync_point); | 41 bool IsSyncPointRetired(uint32 sync_point); |
41 | 42 |
| 43 // Block and wait until this sync point is retired. |
| 44 void WaitSyncPoint(uint32 sync_point); |
| 45 |
| 46 private: |
| 47 typedef std::vector<base::Closure> ClosureList; |
| 48 typedef base::hash_map<uint32, ClosureList> SyncPointMap; |
| 49 |
| 50 bool IsSyncPointRetiredLocked(uint32 sync_point); |
| 51 |
| 52 // Note: callbacks shouldn't be called with this held. |
| 53 base::Lock lock_; |
| 54 SyncPointMap sync_point_map_; |
| 55 uint32 next_sync_point_; |
| 56 base::ConditionVariable cond_var_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(ThreadedSyncPointManager); |
| 59 }; |
| 60 |
| 61 // Does not support retiring and waiting on sync points on different thread. |
| 62 // All methods except GenerateSyncPoint must be called on the same thread. |
| 63 class GPU_EXPORT SyncPointManager |
| 64 : public base::RefCountedThreadSafe<SyncPointManager> { |
| 65 public: |
| 66 SyncPointManager(); |
| 67 |
| 68 uint32 GenerateSyncPoint(); |
| 69 void RetireSyncPoint(uint32 sync_point); |
| 70 void AddSyncPointCallback(uint32 sync_point, const base::Closure& callback); |
| 71 bool IsSyncPointRetired(uint32 sync_point); |
| 72 |
42 private: | 73 private: |
43 friend class base::RefCountedThreadSafe<SyncPointManager>; | 74 friend class base::RefCountedThreadSafe<SyncPointManager>; |
44 typedef std::vector<base::Closure> ClosureList; | |
45 typedef base::hash_map<uint32, ClosureList> SyncPointMap; | |
46 | |
47 ~SyncPointManager(); | 75 ~SyncPointManager(); |
48 | 76 |
49 base::ThreadChecker thread_checker_; | 77 base::ThreadChecker thread_checker_; |
50 | 78 ThreadedSyncPointManager threaded_sync_point_manager_; |
51 // Protects the 2 fields below. Note: callbacks shouldn't be called with this | |
52 // held. | |
53 base::Lock lock_; | |
54 SyncPointMap sync_point_map_; | |
55 uint32 next_sync_point_; | |
56 | |
57 DISALLOW_COPY_AND_ASSIGN(SyncPointManager); | |
58 }; | 79 }; |
59 | 80 |
60 } // namespace gpu | 81 } // namespace gpu |
61 | 82 |
62 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ | 83 #endif // GPU_COMMAND_BUFFER_SERVICE_SYNC_POINT_MANAGER_H_ |
OLD | NEW |