Home | History | Annotate | Download | only in gpu
      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 "base/logging.h"
      8 
      9 namespace content {
     10 
     11 SyncPointManager::SyncPointManager()
     12     : next_sync_point_(1) {
     13 }
     14 
     15 SyncPointManager::~SyncPointManager() {
     16 }
     17 
     18 uint32 SyncPointManager::GenerateSyncPoint() {
     19   base::AutoLock lock(lock_);
     20   uint32 sync_point = next_sync_point_++;
     21 
     22   // Note: wrapping would take days for a buggy/compromized renderer that would
     23   // insert sync points in a loop, but if that were to happen, better explicitly
     24   // crash the GPU process than risk worse.
     25   // For normal operation (at most a few per frame), it would take ~a year to
     26   // wrap.
     27   CHECK(sync_point_map_.find(sync_point) == sync_point_map_.end());
     28   sync_point_map_.insert(std::make_pair(sync_point, ClosureList()));
     29   return sync_point;
     30 }
     31 
     32 void SyncPointManager::RetireSyncPoint(uint32 sync_point) {
     33   DCHECK(thread_checker_.CalledOnValidThread());
     34   ClosureList list;
     35   {
     36     base::AutoLock lock(lock_);
     37     SyncPointMap::iterator it = sync_point_map_.find(sync_point);
     38     DCHECK(it != sync_point_map_.end());
     39     list.swap(it->second);
     40     sync_point_map_.erase(it);
     41   }
     42   for (ClosureList::iterator i = list.begin(); i != list.end(); ++i)
     43     i->Run();
     44 }
     45 
     46 void SyncPointManager::AddSyncPointCallback(uint32 sync_point,
     47                                             const base::Closure& callback) {
     48   DCHECK(thread_checker_.CalledOnValidThread());
     49   {
     50     base::AutoLock lock(lock_);
     51     SyncPointMap::iterator it = sync_point_map_.find(sync_point);
     52     if (it != sync_point_map_.end()) {
     53       it->second.push_back(callback);
     54       return;
     55     }
     56   }
     57   callback.Run();
     58 }
     59 
     60 }  // namespace content
     61