blob: 2dbf3eb283fb37ae40b1f6a54e08267d5e7e0048 [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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000011#include "GrDrawTargetCaps.h"
12#include "GrGpu.h"
13#include "GrIndexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000014#include "GrTypes.h"
15#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000017#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018 #define VALIDATE validate
19#else
sugoi@google.come0e385c2013-03-11 18:50:03 +000020 static void VALIDATE(bool = false) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021#endif
22
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000023// page size
bsalomon@google.com1c13c962011-02-14 16:51:21 +000024#define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 12)
25
26GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu,
27 BufferType bufferType,
28 bool frequentResetHint,
29 size_t blockSize,
30 int preallocBufferCnt) :
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000031 fBlocks(SkTMax(8, 2*preallocBufferCnt)) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000032
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +000033 SkASSERT(NULL != gpu);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000034 fGpu = gpu;
bsalomon@google.com11f0b512011-03-29 20:52:23 +000035 fGpu->ref();
36 fGpuIsReffed = true;
37
bsalomon@google.com1c13c962011-02-14 16:51:21 +000038 fBufferType = bufferType;
39 fFrequentResetHint = frequentResetHint;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000040 fBufferPtr = NULL;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000041 fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000042
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000043 fBytesInUse = 0;
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000044
bsalomon@google.com1c13c962011-02-14 16:51:21 +000045 fPreallocBuffersInUse = 0;
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000046 fPreallocBufferStartIdx = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000047 for (int i = 0; i < preallocBufferCnt; ++i) {
48 GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize);
49 if (NULL != buffer) {
50 *fPreallocBuffers.append() = buffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051 }
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 }
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000086 // fPreallocBuffersInUse will be decremented down to zero in the while loop
87 int preallocBuffersInUse = fPreallocBuffersInUse;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000088 while (!fBlocks.empty()) {
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000089 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000090 }
91 if (fPreallocBuffers.count()) {
92 // must set this after above loop.
bsalomon@google.comb665a6b2012-03-01 20:59:28 +000093 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
94 preallocBuffersInUse) %
95 fPreallocBuffers.count();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000096 }
bsalomon@google.com987dbc02011-12-14 14:44:19 +000097 // 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);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000100 SkASSERT(0 == fPreallocBuffersInUse);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 VALIDATE();
102}
103
104void GrBufferAllocPool::unlock() {
105 VALIDATE();
106
107 if (NULL != fBufferPtr) {
108 BufferBlock& block = fBlocks.back();
109 if (block.fBuffer->isLocked()) {
110 block.fBuffer->unlock();
111 } else {
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000112 size_t flushSize = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000113 flushCpuData(fBlocks.back().fBuffer, flushSize);
114 }
115 fBufferPtr = NULL;
116 }
117 VALIDATE();
118}
119
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000120#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000121void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000122 if (NULL != fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000123 SkASSERT(!fBlocks.empty());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 if (fBlocks.back().fBuffer->isLocked()) {
125 GrGeometryBuffer* buf = fBlocks.back().fBuffer;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000126 SkASSERT(buf->lockPtr() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000127 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000128 SkASSERT(fCpuData.get() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000129 }
130 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000131 SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000132 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000133 size_t bytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000134 for (int i = 0; i < fBlocks.count() - 1; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000135 SkASSERT(!fBlocks[i].fBuffer->isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000136 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000137 for (int i = 0; i < fBlocks.count(); ++i) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000138 size_t bytes = fBlocks[i].fBuffer->sizeInBytes() - fBlocks[i].fBytesFree;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000139 bytesInUse += bytes;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000140 SkASSERT(bytes || unusedBlockAllowed);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000141 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000143 SkASSERT(bytesInUse == fBytesInUse);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000144 if (unusedBlockAllowed) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000145 SkASSERT((fBytesInUse && !fBlocks.empty()) ||
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000146 (!fBytesInUse && (fBlocks.count() < 2)));
147 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000148 SkASSERT((0 == fBytesInUse) == fBlocks.empty());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000149 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000150}
151#endif
152
153void* GrBufferAllocPool::makeSpace(size_t size,
154 size_t alignment,
155 const GrGeometryBuffer** buffer,
156 size_t* offset) {
157 VALIDATE();
158
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000159 SkASSERT(NULL != buffer);
160 SkASSERT(NULL != offset);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161
162 if (NULL != fBufferPtr) {
163 BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000164 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000165 size_t pad = GrSizeAlignUpPad(usedBytes,
166 alignment);
167 if ((size + pad) <= back.fBytesFree) {
168 usedBytes += pad;
169 *offset = usedBytes;
170 *buffer = back.fBuffer;
171 back.fBytesFree -= size + pad;
bsalomon@google.comd5108092012-03-08 15:10:39 +0000172 fBytesInUse += size + pad;
173 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000174 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
175 }
176 }
177
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000178 // 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
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000180 // allow the driver to know that previously issued draws won't read from
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000181 // 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
184 // size.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000185
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000186 if (!createBlock(size)) {
187 return NULL;
188 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000189 SkASSERT(NULL != fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000190
191 *offset = 0;
192 BufferBlock& back = fBlocks.back();
193 *buffer = back.fBuffer;
194 back.fBytesFree -= size;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000195 fBytesInUse += size;
196 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000197 return fBufferPtr;
198}
199
200int GrBufferAllocPool::currentBufferItems(size_t itemSize) const {
201 VALIDATE();
202 if (NULL != fBufferPtr) {
203 const BufferBlock& back = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000204 size_t usedBytes = back.fBuffer->sizeInBytes() - back.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000205 size_t pad = GrSizeAlignUpPad(usedBytes, itemSize);
robertphillips@google.comadacc702013-10-14 21:53:24 +0000206 return static_cast<int>((back.fBytesFree - pad) / itemSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000207 } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) {
robertphillips@google.comadacc702013-10-14 21:53:24 +0000208 return static_cast<int>(fMinBlockSize / itemSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000209 }
210 return 0;
211}
212
213int GrBufferAllocPool::preallocatedBuffersRemaining() const {
214 return fPreallocBuffers.count() - fPreallocBuffersInUse;
215}
216
217int GrBufferAllocPool::preallocatedBufferCount() const {
218 return fPreallocBuffers.count();
219}
220
221void GrBufferAllocPool::putBack(size_t bytes) {
222 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000223
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000224 // 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;
229
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000230 while (bytes) {
231 // caller shouldnt try to put back more than they've taken
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000232 SkASSERT(!fBlocks.empty());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000233 BufferBlock& block = fBlocks.back();
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000234 size_t bytesUsed = block.fBuffer->sizeInBytes() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000235 if (bytes >= bytesUsed) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000236 bytes -= bytesUsed;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000237 fBytesInUse -= bytesUsed;
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000238 // 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();
242 }
243 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244 } else {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000245 block.fBytesFree += bytes;
246 fBytesInUse -= bytes;
247 bytes = 0;
248 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000249 }
250 }
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000251 if (!fPreallocBuffersInUse && fPreallocBuffers.count()) {
252 fPreallocBufferStartIdx = (fPreallocBufferStartIdx +
253 preallocBuffersInUse) %
254 fPreallocBuffers.count();
255 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000256 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000257}
258
259bool GrBufferAllocPool::createBlock(size_t requestSize) {
260
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000261 size_t size = SkTMax(requestSize, fMinBlockSize);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000262 SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000263
264 VALIDATE();
265
266 BufferBlock& block = fBlocks.push_back();
267
268 if (size == fMinBlockSize &&
269 fPreallocBuffersInUse < fPreallocBuffers.count()) {
270
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000271 uint32_t nextBuffer = (fPreallocBuffersInUse +
272 fPreallocBufferStartIdx) %
273 fPreallocBuffers.count();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000274 block.fBuffer = fPreallocBuffers[nextBuffer];
275 block.fBuffer->ref();
276 ++fPreallocBuffersInUse;
277 } else {
278 block.fBuffer = this->createBuffer(size);
279 if (NULL == block.fBuffer) {
280 fBlocks.pop_back();
281 return false;
282 }
283 }
284
285 block.fBytesFree = size;
286 if (NULL != fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000287 SkASSERT(fBlocks.count() > 1);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000288 BufferBlock& prev = fBlocks.fromBack(1);
289 if (prev.fBuffer->isLocked()) {
290 prev.fBuffer->unlock();
291 } else {
292 flushCpuData(prev.fBuffer,
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000293 prev.fBuffer->sizeInBytes() - prev.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000294 }
295 fBufferPtr = NULL;
296 }
297
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000298 SkASSERT(NULL == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000299
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000300 // 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();
bsalomon@google.combcce8922013-03-25 15:38:39 +0000306 if (!attemptLock && fGpu->caps()->bufferLockSupport()) {
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000307 if (fFrequentResetHint) {
308 attemptLock = requestSize > GR_GEOM_BUFFER_LOCK_THRESHOLD;
309 } else {
310 attemptLock = size > GR_GEOM_BUFFER_LOCK_THRESHOLD;
311 }
312 }
313
314 if (attemptLock) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000315 fBufferPtr = block.fBuffer->lock();
316 }
317
318 if (NULL == fBufferPtr) {
bsalomon@google.com7d4679a2011-09-02 22:06:24 +0000319 fBufferPtr = fCpuData.reset(size);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000320 }
321
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000322 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000323
324 return true;
325}
326
327void GrBufferAllocPool::destroyBlock() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000328 SkASSERT(!fBlocks.empty());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000329
330 BufferBlock& block = fBlocks.back();
331 if (fPreallocBuffersInUse > 0) {
332 uint32_t prevPreallocBuffer = (fPreallocBuffersInUse +
bsalomon@google.comb665a6b2012-03-01 20:59:28 +0000333 fPreallocBufferStartIdx +
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000334 (fPreallocBuffers.count() - 1)) %
335 fPreallocBuffers.count();
336 if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) {
337 --fPreallocBuffersInUse;
338 }
339 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000340 SkASSERT(!block.fBuffer->isLocked());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 block.fBuffer->unref();
342 fBlocks.pop_back();
343 fBufferPtr = NULL;
344}
345
346void GrBufferAllocPool::flushCpuData(GrGeometryBuffer* buffer,
347 size_t flushSize) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000348 SkASSERT(NULL != buffer);
349 SkASSERT(!buffer->isLocked());
350 SkASSERT(fCpuData.get() == fBufferPtr);
351 SkASSERT(flushSize <= buffer->sizeInBytes());
bsalomon@google.comd5108092012-03-08 15:10:39 +0000352 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000353
bsalomon@google.combcce8922013-03-25 15:38:39 +0000354 if (fGpu->caps()->bufferLockSupport() &&
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000355 flushSize > GR_GEOM_BUFFER_LOCK_THRESHOLD) {
356 void* data = buffer->lock();
357 if (NULL != data) {
358 memcpy(data, fBufferPtr, flushSize);
359 buffer->unlock();
bsalomon@google.com71bd1ef2011-12-12 20:42:26 +0000360 return;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000361 }
362 }
363 buffer->updateData(fBufferPtr, flushSize);
bsalomon@google.comd5108092012-03-08 15:10:39 +0000364 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365}
366
367GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) {
368 if (kIndex_BufferType == fBufferType) {
369 return fGpu->createIndexBuffer(size, true);
370 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000371 SkASSERT(kVertex_BufferType == fBufferType);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000372 return fGpu->createVertexBuffer(size, true);
373 }
374}
375
376////////////////////////////////////////////////////////////////////////////////
377
378GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu,
379 bool frequentResetHint,
380 size_t bufferSize,
381 int preallocBufferCnt)
382: GrBufferAllocPool(gpu,
383 kVertex_BufferType,
384 frequentResetHint,
385 bufferSize,
386 preallocBufferCnt) {
387}
388
jvanverth@google.coma6338982013-01-31 21:34:25 +0000389void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000390 int vertexCount,
391 const GrVertexBuffer** buffer,
392 int* startVertex) {
393
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000394 SkASSERT(vertexCount >= 0);
395 SkASSERT(NULL != buffer);
396 SkASSERT(NULL != startVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000397
bsalomon@google.com8b484412011-04-18 19:07:44 +0000398 size_t offset = 0; // assign to suppress warning
399 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
jvanverth@google.coma6338982013-01-31 21:34:25 +0000400 void* ptr = INHERITED::makeSpace(vertexSize * vertexCount,
401 vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000402 &geomBuffer,
403 &offset);
404
405 *buffer = (const GrVertexBuffer*) geomBuffer;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000406 SkASSERT(0 == offset % vertexSize);
robertphillips@google.comadacc702013-10-14 21:53:24 +0000407 *startVertex = static_cast<int>(offset / vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000408 return ptr;
409}
410
jvanverth@google.coma6338982013-01-31 21:34:25 +0000411bool GrVertexBufferAllocPool::appendVertices(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000412 int vertexCount,
413 const void* vertices,
414 const GrVertexBuffer** buffer,
415 int* startVertex) {
jvanverth@google.coma6338982013-01-31 21:34:25 +0000416 void* space = makeSpace(vertexSize, vertexCount, buffer, startVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000417 if (NULL != space) {
418 memcpy(space,
419 vertices,
jvanverth@google.coma6338982013-01-31 21:34:25 +0000420 vertexSize * vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000421 return true;
422 } else {
423 return false;
424 }
425}
426
jvanverth@google.coma6338982013-01-31 21:34:25 +0000427int GrVertexBufferAllocPool::preallocatedBufferVertices(size_t vertexSize) const {
robertphillips@google.comadacc702013-10-14 21:53:24 +0000428 return static_cast<int>(INHERITED::preallocatedBufferSize() / vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000429}
430
jvanverth@google.coma6338982013-01-31 21:34:25 +0000431int GrVertexBufferAllocPool::currentBufferVertices(size_t vertexSize) const {
432 return currentBufferItems(vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000433}
434
435////////////////////////////////////////////////////////////////////////////////
436
437GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu,
438 bool frequentResetHint,
439 size_t bufferSize,
440 int preallocBufferCnt)
441: GrBufferAllocPool(gpu,
442 kIndex_BufferType,
443 frequentResetHint,
444 bufferSize,
445 preallocBufferCnt) {
446}
447
448void* GrIndexBufferAllocPool::makeSpace(int indexCount,
449 const GrIndexBuffer** buffer,
450 int* startIndex) {
451
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000452 SkASSERT(indexCount >= 0);
453 SkASSERT(NULL != buffer);
454 SkASSERT(NULL != startIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000455
bsalomon@google.com8b484412011-04-18 19:07:44 +0000456 size_t offset = 0; // assign to suppress warning
457 const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000458 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
459 sizeof(uint16_t),
460 &geomBuffer,
461 &offset);
462
463 *buffer = (const GrIndexBuffer*) geomBuffer;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000464 SkASSERT(0 == offset % sizeof(uint16_t));
robertphillips@google.comadacc702013-10-14 21:53:24 +0000465 *startIndex = static_cast<int>(offset / sizeof(uint16_t));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000466 return ptr;
467}
468
469bool GrIndexBufferAllocPool::appendIndices(int indexCount,
470 const void* indices,
471 const GrIndexBuffer** buffer,
472 int* startIndex) {
473 void* space = makeSpace(indexCount, buffer, startIndex);
474 if (NULL != space) {
475 memcpy(space, indices, sizeof(uint16_t) * indexCount);
476 return true;
477 } else {
478 return false;
479 }
480}
481
482int GrIndexBufferAllocPool::preallocatedBufferIndices() const {
robertphillips@google.comadacc702013-10-14 21:53:24 +0000483 return static_cast<int>(INHERITED::preallocatedBufferSize() / sizeof(uint16_t));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000484}
485
486int GrIndexBufferAllocPool::currentBufferIndices() const {
487 return currentBufferItems(sizeof(uint16_t));
488}