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 #include "gpu/command_buffer/service/sync_point_manager.h" | 5 #include "gpu/command_buffer/service/sync_point_manager.h" |
6 | 6 |
7 #include <climits> | 7 #include <climits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
11 #include "base/sequence_checker.h" | 11 #include "base/sequence_checker.h" |
12 | 12 |
13 namespace gpu { | 13 namespace gpu { |
14 | 14 |
15 static const int kMaxSyncBase = INT_MAX; | 15 static const int kMaxSyncBase = INT_MAX; |
16 | 16 |
17 // static | 17 SyncPointManager::SyncPointManager(bool allow_threaded_wait) |
18 SyncPointManager* SyncPointManager::Create(bool allow_threaded_calls_and_wait) { | 18 : allow_threaded_wait_(allow_threaded_wait), |
19 return new SyncPointManager(allow_threaded_calls_and_wait); | |
20 } | |
21 | |
22 SyncPointManager::SyncPointManager(bool allow_threaded_calls_and_wait) | |
23 : allow_threaded_calls_and_wait_(allow_threaded_calls_and_wait), | |
24 // To reduce the risk that a sync point created in a previous GPU process | 19 // To reduce the risk that a sync point created in a previous GPU process |
25 // will be in flight in the next GPU process, randomize the starting sync | 20 // will be in flight in the next GPU process, randomize the starting sync |
26 // point number. http://crbug.com/373452 | 21 // point number. http://crbug.com/373452 |
27 next_sync_point_(base::RandInt(1, kMaxSyncBase)), | 22 next_sync_point_(base::RandInt(1, kMaxSyncBase)), |
28 retire_cond_var_(&retire_lock_) { | 23 retire_cond_var_(&lock_) {} |
29 if (!allow_threaded_calls_and_wait_) { | |
30 sequence_checker_.reset(new base::SequenceChecker); | |
31 } | |
32 } | |
33 | 24 |
34 SyncPointManager::~SyncPointManager() { | 25 SyncPointManager::~SyncPointManager() {} |
35 } | |
36 | 26 |
37 uint32 SyncPointManager::GenerateSyncPoint() { | 27 uint32 SyncPointManager::GenerateSyncPoint() { |
38 base::AutoLock lock(lock_); | 28 base::AutoLock lock(lock_); |
39 uint32 sync_point = next_sync_point_++; | 29 uint32 sync_point = next_sync_point_++; |
40 // When an integer overflow occurs, don't return 0. | 30 // When an integer overflow occurs, don't return 0. |
41 if (!sync_point) | 31 if (!sync_point) |
42 sync_point = next_sync_point_++; | 32 sync_point = next_sync_point_++; |
43 | 33 |
44 // Note: wrapping would take days for a buggy/compromized renderer that would | 34 // Note: wrapping would take days for a buggy/compromized renderer that would |
45 // insert sync points in a loop, but if that were to happen, better explicitly | 35 // insert sync points in a loop, but if that were to happen, better explicitly |
46 // crash the GPU process than risk worse. | 36 // crash the GPU process than risk worse. |
47 // For normal operation (at most a few per frame), it would take ~a year to | 37 // For normal operation (at most a few per frame), it would take ~a year to |
48 // wrap. | 38 // wrap. |
49 CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end()); | 39 CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end()); |
50 sync_point_map_.insert(std::make_pair(sync_point, ClosureList())); | 40 sync_point_map_.insert(std::make_pair(sync_point, ClosureList())); |
51 return sync_point; | 41 return sync_point; |
52 } | 42 } |
53 | 43 |
54 void SyncPointManager::RetireSyncPoint(uint32 sync_point) { | 44 void SyncPointManager::RetireSyncPoint(uint32 sync_point) { |
55 CheckSequencedThread(); | |
56 ClosureList list; | 45 ClosureList list; |
57 { | 46 { |
58 base::AutoLock lock(lock_); | 47 base::AutoLock lock(lock_); |
59 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | 48 SyncPointMap::iterator it = sync_point_map_.find(sync_point); |
60 if (it == sync_point_map_.end()) { | 49 if (it == sync_point_map_.end()) { |
61 LOG(ERROR) << "Attempted to retire sync point that" | 50 LOG(ERROR) << "Attempted to retire sync point that" |
62 " didn't exist or was already retired."; | 51 " didn't exist or was already retired."; |
63 return; | 52 return; |
64 } | 53 } |
65 list.swap(it->second); | 54 list.swap(it->second); |
66 sync_point_map_.erase(it); | 55 sync_point_map_.erase(it); |
67 } | 56 if (allow_threaded_wait_) |
68 if (allow_threaded_calls_and_wait_) { | 57 retire_cond_var_.Broadcast(); |
69 base::AutoLock lock(retire_lock_); | |
70 retire_cond_var_.Broadcast(); | |
71 } | 58 } |
72 for (ClosureList::iterator i = list.begin(); i != list.end(); ++i) | 59 for (ClosureList::iterator i = list.begin(); i != list.end(); ++i) |
73 i->Run(); | 60 i->Run(); |
74 } | 61 } |
75 | 62 |
76 void SyncPointManager::AddSyncPointCallback(uint32 sync_point, | 63 void SyncPointManager::AddSyncPointCallback(uint32 sync_point, |
77 const base::Closure& callback) { | 64 const base::Closure& callback) { |
78 CheckSequencedThread(); | |
79 { | 65 { |
80 base::AutoLock lock(lock_); | 66 base::AutoLock lock(lock_); |
81 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | 67 SyncPointMap::iterator it = sync_point_map_.find(sync_point); |
82 if (it != sync_point_map_.end()) { | 68 if (it != sync_point_map_.end()) { |
83 it->second.push_back(callback); | 69 it->second.push_back(callback); |
84 return; | 70 return; |
85 } | 71 } |
86 } | 72 } |
87 callback.Run(); | 73 callback.Run(); |
88 } | 74 } |
89 | 75 |
90 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point) { | 76 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point) { |
91 CheckSequencedThread(); | 77 base::AutoLock lock(lock_); |
92 { | 78 return IsSyncPointRetiredLocked(sync_point); |
93 base::AutoLock lock(lock_); | |
94 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | |
95 return it == sync_point_map_.end(); | |
96 } | |
97 } | 79 } |
98 | 80 |
99 void SyncPointManager::ThreadedWaitSyncPoint(uint32 sync_point) { | 81 void SyncPointManager::WaitSyncPoint(uint32 sync_point) { |
100 DCHECK(allow_threaded_calls_and_wait_); | 82 if (!allow_threaded_wait_) { |
101 base::AutoLock lock(retire_lock_); | 83 DCHECK(IsSyncPointRetired(sync_point)); |
102 while (!IsSyncPointRetired(sync_point)) { | 84 return; |
| 85 } |
| 86 |
| 87 base::AutoLock lock(lock_); |
| 88 while (!IsSyncPointRetiredLocked(sync_point)) { |
103 retire_cond_var_.Wait(); | 89 retire_cond_var_.Wait(); |
104 } | 90 } |
105 } | 91 } |
106 | 92 |
107 void SyncPointManager::CheckSequencedThread() { | 93 bool SyncPointManager::IsSyncPointRetiredLocked(uint32 sync_point) { |
108 DCHECK(allow_threaded_calls_and_wait_ || | 94 lock_.AssertAcquired(); |
109 sequence_checker_->CalledOnValidSequencedThread()); | 95 return sync_point_map_.find(sync_point) == sync_point_map_.end(); |
110 } | 96 } |
111 | 97 |
112 } // namespace gpu | 98 } // namespace gpu |
OLD | NEW |