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
BufferTracker(MappedMemoryManager * manager)13 BufferTracker::BufferTracker(MappedMemoryManager* manager)
14 : mapped_memory_(manager) {
15 }
16
~BufferTracker()17 BufferTracker::~BufferTracker() {
18 while (!buffers_.empty()) {
19 RemoveBuffer(buffers_.begin()->first);
20 }
21 }
22
CreateBuffer(GLuint id,GLsizeiptr size)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
GetBuffer(GLuint client_id)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
RemoveBuffer(GLuint client_id)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
FreePendingToken(Buffer * buffer,int32 token)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 buffer->last_usage_token_ = 0;
64 buffer->last_async_upload_token_ = 0;
65 }
66
Unmanage(Buffer * buffer)67 void BufferTracker::Unmanage(Buffer* buffer) {
68 buffer->size_ = 0;
69 buffer->shm_id_ = 0;
70 buffer->shm_offset_ = 0;
71 buffer->address_ = NULL;
72 buffer->last_usage_token_ = 0;
73 buffer->last_async_upload_token_ = 0;
74 }
75
Free(Buffer * buffer)76 void BufferTracker::Free(Buffer* buffer) {
77 if (buffer->address_)
78 mapped_memory_->Free(buffer->address_);
79
80 buffer->size_ = 0;
81 buffer->shm_id_ = 0;
82 buffer->shm_offset_ = 0;
83 buffer->address_ = NULL;
84 buffer->last_usage_token_ = 0;
85 buffer->last_async_upload_token_ = 0;
86 }
87
88 } // namespace gles2
89 } // namespace gpu
90