epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 10 | #include "GrBufferAllocPool.h" |
bsalomon | 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" |
| 14 | #include "GrIndexBuffer.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" |
| 17 | #include "GrVertexBuffer.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 18 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 19 | #include "SkTraceEvent.h" |
| 20 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 21 | #ifdef SK_DEBUG |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 22 | #define VALIDATE validate |
| 23 | #else |
sugoi@google.com | e0e385c | 2013-03-11 18:50:03 +0000 | [diff] [blame] | 24 | static void VALIDATE(bool = false) {} |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | #endif |
| 26 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 27 | static const size_t MIN_VERTEX_BUFFER_SIZE = 1 << 15; |
| 28 | static const size_t MIN_INDEX_BUFFER_SIZE = 1 << 12; |
| 29 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 30 | // page size |
joshualitt | 0709ca0 | 2015-06-04 09:13:46 -0700 | [diff] [blame] | 31 | #define GrBufferAllocPool_MIN_BLOCK_SIZE ((size_t)1 << 15) |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 32 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 33 | #define UNMAP_BUFFER(block) \ |
| 34 | do { \ |
| 35 | TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), \ |
| 36 | "GrBufferAllocPool Unmapping Buffer", \ |
| 37 | TRACE_EVENT_SCOPE_THREAD, \ |
| 38 | "percent_unwritten", \ |
| 39 | (float)((block).fBytesFree) / (block).fBuffer->gpuMemorySize()); \ |
| 40 | (block).fBuffer->unmap(); \ |
| 41 | } while (false) |
| 42 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 43 | GrBufferAllocPool::GrBufferAllocPool(GrGpu* gpu, |
| 44 | BufferType bufferType, |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 45 | size_t blockSize) |
| 46 | : fBlocks(8) { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 47 | |
bsalomon | ecb8e3e | 2015-04-29 04:33:52 -0700 | [diff] [blame] | 48 | fGpu = SkRef(gpu); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 49 | fCpuData = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 50 | fBufferType = bufferType; |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 51 | fBufferPtr = nullptr; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 52 | fMinBlockSize = SkTMax(GrBufferAllocPool_MIN_BLOCK_SIZE, blockSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 53 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 54 | fBytesInUse = 0; |
robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 55 | |
joshualitt | 7224c86 | 2015-05-29 06:46:47 -0700 | [diff] [blame] | 56 | fGeometryBufferMapThreshold = gpu->caps()->geometryBufferMapThreshold(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 57 | } |
| 58 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 59 | void GrBufferAllocPool::deleteBlocks() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 60 | if (fBlocks.count()) { |
| 61 | GrGeometryBuffer* buffer = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 62 | if (buffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 63 | UNMAP_BUFFER(fBlocks.back()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 66 | while (!fBlocks.empty()) { |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 67 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 68 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 69 | SkASSERT(!fBufferPtr); |
| 70 | } |
| 71 | |
| 72 | GrBufferAllocPool::~GrBufferAllocPool() { |
| 73 | VALIDATE(); |
| 74 | this->deleteBlocks(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 75 | sk_free(fCpuData); |
bsalomon | ecb8e3e | 2015-04-29 04:33:52 -0700 | [diff] [blame] | 76 | fGpu->unref(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void GrBufferAllocPool::reset() { |
| 80 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 81 | fBytesInUse = 0; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 82 | this->deleteBlocks(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 83 | |
| 84 | // we may have created a large cpu mirror of a large VB. Reset the size to match our minimum. |
| 85 | this->resetCpuData(fMinBlockSize); |
| 86 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | VALIDATE(); |
| 88 | } |
| 89 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 90 | void GrBufferAllocPool::unmap() { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 91 | VALIDATE(); |
| 92 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 93 | if (fBufferPtr) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 94 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 95 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 96 | UNMAP_BUFFER(block); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 97 | } else { |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 98 | size_t flushSize = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 99 | this->flushCpuData(fBlocks.back(), flushSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 100 | } |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 101 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 102 | } |
| 103 | VALIDATE(); |
| 104 | } |
| 105 | |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 106 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 107 | void GrBufferAllocPool::validate(bool unusedBlockAllowed) const { |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 108 | bool wasDestroyed = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 109 | if (fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 110 | SkASSERT(!fBlocks.empty()); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 111 | if (fBlocks.back().fBuffer->isMapped()) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 112 | GrGeometryBuffer* buf = fBlocks.back().fBuffer; |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 113 | SkASSERT(buf->mapPtr() == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 114 | } else { |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 115 | SkASSERT(fCpuData == fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 116 | } |
| 117 | } else { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 118 | SkASSERT(fBlocks.empty() || !fBlocks.back().fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 119 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 120 | size_t bytesInUse = 0; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 121 | for (int i = 0; i < fBlocks.count() - 1; ++i) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 122 | SkASSERT(!fBlocks[i].fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 123 | } |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 124 | for (int i = 0; !wasDestroyed && i < fBlocks.count(); ++i) { |
| 125 | if (fBlocks[i].fBuffer->wasDestroyed()) { |
| 126 | wasDestroyed = true; |
| 127 | } else { |
| 128 | size_t bytes = fBlocks[i].fBuffer->gpuMemorySize() - fBlocks[i].fBytesFree; |
| 129 | bytesInUse += bytes; |
| 130 | SkASSERT(bytes || unusedBlockAllowed); |
| 131 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 132 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 133 | |
bsalomon | 71cb0c2 | 2014-11-14 12:10:14 -0800 | [diff] [blame] | 134 | if (!wasDestroyed) { |
| 135 | SkASSERT(bytesInUse == fBytesInUse); |
| 136 | if (unusedBlockAllowed) { |
| 137 | SkASSERT((fBytesInUse && !fBlocks.empty()) || |
| 138 | (!fBytesInUse && (fBlocks.count() < 2))); |
| 139 | } else { |
| 140 | SkASSERT((0 == fBytesInUse) == fBlocks.empty()); |
| 141 | } |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 142 | } |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 143 | } |
| 144 | #endif |
| 145 | |
| 146 | void* GrBufferAllocPool::makeSpace(size_t size, |
| 147 | size_t alignment, |
| 148 | const GrGeometryBuffer** buffer, |
| 149 | size_t* offset) { |
| 150 | VALIDATE(); |
| 151 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 152 | SkASSERT(buffer); |
| 153 | SkASSERT(offset); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 154 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 155 | if (fBufferPtr) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 156 | BufferBlock& back = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 157 | size_t usedBytes = back.fBuffer->gpuMemorySize() - back.fBytesFree; |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 158 | size_t pad = GrSizeAlignUpPad(usedBytes, alignment); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 159 | if ((size + pad) <= back.fBytesFree) { |
dongseong.hwang | 8f25c66 | 2015-01-22 10:40:20 -0800 | [diff] [blame] | 160 | memset((void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes), 0, pad); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 161 | usedBytes += pad; |
| 162 | *offset = usedBytes; |
| 163 | *buffer = back.fBuffer; |
| 164 | back.fBytesFree -= size + pad; |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 165 | fBytesInUse += size + pad; |
| 166 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 167 | return (void*)(reinterpret_cast<intptr_t>(fBufferPtr) + usedBytes); |
| 168 | } |
| 169 | } |
| 170 | |
bsalomon@google.com | 96e96df | 2011-10-10 14:49:29 +0000 | [diff] [blame] | 171 | // We could honor the space request using by a partial update of the current |
| 172 | // 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] | 173 | // 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] | 174 | // the part of the buffer we update. Also, the GL buffer implementation |
| 175 | // may be cheating on the actual buffer size by shrinking the buffer on |
| 176 | // updateData() if the amount of data passed is less than the full buffer |
| 177 | // size. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 178 | |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 179 | if (!this->createBlock(size)) { |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 180 | return nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 181 | } |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 182 | SkASSERT(fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 183 | |
| 184 | *offset = 0; |
| 185 | BufferBlock& back = fBlocks.back(); |
| 186 | *buffer = back.fBuffer; |
| 187 | back.fBytesFree -= size; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 188 | fBytesInUse += size; |
| 189 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 190 | return fBufferPtr; |
| 191 | } |
| 192 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 193 | void GrBufferAllocPool::putBack(size_t bytes) { |
| 194 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 195 | |
| 196 | while (bytes) { |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 197 | // 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] | 198 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 199 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 200 | size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 201 | if (bytes >= bytesUsed) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 202 | bytes -= bytesUsed; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 203 | fBytesInUse -= bytesUsed; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 204 | // 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] | 205 | // beyond it, then unmap it. |
| 206 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 207 | UNMAP_BUFFER(block); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 208 | } |
| 209 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 210 | } else { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 211 | block.fBytesFree += bytes; |
| 212 | fBytesInUse -= bytes; |
| 213 | bytes = 0; |
| 214 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 217 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 218 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | bool GrBufferAllocPool::createBlock(size_t requestSize) { |
| 222 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 223 | size_t size = SkTMax(requestSize, fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 224 | SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 225 | |
| 226 | VALIDATE(); |
| 227 | |
| 228 | BufferBlock& block = fBlocks.push_back(); |
| 229 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 230 | block.fBuffer = this->getBuffer(size); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 231 | if (!block.fBuffer) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 232 | fBlocks.pop_back(); |
| 233 | return false; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | } |
| 235 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 236 | block.fBytesFree = block.fBuffer->gpuMemorySize(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 237 | if (fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 238 | SkASSERT(fBlocks.count() > 1); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 239 | BufferBlock& prev = fBlocks.fromBack(1); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 240 | if (prev.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 241 | UNMAP_BUFFER(prev); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 242 | } else { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 243 | this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 244 | } |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 245 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 246 | } |
| 247 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 248 | SkASSERT(!fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 249 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 250 | // 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] | 251 | // Otherwise when buffer mapping is supported we map if the buffer size is greater than the |
| 252 | // threshold. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 253 | bool attemptMap = block.fBuffer->isCPUBacked(); |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 254 | if (!attemptMap && GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) { |
joshualitt | 7224c86 | 2015-05-29 06:46:47 -0700 | [diff] [blame] | 255 | attemptMap = size > fGeometryBufferMapThreshold; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 256 | } |
| 257 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 258 | if (attemptMap) { |
| 259 | fBufferPtr = block.fBuffer->map(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 260 | } |
| 261 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 262 | if (!fBufferPtr) { |
| 263 | fBufferPtr = this->resetCpuData(block.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 264 | } |
| 265 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 266 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 267 | |
| 268 | return true; |
| 269 | } |
| 270 | |
| 271 | void GrBufferAllocPool::destroyBlock() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 272 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 273 | |
| 274 | BufferBlock& block = fBlocks.back(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 275 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 276 | SkASSERT(!block.fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 277 | block.fBuffer->unref(); |
| 278 | fBlocks.pop_back(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 279 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 280 | } |
| 281 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 282 | void* GrBufferAllocPool::resetCpuData(size_t newSize) { |
| 283 | sk_free(fCpuData); |
| 284 | if (newSize) { |
| 285 | if (fGpu->caps()->mustClearUploadedBufferData()) { |
| 286 | fCpuData = sk_calloc(newSize); |
| 287 | } else { |
| 288 | fCpuData = sk_malloc_throw(newSize); |
| 289 | } |
| 290 | } else { |
| 291 | fCpuData = nullptr; |
| 292 | } |
| 293 | return fCpuData; |
| 294 | } |
| 295 | |
| 296 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 297 | void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) { |
| 298 | GrGeometryBuffer* buffer = block.fBuffer; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 299 | SkASSERT(buffer); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 300 | SkASSERT(!buffer->isMapped()); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 301 | SkASSERT(fCpuData == fBufferPtr); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 302 | SkASSERT(flushSize <= buffer->gpuMemorySize()); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 303 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 304 | |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 305 | if (GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() && |
joshualitt | 7224c86 | 2015-05-29 06:46:47 -0700 | [diff] [blame] | 306 | flushSize > fGeometryBufferMapThreshold) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 307 | void* data = buffer->map(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 308 | if (data) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 309 | memcpy(data, fBufferPtr, flushSize); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 310 | UNMAP_BUFFER(block); |
bsalomon@google.com | 71bd1ef | 2011-12-12 20:42:26 +0000 | [diff] [blame] | 311 | return; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | buffer->updateData(fBufferPtr, flushSize); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 315 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 316 | } |
| 317 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 318 | GrGeometryBuffer* GrBufferAllocPool::getBuffer(size_t size) { |
| 319 | |
| 320 | GrResourceProvider* rp = fGpu->getContext()->resourceProvider(); |
| 321 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 322 | static const GrResourceProvider::BufferUsage kUsage = GrResourceProvider::kDynamic_BufferUsage; |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame^] | 323 | // Shouldn't have to use this flag (https://bug.skia.org/4156) |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 324 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 325 | if (kIndex_BufferType == fBufferType) { |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 326 | return rp->createIndexBuffer(size, kUsage, kFlags); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 327 | } else { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 328 | SkASSERT(kVertex_BufferType == fBufferType); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 329 | return rp->createVertexBuffer(size, kUsage, kFlags); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | //////////////////////////////////////////////////////////////////////////////// |
| 334 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 335 | GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu) |
| 336 | : GrBufferAllocPool(gpu, kVertex_BufferType, MIN_VERTEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 337 | } |
| 338 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 339 | void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 340 | int vertexCount, |
| 341 | const GrVertexBuffer** buffer, |
| 342 | int* startVertex) { |
| 343 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 344 | SkASSERT(vertexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 345 | SkASSERT(buffer); |
| 346 | SkASSERT(startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 347 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 348 | size_t offset = 0; // assign to suppress warning |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 349 | const GrGeometryBuffer* geomBuffer = nullptr; // assign to suppress warning |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 350 | void* ptr = INHERITED::makeSpace(vertexSize * vertexCount, |
| 351 | vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 352 | &geomBuffer, |
| 353 | &offset); |
| 354 | |
| 355 | *buffer = (const GrVertexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 356 | SkASSERT(0 == offset % vertexSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 357 | *startVertex = static_cast<int>(offset / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 358 | return ptr; |
| 359 | } |
| 360 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 361 | //////////////////////////////////////////////////////////////////////////////// |
| 362 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 363 | GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu) |
| 364 | : GrBufferAllocPool(gpu, kIndex_BufferType, MIN_INDEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | void* GrIndexBufferAllocPool::makeSpace(int indexCount, |
| 368 | const GrIndexBuffer** buffer, |
| 369 | int* startIndex) { |
| 370 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 371 | SkASSERT(indexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 372 | SkASSERT(buffer); |
| 373 | SkASSERT(startIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 374 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 375 | size_t offset = 0; // assign to suppress warning |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 376 | const GrGeometryBuffer* geomBuffer = nullptr; // assign to suppress warning |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 377 | void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t), |
| 378 | sizeof(uint16_t), |
| 379 | &geomBuffer, |
| 380 | &offset); |
| 381 | |
| 382 | *buffer = (const GrIndexBuffer*) geomBuffer; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 383 | SkASSERT(0 == offset % sizeof(uint16_t)); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 384 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 385 | return ptr; |
| 386 | } |