blob: 72ebe4d084fe835fc7e0afd4e069ad71cc893913 [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"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000017#include "GrPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018#include "GrClip.h"
19
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000020#include "SkTemplates.h"
21
bsalomon@google.com471d4712011-08-23 15:45:25 +000022class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000023class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000024class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000025
bsalomon@google.com1c13c962011-02-14 16:51:21 +000026/**
27 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up
28 * draws for eventual playback into a GrGpu. In theory one draw buffer could
29 * playback into another. When index or vertex buffers are used as geometry
30 * sources it is the callers the draw buffer only holds references to the
31 * buffers. It is the callers responsibility to ensure that the data is still
32 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the
33 * caller's responsibility to ensure that all referenced textures, buffers,
34 * and rendertargets are associated in the GrGpu object that the buffer is
35 * played back into. The buffer requires VB and IB pools to store geometry.
36 */
37
reed@google.comac10a2d2010-12-22 21:39:39 +000038class GrInOrderDrawBuffer : public GrDrawTarget {
39public:
40
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000041 /**
42 * Creates a GrInOrderDrawBuffer
43 *
bsalomon@google.com471d4712011-08-23 15:45:25 +000044 * @param gpu the gpu object where this will be played back
45 * (possible indirectly). GrResources used with the draw
46 * buffer are created by this gpu object.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000047 * @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.com471d4712011-08-23 15:45:25 +000052 GrInOrderDrawBuffer(const GrGpu* gpu,
53 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000054 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000055
56 virtual ~GrInOrderDrawBuffer();
57
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000058 /**
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000059 * Provides the buffer with an index buffer that can be used for quad rendering.
60 * The buffer may be able to batch consecutive drawRects if this is provided.
61 * @param indexBuffer index buffer with quad indices.
62 */
63 void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer);
64
65 /**
bsalomon@google.com97805382012-03-13 14:32:07 +000066 * Empties the draw buffer of any queued up draws. This must not be called
67 * while inside an unbalanced pushGeometrySource().
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000068 */
69 void reset();
70
71 /**
bsalomon@google.com97805382012-03-13 14:32:07 +000072 * plays the queued up draws to another target. Does not empty this buffer
73 * so that it can be played back multiple times. This buffer must not have
74 * an active reserved vertex or index source. Any reserved geometry on
75 * the target will be finalized because it's geometry source will be pushed
76 * before playback and popped afterwards.
77 *
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000078 * @return false if the playback trivially drew nothing because nothing was
79 * recorded.
80 *
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000081 * @param target the target to receive the playback
82 */
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000083 bool playback(GrDrawTarget* target);
bsalomon@google.com97805382012-03-13 14:32:07 +000084
85 /**
86 * A convenience method to do a playback followed by a reset. All the
87 * constraints and side-effects or playback() and reset apply().
88 */
89 void flushTo(GrDrawTarget* target) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000090 if (fFlushing) {
91 // When creating SW-only clip masks, the GrClipMaskManager can
92 // cause a GrContext::flush (when copying the mask results back
93 // to the GPU). Without a guard this results in a recursive call
94 // to this method.
95 return;
96 }
97
98 fFlushing = true;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000099 if (this->playback(target)) {
100 this->reset();
101 }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000102 fFlushing = false;
bsalomon@google.com97805382012-03-13 14:32:07 +0000103 }
104
105 /**
106 * This function allows the draw buffer to automatically flush itself to
107 * another target. This means the buffer may internally call
108 * this->flushTo(target) when it is safe to do so.
109 *
110 * When the auto flush target is set to NULL (as it initially is) the draw
111 * buffer will never automatically flush itself.
112 */
113 void setAutoFlushTarget(GrDrawTarget* target);
114
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000115 // overrides from GrDrawTarget
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000116 virtual void drawRect(const GrRect& rect,
117 const GrMatrix* matrix = NULL,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000118 StageMask stageEnableMask = 0,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000119 const GrRect* srcRects[] = NULL,
bsalomon@google.com97805382012-03-13 14:32:07 +0000120 const GrMatrix* srcMatrices[] = NULL) SK_OVERRIDE;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000121
bsalomon@google.com934c5702012-03-20 21:17:58 +0000122 virtual void drawIndexedInstances(GrPrimitiveType type,
123 int instanceCount,
124 int verticesPerInstance,
125 int indicesPerInstance)
126 SK_OVERRIDE;
127
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000129 int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000130 int* indexCount) const SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000131
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000132 virtual void clear(const GrIRect* rect,
133 GrColor color,
134 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000135
bsalomon@google.com97805382012-03-13 14:32:07 +0000136protected:
bsalomon@google.com97805382012-03-13 14:32:07 +0000137 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
138 int vertexCount,
139 int indexCount) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140private:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000141 enum Cmd {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000142 kDraw_Cmd = 1,
143 kStencilPath_Cmd = 2,
144 kSetState_Cmd = 3,
145 kSetClip_Cmd = 4,
146 kClear_Cmd = 5,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000147 };
148
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000150 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000151 int fStartVertex;
152 int fStartIndex;
153 int fVertexCount;
154 int fIndexCount;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000155 GrVertexLayout fVertexLayout;
156 const GrVertexBuffer* fVertexBuffer;
157 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000158 };
159
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000160 struct StencilPath {
161 SkAutoTUnref<const GrPath> fPath;
162 GrPathFill fFill;
163 };
164
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000165 struct Clear {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000166 Clear() : fRenderTarget(NULL) {}
167 ~Clear() { GrSafeUnref(fRenderTarget); }
168
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000169 GrIRect fRect;
170 GrColor fColor;
171 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000172 };
173
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000174 // overrides from GrDrawTarget
175 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
176 int startVertex,
177 int startIndex,
178 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000179 int indexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000180 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
181 int startVertex,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000182 int vertexCount) SK_OVERRIDE;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000183 virtual void onStencilPath(const GrPath*, GrPathFill) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000184 virtual bool onReserveVertexSpace(GrVertexLayout layout,
185 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000186 void** vertices) SK_OVERRIDE;
187 virtual bool onReserveIndexSpace(int indexCount,
188 void** indices) SK_OVERRIDE;
189 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
190 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000191 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000192 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000193 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000194 int indexCount) SK_OVERRIDE;
195 virtual void releaseVertexArray() SK_OVERRIDE;
196 virtual void releaseIndexArray() SK_OVERRIDE;
197 virtual void geometrySourceWillPush() SK_OVERRIDE;
198 virtual void geometrySourceWillPop(
199 const GeometrySrcState& restoredState) SK_OVERRIDE;
200 virtual void clipWillBeSet(const GrClip& newClip) SK_OVERRIDE;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000201
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000202 // we lazily record state and clip changes in order to skip clips and states
203 // that have no effect.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000204 bool needsNewState() const;
205 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000206
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000207 // these functions record a command
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000208 void recordState();
209 void recordDefaultState();
210 void recordClip();
211 void recordDefaultClip();
212 Draw* recordDraw();
213 StencilPath* recordStencilPath();
214 Clear* recordClear();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000215
216 // call this to invalidate the tracking data that is used to concatenate
217 // multiple draws into a single draw.
218 void resetDrawTracking();
219
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000220 enum {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000221 kCmdPreallocCnt = 32,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000222 kDrawPreallocCnt = 8,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000223 kStencilPathPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000224 kStatePreallocCnt = 8,
225 kClipPreallocCnt = 8,
226 kClearPreallocCnt = 4,
227 kGeoPoolStatePreAllocCnt = 4,
228 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000229
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000230 SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds;
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000231 GrSTAllocator<kDrawPreallocCnt, Draw> fDraws;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000232 GrSTAllocator<kStatePreallocCnt, StencilPath> fStencilPaths;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000233 GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates;
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000234 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
235 GrSTAllocator<kClipPreallocCnt, GrClip> fClips;
bsalomon@google.com97805382012-03-13 14:32:07 +0000236
237 GrDrawTarget* fAutoFlushTarget;
238
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000239 bool fClipSet;
240
bsalomon@google.com934c5702012-03-20 21:17:58 +0000241 GrVertexBufferAllocPool& fVertexPool;
242
243 GrIndexBufferAllocPool& fIndexPool;
244
245 // these are used to attempt to concatenate drawRect calls
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000246 GrVertexLayout fLastRectVertexLayout;
247 const GrIndexBuffer* fQuadIndexBuffer;
248 int fMaxQuads;
249 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000250
bsalomon@google.com934c5702012-03-20 21:17:58 +0000251 // bookkeeping to attempt to concantenate drawIndexedInstances calls
252 struct {
253 int fVerticesPerInstance;
254 int fIndicesPerInstance;
255 void reset() {
256 fVerticesPerInstance = 0;
257 fIndicesPerInstance = 0;
258 }
259 } fInstancedDrawTracker;
bsalomon@google.com92669012011-09-27 19:10:05 +0000260
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000261 struct GeometryPoolState {
262 const GrVertexBuffer* fPoolVertexBuffer;
263 int fPoolStartVertex;
264 const GrIndexBuffer* fPoolIndexBuffer;
265 int fPoolStartIndex;
266 // caller may conservatively over reserve vertices / indices.
267 // we release unused space back to allocator if possible
268 // can only do this if there isn't an intervening pushGeometrySource()
269 size_t fUsedPoolVertexBytes;
270 size_t fUsedPoolIndexBytes;
271 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000272 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000273
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000274 bool fFlushing;
275
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000276 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000277};
278
279#endif