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