blob: b06c3ad94adfe3f7de788d800ef893daca029a65 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com1c13c962011-02-14 16:51:21 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
bsalomon@google.com1c13c962011-02-14 16:51:21 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com1c13c962011-02-14 16:51:21 +000010#include "GrBufferAllocPool.h"
11#include "GrTypes.h"
12#include "GrVertexBuffer.h"
13#include "GrIndexBuffer.h"
14#include "GrGpu.h"
15
16#if GR_DEBUG
17 #define VALIDATE validate
18#else
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000019 static void VALIDATE(bool x = false) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020#endif
21
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000022// page size
bsalomon@google.com1c13c962011-02-14 16:51:21 +000023#define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 12)
24
25GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu,
26 BufferType bufferType,
27 bool frequentResetHint,
28 size_t blockSize,
29 int preallocBufferCnt) :
30 fBlocks(GrMax(8, 2*preallocBufferCnt)) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000031
bsalomon@google.com1c13c962011-02-14 16:51:21 +000032 GrAssert(NULL != gpu);
33 fGpu = gpu;
bsalomon@google.com11f0b512011-03-29 20:52:23 +000034 fGpu->ref();
35 fGpuIsReffed = true;
36
bsalomon@google.com1c13c962011-02-14 16:51:21 +000037 fBufferType = bufferType;
38 fFrequentResetHint = frequentResetHint;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000039 fBufferPtr = NULL;
40 fMinBlockSize = GrMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize);
41
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000042 fBytesInUse = 0;
43
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 fPreallocBuffersInUse = 0;
45 fFirstPreallocBuffer = 0;
46 for (int i = 0; i < preallocBufferCnt; ++i) {
47 GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize);
48 if (NULL != buffer) {
49 *fPreallocBuffers.append() = buffer;
50 buffer->ref();
51 }
52 }
53}
54
55GrBufferAllocPool::~GrBufferAllocPool() {
56 VALIDATE();
57 if (fBlocks.count()) {
58 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
59 if (buffer->isLocked()) {
60 buffer->unlock();
61 }
62 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000063 while (!fBlocks.empty()) {
64 destroyBlock();
65 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +000066 fPreallocBuffers.unrefAll();
67 releaseGpuRef();
68}
69
70void GrBufferAllocPool::releaseGpuRef() {
71 if (fGpuIsReffed) {
72 fGpu->unref();
73 fGpuIsReffed = false;
74 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075}
76
77void GrBufferAllocPool::reset() {
78 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000079 fBytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080 if (fBlocks.count()) {
81 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
82 if (buffer->isLocked()) {
83 buffer->unlock();
84 }
85 }
86 while (!fBlocks.empty()) {
87 destroyBlock();
88 }
89 if (fPreallocBuffers.count()) {
90 // must set this after above loop.
91 fFirstPreallocBuffer = (fFirstPreallocBuffer + fPreallocBuffersInUse) %
92 fPreallocBuffers.count();
93 }
bsalomon@google.com18c9c192011-09-22 21:01:31 +000094 fCpuData.reset(fGpu->getCaps().fBufferLockSupport ? 0 : fMinBlockSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000095 GrAssert(0 == fPreallocBuffersInUse);
96 VALIDATE();
97}
98
99void GrBufferAllocPool::unlock() {
100 VALIDATE();
101
102 if (NULL != fBufferPtr) {
103 BufferBlock& block = fBlocks.back();
104 if (block.fBuffer->isLocked()) {
105 block.fBuffer->unlock();
106 } else {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000107 size_t flushSize = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000108 flushCpuData(fBlocks.back().fBuffer, flushSize);
109 }
110 fBufferPtr = NULL;
111 }
112 VALIDATE();
113}
114
115#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000116void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000117 if (NULL != fBufferPtr) {
118 GrAssert(!fBlocks.empty());
119 if (fBlocks.back().fBuffer->isLocked()) {
120 GrGeometryBuffer* buf = fBlocks.back().fBuffer;
121 GrAssert(buf->lockPtr() == fBufferPtr);
122 } else {
123 GrAssert(fCpuData.get() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 }
125 } else {
126 GrAssert(fBlocks.empty() || !fBlocks.back().fBuffer->isLocked());
127 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000128 size_t bytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000129 for (int i = 0; i < fBlocks.count() - 1; ++i) {
130 GrAssert(!fBlocks[i].fBuffer->isLocked());
131 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000132 for (int i = 0; i < fBlocks.count(); ++i) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000133 size_t bytes = fBlocks[i].fBuffer->sizeInBytes() - fBlocks[i].fBytesFree;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000134 bytesInUse += bytes;
135 GrAssert(bytes || unusedBlockAllowed);
136 }
137
138 GrAssert(bytesInUse == fBytesInUse);
139 if (unusedBlockAllowed) {
140 GrAssert((fBytesInUse && !fBlocks.empty()) ||
141 (!fBytesInUse && (fBlocks.count() < 2)));
142 } else {
143 GrAssert((0 == fBytesInUse) == fBlocks.empty());
144 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000145}
146#endif
147
148void* GrBufferAllocPool::makeSpace(size_t size,
149 size_t alignment,
150 const GrGeometryBuffer** buffer,
151 size_t* offset) {
152 VALIDATE();
153
154 GrAssert(NULL != buffer);
155 GrAssert(NULL != offset);
156
157 if (NULL != fBufferPtr) {
158 BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000159 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000160 size_t pad = GrSizeAlignUpPad(usedBytes,
161 alignment);
162 if ((size + pad) <= back.fBytesFree) {
163 usedBytes += pad;
164 *offset = usedBytes;
165 *buffer = back.fBuffer;
166 back.fBytesFree -= size + pad;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000167 fBytesInUse += size;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
169 }
170 }
171
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000172 // We could honor the space request using by a partial update of the current
173 // VB (if there is room). But we don't currently use draw calls to GL that
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000174 // allow the driver to know that previously issued draws won't read from
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000175 // the part of the buffer we update. Also, the GL buffer implementation
176 // may be cheating on the actual buffer size by shrinking the buffer on
177 // updateData() if the amount of data passed is less than the full buffer
178 // size.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000179
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000180 if (!createBlock(size)) {
181 return NULL;
182 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000183 GrAssert(NULL != fBufferPtr);
184
185 *offset = 0;
186 BufferBlock& back = fBlocks.back();
187 *buffer = back.fBuffer;
188 back.fBytesFree -= size;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000189 fBytesInUse += size;
190 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000191 return fBufferPtr;
192}
193
194int GrBufferAllocPool::currentBufferItems(size_t itemSize) const {
195 VALIDATE();
196 if (NULL != fBufferPtr) {
197 const BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000198 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000199 size_t pad = GrSizeAlignUpPad(usedBytes, itemSize);
200 return (back.fBytesFree - pad) / itemSize;
201 } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) {
202 return fMinBlockSize / itemSize;
203 }
204 return 0;
205}
206
207int GrBufferAllocPool::preallocatedBuffersRemaining() const {
208 return fPreallocBuffers.count() - fPreallocBuffersInUse;
209}
210
211int GrBufferAllocPool::preallocatedBufferCount() const {
212 return fPreallocBuffers.count();
213}
214
215void GrBufferAllocPool::putBack(size_t bytes) {
216 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000217
218 while (bytes) {
219 // caller shouldnt try to put back more than they've taken
220 GrAssert(!fBlocks.empty());
221 BufferBlock& block = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000222 size_t bytesUsed = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000223 if (bytes >= bytesUsed) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000224 bytes -= bytesUsed;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000225 fBytesInUse -= bytesUsed;
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000226 // if we locked a vb to satisfy the make space and we're releasing
227 // beyond it, then unlock it.
228 if (block.fBuffer->isLocked()) {
229 block.fBuffer->unlock();
230 }
231 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000232 } else {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000233 block.fBytesFree += bytes;
234 fBytesInUse -= bytes;
235 bytes = 0;
236 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000237 }
238 }
239 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000240}
241
242bool GrBufferAllocPool::createBlock(size_t requestSize) {
243
244 size_t size = GrMax(requestSize, fMinBlockSize);
245 GrAssert(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
246
247 VALIDATE();
248
249 BufferBlock& block = fBlocks.push_back();
250
251 if (size == fMinBlockSize &&
252 fPreallocBuffersInUse < fPreallocBuffers.count()) {
253
254 uint32_t nextBuffer = (fPreallocBuffersInUse + fFirstPreallocBuffer) %
255 fPreallocBuffers.count();
256 block.fBuffer = fPreallocBuffers[nextBuffer];
257 block.fBuffer->ref();
258 ++fPreallocBuffersInUse;
259 } else {
260 block.fBuffer = this->createBuffer(size);
261 if (NULL == block.fBuffer) {
262 fBlocks.pop_back();
263 return false;
264 }
265 }
266
267 block.fBytesFree = size;
268 if (NULL != fBufferPtr) {
269 GrAssert(fBlocks.count() > 1);
270 BufferBlock& prev = fBlocks.fromBack(1);
271 if (prev.fBuffer->isLocked()) {
272 prev.fBuffer->unlock();
273 } else {
274 flushCpuData(prev.fBuffer,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000275 prev.fBuffer->sizeInBytes() - prev.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000276 }
277 fBufferPtr = NULL;
278 }
279
280 GrAssert(NULL == fBufferPtr);
281
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000282 if (fGpu->getCaps().fBufferLockSupport &&
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000283 size > GR_GEOM_BUFFER_LOCK_THRESHOLD &&
284 (!fFrequentResetHint || requestSize > GR_GEOM_BUFFER_LOCK_THRESHOLD)) {
285 fBufferPtr = block.fBuffer->lock();
286 }
287
288 if (NULL == fBufferPtr) {
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000289 fBufferPtr = fCpuData.reset(size);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000290 }
291
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000292 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293
294 return true;
295}
296
297void GrBufferAllocPool::destroyBlock() {
298 GrAssert(!fBlocks.empty());
299
300 BufferBlock& block = fBlocks.back();
301 if (fPreallocBuffersInUse > 0) {
302 uint32_t prevPreallocBuffer = (fPreallocBuffersInUse +
303 fFirstPreallocBuffer +
304 (fPreallocBuffers.count() - 1)) %
305 fPreallocBuffers.count();
306 if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) {
307 --fPreallocBuffersInUse;
308 }
309 }
310 GrAssert(!block.fBuffer->isLocked());
311 block.fBuffer->unref();
312 fBlocks.pop_back();
313 fBufferPtr = NULL;
314}
315
316void GrBufferAllocPool::flushCpuData(GrGeometryBuffer* buffer,
317 size_t flushSize) {
318 GrAssert(NULL != buffer);
319 GrAssert(!buffer->isLocked());
320 GrAssert(fCpuData.get() == fBufferPtr);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000321 GrAssert(flushSize <= buffer->sizeInBytes());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000322
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000323 if (fGpu->getCaps().fBufferLockSupport &&
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000324 flushSize > GR_GEOM_BUFFER_LOCK_THRESHOLD) {
325 void* data = buffer->lock();
326 if (NULL != data) {
327 memcpy(data, fBufferPtr, flushSize);
328 buffer->unlock();
bsalomon@google.com71bd1ef2011-12-12 20:42:26 +0000329 return;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000330 }
331 }
332 buffer->updateData(fBufferPtr, flushSize);
333}
334
335GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) {
336 if (kIndex_BufferType == fBufferType) {
337 return fGpu->createIndexBuffer(size, true);
338 } else {
339 GrAssert(kVertex_BufferType == fBufferType);
340 return fGpu->createVertexBuffer(size, true);
341 }
342}
343
344////////////////////////////////////////////////////////////////////////////////
345
346GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu,
347 bool frequentResetHint,
348 size_t bufferSize,
349 int preallocBufferCnt)
350: GrBufferAllocPool(gpu,
351 kVertex_BufferType,
352 frequentResetHint,
353 bufferSize,
354 preallocBufferCnt) {
355}
356
357void* GrVertexBufferAllocPool::makeSpace(GrVertexLayout layout,
358 int vertexCount,
359 const GrVertexBuffer** buffer,
360 int* startVertex) {
361
362 GrAssert(vertexCount >= 0);
363 GrAssert(NULL != buffer);
364 GrAssert(NULL != startVertex);
365
366 size_t vSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com8b484412011-04-18 19:07:44 +0000367 size_t offset = 0; // assign to suppress warning
368 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000369 void* ptr = INHERITED::makeSpace(vSize * vertexCount,
370 vSize,
371 &geomBuffer,
372 &offset);
373
374 *buffer = (const GrVertexBuffer*) geomBuffer;
375 GrAssert(0 == offset % vSize);
376 *startVertex = offset / vSize;
377 return ptr;
378}
379
380bool GrVertexBufferAllocPool::appendVertices(GrVertexLayout layout,
381 int vertexCount,
382 const void* vertices,
383 const GrVertexBuffer** buffer,
384 int* startVertex) {
385 void* space = makeSpace(layout, vertexCount, buffer, startVertex);
386 if (NULL != space) {
387 memcpy(space,
388 vertices,
389 GrDrawTarget::VertexSize(layout) * vertexCount);
390 return true;
391 } else {
392 return false;
393 }
394}
395
396int GrVertexBufferAllocPool::preallocatedBufferVertices(GrVertexLayout layout) const {
397 return INHERITED::preallocatedBufferSize() /
398 GrDrawTarget::VertexSize(layout);
399}
400
401int GrVertexBufferAllocPool::currentBufferVertices(GrVertexLayout layout) const {
402 return currentBufferItems(GrDrawTarget::VertexSize(layout));
403}
404
405////////////////////////////////////////////////////////////////////////////////
406
407GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu,
408 bool frequentResetHint,
409 size_t bufferSize,
410 int preallocBufferCnt)
411: GrBufferAllocPool(gpu,
412 kIndex_BufferType,
413 frequentResetHint,
414 bufferSize,
415 preallocBufferCnt) {
416}
417
418void* GrIndexBufferAllocPool::makeSpace(int indexCount,
419 const GrIndexBuffer** buffer,
420 int* startIndex) {
421
422 GrAssert(indexCount >= 0);
423 GrAssert(NULL != buffer);
424 GrAssert(NULL != startIndex);
425
bsalomon@google.com8b484412011-04-18 19:07:44 +0000426 size_t offset = 0; // assign to suppress warning
427 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000428 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
429 sizeof(uint16_t),
430 &geomBuffer,
431 &offset);
432
433 *buffer = (const GrIndexBuffer*) geomBuffer;
434 GrAssert(0 == offset % sizeof(uint16_t));
435 *startIndex = offset / sizeof(uint16_t);
436 return ptr;
437}
438
439bool GrIndexBufferAllocPool::appendIndices(int indexCount,
440 const void* indices,
441 const GrIndexBuffer** buffer,
442 int* startIndex) {
443 void* space = makeSpace(indexCount, buffer, startIndex);
444 if (NULL != space) {
445 memcpy(space, indices, sizeof(uint16_t) * indexCount);
446 return true;
447 } else {
448 return false;
449 }
450}
451
452int GrIndexBufferAllocPool::preallocatedBufferIndices() const {
453 return INHERITED::preallocatedBufferSize() / sizeof(uint16_t);
454}
455
456int GrIndexBufferAllocPool::currentBufferIndices() const {
457 return currentBufferItems(sizeof(uint16_t));
458}