Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / gpu / command_buffer / service / transfer_buffer_manager.cc
blobe9c83e01d003d02db4c4edaeaf72eea90a653e6d
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/transfer_buffer_manager.h"
7 #include <limits>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/process/process_handle.h"
12 #include "base/trace_event/trace_event.h"
13 #include "gpu/command_buffer/common/cmd_buffer_common.h"
14 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
16 using ::base::SharedMemory;
18 namespace gpu {
20 TransferBufferManagerInterface::~TransferBufferManagerInterface() {
23 TransferBufferManager::TransferBufferManager()
24 : shared_memory_bytes_allocated_(0) {
27 TransferBufferManager::~TransferBufferManager() {
28 while (!registered_buffers_.empty()) {
29 BufferMap::iterator it = registered_buffers_.begin();
30 DCHECK(shared_memory_bytes_allocated_ >= it->second->size());
31 shared_memory_bytes_allocated_ -= it->second->size();
32 registered_buffers_.erase(it);
34 DCHECK(!shared_memory_bytes_allocated_);
37 bool TransferBufferManager::Initialize() {
38 return true;
41 bool TransferBufferManager::RegisterTransferBuffer(
42 int32 id,
43 scoped_ptr<BufferBacking> buffer_backing) {
44 if (id <= 0) {
45 DVLOG(0) << "Cannot register transfer buffer with non-positive ID.";
46 return false;
49 // Fail if the ID is in use.
50 if (registered_buffers_.find(id) != registered_buffers_.end()) {
51 DVLOG(0) << "Buffer ID already in use.";
52 return false;
55 // Register the shared memory with the ID.
56 scoped_refptr<Buffer> buffer(new gpu::Buffer(buffer_backing.Pass()));
58 // Check buffer alignment is sane.
59 DCHECK(!(reinterpret_cast<uintptr_t>(buffer->memory()) &
60 (kCommandBufferEntrySize - 1)));
62 shared_memory_bytes_allocated_ += buffer->size();
63 TRACE_COUNTER_ID1(
64 "gpu", "GpuTransferBufferMemory", this, shared_memory_bytes_allocated_);
66 registered_buffers_[id] = buffer;
68 return true;
71 void TransferBufferManager::DestroyTransferBuffer(int32 id) {
72 BufferMap::iterator it = registered_buffers_.find(id);
73 if (it == registered_buffers_.end()) {
74 DVLOG(0) << "Transfer buffer ID was not registered.";
75 return;
78 DCHECK(shared_memory_bytes_allocated_ >= it->second->size());
79 shared_memory_bytes_allocated_ -= it->second->size();
80 TRACE_COUNTER_ID1(
81 "gpu", "GpuTransferBufferMemory", this, shared_memory_bytes_allocated_);
83 registered_buffers_.erase(it);
86 scoped_refptr<Buffer> TransferBufferManager::GetTransferBuffer(int32 id) {
87 if (id == 0)
88 return NULL;
90 BufferMap::iterator it = registered_buffers_.find(id);
91 if (it == registered_buffers_.end())
92 return NULL;
94 return it->second;
97 } // namespace gpu