Chromium Code Reviews| 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 | 11 |
| 12 namespace gpu { | 12 namespace gpu { |
| 13 | 13 |
| 14 static const int kMaxSyncBase = INT_MAX; | 14 static const int kMaxSyncBase = INT_MAX; |
| 15 | 15 |
| 16 SyncPointManager::SyncPointManager() | 16 ThreadedSyncPointManager::ThreadedSyncPointManager() |
| 17 : next_sync_point_(base::RandInt(1, kMaxSyncBase)) { | 17 : next_sync_point_(base::RandInt(1, kMaxSyncBase)), cond_var_(&lock_) { |
| 18 // To reduce the risk that a sync point created in a previous GPU process | 18 // To reduce the risk that a sync point created in a previous GPU process |
| 19 // will be in flight in the next GPU process, randomize the starting sync | 19 // will be in flight in the next GPU process, randomize the starting sync |
| 20 // point number. http://crbug.com/373452 | 20 // point number. http://crbug.com/373452 |
| 21 } | 21 } |
| 22 | 22 |
| 23 SyncPointManager::~SyncPointManager() { | 23 ThreadedSyncPointManager::~ThreadedSyncPointManager() { |
| 24 } | 24 } |
| 25 | 25 |
| 26 uint32 SyncPointManager::GenerateSyncPoint() { | 26 uint32 ThreadedSyncPointManager::GenerateSyncPoint() { |
| 27 base::AutoLock lock(lock_); | 27 base::AutoLock lock(lock_); |
| 28 uint32 sync_point = next_sync_point_++; | 28 uint32 sync_point = next_sync_point_++; |
| 29 // When an integer overflow occurs, don't return 0. | 29 // When an integer overflow occurs, don't return 0. |
| 30 if (!sync_point) | 30 if (!sync_point) |
| 31 sync_point = next_sync_point_++; | 31 sync_point = next_sync_point_++; |
| 32 | 32 |
| 33 // Note: wrapping would take days for a buggy/compromized renderer that would | 33 // Note: wrapping would take days for a buggy/compromized renderer that would |
| 34 // insert sync points in a loop, but if that were to happen, better explicitly | 34 // insert sync points in a loop, but if that were to happen, better explicitly |
| 35 // crash the GPU process than risk worse. | 35 // crash the GPU process than risk worse. |
| 36 // For normal operation (at most a few per frame), it would take ~a year to | 36 // For normal operation (at most a few per frame), it would take ~a year to |
| 37 // wrap. | 37 // wrap. |
| 38 CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end()); | 38 CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end()); |
| 39 sync_point_map_.insert(std::make_pair(sync_point, ClosureList())); | 39 sync_point_map_.insert(std::make_pair(sync_point, ClosureList())); |
| 40 return sync_point; | 40 return sync_point; |
| 41 } | 41 } |
| 42 | 42 |
| 43 void SyncPointManager::RetireSyncPoint(uint32 sync_point) { | 43 void ThreadedSyncPointManager::RetireSyncPoint(uint32 sync_point) { |
| 44 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 45 ClosureList list; | 44 ClosureList list; |
| 45 base::AutoLock lock(lock_); | |
| 46 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | |
| 47 if (it == sync_point_map_.end()) { | |
| 48 LOG(ERROR) << "Attempted to retire sync point that" | |
| 49 " didn't exist or was already retired."; | |
| 50 return; | |
| 51 } | |
| 52 list.swap(it->second); | |
| 53 sync_point_map_.erase(it); | |
| 54 | |
| 55 { | |
| 56 base::AutoUnlock unlock(lock_); | |
| 57 for (ClosureList::iterator i = list.begin(); i != list.end(); ++i) | |
| 58 i->Run(); | |
| 59 } | |
| 60 | |
| 61 cond_var_.Broadcast(); | |
|
no sievers
2015/01/27 21:26:39
Can we somehow avoid dealing with the cond. var/lo
no sievers
2015/01/27 21:26:39
Also, it'd be slightly better to call Broadcast()
| |
| 62 } | |
| 63 | |
| 64 void ThreadedSyncPointManager::AddSyncPointCallback(uint32 sync_point, | |
| 65 const base::Closure& callback) { | |
| 46 { | 66 { |
| 47 base::AutoLock lock(lock_); | 67 base::AutoLock lock(lock_); |
| 48 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | 68 SyncPointMap::iterator it = sync_point_map_.find(sync_point); |
| 49 if (it == sync_point_map_.end()) { | |
| 50 LOG(ERROR) << "Attempted to retire sync point that" | |
| 51 " didn't exist or was already retired."; | |
| 52 return; | |
| 53 } | |
| 54 list.swap(it->second); | |
| 55 sync_point_map_.erase(it); | |
| 56 } | |
| 57 for (ClosureList::iterator i = list.begin(); i != list.end(); ++i) | |
| 58 i->Run(); | |
| 59 } | |
| 60 | |
| 61 void SyncPointManager::AddSyncPointCallback(uint32 sync_point, | |
| 62 const base::Closure& callback) { | |
| 63 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 64 { | |
| 65 base::AutoLock lock(lock_); | |
| 66 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | |
| 67 if (it != sync_point_map_.end()) { | 69 if (it != sync_point_map_.end()) { |
| 68 it->second.push_back(callback); | 70 it->second.push_back(callback); |
| 69 return; | 71 return; |
| 70 } | 72 } |
| 71 } | 73 } |
| 72 callback.Run(); | 74 callback.Run(); |
| 73 } | 75 } |
| 74 | 76 |
| 77 bool ThreadedSyncPointManager::IsSyncPointRetired(uint32 sync_point) { | |
| 78 { | |
| 79 base::AutoLock lock(lock_); | |
| 80 return IsSyncPointRetiredLocked(sync_point); | |
| 81 } | |
| 82 } | |
| 83 | |
| 84 void ThreadedSyncPointManager::WaitSyncPoint(uint32 sync_point) { | |
| 85 base::AutoLock lock(lock_); | |
| 86 while (!IsSyncPointRetiredLocked(sync_point)) { | |
| 87 cond_var_.Wait(); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 bool ThreadedSyncPointManager::IsSyncPointRetiredLocked(uint32 sync_point) { | |
| 92 lock_.AssertAcquired(); | |
| 93 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | |
| 94 return it == sync_point_map_.end(); | |
| 95 } | |
| 96 | |
| 97 SyncPointManager::SyncPointManager() { | |
| 98 } | |
| 99 | |
| 100 SyncPointManager::~SyncPointManager() { | |
| 101 } | |
| 102 | |
| 103 uint32 SyncPointManager::GenerateSyncPoint() { | |
| 104 return threaded_sync_point_manager_.GenerateSyncPoint(); | |
| 105 } | |
| 106 | |
| 107 void SyncPointManager::RetireSyncPoint(uint32 sync_point) { | |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 109 threaded_sync_point_manager_.RetireSyncPoint(sync_point); | |
|
no sievers
2015/01/27 21:26:39
Since this only adds three DCHECKs(), can you eith
| |
| 110 } | |
| 111 | |
| 112 void SyncPointManager::AddSyncPointCallback(uint32 sync_point, | |
| 113 const base::Closure& callback) { | |
| 114 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 115 threaded_sync_point_manager_.AddSyncPointCallback(sync_point, callback); | |
| 116 } | |
| 117 | |
| 75 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point) { | 118 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point) { |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | 119 DCHECK(thread_checker_.CalledOnValidThread()); |
| 77 { | 120 return threaded_sync_point_manager_.IsSyncPointRetired(sync_point); |
| 78 base::AutoLock lock(lock_); | |
| 79 SyncPointMap::iterator it = sync_point_map_.find(sync_point); | |
| 80 return it == sync_point_map_.end(); | |
| 81 } | |
| 82 } | 121 } |
| 83 | 122 |
| 84 } // namespace gpu | 123 } // namespace gpu |
| OLD | NEW |