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