| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrInOrderDrawBuffer_DEFINED |
| 12 | #define GrInOrderDrawBuffer_DEFINED |
| 13 | |
| 14 | #include "GrDrawTarget.h" |
| 15 | #include "GrAllocPool.h" |
| 16 | #include "GrAllocator.h" |
| 17 | #include "GrClip.h" |
| 18 | |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 19 | class GrGpu; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 20 | class GrIndexBufferAllocPool; |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 21 | class GrVertexBufferAllocPool; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 23 | /** |
| 24 | * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up |
| 25 | * draws for eventual playback into a GrGpu. In theory one draw buffer could |
| 26 | * playback into another. When index or vertex buffers are used as geometry |
| 27 | * sources it is the callers the draw buffer only holds references to the |
| 28 | * buffers. It is the callers responsibility to ensure that the data is still |
| 29 | * valid when the draw buffer is played back into a GrGpu. Similarly, it is the |
| 30 | * caller's responsibility to ensure that all referenced textures, buffers, |
| 31 | * and rendertargets are associated in the GrGpu object that the buffer is |
| 32 | * played back into. The buffer requires VB and IB pools to store geometry. |
| 33 | */ |
| 34 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 35 | class GrInOrderDrawBuffer : public GrDrawTarget { |
| 36 | public: |
| 37 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 38 | /** |
| 39 | * Creates a GrInOrderDrawBuffer |
| 40 | * |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 41 | * @param gpu the gpu object where this will be played back |
| 42 | * (possible indirectly). GrResources used with the draw |
| 43 | * buffer are created by this gpu object. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 44 | * @param vertexPool pool where vertices for queued draws will be saved when |
| 45 | * the vertex source is either reserved or array. |
| 46 | * @param indexPool pool where indices for queued draws will be saved when |
| 47 | * the index source is either reserved or array. |
| 48 | */ |
| bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 49 | GrInOrderDrawBuffer(const GrGpu* gpu, |
| 50 | GrVertexBufferAllocPool* vertexPool, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 51 | GrIndexBufferAllocPool* indexPool); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | |
| 53 | virtual ~GrInOrderDrawBuffer(); |
| 54 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 55 | /** |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 56 | * Provides the buffer with an index buffer that can be used for quad rendering. |
| 57 | * The buffer may be able to batch consecutive drawRects if this is provided. |
| 58 | * @param indexBuffer index buffer with quad indices. |
| 59 | */ |
| 60 | void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer); |
| 61 | |
| 62 | /** |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 63 | * Empties the draw buffer of any queued up draws. This must not be called |
| 64 | * while inside an unbalanced pushGeometrySource(). |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 65 | */ |
| 66 | void reset(); |
| 67 | |
| 68 | /** |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 69 | * plays the queued up draws to another target. Does not empty this buffer |
| 70 | * so that it can be played back multiple times. This buffer must not have |
| 71 | * an active reserved vertex or index source. Any reserved geometry on |
| 72 | * the target will be finalized because it's geometry source will be pushed |
| 73 | * before playback and popped afterwards. |
| 74 | * |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 75 | * @return false if the playback trivially drew nothing because nothing was |
| 76 | * recorded. |
| 77 | * |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 78 | * @param target the target to receive the playback |
| 79 | */ |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 80 | bool playback(GrDrawTarget* target); |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 81 | |
| 82 | /** |
| 83 | * A convenience method to do a playback followed by a reset. All the |
| 84 | * constraints and side-effects or playback() and reset apply(). |
| 85 | */ |
| 86 | void flushTo(GrDrawTarget* target) { |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 87 | if (fFlushing) { |
| 88 | // When creating SW-only clip masks, the GrClipMaskManager can |
| 89 | // cause a GrContext::flush (when copying the mask results back |
| 90 | // to the GPU). Without a guard this results in a recursive call |
| 91 | // to this method. |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | fFlushing = true; |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 96 | if (this->playback(target)) { |
| 97 | this->reset(); |
| 98 | } |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 99 | fFlushing = false; |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * This function allows the draw buffer to automatically flush itself to |
| 104 | * another target. This means the buffer may internally call |
| 105 | * this->flushTo(target) when it is safe to do so. |
| 106 | * |
| 107 | * When the auto flush target is set to NULL (as it initially is) the draw |
| 108 | * buffer will never automatically flush itself. |
| 109 | */ |
| 110 | void setAutoFlushTarget(GrDrawTarget* target); |
| 111 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 112 | // overrides from GrDrawTarget |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 113 | virtual void drawRect(const GrRect& rect, |
| 114 | const GrMatrix* matrix = NULL, |
| bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 115 | StageMask stageEnableMask = 0, |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 116 | const GrRect* srcRects[] = NULL, |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 117 | const GrMatrix* srcMatrices[] = NULL) SK_OVERRIDE; |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 118 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 119 | virtual void drawIndexedInstances(GrPrimitiveType type, |
| 120 | int instanceCount, |
| 121 | int verticesPerInstance, |
| 122 | int indicesPerInstance) |
| 123 | SK_OVERRIDE; |
| 124 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 125 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 126 | int* vertexCount, |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 127 | int* indexCount) const SK_OVERRIDE; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 129 | virtual void clear(const GrIRect* rect, |
| 130 | GrColor color, |
| 131 | GrRenderTarget* renderTarget = NULL) SK_OVERRIDE; |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 132 | |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 133 | protected: |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 134 | virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout, |
| 135 | int vertexCount, |
| 136 | int indexCount) SK_OVERRIDE; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | private: |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 138 | enum Cmd { |
| 139 | kDraw_Cmd = 1, |
| 140 | kSetState_Cmd = 2, |
| 141 | kSetClip_Cmd = 3, |
| 142 | kClear_Cmd = 4, |
| 143 | }; |
| 144 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | struct Draw { |
| bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 146 | GrPrimitiveType fPrimitiveType; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 147 | int fStartVertex; |
| 148 | int fStartIndex; |
| 149 | int fVertexCount; |
| 150 | int fIndexCount; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 151 | GrVertexLayout fVertexLayout; |
| 152 | const GrVertexBuffer* fVertexBuffer; |
| 153 | const GrIndexBuffer* fIndexBuffer; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 156 | struct Clear { |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 157 | Clear() : fRenderTarget(NULL) {} |
| 158 | ~Clear() { GrSafeUnref(fRenderTarget); } |
| 159 | |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 160 | GrIRect fRect; |
| 161 | GrColor fColor; |
| 162 | GrRenderTarget* fRenderTarget; |
| bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 163 | }; |
| 164 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 165 | // overrides from GrDrawTarget |
| 166 | virtual void onDrawIndexed(GrPrimitiveType primitiveType, |
| 167 | int startVertex, |
| 168 | int startIndex, |
| 169 | int vertexCount, |
| bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 170 | int indexCount) SK_OVERRIDE; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 171 | virtual void onDrawNonIndexed(GrPrimitiveType primitiveType, |
| 172 | int startVertex, |
| bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 173 | int vertexCount) SK_OVERRIDE; |
| bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 174 | virtual void onStencilPath(const GrPath&, GrPathFill) SK_OVERRIDE; |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 175 | virtual bool onReserveVertexSpace(GrVertexLayout layout, |
| 176 | int vertexCount, |
| bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 177 | void** vertices) SK_OVERRIDE; |
| 178 | virtual bool onReserveIndexSpace(int indexCount, |
| 179 | void** indices) SK_OVERRIDE; |
| 180 | virtual void releaseReservedVertexSpace() SK_OVERRIDE; |
| 181 | virtual void releaseReservedIndexSpace() SK_OVERRIDE; |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 182 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
| bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 183 | int vertexCount) SK_OVERRIDE; |
| bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 184 | virtual void onSetIndexSourceToArray(const void* indexArray, |
| bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 185 | int indexCount) SK_OVERRIDE; |
| 186 | virtual void releaseVertexArray() SK_OVERRIDE; |
| 187 | virtual void releaseIndexArray() SK_OVERRIDE; |
| 188 | virtual void geometrySourceWillPush() SK_OVERRIDE; |
| 189 | virtual void geometrySourceWillPop( |
| 190 | const GeometrySrcState& restoredState) SK_OVERRIDE; |
| 191 | virtual void clipWillBeSet(const GrClip& newClip) SK_OVERRIDE; |
| bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 192 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 193 | // we lazily record state and clip changes in order to skip clips and states |
| 194 | // that have no effect. |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 195 | bool needsNewState() const; |
| 196 | bool needsNewClip() const; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 198 | // these functions record a command |
| 199 | void recordState(); |
| 200 | void recordDefaultState(); |
| 201 | void recordClip(); |
| 202 | void recordDefaultClip(); |
| 203 | Draw* recordDraw(); |
| 204 | Clear* recordClear(); |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 205 | |
| 206 | // call this to invalidate the tracking data that is used to concatenate |
| 207 | // multiple draws into a single draw. |
| 208 | void resetDrawTracking(); |
| 209 | |
| bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 210 | enum { |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 211 | kCmdPreallocCnt = 32, |
| bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 212 | kDrawPreallocCnt = 8, |
| 213 | kStatePreallocCnt = 8, |
| 214 | kClipPreallocCnt = 8, |
| 215 | kClearPreallocCnt = 4, |
| 216 | kGeoPoolStatePreAllocCnt = 4, |
| 217 | }; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 218 | |
| bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 219 | SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds; |
| bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 220 | GrSTAllocator<kDrawPreallocCnt, Draw> fDraws; |
| bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 221 | GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates; |
| bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 222 | GrSTAllocator<kClearPreallocCnt, Clear> fClears; |
| 223 | GrSTAllocator<kClipPreallocCnt, GrClip> fClips; |
| bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 224 | |
| 225 | GrDrawTarget* fAutoFlushTarget; |
| 226 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 227 | bool fClipSet; |
| 228 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 229 | GrVertexBufferAllocPool& fVertexPool; |
| 230 | |
| 231 | GrIndexBufferAllocPool& fIndexPool; |
| 232 | |
| 233 | // these are used to attempt to concatenate drawRect calls |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 234 | GrVertexLayout fLastRectVertexLayout; |
| 235 | const GrIndexBuffer* fQuadIndexBuffer; |
| 236 | int fMaxQuads; |
| 237 | int fCurrQuad; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 238 | |
| bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 239 | // bookkeeping to attempt to concantenate drawIndexedInstances calls |
| 240 | struct { |
| 241 | int fVerticesPerInstance; |
| 242 | int fIndicesPerInstance; |
| 243 | void reset() { |
| 244 | fVerticesPerInstance = 0; |
| 245 | fIndicesPerInstance = 0; |
| 246 | } |
| 247 | } fInstancedDrawTracker; |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 248 | |
| bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 249 | struct GeometryPoolState { |
| 250 | const GrVertexBuffer* fPoolVertexBuffer; |
| 251 | int fPoolStartVertex; |
| 252 | const GrIndexBuffer* fPoolIndexBuffer; |
| 253 | int fPoolStartIndex; |
| 254 | // caller may conservatively over reserve vertices / indices. |
| 255 | // we release unused space back to allocator if possible |
| 256 | // can only do this if there isn't an intervening pushGeometrySource() |
| 257 | size_t fUsedPoolVertexBytes; |
| 258 | size_t fUsedPoolIndexBytes; |
| 259 | }; |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 260 | SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 261 | |
| robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 262 | bool fFlushing; |
| 263 | |
| bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 264 | typedef GrDrawTarget INHERITED; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | #endif |