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 { \ |
| 33 | TRACE_EVENT_INSTANT1(TRACE_DISABLED_BY_DEFAULT("skia.gpu"), \ |
| 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 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 188 | void GrBufferAllocPool::putBack(size_t bytes) { |
| 189 | VALIDATE(); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 190 | |
| 191 | while (bytes) { |
robertphillips | 91d06bc | 2015-05-06 04:38:36 -0700 | [diff] [blame] | 192 | // 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] | 193 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 194 | BufferBlock& block = fBlocks.back(); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 195 | size_t bytesUsed = block.fBuffer->gpuMemorySize() - block.fBytesFree; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 196 | if (bytes >= bytesUsed) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 197 | bytes -= bytesUsed; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 198 | fBytesInUse -= bytesUsed; |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 199 | // 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] | 200 | // beyond it, then unmap it. |
| 201 | if (block.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 202 | UNMAP_BUFFER(block); |
bsalomon@google.com | 6513cd0 | 2011-08-05 20:12:30 +0000 | [diff] [blame] | 203 | } |
| 204 | this->destroyBlock(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 205 | } else { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 206 | block.fBytesFree += bytes; |
| 207 | fBytesInUse -= bytes; |
| 208 | bytes = 0; |
| 209 | break; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 212 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 213 | VALIDATE(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | bool GrBufferAllocPool::createBlock(size_t requestSize) { |
| 217 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 218 | size_t size = SkTMax(requestSize, fMinBlockSize); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 219 | SkASSERT(size >= GrBufferAllocPool_MIN_BLOCK_SIZE); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 220 | |
| 221 | VALIDATE(); |
| 222 | |
| 223 | BufferBlock& block = fBlocks.push_back(); |
| 224 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 225 | block.fBuffer = this->getBuffer(size); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 226 | if (!block.fBuffer) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 227 | fBlocks.pop_back(); |
| 228 | return false; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 229 | } |
| 230 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 231 | block.fBytesFree = block.fBuffer->gpuMemorySize(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 232 | if (fBufferPtr) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 233 | SkASSERT(fBlocks.count() > 1); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | BufferBlock& prev = fBlocks.fromBack(1); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 235 | if (prev.fBuffer->isMapped()) { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 236 | UNMAP_BUFFER(prev); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 237 | } else { |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 238 | this->flushCpuData(prev, prev.fBuffer->gpuMemorySize() - prev.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 239 | } |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 240 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 241 | } |
| 242 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 243 | SkASSERT(!fBufferPtr); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 244 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 245 | // 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] | 246 | // Otherwise when buffer mapping is supported we map if the buffer size is greater than the |
| 247 | // threshold. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 248 | bool attemptMap = block.fBuffer->isCPUBacked(); |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 249 | if (!attemptMap && GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags()) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 250 | attemptMap = size > fBufferMapThreshold; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 253 | if (attemptMap) { |
| 254 | fBufferPtr = block.fBuffer->map(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 255 | } |
| 256 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 257 | if (!fBufferPtr) { |
| 258 | fBufferPtr = this->resetCpuData(block.fBytesFree); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 259 | } |
| 260 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 261 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 262 | |
| 263 | return true; |
| 264 | } |
| 265 | |
| 266 | void GrBufferAllocPool::destroyBlock() { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 267 | SkASSERT(!fBlocks.empty()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 268 | |
| 269 | BufferBlock& block = fBlocks.back(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 270 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 271 | SkASSERT(!block.fBuffer->isMapped()); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 272 | block.fBuffer->unref(); |
| 273 | fBlocks.pop_back(); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 274 | fBufferPtr = nullptr; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 275 | } |
| 276 | |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 277 | void* GrBufferAllocPool::resetCpuData(size_t newSize) { |
| 278 | sk_free(fCpuData); |
| 279 | if (newSize) { |
| 280 | if (fGpu->caps()->mustClearUploadedBufferData()) { |
mtklein | abda35d | 2016-07-14 06:57:31 -0700 | [diff] [blame] | 281 | fCpuData = sk_calloc_throw(newSize); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 282 | } else { |
| 283 | fCpuData = sk_malloc_throw(newSize); |
| 284 | } |
| 285 | } else { |
| 286 | fCpuData = nullptr; |
| 287 | } |
| 288 | return fCpuData; |
| 289 | } |
| 290 | |
| 291 | |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 292 | void GrBufferAllocPool::flushCpuData(const BufferBlock& block, size_t flushSize) { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 293 | GrBuffer* buffer = block.fBuffer; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 294 | SkASSERT(buffer); |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 295 | SkASSERT(!buffer->isMapped()); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 296 | SkASSERT(fCpuData == fBufferPtr); |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 297 | SkASSERT(flushSize <= buffer->gpuMemorySize()); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 298 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 299 | |
bsalomon | 4b91f76 | 2015-05-19 09:29:46 -0700 | [diff] [blame] | 300 | if (GrCaps::kNone_MapFlags != fGpu->caps()->mapBufferFlags() && |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 301 | flushSize > fBufferMapThreshold) { |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 302 | void* data = buffer->map(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 303 | if (data) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 304 | memcpy(data, fBufferPtr, flushSize); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 305 | UNMAP_BUFFER(block); |
bsalomon@google.com | 71bd1ef | 2011-12-12 20:42:26 +0000 | [diff] [blame] | 306 | return; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | buffer->updateData(fBufferPtr, flushSize); |
bsalomon@google.com | d510809 | 2012-03-08 15:10:39 +0000 | [diff] [blame] | 310 | VALIDATE(true); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 311 | } |
| 312 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 313 | GrBuffer* GrBufferAllocPool::getBuffer(size_t size) { |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 314 | |
| 315 | GrResourceProvider* rp = fGpu->getContext()->resourceProvider(); |
| 316 | |
halcanary | 6950de6 | 2015-11-07 05:29:00 -0800 | [diff] [blame] | 317 | // Shouldn't have to use this flag (https://bug.skia.org/4156) |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 318 | static const uint32_t kFlags = GrResourceProvider::kNoPendingIO_Flag; |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 319 | return rp->createBuffer(size, fBufferType, kDynamic_GrAccessPattern, kFlags); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | //////////////////////////////////////////////////////////////////////////////// |
| 323 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 324 | GrVertexBufferAllocPool::GrVertexBufferAllocPool(GrGpu* gpu) |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 325 | : GrBufferAllocPool(gpu, kVertex_GrBufferType, MIN_VERTEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 326 | } |
| 327 | |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 328 | void* GrVertexBufferAllocPool::makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 329 | int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 330 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 331 | int* startVertex) { |
| 332 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 333 | SkASSERT(vertexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 334 | SkASSERT(buffer); |
| 335 | SkASSERT(startVertex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 336 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 337 | size_t offset = 0; // assign to suppress warning |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 338 | void* ptr = INHERITED::makeSpace(vertexSize * vertexCount, |
| 339 | vertexSize, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 340 | buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 341 | &offset); |
| 342 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 343 | SkASSERT(0 == offset % vertexSize); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 344 | *startVertex = static_cast<int>(offset / vertexSize); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 345 | return ptr; |
| 346 | } |
| 347 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 348 | //////////////////////////////////////////////////////////////////////////////// |
| 349 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 350 | GrIndexBufferAllocPool::GrIndexBufferAllocPool(GrGpu* gpu) |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 351 | : GrBufferAllocPool(gpu, kIndex_GrBufferType, MIN_INDEX_BUFFER_SIZE) { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void* GrIndexBufferAllocPool::makeSpace(int indexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 355 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 356 | int* startIndex) { |
| 357 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 358 | SkASSERT(indexCount >= 0); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 359 | SkASSERT(buffer); |
| 360 | SkASSERT(startIndex); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | 8b48441 | 2011-04-18 19:07:44 +0000 | [diff] [blame] | 362 | size_t offset = 0; // assign to suppress warning |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 363 | void* ptr = INHERITED::makeSpace(indexCount * sizeof(uint16_t), |
| 364 | sizeof(uint16_t), |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 365 | buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 366 | &offset); |
| 367 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 368 | SkASSERT(0 == offset % sizeof(uint16_t)); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 369 | *startIndex = static_cast<int>(offset / sizeof(uint16_t)); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 370 | return ptr; |
| 371 | } |