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