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 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 8 | #ifndef GrBufferAllocPool_DEFINED |
| 9 | #define GrBufferAllocPool_DEFINED |
| 10 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 11 | #include "SkTArray.h" |
bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 12 | #include "SkTDArray.h" |
commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 14 | #include "GrTypesPriv.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 15 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 16 | class GrBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 17 | class GrGpu; |
| 18 | |
| 19 | /** |
| 20 | * A pool of geometry buffers tied to a GrGpu. |
| 21 | * |
| 22 | * The pool allows a client to make space for geometry and then put back excess |
| 23 | * space if it over allocated. When a client is ready to draw from the pool |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 24 | * it calls unmap on the pool ensure buffers are ready for drawing. The pool |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 25 | * can be reset after drawing is completed to recycle space. |
| 26 | * |
| 27 | * At creation time a minimum per-buffer size can be specified. Additionally, |
| 28 | * a number of buffers to preallocate can be specified. These will |
| 29 | * be allocated at the min size and kept around until the pool is destroyed. |
| 30 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 31 | class GrBufferAllocPool : SkNoncopyable { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 32 | public: |
| 33 | /** |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 34 | * Ensures all buffers are unmapped and have all data written to them. |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 35 | * Call before drawing using buffers from the pool. |
| 36 | */ |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 37 | void unmap(); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Invalidates all the data in the pool, unrefs non-preallocated buffers. |
| 41 | */ |
| 42 | void reset(); |
| 43 | |
| 44 | /** |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 45 | * Frees data from makeSpaces in LIFO order. |
| 46 | */ |
| 47 | void putBack(size_t bytes); |
| 48 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 49 | protected: |
| 50 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 51 | * Constructor |
| 52 | * |
| 53 | * @param gpu The GrGpu used to create the buffers. |
| 54 | * @param bufferType The type of buffers to create. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 55 | * @param bufferSize The minimum size of created buffers. |
| 56 | * This value will be clamped to some |
| 57 | * reasonable minimum. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | GrBufferAllocPool(GrGpu* gpu, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 60 | GrBufferType bufferType, |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 61 | size_t bufferSize = 0); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 62 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 63 | virtual ~GrBufferAllocPool(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 65 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 66 | * Returns a block of memory to hold data. A buffer designated to hold the |
| 67 | * data is given to the caller. The buffer may or may not be locked. The |
| 68 | * returned ptr remains valid until any of the following: |
| 69 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 70 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 71 | * *reset is called. |
| 72 | * *this object is destroyed. |
| 73 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 74 | * Once unmap on the pool is called the data is guaranteed to be in the |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 75 | * buffer at the offset indicated by offset. Until that time it may be |
| 76 | * in temporary storage and/or the buffer may be locked. |
| 77 | * |
| 78 | * @param size the amount of data to make space for |
| 79 | * @param alignment alignment constraint from start of buffer |
| 80 | * @param buffer returns the buffer that will hold the data. |
| 81 | * @param offset returns the offset into buffer of the data. |
| 82 | * @return pointer to where the client should write the data. |
| 83 | */ |
| 84 | void* makeSpace(size_t size, |
| 85 | size_t alignment, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 86 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | size_t* offset); |
| 88 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 89 | /** |
| 90 | * Returns a block of memory to hold data. A buffer designated to hold the |
| 91 | * data is given to the caller. The buffer may or may not be locked. The |
| 92 | * returned ptr remains valid until any of the following: |
| 93 | * *makeSpace is called again. |
| 94 | * *unmap is called. |
| 95 | * *reset is called. |
| 96 | * *this object is destroyed. |
| 97 | * |
| 98 | * Once unmap on the pool is called the data is guaranteed to be in the |
| 99 | * buffer at the offset indicated by offset. Until that time it may be |
| 100 | * in temporary storage and/or the buffer may be locked. |
| 101 | * |
| 102 | * The caller requests a minimum number of bytes, but the block may be (much) |
| 103 | * larger. Assuming that a new block must be allocated, it will be fallbackSize bytes. |
| 104 | * The actual block size is returned in actualSize. |
| 105 | * |
| 106 | * @param minSize the minimum amount of data to make space for |
| 107 | * @param fallbackSize the amount of data to make space for if a new block is needed |
| 108 | * @param alignment alignment constraint from start of buffer |
| 109 | * @param buffer returns the buffer that will hold the data. |
| 110 | * @param offset returns the offset into buffer of the data. |
| 111 | * @param actualSize returns the capacity of the block |
| 112 | * @return pointer to where the client should write the data. |
| 113 | */ |
| 114 | void* makeSpaceAtLeast(size_t minSize, |
| 115 | size_t fallbackSize, |
| 116 | size_t alignment, |
| 117 | const GrBuffer** buffer, |
| 118 | size_t* offset, |
| 119 | size_t* actualSize); |
| 120 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 121 | GrBuffer* getBuffer(size_t size); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 122 | |
| 123 | private: |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 124 | struct BufferBlock { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 125 | size_t fBytesFree; |
| 126 | GrBuffer* fBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | bool createBlock(size_t requestSize); |
| 130 | void destroyBlock(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 131 | void deleteBlocks(); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 132 | void flushCpuData(const BufferBlock& block, size_t flushSize); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 133 | void* resetCpuData(size_t newSize); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 134 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 135 | void validate(bool unusedBlockAllowed = false) const; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 136 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 137 | size_t fBytesInUse; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 138 | |
| 139 | GrGpu* fGpu; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 140 | size_t fMinBlockSize; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 141 | GrBufferType fBufferType; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 143 | SkTArray<BufferBlock> fBlocks; |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 144 | void* fCpuData; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 145 | void* fBufferPtr; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 146 | size_t fBufferMapThreshold; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 149 | /** |
| 150 | * A GrBufferAllocPool of vertex buffers |
| 151 | */ |
| 152 | class GrVertexBufferAllocPool : public GrBufferAllocPool { |
| 153 | public: |
| 154 | /** |
| 155 | * Constructor |
| 156 | * |
| 157 | * @param gpu The GrGpu used to create the vertex buffers. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 158 | */ |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 159 | GrVertexBufferAllocPool(GrGpu* gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * Returns a block of memory to hold vertices. A buffer designated to hold |
| 163 | * the vertices given to the caller. The buffer may or may not be locked. |
| 164 | * The returned ptr remains valid until any of the following: |
| 165 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 166 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 167 | * *reset is called. |
| 168 | * *this object is destroyed. |
| 169 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 170 | * Once unmap on the pool is called the vertices are guaranteed to be in |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 171 | * the buffer at the offset indicated by startVertex. Until that time they |
| 172 | * may be in temporary storage and/or the buffer may be locked. |
| 173 | * |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 174 | * @param vertexSize specifies size of a vertex to allocate space for |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 175 | * @param vertexCount number of vertices to allocate space for |
| 176 | * @param buffer returns the vertex buffer that will hold the |
| 177 | * vertices. |
| 178 | * @param startVertex returns the offset into buffer of the first vertex. |
| 179 | * In units of the size of a vertex from layout param. |
| 180 | * @return pointer to first vertex. |
| 181 | */ |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 182 | void* makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 183 | int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 184 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 185 | int* startVertex); |
| 186 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 187 | /** |
| 188 | * Returns a block of memory to hold vertices. A buffer designated to hold |
| 189 | * the vertices given to the caller. The buffer may or may not be locked. |
| 190 | * The returned ptr remains valid until any of the following: |
| 191 | * *makeSpace is called again. |
| 192 | * *unmap is called. |
| 193 | * *reset is called. |
| 194 | * *this object is destroyed. |
| 195 | * |
| 196 | * Once unmap on the pool is called the vertices are guaranteed to be in |
| 197 | * the buffer at the offset indicated by startVertex. Until that time they |
| 198 | * may be in temporary storage and/or the buffer may be locked. |
| 199 | * |
| 200 | * The caller requests a minimum number of vertices, but the block may be (much) |
| 201 | * larger. Assuming that a new block must be allocated, it will be sized to hold |
| 202 | * fallbackVertexCount vertices. The actual block size (in vertices) is returned in |
| 203 | * actualVertexCount. |
| 204 | * |
| 205 | * @param vertexSize specifies size of a vertex to allocate space for |
| 206 | * @param minVertexCount minimum number of vertices to allocate space for |
| 207 | * @param fallbackVertexCount number of vertices to allocate space for if a new block is needed |
| 208 | * @param buffer returns the vertex buffer that will hold the vertices. |
| 209 | * @param startVertex returns the offset into buffer of the first vertex. |
| 210 | * In units of the size of a vertex from layout param. |
| 211 | * @param actualVertexCount returns the capacity of the block (in vertices) |
| 212 | * @return pointer to first vertex. |
| 213 | */ |
| 214 | void* makeSpaceAtLeast(size_t vertexSize, |
| 215 | int minVertexCount, |
| 216 | int fallbackVertexCount, |
| 217 | const GrBuffer** buffer, |
| 218 | int* startVertex, |
| 219 | int* actualVertexCount); |
| 220 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 221 | private: |
| 222 | typedef GrBufferAllocPool INHERITED; |
| 223 | }; |
| 224 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 225 | /** |
| 226 | * A GrBufferAllocPool of index buffers |
| 227 | */ |
| 228 | class GrIndexBufferAllocPool : public GrBufferAllocPool { |
| 229 | public: |
| 230 | /** |
| 231 | * Constructor |
| 232 | * |
| 233 | * @param gpu The GrGpu used to create the index buffers. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 234 | */ |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 235 | GrIndexBufferAllocPool(GrGpu* gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 236 | |
| 237 | /** |
| 238 | * Returns a block of memory to hold indices. A buffer designated to hold |
| 239 | * the indices is given to the caller. The buffer may or may not be locked. |
| 240 | * The returned ptr remains valid until any of the following: |
| 241 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 242 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 243 | * *reset is called. |
| 244 | * *this object is destroyed. |
| 245 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 246 | * Once unmap on the pool is called the indices are guaranteed to be in the |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 247 | * buffer at the offset indicated by startIndex. Until that time they may be |
| 248 | * in temporary storage and/or the buffer may be locked. |
| 249 | * |
| 250 | * @param indexCount number of indices to allocate space for |
| 251 | * @param buffer returns the index buffer that will hold the indices. |
| 252 | * @param startIndex returns the offset into buffer of the first index. |
| 253 | * @return pointer to first index. |
| 254 | */ |
| 255 | void* makeSpace(int indexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 256 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 257 | int* startIndex); |
| 258 | |
Brian Osman | 49b7b6f | 2017-06-20 14:43:58 -0400 | [diff] [blame] | 259 | /** |
| 260 | * Returns a block of memory to hold indices. A buffer designated to hold |
| 261 | * the indices is given to the caller. The buffer may or may not be locked. |
| 262 | * The returned ptr remains valid until any of the following: |
| 263 | * *makeSpace is called again. |
| 264 | * *unmap is called. |
| 265 | * *reset is called. |
| 266 | * *this object is destroyed. |
| 267 | * |
| 268 | * Once unmap on the pool is called the indices are guaranteed to be in the |
| 269 | * buffer at the offset indicated by startIndex. Until that time they may be |
| 270 | * in temporary storage and/or the buffer may be locked. |
| 271 | * |
| 272 | * The caller requests a minimum number of indices, but the block may be (much) |
| 273 | * larger. Assuming that a new block must be allocated, it will be sized to hold |
| 274 | * fallbackIndexCount indices. The actual block size (in indices) is returned in |
| 275 | * actualIndexCount. |
| 276 | * |
| 277 | * @param minIndexCount minimum number of indices to allocate space for |
| 278 | * @param fallbackIndexCount number of indices to allocate space for if a new block is needed |
| 279 | * @param buffer returns the index buffer that will hold the indices. |
| 280 | * @param startIndex returns the offset into buffer of the first index. |
| 281 | * @param actualIndexCount returns the capacity of the block (in indices) |
| 282 | * @return pointer to first index. |
| 283 | */ |
| 284 | void* makeSpaceAtLeast(int minIndexCount, |
| 285 | int fallbackIndexCount, |
| 286 | const GrBuffer** buffer, |
| 287 | int* startIndex, |
| 288 | int* actualIndexCount); |
| 289 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 290 | private: |
| 291 | typedef GrBufferAllocPool INHERITED; |
| 292 | }; |
| 293 | |
| 294 | #endif |