blob: 2a3bddb081938f44da8a1f8692dd87274efd6d59 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#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
19class GrVertexBufferAllocPool;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020class GrIndexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000021
bsalomon@google.com1c13c962011-02-14 16:51:21 +000022/**
23 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up
24 * draws for eventual playback into a GrGpu. In theory one draw buffer could
25 * playback into another. When index or vertex buffers are used as geometry
26 * sources it is the callers the draw buffer only holds references to the
27 * buffers. It is the callers responsibility to ensure that the data is still
28 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the
29 * caller's responsibility to ensure that all referenced textures, buffers,
30 * and rendertargets are associated in the GrGpu object that the buffer is
31 * played back into. The buffer requires VB and IB pools to store geometry.
32 */
33
reed@google.comac10a2d2010-12-22 21:39:39 +000034class GrInOrderDrawBuffer : public GrDrawTarget {
35public:
36
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000037 /**
38 * Creates a GrInOrderDrawBuffer
39 *
40 * @param vertexPool pool where vertices for queued draws will be saved when
41 * the vertex source is either reserved or array.
42 * @param indexPool pool where indices for queued draws will be saved when
43 * the index source is either reserved or array.
44 */
bsalomon@google.com1c13c962011-02-14 16:51:21 +000045 GrInOrderDrawBuffer(GrVertexBufferAllocPool* vertexPool,
46 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000047
48 virtual ~GrInOrderDrawBuffer();
49
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000050 /**
51 * Copies the draw state and clip from target to this draw buffer.
52 *
53 * @param target the target whose clip and state should be copied.
54 */
reed@google.comac10a2d2010-12-22 21:39:39 +000055 void initializeDrawStateAndClip(const GrDrawTarget& target);
56
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000057 /**
58 * Provides the buffer with an index buffer that can be used for quad rendering.
59 * The buffer may be able to batch consecutive drawRects if this is provided.
60 * @param indexBuffer index buffer with quad indices.
61 */
62 void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer);
63
64 /**
65 * Empties the draw buffer of any queued up draws.
66 */
67 void reset();
68
69 /**
70 * plays the queued up draws to another target. Does not empty this buffer so
71 * that it can be played back multiple times.
72 * @param target the target to receive the playback
73 */
74 void playback(GrDrawTarget* target);
75
76 // overrides from GrDrawTarget
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000077 virtual void drawRect(const GrRect& rect,
78 const GrMatrix* matrix = NULL,
79 int stageEnableMask = 0,
80 const GrRect* srcRects[] = NULL,
81 const GrMatrix* srcMatrices[] = NULL);
82
reed@google.comac10a2d2010-12-22 21:39:39 +000083 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000084 int* vertexCount,
85 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +000086
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000087 virtual void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com0b335c12011-04-25 19:17:44 +000088
reed@google.comac10a2d2010-12-22 21:39:39 +000089private:
90
91 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +000092 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000093 int fStartVertex;
94 int fStartIndex;
95 int fVertexCount;
96 int fIndexCount;
97 bool fStateChanged;
98 bool fClipChanged;
99 GrVertexLayout fVertexLayout;
100 const GrVertexBuffer* fVertexBuffer;
101 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 };
103
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000104 struct Clear {
105 int fBeforeDrawIdx;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000106 GrIRect fRect;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000107 GrColor fColor;
108 };
109
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000110 // overrides from GrDrawTarget
111 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
112 int startVertex,
113 int startIndex,
114 int vertexCount,
115 int indexCount);
116 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
117 int startVertex,
118 int vertexCount);
119 virtual bool onReserveVertexSpace(GrVertexLayout layout,
120 int vertexCount,
121 void** vertices);
122 virtual bool onReserveIndexSpace(int indexCount, void** indices);
123 virtual void releaseReservedVertexSpace();
124 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000125 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000126 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000127 virtual void onSetIndexSourceToArray(const void* indexArray,
128 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000129 virtual void releaseVertexArray();
130 virtual void releaseIndexArray();
131 virtual void geometrySourceWillPush();
132 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
133 virtual void clipWillBeSet(const GrClip& newClip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000134
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000135 bool needsNewState() const;
136 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000137
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000138 void pushState();
139 void pushClip();
reed@google.comac10a2d2010-12-22 21:39:39 +0000140
141 GrTAllocator<Draw> fDraws;
reed@google.comac10a2d2010-12-22 21:39:39 +0000142 GrTAllocator<SavedDrawState> fStates;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000143 GrTAllocator<Clear> fClears;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
145 GrTAllocator<GrClip> fClips;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000146 bool fClipSet;
147
148 GrVertexLayout fLastRectVertexLayout;
149 const GrIndexBuffer* fQuadIndexBuffer;
150 int fMaxQuads;
151 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000152
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000153 GrVertexBufferAllocPool& fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000154
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 GrIndexBufferAllocPool& fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000156 struct GeometryPoolState {
157 const GrVertexBuffer* fPoolVertexBuffer;
158 int fPoolStartVertex;
159 const GrIndexBuffer* fPoolIndexBuffer;
160 int fPoolStartIndex;
161 // caller may conservatively over reserve vertices / indices.
162 // we release unused space back to allocator if possible
163 // can only do this if there isn't an intervening pushGeometrySource()
164 size_t fUsedPoolVertexBytes;
165 size_t fUsedPoolIndexBytes;
166 };
167 GrTArray<GeometryPoolState> fGeoPoolStateStack;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000169
170 enum {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000171 kDrawPreallocCnt = 8,
172 kStatePreallocCnt = 8,
173 kClipPreallocCnt = 8,
174 kClearPreallocCnt = 4,
175 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000176 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000177
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000178 GrAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage;
179 GrAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage;
180 GrAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000181 GrAlignedSTStorage<kClearPreallocCnt, Clear> fClearStorage;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000182 GrAlignedSTStorage<kGeoPoolStatePreAllocCnt,
183 GeometryPoolState> fGeoStackStorage;
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000184
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000185 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000186};
187
188#endif