blob: 70924f27b9fbef137e461476bf4240801278bc88 [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;
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000043
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 fPreallocBuffersInUse = 0;
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000045 fPreallocBufferStartIdx = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000046 for (int i = 0; i < preallocBufferCnt; ++i) {
47 GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize);
48 if (NULL != buffer) {
49 *fPreallocBuffers.append() = buffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000050 }
51 }
52}
53
54GrBufferAllocPool::~GrBufferAllocPool() {
55 VALIDATE();
56 if (fBlocks.count()) {
57 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
58 if (buffer->isLocked()) {
59 buffer->unlock();
60 }
61 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000062 while (!fBlocks.empty()) {
63 destroyBlock();
64 }
bsalomon@google.com11f0b512011-03-29 20:52:23 +000065 fPreallocBuffers.unrefAll();
66 releaseGpuRef();
67}
68
69void GrBufferAllocPool::releaseGpuRef() {
70 if (fGpuIsReffed) {
71 fGpu->unref();
72 fGpuIsReffed = false;
73 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074}
75
76void GrBufferAllocPool::reset() {
77 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000078 fBytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000079 if (fBlocks.count()) {
80 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
81 if (buffer->isLocked()) {
82 buffer->unlock();
83 }
84 }
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000085 // fPreallocBuffersInUse will be decremented down to zero in the while loop
86 int preallocBuffersInUse = fPreallocBuffersInUse;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 while (!fBlocks.empty()) {
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000088 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000089 }
90 if (fPreallocBuffers.count()) {
91 // must set this after above loop.
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000092 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
93 preallocBuffersInUse) %
94 fPreallocBuffers.count();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000095 }
bsalomon@google.com987dbc02011-12-14 14:44:19 +000096 // we may have created a large cpu mirror of a large VB. Reset the size
97 // to match our pre-allocated VBs.
98 fCpuData.reset(fMinBlockSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099 GrAssert(0 == fPreallocBuffersInUse);
100 VALIDATE();
101}
102
103void GrBufferAllocPool::unlock() {
104 VALIDATE();
105
106 if (NULL != fBufferPtr) {
107 BufferBlock& block = fBlocks.back();
108 if (block.fBuffer->isLocked()) {
109 block.fBuffer->unlock();
110 } else {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000111 size_t flushSize = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 flushCpuData(fBlocks.back().fBuffer, flushSize);
113 }
114 fBufferPtr = NULL;
115 }
116 VALIDATE();
117}
118
119#if GR_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000120void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000121 if (NULL != fBufferPtr) {
122 GrAssert(!fBlocks.empty());
123 if (fBlocks.back().fBuffer->isLocked()) {
124 GrGeometryBuffer* buf = fBlocks.back().fBuffer;
125 GrAssert(buf->lockPtr() == fBufferPtr);
126 } else {
127 GrAssert(fCpuData.get() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000128 }
129 } else {
130 GrAssert(fBlocks.empty() || !fBlocks.back().fBuffer->isLocked());
131 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000132 size_t bytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133 for (int i = 0; i < fBlocks.count() - 1; ++i) {
134 GrAssert(!fBlocks[i].fBuffer->isLocked());
135 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000136 for (int i = 0; i < fBlocks.count(); ++i) {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000137 size_t bytes = fBlocks[i].fBuffer->sizeInBytes() - fBlocks[i].fBytesFree;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000138 bytesInUse += bytes;
139 GrAssert(bytes || unusedBlockAllowed);
140 }
141
142 GrAssert(bytesInUse == fBytesInUse);
143 if (unusedBlockAllowed) {
144 GrAssert((fBytesInUse && !fBlocks.empty()) ||
145 (!fBytesInUse && (fBlocks.count() < 2)));
146 } else {
147 GrAssert((0 == fBytesInUse) == fBlocks.empty());
148 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000149}
150#endif
151
152void* GrBufferAllocPool::makeSpace(size_t size,
153 size_t alignment,
154 const GrGeometryBuffer** buffer,
155 size_t* offset) {
156 VALIDATE();
157
158 GrAssert(NULL != buffer);
159 GrAssert(NULL != offset);
160
161 if (NULL != fBufferPtr) {
162 BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000163 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000164 size_t pad = GrSizeAlignUpPad(usedBytes,
165 alignment);
166 if ((size + pad) <= back.fBytesFree) {
167 usedBytes += pad;
168 *offset = usedBytes;
169 *buffer = back.fBuffer;
170 back.fBytesFree -= size + pad;
bsalomon@google.comd5108092012-03-08 15:10:39 +0000171 fBytesInUse += size + pad;
172 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000173 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
174 }
175 }
176
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000177 // We could honor the space request using by a partial update of the current
178 // VB (if there is room). But we don't currently use draw calls to GL that
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000179 // allow the driver to know that previously issued draws won't read from
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000180 // the part of the buffer we update. Also, the GL buffer implementation
181 // may be cheating on the actual buffer size by shrinking the buffer on
182 // updateData() if the amount of data passed is less than the full buffer
183 // size.
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000184
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000185 if (!createBlock(size)) {
186 return NULL;
187 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000188 GrAssert(NULL != fBufferPtr);
189
190 *offset = 0;
191 BufferBlock& back = fBlocks.back();
192 *buffer = back.fBuffer;
193 back.fBytesFree -= size;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000194 fBytesInUse += size;
195 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000196 return fBufferPtr;
197}
198
199int GrBufferAllocPool::currentBufferItems(size_t itemSize) const {
200 VALIDATE();
201 if (NULL != fBufferPtr) {
202 const BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000203 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000204 size_t pad = GrSizeAlignUpPad(usedBytes, itemSize);
205 return (back.fBytesFree - pad) / itemSize;
206 } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) {
207 return fMinBlockSize / itemSize;
208 }
209 return 0;
210}
211
212int GrBufferAllocPool::preallocatedBuffersRemaining() const {
213 return fPreallocBuffers.count() - fPreallocBuffersInUse;
214}
215
216int GrBufferAllocPool::preallocatedBufferCount() const {
217 return fPreallocBuffers.count();
218}
219
220void GrBufferAllocPool::putBack(size_t bytes) {
221 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000222
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000223 // if the putBack unwinds all the preallocated buffers then we will
224 // advance the starting index. As blocks are destroyed fPreallocBuffersInUse
225 // will be decremented. I will reach zero if all blocks using preallocated
226 // buffers are released.
227 int preallocBuffersInUse = fPreallocBuffersInUse;
228
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000229 while (bytes) {
230 // caller shouldnt try to put back more than they've taken
231 GrAssert(!fBlocks.empty());
232 BufferBlock& block = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000233 size_t bytesUsed = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000234 if (bytes >= bytesUsed) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000235 bytes -= bytesUsed;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000236 fBytesInUse -= bytesUsed;
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000237 // if we locked a vb to satisfy the make space and we're releasing
238 // beyond it, then unlock it.
239 if (block.fBuffer->isLocked()) {
240 block.fBuffer->unlock();
241 }
242 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000243 } else {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000244 block.fBytesFree += bytes;
245 fBytesInUse -= bytes;
246 bytes = 0;
247 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000248 }
249 }
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000250 if (!fPreallocBuffersInUse && fPreallocBuffers.count()) {
251 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
252 preallocBuffersInUse) %
253 fPreallocBuffers.count();
254 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000256}
257
258bool GrBufferAllocPool::createBlock(size_t requestSize) {
259
260 size_t size = GrMax(requestSize, fMinBlockSize);
261 GrAssert(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
262
263 VALIDATE();
264
265 BufferBlock& block = fBlocks.push_back();
266
267 if (size == fMinBlockSize &&
268 fPreallocBuffersInUse < fPreallocBuffers.count()) {
269
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000270 uint32_t nextBuffer = (fPreallocBuffersInUse +
271 fPreallocBufferStartIdx) %
272 fPreallocBuffers.count();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273 block.fBuffer = fPreallocBuffers[nextBuffer];
274 block.fBuffer->ref();
275 ++fPreallocBuffersInUse;
276 } else {
277 block.fBuffer = this->createBuffer(size);
278 if (NULL == block.fBuffer) {
279 fBlocks.pop_back();
280 return false;
281 }
282 }
283
284 block.fBytesFree = size;
285 if (NULL != fBufferPtr) {
286 GrAssert(fBlocks.count() > 1);
287 BufferBlock& prev = fBlocks.fromBack(1);
288 if (prev.fBuffer->isLocked()) {
289 prev.fBuffer->unlock();
290 } else {
291 flushCpuData(prev.fBuffer,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000292 prev.fBuffer->sizeInBytes() - prev.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000293 }
294 fBufferPtr = NULL;
295 }
296
297 GrAssert(NULL == fBufferPtr);
298
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000299 if (fGpu->getCaps().fBufferLockSupport &&
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000300 size > GR_GEOM_BUFFER_LOCK_THRESHOLD &&
301 (!fFrequentResetHint || requestSize > GR_GEOM_BUFFER_LOCK_THRESHOLD)) {
302 fBufferPtr = block.fBuffer->lock();
303 }
304
305 if (NULL == fBufferPtr) {
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000306 fBufferPtr = fCpuData.reset(size);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000307 }
308
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000309 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000310
311 return true;
312}
313
314void GrBufferAllocPool::destroyBlock() {
315 GrAssert(!fBlocks.empty());
316
317 BufferBlock& block = fBlocks.back();
318 if (fPreallocBuffersInUse > 0) {
319 uint32_t prevPreallocBuffer = (fPreallocBuffersInUse +
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000320 fPreallocBufferStartIdx +
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000321 (fPreallocBuffers.count() - 1)) %
322 fPreallocBuffers.count();
323 if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) {
324 --fPreallocBuffersInUse;
325 }
326 }
327 GrAssert(!block.fBuffer->isLocked());
328 block.fBuffer->unref();
329 fBlocks.pop_back();
330 fBufferPtr = NULL;
331}
332
333void GrBufferAllocPool::flushCpuData(GrGeometryBuffer* buffer,
334 size_t flushSize) {
335 GrAssert(NULL != buffer);
336 GrAssert(!buffer->isLocked());
337 GrAssert(fCpuData.get() == fBufferPtr);
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000338 GrAssert(flushSize <= buffer->sizeInBytes());
bsalomon@google.comd5108092012-03-08 15:10:39 +0000339 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000340
bsalomon@google.com18c9c192011-09-22 21:01:31 +0000341 if (fGpu->getCaps().fBufferLockSupport &&
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000342 flushSize > GR_GEOM_BUFFER_LOCK_THRESHOLD) {
343 void* data = buffer->lock();
344 if (NULL != data) {
345 memcpy(data, fBufferPtr, flushSize);
346 buffer->unlock();
bsalomon@google.com71bd1ef2011-12-12 20:42:26 +0000347 return;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000348 }
349 }
350 buffer->updateData(fBufferPtr, flushSize);
bsalomon@google.comd5108092012-03-08 15:10:39 +0000351 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000352}
353
354GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) {
355 if (kIndex_BufferType == fBufferType) {
356 return fGpu->createIndexBuffer(size, true);
357 } else {
358 GrAssert(kVertex_BufferType == fBufferType);
359 return fGpu->createVertexBuffer(size, true);
360 }
361}
362
363////////////////////////////////////////////////////////////////////////////////
364
365GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu,
366 bool frequentResetHint,
367 size_t bufferSize,
368 int preallocBufferCnt)
369: GrBufferAllocPool(gpu,
370 kVertex_BufferType,
371 frequentResetHint,
372 bufferSize,
373 preallocBufferCnt) {
374}
375
376void* GrVertexBufferAllocPool::makeSpace(GrVertexLayout layout,
377 int vertexCount,
378 const GrVertexBuffer** buffer,
379 int* startVertex) {
380
381 GrAssert(vertexCount >= 0);
382 GrAssert(NULL != buffer);
383 GrAssert(NULL != startVertex);
384
385 size_t vSize = GrDrawTarget::VertexSize(layout);
bsalomon@google.com8b484412011-04-18 19:07:44 +0000386 size_t offset = 0; // assign to suppress warning
387 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000388 void* ptr = INHERITED::makeSpace(vSize * vertexCount,
389 vSize,
390 &geomBuffer,
391 &offset);
392
393 *buffer = (const GrVertexBuffer*) geomBuffer;
394 GrAssert(0 == offset % vSize);
395 *startVertex = offset / vSize;
396 return ptr;
397}
398
399bool GrVertexBufferAllocPool::appendVertices(GrVertexLayout layout,
400 int vertexCount,
401 const void* vertices,
402 const GrVertexBuffer** buffer,
403 int* startVertex) {
404 void* space = makeSpace(layout, vertexCount, buffer, startVertex);
405 if (NULL != space) {
406 memcpy(space,
407 vertices,
408 GrDrawTarget::VertexSize(layout) * vertexCount);
409 return true;
410 } else {
411 return false;
412 }
413}
414
415int GrVertexBufferAllocPool::preallocatedBufferVertices(GrVertexLayout layout) const {
416 return INHERITED::preallocatedBufferSize() /
417 GrDrawTarget::VertexSize(layout);
418}
419
420int GrVertexBufferAllocPool::currentBufferVertices(GrVertexLayout layout) const {
421 return currentBufferItems(GrDrawTarget::VertexSize(layout));
422}
423
424////////////////////////////////////////////////////////////////////////////////
425
426GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu,
427 bool frequentResetHint,
428 size_t bufferSize,
429 int preallocBufferCnt)
430: GrBufferAllocPool(gpu,
431 kIndex_BufferType,
432 frequentResetHint,
433 bufferSize,
434 preallocBufferCnt) {
435}
436
437void* GrIndexBufferAllocPool::makeSpace(int indexCount,
438 const GrIndexBuffer** buffer,
439 int* startIndex) {
440
441 GrAssert(indexCount >= 0);
442 GrAssert(NULL != buffer);
443 GrAssert(NULL != startIndex);
444
bsalomon@google.com8b484412011-04-18 19:07:44 +0000445 size_t offset = 0; // assign to suppress warning
446 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000447 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
448 sizeof(uint16_t),
449 &geomBuffer,
450 &offset);
451
452 *buffer = (const GrIndexBuffer*) geomBuffer;
453 GrAssert(0 == offset % sizeof(uint16_t));
454 *startIndex = offset / sizeof(uint16_t);
455 return ptr;
456}
457
458bool GrIndexBufferAllocPool::appendIndices(int indexCount,
459 const void* indices,
460 const GrIndexBuffer** buffer,
461 int* startIndex) {
462 void* space = makeSpace(indexCount, buffer, startIndex);
463 if (NULL != space) {
464 memcpy(space, indices, sizeof(uint16_t) * indexCount);
465 return true;
466 } else {
467 return false;
468 }
469}
470
471int GrIndexBufferAllocPool::preallocatedBufferIndices() const {
472 return INHERITED::preallocatedBufferSize() / sizeof(uint16_t);
473}
474
475int GrIndexBufferAllocPool::currentBufferIndices() const {
476 return currentBufferItems(sizeof(uint16_t));
477}