reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 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 | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 44 | GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool, |
| 45 | GrIndexBufferAllocPool* indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 46 | |
| 47 | virtual ~GrInOrderDrawBuffer(); |
| 48 | |
| 49 | void initializeDrawStateAndClip(const GrDrawTarget& target); |
| 50 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 51 | virtual void drawIndexed(PrimitiveType primitiveType, |
| 52 | int startVertex, |
| 53 | int startIndex, |
| 54 | int vertexCount, |
| 55 | int indexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 56 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 57 | virtual void drawNonIndexed(PrimitiveType primitiveType, |
| 58 | int startVertex, |
| 59 | int vertexCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | |
| 61 | virtual bool geometryHints(GrVertexLayout vertexLayout, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 62 | int* vertexCount, |
| 63 | int* indexCount) const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 64 | |
| 65 | void reset(); |
| 66 | |
| 67 | void playback(GrDrawTarget* target); |
| 68 | |
| 69 | private: |
| 70 | |
| 71 | struct Draw { |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 72 | PrimitiveType fPrimitiveType; |
| 73 | int fStartVertex; |
| 74 | int fStartIndex; |
| 75 | int fVertexCount; |
| 76 | int fIndexCount; |
| 77 | bool fStateChanged; |
| 78 | bool fClipChanged; |
| 79 | GrVertexLayout fVertexLayout; |
| 80 | const GrVertexBuffer* fVertexBuffer; |
| 81 | const GrIndexBuffer* fIndexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout, |
| 85 | void** vertices, |
| 86 | void** indices); |
| 87 | virtual void releaseGeometryHelper(); |
| 88 | virtual void clipWillChange(const GrClip& clip); |
| 89 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 90 | virtual void setVertexSourceToArrayHelper(const void* vertexArray, |
| 91 | int vertexCount); |
| 92 | |
| 93 | virtual void setIndexSourceToArrayHelper(const void* indexArray, |
| 94 | int indexCount); |
| 95 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | |
| 97 | bool grabState(); |
| 98 | bool grabClip(); |
| 99 | |
| 100 | GrTAllocator<Draw> fDraws; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 101 | // HACK: We currently do not hold refs on RTs in the saved draw states. |
| 102 | // The reason is that in the GL implementation when a GrTexture is destroyed |
| 103 | // that has an associated RT the RT is destroyed regardless of its ref count. |
| 104 | // We need a third object that holds the shared GL ids and persists until |
| 105 | // both reach ref count 0. (skia issue 122) |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 106 | GrTAllocator<SavedDrawState> fStates; |
| 107 | |
| 108 | GrTAllocator<GrClip> fClips; |
| 109 | bool fClipChanged; |
| 110 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 111 | GrVertexBufferAllocPool& fVertexPool; |
| 112 | const GrVertexBuffer* fCurrPoolVertexBuffer; |
| 113 | int fCurrPoolStartVertex; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame^] | 115 | GrIndexBufferAllocPool& fIndexPool; |
| 116 | const GrIndexBuffer* fCurrPoolIndexBuffer; |
| 117 | int fCurrPoolStartIndex; |
| 118 | |
| 119 | // caller may conservatively over reserve vertices / indices. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 120 | // we release unused space back to allocator if possible |
| 121 | size_t fReservedVertexBytes; |
| 122 | size_t fReservedIndexBytes; |
| 123 | size_t fUsedReservedVertexBytes; |
| 124 | size_t fUsedReservedIndexBytes; |
| 125 | |
| 126 | static const uint32_t STATES_BLOCK_SIZE = 8; |
| 127 | static const uint32_t DRAWS_BLOCK_SIZE = 8; |
| 128 | static const uint32_t CLIPS_BLOCK_SIZE = 8; |
| 129 | static const uint32_t VERTEX_BLOCK_SIZE = 1 << 12; |
| 130 | static const uint32_t INDEX_BLOCK_SIZE = 1 << 10; |
| 131 | int8_t fDrawsStorage[sizeof(Draw) * |
| 132 | DRAWS_BLOCK_SIZE]; |
| 133 | int8_t fStatesStorage[sizeof(SavedDrawState) * |
| 134 | STATES_BLOCK_SIZE]; |
| 135 | int8_t fClipsStorage[sizeof(GrClip) * |
| 136 | CLIPS_BLOCK_SIZE]; |
| 137 | }; |
| 138 | |
| 139 | #endif |