bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2010 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 8 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 9 | #include "GrBufferAllocPool.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 10 | #include "GrBuffer.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 11 | #include "GrCaps.h" |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 12 | #include "GrContext.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 14 | #include "GrResourceProvider.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 15 | #include "GrTypes.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 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 25 | static const size_t MIN_VERTEX_BUFFER_SIZE = 1 << 15; |
| 26 | static const size_t MIN_INDEX_BUFFER_SIZE = 1 << 12; |
| 27 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 28 | // page size |
joshualitt | 0709ca0 | 2015-06-04 09:13:46 -0700 | [diff] [blame] | 29 | #define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 15) |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 30 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 31 | #define UNMAP_BUFFER(block) \ |
| 32 | do { \ |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 33 | TRACE_EVENT_INSTANT1("skia.gpu", \ |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 34 | "GrBufferAllocPool Unmapping Buffer", \ |
| 35 | TRACE_EVENT_SCOPE_THREAD, \ |
| 36 | "percent_unwritten", \ |
| 37 | (float)((block).fBytesFree) / (block).fBuffer->gpuMemorySize()); \ |
| 38 | (block).fBuffer->unmap(); \ |
| 39 | } while (false) |
| 40 | |
Robert Phillips | bdda0ba | 2017-08-31 09:17:43 -0400 | [diff] [blame] | 41 | GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu, GrBufferType bufferType, size_t blockSize) |
| 42 | : fBlocks(8) { |
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 | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 45 | fCpuData = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 46 | fBufferType = bufferType; |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 47 | fBufferPtr = nullptr; |
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; |
robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 51 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 52 | fBufferMapThreshold = gpu->caps()->bufferMapThreshold(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 53 | } |
| 54 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 55 | void GrBufferAllocPool::deleteBlocks() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 56 | if (fBlocks.count()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 57 | GrBuffer* buffer = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 58 | if (buffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 59 | UNMAP_BUFFER(fBlocks.back()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 62 | while (!fBlocks.empty()) { |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 63 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 64 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 65 | SkASSERT(!fBufferPtr); |
| 66 | } |
| 67 | |
| 68 | GrBufferAllocPool::~GrBufferAllocPool() { |
| 69 | VALIDATE(); |
| 70 | this->deleteBlocks(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 71 | sk_free(fCpuData); |
bsalomon | ecb8e3e | 2015-04-29 04:33:52 -0700 | [diff] [blame] | 72 | fGpu->unref(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void GrBufferAllocPool::reset() { |
| 76 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 77 | fBytesInUse = 0; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 78 | this->deleteBlocks(); |
robertphillips | 48d91b5 | 2016-08-18 14:01:14 -0700 | [diff] [blame] | 79 | this->resetCpuData(0); // delete all the cpu-side memory |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 80 | VALIDATE(); |
| 81 | } |
| 82 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 83 | void GrBufferAllocPool::unmap() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 84 | VALIDATE(); |
| 85 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 86 | if (fBufferPtr) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 88 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 89 | UNMAP_BUFFER(block); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 90 | } else { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 91 | size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 92 | this->flushCpuData(fBlocks.back(), flushSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 93 | } |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 94 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 95 | } |
| 96 | VALIDATE(); |
| 97 | } |
| 98 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 99 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 100 | void GrBufferAllocPool::validate(bool unusedBlockAllowed) const { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 101 | bool wasDestroyed = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 102 | if (fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 103 | SkASSERT(!fBlocks.empty()); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 104 | if (fBlocks.back().fBuffer->isMapped()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 105 | GrBuffer* buf = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 106 | SkASSERT(buf->mapPtr() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 107 | } else { |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 108 | SkASSERT(fCpuData == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 109 | } |
| 110 | } else { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 111 | SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 112 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 113 | size_t bytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 114 | for (int i = 0; i < fBlocks.count() - 1; ++i) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 115 | SkASSERT(!fBlocks[i].fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 116 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 117 | for (int i = 0; !wasDestroyed && i < fBlocks.count(); ++i) { |
| 118 | if (fBlocks[i].fBuffer->wasDestroyed()) { |
| 119 | wasDestroyed = true; |
| 120 | } else { |
| 121 | size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree; |
| 122 | bytesInUse += bytes; |
| 123 | SkASSERT(bytes || unusedBlockAllowed); |
| 124 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 125 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 126 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 127 | if (!wasDestroyed) { |
| 128 | SkASSERT(bytesInUse == fBytesInUse); |
| 129 | if (unusedBlockAllowed) { |
| 130 | SkASSERT((fBytesInUse && !fBlocks.empty()) || |
| 131 | (!fBytesInUse && (fBlocks.count() < 2))); |
| 132 | } else { |
| 133 | SkASSERT((0 == fBytesInUse) == fBlocks.empty()); |
| 134 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 135 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 136 | } |
| 137 | #endif |
| 138 | |
| 139 | void* GrBufferAllocPool::makeSpace(size_t size, |
| 140 | size_t alignment, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 141 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 142 | size_t* offset) { |
| 143 | VALIDATE(); |
| 144 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 145 | SkASSERT(buffer); |
| 146 | SkASSERT(offset); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 147 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 148 | if (fBufferPtr) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 149 | BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 150 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 151 | size_t pad = GrSizeAlignUpPad(usedBytes, alignment); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 152 | if ((size + pad) <= back.fBytesFree) { |
dongseong.hwang | 8f25c66 | 2015-01-22 10:40:20 -0800 | [diff] [blame] | 153 | memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 154 | usedBytes += pad; |
| 155 | *offset = usedBytes; |
| 156 | *buffer = back.fBuffer; |
| 157 | back.fBytesFree -= size + pad; |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 158 | fBytesInUse += size + pad; |
| 159 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 160 | return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes); |
| 161 | } |
| 162 | } |
| 163 | |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 164 | // We could honor the space request using by a partial update of the current |
| 165 | // 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] | 166 | // 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] | 167 | // the part of the buffer we update. Also, the GL buffer implementation |
| 168 | // may be cheating on the actual buffer size by shrinking the buffer on |
| 169 | // updateData() if the amount of data passed is less than the full buffer |
| 170 | // size. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 171 | |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 172 | if (!this->createBlock(size)) { |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 173 | return nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 174 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 175 | SkASSERT(fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 176 | |
| 177 | *offset = 0; |
| 178 | BufferBlock& back = fBlocks.back(); |
| 179 | *buffer = back.fBuffer; |
| 180 | back.fBytesFree -= size; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 181 | fBytesInUse += size; |
| 182 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 183 | return fBufferPtr; |
| 184 | } |
| 185 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 186 | void* GrBufferAllocPool::makeSpaceAtLeast(size_t minSize, |
| 187 | size_t fallbackSize, |
| 188 | size_t alignment, |
| 189 | const GrBuffer** buffer, |
| 190 | size_t* offset, |
| 191 | size_t* actualSize) { |
| 192 | VALIDATE(); |
| 193 | |
| 194 | SkASSERT(buffer); |
| 195 | SkASSERT(offset); |
| 196 | SkASSERT(actualSize); |
| 197 | |
| 198 | if (fBufferPtr) { |
| 199 | BufferBlock& back = fBlocks.back(); |
| 200 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
| 201 | size_t pad = GrSizeAlignUpPad(usedBytes, alignment); |
| 202 | if ((minSize + pad) <= back.fBytesFree) { |
| 203 | // Consume padding first, to make subsequent alignment math easier |
| 204 | memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad); |
| 205 | usedBytes += pad; |
| 206 | back.fBytesFree -= pad; |
| 207 | fBytesInUse += pad; |
| 208 | |
| 209 | // Give caller all remaining space in this block (but aligned correctly) |
| 210 | size_t size = GrSizeAlignDown(back.fBytesFree, alignment); |
| 211 | *offset = usedBytes; |
| 212 | *buffer = back.fBuffer; |
| 213 | *actualSize = size; |
| 214 | back.fBytesFree -= size; |
| 215 | fBytesInUse += size; |
| 216 | VALIDATE(); |
| 217 | return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // We could honor the space request using by a partial update of the current |
| 222 | // VB (if there is room). But we don't currently use draw calls to GL that |
| 223 | // allow the driver to know that previously issued draws won't read from |
| 224 | // the part of the buffer we update. Also, the GL buffer implementation |
| 225 | // may be cheating on the actual buffer size by shrinking the buffer on |
| 226 | // updateData() if the amount of data passed is less than the full buffer |
| 227 | // size. |
| 228 | |
| 229 | if (!this->createBlock(fallbackSize)) { |
| 230 | return nullptr; |
| 231 | } |
| 232 | SkASSERT(fBufferPtr); |
| 233 | |
| 234 | *offset = 0; |
| 235 | BufferBlock& back = fBlocks.back(); |
| 236 | *buffer = back.fBuffer; |
| 237 | *actualSize = fallbackSize; |
| 238 | back.fBytesFree -= fallbackSize; |
| 239 | fBytesInUse += fallbackSize; |
| 240 | VALIDATE(); |
| 241 | return fBufferPtr; |
| 242 | } |
| 243 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 244 | void GrBufferAllocPool::putBack(size_t bytes) { |
| 245 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 246 | |
| 247 | while (bytes) { |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 248 | // caller shouldn't try to put back more than they've taken |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 249 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 250 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 251 | size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 252 | if (bytes >= bytesUsed) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 253 | bytes -= bytesUsed; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 254 | fBytesInUse -= bytesUsed; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 255 | // 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] | 256 | // beyond it, then unmap it. |
| 257 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 258 | UNMAP_BUFFER(block); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 259 | } |
| 260 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 261 | } else { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 262 | block.fBytesFree += bytes; |
| 263 | fBytesInUse -= bytes; |
| 264 | bytes = 0; |
| 265 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 268 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 269 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | bool GrBufferAllocPool::createBlock(size_t requestSize) { |
| 273 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 274 | size_t size = SkTMax(requestSize, fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 275 | SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 276 | |
| 277 | VALIDATE(); |
| 278 | |
| 279 | BufferBlock& block = fBlocks.push_back(); |
| 280 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 281 | block.fBuffer = this->getBuffer(size); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 282 | if (!block.fBuffer) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 283 | fBlocks.pop_back(); |
| 284 | return false; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 285 | } |
| 286 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 287 | block.fBytesFree = block.fBuffer->gpuMemorySize(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 288 | if (fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 289 | SkASSERT(fBlocks.count() > 1); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 290 | BufferBlock& prev = fBlocks.fromBack(1); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 291 | if (prev.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 292 | UNMAP_BUFFER(prev); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 293 | } else { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 294 | this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 295 | } |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 296 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 297 | } |
| 298 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 299 | SkASSERT(!fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 300 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 301 | // 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] | 302 | // Otherwise when buffer mapping is supported we map if the buffer size is greater than the |
| 303 | // threshold. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 304 | bool attemptMap = block.fBuffer->isCPUBacked(); |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 305 | if (!attemptMap && GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 306 | attemptMap = size > fBufferMapThreshold; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 307 | } |
| 308 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 309 | if (attemptMap) { |
| 310 | fBufferPtr = block.fBuffer->map(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 311 | } |
| 312 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 313 | if (!fBufferPtr) { |
| 314 | fBufferPtr = this->resetCpuData(block.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 315 | } |
| 316 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 317 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 318 | |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | void GrBufferAllocPool::destroyBlock() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 323 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 324 | |
| 325 | BufferBlock& block = fBlocks.back(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 326 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 327 | SkASSERT(!block.fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 328 | block.fBuffer->unref(); |
| 329 | fBlocks.pop_back(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 330 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 331 | } |
| 332 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 333 | void* GrBufferAllocPool::resetCpuData(size_t newSize) { |
| 334 | sk_free(fCpuData); |
| 335 | if (newSize) { |
| 336 | if (fGpu->caps()->mustClearUploadedBufferData()) { |
mtklein | abda35d | 2016-07-14 06:57:31 -0700 | [diff] [blame] | 337 | fCpuData = sk_calloc_throw(newSize); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 338 | } else { |
| 339 | fCpuData = sk_malloc_throw(newSize); |
| 340 | } |
| 341 | } else { |
| 342 | fCpuData = nullptr; |
| 343 | } |
| 344 | return fCpuData; |
| 345 | } |
| 346 | |
| 347 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 348 | void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 349 | GrBuffer* buffer = block.fBuffer; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 350 | SkASSERT(buffer); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 351 | SkASSERT(!buffer->isMapped()); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 352 | SkASSERT(fCpuData == fBufferPtr); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 353 | SkASSERT(flushSize <= buffer->gpuMemorySize()); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 354 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 355 | |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 356 | if (GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() && |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 357 | flushSize > fBufferMapThreshold) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 358 | void* data = buffer->map(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 359 | if (data) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 360 | memcpy(data, fBufferPtr, flushSize); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 361 | UNMAP_BUFFER(block); |
bsalomon@google.com | 71bd1ef | 2011-12-12 20:42:26 +0000 | [diff] [blame] | 362 | return; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | buffer->updateData(fBufferPtr, flushSize); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 366 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 367 | } |
| 368 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 369 | GrBuffer* GrBufferAllocPool::getBuffer(size_t size) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 370 | |
| 371 | GrResourceProvider* rp = fGpu->getContext()->resourceProvider(); |
| 372 | |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 373 | // Shouldn't have to use this flag (https://bug.skia.org/4156) |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 374 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 375 | return rp->createBuffer(size, fBufferType, kDynamic_GrAccessPattern, kFlags); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | //////////////////////////////////////////////////////////////////////////////// |
| 379 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 380 | GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu) |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 381 | : GrBufferAllocPool(gpu, kVertex_GrBufferType, MIN_VERTEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 382 | } |
| 383 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 384 | void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 385 | int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 386 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 387 | int* startVertex) { |
| 388 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 389 | SkASSERT(vertexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 390 | SkASSERT(buffer); |
| 391 | SkASSERT(startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 392 | |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 393 | size_t offset SK_INIT_TO_AVOID_WARNING; |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 394 | void* ptr = INHERITED::makeSpace(vertexSize * vertexCount, |
| 395 | vertexSize, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 396 | buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 397 | &offset); |
| 398 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 399 | SkASSERT(0 == offset % vertexSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 400 | *startVertex = static_cast<int>(offset / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 401 | return ptr; |
| 402 | } |
| 403 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 404 | void* GrVertexBufferAllocPool::makeSpaceAtLeast(size_t vertexSize, int minVertexCount, |
| 405 | int fallbackVertexCount, const GrBuffer** buffer, |
| 406 | int* startVertex, int* actualVertexCount) { |
| 407 | |
| 408 | SkASSERT(minVertexCount >= 0); |
| 409 | SkASSERT(fallbackVertexCount >= minVertexCount); |
| 410 | SkASSERT(buffer); |
| 411 | SkASSERT(startVertex); |
| 412 | SkASSERT(actualVertexCount); |
| 413 | |
| 414 | size_t offset SK_INIT_TO_AVOID_WARNING; |
| 415 | size_t actualSize SK_INIT_TO_AVOID_WARNING; |
| 416 | void* ptr = INHERITED::makeSpaceAtLeast(vertexSize * minVertexCount, |
| 417 | vertexSize * fallbackVertexCount, |
| 418 | vertexSize, |
| 419 | buffer, |
| 420 | &offset, |
| 421 | &actualSize); |
| 422 | |
| 423 | SkASSERT(0 == offset % vertexSize); |
| 424 | *startVertex = static_cast<int>(offset / vertexSize); |
| 425 | |
| 426 | SkASSERT(0 == actualSize % vertexSize); |
| 427 | SkASSERT(actualSize >= vertexSize * minVertexCount); |
| 428 | *actualVertexCount = static_cast<int>(actualSize / vertexSize); |
| 429 | |
| 430 | return ptr; |
| 431 | } |
| 432 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 433 | //////////////////////////////////////////////////////////////////////////////// |
| 434 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 435 | GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu) |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 436 | : GrBufferAllocPool(gpu, kIndex_GrBufferType, MIN_INDEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | void* GrIndexBufferAllocPool::makeSpace(int indexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 440 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 441 | int* startIndex) { |
| 442 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 443 | SkASSERT(indexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 444 | SkASSERT(buffer); |
| 445 | SkASSERT(startIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 446 | |
Robert Phillips | c787e49 | 2017-02-28 11:26:32 -0500 | [diff] [blame] | 447 | size_t offset SK_INIT_TO_AVOID_WARNING; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 448 | void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t), |
| 449 | sizeof(uint16_t), |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 450 | buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 451 | &offset); |
| 452 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 453 | SkASSERT(0 == offset % sizeof(uint16_t)); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 454 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 455 | return ptr; |
| 456 | } |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 457 | |
| 458 | void* GrIndexBufferAllocPool::makeSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
| 459 | const GrBuffer** buffer, int* startIndex, |
| 460 | int* actualIndexCount) { |
| 461 | SkASSERT(minIndexCount >= 0); |
| 462 | SkASSERT(fallbackIndexCount >= minIndexCount); |
| 463 | SkASSERT(buffer); |
| 464 | SkASSERT(startIndex); |
| 465 | SkASSERT(actualIndexCount); |
| 466 | |
| 467 | size_t offset SK_INIT_TO_AVOID_WARNING; |
| 468 | size_t actualSize SK_INIT_TO_AVOID_WARNING; |
| 469 | void* ptr = INHERITED::makeSpaceAtLeast(minIndexCount * sizeof(uint16_t), |
| 470 | fallbackIndexCount * sizeof(uint16_t), |
| 471 | sizeof(uint16_t), |
| 472 | buffer, |
| 473 | &offset, |
| 474 | &actualSize); |
| 475 | |
| 476 | SkASSERT(0 == offset % sizeof(uint16_t)); |
| 477 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
| 478 | |
| 479 | SkASSERT(0 == actualSize % sizeof(uint16_t)); |
| 480 | SkASSERT(actualSize >= minIndexCount * sizeof(uint16_t)); |
| 481 | *actualIndexCount = static_cast<int>(actualSize / sizeof(uint16_t)); |
| 482 | return ptr; |
| 483 | } |