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 | /** |
| 56 | * Copies the draw state and clip from target to this draw buffer. |
| 57 | * |
| 58 | * @param target the target whose clip and state should be copied. |
| 59 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | void initializeDrawStateAndClip(const GrDrawTarget& target); |
| 61 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Provides the buffer with an index buffer that can be used for quad rendering. |
| 64 | * The buffer may be able to batch consecutive drawRects if this is provided. |
| 65 | * @param indexBuffer index buffer with quad indices. |
| 66 | */ |
| 67 | void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer); |
| 68 | |
| 69 | /** |
| 70 | * Empties the draw buffer of any queued up draws. |
| 71 | */ |
| 72 | void reset(); |
| 73 | |
| 74 | /** |
| 75 | * plays the queued up draws to another target. Does not empty this buffer so |
| 76 | * that it can be played back multiple times. |
| 77 | * @param target the target to receive the playback |
| 78 | */ |
| 79 | void playback(GrDrawTarget* target); |
| 80 | |
| 81 | // overrides from GrDrawTarget |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 82 | virtual void drawRect(const GrRect& rect, |
| 83 | const GrMatrix* matrix = NULL, |
| 84 | int stageEnableMask = 0, |
| 85 | const GrRect* srcRects[] = NULL, |
| 86 | const GrMatrix* srcMatrices[] = NULL); |
| 87 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 89 | int* vertexCount, |
| 90 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | 6aa25c3 | 2011-04-27 19:55:29 +0000 | [diff] [blame] | 92 | virtual void clear(const GrIRect* rect, GrColor color); |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 94 | virtual bool willUseHWAALines() const; |
| 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 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 148 | const GrGpu* fGpu; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 149 | GrTAllocator<Draw> fDraws; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 150 | GrTAllocator<SavedDrawState> fStates; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 151 | GrTAllocator<Clear> fClears; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 152 | |
| 153 | GrTAllocator<GrClip> fClips; |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 154 | bool fClipSet; |
| 155 | |
| 156 | GrVertexLayout fLastRectVertexLayout; |
| 157 | const GrIndexBuffer* fQuadIndexBuffer; |
| 158 | int fMaxQuads; |
| 159 | int fCurrQuad; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 160 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 161 | GrVertexBufferAllocPool& fVertexPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 163 | GrIndexBufferAllocPool& fIndexPool; |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 164 | struct GeometryPoolState { |
| 165 | const GrVertexBuffer* fPoolVertexBuffer; |
| 166 | int fPoolStartVertex; |
| 167 | const GrIndexBuffer* fPoolIndexBuffer; |
| 168 | int fPoolStartIndex; |
| 169 | // caller may conservatively over reserve vertices / indices. |
| 170 | // we release unused space back to allocator if possible |
| 171 | // can only do this if there isn't an intervening pushGeometrySource() |
| 172 | size_t fUsedPoolVertexBytes; |
| 173 | size_t fUsedPoolIndexBytes; |
| 174 | }; |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 175 | SkTArray<GeometryPoolState> fGeoPoolStateStack; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 176 | |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 177 | |
| 178 | enum { |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 179 | kDrawPreallocCnt = 8, |
| 180 | kStatePreallocCnt = 8, |
| 181 | kClipPreallocCnt = 8, |
| 182 | kClearPreallocCnt = 4, |
| 183 | kGeoPoolStatePreAllocCnt = 4, |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 184 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 185 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 186 | SkAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage; |
| 187 | SkAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage; |
| 188 | SkAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage; |
| 189 | SkAlignedSTStorage<kClearPreallocCnt, Clear> fClearStorage; |
| 190 | SkAlignedSTStorage<kGeoPoolStatePreAllocCnt, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 191 | GeometryPoolState> fGeoStackStorage; |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 192 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 193 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
| 196 | #endif |