blob: 9b5561a1ffb41ab48ee6b879dc4239fee66a819e [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) {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000084 if (fFlushing) {
85 // When creating SW-only clip masks, the GrClipMaskManager can
86 // cause a GrContext::flush (when copying the mask results back
87 // to the GPU). Without a guard this results in a recursive call
88 // to this method.
89 return;
90 }
91
92 fFlushing = true;
bsalomon@google.com97805382012-03-13 14:32:07 +000093 this->playback(target);
94 this->reset();
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000095 fFlushing = false;
bsalomon@google.com97805382012-03-13 14:32:07 +000096 }
97
98 /**
99 * This function allows the draw buffer to automatically flush itself to
100 * another target. This means the buffer may internally call
101 * this->flushTo(target) when it is safe to do so.
102 *
103 * When the auto flush target is set to NULL (as it initially is) the draw
104 * buffer will never automatically flush itself.
105 */
106 void setAutoFlushTarget(GrDrawTarget* target);
107
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000108 // overrides from GrDrawTarget
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000109 virtual void drawRect(const GrRect& rect,
110 const GrMatrix* matrix = NULL,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000111 StageMask stageEnableMask = 0,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000112 const GrRect* srcRects[] = NULL,
bsalomon@google.com97805382012-03-13 14:32:07 +0000113 const GrMatrix* srcMatrices[] = NULL) SK_OVERRIDE;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000114
bsalomon@google.com934c5702012-03-20 21:17:58 +0000115 virtual void drawIndexedInstances(GrPrimitiveType type,
116 int instanceCount,
117 int verticesPerInstance,
118 int indicesPerInstance)
119 SK_OVERRIDE;
120
reed@google.comac10a2d2010-12-22 21:39:39 +0000121 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000122 int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +0000123 int* indexCount) const SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000124
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000125 virtual void clear(const GrIRect* rect,
126 GrColor color,
127 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000128
bsalomon@google.com97805382012-03-13 14:32:07 +0000129protected:
bsalomon@google.com97805382012-03-13 14:32:07 +0000130 virtual void willReserveVertexAndIndexSpace(GrVertexLayout vertexLayout,
131 int vertexCount,
132 int indexCount) SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +0000133private:
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +0000135 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000136 int fStartVertex;
137 int fStartIndex;
138 int fVertexCount;
139 int fIndexCount;
140 bool fStateChanged;
141 bool fClipChanged;
142 GrVertexLayout fVertexLayout;
143 const GrVertexBuffer* fVertexBuffer;
144 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000145 };
146
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000147 struct Clear {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000148 Clear() : fRenderTarget(NULL) {}
149 ~Clear() { GrSafeUnref(fRenderTarget); }
150
151 int fBeforeDrawIdx;
152 GrIRect fRect;
153 GrColor fColor;
154 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000155 };
156
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000157 // overrides from GrDrawTarget
158 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
159 int startVertex,
160 int startIndex,
161 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000162 int indexCount) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000163 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
164 int startVertex,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000165 int vertexCount) SK_OVERRIDE;
bsalomon@google.com64aef2b2012-06-11 15:36:13 +0000166 virtual void onStencilPath(const GrPath&, GrPathFill) SK_OVERRIDE;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000167 virtual bool onReserveVertexSpace(GrVertexLayout layout,
168 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000169 void** vertices) SK_OVERRIDE;
170 virtual bool onReserveIndexSpace(int indexCount,
171 void** indices) SK_OVERRIDE;
172 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
173 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000174 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000175 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000176 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000177 int indexCount) SK_OVERRIDE;
178 virtual void releaseVertexArray() SK_OVERRIDE;
179 virtual void releaseIndexArray() SK_OVERRIDE;
180 virtual void geometrySourceWillPush() SK_OVERRIDE;
181 virtual void geometrySourceWillPop(
182 const GeometrySrcState& restoredState) SK_OVERRIDE;
183 virtual void clipWillBeSet(const GrClip& newClip) SK_OVERRIDE;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000184
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000185 bool needsNewState() const;
186 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000187
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000188 void pushState();
robertphillips@google.com49d9fd52012-05-23 11:44:08 +0000189 void storeClip();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000190
191 // call this to invalidate the tracking data that is used to concatenate
192 // multiple draws into a single draw.
193 void resetDrawTracking();
194
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000195 enum {
196 kDrawPreallocCnt = 8,
197 kStatePreallocCnt = 8,
198 kClipPreallocCnt = 8,
199 kClearPreallocCnt = 4,
200 kGeoPoolStatePreAllocCnt = 4,
201 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000202
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000203 GrSTAllocator<kDrawPreallocCnt, Draw> fDraws;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000204 GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates;
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000205 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
206 GrSTAllocator<kClipPreallocCnt, GrClip> fClips;
bsalomon@google.com97805382012-03-13 14:32:07 +0000207
208 GrDrawTarget* fAutoFlushTarget;
209
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000210 bool fClipSet;
211
bsalomon@google.com934c5702012-03-20 21:17:58 +0000212 GrVertexBufferAllocPool& fVertexPool;
213
214 GrIndexBufferAllocPool& fIndexPool;
215
216 // these are used to attempt to concatenate drawRect calls
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000217 GrVertexLayout fLastRectVertexLayout;
218 const GrIndexBuffer* fQuadIndexBuffer;
219 int fMaxQuads;
220 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000221
bsalomon@google.com934c5702012-03-20 21:17:58 +0000222 // bookkeeping to attempt to concantenate drawIndexedInstances calls
223 struct {
224 int fVerticesPerInstance;
225 int fIndicesPerInstance;
226 void reset() {
227 fVerticesPerInstance = 0;
228 fIndicesPerInstance = 0;
229 }
230 } fInstancedDrawTracker;
bsalomon@google.com92669012011-09-27 19:10:05 +0000231
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000232 struct GeometryPoolState {
233 const GrVertexBuffer* fPoolVertexBuffer;
234 int fPoolStartVertex;
235 const GrIndexBuffer* fPoolIndexBuffer;
236 int fPoolStartIndex;
237 // caller may conservatively over reserve vertices / indices.
238 // we release unused space back to allocator if possible
239 // can only do this if there isn't an intervening pushGeometrySource()
240 size_t fUsedPoolVertexBytes;
241 size_t fUsedPoolIndexBytes;
242 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000243 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000244
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000245 bool fFlushing;
246
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000247 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000248};
249
250#endif