blob: f935be1d69e54914859d305eb7dfb96d874e6fe2 [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 /**
56 * Copies the draw state and clip from target to this draw buffer.
57 *
58 * @param target the target whose clip and state should be copied.
59 */
reed@google.comac10a2d2010-12-22 21:39:39 +000060 void initializeDrawStateAndClip(const GrDrawTarget& target);
61
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000062 /**
63 * Provides the buffer with an index buffer that can be used for quad rendering.
64 * The buffer may be able to batch consecutive drawRects if this is provided.
65 * @param indexBuffer index buffer with quad indices.
66 */
67 void setQuadIndexBuffer(const GrIndexBuffer* indexBuffer);
68
69 /**
70 * Empties the draw buffer of any queued up draws.
71 */
72 void reset();
73
74 /**
75 * plays the queued up draws to another target. Does not empty this buffer so
76 * that it can be played back multiple times.
77 * @param target the target to receive the playback
78 */
79 void playback(GrDrawTarget* target);
80
81 // overrides from GrDrawTarget
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000082 virtual void drawRect(const GrRect& rect,
83 const GrMatrix* matrix = NULL,
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +000084 StageMask stageEnableMask = 0,
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000085 const GrRect* srcRects[] = NULL,
86 const GrMatrix* srcMatrices[] = NULL);
87
reed@google.comac10a2d2010-12-22 21:39:39 +000088 virtual bool geometryHints(GrVertexLayout vertexLayout,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000089 int* vertexCount,
90 int* indexCount) const;
reed@google.comac10a2d2010-12-22 21:39:39 +000091
bsalomon@google.com6aa25c32011-04-27 19:55:29 +000092 virtual void clear(const GrIRect* rect, GrColor color);
bsalomon@google.com0b335c12011-04-25 19:17:44 +000093
reed@google.comac10a2d2010-12-22 21:39:39 +000094private:
95
96 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +000097 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000098 int fStartVertex;
99 int fStartIndex;
100 int fVertexCount;
101 int fIndexCount;
102 bool fStateChanged;
103 bool fClipChanged;
104 GrVertexLayout fVertexLayout;
105 const GrVertexBuffer* fVertexBuffer;
106 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 };
108
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000109 struct Clear {
110 int fBeforeDrawIdx;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000111 GrIRect fRect;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000112 GrColor fColor;
113 };
114
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000115 // overrides from GrDrawTarget
116 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
117 int startVertex,
118 int startIndex,
119 int vertexCount,
120 int indexCount);
121 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
122 int startVertex,
123 int vertexCount);
124 virtual bool onReserveVertexSpace(GrVertexLayout layout,
125 int vertexCount,
126 void** vertices);
127 virtual bool onReserveIndexSpace(int indexCount, void** indices);
128 virtual void releaseReservedVertexSpace();
129 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000130 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000131 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000132 virtual void onSetIndexSourceToArray(const void* indexArray,
133 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000134 virtual void releaseVertexArray();
135 virtual void releaseIndexArray();
136 virtual void geometrySourceWillPush();
137 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
138 virtual void clipWillBeSet(const GrClip& newClip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000139
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000140 bool needsNewState() const;
141 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000142
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000143 void pushState();
144 void pushClip();
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000145
146 enum {
147 kDrawPreallocCnt = 8,
148 kStatePreallocCnt = 8,
149 kClipPreallocCnt = 8,
150 kClearPreallocCnt = 4,
151 kGeoPoolStatePreAllocCnt = 4,
152 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000153
bsalomon@google.com471d4712011-08-23 15:45:25 +0000154 const GrGpu* fGpu;
reed@google.comac10a2d2010-12-22 21:39:39 +0000155
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000156 GrSTAllocator<kDrawPreallocCnt, Draw> fDraws;
157 GrSTAllocator<kStatePreallocCnt, SavedDrawState> fStates;
158 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
159 GrSTAllocator<kClipPreallocCnt, GrClip> fClips;
160
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000161 bool fClipSet;
162
163 GrVertexLayout fLastRectVertexLayout;
164 const GrIndexBuffer* fQuadIndexBuffer;
165 int fMaxQuads;
166 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000167
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000168 GrVertexBufferAllocPool& fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000169
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000170 GrIndexBufferAllocPool& fIndexPool;
bsalomon@google.com92669012011-09-27 19:10:05 +0000171
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000172 struct GeometryPoolState {
173 const GrVertexBuffer* fPoolVertexBuffer;
174 int fPoolStartVertex;
175 const GrIndexBuffer* fPoolIndexBuffer;
176 int fPoolStartIndex;
177 // caller may conservatively over reserve vertices / indices.
178 // we release unused space back to allocator if possible
179 // can only do this if there isn't an intervening pushGeometrySource()
180 size_t fUsedPoolVertexBytes;
181 size_t fUsedPoolIndexBytes;
182 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000183 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +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