OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "o3d/gpu_plugin/command_buffer.h" | 5 #include "o3d/gpu_plugin/command_buffer.h" |
6 | 6 |
7 namespace o3d { | 7 namespace o3d { |
8 namespace gpu_plugin { | 8 namespace gpu_plugin { |
9 | 9 |
10 CommandBuffer::CommandBuffer(NPP npp) | 10 CommandBuffer::CommandBuffer(NPP npp) |
11 : npp_(npp), | 11 : npp_(npp), |
12 size_(0), | 12 size_(0), |
13 get_offset_(0), | 13 get_offset_(0), |
14 put_offset_(0) { | 14 put_offset_(0) { |
| 15 // Element zero is always NULL. |
| 16 registered_objects_.push_back(NPObjectPointer<NPObject>()); |
15 } | 17 } |
16 | 18 |
17 CommandBuffer::~CommandBuffer() { | 19 CommandBuffer::~CommandBuffer() { |
18 } | 20 } |
19 | 21 |
20 bool CommandBuffer::Initialize(int32 size) { | 22 bool CommandBuffer::Initialize(int32 size) { |
21 if (shared_memory_.Get()) | 23 if (shared_memory_.Get()) |
22 return false; | 24 return false; |
23 | 25 |
24 NPObjectPointer<NPObject> window = NPObjectPointer<NPObject>::FromReturned( | 26 NPObjectPointer<NPObject> window = NPObjectPointer<NPObject>::FromReturned( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 } | 92 } |
91 | 93 |
92 int32 CommandBuffer::GetPutOffset() { | 94 int32 CommandBuffer::GetPutOffset() { |
93 return put_offset_; | 95 return put_offset_; |
94 } | 96 } |
95 | 97 |
96 void CommandBuffer::SetPutOffsetChangeCallback(Callback0::Type* callback) { | 98 void CommandBuffer::SetPutOffsetChangeCallback(Callback0::Type* callback) { |
97 put_offset_change_callback_.reset(callback); | 99 put_offset_change_callback_.reset(callback); |
98 } | 100 } |
99 | 101 |
| 102 int32 CommandBuffer::RegisterObject(NPObjectPointer<NPObject> object) { |
| 103 if (!object.Get()) |
| 104 return 0; |
| 105 |
| 106 if (unused_registered_object_elements_.empty()) { |
| 107 // Check we haven't exceeded the range that fits in the range of a 32-bit |
| 108 // integer. |
| 109 int32 handle = static_cast<int32>(registered_objects_.size()); |
| 110 if (handle != registered_objects_.size()) |
| 111 return -1; |
| 112 |
| 113 registered_objects_.push_back(object); |
| 114 return handle; |
| 115 } |
| 116 |
| 117 int32 handle = *unused_registered_object_elements_.begin(); |
| 118 unused_registered_object_elements_.erase( |
| 119 unused_registered_object_elements_.begin()); |
| 120 DCHECK(!registered_objects_[handle].Get()); |
| 121 registered_objects_[handle] = object; |
| 122 return handle; |
| 123 } |
| 124 |
| 125 void CommandBuffer::UnregisterObject(NPObjectPointer<NPObject> object, |
| 126 int32 handle) { |
| 127 if (handle <= 0) |
| 128 return; |
| 129 |
| 130 if (static_cast<size_t>(handle) >= registered_objects_.size()) |
| 131 return; |
| 132 |
| 133 if (registered_objects_[handle] != object) |
| 134 return; |
| 135 |
| 136 registered_objects_[handle] = NPObjectPointer<NPObject>(); |
| 137 unused_registered_object_elements_.insert(handle); |
| 138 |
| 139 // Remove all null objects from the end of the vector. This allows the vector |
| 140 // to shrink when, for example, all objects are unregistered. Note that this |
| 141 // loop never removes element zero, which is always NULL. |
| 142 while (registered_objects_.size() > 1 && !registered_objects_.back().Get()) { |
| 143 registered_objects_.pop_back(); |
| 144 unused_registered_object_elements_.erase( |
| 145 static_cast<int32>(registered_objects_.size())); |
| 146 } |
| 147 } |
| 148 |
| 149 NPObjectPointer<NPObject> CommandBuffer::GetRegisteredObject(int32 handle) { |
| 150 DCHECK_GE(handle, 0); |
| 151 DCHECK_LT(static_cast<size_t>(handle), registered_objects_.size()); |
| 152 |
| 153 return registered_objects_[handle]; |
| 154 } |
| 155 |
100 } // namespace gpu_plugin | 156 } // namespace gpu_plugin |
101 } // namespace o3d | 157 } // namespace o3d |
OLD | NEW |