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