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"
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
;
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() {
41 bool TransferBufferManager::RegisterTransferBuffer(
43 scoped_ptr
<BufferBacking
> buffer_backing
) {
45 DVLOG(0) << "Cannot register transfer buffer with non-positive ID.";
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.";
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();
64 "gpu", "GpuTransferBufferMemory", this, shared_memory_bytes_allocated_
);
66 registered_buffers_
[id
] = buffer
;
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.";
78 DCHECK(shared_memory_bytes_allocated_
>= it
->second
->size());
79 shared_memory_bytes_allocated_
-= it
->second
->size();
81 "gpu", "GpuTransferBufferMemory", this, shared_memory_bytes_allocated_
);
83 registered_buffers_
.erase(it
);
86 scoped_refptr
<Buffer
> TransferBufferManager::GetTransferBuffer(int32 id
) {
90 BufferMap::iterator it
= registered_buffers_
.find(id
);
91 if (it
== registered_buffers_
.end())