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 |
(...skipping 23 matching lines...) Expand all Loading... |
34 * Call before drawing using buffers from the pool. | 34 * Call before drawing using buffers from the pool. |
35 */ | 35 */ |
36 void unmap(); | 36 void unmap(); |
37 | 37 |
38 /** | 38 /** |
39 * Invalidates all the data in the pool, unrefs non-preallocated buffers. | 39 * Invalidates all the data in the pool, unrefs non-preallocated buffers. |
40 */ | 40 */ |
41 void reset(); | 41 void reset(); |
42 | 42 |
43 /** | 43 /** |
44 * Gets the number of preallocated buffers that are yet to be used. | |
45 */ | |
46 int preallocatedBuffersRemaining() const; | |
47 | |
48 /** | |
49 * gets the number of preallocated buffers | |
50 */ | |
51 int preallocatedBufferCount() const; | |
52 | |
53 /** | |
54 * Frees data from makeSpaces in LIFO order. | 44 * Frees data from makeSpaces in LIFO order. |
55 */ | 45 */ |
56 void putBack(size_t bytes); | 46 void putBack(size_t bytes); |
57 | 47 |
58 /** | |
59 * Gets the GrGpu that this pool is associated with. | |
60 */ | |
61 GrGpu* getGpu() { return fGpu; } | |
62 | |
63 protected: | 48 protected: |
64 /** | 49 /** |
65 * Used to determine what type of buffers to create. We could make the | 50 * Used to determine what type of buffers to create. We could make the |
66 * createBuffer a virtual except that we want to use it in the cons for | 51 * createBuffer a virtual except that we want to use it in the cons for |
67 * pre-allocated buffers. | 52 * pre-allocated buffers. |
68 */ | 53 */ |
69 enum BufferType { | 54 enum BufferType { |
70 kVertex_BufferType, | 55 kVertex_BufferType, |
71 kIndex_BufferType, | 56 kIndex_BufferType, |
72 }; | 57 }; |
(...skipping 11 matching lines...) Expand all Loading... |
84 * is destroyed. | 69 * is destroyed. |
85 */ | 70 */ |
86 GrBufferAllocPool(GrGpu* gpu, | 71 GrBufferAllocPool(GrGpu* gpu, |
87 BufferType bufferType, | 72 BufferType bufferType, |
88 size_t bufferSize = 0, | 73 size_t bufferSize = 0, |
89 int preallocBufferCnt = 0); | 74 int preallocBufferCnt = 0); |
90 | 75 |
91 virtual ~GrBufferAllocPool(); | 76 virtual ~GrBufferAllocPool(); |
92 | 77 |
93 /** | 78 /** |
94 * Gets the size of the preallocated buffers. | |
95 * | |
96 * @return the size of preallocated buffers. | |
97 */ | |
98 size_t preallocatedBufferSize() const { | |
99 return fPreallocBuffers.count() ? fMinBlockSize : 0; | |
100 } | |
101 | |
102 /** | |
103 * Returns a block of memory to hold data. A buffer designated to hold the | 79 * Returns a block of memory to hold data. A buffer designated to hold the |
104 * data is given to the caller. The buffer may or may not be locked. The | 80 * data is given to the caller. The buffer may or may not be locked. The |
105 * returned ptr remains valid until any of the following: | 81 * returned ptr remains valid until any of the following: |
106 * *makeSpace is called again. | 82 * *makeSpace is called again. |
107 * *unmap is called. | 83 * *unmap is called. |
108 * *reset is called. | 84 * *reset is called. |
109 * *this object is destroyed. | 85 * *this object is destroyed. |
110 * | 86 * |
111 * Once unmap on the pool is called the data is guaranteed to be in the | 87 * Once unmap on the pool is called the data is guaranteed to be in the |
112 * buffer at the offset indicated by offset. Until that time it may be | 88 * buffer at the offset indicated by offset. Until that time it may be |
113 * in temporary storage and/or the buffer may be locked. | 89 * in temporary storage and/or the buffer may be locked. |
114 * | 90 * |
115 * @param size the amount of data to make space for | 91 * @param size the amount of data to make space for |
116 * @param alignment alignment constraint from start of buffer | 92 * @param alignment alignment constraint from start of buffer |
117 * @param buffer returns the buffer that will hold the data. | 93 * @param buffer returns the buffer that will hold the data. |
118 * @param offset returns the offset into buffer of the data. | 94 * @param offset returns the offset into buffer of the data. |
119 * @return pointer to where the client should write the data. | 95 * @return pointer to where the client should write the data. |
120 */ | 96 */ |
121 void* makeSpace(size_t size, | 97 void* makeSpace(size_t size, |
122 size_t alignment, | 98 size_t alignment, |
123 const GrGeometryBuffer** buffer, | 99 const GrGeometryBuffer** buffer, |
124 size_t* offset); | 100 size_t* offset); |
125 | 101 |
126 /** | |
127 * Gets the number of items of a size that can be added to the current | |
128 * buffer without spilling to another buffer. If the pool has been reset, or | |
129 * the previous makeSpace completely exhausted a buffer then the returned | |
130 * size will be the size of the next available preallocated buffer, or zero | |
131 * if no preallocated buffer remains available. It is assumed that items | |
132 * should be itemSize-aligned from the start of a buffer. | |
133 * | |
134 * @return the number of items that would fit in the current buffer. | |
135 */ | |
136 int currentBufferItems(size_t itemSize) const; | |
137 | |
138 GrGeometryBuffer* createBuffer(size_t size); | 102 GrGeometryBuffer* createBuffer(size_t size); |
139 | 103 |
140 private: | 104 private: |
141 | |
142 // The GrGpu must be able to clear the ref of pools it creates as members | |
143 friend class GrGpu; | |
144 | |
145 struct BufferBlock { | 105 struct BufferBlock { |
146 size_t fBytesFree; | 106 size_t fBytesFree; |
147 GrGeometryBuffer* fBuffer; | 107 GrGeometryBuffer* fBuffer; |
148 }; | 108 }; |
149 | 109 |
150 bool createBlock(size_t requestSize); | 110 bool createBlock(size_t requestSize); |
151 void destroyBlock(); | 111 void destroyBlock(); |
152 void flushCpuData(const BufferBlock& block, size_t flushSize); | 112 void flushCpuData(const BufferBlock& block, size_t flushSize); |
153 #ifdef SK_DEBUG | 113 #ifdef SK_DEBUG |
154 void validate(bool unusedBlockAllowed = false) const; | 114 void validate(bool unusedBlockAllowed = false) const; |
(...skipping 19 matching lines...) Expand all Loading... |
174 | 134 |
175 /** | 135 /** |
176 * A GrBufferAllocPool of vertex buffers | 136 * A GrBufferAllocPool of vertex buffers |
177 */ | 137 */ |
178 class GrVertexBufferAllocPool : public GrBufferAllocPool { | 138 class GrVertexBufferAllocPool : public GrBufferAllocPool { |
179 public: | 139 public: |
180 /** | 140 /** |
181 * Constructor | 141 * Constructor |
182 * | 142 * |
183 * @param gpu The GrGpu used to create the vertex buffers. | 143 * @param gpu The GrGpu used to create the vertex buffers. |
184 * @param bufferSize The minimum size of created VBs This value | 144 * @param bufferSize The minimum size of created VBs. This value |
185 * will be clamped to some reasonable minimum. | 145 * will be clamped to some reasonable minimum. |
186 * @param preallocBufferCnt The pool will allocate this number of VBs at | 146 * @param preallocBufferCnt The pool will allocate this number of VBs at |
187 * bufferSize and keep them until it is | 147 * bufferSize and keep them until it is |
188 * destroyed. | 148 * destroyed. |
189 */ | 149 */ |
190 GrVertexBufferAllocPool(GrGpu* gpu, size_t bufferSize = 0, int preallocBuffe
rCnt = 0); | 150 GrVertexBufferAllocPool(GrGpu* gpu, size_t bufferSize = 0, int preallocBuffe
rCnt = 0); |
191 | 151 |
192 /** | 152 /** |
193 * Returns a block of memory to hold vertices. A buffer designated to hold | 153 * Returns a block of memory to hold vertices. A buffer designated to hold |
194 * the vertices given to the caller. The buffer may or may not be locked. | 154 * the vertices given to the caller. The buffer may or may not be locked. |
(...skipping 13 matching lines...) Expand all Loading... |
208 * vertices. | 168 * vertices. |
209 * @param startVertex returns the offset into buffer of the first vertex. | 169 * @param startVertex returns the offset into buffer of the first vertex. |
210 * In units of the size of a vertex from layout param. | 170 * In units of the size of a vertex from layout param. |
211 * @return pointer to first vertex. | 171 * @return pointer to first vertex. |
212 */ | 172 */ |
213 void* makeSpace(size_t vertexSize, | 173 void* makeSpace(size_t vertexSize, |
214 int vertexCount, | 174 int vertexCount, |
215 const GrVertexBuffer** buffer, | 175 const GrVertexBuffer** buffer, |
216 int* startVertex); | 176 int* startVertex); |
217 | 177 |
218 /** | |
219 * Gets the number of vertices that can be added to the current VB without | |
220 * spilling to another VB. If the pool has been reset, or the previous | |
221 * makeSpace completely exhausted a VB then the returned number of vertices | |
222 * would fit in the next available preallocated buffer. If any makeSpace | |
223 * would force a new VB to be created the return value will be zero. | |
224 * | |
225 * @param the size of a vertex to compute space for. | |
226 * @return the number of vertices that would fit in the current buffer. | |
227 */ | |
228 int currentBufferVertices(size_t vertexSize) const; | |
229 | |
230 /** | |
231 * Gets the number of vertices that can fit in a preallocated vertex buffer
. | |
232 * Zero if no preallocated buffers. | |
233 * | |
234 * @param the size of a vertex to compute space for. | |
235 * | |
236 * @return number of vertices that fit in one of the preallocated vertex | |
237 * buffers. | |
238 */ | |
239 int preallocatedBufferVertices(size_t vertexSize) const; | |
240 | |
241 private: | 178 private: |
242 typedef GrBufferAllocPool INHERITED; | 179 typedef GrBufferAllocPool INHERITED; |
243 }; | 180 }; |
244 | 181 |
245 class GrIndexBuffer; | 182 class GrIndexBuffer; |
246 | 183 |
247 /** | 184 /** |
248 * A GrBufferAllocPool of index buffers | 185 * A GrBufferAllocPool of index buffers |
249 */ | 186 */ |
250 class GrIndexBufferAllocPool : public GrBufferAllocPool { | 187 class GrIndexBufferAllocPool : public GrBufferAllocPool { |
251 public: | 188 public: |
252 /** | 189 /** |
253 * Constructor | 190 * Constructor |
254 * | 191 * |
255 * @param gpu The GrGpu used to create the index buffers. | 192 * @param gpu The GrGpu used to create the index buffers. |
256 * @param bufferSize The minimum size of created IBs This value | 193 * @param bufferSize The minimum size of created IBs. This value |
257 * will be clamped to some reasonable minimum. | 194 * will be clamped to some reasonable minimum. |
258 * @param preallocBufferCnt The pool will allocate this number of VBs at | 195 * @param preallocBufferCnt The pool will allocate this number of VBs at |
259 * bufferSize and keep them until it is | 196 * bufferSize and keep them until it is |
260 * destroyed. | 197 * destroyed. |
261 */ | 198 */ |
262 GrIndexBufferAllocPool(GrGpu* gpu, | 199 GrIndexBufferAllocPool(GrGpu* gpu, |
263 size_t bufferSize = 0, | 200 size_t bufferSize = 0, |
264 int preallocBufferCnt = 0); | 201 int preallocBufferCnt = 0); |
265 | 202 |
266 /** | 203 /** |
(...skipping 11 matching lines...) Expand all Loading... |
278 * | 215 * |
279 * @param indexCount number of indices to allocate space for | 216 * @param indexCount number of indices to allocate space for |
280 * @param buffer returns the index buffer that will hold the indices. | 217 * @param buffer returns the index buffer that will hold the indices. |
281 * @param startIndex returns the offset into buffer of the first index. | 218 * @param startIndex returns the offset into buffer of the first index. |
282 * @return pointer to first index. | 219 * @return pointer to first index. |
283 */ | 220 */ |
284 void* makeSpace(int indexCount, | 221 void* makeSpace(int indexCount, |
285 const GrIndexBuffer** buffer, | 222 const GrIndexBuffer** buffer, |
286 int* startIndex); | 223 int* startIndex); |
287 | 224 |
288 /** | |
289 * Gets the number of indices that can be added to the current IB without | |
290 * spilling to another IB. If the pool has been reset, or the previous | |
291 * makeSpace completely exhausted a IB then the returned number of indices | |
292 * would fit in the next available preallocated buffer. If any makeSpace | |
293 * would force a new IB to be created the return value will be zero. | |
294 */ | |
295 int currentBufferIndices() const; | |
296 | |
297 /** | |
298 * Gets the number of indices that can fit in a preallocated index buffer. | |
299 * Zero if no preallocated buffers. | |
300 * | |
301 * @return number of indices that fit in one of the preallocated index | |
302 * buffers. | |
303 */ | |
304 int preallocatedBufferIndices() const; | |
305 | |
306 private: | 225 private: |
307 typedef GrBufferAllocPool INHERITED; | 226 typedef GrBufferAllocPool INHERITED; |
308 }; | 227 }; |
309 | 228 |
310 #endif | 229 #endif |
OLD | NEW |