bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #ifndef GrBufferAllocPool_DEFINED |
| 19 | #define GrBufferAllocPool_DEFINED |
| 20 | |
| 21 | #include "GrNoncopyable.h" |
| 22 | #include "GrTDArray.h" |
| 23 | #include "GrTArray.h" |
| 24 | #include "GrMemory.h" |
| 25 | |
| 26 | class GrGeometryBuffer; |
| 27 | class GrGpu; |
| 28 | |
| 29 | /** |
| 30 | * A pool of geometry buffers tied to a GrGpu. |
| 31 | * |
| 32 | * The pool allows a client to make space for geometry and then put back excess |
| 33 | * space if it over allocated. When a client is ready to draw from the pool |
| 34 | * it calls unlock on the pool ensure buffers are ready for drawing. The pool |
| 35 | * can be reset after drawing is completed to recycle space. |
| 36 | * |
| 37 | * At creation time a minimum per-buffer size can be specified. Additionally, |
| 38 | * a number of buffers to preallocate can be specified. These will |
| 39 | * be allocated at the min size and kept around until the pool is destroyed. |
| 40 | */ |
| 41 | class GrBufferAllocPool : GrNoncopyable { |
| 42 | protected: |
| 43 | |
| 44 | // We could make the createBuffer a virtual except that we want to use it |
| 45 | // in the cons for pre-allocated buffers. |
| 46 | enum BufferType { |
| 47 | kVertex_BufferType, |
| 48 | kIndex_BufferType, |
| 49 | }; |
| 50 | |
| 51 | /** |
| 52 | * Constructor |
| 53 | * |
| 54 | * @param gpu The GrGpu used to create the buffers. |
| 55 | * @param bufferType The type of buffers to create. |
| 56 | * @param frequentResetHint A hint that indicates that the pool |
| 57 | * should expect frequent unlock() calls |
| 58 | * (as opposed to many makeSpace / acquires |
| 59 | * between resets). |
| 60 | * @param bufferSize The minimum size of created buffers. |
| 61 | * This value will be clamped to some |
| 62 | * reasonable minimum. |
| 63 | * @param preallocBufferCnt The pool will allocate this number of |
| 64 | * buffers at bufferSize and keep them until it |
| 65 | * is destroyed. |
| 66 | */ |
| 67 | GrBufferAllocPool(GrGpu* gpu, |
| 68 | BufferType bufferType, |
| 69 | bool frequentResetHint, |
| 70 | size_t bufferSize = 0, |
| 71 | int preallocBufferCnt = 0); |
| 72 | |
| 73 | virtual ~GrBufferAllocPool(); |
| 74 | |
| 75 | public: |
| 76 | /** |
| 77 | * Ensures all buffers are unlocked and have all data written to them. |
| 78 | * Call before drawing using buffers from the pool. |
| 79 | */ |
| 80 | void unlock(); |
| 81 | |
| 82 | /** |
| 83 | * Invalidates all the data in the pool, unrefs non-preallocated buffers. |
| 84 | */ |
| 85 | void reset(); |
| 86 | |
| 87 | /** |
| 88 | * Gets the number of preallocated buffers that are yet to be used. |
| 89 | */ |
| 90 | int preallocatedBuffersRemaining() const; |
| 91 | |
| 92 | /** |
| 93 | * gets the number of preallocated buffers |
| 94 | */ |
| 95 | int preallocatedBufferCount() const; |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Frees data from makeSpaces in LIFO order. |
| 100 | */ |
| 101 | void putBack(size_t bytes); |
| 102 | |
| 103 | /** |
| 104 | * Gets the GrGpu that this pool is associated with. |
| 105 | */ |
| 106 | GrGpu* getGpu() { return fGpu; } |
| 107 | |
| 108 | protected: |
| 109 | /** |
| 110 | * Gets the size of the preallocated buffers. |
| 111 | * |
| 112 | * @return the size of preallocated buffers. |
| 113 | */ |
| 114 | size_t preallocatedBufferSize() const { |
| 115 | return fPreallocBuffers.count() ? fMinBlockSize : 0; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Returns a block of memory to hold data. A buffer designated to hold the |
| 120 | * data is given to the caller. The buffer may or may not be locked. The |
| 121 | * returned ptr remains valid until any of the following: |
| 122 | * *makeSpace is called again. |
| 123 | * *unlock is called. |
| 124 | * *reset is called. |
| 125 | * *this object is destroyed. |
| 126 | * |
| 127 | * Once unlock on the pool is called the data is guaranteed to be in the |
| 128 | * buffer at the offset indicated by offset. Until that time it may be |
| 129 | * in temporary storage and/or the buffer may be locked. |
| 130 | * |
| 131 | * @param size the amount of data to make space for |
| 132 | * @param alignment alignment constraint from start of buffer |
| 133 | * @param buffer returns the buffer that will hold the data. |
| 134 | * @param offset returns the offset into buffer of the data. |
| 135 | * @return pointer to where the client should write the data. |
| 136 | */ |
| 137 | void* makeSpace(size_t size, |
| 138 | size_t alignment, |
| 139 | const GrGeometryBuffer** buffer, |
| 140 | size_t* offset); |
| 141 | |
| 142 | /** |
| 143 | * Gets the number of items of a size that can be added to the current |
| 144 | * buffer without spilling to another buffer. If the pool has been reset, or |
| 145 | * the previous makeSpace completely exhausted a buffer then the returned |
| 146 | * size will be the size of the next available preallocated buffer, or zero |
| 147 | * if no preallocated buffer remains available. It is assumed that items |
| 148 | * should be itemSize-aligned from the start of a buffer. |
| 149 | * |
| 150 | * @return the number of items that would fit in the current buffer. |
| 151 | */ |
| 152 | int currentBufferItems(size_t itemSize) const; |
| 153 | |
| 154 | GrGeometryBuffer* createBuffer(size_t size); |
| 155 | |
| 156 | private: |
| 157 | |
| 158 | struct BufferBlock { |
| 159 | size_t fBytesFree; |
| 160 | GrGeometryBuffer* fBuffer; |
| 161 | }; |
| 162 | |
| 163 | bool createBlock(size_t requestSize); |
| 164 | void destroyBlock(); |
| 165 | void flushCpuData(GrGeometryBuffer* buffer, size_t flushSize); |
| 166 | #if GR_DEBUG |
| 167 | void validate() const; |
| 168 | #endif |
| 169 | |
| 170 | GrGpu* fGpu; |
| 171 | bool fFrequentResetHint; |
| 172 | GrTDArray<GrGeometryBuffer*> fPreallocBuffers; |
| 173 | size_t fMinBlockSize; |
| 174 | BufferType fBufferType; |
| 175 | |
| 176 | GrTArray<BufferBlock> fBlocks; |
| 177 | int fPreallocBuffersInUse; |
| 178 | int fFirstPreallocBuffer; |
| 179 | GrAutoMalloc fCpuData; |
| 180 | void* fBufferPtr; |
| 181 | }; |
| 182 | |
| 183 | class GrVertexBuffer; |
| 184 | |
| 185 | /** |
| 186 | * A GrBufferAllocPool of vertex buffers |
| 187 | */ |
| 188 | class GrVertexBufferAllocPool : public GrBufferAllocPool { |
| 189 | public: |
| 190 | /** |
| 191 | * Constructor |
| 192 | * |
| 193 | * @param gpu The GrGpu used to create the vertex buffers. |
| 194 | * @param frequentResetHint A hint that indicates that the pool |
| 195 | * should expect frequent unlock() calls |
| 196 | * (as opposed to many makeSpace / acquires |
| 197 | * between resets). |
| 198 | * @param bufferSize The minimum size of created VBs This value |
| 199 | * will be clamped to some reasonable minimum. |
| 200 | * @param preallocBufferCnt The pool will allocate this number of VBs at |
| 201 | * bufferSize and keep them until it is |
| 202 | * destroyed. |
| 203 | */ |
| 204 | GrVertexBufferAllocPool(GrGpu* gpu, |
| 205 | bool frequentResetHint, |
| 206 | size_t bufferSize = 0, |
| 207 | int preallocBufferCnt = 0); |
| 208 | |
| 209 | /** |
| 210 | * Returns a block of memory to hold vertices. A buffer designated to hold |
| 211 | * the vertices given to the caller. The buffer may or may not be locked. |
| 212 | * The returned ptr remains valid until any of the following: |
| 213 | * *makeSpace is called again. |
| 214 | * *unlock is called. |
| 215 | * *reset is called. |
| 216 | * *this object is destroyed. |
| 217 | * |
| 218 | * Once unlock on the pool is called the vertices are guaranteed to be in |
| 219 | * the buffer at the offset indicated by startVertex. Until that time they |
| 220 | * may be in temporary storage and/or the buffer may be locked. |
| 221 | * |
| 222 | * @param layout specifies type of vertices to allocate space for |
| 223 | * @param vertexCount number of vertices to allocate space for |
| 224 | * @param buffer returns the vertex buffer that will hold the |
| 225 | * vertices. |
| 226 | * @param startVertex returns the offset into buffer of the first vertex. |
| 227 | * In units of the size of a vertex from layout param. |
| 228 | * @return pointer to first vertex. |
| 229 | */ |
| 230 | void* makeSpace(GrVertexLayout layout, |
| 231 | int vertexCount, |
| 232 | const GrVertexBuffer** buffer, |
| 233 | int* startVertex); |
| 234 | |
| 235 | /** |
| 236 | * Shortcut to make space and then write verts into the made space. |
| 237 | */ |
| 238 | bool appendVertices(GrVertexLayout layout, |
| 239 | int vertexCount, |
| 240 | const void* vertices, |
| 241 | const GrVertexBuffer** buffer, |
| 242 | int* startVertex); |
| 243 | |
| 244 | /** |
| 245 | * Gets the number of vertices that can be added to the current VB without |
| 246 | * spilling to another VB. If the pool has been reset, or the previous |
| 247 | * makeSpace completely exhausted a VB then the returned number of vertices |
| 248 | * would fit in the next available preallocated buffer. If any makeSpace |
| 249 | * would force a new VB to be created the return value will be zero. |
| 250 | * |
| 251 | * @param the format of vertices to compute space for. |
| 252 | * @return the number of vertices that would fit in the current buffer. |
| 253 | */ |
| 254 | int currentBufferVertices(GrVertexLayout layout) const; |
| 255 | |
| 256 | /** |
| 257 | * Gets the number of vertices that can fit in a preallocated vertex buffer. |
| 258 | * Zero if no preallocated buffers. |
| 259 | * |
| 260 | * @param the format of vertices to compute space for. |
| 261 | * |
| 262 | * @return number of vertices that fit in one of the preallocated vertex |
| 263 | * buffers. |
| 264 | */ |
| 265 | int preallocatedBufferVertices(GrVertexLayout layout) const; |
| 266 | |
| 267 | private: |
| 268 | typedef GrBufferAllocPool INHERITED; |
| 269 | }; |
| 270 | |
| 271 | class GrIndexBuffer; |
| 272 | |
| 273 | /** |
| 274 | * A GrBufferAllocPool of index buffers |
| 275 | */ |
| 276 | class GrIndexBufferAllocPool : public GrBufferAllocPool { |
| 277 | public: |
| 278 | /** |
| 279 | * Constructor |
| 280 | * |
| 281 | * @param gpu The GrGpu used to create the index buffers. |
| 282 | * @param frequentResetHint A hint that indicates that the pool |
| 283 | * should expect frequent unlock() calls |
| 284 | * (as opposed to many makeSpace / acquires |
| 285 | * between resets). |
| 286 | * @param bufferSize The minimum size of created IBs This value |
| 287 | * will be clamped to some reasonable minimum. |
| 288 | * @param preallocBufferCnt The pool will allocate this number of VBs at |
| 289 | * bufferSize and keep them until it is |
| 290 | * destroyed. |
| 291 | */ |
| 292 | GrIndexBufferAllocPool(GrGpu* gpu, |
| 293 | bool frequentResetHint, |
| 294 | size_t bufferSize = 0, |
| 295 | int preallocBufferCnt = 0); |
| 296 | |
| 297 | /** |
| 298 | * Returns a block of memory to hold indices. A buffer designated to hold |
| 299 | * the indices is given to the caller. The buffer may or may not be locked. |
| 300 | * The returned ptr remains valid until any of the following: |
| 301 | * *makeSpace is called again. |
| 302 | * *unlock is called. |
| 303 | * *reset is called. |
| 304 | * *this object is destroyed. |
| 305 | * |
| 306 | * Once unlock on the pool is called the indices are guaranteed to be in the |
| 307 | * buffer at the offset indicated by startIndex. Until that time they may be |
| 308 | * in temporary storage and/or the buffer may be locked. |
| 309 | * |
| 310 | * @param indexCount number of indices to allocate space for |
| 311 | * @param buffer returns the index buffer that will hold the indices. |
| 312 | * @param startIndex returns the offset into buffer of the first index. |
| 313 | * @return pointer to first index. |
| 314 | */ |
| 315 | void* makeSpace(int indexCount, |
| 316 | const GrIndexBuffer** buffer, |
| 317 | int* startIndex); |
| 318 | |
| 319 | /** |
| 320 | * Shortcut to make space and then write indices into the made space. |
| 321 | */ |
| 322 | bool appendIndices(int indexCount, |
| 323 | const void* indices, |
| 324 | const GrIndexBuffer** buffer, |
| 325 | int* startIndex); |
| 326 | |
| 327 | /** |
| 328 | * Gets the number of indices that can be added to the current IB without |
| 329 | * spilling to another IB. If the pool has been reset, or the previous |
| 330 | * makeSpace completely exhausted a IB then the returned number of indices |
| 331 | * would fit in the next available preallocated buffer. If any makeSpace |
| 332 | * would force a new IB to be created the return value will be zero. |
| 333 | */ |
| 334 | int currentBufferIndices() const; |
| 335 | |
| 336 | /** |
| 337 | * Gets the number of indices that can fit in a preallocated index buffer. |
| 338 | * Zero if no preallocated buffers. |
| 339 | * |
| 340 | * @return number of indices that fit in one of the preallocated index |
| 341 | * buffers. |
| 342 | */ |
| 343 | int preallocatedBufferIndices() const; |
| 344 | |
| 345 | private: |
| 346 | typedef GrBufferAllocPool INHERITED; |
| 347 | }; |
| 348 | |
| 349 | #endif |