blob: e3f30b0c1c57bce3e8f66501ee6559cb3fe36985 [file] [log] [blame]
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2010 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
bsalomon@google.com1c13c962011-02-14 16:51:21 +00006 */
7
epoger@google.comec3ed6a2011-07-28 14:26:00 +00008
bsalomon@google.com1c13c962011-02-14 16:51:21 +00009#include "GrBufferAllocPool.h"
cdalton397536c2016-03-25 12:15:03 -070010#include "GrBuffer.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
robertphillips1b8e1b52015-06-24 06:54:10 -070012#include "GrContext.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrGpu.h"
robertphillips1b8e1b52015-06-24 06:54:10 -070014#include "GrResourceProvider.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000015#include "GrTypes.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016
bsalomon3512eda2014-06-26 12:56:22 -070017#include "SkTraceEvent.h"
18
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000019#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020 #define VALIDATE validate
21#else
sugoi@google.come0e385c2013-03-11 18:50:03 +000022 static void VALIDATE(bool = false) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000023#endif
24
robertphillips1b8e1b52015-06-24 06:54:10 -070025static const size_t MIN_VERTEX_BUFFER_SIZE = 1 << 15;
26static const size_t MIN_INDEX_BUFFER_SIZE = 1 << 12;
27
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000028// page size
joshualitt0709ca02015-06-04 09:13:46 -070029#define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 15)
bsalomon@google.com1c13c962011-02-14 16:51:21 +000030
bsalomon3512eda2014-06-26 12:56:22 -070031#define UNMAP_BUFFER(block) \
32do { \
33 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), \
34 "GrBufferAllocPool Unmapping Buffer", \
35 TRACE_EVENT_SCOPE_THREAD, \
36 "percent_unwritten", \
37 (float)((block).fBytesFree) / (block).fBuffer->gpuMemorySize()); \
38 (block).fBuffer->unmap(); \
39} while (false)
40
bsalomon@google.com1c13c962011-02-14 16:51:21 +000041GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu,
cdalton397536c2016-03-25 12:15:03 -070042 GrBufferType bufferType,
robertphillips1b8e1b52015-06-24 06:54:10 -070043 size_t blockSize)
44 : fBlocks(8) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000045
bsalomonecb8e3e2015-04-29 04:33:52 -070046 fGpu = SkRef(gpu);
bsalomon7dea7b72015-08-19 08:26:51 -070047 fCpuData = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048 fBufferType = bufferType;
bsalomon7dea7b72015-08-19 08:26:51 -070049 fBufferPtr = nullptr;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000050 fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000052 fBytesInUse = 0;
robertphillipseea2ff72015-05-14 05:24:53 -070053
cdalton397536c2016-03-25 12:15:03 -070054 fBufferMapThreshold = gpu->caps()->bufferMapThreshold();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000055}
56
robertphillips1b8e1b52015-06-24 06:54:10 -070057void GrBufferAllocPool::deleteBlocks() {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000058 if (fBlocks.count()) {
cdalton397536c2016-03-25 12:15:03 -070059 GrBuffer* buffer = fBlocks.back().fBuffer;
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000060 if (buffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -070061 UNMAP_BUFFER(fBlocks.back());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000062 }
63 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000064 while (!fBlocks.empty()) {
robertphillips91d06bc2015-05-06 04:38:36 -070065 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000066 }
robertphillips1b8e1b52015-06-24 06:54:10 -070067 SkASSERT(!fBufferPtr);
68}
69
70GrBufferAllocPool::~GrBufferAllocPool() {
71 VALIDATE();
72 this->deleteBlocks();
bsalomon7dea7b72015-08-19 08:26:51 -070073 sk_free(fCpuData);
bsalomonecb8e3e2015-04-29 04:33:52 -070074 fGpu->unref();
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;
robertphillips1b8e1b52015-06-24 06:54:10 -070080 this->deleteBlocks();
robertphillips48d91b52016-08-18 14:01:14 -070081 this->resetCpuData(0); // delete all the cpu-side memory
bsalomon@google.com1c13c962011-02-14 16:51:21 +000082 VALIDATE();
83}
84
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000085void GrBufferAllocPool::unmap() {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000086 VALIDATE();
87
bsalomon49f085d2014-09-05 13:34:00 -070088 if (fBufferPtr) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000089 BufferBlock& block = fBlocks.back();
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000090 if (block.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -070091 UNMAP_BUFFER(block);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000092 } else {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000093 size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree;
bsalomon3512eda2014-06-26 12:56:22 -070094 this->flushCpuData(fBlocks.back(), flushSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000095 }
bsalomon7dea7b72015-08-19 08:26:51 -070096 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 }
98 VALIDATE();
99}
100
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000101#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000102void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800103 bool wasDestroyed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700104 if (fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000105 SkASSERT(!fBlocks.empty());
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000106 if (fBlocks.back().fBuffer->isMapped()) {
cdalton397536c2016-03-25 12:15:03 -0700107 GrBuffer* buf = fBlocks.back().fBuffer;
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000108 SkASSERT(buf->mapPtr() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000109 } else {
bsalomon7dea7b72015-08-19 08:26:51 -0700110 SkASSERT(fCpuData == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000111 }
112 } else {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000113 SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000114 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000115 size_t bytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 for (int i = 0; i < fBlocks.count() - 1; ++i) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000117 SkASSERT(!fBlocks[i].fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000118 }
bsalomon71cb0c22014-11-14 12:10:14 -0800119 for (int i = 0; !wasDestroyed && i < fBlocks.count(); ++i) {
120 if (fBlocks[i].fBuffer->wasDestroyed()) {
121 wasDestroyed = true;
122 } else {
123 size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree;
124 bytesInUse += bytes;
125 SkASSERT(bytes || unusedBlockAllowed);
126 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000127 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128
bsalomon71cb0c22014-11-14 12:10:14 -0800129 if (!wasDestroyed) {
130 SkASSERT(bytesInUse == fBytesInUse);
131 if (unusedBlockAllowed) {
132 SkASSERT((fBytesInUse && !fBlocks.empty()) ||
133 (!fBytesInUse && (fBlocks.count() < 2)));
134 } else {
135 SkASSERT((0 == fBytesInUse) == fBlocks.empty());
136 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000137 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000138}
139#endif
140
141void* GrBufferAllocPool::makeSpace(size_t size,
142 size_t alignment,
cdalton397536c2016-03-25 12:15:03 -0700143 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000144 size_t* offset) {
145 VALIDATE();
146
bsalomon49f085d2014-09-05 13:34:00 -0700147 SkASSERT(buffer);
148 SkASSERT(offset);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000149
bsalomon49f085d2014-09-05 13:34:00 -0700150 if (fBufferPtr) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000151 BufferBlock& back = fBlocks.back();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000152 size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree;
robertphillips1b8e1b52015-06-24 06:54:10 -0700153 size_t pad = GrSizeAlignUpPad(usedBytes, alignment);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000154 if ((size + pad) <= back.fBytesFree) {
dongseong.hwang8f25c662015-01-22 10:40:20 -0800155 memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000156 usedBytes += pad;
157 *offset = usedBytes;
158 *buffer = back.fBuffer;
159 back.fBytesFree -= size + pad;
bsalomon@google.comd5108092012-03-08 15:10:39 +0000160 fBytesInUse += size + pad;
161 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000162 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
163 }
164 }
165
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000166 // We could honor the space request using by a partial update of the current
167 // VB (if there is room). But we don't currently use draw calls to GL that
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000168 // allow the driver to know that previously issued draws won't read from
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000169 // the part of the buffer we update. Also, the GL buffer implementation
170 // may be cheating on the actual buffer size by shrinking the buffer on
171 // updateData() if the amount of data passed is less than the full buffer
172 // size.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000173
robertphillips91d06bc2015-05-06 04:38:36 -0700174 if (!this->createBlock(size)) {
bsalomon7dea7b72015-08-19 08:26:51 -0700175 return nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000176 }
bsalomon49f085d2014-09-05 13:34:00 -0700177 SkASSERT(fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000178
179 *offset = 0;
180 BufferBlock& back = fBlocks.back();
181 *buffer = back.fBuffer;
182 back.fBytesFree -= size;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000183 fBytesInUse += size;
184 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000185 return fBufferPtr;
186}
187
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000188void GrBufferAllocPool::putBack(size_t bytes) {
189 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000190
191 while (bytes) {
robertphillips91d06bc2015-05-06 04:38:36 -0700192 // caller shouldn't try to put back more than they've taken
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000193 SkASSERT(!fBlocks.empty());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000194 BufferBlock& block = fBlocks.back();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000195 size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000196 if (bytes >= bytesUsed) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000197 bytes -= bytesUsed;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000198 fBytesInUse -= bytesUsed;
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000199 // if we locked a vb to satisfy the make space and we're releasing
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000200 // beyond it, then unmap it.
201 if (block.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -0700202 UNMAP_BUFFER(block);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000203 }
204 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000205 } else {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000206 block.fBytesFree += bytes;
207 fBytesInUse -= bytes;
208 bytes = 0;
209 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000210 }
211 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700212
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000213 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000214}
215
216bool GrBufferAllocPool::createBlock(size_t requestSize) {
217
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000218 size_t size = SkTMax(requestSize, fMinBlockSize);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000219 SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000220
221 VALIDATE();
222
223 BufferBlock& block = fBlocks.push_back();
224
robertphillips1b8e1b52015-06-24 06:54:10 -0700225 block.fBuffer = this->getBuffer(size);
bsalomon7dea7b72015-08-19 08:26:51 -0700226 if (!block.fBuffer) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700227 fBlocks.pop_back();
228 return false;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000229 }
230
robertphillips1b8e1b52015-06-24 06:54:10 -0700231 block.fBytesFree = block.fBuffer->gpuMemorySize();
bsalomon49f085d2014-09-05 13:34:00 -0700232 if (fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000233 SkASSERT(fBlocks.count() > 1);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000234 BufferBlock& prev = fBlocks.fromBack(1);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000235 if (prev.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -0700236 UNMAP_BUFFER(prev);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000237 } else {
bsalomon3512eda2014-06-26 12:56:22 -0700238 this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239 }
bsalomon7dea7b72015-08-19 08:26:51 -0700240 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000241 }
242
bsalomon7dea7b72015-08-19 08:26:51 -0700243 SkASSERT(!fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000245 // If the buffer is CPU-backed we map it because it is free to do so and saves a copy.
bsalomonecb8e3e2015-04-29 04:33:52 -0700246 // Otherwise when buffer mapping is supported we map if the buffer size is greater than the
247 // threshold.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000248 bool attemptMap = block.fBuffer->isCPUBacked();
bsalomon4b91f762015-05-19 09:29:46 -0700249 if (!attemptMap && GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) {
cdalton397536c2016-03-25 12:15:03 -0700250 attemptMap = size > fBufferMapThreshold;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000251 }
252
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000253 if (attemptMap) {
254 fBufferPtr = block.fBuffer->map();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000255 }
256
bsalomon7dea7b72015-08-19 08:26:51 -0700257 if (!fBufferPtr) {
258 fBufferPtr = this->resetCpuData(block.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000259 }
260
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000261 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000262
263 return true;
264}
265
266void GrBufferAllocPool::destroyBlock() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000267 SkASSERT(!fBlocks.empty());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000268
269 BufferBlock& block = fBlocks.back();
robertphillips1b8e1b52015-06-24 06:54:10 -0700270
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000271 SkASSERT(!block.fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000272 block.fBuffer->unref();
273 fBlocks.pop_back();
bsalomon7dea7b72015-08-19 08:26:51 -0700274 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000275}
276
bsalomon7dea7b72015-08-19 08:26:51 -0700277void* GrBufferAllocPool::resetCpuData(size_t newSize) {
278 sk_free(fCpuData);
279 if (newSize) {
280 if (fGpu->caps()->mustClearUploadedBufferData()) {
mtkleinabda35d2016-07-14 06:57:31 -0700281 fCpuData = sk_calloc_throw(newSize);
bsalomon7dea7b72015-08-19 08:26:51 -0700282 } else {
283 fCpuData = sk_malloc_throw(newSize);
284 }
285 } else {
286 fCpuData = nullptr;
287 }
288 return fCpuData;
289}
290
291
bsalomon3512eda2014-06-26 12:56:22 -0700292void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) {
cdalton397536c2016-03-25 12:15:03 -0700293 GrBuffer* buffer = block.fBuffer;
bsalomon49f085d2014-09-05 13:34:00 -0700294 SkASSERT(buffer);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000295 SkASSERT(!buffer->isMapped());
bsalomon7dea7b72015-08-19 08:26:51 -0700296 SkASSERT(fCpuData == fBufferPtr);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000297 SkASSERT(flushSize <= buffer->gpuMemorySize());
bsalomon@google.comd5108092012-03-08 15:10:39 +0000298 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000299
bsalomon4b91f762015-05-19 09:29:46 -0700300 if (GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() &&
cdalton397536c2016-03-25 12:15:03 -0700301 flushSize > fBufferMapThreshold) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000302 void* data = buffer->map();
bsalomon49f085d2014-09-05 13:34:00 -0700303 if (data) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000304 memcpy(data, fBufferPtr, flushSize);
bsalomon3512eda2014-06-26 12:56:22 -0700305 UNMAP_BUFFER(block);
bsalomon@google.com71bd1ef2011-12-12 20:42:26 +0000306 return;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000307 }
308 }
309 buffer->updateData(fBufferPtr, flushSize);
bsalomon@google.comd5108092012-03-08 15:10:39 +0000310 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000311}
312
cdalton397536c2016-03-25 12:15:03 -0700313GrBuffer* GrBufferAllocPool::getBuffer(size_t size) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700314
315 GrResourceProvider* rp = fGpu->getContext()->resourceProvider();
316
halcanary6950de62015-11-07 05:29:00 -0800317 // Shouldn't have to use this flag (https://bug.skia.org/4156)
bsalomoneae62002015-07-31 13:59:30 -0700318 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
cdaltone2e71c22016-04-07 18:13:29 -0700319 return rp->createBuffer(size, fBufferType, kDynamic_GrAccessPattern, kFlags);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000320}
321
322////////////////////////////////////////////////////////////////////////////////
323
robertphillips1b8e1b52015-06-24 06:54:10 -0700324GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu)
cdalton397536c2016-03-25 12:15:03 -0700325 : GrBufferAllocPool(gpu, kVertex_GrBufferType, MIN_VERTEX_BUFFER_SIZE) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000326}
327
jvanverth@google.coma6338982013-01-31 21:34:25 +0000328void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000329 int vertexCount,
cdalton397536c2016-03-25 12:15:03 -0700330 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000331 int* startVertex) {
332
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000333 SkASSERT(vertexCount >= 0);
bsalomon49f085d2014-09-05 13:34:00 -0700334 SkASSERT(buffer);
335 SkASSERT(startVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000336
bsalomon@google.com8b484412011-04-18 19:07:44 +0000337 size_t offset = 0; // assign to suppress warning
jvanverth@google.coma6338982013-01-31 21:34:25 +0000338 void* ptr = INHERITED::makeSpace(vertexSize * vertexCount,
339 vertexSize,
cdalton397536c2016-03-25 12:15:03 -0700340 buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000341 &offset);
342
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000343 SkASSERT(0 == offset % vertexSize);
robertphillips@google.comadacc702013-10-14 21:53:24 +0000344 *startVertex = static_cast<int>(offset / vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000345 return ptr;
346}
347
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000348////////////////////////////////////////////////////////////////////////////////
349
robertphillips1b8e1b52015-06-24 06:54:10 -0700350GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu)
cdalton397536c2016-03-25 12:15:03 -0700351 : GrBufferAllocPool(gpu, kIndex_GrBufferType, MIN_INDEX_BUFFER_SIZE) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000352}
353
354void* GrIndexBufferAllocPool::makeSpace(int indexCount,
cdalton397536c2016-03-25 12:15:03 -0700355 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000356 int* startIndex) {
357
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000358 SkASSERT(indexCount >= 0);
bsalomon49f085d2014-09-05 13:34:00 -0700359 SkASSERT(buffer);
360 SkASSERT(startIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000361
bsalomon@google.com8b484412011-04-18 19:07:44 +0000362 size_t offset = 0; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000363 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
364 sizeof(uint16_t),
cdalton397536c2016-03-25 12:15:03 -0700365 buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000366 &offset);
367
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000368 SkASSERT(0 == offset % sizeof(uint16_t));
robertphillips@google.comadacc702013-10-14 21:53:24 +0000369 *startIndex = static_cast<int>(offset / sizeof(uint16_t));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000370 return ptr;
371}