blob: 463ea2d6512a30f1f00ca0961bb6f100c4c0abfa [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;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027class GrIndexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
bsalomon@google.com1c13c962011-02-14 16:51:21 +000029/**
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.comac10a2d2010-12-22 21:39:39 +000041class GrInOrderDrawBuffer : public GrDrawTarget {
42public:
43
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool,
45 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000046
47 virtual ~GrInOrderDrawBuffer();
48
49 void initializeDrawStateAndClip(const GrDrawTarget& target);
50
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051 virtual void drawIndexed(PrimitiveType primitiveType,
52 int startVertex,
53 int startIndex,
54 int vertexCount,
55 int indexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +000056
bsalomon@google.com1c13c962011-02-14 16:51:21 +000057 virtual void drawNonIndexed(PrimitiveType primitiveType,
58 int startVertex,
59 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +000060
61 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000062 int* vertexCount,
63 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +000064
65 void reset();
66
67 void playback(GrDrawTarget* target);
68
69private:
70
71 struct Draw {
bsalomon@google.com1c13c962011-02-14 16:51:21 +000072 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.comac10a2d2010-12-22 21:39:39 +000082 };
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.com1c13c962011-02-14 16:51:21 +000090 virtual void setVertexSourceToArrayHelper(const void* vertexArray,
91 int vertexCount);
92
93 virtual void setIndexSourceToArrayHelper(const void* indexArray,
94 int indexCount);
95
reed@google.comac10a2d2010-12-22 21:39:39 +000096
97 bool grabState();
98 bool grabClip();
99
100 GrTAllocator<Draw> fDraws;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000101 // 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.comac10a2d2010-12-22 21:39:39 +0000106 GrTAllocator<SavedDrawState> fStates;
107
108 GrTAllocator<GrClip> fClips;
109 bool fClipChanged;
110
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000111 GrVertexBufferAllocPool& fVertexPool;
112 const GrVertexBuffer* fCurrPoolVertexBuffer;
113 int fCurrPoolStartVertex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000115 GrIndexBufferAllocPool& fIndexPool;
116 const GrIndexBuffer* fCurrPoolIndexBuffer;
117 int fCurrPoolStartIndex;
118
119 // caller may conservatively over reserve vertices / indices.
reed@google.comac10a2d2010-12-22 21:39:39 +0000120 // 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