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 | |
| 19 | class GrVertexBufferAllocPool; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 20 | class GrIndexBufferAllocPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 22 | /** |
| 23 | * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up |
| 24 | * draws for eventual playback into a GrGpu. In theory one draw buffer could |
| 25 | * playback into another. When index or vertex buffers are used as geometry |
| 26 | * sources it is the callers the draw buffer only holds references to the |
| 27 | * buffers. It is the callers responsibility to ensure that the data is still |
| 28 | * valid when the draw buffer is played back into a GrGpu. Similarly, it is the |
| 29 | * caller's responsibility to ensure that all referenced textures, buffers, |
| 30 | * and rendertargets are associated in the GrGpu object that the buffer is |
| 31 | * played back into. The buffer requires VB and IB pools to store geometry. |
| 32 | */ |
| 33 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | class GrInOrderDrawBuffer : public GrDrawTarget { |
| 35 | public: |
| 36 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 37 | /** |
| 38 | * Creates a GrInOrderDrawBuffer |
| 39 | * |
| 40 | * @param vertexPool pool where vertices for queued draws will be saved when |
| 41 | * the vertex source is either reserved or array. |
| 42 | * @param indexPool pool where indices for queued draws will be saved when |
| 43 | * the index source is either reserved or array. |
| 44 | */ |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 45 | GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool, |
| 46 | GrIndexBufferAllocPool* indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 47 | |
| 48 | virtual ~GrInOrderDrawBuffer(); |
| 49 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 50 | /** |
| 51 | * Copies the draw state and clip from target to this draw buffer. |
| 52 | * |
| 53 | * @param target the target whose clip and state should be copied. |
| 54 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 55 | void initializeDrawStateAndClip(const GrDrawTarget& target); |
| 56 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Provides the buffer with an index buffer that can be used for quad rendering. |
| 59 | * The buffer may be able to batch consecutive drawRects if this is provided. |
| 60 | * @param indexBuffer index buffer with quad indices. |
| 61 | */ |
| 62 | void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer); |
| 63 | |
| 64 | /** |
| 65 | * Empties the draw buffer of any queued up draws. |
| 66 | */ |
| 67 | void reset(); |
| 68 | |
| 69 | /** |
| 70 | * plays the queued up draws to another target. Does not empty this buffer so |
| 71 | * that it can be played back multiple times. |
| 72 | * @param target the target to receive the playback |
| 73 | */ |
| 74 | void playback(GrDrawTarget* target); |
| 75 | |
| 76 | // overrides from GrDrawTarget |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 77 | virtual void drawRect(const GrRect& rect, |
| 78 | const GrMatrix* matrix = NULL, |
| 79 | int stageEnableMask = 0, |
| 80 | const GrRect* srcRects[] = NULL, |
| 81 | const GrMatrix* srcMatrices[] = NULL); |
| 82 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 83 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 84 | int* vertexCount, |
| 85 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 87 | virtual void clear(const GrIRect* rect, GrColor color); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 88 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | private: |
| 90 | |
| 91 | struct Draw { |
bsalomon@google.com | ffca400 | 2011-02-22 20:34:01 +0000 | [diff] [blame] | 92 | GrPrimitiveType fPrimitiveType; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 93 | int fStartVertex; |
| 94 | int fStartIndex; |
| 95 | int fVertexCount; |
| 96 | int fIndexCount; |
| 97 | bool fStateChanged; |
| 98 | bool fClipChanged; |
| 99 | GrVertexLayout fVertexLayout; |
| 100 | const GrVertexBuffer* fVertexBuffer; |
| 101 | const GrIndexBuffer* fIndexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | }; |
| 103 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 104 | struct Clear { |
| 105 | int fBeforeDrawIdx; |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 106 | GrIRect fRect; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 107 | GrColor fColor; |
| 108 | }; |
| 109 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 110 | // overrides from GrDrawTarget |
| 111 | virtual void onDrawIndexed(GrPrimitiveType primitiveType, |
| 112 | int startVertex, |
| 113 | int startIndex, |
| 114 | int vertexCount, |
| 115 | int indexCount); |
| 116 | virtual void onDrawNonIndexed(GrPrimitiveType primitiveType, |
| 117 | int startVertex, |
| 118 | int vertexCount); |
| 119 | virtual bool onReserveVertexSpace(GrVertexLayout layout, |
| 120 | int vertexCount, |
| 121 | void** vertices); |
| 122 | virtual bool onReserveIndexSpace(int indexCount, void** indices); |
| 123 | virtual void releaseReservedVertexSpace(); |
| 124 | virtual void releaseReservedIndexSpace(); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 125 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 126 | int vertexCount); |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 127 | virtual void onSetIndexSourceToArray(const void* indexArray, |
| 128 | int indexCount); |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 129 | virtual void releaseVertexArray(); |
| 130 | virtual void releaseIndexArray(); |
| 131 | virtual void geometrySourceWillPush(); |
| 132 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState); |
| 133 | virtual void clipWillBeSet(const GrClip& newClip); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 134 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 135 | bool needsNewState() const; |
| 136 | bool needsNewClip() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 137 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 138 | void pushState(); |
| 139 | void pushClip(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 140 | |
| 141 | GrTAllocator<Draw> fDraws; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 142 | GrTAllocator<SavedDrawState> fStates; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 143 | GrTAllocator<Clear> fClears; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | |
| 145 | GrTAllocator<GrClip> fClips; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 146 | bool fClipSet; |
| 147 | |
| 148 | GrVertexLayout fLastRectVertexLayout; |
| 149 | const GrIndexBuffer* fQuadIndexBuffer; |
| 150 | int fMaxQuads; |
| 151 | int fCurrQuad; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 153 | GrVertexBufferAllocPool& fVertexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 154 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 155 | GrIndexBufferAllocPool& fIndexPool; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 156 | struct GeometryPoolState { |
| 157 | const GrVertexBuffer* fPoolVertexBuffer; |
| 158 | int fPoolStartVertex; |
| 159 | const GrIndexBuffer* fPoolIndexBuffer; |
| 160 | int fPoolStartIndex; |
| 161 | // caller may conservatively over reserve vertices / indices. |
| 162 | // we release unused space back to allocator if possible |
| 163 | // can only do this if there isn't an intervening pushGeometrySource() |
| 164 | size_t fUsedPoolVertexBytes; |
| 165 | size_t fUsedPoolIndexBytes; |
| 166 | }; |
| 167 | GrTArray<GeometryPoolState> fGeoPoolStateStack; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 168 | |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 169 | |
| 170 | enum { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 171 | kDrawPreallocCnt = 8, |
| 172 | kStatePreallocCnt = 8, |
| 173 | kClipPreallocCnt = 8, |
| 174 | kClearPreallocCnt = 4, |
| 175 | kGeoPoolStatePreAllocCnt = 4, |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 176 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 177 | |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 178 | GrAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage; |
| 179 | GrAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage; |
| 180 | GrAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 181 | GrAlignedSTStorage<kClearPreallocCnt, Clear> fClearStorage; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 182 | GrAlignedSTStorage<kGeoPoolStatePreAllocCnt, |
| 183 | GeometryPoolState> fGeoStackStorage; |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 184 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 185 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | #endif |