blob: 03a2f2d1ee8d23935913a1eaf55a73e851f7a356 [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
bsalomon@google.com1da07462011-03-10 14:51:57 +00002 Copyright 2011 Google Inc.
reed@google.comac10a2d2010-12-22 21:39:39 +00003
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.com86afc2a2011-02-16 16:12:19 +000044 /**
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.com1c13c962011-02-14 16:51:21 +000052 GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool,
53 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000054
55 virtual ~GrInOrderDrawBuffer();
56
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000057 /**
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.comac10a2d2010-12-22 21:39:39 +000062 void initializeDrawStateAndClip(const GrDrawTarget& target);
63
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000064 /**
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.comffca4002011-02-22 20:34:01 +000084 virtual void drawIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000085 int startVertex,
86 int startIndex,
87 int vertexCount,
88 int indexCount);
bsalomon@google.comffca4002011-02-22 20:34:01 +000089 virtual void drawNonIndexed(GrPrimitiveType primitiveType,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000090 int startVertex,
91 int vertexCount);
reed@google.comac10a2d2010-12-22 21:39:39 +000092
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000093 virtual void drawRect(const GrRect& rect,
94 const GrMatrix* matrix = NULL,
95 int stageEnableMask = 0,
96 const GrRect* srcRects[] = NULL,
97 const GrMatrix* srcMatrices[] = NULL);
98
reed@google.comac10a2d2010-12-22 21:39:39 +000099 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 int* vertexCount,
101 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000102
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000103 virtual void clear(GrColor color);
104
reed@google.comac10a2d2010-12-22 21:39:39 +0000105private:
106
107 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000108 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000109 int fStartVertex;
110 int fStartIndex;
111 int fVertexCount;
112 int fIndexCount;
113 bool fStateChanged;
114 bool fClipChanged;
115 GrVertexLayout fVertexLayout;
116 const GrVertexBuffer* fVertexBuffer;
117 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000118 };
119
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000120 struct Clear {
121 int fBeforeDrawIdx;
122 GrColor fColor;
123 };
124
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000125 virtual bool onAcquireGeometry(GrVertexLayout vertexLayout,
126 void** vertices,
127 void** indices);
128 virtual void onReleaseGeometry();
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000129 virtual void clipWillBeSet(const GrClip& newClip);
reed@google.comac10a2d2010-12-22 21:39:39 +0000130
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000131 virtual void onSetVertexSourceToArray(const void* vertexArray,
132 int vertexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000133
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000134 virtual void onSetIndexSourceToArray(const void* indexArray,
135 int indexCount);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000136
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000137 bool needsNewState() const;
138 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000139
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000140 void pushState();
141 void pushClip();
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
143 GrTAllocator<Draw> fDraws;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144 GrTAllocator<SavedDrawState> fStates;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000145 GrTAllocator<Clear> fClears;
reed@google.comac10a2d2010-12-22 21:39:39 +0000146
147 GrTAllocator<GrClip> fClips;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000148 bool fClipSet;
149
150 GrVertexLayout fLastRectVertexLayout;
151 const GrIndexBuffer* fQuadIndexBuffer;
152 int fMaxQuads;
153 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 GrVertexBufferAllocPool& fVertexPool;
156 const GrVertexBuffer* fCurrPoolVertexBuffer;
157 int fCurrPoolStartVertex;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000159 GrIndexBufferAllocPool& fIndexPool;
160 const GrIndexBuffer* fCurrPoolIndexBuffer;
161 int fCurrPoolStartIndex;
162
163 // caller may conservatively over reserve vertices / indices.
reed@google.comac10a2d2010-12-22 21:39:39 +0000164 // we release unused space back to allocator if possible
165 size_t fReservedVertexBytes;
166 size_t fReservedIndexBytes;
167 size_t fUsedReservedVertexBytes;
168 size_t fUsedReservedIndexBytes;
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000169
170 enum {
171 kDrawPreallocCnt = 8,
172 kStatePreallocCnt = 8,
173 kClipPreallocCnt = 8,
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000174 kClearPreallocCnt = 4,
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000175 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000176
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000177 GrAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage;
178 GrAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage;
179 GrAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000180 GrAlignedSTStorage<kClearPreallocCnt, Clear> fClearStorage;
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000181
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000182 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000183};
184
185#endif