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 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 17 | #include "SkTraceEvent.h" |
| 18 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 19 | #ifdef SK_DEBUG |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 20 | #define VALIDATE validate |
| 21 | #else |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 22 | static void VALIDATE(bool = false) {} |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 25 | // page size |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 26 | #define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 12) |
| 27 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 28 | #define UNMAP_BUFFER(block) \ |
| 29 | do { \ |
| 30 | TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), \ |
| 31 | "GrBufferAllocPool Unmapping Buffer", \ |
| 32 | TRACE_EVENT_SCOPE_THREAD, \ |
| 33 | "percent_unwritten", \ |
| 34 | (float)((block).fBytesFree) / (block).fBuffer->gpuMemorySize()); \ |
| 35 | (block).fBuffer->unmap(); \ |
| 36 | } while (false) |
| 37 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 38 | GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu, |
| 39 | BufferType bufferType, |
| 40 | bool frequentResetHint, |
| 41 | size_t blockSize, |
| 42 | int preallocBufferCnt) : |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 43 | fBlocks(SkTMax(8, 2*preallocBufferCnt)) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 44 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 45 | SkASSERT(NULL != gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 46 | fGpu = gpu; |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 47 | fGpu->ref(); |
| 48 | fGpuIsReffed = true; |
| 49 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 50 | fBufferType = bufferType; |
| 51 | fFrequentResetHint = frequentResetHint; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 52 | fBufferPtr = NULL; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 53 | fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 54 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 55 | fBytesInUse = 0; |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 57 | fPreallocBuffersInUse = 0; |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 58 | fPreallocBufferStartIdx = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 59 | for (int i = 0; i < preallocBufferCnt; ++i) { |
| 60 | GrGeometryBuffer* buffer = this->createBuffer(fMinBlockSize); |
| 61 | if (NULL != buffer) { |
| 62 | *fPreallocBuffers.append() = buffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | GrBufferAllocPool::~GrBufferAllocPool() { |
| 68 | VALIDATE(); |
| 69 | if (fBlocks.count()) { |
| 70 | GrGeometryBuffer* buffer = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 71 | if (buffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 72 | UNMAP_BUFFER(fBlocks.back()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 75 | while (!fBlocks.empty()) { |
| 76 | destroyBlock(); |
| 77 | } |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 78 | fPreallocBuffers.unrefAll(); |
| 79 | releaseGpuRef(); |
| 80 | } |
| 81 | |
| 82 | void GrBufferAllocPool::releaseGpuRef() { |
| 83 | if (fGpuIsReffed) { |
| 84 | fGpu->unref(); |
| 85 | fGpuIsReffed = false; |
| 86 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | void GrBufferAllocPool::reset() { |
| 90 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 91 | fBytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 92 | if (fBlocks.count()) { |
| 93 | GrGeometryBuffer* buffer = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 94 | if (buffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 95 | UNMAP_BUFFER(fBlocks.back()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 98 | // fPreallocBuffersInUse will be decremented down to zero in the while loop |
| 99 | int preallocBuffersInUse = fPreallocBuffersInUse; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 100 | while (!fBlocks.empty()) { |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 101 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 102 | } |
| 103 | if (fPreallocBuffers.count()) { |
| 104 | // must set this after above loop. |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 105 | fPreallocBufferStartIdx = (fPreallocBufferStartIdx + |
| 106 | preallocBuffersInUse) % |
| 107 | fPreallocBuffers.count(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 108 | } |
bsalomon@google.com | 987dbc0 | 2011-12-14 14:44:19 +0000 | [diff] [blame] | 109 | // we may have created a large cpu mirror of a large VB. Reset the size |
| 110 | // to match our pre-allocated VBs. |
| 111 | fCpuData.reset(fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 112 | SkASSERT(0 == fPreallocBuffersInUse); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 113 | VALIDATE(); |
| 114 | } |
| 115 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 116 | void GrBufferAllocPool::unmap() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 117 | VALIDATE(); |
| 118 | |
| 119 | if (NULL != fBufferPtr) { |
| 120 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 121 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 122 | UNMAP_BUFFER(block); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 123 | } else { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 124 | size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 125 | this->flushCpuData(fBlocks.back(), flushSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 126 | } |
| 127 | fBufferPtr = NULL; |
| 128 | } |
| 129 | VALIDATE(); |
| 130 | } |
| 131 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 132 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 133 | void GrBufferAllocPool::validate(bool unusedBlockAllowed) const { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 134 | if (NULL != fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 135 | SkASSERT(!fBlocks.empty()); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 136 | if (fBlocks.back().fBuffer->isMapped()) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 137 | GrGeometryBuffer* buf = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 138 | SkASSERT(buf->mapPtr() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 139 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 140 | SkASSERT(fCpuData.get() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 141 | } |
| 142 | } else { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 143 | SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 144 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 145 | size_t bytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 146 | for (int i = 0; i < fBlocks.count() - 1; ++i) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 147 | SkASSERT(!fBlocks[i].fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 148 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 149 | for (int i = 0; i < fBlocks.count(); ++i) { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 150 | size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 151 | bytesInUse += bytes; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 152 | SkASSERT(bytes || unusedBlockAllowed); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 153 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 154 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 155 | SkASSERT(bytesInUse == fBytesInUse); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 156 | if (unusedBlockAllowed) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 157 | SkASSERT((fBytesInUse && !fBlocks.empty()) || |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 158 | (!fBytesInUse && (fBlocks.count() < 2))); |
| 159 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 160 | SkASSERT((0 == fBytesInUse) == fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 161 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 162 | } |
| 163 | #endif |
| 164 | |
| 165 | void* GrBufferAllocPool::makeSpace(size_t size, |
| 166 | size_t alignment, |
| 167 | const GrGeometryBuffer** buffer, |
| 168 | size_t* offset) { |
| 169 | VALIDATE(); |
| 170 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 171 | SkASSERT(NULL != buffer); |
| 172 | SkASSERT(NULL != offset); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 173 | |
| 174 | if (NULL != fBufferPtr) { |
| 175 | BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 176 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 177 | size_t pad = GrSizeAlignUpPad(usedBytes, |
| 178 | alignment); |
| 179 | if ((size + pad) <= back.fBytesFree) { |
| 180 | usedBytes += pad; |
| 181 | *offset = usedBytes; |
| 182 | *buffer = back.fBuffer; |
| 183 | back.fBytesFree -= size + pad; |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 184 | fBytesInUse += size + pad; |
| 185 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 186 | return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes); |
| 187 | } |
| 188 | } |
| 189 | |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 190 | // We could honor the space request using by a partial update of the current |
| 191 | // 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] | 192 | // 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] | 193 | // the part of the buffer we update. Also, the GL buffer implementation |
| 194 | // may be cheating on the actual buffer size by shrinking the buffer on |
| 195 | // updateData() if the amount of data passed is less than the full buffer |
| 196 | // size. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 197 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 198 | if (!createBlock(size)) { |
| 199 | return NULL; |
| 200 | } |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 201 | SkASSERT(NULL != fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 202 | |
| 203 | *offset = 0; |
| 204 | BufferBlock& back = fBlocks.back(); |
| 205 | *buffer = back.fBuffer; |
| 206 | back.fBytesFree -= size; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 207 | fBytesInUse += size; |
| 208 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 209 | return fBufferPtr; |
| 210 | } |
| 211 | |
| 212 | int GrBufferAllocPool::currentBufferItems(size_t itemSize) const { |
| 213 | VALIDATE(); |
| 214 | if (NULL != fBufferPtr) { |
| 215 | const BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 216 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 217 | size_t pad = GrSizeAlignUpPad(usedBytes, itemSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 218 | return static_cast<int>((back.fBytesFree - pad) / itemSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 219 | } else if (fPreallocBuffersInUse < fPreallocBuffers.count()) { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 220 | return static_cast<int>(fMinBlockSize / itemSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 221 | } |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | int GrBufferAllocPool::preallocatedBuffersRemaining() const { |
| 226 | return fPreallocBuffers.count() - fPreallocBuffersInUse; |
| 227 | } |
| 228 | |
| 229 | int GrBufferAllocPool::preallocatedBufferCount() const { |
| 230 | return fPreallocBuffers.count(); |
| 231 | } |
| 232 | |
| 233 | void GrBufferAllocPool::putBack(size_t bytes) { |
| 234 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 235 | |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 236 | // if the putBack unwinds all the preallocated buffers then we will |
| 237 | // advance the starting index. As blocks are destroyed fPreallocBuffersInUse |
| 238 | // will be decremented. I will reach zero if all blocks using preallocated |
| 239 | // buffers are released. |
| 240 | int preallocBuffersInUse = fPreallocBuffersInUse; |
| 241 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 242 | while (bytes) { |
| 243 | // caller shouldnt try to put back more than they've taken |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 244 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 245 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 246 | size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 247 | if (bytes >= bytesUsed) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 248 | bytes -= bytesUsed; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 249 | fBytesInUse -= bytesUsed; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 250 | // if we locked a vb to satisfy the make space and we're releasing |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 251 | // beyond it, then unmap it. |
| 252 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 253 | UNMAP_BUFFER(block); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 254 | } |
| 255 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 256 | } else { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 257 | block.fBytesFree += bytes; |
| 258 | fBytesInUse -= bytes; |
| 259 | bytes = 0; |
| 260 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 263 | if (!fPreallocBuffersInUse && fPreallocBuffers.count()) { |
| 264 | fPreallocBufferStartIdx = (fPreallocBufferStartIdx + |
| 265 | preallocBuffersInUse) % |
| 266 | fPreallocBuffers.count(); |
| 267 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 268 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | bool GrBufferAllocPool::createBlock(size_t requestSize) { |
| 272 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 273 | size_t size = SkTMax(requestSize, fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 274 | SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 275 | |
| 276 | VALIDATE(); |
| 277 | |
| 278 | BufferBlock& block = fBlocks.push_back(); |
| 279 | |
| 280 | if (size == fMinBlockSize && |
| 281 | fPreallocBuffersInUse < fPreallocBuffers.count()) { |
| 282 | |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 283 | uint32_t nextBuffer = (fPreallocBuffersInUse + |
| 284 | fPreallocBufferStartIdx) % |
| 285 | fPreallocBuffers.count(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 286 | block.fBuffer = fPreallocBuffers[nextBuffer]; |
| 287 | block.fBuffer->ref(); |
| 288 | ++fPreallocBuffersInUse; |
| 289 | } else { |
| 290 | block.fBuffer = this->createBuffer(size); |
| 291 | if (NULL == block.fBuffer) { |
| 292 | fBlocks.pop_back(); |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | block.fBytesFree = size; |
| 298 | if (NULL != fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 299 | SkASSERT(fBlocks.count() > 1); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 300 | BufferBlock& prev = fBlocks.fromBack(1); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 301 | if (prev.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 302 | UNMAP_BUFFER(prev); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 303 | } else { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 304 | this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 305 | } |
| 306 | fBufferPtr = NULL; |
| 307 | } |
| 308 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 309 | SkASSERT(NULL == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 310 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 311 | // If the buffer is CPU-backed we map it because it is free to do so and saves a copy. |
| 312 | // Otherwise when buffer mapping is supported: |
| 313 | // a) If the frequently reset hint is set we only map when the requested size meets a |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 314 | // threshold (since we don't expect it is likely that we will see more vertex data) |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 315 | // b) If the hint is not set we map if the buffer size is greater than the threshold. |
| 316 | bool attemptMap = block.fBuffer->isCPUBacked(); |
| 317 | if (!attemptMap && GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) { |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 318 | if (fFrequentResetHint) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 319 | attemptMap = requestSize > GR_GEOM_BUFFER_MAP_THRESHOLD; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 320 | } else { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 321 | attemptMap = size > GR_GEOM_BUFFER_MAP_THRESHOLD; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 325 | if (attemptMap) { |
| 326 | fBufferPtr = block.fBuffer->map(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | if (NULL == fBufferPtr) { |
bsalomon@google.com | 7d4679a | 2011-09-02 22:06:24 +0000 | [diff] [blame] | 330 | fBufferPtr = fCpuData.reset(size); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 333 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 334 | |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | void GrBufferAllocPool::destroyBlock() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 339 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 340 | |
| 341 | BufferBlock& block = fBlocks.back(); |
| 342 | if (fPreallocBuffersInUse > 0) { |
| 343 | uint32_t prevPreallocBuffer = (fPreallocBuffersInUse + |
bsalomon@google.com | b665a6b | 2012-03-01 20:59:28 +0000 | [diff] [blame] | 344 | fPreallocBufferStartIdx + |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 345 | (fPreallocBuffers.count() - 1)) % |
| 346 | fPreallocBuffers.count(); |
| 347 | if (block.fBuffer == fPreallocBuffers[prevPreallocBuffer]) { |
| 348 | --fPreallocBuffersInUse; |
| 349 | } |
| 350 | } |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 351 | SkASSERT(!block.fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 352 | block.fBuffer->unref(); |
| 353 | fBlocks.pop_back(); |
| 354 | fBufferPtr = NULL; |
| 355 | } |
| 356 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 357 | void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) { |
| 358 | GrGeometryBuffer* buffer = block.fBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 359 | SkASSERT(NULL != buffer); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 360 | SkASSERT(!buffer->isMapped()); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 361 | SkASSERT(fCpuData.get() == fBufferPtr); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 362 | SkASSERT(flushSize <= buffer->gpuMemorySize()); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 363 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 364 | |
commit-bot@chromium.org | 160b478 | 2014-05-05 12:32:37 +0000 | [diff] [blame] | 365 | if (GrDrawTargetCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() && |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 366 | flushSize > GR_GEOM_BUFFER_MAP_THRESHOLD) { |
| 367 | void* data = buffer->map(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 368 | if (NULL != data) { |
| 369 | memcpy(data, fBufferPtr, flushSize); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame^] | 370 | UNMAP_BUFFER(block); |
bsalomon@google.com | 71bd1ef | 2011-12-12 20:42:26 +0000 | [diff] [blame] | 371 | return; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | buffer->updateData(fBufferPtr, flushSize); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 375 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | GrGeometryBuffer* GrBufferAllocPool::createBuffer(size_t size) { |
| 379 | if (kIndex_BufferType == fBufferType) { |
| 380 | return fGpu->createIndexBuffer(size, true); |
| 381 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 382 | SkASSERT(kVertex_BufferType == fBufferType); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 383 | return fGpu->createVertexBuffer(size, true); |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | //////////////////////////////////////////////////////////////////////////////// |
| 388 | |
| 389 | GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu, |
| 390 | bool frequentResetHint, |
| 391 | size_t bufferSize, |
| 392 | int preallocBufferCnt) |
| 393 | : GrBufferAllocPool(gpu, |
| 394 | kVertex_BufferType, |
| 395 | frequentResetHint, |
| 396 | bufferSize, |
| 397 | preallocBufferCnt) { |
| 398 | } |
| 399 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 400 | void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 401 | int vertexCount, |
| 402 | const GrVertexBuffer** buffer, |
| 403 | int* startVertex) { |
| 404 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 405 | SkASSERT(vertexCount >= 0); |
| 406 | SkASSERT(NULL != buffer); |
| 407 | SkASSERT(NULL != startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 408 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 409 | size_t offset = 0; // assign to suppress warning |
| 410 | const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 411 | void* ptr = INHERITED::makeSpace(vertexSize * vertexCount, |
| 412 | vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 413 | &geomBuffer, |
| 414 | &offset); |
| 415 | |
| 416 | *buffer = (const GrVertexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 417 | SkASSERT(0 == offset % vertexSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 418 | *startVertex = static_cast<int>(offset / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 419 | return ptr; |
| 420 | } |
| 421 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 422 | bool GrVertexBufferAllocPool::appendVertices(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 423 | int vertexCount, |
| 424 | const void* vertices, |
| 425 | const GrVertexBuffer** buffer, |
| 426 | int* startVertex) { |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 427 | void* space = makeSpace(vertexSize, vertexCount, buffer, startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 428 | if (NULL != space) { |
| 429 | memcpy(space, |
| 430 | vertices, |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 431 | vertexSize * vertexCount); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 432 | return true; |
| 433 | } else { |
| 434 | return false; |
| 435 | } |
| 436 | } |
| 437 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 438 | int GrVertexBufferAllocPool::preallocatedBufferVertices(size_t vertexSize) const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 439 | return static_cast<int>(INHERITED::preallocatedBufferSize() / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 440 | } |
| 441 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 442 | int GrVertexBufferAllocPool::currentBufferVertices(size_t vertexSize) const { |
| 443 | return currentBufferItems(vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | //////////////////////////////////////////////////////////////////////////////// |
| 447 | |
| 448 | GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu, |
| 449 | bool frequentResetHint, |
| 450 | size_t bufferSize, |
| 451 | int preallocBufferCnt) |
| 452 | : GrBufferAllocPool(gpu, |
| 453 | kIndex_BufferType, |
| 454 | frequentResetHint, |
| 455 | bufferSize, |
| 456 | preallocBufferCnt) { |
| 457 | } |
| 458 | |
| 459 | void* GrIndexBufferAllocPool::makeSpace(int indexCount, |
| 460 | const GrIndexBuffer** buffer, |
| 461 | int* startIndex) { |
| 462 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 463 | SkASSERT(indexCount >= 0); |
| 464 | SkASSERT(NULL != buffer); |
| 465 | SkASSERT(NULL != startIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 466 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 467 | size_t offset = 0; // assign to suppress warning |
| 468 | const GrGeometryBuffer* geomBuffer = NULL; // assign to suppress warning |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 469 | void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t), |
| 470 | sizeof(uint16_t), |
| 471 | &geomBuffer, |
| 472 | &offset); |
| 473 | |
| 474 | *buffer = (const GrIndexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 475 | SkASSERT(0 == offset % sizeof(uint16_t)); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 476 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 477 | return ptr; |
| 478 | } |
| 479 | |
| 480 | bool GrIndexBufferAllocPool::appendIndices(int indexCount, |
| 481 | const void* indices, |
| 482 | const GrIndexBuffer** buffer, |
| 483 | int* startIndex) { |
| 484 | void* space = makeSpace(indexCount, buffer, startIndex); |
| 485 | if (NULL != space) { |
| 486 | memcpy(space, indices, sizeof(uint16_t) * indexCount); |
| 487 | return true; |
| 488 | } else { |
| 489 | return false; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | int GrIndexBufferAllocPool::preallocatedBufferIndices() const { |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 494 | return static_cast<int>(INHERITED::preallocatedBufferSize() / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int GrIndexBufferAllocPool::currentBufferIndices() const { |
| 498 | return currentBufferItems(sizeof(uint16_t)); |
| 499 | } |