blob: ed7d97d06f56c2158d09a604098d4ffcec98833d [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,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000146 int indexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000147 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
148 int startVertex,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000149 int vertexCount) SK_OVERRIDE;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000150 virtual void onStencilPath(const GrPath&, GrPathFill) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000151 virtual bool onReserveVertexSpace(GrVertexLayout layout,
152 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000153 void** vertices) SK_OVERRIDE;
154 virtual bool onReserveIndexSpace(int indexCount,
155 void** indices) SK_OVERRIDE;
156 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
157 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000158 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000159 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000160 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000161 int indexCount) SK_OVERRIDE;
162 virtual void releaseVertexArray() SK_OVERRIDE;
163 virtual void releaseIndexArray() SK_OVERRIDE;
164 virtual void geometrySourceWillPush() SK_OVERRIDE;
165 virtual void geometrySourceWillPop(
166 const GeometrySrcState& restoredState) SK_OVERRIDE;
167 virtual void clipWillBeSet(const GrClip& newClip) SK_OVERRIDE;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000169 bool needsNewState() const;
170 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000171
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000172 void pushState();
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000173 void storeClip();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000174
175 // call this to invalidate the tracking data that is used to concatenate
176 // multiple draws into a single draw.
177 void resetDrawTracking();
178
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000179 enum {
180 kDrawPreallocCnt = 8,
181 kStatePreallocCnt = 8,
182 kClipPreallocCnt = 8,
183 kClearPreallocCnt = 4,
184 kGeoPoolStatePreAllocCnt = 4,
185 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000186
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000187 GrSTAllocator<kDrawPreallocCnt, Draw> fDraws;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000188 GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates;
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000189 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
190 GrSTAllocator<kClipPreallocCnt, GrClip> fClips;
bsalomon@google.com97805382012-03-13 14:32:07 +0000191
192 GrDrawTarget* fAutoFlushTarget;
193
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000194 bool fClipSet;
195
bsalomon@google.com934c5702012-03-20 21:17:58 +0000196 GrVertexBufferAllocPool& fVertexPool;
197
198 GrIndexBufferAllocPool& fIndexPool;
199
200 // these are used to attempt to concatenate drawRect calls
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000201 GrVertexLayout fLastRectVertexLayout;
202 const GrIndexBuffer* fQuadIndexBuffer;
203 int fMaxQuads;
204 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000205
bsalomon@google.com934c5702012-03-20 21:17:58 +0000206 // bookkeeping to attempt to concantenate drawIndexedInstances calls
207 struct {
208 int fVerticesPerInstance;
209 int fIndicesPerInstance;
210 void reset() {
211 fVerticesPerInstance = 0;
212 fIndicesPerInstance = 0;
213 }
214 } fInstancedDrawTracker;
bsalomon@google.com92669012011-09-27 19:10:05 +0000215
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000216 struct GeometryPoolState {
217 const GrVertexBuffer* fPoolVertexBuffer;
218 int fPoolStartVertex;
219 const GrIndexBuffer* fPoolIndexBuffer;
220 int fPoolStartIndex;
221 // caller may conservatively over reserve vertices / indices.
222 // we release unused space back to allocator if possible
223 // can only do this if there isn't an intervening pushGeometrySource()
224 size_t fUsedPoolVertexBytes;
225 size_t fUsedPoolIndexBytes;
226 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000227 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000228
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000229 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000230};
231
232#endif