Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(141)

Side by Side Diff: core/cross/buffer.cc

Issue 155276: Add ClientInfo... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/o3d/
Patch Set: '' Created 11 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright 2009, Google Inc. 2 * Copyright 2009, Google Inc.
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 6 * modification, are permitted provided that the following conditions are
7 * met: 7 * met:
8 * 8 *
9 * * Redistributions of source code must retain the above copyright 9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 16 matching lines...) Expand all
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32 32
33 // This file contains the definitions of Buffer, VertexBuffer and IndexBuffer. 33 // This file contains the definitions of Buffer, VertexBuffer and IndexBuffer.
34 34
35 #include "core/cross/precompile.h" 35 #include "core/cross/precompile.h"
36 #include "core/cross/buffer.h" 36 #include "core/cross/buffer.h"
37 #include "core/cross/client_info.h"
37 #include "core/cross/renderer.h" 38 #include "core/cross/renderer.h"
38 #include "core/cross/features.h" 39 #include "core/cross/features.h"
39 #include "core/cross/error.h" 40 #include "core/cross/error.h"
40 #include "import/cross/memory_stream.h" 41 #include "import/cross/memory_stream.h"
41 #include "import/cross/raw_data.h" 42 #include "import/cross/raw_data.h"
42 43
43 namespace o3d { 44 namespace o3d {
44 45
45 O3D_DEFN_CLASS(Buffer, NamedObject); 46 O3D_DEFN_CLASS(Buffer, NamedObject);
46 O3D_DEFN_CLASS(VertexBufferBase, Buffer); 47 O3D_DEFN_CLASS(VertexBufferBase, Buffer);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 features_(service_locator->GetService<Features>()), 102 features_(service_locator->GetService<Features>()),
102 access_mode_(NONE), 103 access_mode_(NONE),
103 field_change_count_(0), 104 field_change_count_(0),
104 total_components_(0), 105 total_components_(0),
105 stride_(0), 106 stride_(0),
106 num_elements_(0), 107 num_elements_(0),
107 lock_count_(0) { 108 lock_count_(0) {
108 } 109 }
109 110
110 Buffer::~Buffer() { 111 Buffer::~Buffer() {
112 AdjustBufferMemoryInfo(false);
111 for (unsigned ii = 0; ii < fields_.size(); ++ii) { 113 for (unsigned ii = 0; ii < fields_.size(); ++ii) {
112 if (!fields_[ii].IsNull()) { 114 if (!fields_[ii].IsNull()) {
113 fields_[ii]->ClearBuffer(); 115 fields_[ii]->ClearBuffer();
114 } 116 }
115 } 117 }
116 } 118 }
117 119
120 void Buffer::AdjustBufferMemoryInfo(bool add) {
121 // Only count VRAM/hardware buffers.
122 if (IsA(VertexBuffer::GetApparentClass()) ||
123 IsA(IndexBuffer::GetApparentClass())) {
124 size_t size_in_bytes = num_elements_ * stride_;
125 ServiceDependency<ClientInfoManager> client_info_manager(service_locator());
apatrick 2009/07/09 18:22:00 You don't need to use a ServiceDependency here. Ge
126 client_info_manager->AdjustBufferMemoryUsed(
127 static_cast<int>(size_in_bytes) * (add ? 1 : -1));
128 }
129 }
130
118 bool Buffer::AllocateElements(unsigned num_elements) { 131 bool Buffer::AllocateElements(unsigned num_elements) {
119 if (access_mode_ != NONE) { 132 if (access_mode_ != NONE) {
120 O3D_ERROR(service_locator()) << "Attempt to allocate locked Buffer '" 133 O3D_ERROR(service_locator()) << "Attempt to allocate locked Buffer '"
121 << name() << "'"; 134 << name() << "'";
122 return false; 135 return false;
123 } 136 }
124 137
125 if (stride_ == 0) { 138 if (stride_ == 0) {
126 O3D_ERROR(service_locator()) 139 O3D_ERROR(service_locator())
127 << "No fields have been set on Buffer '" << name() << "'"; 140 << "No fields have been set on Buffer '" << name() << "'";
(...skipping 17 matching lines...) Expand all
145 } 158 }
146 159
147 size_t size_in_bytes = num_elements * stride_; 160 size_t size_in_bytes = num_elements * stride_;
148 161
149 if (size_in_bytes == 0) { 162 if (size_in_bytes == 0) {
150 O3D_ERROR(service_locator()) 163 O3D_ERROR(service_locator())
151 << "Attempt to allocate zero bytes for Buffer '" << name() << "'"; 164 << "Attempt to allocate zero bytes for Buffer '" << name() << "'";
152 return false; 165 return false;
153 } 166 }
154 167
168 bool success = true;
155 if (!ConcreteAllocate(size_in_bytes)) { 169 if (!ConcreteAllocate(size_in_bytes)) {
156 num_elements_ = 0; 170 num_elements = 0;
157 return false; 171 size_in_bytes = 0;
172 success = false;
158 } 173 }
159 174
160 num_elements_ = num_elements; 175 num_elements_ = num_elements;
161 return true; 176
177 AdjustBufferMemoryInfo(true);
178
179 return success;
162 } 180 }
163 181
164 void Buffer::Free() { 182 void Buffer::Free() {
165 if (num_elements_ > 0) { 183 if (num_elements_ > 0) {
166 ConcreteFree(); 184 ConcreteFree();
185 AdjustBufferMemoryInfo(false);
167 num_elements_ = 0; 186 num_elements_ = 0;
168 } 187 }
169 } 188 }
170 189
171 bool Buffer::ReshuffleBuffer(unsigned int new_stride, Field* field_to_remove) { 190 bool Buffer::ReshuffleBuffer(unsigned int new_stride, Field* field_to_remove) {
172 if (new_stride == 0) { 191 if (new_stride == 0) {
192 AdjustBufferMemoryInfo(false);
173 ConcreteFree(); 193 ConcreteFree();
194 stride_ = 0;
174 return true; 195 return true;
175 } 196 }
176 if (num_elements_) { 197 if (num_elements_) {
177 size_t size_in_bytes = num_elements_ * new_stride; 198 size_t size_in_bytes = num_elements_ * new_stride;
178 std::vector<uint8> temp(size_in_bytes); 199 std::vector<uint8> temp(size_in_bytes);
179 200
180 // Copy old fields into new buffer. 201 // Copy old fields into new buffer.
181 { 202 {
182 BufferLockHelper helper(this); 203 BufferLockHelper helper(this);
183 void* source = helper.GetData(Buffer::READ_ONLY); 204 void* source = helper.GetData(Buffer::READ_ONLY);
(...skipping 11 matching lines...) Expand all
195 PointerFromVoidPointer<void*>(&temp[0], offset), 216 PointerFromVoidPointer<void*>(&temp[0], offset),
196 new_stride); 217 new_stride);
197 field->set_offset(offset); 218 field->set_offset(offset);
198 offset += field->size(); 219 offset += field->size();
199 } 220 }
200 } 221 }
201 } 222 }
202 // Copy the reorganized data into a new buffer. 223 // Copy the reorganized data into a new buffer.
203 { 224 {
204 ConcreteFree(); 225 ConcreteFree();
226 AdjustBufferMemoryInfo(false);
205 if (!ConcreteAllocate(size_in_bytes)) { 227 if (!ConcreteAllocate(size_in_bytes)) {
206 num_elements_ = 0; 228 num_elements_ = 0;
207 O3D_ERROR(service_locator()) 229 O3D_ERROR(service_locator())
208 << "Couldn't allocate buffer of size: " << size_in_bytes 230 << "Couldn't allocate buffer of size: " << size_in_bytes
209 << " for Buffer '" << name() << "'"; 231 << " for Buffer '" << name() << "'";
210 return false; 232 return false;
211 } 233 }
212 // stride_ must be set before GetData is called so that the proper size 234 // stride_ must be set before GetData is called so that the proper size
213 // buffer is allocated. We also need to set it after this function is 235 // buffer is allocated. We also need to set it after this function is
214 // is completed (see CreateField, RemoveField) for when we create a new 236 // is completed (see CreateField, RemoveField) for when we create a new
215 // buffer with no fields yet. 237 // buffer with no fields yet.
216 stride_ = new_stride; 238 stride_ = new_stride;
239 AdjustBufferMemoryInfo(true);
217 BufferLockHelper helper(this); 240 BufferLockHelper helper(this);
218 void* destination = helper.GetData(Buffer::WRITE_ONLY); 241 void* destination = helper.GetData(Buffer::WRITE_ONLY);
219 if (!destination) { 242 if (!destination) {
220 return false; 243 return false;
221 } 244 }
222 memcpy(destination, &temp[0], size_in_bytes); 245 memcpy(destination, &temp[0], size_in_bytes);
223 } 246 }
224 } 247 }
225 return true; 248 return true;
226 } 249 }
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
573 locked_ = buffer_->Lock(access_mode, &data_); 596 locked_ = buffer_->Lock(access_mode, &data_);
574 if (!locked_) { 597 if (!locked_) {
575 O3D_ERROR(buffer_->service_locator()) 598 O3D_ERROR(buffer_->service_locator())
576 << "Unable to lock buffer '" << buffer_->name() << "'"; 599 << "Unable to lock buffer '" << buffer_->name() << "'";
577 } 600 }
578 } 601 }
579 return data_; 602 return data_;
580 } 603 }
581 604
582 } // namespace o3d 605 } // namespace o3d
OLDNEW
« no previous file with comments | « core/cross/buffer.h ('k') | core/cross/client.cc » ('j') | core/cross/client_info_test.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698