• 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 "gpu/command_buffer/service/transfer_buffer_manager.h"
6 
7 #include <limits>
8 
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/debug/trace_event.h"
12 #include "base/process/process_handle.h"
13 #include "gpu/command_buffer/common/cmd_buffer_common.h"
14 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
15 
16 using ::base::SharedMemory;
17 
18 namespace gpu {
19 
~TransferBufferManagerInterface()20 TransferBufferManagerInterface::~TransferBufferManagerInterface() {
21 }
22 
TransferBufferManager()23 TransferBufferManager::TransferBufferManager()
24     : shared_memory_bytes_allocated_(0) {
25 }
26 
~TransferBufferManager()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);
33   }
34   DCHECK(!shared_memory_bytes_allocated_);
35 }
36 
Initialize()37 bool TransferBufferManager::Initialize() {
38   return true;
39 }
40 
RegisterTransferBuffer(int32 id,scoped_ptr<BufferBacking> buffer_backing)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;
47   }
48 
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;
53   }
54 
55   // Register the shared memory with the ID.
56   scoped_refptr<Buffer> buffer(new gpu::Buffer(buffer_backing.Pass()));
57 
58   // Check buffer alignment is sane.
59   DCHECK(!(reinterpret_cast<uintptr_t>(buffer->memory()) &
60            (kCommandBufferEntrySize - 1)));
61 
62   shared_memory_bytes_allocated_ += buffer->size();
63   TRACE_COUNTER_ID1(
64       "gpu", "GpuTransferBufferMemory", this, shared_memory_bytes_allocated_);
65 
66   registered_buffers_[id] = buffer;
67 
68   return true;
69 }
70 
DestroyTransferBuffer(int32 id)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;
76   }
77 
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_);
82 
83   registered_buffers_.erase(it);
84 }
85 
GetTransferBuffer(int32 id)86 scoped_refptr<Buffer> TransferBufferManager::GetTransferBuffer(int32 id) {
87   if (id == 0)
88     return NULL;
89 
90   BufferMap::iterator it = registered_buffers_.find(id);
91   if (it == registered_buffers_.end())
92     return NULL;
93 
94   return it->second;
95 }
96 
97 }  // namespace gpu
98 
99