blob: 3b27d70f3d5ae718c9bf7a5535cbcdf76cfde263 [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
bsalomon@google.com471d4712011-08-23 15:45:25 +000019class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000021class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000022
bsalomon@google.com1c13c962011-02-14 16:51:21 +000023/**
24 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up
25 * draws for eventual playback into a GrGpu. In theory one draw buffer could
26 * playback into another. When index or vertex buffers are used as geometry
27 * sources it is the callers the draw buffer only holds references to the
28 * buffers. It is the callers responsibility to ensure that the data is still
29 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the
30 * caller's responsibility to ensure that all referenced textures, buffers,
31 * and rendertargets are associated in the GrGpu object that the buffer is
32 * played back into. The buffer requires VB and IB pools to store geometry.
33 */
34
reed@google.comac10a2d2010-12-22 21:39:39 +000035class GrInOrderDrawBuffer : public GrDrawTarget {
36public:
37
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000038 /**
39 * Creates a GrInOrderDrawBuffer
40 *
bsalomon@google.com471d4712011-08-23 15:45:25 +000041 * @param gpu the gpu object where this will be played back
42 * (possible indirectly). GrResources used with the draw
43 * buffer are created by this gpu object.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000044 * @param vertexPool pool where vertices for queued draws will be saved when
45 * the vertex source is either reserved or array.
46 * @param indexPool pool where indices for queued draws will be saved when
47 * the index source is either reserved or array.
48 */
bsalomon@google.com471d4712011-08-23 15:45:25 +000049 GrInOrderDrawBuffer(const GrGpu* gpu,
50 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000052
53 virtual ~GrInOrderDrawBuffer();
54
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000055 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000056 * Provides the buffer with an index buffer that can be used for quad rendering.
57 * The buffer may be able to batch consecutive drawRects if this is provided.
58 * @param indexBuffer index buffer with quad indices.
59 */
60 void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer);
61
62 /**
bsalomon@google.com97805382012-03-13 14:32:07 +000063 * Empties the draw buffer of any queued up draws. This must not be called
64 * while inside an unbalanced pushGeometrySource().
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000065 */
66 void reset();
67
68 /**
bsalomon@google.com97805382012-03-13 14:32:07 +000069 * plays the queued up draws to another target. Does not empty this buffer
70 * so that it can be played back multiple times. This buffer must not have
71 * an active reserved vertex or index source. Any reserved geometry on
72 * the target will be finalized because it's geometry source will be pushed
73 * before playback and popped afterwards.
74 *
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000075 * @param target the target to receive the playback
76 */
77 void playback(GrDrawTarget* target);
bsalomon@google.com97805382012-03-13 14:32:07 +000078
79 /**
80 * A convenience method to do a playback followed by a reset. All the
81 * constraints and side-effects or playback() and reset apply().
82 */
83 void flushTo(GrDrawTarget* target) {
84 this->playback(target);
85 this->reset();
86 }
87
88 /**
89 * This function allows the draw buffer to automatically flush itself to
90 * another target. This means the buffer may internally call
91 * this->flushTo(target) when it is safe to do so.
92 *
93 * When the auto flush target is set to NULL (as it initially is) the draw
94 * buffer will never automatically flush itself.
95 */
96 void setAutoFlushTarget(GrDrawTarget* target);
97
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000098 // overrides from GrDrawTarget
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000099 virtual void drawRect(const GrRect& rect,
100 const GrMatrix* matrix = NULL,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000101 StageMask stageEnableMask = 0,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000102 const GrRect* srcRects[] = NULL,
bsalomon@google.com97805382012-03-13 14:32:07 +0000103 const GrMatrix* srcMatrices[] = NULL) SK_OVERRIDE;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000104
bsalomon@google.com934c5702012-03-20 21:17:58 +0000105 virtual void drawIndexedInstances(GrPrimitiveType type,
106 int instanceCount,
107 int verticesPerInstance,
108 int indicesPerInstance)
109 SK_OVERRIDE;
110
reed@google.comac10a2d2010-12-22 21:39:39 +0000111 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000112 int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000113 int* indexCount) const SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000114
bsalomon@google.com97805382012-03-13 14:32:07 +0000115 virtual void clear(const GrIRect* rect, GrColor color) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000116
bsalomon@google.com97805382012-03-13 14:32:07 +0000117protected:
bsalomon@google.com97805382012-03-13 14:32:07 +0000118 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
119 int vertexCount,
120 int indexCount) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000121private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000122 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000123 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000124 int fStartVertex;
125 int fStartIndex;
126 int fVertexCount;
127 int fIndexCount;
128 bool fStateChanged;
129 bool fClipChanged;
130 GrVertexLayout fVertexLayout;
131 const GrVertexBuffer* fVertexBuffer;
132 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000133 };
134
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000135 struct Clear {
136 int fBeforeDrawIdx;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000137 GrIRect fRect;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000138 GrColor fColor;
139 };
140
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000141 // overrides from GrDrawTarget
142 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
143 int startVertex,
144 int startIndex,
145 int vertexCount,
146 int indexCount);
147 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
148 int startVertex,
149 int vertexCount);
150 virtual bool onReserveVertexSpace(GrVertexLayout layout,
151 int vertexCount,
152 void** vertices);
153 virtual bool onReserveIndexSpace(int indexCount, void** indices);
154 virtual void releaseReservedVertexSpace();
155 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000156 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000157 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000158 virtual void onSetIndexSourceToArray(const void* indexArray,
159 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000160 virtual void releaseVertexArray();
161 virtual void releaseIndexArray();
162 virtual void geometrySourceWillPush();
163 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
164 virtual void clipWillBeSet(const GrClip& newClip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000165
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000166 bool needsNewState() const;
167 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000169 void pushState();
170 void pushClip();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000171
172 // call this to invalidate the tracking data that is used to concatenate
173 // multiple draws into a single draw.
174 void resetDrawTracking();
175
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000176 enum {
177 kDrawPreallocCnt = 8,
178 kStatePreallocCnt = 8,
179 kClipPreallocCnt = 8,
180 kClearPreallocCnt = 4,
181 kGeoPoolStatePreAllocCnt = 4,
182 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000183
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000184 GrSTAllocator<kDrawPreallocCnt, Draw> fDraws;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000185 GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates;
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000186 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
187 GrSTAllocator<kClipPreallocCnt, GrClip> fClips;
bsalomon@google.com97805382012-03-13 14:32:07 +0000188
189 GrDrawTarget* fAutoFlushTarget;
190
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000191 bool fClipSet;
192
bsalomon@google.com934c5702012-03-20 21:17:58 +0000193 GrVertexBufferAllocPool& fVertexPool;
194
195 GrIndexBufferAllocPool& fIndexPool;
196
197 // these are used to attempt to concatenate drawRect calls
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000198 GrVertexLayout fLastRectVertexLayout;
199 const GrIndexBuffer* fQuadIndexBuffer;
200 int fMaxQuads;
201 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
bsalomon@google.com934c5702012-03-20 21:17:58 +0000203 // bookkeeping to attempt to concantenate drawIndexedInstances calls
204 struct {
205 int fVerticesPerInstance;
206 int fIndicesPerInstance;
207 void reset() {
208 fVerticesPerInstance = 0;
209 fIndicesPerInstance = 0;
210 }
211 } fInstancedDrawTracker;
bsalomon@google.com92669012011-09-27 19:10:05 +0000212
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000213 struct GeometryPoolState {
214 const GrVertexBuffer* fPoolVertexBuffer;
215 int fPoolStartVertex;
216 const GrIndexBuffer* fPoolIndexBuffer;
217 int fPoolStartIndex;
218 // caller may conservatively over reserve vertices / indices.
219 // we release unused space back to allocator if possible
220 // can only do this if there isn't an intervening pushGeometrySource()
221 size_t fUsedPoolVertexBytes;
222 size_t fUsedPoolIndexBytes;
223 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000224 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000226 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000227};
228
229#endif