| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 1 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 8 | #ifndef GrBufferAllocPool_DEFINED |
| 9 | #define GrBufferAllocPool_DEFINED |
| 10 | |
| bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 11 | #include "SkTArray.h" |
| bsalomon@google.com | 21cbec4 | 2013-01-07 17:23:00 +0000 | [diff] [blame] | 12 | #include "SkTDArray.h" |
| commit-bot@chromium.org | a0b4028 | 2013-09-18 13:00:55 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 14 | |
| 15 | class GrGeometryBuffer; |
| 16 | class 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.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 23 | * it calls unmap on the pool ensure buffers are ready for drawing. The pool |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 24 | * 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.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 30 | class GrBufferAllocPool : SkNoncopyable { |
| bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 31 | public: |
| 32 | /** |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 33 | * Ensures all buffers are unmapped and have all data written to them. |
| bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 34 | * Call before drawing using buffers from the pool. |
| 35 | */ |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 36 | void unmap(); |
| bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Invalidates all the data in the pool, unrefs non-preallocated buffers. |
| 40 | */ |
| 41 | void reset(); |
| 42 | |
| 43 | /** |
| bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 44 | * Frees data from makeSpaces in LIFO order. |
| 45 | */ |
| 46 | void putBack(size_t bytes); |
| 47 | |
| bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 48 | protected: |
| 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 54 | 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.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 64 | * @param bufferSize The minimum size of created buffers. |
| 65 | * This value will be clamped to some |
| 66 | * reasonable minimum. |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 67 | * @param preallocBufferCnt The pool will allocate this number of |
| 68 | * buffers at bufferSize and keep them until it |
| 69 | * is destroyed. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 70 | */ |
| 71 | GrBufferAllocPool(GrGpu* gpu, |
| 72 | BufferType bufferType, |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 73 | size_t bufferSize = 0, |
| 74 | int preallocBufferCnt = 0); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 75 | |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 76 | virtual ~GrBufferAllocPool(); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 77 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 78 | /** |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 79 | * Returns a block of memory to hold data. A buffer designated to hold the |
| 80 | * data is given to the caller. The buffer may or may not be locked. The |
| 81 | * returned ptr remains valid until any of the following: |
| 82 | * *makeSpace is called again. |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 83 | * *unmap is called. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 84 | * *reset is called. |
| 85 | * *this object is destroyed. |
| 86 | * |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 87 | * Once unmap on the pool is called the data is guaranteed to be in the |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 88 | * buffer at the offset indicated by offset. Until that time it may be |
| 89 | * in temporary storage and/or the buffer may be locked. |
| 90 | * |
| 91 | * @param size the amount of data to make space for |
| 92 | * @param alignment alignment constraint from start of buffer |
| 93 | * @param buffer returns the buffer that will hold the data. |
| 94 | * @param offset returns the offset into buffer of the data. |
| 95 | * @return pointer to where the client should write the data. |
| 96 | */ |
| 97 | void* makeSpace(size_t size, |
| 98 | size_t alignment, |
| 99 | const GrGeometryBuffer** buffer, |
| 100 | size_t* offset); |
| 101 | |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 102 | GrGeometryBuffer* createBuffer(size_t size); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 103 | |
| 104 | private: |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 105 | struct BufferBlock { |
| 106 | size_t fBytesFree; |
| 107 | GrGeometryBuffer* fBuffer; |
| 108 | }; |
| 109 | |
| 110 | bool createBlock(size_t requestSize); |
| 111 | void destroyBlock(); |
| bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 112 | void flushCpuData(const BufferBlock& block, size_t flushSize); |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 113 | #ifdef SK_DEBUG |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 114 | void validate(bool unusedBlockAllowed = false) const; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 115 | #endif |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 116 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 117 | size_t fBytesInUse; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 118 | |
| 119 | GrGpu* fGpu; |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 120 | SkTDArray<GrGeometryBuffer*> fPreallocBuffers; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 121 | size_t fMinBlockSize; |
| 122 | BufferType fBufferType; |
| 123 | |
| bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 124 | SkTArray<BufferBlock> fBlocks; |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 125 | int fPreallocBuffersInUse; |
| 126 | // We attempt to cycle through the preallocated buffers rather than |
| 127 | // always starting from the first. |
| 128 | int fPreallocBufferStartIdx; |
| bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 129 | SkAutoMalloc fCpuData; |
| 130 | void* fBufferPtr; |
| joshualitt | 7224c86 | 2015-05-29 06:46:47 -0700 | [diff] [blame^] | 131 | size_t fGeometryBufferMapThreshold; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | class GrVertexBuffer; |
| 135 | |
| 136 | /** |
| 137 | * A GrBufferAllocPool of vertex buffers |
| 138 | */ |
| 139 | class GrVertexBufferAllocPool : public GrBufferAllocPool { |
| 140 | public: |
| 141 | /** |
| 142 | * Constructor |
| 143 | * |
| 144 | * @param gpu The GrGpu used to create the vertex buffers. |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 145 | * @param bufferSize The minimum size of created VBs. This value |
| 146 | * will be clamped to some reasonable minimum. |
| 147 | * @param preallocBufferCnt The pool will allocate this number of VBs at |
| 148 | * bufferSize and keep them until it is |
| 149 | * destroyed. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 150 | */ |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 151 | GrVertexBufferAllocPool(GrGpu* gpu, size_t bufferSize = 0, int preallocBufferCnt = 0); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Returns a block of memory to hold vertices. A buffer designated to hold |
| 155 | * the vertices given to the caller. The buffer may or may not be locked. |
| 156 | * The returned ptr remains valid until any of the following: |
| 157 | * *makeSpace is called again. |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 158 | * *unmap is called. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 159 | * *reset is called. |
| 160 | * *this object is destroyed. |
| 161 | * |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 162 | * Once unmap on the pool is called the vertices are guaranteed to be in |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 163 | * the buffer at the offset indicated by startVertex. Until that time they |
| 164 | * may be in temporary storage and/or the buffer may be locked. |
| 165 | * |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 166 | * @param vertexSize specifies size of a vertex to allocate space for |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 167 | * @param vertexCount number of vertices to allocate space for |
| 168 | * @param buffer returns the vertex buffer that will hold the |
| 169 | * vertices. |
| 170 | * @param startVertex returns the offset into buffer of the first vertex. |
| 171 | * In units of the size of a vertex from layout param. |
| 172 | * @return pointer to first vertex. |
| 173 | */ |
| jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 174 | void* makeSpace(size_t vertexSize, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 175 | int vertexCount, |
| 176 | const GrVertexBuffer** buffer, |
| 177 | int* startVertex); |
| 178 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 179 | private: |
| 180 | typedef GrBufferAllocPool INHERITED; |
| 181 | }; |
| 182 | |
| 183 | class GrIndexBuffer; |
| 184 | |
| 185 | /** |
| 186 | * A GrBufferAllocPool of index buffers |
| 187 | */ |
| 188 | class GrIndexBufferAllocPool : public GrBufferAllocPool { |
| 189 | public: |
| 190 | /** |
| 191 | * Constructor |
| 192 | * |
| 193 | * @param gpu The GrGpu used to create the index buffers. |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 194 | * @param bufferSize The minimum size of created IBs. This value |
| 195 | * will be clamped to some reasonable minimum. |
| 196 | * @param preallocBufferCnt The pool will allocate this number of VBs at |
| 197 | * bufferSize and keep them until it is |
| 198 | * destroyed. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 199 | */ |
| robertphillips | eea2ff7 | 2015-05-14 05:24:53 -0700 | [diff] [blame] | 200 | GrIndexBufferAllocPool(GrGpu* gpu, |
| 201 | size_t bufferSize = 0, |
| 202 | int preallocBufferCnt = 0); |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * Returns a block of memory to hold indices. A buffer designated to hold |
| 206 | * the indices is given to the caller. The buffer may or may not be locked. |
| 207 | * The returned ptr remains valid until any of the following: |
| 208 | * *makeSpace is called again. |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 209 | * *unmap is called. |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 210 | * *reset is called. |
| 211 | * *this object is destroyed. |
| 212 | * |
| commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 213 | * Once unmap on the pool is called the indices are guaranteed to be in the |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 214 | * buffer at the offset indicated by startIndex. Until that time they may be |
| 215 | * in temporary storage and/or the buffer may be locked. |
| 216 | * |
| 217 | * @param indexCount number of indices to allocate space for |
| 218 | * @param buffer returns the index buffer that will hold the indices. |
| 219 | * @param startIndex returns the offset into buffer of the first index. |
| 220 | * @return pointer to first index. |
| 221 | */ |
| 222 | void* makeSpace(int indexCount, |
| 223 | const GrIndexBuffer** buffer, |
| 224 | int* startIndex); |
| 225 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 226 | private: |
| 227 | typedef GrBufferAllocPool INHERITED; |
| 228 | }; |
| 229 | |
| 230 | #endif |