| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 Google Inc. | 2 * Copyright 2010 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #ifndef GrBufferAllocPool_DEFINED | 8 #ifndef GrBufferAllocPool_DEFINED |
| 9 #define GrBufferAllocPool_DEFINED | 9 #define GrBufferAllocPool_DEFINED |
| 10 | 10 |
| 11 #include "SkTArray.h" | 11 #include "SkTArray.h" |
| 12 #include "SkTDArray.h" | 12 #include "SkTDArray.h" |
| 13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 #include "GrTypesPriv.h" | |
| 15 | 14 |
| 16 class GrBuffer; | 15 class GrGeometryBuffer; |
| 17 class GrGpu; | 16 class GrGpu; |
| 18 | 17 |
| 19 /** | 18 /** |
| 20 * A pool of geometry buffers tied to a GrGpu. | 19 * A pool of geometry buffers tied to a GrGpu. |
| 21 * | 20 * |
| 22 * The pool allows a client to make space for geometry and then put back excess | 21 * 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 | 22 * space if it over allocated. When a client is ready to draw from the pool |
| 24 * it calls unmap on the pool ensure buffers are ready for drawing. The pool | 23 * it calls unmap on the pool ensure buffers are ready for drawing. The pool |
| 25 * can be reset after drawing is completed to recycle space. | 24 * can be reset after drawing is completed to recycle space. |
| 26 * | 25 * |
| (...skipping 14 matching lines...) Expand all Loading... |
| 41 */ | 40 */ |
| 42 void reset(); | 41 void reset(); |
| 43 | 42 |
| 44 /** | 43 /** |
| 45 * Frees data from makeSpaces in LIFO order. | 44 * Frees data from makeSpaces in LIFO order. |
| 46 */ | 45 */ |
| 47 void putBack(size_t bytes); | 46 void putBack(size_t bytes); |
| 48 | 47 |
| 49 protected: | 48 protected: |
| 50 /** | 49 /** |
| 50 * Used to determine what type of buffers to create. We could make the |
| 51 * createBuffer a virtual except that we want to use it in the cons for |
| 52 * pre-allocated buffers. |
| 53 */ |
| 54 enum BufferType { |
| 55 kVertex_BufferType, |
| 56 kIndex_BufferType, |
| 57 }; |
| 58 |
| 59 /** |
| 51 * Constructor | 60 * Constructor |
| 52 * | 61 * |
| 53 * @param gpu The GrGpu used to create the buffers. | 62 * @param gpu The GrGpu used to create the buffers. |
| 54 * @param bufferType The type of buffers to create. | 63 * @param bufferType The type of buffers to create. |
| 55 * @param bufferSize The minimum size of created buffers. | 64 * @param bufferSize The minimum size of created buffers. |
| 56 * This value will be clamped to some | 65 * This value will be clamped to some |
| 57 * reasonable minimum. | 66 * reasonable minimum. |
| 58 */ | 67 */ |
| 59 GrBufferAllocPool(GrGpu* gpu, | 68 GrBufferAllocPool(GrGpu* gpu, |
| 60 GrBufferType bufferType, | 69 BufferType bufferType, |
| 61 size_t bufferSize = 0); | 70 size_t bufferSize = 0); |
| 62 | 71 |
| 63 virtual ~GrBufferAllocPool(); | 72 virtual ~GrBufferAllocPool(); |
| 64 | 73 |
| 65 /** | 74 /** |
| 66 * Returns a block of memory to hold data. A buffer designated to hold the | 75 * 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 | 76 * 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: | 77 * returned ptr remains valid until any of the following: |
| 69 * *makeSpace is called again. | 78 * *makeSpace is called again. |
| 70 * *unmap is called. | 79 * *unmap is called. |
| 71 * *reset is called. | 80 * *reset is called. |
| 72 * *this object is destroyed. | 81 * *this object is destroyed. |
| 73 * | 82 * |
| 74 * Once unmap on the pool is called the data is guaranteed to be in the | 83 * Once unmap on the pool is called the data is guaranteed to be in the |
| 75 * buffer at the offset indicated by offset. Until that time it may be | 84 * buffer at the offset indicated by offset. Until that time it may be |
| 76 * in temporary storage and/or the buffer may be locked. | 85 * in temporary storage and/or the buffer may be locked. |
| 77 * | 86 * |
| 78 * @param size the amount of data to make space for | 87 * @param size the amount of data to make space for |
| 79 * @param alignment alignment constraint from start of buffer | 88 * @param alignment alignment constraint from start of buffer |
| 80 * @param buffer returns the buffer that will hold the data. | 89 * @param buffer returns the buffer that will hold the data. |
| 81 * @param offset returns the offset into buffer of the data. | 90 * @param offset returns the offset into buffer of the data. |
| 82 * @return pointer to where the client should write the data. | 91 * @return pointer to where the client should write the data. |
| 83 */ | 92 */ |
| 84 void* makeSpace(size_t size, | 93 void* makeSpace(size_t size, |
| 85 size_t alignment, | 94 size_t alignment, |
| 86 const GrBuffer** buffer, | 95 const GrGeometryBuffer** buffer, |
| 87 size_t* offset); | 96 size_t* offset); |
| 88 | 97 |
| 89 GrBuffer* getBuffer(size_t size); | 98 GrGeometryBuffer* getBuffer(size_t size); |
| 90 | 99 |
| 91 private: | 100 private: |
| 92 struct BufferBlock { | 101 struct BufferBlock { |
| 93 size_t fBytesFree; | 102 size_t fBytesFree; |
| 94 GrBuffer* fBuffer; | 103 GrGeometryBuffer* fBuffer; |
| 95 }; | 104 }; |
| 96 | 105 |
| 97 bool createBlock(size_t requestSize); | 106 bool createBlock(size_t requestSize); |
| 98 void destroyBlock(); | 107 void destroyBlock(); |
| 99 void deleteBlocks(); | 108 void deleteBlocks(); |
| 100 void flushCpuData(const BufferBlock& block, size_t flushSize); | 109 void flushCpuData(const BufferBlock& block, size_t flushSize); |
| 101 void* resetCpuData(size_t newSize); | 110 void* resetCpuData(size_t newSize); |
| 102 #ifdef SK_DEBUG | 111 #ifdef SK_DEBUG |
| 103 void validate(bool unusedBlockAllowed = false) const; | 112 void validate(bool unusedBlockAllowed = false) const; |
| 104 #endif | 113 #endif |
| 105 size_t fBytesInUse; | 114 size_t fBytesInUse; |
| 106 | 115 |
| 107 GrGpu* fGpu; | 116 GrGpu* fGpu; |
| 108 size_t fMinBlockSize; | 117 size_t fMinBlockSize; |
| 109 GrBufferType fBufferType; | 118 BufferType fBufferType; |
| 110 | 119 |
| 111 SkTArray<BufferBlock> fBlocks; | 120 SkTArray<BufferBlock> fBlocks; |
| 112 void* fCpuData; | 121 void* fCpuData; |
| 113 void* fBufferPtr; | 122 void* fBufferPtr; |
| 114 size_t fBufferMapThreshold; | 123 size_t fGeometryBufferMapThreshold; |
| 115 }; | 124 }; |
| 116 | 125 |
| 126 class GrVertexBuffer; |
| 127 |
| 117 /** | 128 /** |
| 118 * A GrBufferAllocPool of vertex buffers | 129 * A GrBufferAllocPool of vertex buffers |
| 119 */ | 130 */ |
| 120 class GrVertexBufferAllocPool : public GrBufferAllocPool { | 131 class GrVertexBufferAllocPool : public GrBufferAllocPool { |
| 121 public: | 132 public: |
| 122 /** | 133 /** |
| 123 * Constructor | 134 * Constructor |
| 124 * | 135 * |
| 125 * @param gpu The GrGpu used to create the vertex buffers. | 136 * @param gpu The GrGpu used to create the vertex buffers. |
| 126 */ | 137 */ |
| (...skipping 15 matching lines...) Expand all Loading... |
| 142 * @param vertexSize specifies size of a vertex to allocate space for | 153 * @param vertexSize specifies size of a vertex to allocate space for |
| 143 * @param vertexCount number of vertices to allocate space for | 154 * @param vertexCount number of vertices to allocate space for |
| 144 * @param buffer returns the vertex buffer that will hold the | 155 * @param buffer returns the vertex buffer that will hold the |
| 145 * vertices. | 156 * vertices. |
| 146 * @param startVertex returns the offset into buffer of the first vertex. | 157 * @param startVertex returns the offset into buffer of the first vertex. |
| 147 * In units of the size of a vertex from layout param. | 158 * In units of the size of a vertex from layout param. |
| 148 * @return pointer to first vertex. | 159 * @return pointer to first vertex. |
| 149 */ | 160 */ |
| 150 void* makeSpace(size_t vertexSize, | 161 void* makeSpace(size_t vertexSize, |
| 151 int vertexCount, | 162 int vertexCount, |
| 152 const GrBuffer** buffer, | 163 const GrVertexBuffer** buffer, |
| 153 int* startVertex); | 164 int* startVertex); |
| 154 | 165 |
| 155 private: | 166 private: |
| 156 typedef GrBufferAllocPool INHERITED; | 167 typedef GrBufferAllocPool INHERITED; |
| 157 }; | 168 }; |
| 158 | 169 |
| 170 class GrIndexBuffer; |
| 171 |
| 159 /** | 172 /** |
| 160 * A GrBufferAllocPool of index buffers | 173 * A GrBufferAllocPool of index buffers |
| 161 */ | 174 */ |
| 162 class GrIndexBufferAllocPool : public GrBufferAllocPool { | 175 class GrIndexBufferAllocPool : public GrBufferAllocPool { |
| 163 public: | 176 public: |
| 164 /** | 177 /** |
| 165 * Constructor | 178 * Constructor |
| 166 * | 179 * |
| 167 * @param gpu The GrGpu used to create the index buffers. | 180 * @param gpu The GrGpu used to create the index buffers. |
| 168 */ | 181 */ |
| (...skipping 11 matching lines...) Expand all Loading... |
| 180 * Once unmap on the pool is called the indices are guaranteed to be in the | 193 * Once unmap on the pool is called the indices are guaranteed to be in the |
| 181 * buffer at the offset indicated by startIndex. Until that time they may be | 194 * buffer at the offset indicated by startIndex. Until that time they may be |
| 182 * in temporary storage and/or the buffer may be locked. | 195 * in temporary storage and/or the buffer may be locked. |
| 183 * | 196 * |
| 184 * @param indexCount number of indices to allocate space for | 197 * @param indexCount number of indices to allocate space for |
| 185 * @param buffer returns the index buffer that will hold the indices. | 198 * @param buffer returns the index buffer that will hold the indices. |
| 186 * @param startIndex returns the offset into buffer of the first index. | 199 * @param startIndex returns the offset into buffer of the first index. |
| 187 * @return pointer to first index. | 200 * @return pointer to first index. |
| 188 */ | 201 */ |
| 189 void* makeSpace(int indexCount, | 202 void* makeSpace(int indexCount, |
| 190 const GrBuffer** buffer, | 203 const GrIndexBuffer** buffer, |
| 191 int* startIndex); | 204 int* startIndex); |
| 192 | 205 |
| 193 private: | 206 private: |
| 194 typedef GrBufferAllocPool INHERITED; | 207 typedef GrBufferAllocPool INHERITED; |
| 195 }; | 208 }; |
| 196 | 209 |
| 197 #endif | 210 #endif |
| OLD | NEW |