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" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 14 | #include "GrTypesPriv.h" |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 15 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 16 | class GrBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 17 | class GrGpu; |
| 18 | |
| 19 | /** |
| 20 | * A pool of geometry buffers tied to a GrGpu. |
| 21 | * |
| 22 | * 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 |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 24 | * 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] | 25 | * can be reset after drawing is completed to recycle space. |
| 26 | * |
| 27 | * At creation time a minimum per-buffer size can be specified. Additionally, |
| 28 | * a number of buffers to preallocate can be specified. These will |
| 29 | * be allocated at the min size and kept around until the pool is destroyed. |
| 30 | */ |
commit-bot@chromium.org | e3beb6b | 2014-04-07 19:34:38 +0000 | [diff] [blame] | 31 | class GrBufferAllocPool : SkNoncopyable { |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 32 | public: |
| 33 | /** |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 34 | * 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] | 35 | * Call before drawing using buffers from the pool. |
| 36 | */ |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 37 | void unmap(); |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Invalidates all the data in the pool, unrefs non-preallocated buffers. |
| 41 | */ |
| 42 | void reset(); |
| 43 | |
| 44 | /** |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 45 | * Frees data from makeSpaces in LIFO order. |
| 46 | */ |
| 47 | void putBack(size_t bytes); |
| 48 | |
bsalomon@google.com | 11f0b51 | 2011-03-29 20:52:23 +0000 | [diff] [blame] | 49 | protected: |
| 50 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 51 | * Constructor |
| 52 | * |
| 53 | * @param gpu The GrGpu used to create the buffers. |
| 54 | * @param bufferType The type of buffers to create. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 55 | * @param bufferSize The minimum size of created buffers. |
| 56 | * This value will be clamped to some |
| 57 | * reasonable minimum. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 58 | */ |
| 59 | GrBufferAllocPool(GrGpu* gpu, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 60 | GrBufferType bufferType, |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 61 | size_t bufferSize = 0); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 62 | |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 63 | virtual ~GrBufferAllocPool(); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 65 | /** |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 66 | * 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 |
| 68 | * returned ptr remains valid until any of the following: |
| 69 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 70 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 71 | * *reset is called. |
| 72 | * *this object is destroyed. |
| 73 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 74 | * 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] | 75 | * buffer at the offset indicated by offset. Until that time it may be |
| 76 | * in temporary storage and/or the buffer may be locked. |
| 77 | * |
| 78 | * @param size the amount of data to make space for |
| 79 | * @param alignment alignment constraint from start of buffer |
| 80 | * @param buffer returns the buffer that will hold the data. |
| 81 | * @param offset returns the offset into buffer of the data. |
| 82 | * @return pointer to where the client should write the data. |
| 83 | */ |
| 84 | void* makeSpace(size_t size, |
| 85 | size_t alignment, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 86 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 87 | size_t* offset); |
| 88 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 89 | GrBuffer* getBuffer(size_t size); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 90 | |
| 91 | private: |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 92 | struct BufferBlock { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 93 | size_t fBytesFree; |
| 94 | GrBuffer* fBuffer; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | bool createBlock(size_t requestSize); |
| 98 | void destroyBlock(); |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 99 | void deleteBlocks(); |
bsalomon | 3512eda | 2014-06-26 12:56:22 -0700 | [diff] [blame] | 100 | void flushCpuData(const BufferBlock& block, size_t flushSize); |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 101 | void* resetCpuData(size_t newSize); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 102 | #ifdef SK_DEBUG |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 103 | void validate(bool unusedBlockAllowed = false) const; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 104 | #endif |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 105 | size_t fBytesInUse; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 106 | |
| 107 | GrGpu* fGpu; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 108 | size_t fMinBlockSize; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 109 | GrBufferType fBufferType; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 110 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 111 | SkTArray<BufferBlock> fBlocks; |
bsalomon | 7dea7b7 | 2015-08-19 08:26:51 -0700 | [diff] [blame] | 112 | void* fCpuData; |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 113 | void* fBufferPtr; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 114 | size_t fBufferMapThreshold; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 117 | /** |
| 118 | * A GrBufferAllocPool of vertex buffers |
| 119 | */ |
| 120 | class GrVertexBufferAllocPool : public GrBufferAllocPool { |
| 121 | public: |
| 122 | /** |
| 123 | * Constructor |
| 124 | * |
| 125 | * @param gpu The GrGpu used to create the vertex buffers. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 126 | */ |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 127 | GrVertexBufferAllocPool(GrGpu* gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Returns a block of memory to hold vertices. A buffer designated to hold |
| 131 | * the vertices given to the caller. The buffer may or may not be locked. |
| 132 | * The returned ptr remains valid until any of the following: |
| 133 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 134 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 135 | * *reset is called. |
| 136 | * *this object is destroyed. |
| 137 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 138 | * 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] | 139 | * the buffer at the offset indicated by startVertex. Until that time they |
| 140 | * may be in temporary storage and/or the buffer may be locked. |
| 141 | * |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 142 | * @param vertexSize specifies size of a vertex to allocate space for |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 143 | * @param vertexCount number of vertices to allocate space for |
| 144 | * @param buffer returns the vertex buffer that will hold the |
| 145 | * vertices. |
| 146 | * @param startVertex returns the offset into buffer of the first vertex. |
| 147 | * In units of the size of a vertex from layout param. |
| 148 | * @return pointer to first vertex. |
| 149 | */ |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 150 | void* makeSpace(size_t vertexSize, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 151 | int vertexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 152 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 153 | int* startVertex); |
| 154 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 155 | private: |
| 156 | typedef GrBufferAllocPool INHERITED; |
| 157 | }; |
| 158 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 159 | /** |
| 160 | * A GrBufferAllocPool of index buffers |
| 161 | */ |
| 162 | class GrIndexBufferAllocPool : public GrBufferAllocPool { |
| 163 | public: |
| 164 | /** |
| 165 | * Constructor |
| 166 | * |
| 167 | * @param gpu The GrGpu used to create the index buffers. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 168 | */ |
robertphillips | 1b8e1b5 | 2015-06-24 06:54:10 -0700 | [diff] [blame] | 169 | GrIndexBufferAllocPool(GrGpu* gpu); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * Returns a block of memory to hold indices. A buffer designated to hold |
| 173 | * the indices is given to the caller. The buffer may or may not be locked. |
| 174 | * The returned ptr remains valid until any of the following: |
| 175 | * *makeSpace is called again. |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 176 | * *unmap is called. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 177 | * *reset is called. |
| 178 | * *this object is destroyed. |
| 179 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 180 | * 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] | 181 | * buffer at the offset indicated by startIndex. Until that time they may be |
| 182 | * in temporary storage and/or the buffer may be locked. |
| 183 | * |
| 184 | * @param indexCount number of indices to allocate space for |
| 185 | * @param buffer returns the index buffer that will hold the indices. |
| 186 | * @param startIndex returns the offset into buffer of the first index. |
| 187 | * @return pointer to first index. |
| 188 | */ |
| 189 | void* makeSpace(int indexCount, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 190 | const GrBuffer** buffer, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 191 | int* startIndex); |
| 192 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 193 | private: |
| 194 | typedef GrBufferAllocPool INHERITED; |
| 195 | }; |
| 196 | |
| 197 | #endif |