• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/common/gpu/sync_point_manager.h"
6 
7 #include <climits>
8 
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 
12 namespace content {
13 
14 static const int kMaxSyncBase = INT_MAX;
15 
SyncPointManager()16 SyncPointManager::SyncPointManager()
17     : next_sync_point_(base::RandInt(1, kMaxSyncBase)) {
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
20   // point number. http://crbug.com/373452
21 }
22 
~SyncPointManager()23 SyncPointManager::~SyncPointManager() {
24 }
25 
GenerateSyncPoint()26 uint32 SyncPointManager::GenerateSyncPoint() {
27   base::AutoLock lock(lock_);
28   uint32 sync_point = next_sync_point_++;
29   // When an integer overflow occurs, don't return 0.
30   if (!sync_point)
31     sync_point = next_sync_point_++;
32 
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
35   // crash the GPU process than risk worse.
36   // For normal operation (at most a few per frame), it would take ~a year to
37   // wrap.
38   CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end());
39   sync_point_map_.insert(std::make_pair(sync_point, ClosureList()));
40   return sync_point;
41 }
42 
RetireSyncPoint(uint32 sync_point)43 void SyncPointManager::RetireSyncPoint(uint32 sync_point) {
44   DCHECK(thread_checker_.CalledOnValidThread());
45   ClosureList list;
46   {
47     base::AutoLock lock(lock_);
48     SyncPointMap::iterator it = sync_point_map_.find(sync_point);
49     DCHECK(it != sync_point_map_.end());
50     list.swap(it->second);
51     sync_point_map_.erase(it);
52   }
53   for (ClosureList::iterator i = list.begin(); i != list.end(); ++i)
54     i->Run();
55 }
56 
AddSyncPointCallback(uint32 sync_point,const base::Closure & callback)57 void SyncPointManager::AddSyncPointCallback(uint32 sync_point,
58                                             const base::Closure& callback) {
59   DCHECK(thread_checker_.CalledOnValidThread());
60   {
61     base::AutoLock lock(lock_);
62     SyncPointMap::iterator it = sync_point_map_.find(sync_point);
63     if (it != sync_point_map_.end()) {
64       it->second.push_back(callback);
65       return;
66     }
67   }
68   callback.Run();
69 }
70 
IsSyncPointRetired(uint32 sync_point)71 bool SyncPointManager::IsSyncPointRetired(uint32 sync_point) {
72   DCHECK(thread_checker_.CalledOnValidThread());
73   {
74     base::AutoLock lock(lock_);
75     SyncPointMap::iterator it = sync_point_map_.find(sync_point);
76     return it == sync_point_map_.end();
77   }
78 }
79 
80 }  // namespace content
81