blob: 23759caff8ce3e905bca3fc9638f08e462640ca3 [file] [log] [blame]
bsalomon@google.com1c13c962011-02-14 16:51:21 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.com1c13c962011-02-14 16:51:21 +00006 */
7
bsalomon@google.com1c13c962011-02-14 16:51:21 +00008#ifndef GrBufferAllocPool_DEFINED
9#define GrBufferAllocPool_DEFINED
10
Ben Wagnerd5148e32018-07-16 17:44:06 -040011#include "GrTypesPriv.h"
12#include "SkNoncopyable.h"
bsalomon@google.com49313f62011-09-14 13:54:05 +000013#include "SkTArray.h"
bsalomon@google.com21cbec42013-01-07 17:23:00 +000014#include "SkTDArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000015#include "SkTypes.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016
cdalton397536c2016-03-25 12:15:03 -070017class GrBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000018class GrGpu;
19
20/**
21 * A pool of geometry buffers tied to a GrGpu.
22 *
23 * The pool allows a client to make space for geometry and then put back excess
24 * space if it over allocated. When a client is ready to draw from the pool
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000025 * it calls unmap on the pool ensure buffers are ready for drawing. The pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +000026 * can be reset after drawing is completed to recycle space.
27 *
28 * At creation time a minimum per-buffer size can be specified. Additionally,
29 * a number of buffers to preallocate can be specified. These will
30 * be allocated at the min size and kept around until the pool is destroyed.
31 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000032class GrBufferAllocPool : SkNoncopyable {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000033public:
34 /**
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000035 * Ensures all buffers are unmapped and have all data written to them.
bsalomon@google.com11f0b512011-03-29 20:52:23 +000036 * Call before drawing using buffers from the pool.
37 */
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000038 void unmap();
bsalomon@google.com11f0b512011-03-29 20:52:23 +000039
40 /**
41 * Invalidates all the data in the pool, unrefs non-preallocated buffers.
42 */
43 void reset();
44
45 /**
bsalomon@google.com11f0b512011-03-29 20:52:23 +000046 * Frees data from makeSpaces in LIFO order.
47 */
48 void putBack(size_t bytes);
49
bsalomon@google.com11f0b512011-03-29 20:52:23 +000050protected:
51 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +000052 * Constructor
53 *
54 * @param gpu The GrGpu used to create the buffers.
55 * @param bufferType The type of buffers to create.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000056 * @param bufferSize The minimum size of created buffers.
57 * This value will be clamped to some
58 * reasonable minimum.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000059 */
60 GrBufferAllocPool(GrGpu* gpu,
cdalton397536c2016-03-25 12:15:03 -070061 GrBufferType bufferType,
robertphillips1b8e1b52015-06-24 06:54:10 -070062 size_t bufferSize = 0);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000063
robertphillips1b8e1b52015-06-24 06:54:10 -070064 virtual ~GrBufferAllocPool();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000065
bsalomon@google.com1c13c962011-02-14 16:51:21 +000066 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +000067 * Returns a block of memory to hold data. A buffer designated to hold the
68 * data is given to the caller. The buffer may or may not be locked. The
69 * returned ptr remains valid until any of the following:
70 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000071 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072 * *reset is called.
73 * *this object is destroyed.
74 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000075 * Once unmap on the pool is called the data is guaranteed to be in the
bsalomon@google.com1c13c962011-02-14 16:51:21 +000076 * buffer at the offset indicated by offset. Until that time it may be
77 * in temporary storage and/or the buffer may be locked.
78 *
79 * @param size the amount of data to make space for
80 * @param alignment alignment constraint from start of buffer
81 * @param buffer returns the buffer that will hold the data.
82 * @param offset returns the offset into buffer of the data.
83 * @return pointer to where the client should write the data.
84 */
85 void* makeSpace(size_t size,
86 size_t alignment,
cdalton397536c2016-03-25 12:15:03 -070087 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000088 size_t* offset);
89
Brian Osman49b7b6f2017-06-20 14:43:58 -040090 /**
91 * Returns a block of memory to hold data. A buffer designated to hold the
92 * data is given to the caller. The buffer may or may not be locked. The
93 * returned ptr remains valid until any of the following:
94 * *makeSpace is called again.
95 * *unmap is called.
96 * *reset is called.
97 * *this object is destroyed.
98 *
99 * Once unmap on the pool is called the data is guaranteed to be in the
100 * buffer at the offset indicated by offset. Until that time it may be
101 * in temporary storage and/or the buffer may be locked.
102 *
103 * The caller requests a minimum number of bytes, but the block may be (much)
104 * larger. Assuming that a new block must be allocated, it will be fallbackSize bytes.
105 * The actual block size is returned in actualSize.
106 *
107 * @param minSize the minimum amount of data to make space for
108 * @param fallbackSize the amount of data to make space for if a new block is needed
109 * @param alignment alignment constraint from start of buffer
110 * @param buffer returns the buffer that will hold the data.
111 * @param offset returns the offset into buffer of the data.
112 * @param actualSize returns the capacity of the block
113 * @return pointer to where the client should write the data.
114 */
115 void* makeSpaceAtLeast(size_t minSize,
116 size_t fallbackSize,
117 size_t alignment,
118 const GrBuffer** buffer,
119 size_t* offset,
120 size_t* actualSize);
121
cdalton397536c2016-03-25 12:15:03 -0700122 GrBuffer* getBuffer(size_t size);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000123
124private:
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000125 struct BufferBlock {
cdalton397536c2016-03-25 12:15:03 -0700126 size_t fBytesFree;
127 GrBuffer* fBuffer;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000128 };
129
130 bool createBlock(size_t requestSize);
131 void destroyBlock();
robertphillips1b8e1b52015-06-24 06:54:10 -0700132 void deleteBlocks();
bsalomon3512eda2014-06-26 12:56:22 -0700133 void flushCpuData(const BufferBlock& block, size_t flushSize);
bsalomon7dea7b72015-08-19 08:26:51 -0700134 void* resetCpuData(size_t newSize);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000135#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000136 void validate(bool unusedBlockAllowed = false) const;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000137#endif
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000138 size_t fBytesInUse;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000139
140 GrGpu* fGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141 size_t fMinBlockSize;
cdalton397536c2016-03-25 12:15:03 -0700142 GrBufferType fBufferType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000143
bsalomon@google.com49313f62011-09-14 13:54:05 +0000144 SkTArray<BufferBlock> fBlocks;
bsalomon7dea7b72015-08-19 08:26:51 -0700145 void* fCpuData;
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000146 void* fBufferPtr;
cdalton397536c2016-03-25 12:15:03 -0700147 size_t fBufferMapThreshold;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000148};
149
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000150/**
151 * A GrBufferAllocPool of vertex buffers
152 */
153class GrVertexBufferAllocPool : public GrBufferAllocPool {
154public:
155 /**
156 * Constructor
157 *
158 * @param gpu The GrGpu used to create the vertex buffers.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159 */
robertphillips1b8e1b52015-06-24 06:54:10 -0700160 GrVertexBufferAllocPool(GrGpu* gpu);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161
162 /**
163 * Returns a block of memory to hold vertices. A buffer designated to hold
164 * the vertices given to the caller. The buffer may or may not be locked.
165 * The returned ptr remains valid until any of the following:
166 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000167 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168 * *reset is called.
169 * *this object is destroyed.
170 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000171 * Once unmap on the pool is called the vertices are guaranteed to be in
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000172 * the buffer at the offset indicated by startVertex. Until that time they
173 * may be in temporary storage and/or the buffer may be locked.
174 *
jvanverth@google.coma6338982013-01-31 21:34:25 +0000175 * @param vertexSize specifies size of a vertex to allocate space for
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000176 * @param vertexCount number of vertices to allocate space for
177 * @param buffer returns the vertex buffer that will hold the
178 * vertices.
179 * @param startVertex returns the offset into buffer of the first vertex.
180 * In units of the size of a vertex from layout param.
181 * @return pointer to first vertex.
182 */
jvanverth@google.coma6338982013-01-31 21:34:25 +0000183 void* makeSpace(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000184 int vertexCount,
cdalton397536c2016-03-25 12:15:03 -0700185 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000186 int* startVertex);
187
Brian Osman49b7b6f2017-06-20 14:43:58 -0400188 /**
189 * Returns a block of memory to hold vertices. A buffer designated to hold
190 * the vertices given to the caller. The buffer may or may not be locked.
191 * The returned ptr remains valid until any of the following:
192 * *makeSpace is called again.
193 * *unmap is called.
194 * *reset is called.
195 * *this object is destroyed.
196 *
197 * Once unmap on the pool is called the vertices are guaranteed to be in
198 * the buffer at the offset indicated by startVertex. Until that time they
199 * may be in temporary storage and/or the buffer may be locked.
200 *
201 * The caller requests a minimum number of vertices, but the block may be (much)
202 * larger. Assuming that a new block must be allocated, it will be sized to hold
203 * fallbackVertexCount vertices. The actual block size (in vertices) is returned in
204 * actualVertexCount.
205 *
206 * @param vertexSize specifies size of a vertex to allocate space for
207 * @param minVertexCount minimum number of vertices to allocate space for
208 * @param fallbackVertexCount number of vertices to allocate space for if a new block is needed
209 * @param buffer returns the vertex buffer that will hold the vertices.
210 * @param startVertex returns the offset into buffer of the first vertex.
211 * In units of the size of a vertex from layout param.
212 * @param actualVertexCount returns the capacity of the block (in vertices)
213 * @return pointer to first vertex.
214 */
215 void* makeSpaceAtLeast(size_t vertexSize,
216 int minVertexCount,
217 int fallbackVertexCount,
218 const GrBuffer** buffer,
219 int* startVertex,
220 int* actualVertexCount);
221
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000222private:
223 typedef GrBufferAllocPool INHERITED;
224};
225
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000226/**
227 * A GrBufferAllocPool of index buffers
228 */
229class GrIndexBufferAllocPool : public GrBufferAllocPool {
230public:
231 /**
232 * Constructor
233 *
234 * @param gpu The GrGpu used to create the index buffers.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000235 */
robertphillips1b8e1b52015-06-24 06:54:10 -0700236 GrIndexBufferAllocPool(GrGpu* gpu);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000237
238 /**
239 * Returns a block of memory to hold indices. A buffer designated to hold
240 * the indices is given to the caller. The buffer may or may not be locked.
241 * The returned ptr remains valid until any of the following:
242 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000243 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244 * *reset is called.
245 * *this object is destroyed.
246 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000247 * Once unmap on the pool is called the indices are guaranteed to be in the
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000248 * buffer at the offset indicated by startIndex. Until that time they may be
249 * in temporary storage and/or the buffer may be locked.
250 *
251 * @param indexCount number of indices to allocate space for
252 * @param buffer returns the index buffer that will hold the indices.
253 * @param startIndex returns the offset into buffer of the first index.
254 * @return pointer to first index.
255 */
256 void* makeSpace(int indexCount,
cdalton397536c2016-03-25 12:15:03 -0700257 const GrBuffer** buffer,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000258 int* startIndex);
259
Brian Osman49b7b6f2017-06-20 14:43:58 -0400260 /**
261 * Returns a block of memory to hold indices. A buffer designated to hold
262 * the indices is given to the caller. The buffer may or may not be locked.
263 * The returned ptr remains valid until any of the following:
264 * *makeSpace is called again.
265 * *unmap is called.
266 * *reset is called.
267 * *this object is destroyed.
268 *
269 * Once unmap on the pool is called the indices are guaranteed to be in the
270 * buffer at the offset indicated by startIndex. Until that time they may be
271 * in temporary storage and/or the buffer may be locked.
272 *
273 * The caller requests a minimum number of indices, but the block may be (much)
274 * larger. Assuming that a new block must be allocated, it will be sized to hold
275 * fallbackIndexCount indices. The actual block size (in indices) is returned in
276 * actualIndexCount.
277 *
278 * @param minIndexCount minimum number of indices to allocate space for
279 * @param fallbackIndexCount number of indices to allocate space for if a new block is needed
280 * @param buffer returns the index buffer that will hold the indices.
281 * @param startIndex returns the offset into buffer of the first index.
282 * @param actualIndexCount returns the capacity of the block (in indices)
283 * @return pointer to first index.
284 */
285 void* makeSpaceAtLeast(int minIndexCount,
286 int fallbackIndexCount,
287 const GrBuffer** buffer,
288 int* startIndex,
289 int* actualIndexCount);
290
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000291private:
292 typedef GrBufferAllocPool INHERITED;
293};
294
295#endif