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
3 // found in the LICENSE file.
5 #include "gpu/command_buffer/service/sync_point_manager.h"
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/sequence_checker.h"
15 static const int kMaxSyncBase
= INT_MAX
;
18 SyncPointManager
* SyncPointManager::Create(bool allow_threaded_calls
) {
19 return new SyncPointManager(allow_threaded_calls
);
22 SyncPointManager::SyncPointManager(bool allow_threaded_calls
)
23 : next_sync_point_(base::RandInt(1, kMaxSyncBase
)) {
24 // 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
26 // point number. http://crbug.com/373452
28 if (!allow_threaded_calls
) {
29 sequence_checker_
.reset(new base::SequenceChecker
);
33 SyncPointManager::~SyncPointManager() {
36 uint32
SyncPointManager::GenerateSyncPoint() {
37 base::AutoLock
lock(lock_
);
38 uint32 sync_point
= next_sync_point_
++;
39 // When an integer overflow occurs, don't return 0.
41 sync_point
= next_sync_point_
++;
43 // Note: wrapping would take days for a buggy/compromized renderer that would
44 // insert sync points in a loop, but if that were to happen, better explicitly
45 // crash the GPU process than risk worse.
46 // For normal operation (at most a few per frame), it would take ~a year to
48 CHECK(sync_point_map_
.find(sync_point
) == sync_point_map_
.end());
49 sync_point_map_
.insert(std::make_pair(sync_point
, ClosureList()));
53 void SyncPointManager::RetireSyncPoint(uint32 sync_point
) {
54 CheckSequencedThread();
57 base::AutoLock
lock(lock_
);
58 SyncPointMap::iterator it
= sync_point_map_
.find(sync_point
);
59 if (it
== sync_point_map_
.end()) {
60 LOG(ERROR
) << "Attempted to retire sync point that"
61 " didn't exist or was already retired.";
64 list
.swap(it
->second
);
65 sync_point_map_
.erase(it
);
67 for (ClosureList::iterator i
= list
.begin(); i
!= list
.end(); ++i
)
71 void SyncPointManager::AddSyncPointCallback(uint32 sync_point
,
72 const base::Closure
& callback
) {
73 CheckSequencedThread();
75 base::AutoLock
lock(lock_
);
76 SyncPointMap::iterator it
= sync_point_map_
.find(sync_point
);
77 if (it
!= sync_point_map_
.end()) {
78 it
->second
.push_back(callback
);
85 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point
) {
86 CheckSequencedThread();
88 base::AutoLock
lock(lock_
);
89 SyncPointMap::iterator it
= sync_point_map_
.find(sync_point
);
90 return it
== sync_point_map_
.end();
94 void SyncPointManager::CheckSequencedThread() {
95 DCHECK(!sequence_checker_
||
96 sequence_checker_
->CalledOnValidSequencedThread());