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