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/client/buffer_tracker.h" 6 7 #include "gpu/command_buffer/client/cmd_buffer_helper.h" 8 #include "gpu/command_buffer/client/mapped_memory.h" 9 10 namespace gpu { 11 namespace gles2 { 12 13 BufferTracker::BufferTracker(MappedMemoryManager* manager) 14 : mapped_memory_(manager) { 15 } 16 17 BufferTracker::~BufferTracker() { 18 while (!buffers_.empty()) { 19 RemoveBuffer(buffers_.begin()->first); 20 } 21 } 22 23 BufferTracker::Buffer* BufferTracker::CreateBuffer( 24 GLuint id, GLsizeiptr size) { 25 DCHECK_NE(0u, id); 26 DCHECK_LE(0, size); 27 int32 shm_id = -1; 28 uint32 shm_offset = 0; 29 void* address = NULL; 30 if (size) 31 address = mapped_memory_->Alloc(size, &shm_id, &shm_offset); 32 33 Buffer* buffer = new Buffer(id, size, shm_id, shm_offset, address); 34 std::pair<BufferMap::iterator, bool> result = 35 buffers_.insert(std::make_pair(id, buffer)); 36 DCHECK(result.second); 37 return buffer; 38 } 39 40 BufferTracker::Buffer* BufferTracker::GetBuffer(GLuint client_id) { 41 BufferMap::iterator it = buffers_.find(client_id); 42 return it != buffers_.end() ? it->second : NULL; 43 } 44 45 void BufferTracker::RemoveBuffer(GLuint client_id) { 46 BufferMap::iterator it = buffers_.find(client_id); 47 if (it != buffers_.end()) { 48 Buffer* buffer = it->second; 49 buffers_.erase(it); 50 if (buffer->address_) 51 mapped_memory_->Free(buffer->address_); 52 delete buffer; 53 } 54 } 55 56 void BufferTracker::FreePendingToken(Buffer* buffer, int32 token) { 57 if (buffer->address_) 58 mapped_memory_->FreePendingToken(buffer->address_, token); 59 buffer->size_ = 0; 60 buffer->shm_id_ = 0; 61 buffer->shm_offset_ = 0; 62 buffer->address_ = NULL; 63 } 64 65 66 } // namespace gles2 67 } // namespace gpu 68