blob: f0b5c9ef4df228b16698aaab990cc1a578df88fa [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,
84 int stageEnableMask = 0,
85 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
bsalomon@google.com471d4712011-08-23 15:45:25 +000094 virtual bool willUseHWAALines() const;
95
reed@google.comac10a2d2010-12-22 21:39:39 +000096private:
97
98 struct Draw {
bsalomon@google.comffca4002011-02-22 20:34:01 +000099 GrPrimitiveType fPrimitiveType;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 int fStartVertex;
101 int fStartIndex;
102 int fVertexCount;
103 int fIndexCount;
104 bool fStateChanged;
105 bool fClipChanged;
106 GrVertexLayout fVertexLayout;
107 const GrVertexBuffer* fVertexBuffer;
108 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000109 };
110
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000111 struct Clear {
112 int fBeforeDrawIdx;
bsalomon@google.com6aa25c32011-04-27 19:55:29 +0000113 GrIRect fRect;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000114 GrColor fColor;
115 };
116
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000117 // overrides from GrDrawTarget
118 virtual void onDrawIndexed(GrPrimitiveType primitiveType,
119 int startVertex,
120 int startIndex,
121 int vertexCount,
122 int indexCount);
123 virtual void onDrawNonIndexed(GrPrimitiveType primitiveType,
124 int startVertex,
125 int vertexCount);
126 virtual bool onReserveVertexSpace(GrVertexLayout layout,
127 int vertexCount,
128 void** vertices);
129 virtual bool onReserveIndexSpace(int indexCount, void** indices);
130 virtual void releaseReservedVertexSpace();
131 virtual void releaseReservedIndexSpace();
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000132 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000133 int vertexCount);
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000134 virtual void onSetIndexSourceToArray(const void* indexArray,
135 int indexCount);
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000136 virtual void releaseVertexArray();
137 virtual void releaseIndexArray();
138 virtual void geometrySourceWillPush();
139 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState);
140 virtual void clipWillBeSet(const GrClip& newClip);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000141
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000142 bool needsNewState() const;
143 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000144
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000145 void pushState();
146 void pushClip();
reed@google.comac10a2d2010-12-22 21:39:39 +0000147
bsalomon@google.com471d4712011-08-23 15:45:25 +0000148 const GrGpu* fGpu;
reed@google.comac10a2d2010-12-22 21:39:39 +0000149 GrTAllocator<Draw> fDraws;
reed@google.comac10a2d2010-12-22 21:39:39 +0000150 GrTAllocator<SavedDrawState> fStates;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000151 GrTAllocator<Clear> fClears;
reed@google.comac10a2d2010-12-22 21:39:39 +0000152
153 GrTAllocator<GrClip> fClips;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000154 bool fClipSet;
155
156 GrVertexLayout fLastRectVertexLayout;
157 const GrIndexBuffer* fQuadIndexBuffer;
158 int fMaxQuads;
159 int fCurrQuad;
reed@google.comac10a2d2010-12-22 21:39:39 +0000160
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000161 GrVertexBufferAllocPool& fVertexPool;
reed@google.comac10a2d2010-12-22 21:39:39 +0000162
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000163 GrIndexBufferAllocPool& fIndexPool;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000164 struct GeometryPoolState {
165 const GrVertexBuffer* fPoolVertexBuffer;
166 int fPoolStartVertex;
167 const GrIndexBuffer* fPoolIndexBuffer;
168 int fPoolStartIndex;
169 // caller may conservatively over reserve vertices / indices.
170 // we release unused space back to allocator if possible
171 // can only do this if there isn't an intervening pushGeometrySource()
172 size_t fUsedPoolVertexBytes;
173 size_t fUsedPoolIndexBytes;
174 };
bsalomon@google.com49313f62011-09-14 13:54:05 +0000175 SkTArray<GeometryPoolState> fGeoPoolStateStack;
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000176
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000177
178 enum {
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000179 kDrawPreallocCnt = 8,
180 kStatePreallocCnt = 8,
181 kClipPreallocCnt = 8,
182 kClearPreallocCnt = 4,
183 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000184 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000185
bsalomon@google.com49313f62011-09-14 13:54:05 +0000186 SkAlignedSTStorage<kDrawPreallocCnt, Draw> fDrawStorage;
187 SkAlignedSTStorage<kStatePreallocCnt, SavedDrawState> fStateStorage;
188 SkAlignedSTStorage<kClipPreallocCnt, GrClip> fClipStorage;
189 SkAlignedSTStorage<kClearPreallocCnt, Clear> fClearStorage;
190 SkAlignedSTStorage<kGeoPoolStatePreAllocCnt,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000191 GeometryPoolState> fGeoStackStorage;
bsalomon@google.coma55847b2011-04-20 15:47:04 +0000192
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000193 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000194};
195
196#endif