blob: 805861ada1dcba241e389a66269ea182e3186ab0 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
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
26class GrVertexBufferAllocPool;
27
28// TODO: don't save clip per draw
29class GrInOrderDrawBuffer : public GrDrawTarget {
30public:
31
32 GrInOrderDrawBuffer(GrVertexBufferAllocPool* pool = NULL);
33
34 virtual ~GrInOrderDrawBuffer();
35
36 void initializeDrawStateAndClip(const GrDrawTarget& target);
37
38 virtual void drawIndexed(PrimitiveType type,
39 uint32_t startVertex,
40 uint32_t startIndex,
41 uint32_t vertexCount,
42 uint32_t indexCount);
43
44 virtual void drawNonIndexed(PrimitiveType type,
45 uint32_t startVertex,
46 uint32_t vertexCount);
47
48 virtual bool geometryHints(GrVertexLayout vertexLayout,
49 int32_t* vertexCount,
50 int32_t* indexCount) const;
51
52 void reset();
53
54 void playback(GrDrawTarget* target);
55
56private:
57
58 struct Draw {
59 PrimitiveType fType;
60 uint32_t fStartVertex;
61 uint32_t fStartIndex;
62 uint32_t fVertexCount;
63 uint32_t fIndexCount;
64 bool fStateChange;
65 GrVertexLayout fVertexLayout;
66 bool fUseVertexBuffer;
67 bool fClipChanged;
68 union {
69 const GrVertexBuffer* fVertexBuffer;
70 const void* fVertexArray;
71 };
72 bool fUseIndexBuffer;
73 union {
74 const GrIndexBuffer* fIndexBuffer;
75 const void* fIndexArray;
76 };
77 };
78
79 virtual bool acquireGeometryHelper(GrVertexLayout vertexLayout,
80 void** vertices,
81 void** indices);
82 virtual void releaseGeometryHelper();
83 virtual void clipWillChange(const GrClip& clip);
84
85
86 bool grabState();
87 bool grabClip();
88
89 GrTAllocator<Draw> fDraws;
90 // HACK: We hold refs on textures in saved state but not RTs, VBs, and IBs.
91 // a) RTs aren't ref counted (yet)
92 // b) we are only using this class for text which doesn't use VBs or IBs
93 // This should be fixed by either refcounting them all or having some
94 // notification occur if a cache is purging an object we have a ptr to.
95 GrTAllocator<SavedDrawState> fStates;
96
97 GrTAllocator<GrClip> fClips;
98 bool fClipChanged;
99
100 // vertices are either queued in cpu arrays or some vertex buffer pool
101 // that knows about a specific GrGpu object.
102 GrAllocPool fCPUVertices;
103 GrVertexBufferAllocPool* fBufferVertices;
104 GrAllocPool fIndices;
105 void* fCurrReservedVertices;
106 void* fCurrReservedIndices;
107 // valid if we're queueing vertices in fBufferVertices
108 GrVertexBuffer* fCurrVertexBuffer;
109 uint32_t fCurrStartVertex;
110
111 // caller may conservatively over allocate vertices / indices.
112 // we release unused space back to allocator if possible
113 size_t fReservedVertexBytes;
114 size_t fReservedIndexBytes;
115 size_t fUsedReservedVertexBytes;
116 size_t fUsedReservedIndexBytes;
117
118 static const uint32_t STATES_BLOCK_SIZE = 8;
119 static const uint32_t DRAWS_BLOCK_SIZE = 8;
120 static const uint32_t CLIPS_BLOCK_SIZE = 8;
121 static const uint32_t VERTEX_BLOCK_SIZE = 1 << 12;
122 static const uint32_t INDEX_BLOCK_SIZE = 1 << 10;
123 int8_t fDrawsStorage[sizeof(Draw) *
124 DRAWS_BLOCK_SIZE];
125 int8_t fStatesStorage[sizeof(SavedDrawState) *
126 STATES_BLOCK_SIZE];
127 int8_t fClipsStorage[sizeof(GrClip) *
128 CLIPS_BLOCK_SIZE];
129};
130
131#endif