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