blob: b18112efae48a2e7e1c36891fcc9a110dd8f899e [file] [log] [blame]
reed@google.comac10a2d2010-12-22 21:39:39 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00006 */
7
reed@google.comac10a2d2010-12-22 21:39:39 +00008#ifndef GrInOrderDrawBuffer_DEFINED
9#define GrInOrderDrawBuffer_DEFINED
10
11#include "GrDrawTarget.h"
12#include "GrAllocPool.h"
13#include "GrAllocator.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000014#include "GrPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
robertphillips@google.com641f8b12012-07-31 19:15:58 +000016#include "SkClipStack.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000017#include "SkTemplates.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000018#include "SkTypes.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000019
bsalomon@google.com471d4712011-08-23 15:45:25 +000020class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000021class GrIndexBufferAllocPool;
cdaltonb85a0aa2014-07-21 15:32:44 -070022class GrPathRange;
bsalomon@google.com471d4712011-08-23 15:45:25 +000023class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com1c13c962011-02-14 16:51:21 +000025/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000026 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
27 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
28 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
29 * references to the buffers. It is the callers responsibility to ensure that the data is still
30 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
31 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
32 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
33 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000034 */
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.com6e4e6502013-02-25 20:12:45 +000041 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000042 * @param vertexPool pool where vertices for queued draws will be saved when
43 * the vertex source is either reserved or array.
44 * @param indexPool pool where indices for queued draws will be saved when
45 * the index source is either reserved or array.
46 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000048 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000050
51 virtual ~GrInOrderDrawBuffer();
52
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000053 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000054 * Empties the draw buffer of any queued up draws. This must not be called while inside an
55 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000056 */
57 void reset();
58
59 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000060 * This plays the queued up draws to its GrGpu target. It also resets this object (i.e. flushing
bsalomon@google.com55e4a202013-01-11 13:54:21 +000061 * is destructive). This buffer must not have an active reserved vertex or index source. Any
62 * reserved geometry on the target will be finalized because it's geometry source will be pushed
63 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000064 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000065 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000066
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000067 // tracking for draws
68 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
69
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000070 // overrides from GrDrawTarget
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000071 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000072 int* indexCount) const SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000074 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +000075 bool canIgnoreRect,
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000076 GrRenderTarget* renderTarget) SK_OVERRIDE;
77
78 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000079
bsalomon@google.comeb851172013-04-15 13:51:00 +000080 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
81
bsalomon@google.com97805382012-03-13 14:32:07 +000082protected:
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +000083 virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
84
reed@google.comac10a2d2010-12-22 21:39:39 +000085private:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000086 enum Cmd {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000087 kDraw_Cmd = 1,
88 kStencilPath_Cmd = 2,
89 kSetState_Cmd = 3,
90 kSetClip_Cmd = 4,
91 kClear_Cmd = 5,
bsalomon@google.com116ad842013-04-09 15:38:19 +000092 kCopySurface_Cmd = 6,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000093 kDrawPath_Cmd = 7,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000094 kDrawPaths_Cmd = 8,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000095 };
96
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000097 class DrawRecord : public DrawInfo {
98 public:
99 DrawRecord(const DrawInfo& info) : DrawInfo(info) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000100 const GrVertexBuffer* fVertexBuffer;
101 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 };
103
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000104 struct StencilPath : public ::SkNoncopyable {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000105 StencilPath();
106
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107 SkAutoTUnref<const GrPath> fPath;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000108 SkPath::FillType fFill;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000109 };
110
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000111 struct DrawPath : public ::SkNoncopyable {
112 DrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000113
114 SkAutoTUnref<const GrPath> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000115 SkPath::FillType fFill;
116 GrDeviceCoordTexture fDstCopy;
117 };
118
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000119 struct DrawPaths : public ::SkNoncopyable {
120 DrawPaths();
121 ~DrawPaths();
122
cdaltonb85a0aa2014-07-21 15:32:44 -0700123 SkAutoTUnref<const GrPathRange> fPathRange;
124 uint32_t* fIndices;
125 size_t fCount;
126 float* fTransforms;
127 PathTransformType fTransformsType;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000128 SkPath::FillType fFill;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000129 GrDeviceCoordTexture fDstCopy;
130 };
131
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000132 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000133 struct Clear : public ::SkNoncopyable {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000134 Clear() : fRenderTarget(NULL) {}
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000135 ~Clear() { SkSafeUnref(fRenderTarget); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000136
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000137 SkIRect fRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000138 GrColor fColor;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000139 bool fCanIgnoreRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000140 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000141 };
142
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000143 struct CopySurface : public ::SkNoncopyable {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000144 SkAutoTUnref<GrSurface> fDst;
145 SkAutoTUnref<GrSurface> fSrc;
146 SkIRect fSrcRect;
147 SkIPoint fDstPoint;
148 };
149
bsalomonf0480b12014-07-02 12:11:24 -0700150 struct Clip : public ::SkNoncopyable {
151 SkClipStack fStack;
152 SkIPoint fOrigin;
153 };
154
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000155 // overrides from GrDrawTarget
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000156 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000157 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000158 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000159 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000160 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000161
162 virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
163 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000164 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
cdaltonb85a0aa2014-07-21 15:32:44 -0700165 virtual void onDrawPaths(const GrPathRange*,
166 const uint32_t indices[], int count,
167 const float transforms[], PathTransformType,
168 SkPath::FillType, const GrDeviceCoordTexture*) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000169
jvanverth@google.coma6338982013-01-31 21:34:25 +0000170 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000171 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000172 void** vertices) SK_OVERRIDE;
173 virtual bool onReserveIndexSpace(int indexCount,
174 void** indices) SK_OVERRIDE;
175 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
176 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000177 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000178 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000179 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000180 int indexCount) SK_OVERRIDE;
181 virtual void releaseVertexArray() SK_OVERRIDE;
182 virtual void releaseIndexArray() SK_OVERRIDE;
183 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000184 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000185 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000186 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000187 virtual bool onCopySurface(GrSurface* dst,
188 GrSurface* src,
189 const SkIRect& srcRect,
190 const SkIPoint& dstPoint) SK_OVERRIDE;
191 virtual bool onCanCopySurface(GrSurface* dst,
192 GrSurface* src,
193 const SkIRect& srcRect,
194 const SkIPoint& dstPoint) SK_OVERRIDE;
195
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000196 bool quickInsideClip(const SkRect& devBounds);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000197
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000198 virtual void didAddGpuTraceMarker() SK_OVERRIDE {}
199 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE {}
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000200
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000201 // Attempts to concat instances from info onto the previous draw. info must represent an
202 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
203 int concatInstancedDraw(const DrawInfo& info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000204
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000205 // we lazily record state and clip changes in order to skip clips and states that have no
206 // effect.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000207 bool needsNewState() const;
208 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000209
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000210 // these functions record a command
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000211 void recordState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000212 void recordClip();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000213 DrawRecord* recordDraw(const DrawInfo&);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000214 StencilPath* recordStencilPath();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000215 DrawPath* recordDrawPath();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000216 DrawPaths* recordDrawPaths();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000217 Clear* recordClear();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000218 CopySurface* recordCopySurface();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000219
bsalomon@google.com116ad842013-04-09 15:38:19 +0000220 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000221 enum {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000222 kCmdPreallocCnt = 32,
bsalomonbce3d6d2014-07-02 07:54:42 -0700223 kDrawPreallocCnt = 16,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000224 kStencilPathPreallocCnt = 8,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000225 kDrawPathPreallocCnt = 8,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000226 kDrawPathsPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000227 kStatePreallocCnt = 8,
228 kClipPreallocCnt = 8,
bsalomonbce3d6d2014-07-02 07:54:42 -0700229 kClearPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000230 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000231 kCopySurfacePreallocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000232 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000233
bsalomonbce3d6d2014-07-02 07:54:42 -0700234 typedef GrTAllocator<DrawRecord> DrawAllocator;
235 typedef GrTAllocator<StencilPath> StencilPathAllocator;
236 typedef GrTAllocator<DrawPath> DrawPathAllocator;
237 typedef GrTAllocator<DrawPaths> DrawPathsAllocator;
bsalomona70353e2014-07-07 08:15:07 -0700238 typedef GrTAllocator<GrDrawState> StateAllocator;
bsalomonbce3d6d2014-07-02 07:54:42 -0700239 typedef GrTAllocator<Clear> ClearAllocator;
240 typedef GrTAllocator<CopySurface> CopySurfaceAllocator;
bsalomonf0480b12014-07-02 12:11:24 -0700241 typedef GrTAllocator<Clip> ClipAllocator;
bsalomonbce3d6d2014-07-02 07:54:42 -0700242
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000243 GrSTAllocator<kDrawPreallocCnt, DrawRecord> fDraws;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000244 GrSTAllocator<kStencilPathPreallocCnt, StencilPath> fStencilPaths;
245 GrSTAllocator<kDrawPathPreallocCnt, DrawPath> fDrawPath;
246 GrSTAllocator<kDrawPathsPreallocCnt, DrawPaths> fDrawPaths;
bsalomona70353e2014-07-07 08:15:07 -0700247 GrSTAllocator<kStatePreallocCnt, GrDrawState> fStates;
bsalomon@google.comca432082013-01-23 19:53:46 +0000248 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000249 GrSTAllocator<kCopySurfacePreallocCnt, CopySurface> fCopySurfaces;
bsalomonf0480b12014-07-02 12:11:24 -0700250 GrSTAllocator<kClipPreallocCnt, Clip> fClips;
bsalomonbce3d6d2014-07-02 07:54:42 -0700251
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000252 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
bsalomon@google.com97805382012-03-13 14:32:07 +0000253
bsalomonbce3d6d2014-07-02 07:54:42 -0700254 SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds;
255
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000256 GrDrawTarget* fDstGpu;
bsalomon@google.com97805382012-03-13 14:32:07 +0000257
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000258 bool fClipSet;
259
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000260 enum ClipProxyState {
261 kUnknown_ClipProxyState,
262 kValid_ClipProxyState,
263 kInvalid_ClipProxyState
264 };
265 ClipProxyState fClipProxyState;
266 SkRect fClipProxy;
267
bsalomon@google.com934c5702012-03-20 21:17:58 +0000268 GrVertexBufferAllocPool& fVertexPool;
269
270 GrIndexBufferAllocPool& fIndexPool;
271
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000272 struct GeometryPoolState {
273 const GrVertexBuffer* fPoolVertexBuffer;
274 int fPoolStartVertex;
275 const GrIndexBuffer* fPoolIndexBuffer;
276 int fPoolStartIndex;
277 // caller may conservatively over reserve vertices / indices.
278 // we release unused space back to allocator if possible
279 // can only do this if there isn't an intervening pushGeometrySource()
280 size_t fUsedPoolVertexBytes;
281 size_t fUsedPoolIndexBytes;
282 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000283 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000284
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000285 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000286
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000287 void addToCmdBuffer(uint8_t cmd);
288
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000289 bool fFlushing;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000290 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000291
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000292 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000293};
294
295#endif