epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 10 | #include "GrBufferAllocPool.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 11 | #include "GrDrawTargetCaps.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrIndexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 14 | #include "GrTypes.h" |
| 15 | #include "GrVertexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 16 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 17 | #ifdef SK_DEBUG |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 18 | #define VALIDATE validate |
| 19 | #else |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 20 | static void VALIDATE(bool = false) {} |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 21 | #endif |
| 22 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 23 | // page size |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 24 | #define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 12) |
| 25 | |
| 26 | GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu, |
| 27 | BufferType bufferType, |
| 28 | bool frequentResetHint, |
| 29 | size_t blockSize, |
| 30 | int preallocBufferCnt) : |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 31 | fBlocks(SkTMax(8, 2*preallocBufferCnt)) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 32 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 33 | SkASSERT(NULL != gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 34 | fGpu = gpu; |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 35 | fGpu->ref(); |
| 36 | fGpuIsReffed = true; |
| 37 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 38 | fBufferType = bufferType; |
| 39 | fFrequentResetHint = frequentResetHint; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 40 | fBufferPtr = NULL; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 41 | fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 43 | fBytesInUse = 0; |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 44 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 45 | fPreallocBuffersInUse = 0; |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 46 | fPreallocBufferStartIdx = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 47 | for (int i = 0; i < preallocBufferCnt; ++i) { |
| 48 | GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize); |
| 49 | if (NULL != buffer) { |
| 50 | *fPreallocBuffers.append() = buffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | GrBufferAllocPool::~GrBufferAllocPool() { |
| 56 | VALIDATE(); |
| 57 | if (fBlocks.count()) { |
| 58 | GrGeometryBuffer* buffer = fBlocks.back().fBuffer; |
| 59 | if (buffer->isLocked()) { |
| 60 | buffer->unlock(); |
| 61 | } |
| 62 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 63 | while (!fBlocks.empty()) { |
| 64 | destroyBlock(); |
| 65 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 66 | fPreallocBuffers.unrefAll(); |
| 67 | releaseGpuRef(); |
| 68 | } |
| 69 | |
| 70 | void GrBufferAllocPool::releaseGpuRef() { |
| 71 | if (fGpuIsReffed) { |
| 72 | fGpu->unref(); |
| 73 | fGpuIsReffed = false; |
| 74 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void GrBufferAllocPool::reset() { |
| 78 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 79 | fBytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 80 | if (fBlocks.count()) { |
| 81 | GrGeometryBuffer* buffer = fBlocks.back().fBuffer; |
| 82 | if (buffer->isLocked()) { |
| 83 | buffer->unlock(); |
| 84 | } |
| 85 | } |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 86 | // fPreallocBuffersInUse will be decremented down to zero in the while loop |
| 87 | int preallocBuffersInUse = fPreallocBuffersInUse; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 88 | while (!fBlocks.empty()) { |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 89 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 90 | } |
| 91 | if (fPreallocBuffers.count()) { |
| 92 | // must set this after above loop. |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 93 | fPreallocBufferStartIdx = (fPreallocBufferStartIdx + |
| 94 | preallocBuffersInUse) % |
| 95 | fPreallocBuffers.count(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 96 | } |
bsalomon@google.com | 987dbc0 | 2011-12-14 14:44:19 +0000 | [diff] [blame] | 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); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 100 | SkASSERT(0 == fPreallocBuffersInUse); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 101 | VALIDATE(); |
| 102 | } |
| 103 | |
| 104 | void 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 { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 112 | size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 113 | flushCpuData(fBlocks.back().fBuffer, flushSize); |
| 114 | } |
| 115 | fBufferPtr = NULL; |
| 116 | } |
| 117 | VALIDATE(); |
| 118 | } |
| 119 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 120 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 121 | void GrBufferAllocPool::validate(bool unusedBlockAllowed) const { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 122 | if (NULL != fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 123 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 124 | if (fBlocks.back().fBuffer->isLocked()) { |
| 125 | GrGeometryBuffer* buf = fBlocks.back().fBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 126 | SkASSERT(buf->lockPtr() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 127 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 128 | SkASSERT(fCpuData.get() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 129 | } |
| 130 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 131 | SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isLocked()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 132 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 133 | size_t bytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 134 | for (int i = 0; i < fBlocks.count() - 1; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 135 | SkASSERT(!fBlocks[i].fBuffer->isLocked()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 136 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 137 | for (int i = 0; i < fBlocks.count(); ++i) { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 138 | size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 139 | bytesInUse += bytes; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 140 | SkASSERT(bytes || unusedBlockAllowed); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 141 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 142 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 143 | SkASSERT(bytesInUse == fBytesInUse); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 144 | if (unusedBlockAllowed) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 145 | SkASSERT((fBytesInUse && !fBlocks.empty()) || |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 146 | (!fBytesInUse && (fBlocks.count() < 2))); |
| 147 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 148 | SkASSERT((0 == fBytesInUse) == fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 149 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 150 | } |
| 151 | #endif |
| 152 | |
| 153 | void* GrBufferAllocPool::makeSpace(size_t size, |
| 154 | size_t alignment, |
| 155 | const GrGeometryBuffer** buffer, |
| 156 | size_t* offset) { |
| 157 | VALIDATE(); |
| 158 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 159 | SkASSERT(NULL != buffer); |
| 160 | SkASSERT(NULL != offset); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 161 | |
| 162 | if (NULL != fBufferPtr) { |
| 163 | BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 164 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 165 | 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.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 172 | fBytesInUse += size + pad; |
| 173 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 174 | return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes); |
| 175 | } |
| 176 | } |
| 177 | |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 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 |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 180 | // allow the driver to know that previously issued draws won't read from |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 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 |
| 184 | // size. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 185 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 186 | if (!createBlock(size)) { |
| 187 | return NULL; |
| 188 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 189 | SkASSERT(NULL != fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 190 | |
| 191 | *offset = 0; |
| 192 | BufferBlock& back = fBlocks.back(); |
| 193 | *buffer = back.fBuffer; |
| 194 | back.fBytesFree -= size; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 195 | fBytesInUse += size; |
| 196 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 197 | return fBufferPtr; |
| 198 | } |
| 199 | |
| 200 | int GrBufferAllocPool::currentBufferItems(size_t itemSize) const { |
| 201 | VALIDATE(); |
| 202 | if (NULL != fBufferPtr) { |
| 203 | const BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 204 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 205 | size_t pad = GrSizeAlignUpPad(usedBytes, itemSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 206 | return static_cast<int>((back.fBytesFree - pad) / itemSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 207 | } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 208 | return static_cast<int>(fMinBlockSize / itemSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | int GrBufferAllocPool::preallocatedBuffersRemaining() const { |
| 214 | return fPreallocBuffers.count() - fPreallocBuffersInUse; |
| 215 | } |
| 216 | |
| 217 | int GrBufferAllocPool::preallocatedBufferCount() const { |
| 218 | return fPreallocBuffers.count(); |
| 219 | } |
| 220 | |
| 221 | void GrBufferAllocPool::putBack(size_t bytes) { |
| 222 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 223 | |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 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; |
| 229 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 230 | while (bytes) { |
| 231 | // caller shouldnt try to put back more than they've taken |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 232 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 233 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 234 | size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 235 | if (bytes >= bytesUsed) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 236 | bytes -= bytesUsed; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 237 | fBytesInUse -= bytesUsed; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 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(); |
| 242 | } |
| 243 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 244 | } else { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 245 | block.fBytesFree += bytes; |
| 246 | fBytesInUse -= bytes; |
| 247 | bytes = 0; |
| 248 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 251 | if (!fPreallocBuffersInUse && fPreallocBuffers.count()) { |
| 252 | fPreallocBufferStartIdx = (fPreallocBufferStartIdx + |
| 253 | preallocBuffersInUse) % |
| 254 | fPreallocBuffers.count(); |
| 255 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 256 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | bool GrBufferAllocPool::createBlock(size_t requestSize) { |
| 260 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 261 | size_t size = SkTMax(requestSize, fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 262 | SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 263 | |
| 264 | VALIDATE(); |
| 265 | |
| 266 | BufferBlock& block = fBlocks.push_back(); |
| 267 | |
| 268 | if (size == fMinBlockSize && |
| 269 | fPreallocBuffersInUse < fPreallocBuffers.count()) { |
| 270 | |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 271 | uint32_t nextBuffer = (fPreallocBuffersInUse + |
| 272 | fPreallocBufferStartIdx) % |
| 273 | fPreallocBuffers.count(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 274 | 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.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 287 | SkASSERT(fBlocks.count() > 1); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 288 | BufferBlock& prev = fBlocks.fromBack(1); |
| 289 | if (prev.fBuffer->isLocked()) { |
| 290 | prev.fBuffer->unlock(); |
| 291 | } else { |
| 292 | flushCpuData(prev.fBuffer, |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 293 | prev.fBuffer->gpuMemorySize() - prev.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 294 | } |
| 295 | fBufferPtr = NULL; |
| 296 | } |
| 297 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 298 | SkASSERT(NULL == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 299 | |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 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(); |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame^] | 306 | if (!attemptLock && GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 307 | 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 315 | fBufferPtr = block.fBuffer->lock(); |
| 316 | } |
| 317 | |
| 318 | if (NULL == fBufferPtr) { |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 319 | fBufferPtr = fCpuData.reset(size); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 320 | } |
| 321 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 322 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 323 | |
| 324 | return true; |
| 325 | } |
| 326 | |
| 327 | void GrBufferAllocPool::destroyBlock() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 328 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 329 | |
| 330 | BufferBlock& block = fBlocks.back(); |
| 331 | if (fPreallocBuffersInUse > 0) { |
| 332 | uint32_t prevPreallocBuffer = (fPreallocBuffersInUse + |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 333 | fPreallocBufferStartIdx + |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 334 | (fPreallocBuffers.count() - 1)) % |
| 335 | fPreallocBuffers.count(); |
| 336 | if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) { |
| 337 | --fPreallocBuffersInUse; |
| 338 | } |
| 339 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 340 | SkASSERT(!block.fBuffer->isLocked()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | block.fBuffer->unref(); |
| 342 | fBlocks.pop_back(); |
| 343 | fBufferPtr = NULL; |
| 344 | } |
| 345 | |
| 346 | void GrBufferAllocPool::flushCpuData(GrGeometryBuffer* buffer, |
| 347 | size_t flushSize) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 348 | SkASSERT(NULL != buffer); |
| 349 | SkASSERT(!buffer->isLocked()); |
| 350 | SkASSERT(fCpuData.get() == fBufferPtr); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 351 | SkASSERT(flushSize <= buffer->gpuMemorySize()); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 352 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 353 | |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame^] | 354 | if (GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() && |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 355 | 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.com | 71bd1ef | 2011-12-12 20:42:26 +0000 | [diff] [blame] | 360 | return; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | buffer->updateData(fBufferPtr, flushSize); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 364 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) { |
| 368 | if (kIndex_BufferType == fBufferType) { |
| 369 | return fGpu->createIndexBuffer(size, true); |
| 370 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 371 | SkASSERT(kVertex_BufferType == fBufferType); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 372 | return fGpu->createVertexBuffer(size, true); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | //////////////////////////////////////////////////////////////////////////////// |
| 377 | |
| 378 | GrVertexBufferAllocPool::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.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 389 | void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 390 | int vertexCount, |
| 391 | const GrVertexBuffer** buffer, |
| 392 | int* startVertex) { |
| 393 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 394 | SkASSERT(vertexCount >= 0); |
| 395 | SkASSERT(NULL != buffer); |
| 396 | SkASSERT(NULL != startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 397 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 398 | size_t offset = 0; // assign to suppress warning |
| 399 | const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 400 | void* ptr = INHERITED::makeSpace(vertexSize * vertexCount, |
| 401 | vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 402 | &geomBuffer, |
| 403 | &offset); |
| 404 | |
| 405 | *buffer = (const GrVertexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 406 | SkASSERT(0 == offset % vertexSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 407 | *startVertex = static_cast<int>(offset / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 408 | return ptr; |
| 409 | } |
| 410 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 411 | bool GrVertexBufferAllocPool::appendVertices(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 412 | int vertexCount, |
| 413 | const void* vertices, |
| 414 | const GrVertexBuffer** buffer, |
| 415 | int* startVertex) { |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 416 | void* space = makeSpace(vertexSize, vertexCount, buffer, startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 417 | if (NULL != space) { |
| 418 | memcpy(space, |
| 419 | vertices, |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 420 | vertexSize * vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 421 | return true; |
| 422 | } else { |
| 423 | return false; |
| 424 | } |
| 425 | } |
| 426 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 427 | int GrVertexBufferAllocPool::preallocatedBufferVertices(size_t vertexSize) const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 428 | return static_cast<int>(INHERITED::preallocatedBufferSize() / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 429 | } |
| 430 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 431 | int GrVertexBufferAllocPool::currentBufferVertices(size_t vertexSize) const { |
| 432 | return currentBufferItems(vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | //////////////////////////////////////////////////////////////////////////////// |
| 436 | |
| 437 | GrIndexBufferAllocPool::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 | |
| 448 | void* GrIndexBufferAllocPool::makeSpace(int indexCount, |
| 449 | const GrIndexBuffer** buffer, |
| 450 | int* startIndex) { |
| 451 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 452 | SkASSERT(indexCount >= 0); |
| 453 | SkASSERT(NULL != buffer); |
| 454 | SkASSERT(NULL != startIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 455 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 456 | size_t offset = 0; // assign to suppress warning |
| 457 | const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 458 | void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t), |
| 459 | sizeof(uint16_t), |
| 460 | &geomBuffer, |
| 461 | &offset); |
| 462 | |
| 463 | *buffer = (const GrIndexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 464 | SkASSERT(0 == offset % sizeof(uint16_t)); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 465 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 466 | return ptr; |
| 467 | } |
| 468 | |
| 469 | bool 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 | |
| 482 | int GrIndexBufferAllocPool::preallocatedBufferIndices() const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 483 | return static_cast<int>(INHERITED::preallocatedBufferSize() / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | int GrIndexBufferAllocPool::currentBufferIndices() const { |
| 487 | return currentBufferItems(sizeof(uint16_t)); |
| 488 | } |