3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 #include "GrBufferAllocPool.h"
11 #include "GrDrawTargetCaps.h"
13 #include "GrIndexBuffer.h"
15 #include "GrVertexBuffer.h"
18 #define VALIDATE validate
20 static void VALIDATE(bool = false) {}
24 #define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 12)
26 GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu,
27 BufferType bufferType,
28 bool frequentResetHint,
30 int preallocBufferCnt) :
31 fBlocks(SkTMax(8, 2*preallocBufferCnt)) {
33 SkASSERT(NULL != gpu);
38 fBufferType = bufferType;
39 fFrequentResetHint = frequentResetHint;
41 fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize);
45 fPreallocBuffersInUse = 0;
46 fPreallocBufferStartIdx = 0;
47 for (int i = 0; i < preallocBufferCnt; ++i) {
48 GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize);
50 *fPreallocBuffers.append() = buffer;
55 GrBufferAllocPool::~GrBufferAllocPool() {
57 if (fBlocks.count()) {
58 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
59 if (buffer->isLocked()) {
63 while (!fBlocks.empty()) {
66 fPreallocBuffers.unrefAll();
70 void GrBufferAllocPool::releaseGpuRef() {
77 void GrBufferAllocPool::reset() {
80 if (fBlocks.count()) {
81 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
82 if (buffer->isLocked()) {
86 // fPreallocBuffersInUse will be decremented down to zero in the while loop
87 int preallocBuffersInUse = fPreallocBuffersInUse;
88 while (!fBlocks.empty()) {
91 if (fPreallocBuffers.count()) {
92 // must set this after above loop.
93 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
94 preallocBuffersInUse) %
95 fPreallocBuffers.count();
97 // we may have created a large cpu mirror of a large VB. Reset the size
98 // to match our pre-allocated VBs.
99 fCpuData.reset(fMinBlockSize);
100 SkASSERT(0 == fPreallocBuffersInUse);
104 void GrBufferAllocPool::unlock() {
107 if (NULL != fBufferPtr) {
108 BufferBlock& block = fBlocks.back();
109 if (block.fBuffer->isLocked()) {
110 block.fBuffer->unlock();
112 size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree;
113 flushCpuData(fBlocks.back().fBuffer, flushSize);
121 void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
122 if (NULL != fBufferPtr) {
123 SkASSERT(!fBlocks.empty());
124 if (fBlocks.back().fBuffer->isLocked()) {
125 GrGeometryBuffer* buf = fBlocks.back().fBuffer;
126 SkASSERT(buf->lockPtr() == fBufferPtr);
128 SkASSERT(fCpuData.get() == fBufferPtr);
131 SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isLocked());
133 size_t bytesInUse = 0;
134 for (int i = 0; i < fBlocks.count() - 1; ++i) {
135 SkASSERT(!fBlocks[i].fBuffer->isLocked());
137 for (int i = 0; i < fBlocks.count(); ++i) {
138 size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree;
140 SkASSERT(bytes || unusedBlockAllowed);
143 SkASSERT(bytesInUse == fBytesInUse);
144 if (unusedBlockAllowed) {
145 SkASSERT((fBytesInUse && !fBlocks.empty()) ||
146 (!fBytesInUse && (fBlocks.count() < 2)));
148 SkASSERT((0 == fBytesInUse) == fBlocks.empty());
153 void* GrBufferAllocPool::makeSpace(size_t size,
155 const GrGeometryBuffer** buffer,
159 SkASSERT(NULL != buffer);
160 SkASSERT(NULL != offset);
162 if (NULL != fBufferPtr) {
163 BufferBlock& back = fBlocks.back();
164 size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree;
165 size_t pad = GrSizeAlignUpPad(usedBytes,
167 if ((size + pad) <= back.fBytesFree) {
170 *buffer = back.fBuffer;
171 back.fBytesFree -= size + pad;
172 fBytesInUse += size + pad;
174 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
178 // We could honor the space request using by a partial update of the current
179 // VB (if there is room). But we don't currently use draw calls to GL that
180 // allow the driver to know that previously issued draws won't read from
181 // the part of the buffer we update. Also, the GL buffer implementation
182 // may be cheating on the actual buffer size by shrinking the buffer on
183 // updateData() if the amount of data passed is less than the full buffer
186 if (!createBlock(size)) {
189 SkASSERT(NULL != fBufferPtr);
192 BufferBlock& back = fBlocks.back();
193 *buffer = back.fBuffer;
194 back.fBytesFree -= size;
200 int GrBufferAllocPool::currentBufferItems(size_t itemSize) const {
202 if (NULL != fBufferPtr) {
203 const BufferBlock& back = fBlocks.back();
204 size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree;
205 size_t pad = GrSizeAlignUpPad(usedBytes, itemSize);
206 return static_cast<int>((back.fBytesFree - pad) / itemSize);
207 } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) {
208 return static_cast<int>(fMinBlockSize / itemSize);
213 int GrBufferAllocPool::preallocatedBuffersRemaining() const {
214 return fPreallocBuffers.count() - fPreallocBuffersInUse;
217 int GrBufferAllocPool::preallocatedBufferCount() const {
218 return fPreallocBuffers.count();
221 void GrBufferAllocPool::putBack(size_t bytes) {
224 // if the putBack unwinds all the preallocated buffers then we will
225 // advance the starting index. As blocks are destroyed fPreallocBuffersInUse
226 // will be decremented. I will reach zero if all blocks using preallocated
227 // buffers are released.
228 int preallocBuffersInUse = fPreallocBuffersInUse;
231 // caller shouldnt try to put back more than they've taken
232 SkASSERT(!fBlocks.empty());
233 BufferBlock& block = fBlocks.back();
234 size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree;
235 if (bytes >= bytesUsed) {
237 fBytesInUse -= bytesUsed;
238 // if we locked a vb to satisfy the make space and we're releasing
239 // beyond it, then unlock it.
240 if (block.fBuffer->isLocked()) {
241 block.fBuffer->unlock();
243 this->destroyBlock();
245 block.fBytesFree += bytes;
246 fBytesInUse -= bytes;
251 if (!fPreallocBuffersInUse && fPreallocBuffers.count()) {
252 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
253 preallocBuffersInUse) %
254 fPreallocBuffers.count();
259 bool GrBufferAllocPool::createBlock(size_t requestSize) {
261 size_t size = SkTMax(requestSize, fMinBlockSize);
262 SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
266 BufferBlock& block = fBlocks.push_back();
268 if (size == fMinBlockSize &&
269 fPreallocBuffersInUse < fPreallocBuffers.count()) {
271 uint32_t nextBuffer = (fPreallocBuffersInUse +
272 fPreallocBufferStartIdx) %
273 fPreallocBuffers.count();
274 block.fBuffer = fPreallocBuffers[nextBuffer];
275 block.fBuffer->ref();
276 ++fPreallocBuffersInUse;
278 block.fBuffer = this->createBuffer(size);
279 if (NULL == block.fBuffer) {
285 block.fBytesFree = size;
286 if (NULL != fBufferPtr) {
287 SkASSERT(fBlocks.count() > 1);
288 BufferBlock& prev = fBlocks.fromBack(1);
289 if (prev.fBuffer->isLocked()) {
290 prev.fBuffer->unlock();
292 flushCpuData(prev.fBuffer,
293 prev.fBuffer->gpuMemorySize() - prev.fBytesFree);
298 SkASSERT(NULL == fBufferPtr);
300 // If the buffer is CPU-backed we lock it because it is free to do so and saves a copy.
301 // Otherwise when buffer locking is supported:
302 // a) If the frequently reset hint is set we only lock when the requested size meets a
303 // threshold (since we don't expect it is likely that we will see more vertex data)
304 // b) If the hint is not set we lock if the buffer size is greater than the threshold.
305 bool attemptLock = block.fBuffer->isCPUBacked();
306 if (!attemptLock && GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) {
307 if (fFrequentResetHint) {
308 attemptLock = requestSize > GR_GEOM_BUFFER_LOCK_THRESHOLD;
310 attemptLock = size > GR_GEOM_BUFFER_LOCK_THRESHOLD;
315 fBufferPtr = block.fBuffer->lock();
318 if (NULL == fBufferPtr) {
319 fBufferPtr = fCpuData.reset(size);
327 void GrBufferAllocPool::destroyBlock() {
328 SkASSERT(!fBlocks.empty());
330 BufferBlock& block = fBlocks.back();
331 if (fPreallocBuffersInUse > 0) {
332 uint32_t prevPreallocBuffer = (fPreallocBuffersInUse +
333 fPreallocBufferStartIdx +
334 (fPreallocBuffers.count() - 1)) %
335 fPreallocBuffers.count();
336 if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) {
337 --fPreallocBuffersInUse;
340 SkASSERT(!block.fBuffer->isLocked());
341 block.fBuffer->unref();
346 void GrBufferAllocPool::flushCpuData(GrGeometryBuffer* buffer,
348 SkASSERT(NULL != buffer);
349 SkASSERT(!buffer->isLocked());
350 SkASSERT(fCpuData.get() == fBufferPtr);
351 SkASSERT(flushSize <= buffer->gpuMemorySize());
354 if (GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() &&
355 flushSize > GR_GEOM_BUFFER_LOCK_THRESHOLD) {
356 void* data = buffer->lock();
358 memcpy(data, fBufferPtr, flushSize);
363 buffer->updateData(fBufferPtr, flushSize);
367 GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) {
368 if (kIndex_BufferType == fBufferType) {
369 return fGpu->createIndexBuffer(size, true);
371 SkASSERT(kVertex_BufferType == fBufferType);
372 return fGpu->createVertexBuffer(size, true);
376 ////////////////////////////////////////////////////////////////////////////////
378 GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu,
379 bool frequentResetHint,
381 int preallocBufferCnt)
382 : GrBufferAllocPool(gpu,
389 void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize,
391 const GrVertexBuffer** buffer,
394 SkASSERT(vertexCount >= 0);
395 SkASSERT(NULL != buffer);
396 SkASSERT(NULL != startVertex);
398 size_t offset = 0; // assign to suppress warning
399 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
400 void* ptr = INHERITED::makeSpace(vertexSize * vertexCount,
405 *buffer = (const GrVertexBuffer*) geomBuffer;
406 SkASSERT(0 == offset % vertexSize);
407 *startVertex = static_cast<int>(offset / vertexSize);
411 bool GrVertexBufferAllocPool::appendVertices(size_t vertexSize,
413 const void* vertices,
414 const GrVertexBuffer** buffer,
416 void* space = makeSpace(vertexSize, vertexCount, buffer, startVertex);
420 vertexSize * vertexCount);
427 int GrVertexBufferAllocPool::preallocatedBufferVertices(size_t vertexSize) const {
428 return static_cast<int>(INHERITED::preallocatedBufferSize() / vertexSize);
431 int GrVertexBufferAllocPool::currentBufferVertices(size_t vertexSize) const {
432 return currentBufferItems(vertexSize);
435 ////////////////////////////////////////////////////////////////////////////////
437 GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu,
438 bool frequentResetHint,
440 int preallocBufferCnt)
441 : GrBufferAllocPool(gpu,
448 void* GrIndexBufferAllocPool::makeSpace(int indexCount,
449 const GrIndexBuffer** buffer,
452 SkASSERT(indexCount >= 0);
453 SkASSERT(NULL != buffer);
454 SkASSERT(NULL != startIndex);
456 size_t offset = 0; // assign to suppress warning
457 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
458 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
463 *buffer = (const GrIndexBuffer*) geomBuffer;
464 SkASSERT(0 == offset % sizeof(uint16_t));
465 *startIndex = static_cast<int>(offset / sizeof(uint16_t));
469 bool GrIndexBufferAllocPool::appendIndices(int indexCount,
471 const GrIndexBuffer** buffer,
473 void* space = makeSpace(indexCount, buffer, startIndex);
475 memcpy(space, indices, sizeof(uint16_t) * indexCount);
482 int GrIndexBufferAllocPool::preallocatedBufferIndices() const {
483 return static_cast<int>(INHERITED::preallocatedBufferSize() / sizeof(uint16_t));
486 int GrIndexBufferAllocPool::currentBufferIndices() const {
487 return currentBufferItems(sizeof(uint16_t));