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 | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 75 | * @param target the target to receive the playback |
| 76 | */ |
| 77 | void playback(GrDrawTarget* target); |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * A convenience method to do a playback followed by a reset. All the |
| 81 | * constraints and side-effects or playback() and reset apply(). |
| 82 | */ |
| 83 | void flushTo(GrDrawTarget* target) { |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 84 | if (fFlushing) { |
| 85 | // When creating SW-only clip masks, the GrClipMaskManager can |
| 86 | // cause a GrContext::flush (when copying the mask results back |
| 87 | // to the GPU). Without a guard this results in a recursive call |
| 88 | // to this method. |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | fFlushing = true; |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 93 | this->playback(target); |
| 94 | this->reset(); |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 95 | fFlushing = false; |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * This function allows the draw buffer to automatically flush itself to |
| 100 | * another target. This means the buffer may internally call |
| 101 | * this->flushTo(target) when it is safe to do so. |
| 102 | * |
| 103 | * When the auto flush target is set to NULL (as it initially is) the draw |
| 104 | * buffer will never automatically flush itself. |
| 105 | */ |
| 106 | void setAutoFlushTarget(GrDrawTarget* target); |
| 107 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 108 | // overrides from GrDrawTarget |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 109 | virtual void drawRect(const GrRect& rect, |
| 110 | const GrMatrix* matrix = NULL, |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 111 | StageMask stageEnableMask = 0, |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 112 | const GrRect* srcRects[] = NULL, |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 113 | const GrMatrix* srcMatrices[] = NULL) SK_OVERRIDE; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 115 | virtual void drawIndexedInstances(GrPrimitiveType type, |
| 116 | int instanceCount, |
| 117 | int verticesPerInstance, |
| 118 | int indicesPerInstance) |
| 119 | SK_OVERRIDE; |
| 120 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 122 | int* vertexCount, |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 123 | int* indexCount) const SK_OVERRIDE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 125 | virtual void clear(const GrIRect* rect, |
| 126 | GrColor color, |
| 127 | GrRenderTarget* renderTarget = NULL) SK_OVERRIDE; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 128 | |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 129 | protected: |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 130 | virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout, |
| 131 | int vertexCount, |
| 132 | int indexCount) SK_OVERRIDE; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 133 | private: |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | struct Draw { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 135 | GrPrimitiveType fPrimitiveType; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 136 | int fStartVertex; |
| 137 | int fStartIndex; |
| 138 | int fVertexCount; |
| 139 | int fIndexCount; |
| 140 | bool fStateChanged; |
| 141 | bool fClipChanged; |
| 142 | GrVertexLayout fVertexLayout; |
| 143 | const GrVertexBuffer* fVertexBuffer; |
| 144 | const GrIndexBuffer* fIndexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 147 | struct Clear { |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 148 | Clear() : fRenderTarget(NULL) {} |
| 149 | ~Clear() { GrSafeUnref(fRenderTarget); } |
| 150 | |
| 151 | int fBeforeDrawIdx; |
| 152 | GrIRect fRect; |
| 153 | GrColor fColor; |
| 154 | GrRenderTarget* fRenderTarget; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 157 | // overrides from GrDrawTarget |
| 158 | virtual void onDrawIndexed(GrPrimitiveType primitiveType, |
| 159 | int startVertex, |
| 160 | int startIndex, |
| 161 | int vertexCount, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 162 | int indexCount) SK_OVERRIDE; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 163 | virtual void onDrawNonIndexed(GrPrimitiveType primitiveType, |
| 164 | int startVertex, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 165 | int vertexCount) SK_OVERRIDE; |
bsalomon@google.com | 64aef2b | 2012-06-11 15:36:13 +0000 | [diff] [blame] | 166 | virtual void onStencilPath(const GrPath&, GrPathFill) SK_OVERRIDE; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 167 | virtual bool onReserveVertexSpace(GrVertexLayout layout, |
| 168 | int vertexCount, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 169 | void** vertices) SK_OVERRIDE; |
| 170 | virtual bool onReserveIndexSpace(int indexCount, |
| 171 | void** indices) SK_OVERRIDE; |
| 172 | virtual void releaseReservedVertexSpace() SK_OVERRIDE; |
| 173 | virtual void releaseReservedIndexSpace() SK_OVERRIDE; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 174 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 175 | int vertexCount) SK_OVERRIDE; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 176 | virtual void onSetIndexSourceToArray(const void* indexArray, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 177 | int indexCount) SK_OVERRIDE; |
| 178 | virtual void releaseVertexArray() SK_OVERRIDE; |
| 179 | virtual void releaseIndexArray() SK_OVERRIDE; |
| 180 | virtual void geometrySourceWillPush() SK_OVERRIDE; |
| 181 | virtual void geometrySourceWillPop( |
| 182 | const GeometrySrcState& restoredState) SK_OVERRIDE; |
| 183 | virtual void clipWillBeSet(const GrClip& newClip) SK_OVERRIDE; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 184 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 185 | bool needsNewState() const; |
| 186 | bool needsNewClip() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 188 | void pushState(); |
robertphillips@google.com | 49d9fd5 | 2012-05-23 11:44:08 +0000 | [diff] [blame] | 189 | void storeClip(); |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 190 | |
| 191 | // call this to invalidate the tracking data that is used to concatenate |
| 192 | // multiple draws into a single draw. |
| 193 | void resetDrawTracking(); |
| 194 | |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 195 | enum { |
| 196 | kDrawPreallocCnt = 8, |
| 197 | kStatePreallocCnt = 8, |
| 198 | kClipPreallocCnt = 8, |
| 199 | kClearPreallocCnt = 4, |
| 200 | kGeoPoolStatePreAllocCnt = 4, |
| 201 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 203 | GrSTAllocator<kDrawPreallocCnt, Draw> fDraws; |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 204 | GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates; |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 205 | GrSTAllocator<kClearPreallocCnt, Clear> fClears; |
| 206 | GrSTAllocator<kClipPreallocCnt, GrClip> fClips; |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 207 | |
| 208 | GrDrawTarget* fAutoFlushTarget; |
| 209 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 210 | bool fClipSet; |
| 211 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 212 | GrVertexBufferAllocPool& fVertexPool; |
| 213 | |
| 214 | GrIndexBufferAllocPool& fIndexPool; |
| 215 | |
| 216 | // these are used to attempt to concatenate drawRect calls |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 217 | GrVertexLayout fLastRectVertexLayout; |
| 218 | const GrIndexBuffer* fQuadIndexBuffer; |
| 219 | int fMaxQuads; |
| 220 | int fCurrQuad; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 222 | // bookkeeping to attempt to concantenate drawIndexedInstances calls |
| 223 | struct { |
| 224 | int fVerticesPerInstance; |
| 225 | int fIndicesPerInstance; |
| 226 | void reset() { |
| 227 | fVerticesPerInstance = 0; |
| 228 | fIndicesPerInstance = 0; |
| 229 | } |
| 230 | } fInstancedDrawTracker; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 231 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 232 | struct GeometryPoolState { |
| 233 | const GrVertexBuffer* fPoolVertexBuffer; |
| 234 | int fPoolStartVertex; |
| 235 | const GrIndexBuffer* fPoolIndexBuffer; |
| 236 | int fPoolStartIndex; |
| 237 | // caller may conservatively over reserve vertices / indices. |
| 238 | // we release unused space back to allocator if possible |
| 239 | // can only do this if there isn't an intervening pushGeometrySource() |
| 240 | size_t fUsedPoolVertexBytes; |
| 241 | size_t fUsedPoolIndexBytes; |
| 242 | }; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 243 | SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 244 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame^] | 245 | bool fFlushing; |
| 246 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 247 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 248 | }; |
| 249 | |
| 250 | #endif |