blob: 8641bbbe87a54e723a3346a9b0ee112c8da28f05 [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
bsalomon@google.com49313f62011-09-14 13:54:05 +000011#include "SkTArray.h"
bsalomon@google.com21cbec42013-01-07 17:23:00 +000012#include "SkTDArray.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000013#include "SkTypes.h"
bsalomon@google.com1c13c962011-02-14 16:51:21 +000014
15class GrGeometryBuffer;
16class GrGpu;
17
18/**
19 * A pool of geometry buffers tied to a GrGpu.
20 *
21 * The pool allows a client to make space for geometry and then put back excess
22 * space if it over allocated. When a client is ready to draw from the pool
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000023 * it calls unmap on the pool ensure buffers are ready for drawing. The pool
bsalomon@google.com1c13c962011-02-14 16:51:21 +000024 * can be reset after drawing is completed to recycle space.
25 *
26 * At creation time a minimum per-buffer size can be specified. Additionally,
27 * a number of buffers to preallocate can be specified. These will
28 * be allocated at the min size and kept around until the pool is destroyed.
29 */
commit-bot@chromium.orge3beb6b2014-04-07 19:34:38 +000030class GrBufferAllocPool : SkNoncopyable {
bsalomon@google.com11f0b512011-03-29 20:52:23 +000031public:
32 /**
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000033 * Ensures all buffers are unmapped and have all data written to them.
bsalomon@google.com11f0b512011-03-29 20:52:23 +000034 * Call before drawing using buffers from the pool.
35 */
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000036 void unmap();
bsalomon@google.com11f0b512011-03-29 20:52:23 +000037
38 /**
39 * Invalidates all the data in the pool, unrefs non-preallocated buffers.
40 */
41 void reset();
42
43 /**
bsalomon@google.com11f0b512011-03-29 20:52:23 +000044 * Frees data from makeSpaces in LIFO order.
45 */
46 void putBack(size_t bytes);
47
bsalomon@google.com11f0b512011-03-29 20:52:23 +000048protected:
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 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000054 enum BufferType {
55 kVertex_BufferType,
56 kIndex_BufferType,
57 };
58
59 /**
60 * Constructor
61 *
62 * @param gpu The GrGpu used to create the buffers.
63 * @param bufferType The type of buffers to create.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000064 * @param bufferSize The minimum size of created buffers.
65 * This value will be clamped to some
66 * reasonable minimum.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000067 */
68 GrBufferAllocPool(GrGpu* gpu,
69 BufferType bufferType,
robertphillipsc5f1c542015-05-13 10:55:33 -070070 size_t bufferSize = 0);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000071
robertphillipsc5f1c542015-05-13 10:55:33 -070072 virtual ~GrBufferAllocPool();
bsalomon@google.com1c13c962011-02-14 16:51:21 +000073
bsalomon@google.com1c13c962011-02-14 16:51:21 +000074 /**
bsalomon@google.com1c13c962011-02-14 16:51:21 +000075 * Returns a block of memory to hold data. A buffer designated to hold the
76 * data is given to the caller. The buffer may or may not be locked. The
77 * returned ptr remains valid until any of the following:
78 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000079 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +000080 * *reset is called.
81 * *this object is destroyed.
82 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000083 * Once unmap on the pool is called the data is guaranteed to be in the
bsalomon@google.com1c13c962011-02-14 16:51:21 +000084 * buffer at the offset indicated by offset. Until that time it may be
85 * in temporary storage and/or the buffer may be locked.
86 *
87 * @param size the amount of data to make space for
88 * @param alignment alignment constraint from start of buffer
89 * @param buffer returns the buffer that will hold the data.
90 * @param offset returns the offset into buffer of the data.
91 * @return pointer to where the client should write the data.
92 */
93 void* makeSpace(size_t size,
94 size_t alignment,
95 const GrGeometryBuffer** buffer,
96 size_t* offset);
97
robertphillipsc5f1c542015-05-13 10:55:33 -070098 GrGeometryBuffer* getBuffer(size_t size);
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099
100private:
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 struct BufferBlock {
102 size_t fBytesFree;
103 GrGeometryBuffer* fBuffer;
104 };
105
106 bool createBlock(size_t requestSize);
107 void destroyBlock();
robertphillipsc5f1c542015-05-13 10:55:33 -0700108 void deleteBlocks();
bsalomon3512eda2014-06-26 12:56:22 -0700109 void flushCpuData(const BufferBlock& block, size_t flushSize);
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000110#ifdef SK_DEBUG
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000111 void validate(bool unusedBlockAllowed = false) const;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000113
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000114 size_t fBytesInUse;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115
116 GrGpu* fGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000117 size_t fMinBlockSize;
118 BufferType fBufferType;
119
bsalomon@google.com49313f62011-09-14 13:54:05 +0000120 SkTArray<BufferBlock> fBlocks;
bsalomon@google.com3582bf92011-06-30 21:32:31 +0000121 SkAutoMalloc fCpuData;
122 void* fBufferPtr;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000123};
124
125class GrVertexBuffer;
126
127/**
128 * A GrBufferAllocPool of vertex buffers
129 */
130class GrVertexBufferAllocPool : public GrBufferAllocPool {
131public:
132 /**
133 * Constructor
134 *
135 * @param gpu The GrGpu used to create the vertex buffers.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000136 */
robertphillipsc5f1c542015-05-13 10:55:33 -0700137 GrVertexBufferAllocPool(GrGpu* gpu);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000138
139 /**
140 * Returns a block of memory to hold vertices. A buffer designated to hold
141 * the vertices given to the caller. The buffer may or may not be locked.
142 * The returned ptr remains valid until any of the following:
143 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000144 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000145 * *reset is called.
146 * *this object is destroyed.
147 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000148 * Once unmap on the pool is called the vertices are guaranteed to be in
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000149 * the buffer at the offset indicated by startVertex. Until that time they
150 * may be in temporary storage and/or the buffer may be locked.
151 *
jvanverth@google.coma6338982013-01-31 21:34:25 +0000152 * @param vertexSize specifies size of a vertex to allocate space for
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000153 * @param vertexCount number of vertices to allocate space for
154 * @param buffer returns the vertex buffer that will hold the
155 * vertices.
156 * @param startVertex returns the offset into buffer of the first vertex.
157 * In units of the size of a vertex from layout param.
158 * @return pointer to first vertex.
159 */
jvanverth@google.coma6338982013-01-31 21:34:25 +0000160 void* makeSpace(size_t vertexSize,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161 int vertexCount,
162 const GrVertexBuffer** buffer,
163 int* startVertex);
164
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000165private:
166 typedef GrBufferAllocPool INHERITED;
167};
168
169class GrIndexBuffer;
170
171/**
172 * A GrBufferAllocPool of index buffers
173 */
174class GrIndexBufferAllocPool : public GrBufferAllocPool {
175public:
176 /**
177 * Constructor
178 *
179 * @param gpu The GrGpu used to create the index buffers.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000180 */
robertphillipsc5f1c542015-05-13 10:55:33 -0700181 GrIndexBufferAllocPool(GrGpu* gpu);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000182
183 /**
184 * Returns a block of memory to hold indices. A buffer designated to hold
185 * the indices is given to the caller. The buffer may or may not be locked.
186 * The returned ptr remains valid until any of the following:
187 * *makeSpace is called again.
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000188 * *unmap is called.
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000189 * *reset is called.
190 * *this object is destroyed.
191 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000192 * Once unmap on the pool is called the indices are guaranteed to be in the
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000193 * buffer at the offset indicated by startIndex. Until that time they may be
194 * in temporary storage and/or the buffer may be locked.
195 *
196 * @param indexCount number of indices to allocate space for
197 * @param buffer returns the index buffer that will hold the indices.
198 * @param startIndex returns the offset into buffer of the first index.
199 * @return pointer to first index.
200 */
201 void* makeSpace(int indexCount,
202 const GrIndexBuffer** buffer,
203 int* startIndex);
204
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000205private:
206 typedef GrBufferAllocPool INHERITED;
207};
208
209#endif