blob: 210ab44c687ea516ceac296eb0738a8bef2f9bc6 [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"
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"
14#include "GrIndexBuffer.h"
robertphillips1b8e1b52015-06-24 06:54:10 -070015#include "GrResourceProvider.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016#include "GrTypes.h"
17#include "GrVertexBuffer.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018
bsalomon3512eda2014-06-26 12:56:22 -070019#include "SkTraceEvent.h"
20
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +000021#ifdef SK_DEBUG
bsalomon@google.com1c13c962011-02-14 16:51:21 +000022 #define VALIDATE validate
23#else
sugoi@google.come0e385c2013-03-11 18:50:03 +000024 static void VALIDATE(bool = false) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000025#endif
26
robertphillips1b8e1b52015-06-24 06:54:10 -070027static const size_t MIN_VERTEX_BUFFER_SIZE = 1 << 15;
28static const size_t MIN_INDEX_BUFFER_SIZE = 1 << 12;
29
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000030// page size
joshualitt0709ca02015-06-04 09:13:46 -070031#define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 15)
bsalomon@google.com1c13c962011-02-14 16:51:21 +000032
bsalomon3512eda2014-06-26 12:56:22 -070033#define UNMAP_BUFFER(block) \
34do { \
35 TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), \
36 "GrBufferAllocPool Unmapping Buffer", \
37 TRACE_EVENT_SCOPE_THREAD, \
38 "percent_unwritten", \
39 (float)((block).fBytesFree) / (block).fBuffer->gpuMemorySize()); \
40 (block).fBuffer->unmap(); \
41} while (false)
42
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu,
44 BufferType bufferType,
robertphillips1b8e1b52015-06-24 06:54:10 -070045 size_t blockSize)
46 : fBlocks(8) {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000047
bsalomonecb8e3e2015-04-29 04:33:52 -070048 fGpu = SkRef(gpu);
bsalomon7dea7b72015-08-19 08:26:51 -070049 fCpuData = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000050 fBufferType = bufferType;
bsalomon7dea7b72015-08-19 08:26:51 -070051 fBufferPtr = nullptr;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000052 fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000053
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000054 fBytesInUse = 0;
robertphillipseea2ff72015-05-14 05:24:53 -070055
joshualitt7224c862015-05-29 06:46:47 -070056 fGeometryBufferMapThreshold = gpu->caps()->geometryBufferMapThreshold();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000057}
58
robertphillips1b8e1b52015-06-24 06:54:10 -070059void GrBufferAllocPool::deleteBlocks() {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000060 if (fBlocks.count()) {
61 GrGeometryBuffer* buffer = fBlocks.back().fBuffer;
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000062 if (buffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -070063 UNMAP_BUFFER(fBlocks.back());
bsalomon@google.com1c13c962011-02-14 16:51:21 +000064 }
65 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +000066 while (!fBlocks.empty()) {
robertphillips91d06bc2015-05-06 04:38:36 -070067 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000068 }
robertphillips1b8e1b52015-06-24 06:54:10 -070069 SkASSERT(!fBufferPtr);
70}
71
72GrBufferAllocPool::~GrBufferAllocPool() {
73 VALIDATE();
74 this->deleteBlocks();
bsalomon7dea7b72015-08-19 08:26:51 -070075 sk_free(fCpuData);
bsalomonecb8e3e2015-04-29 04:33:52 -070076 fGpu->unref();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000077}
78
79void GrBufferAllocPool::reset() {
80 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +000081 fBytesInUse = 0;
robertphillips1b8e1b52015-06-24 06:54:10 -070082 this->deleteBlocks();
bsalomon7dea7b72015-08-19 08:26:51 -070083
84 // we may have created a large cpu mirror of a large VB. Reset the size to match our minimum.
85 this->resetCpuData(fMinBlockSize);
86
bsalomon@google.com1c13c962011-02-14 16:51:21 +000087 VALIDATE();
88}
89
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000090void GrBufferAllocPool::unmap() {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000091 VALIDATE();
92
bsalomon49f085d2014-09-05 13:34:00 -070093 if (fBufferPtr) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000094 BufferBlock& block = fBlocks.back();
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000095 if (block.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -070096 UNMAP_BUFFER(block);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 } else {
commit-bot@chromium.org089a7802014-05-02 21:38:22 +000098 size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree;
bsalomon3512eda2014-06-26 12:56:22 -070099 this->flushCpuData(fBlocks.back(), flushSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 }
bsalomon7dea7b72015-08-19 08:26:51 -0700101 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000102 }
103 VALIDATE();
104}
105
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000106#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000107void GrBufferAllocPool::validate(bool unusedBlockAllowed) const {
bsalomon71cb0c22014-11-14 12:10:14 -0800108 bool wasDestroyed = false;
bsalomon49f085d2014-09-05 13:34:00 -0700109 if (fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000110 SkASSERT(!fBlocks.empty());
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000111 if (fBlocks.back().fBuffer->isMapped()) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 GrGeometryBuffer* buf = fBlocks.back().fBuffer;
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000113 SkASSERT(buf->mapPtr() == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000114 } else {
bsalomon7dea7b72015-08-19 08:26:51 -0700115 SkASSERT(fCpuData == fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000116 }
117 } else {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000118 SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000119 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000120 size_t bytesInUse = 0;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000121 for (int i = 0; i < fBlocks.count() - 1; ++i) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000122 SkASSERT(!fBlocks[i].fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000123 }
bsalomon71cb0c22014-11-14 12:10:14 -0800124 for (int i = 0; !wasDestroyed && i < fBlocks.count(); ++i) {
125 if (fBlocks[i].fBuffer->wasDestroyed()) {
126 wasDestroyed = true;
127 } else {
128 size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree;
129 bytesInUse += bytes;
130 SkASSERT(bytes || unusedBlockAllowed);
131 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000132 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000133
bsalomon71cb0c22014-11-14 12:10:14 -0800134 if (!wasDestroyed) {
135 SkASSERT(bytesInUse == fBytesInUse);
136 if (unusedBlockAllowed) {
137 SkASSERT((fBytesInUse && !fBlocks.empty()) ||
138 (!fBytesInUse && (fBlocks.count() < 2)));
139 } else {
140 SkASSERT((0 == fBytesInUse) == fBlocks.empty());
141 }
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000142 }
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000143}
144#endif
145
146void* GrBufferAllocPool::makeSpace(size_t size,
147 size_t alignment,
148 const GrGeometryBuffer** buffer,
149 size_t* offset) {
150 VALIDATE();
151
bsalomon49f085d2014-09-05 13:34:00 -0700152 SkASSERT(buffer);
153 SkASSERT(offset);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000154
bsalomon49f085d2014-09-05 13:34:00 -0700155 if (fBufferPtr) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000156 BufferBlock& back = fBlocks.back();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000157 size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree;
robertphillips1b8e1b52015-06-24 06:54:10 -0700158 size_t pad = GrSizeAlignUpPad(usedBytes, alignment);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159 if ((size + pad) <= back.fBytesFree) {
dongseong.hwang8f25c662015-01-22 10:40:20 -0800160 memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161 usedBytes += pad;
162 *offset = usedBytes;
163 *buffer = back.fBuffer;
164 back.fBytesFree -= size + pad;
bsalomon@google.comd5108092012-03-08 15:10:39 +0000165 fBytesInUse += size + pad;
166 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000167 return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes);
168 }
169 }
170
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000171 // We could honor the space request using by a partial update of the current
172 // VB (if there is room). But we don't currently use draw calls to GL that
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000173 // allow the driver to know that previously issued draws won't read from
bsalomon@google.com96e96df2011-10-10 14:49:29 +0000174 // the part of the buffer we update. Also, the GL buffer implementation
175 // may be cheating on the actual buffer size by shrinking the buffer on
176 // updateData() if the amount of data passed is less than the full buffer
177 // size.
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000178
robertphillips91d06bc2015-05-06 04:38:36 -0700179 if (!this->createBlock(size)) {
bsalomon7dea7b72015-08-19 08:26:51 -0700180 return nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000181 }
bsalomon49f085d2014-09-05 13:34:00 -0700182 SkASSERT(fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000183
184 *offset = 0;
185 BufferBlock& back = fBlocks.back();
186 *buffer = back.fBuffer;
187 back.fBytesFree -= size;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000188 fBytesInUse += size;
189 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000190 return fBufferPtr;
191}
192
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000193void GrBufferAllocPool::putBack(size_t bytes) {
194 VALIDATE();
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000195
196 while (bytes) {
robertphillips91d06bc2015-05-06 04:38:36 -0700197 // caller shouldn't try to put back more than they've taken
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000198 SkASSERT(!fBlocks.empty());
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000199 BufferBlock& block = fBlocks.back();
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000200 size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000201 if (bytes >= bytesUsed) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000202 bytes -= bytesUsed;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000203 fBytesInUse -= bytesUsed;
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000204 // if we locked a vb to satisfy the make space and we're releasing
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000205 // beyond it, then unmap it.
206 if (block.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -0700207 UNMAP_BUFFER(block);
bsalomon@google.com6513cd02011-08-05 20:12:30 +0000208 }
209 this->destroyBlock();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000210 } else {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000211 block.fBytesFree += bytes;
212 fBytesInUse -= bytes;
213 bytes = 0;
214 break;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000215 }
216 }
robertphillips1b8e1b52015-06-24 06:54:10 -0700217
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000218 VALIDATE();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000219}
220
221bool GrBufferAllocPool::createBlock(size_t requestSize) {
222
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000223 size_t size = SkTMax(requestSize, fMinBlockSize);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000224 SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000225
226 VALIDATE();
227
228 BufferBlock& block = fBlocks.push_back();
229
robertphillips1b8e1b52015-06-24 06:54:10 -0700230 block.fBuffer = this->getBuffer(size);
bsalomon7dea7b72015-08-19 08:26:51 -0700231 if (!block.fBuffer) {
robertphillips1b8e1b52015-06-24 06:54:10 -0700232 fBlocks.pop_back();
233 return false;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000234 }
235
robertphillips1b8e1b52015-06-24 06:54:10 -0700236 block.fBytesFree = block.fBuffer->gpuMemorySize();
bsalomon49f085d2014-09-05 13:34:00 -0700237 if (fBufferPtr) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000238 SkASSERT(fBlocks.count() > 1);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000239 BufferBlock& prev = fBlocks.fromBack(1);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000240 if (prev.fBuffer->isMapped()) {
bsalomon3512eda2014-06-26 12:56:22 -0700241 UNMAP_BUFFER(prev);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000242 } else {
bsalomon3512eda2014-06-26 12:56:22 -0700243 this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244 }
bsalomon7dea7b72015-08-19 08:26:51 -0700245 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000246 }
247
bsalomon7dea7b72015-08-19 08:26:51 -0700248 SkASSERT(!fBufferPtr);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000249
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000250 // 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 -0700251 // Otherwise when buffer mapping is supported we map if the buffer size is greater than the
252 // threshold.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000253 bool attemptMap = block.fBuffer->isCPUBacked();
bsalomon4b91f762015-05-19 09:29:46 -0700254 if (!attemptMap && GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) {
joshualitt7224c862015-05-29 06:46:47 -0700255 attemptMap = size > fGeometryBufferMapThreshold;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000256 }
257
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000258 if (attemptMap) {
259 fBufferPtr = block.fBuffer->map();
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000260 }
261
bsalomon7dea7b72015-08-19 08:26:51 -0700262 if (!fBufferPtr) {
263 fBufferPtr = this->resetCpuData(block.fBytesFree);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000264 }
265
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000266 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000267
268 return true;
269}
270
271void GrBufferAllocPool::destroyBlock() {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000272 SkASSERT(!fBlocks.empty());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000273
274 BufferBlock& block = fBlocks.back();
robertphillips1b8e1b52015-06-24 06:54:10 -0700275
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000276 SkASSERT(!block.fBuffer->isMapped());
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277 block.fBuffer->unref();
278 fBlocks.pop_back();
bsalomon7dea7b72015-08-19 08:26:51 -0700279 fBufferPtr = nullptr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000280}
281
bsalomon7dea7b72015-08-19 08:26:51 -0700282void* GrBufferAllocPool::resetCpuData(size_t newSize) {
283 sk_free(fCpuData);
284 if (newSize) {
285 if (fGpu->caps()->mustClearUploadedBufferData()) {
286 fCpuData = sk_calloc(newSize);
287 } else {
288 fCpuData = sk_malloc_throw(newSize);
289 }
290 } else {
291 fCpuData = nullptr;
292 }
293 return fCpuData;
294}
295
296
bsalomon3512eda2014-06-26 12:56:22 -0700297void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) {
298 GrGeometryBuffer* buffer = block.fBuffer;
bsalomon49f085d2014-09-05 13:34:00 -0700299 SkASSERT(buffer);
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000300 SkASSERT(!buffer->isMapped());
bsalomon7dea7b72015-08-19 08:26:51 -0700301 SkASSERT(fCpuData == fBufferPtr);
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000302 SkASSERT(flushSize <= buffer->gpuMemorySize());
bsalomon@google.comd5108092012-03-08 15:10:39 +0000303 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000304
bsalomon4b91f762015-05-19 09:29:46 -0700305 if (GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() &&
joshualitt7224c862015-05-29 06:46:47 -0700306 flushSize > fGeometryBufferMapThreshold) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000307 void* data = buffer->map();
bsalomon49f085d2014-09-05 13:34:00 -0700308 if (data) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000309 memcpy(data, fBufferPtr, flushSize);
bsalomon3512eda2014-06-26 12:56:22 -0700310 UNMAP_BUFFER(block);
bsalomon@google.com71bd1ef2011-12-12 20:42:26 +0000311 return;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000312 }
313 }
314 buffer->updateData(fBufferPtr, flushSize);
bsalomon@google.comd5108092012-03-08 15:10:39 +0000315 VALIDATE(true);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000316}
317
robertphillips1b8e1b52015-06-24 06:54:10 -0700318GrGeometryBuffer* GrBufferAllocPool::getBuffer(size_t size) {
319
320 GrResourceProvider* rp = fGpu->getContext()->resourceProvider();
321
bsalomoneae62002015-07-31 13:59:30 -0700322 static const GrResourceProvider::BufferUsage kUsage = GrResourceProvider::kDynamic_BufferUsage;
323 // Shouldn't have to use this flag (http://skbug.com/4156)
324 static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000325 if (kIndex_BufferType == fBufferType) {
bsalomoneae62002015-07-31 13:59:30 -0700326 return rp->createIndexBuffer(size, kUsage, kFlags);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000327 } else {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000328 SkASSERT(kVertex_BufferType == fBufferType);
bsalomoneae62002015-07-31 13:59:30 -0700329 return rp->createVertexBuffer(size, kUsage, kFlags);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000330 }
331}
332
333////////////////////////////////////////////////////////////////////////////////
334
robertphillips1b8e1b52015-06-24 06:54:10 -0700335GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu)
336 : GrBufferAllocPool(gpu, kVertex_BufferType, MIN_VERTEX_BUFFER_SIZE) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000337}
338
jvanverth@google.coma6338982013-01-31 21:34:25 +0000339void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000340 int vertexCount,
341 const GrVertexBuffer** buffer,
342 int* startVertex) {
343
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000344 SkASSERT(vertexCount >= 0);
bsalomon49f085d2014-09-05 13:34:00 -0700345 SkASSERT(buffer);
346 SkASSERT(startVertex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000347
bsalomon@google.com8b484412011-04-18 19:07:44 +0000348 size_t offset = 0; // assign to suppress warning
bsalomon7dea7b72015-08-19 08:26:51 -0700349 const GrGeometryBuffer* geomBuffer = nullptr; // assign to suppress warning
jvanverth@google.coma6338982013-01-31 21:34:25 +0000350 void* ptr = INHERITED::makeSpace(vertexSize * vertexCount,
351 vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000352 &geomBuffer,
353 &offset);
354
355 *buffer = (const GrVertexBuffer*) geomBuffer;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000356 SkASSERT(0 == offset % vertexSize);
robertphillips@google.comadacc702013-10-14 21:53:24 +0000357 *startVertex = static_cast<int>(offset / vertexSize);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000358 return ptr;
359}
360
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000361////////////////////////////////////////////////////////////////////////////////
362
robertphillips1b8e1b52015-06-24 06:54:10 -0700363GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu)
364 : GrBufferAllocPool(gpu, kIndex_BufferType, MIN_INDEX_BUFFER_SIZE) {
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000365}
366
367void* GrIndexBufferAllocPool::makeSpace(int indexCount,
368 const GrIndexBuffer** buffer,
369 int* startIndex) {
370
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000371 SkASSERT(indexCount >= 0);
bsalomon49f085d2014-09-05 13:34:00 -0700372 SkASSERT(buffer);
373 SkASSERT(startIndex);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000374
bsalomon@google.com8b484412011-04-18 19:07:44 +0000375 size_t offset = 0; // assign to suppress warning
bsalomon7dea7b72015-08-19 08:26:51 -0700376 const GrGeometryBuffer* geomBuffer = nullptr; // assign to suppress warning
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000377 void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t),
378 sizeof(uint16_t),
379 &geomBuffer,
380 &offset);
381
382 *buffer = (const GrIndexBuffer*) geomBuffer;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000383 SkASSERT(0 == offset % sizeof(uint16_t));
robertphillips@google.comadacc702013-10-14 21:53:24 +0000384 *startIndex = static_cast<int>(offset / sizeof(uint16_t));
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000385 return ptr;
386}