blob: fd060143e5eccf8e3d9ef9a3d80d43c4a57d4555 [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;
bsalomon@google.com471d4712011-08-23 15:45:25 +000022class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000023
bsalomon@google.com1c13c962011-02-14 16:51:21 +000024/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000025 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
26 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
27 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
28 * references to the 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 caller's
30 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
31 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
32 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000033 */
reed@google.comac10a2d2010-12-22 21:39:39 +000034class GrInOrderDrawBuffer : public GrDrawTarget {
35public:
36
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000037 /**
38 * Creates a GrInOrderDrawBuffer
39 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000040 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000041 * @param vertexPool pool where vertices for queued draws will be saved when
42 * the vertex source is either reserved or array.
43 * @param indexPool pool where indices for queued draws will be saved when
44 * the index source is either reserved or array.
45 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000047 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000048 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000049
50 virtual ~GrInOrderDrawBuffer();
51
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000052 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000053 * Empties the draw buffer of any queued up draws. This must not be called while inside an
54 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000055 */
56 void reset();
57
58 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000059 * 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 +000060 * is destructive). This buffer must not have an active reserved vertex or index source. Any
61 * reserved geometry on the target will be finalized because it's geometry source will be pushed
62 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000063 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000064 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000065
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000066 // tracking for draws
67 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
68
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000069 // overrides from GrDrawTarget
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000070 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000071 int* indexCount) const SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000072 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000073 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +000074 bool canIgnoreRect,
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000075 GrRenderTarget* renderTarget) SK_OVERRIDE;
76
77 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000078
bsalomon@google.comeb851172013-04-15 13:51:00 +000079 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
80
bsalomon@google.com97805382012-03-13 14:32:07 +000081protected:
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +000082 virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
83
reed@google.comac10a2d2010-12-22 21:39:39 +000084private:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000085 enum Cmd {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086 kDraw_Cmd = 1,
87 kStencilPath_Cmd = 2,
88 kSetState_Cmd = 3,
89 kSetClip_Cmd = 4,
90 kClear_Cmd = 5,
bsalomon@google.com116ad842013-04-09 15:38:19 +000091 kCopySurface_Cmd = 6,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000092 kDrawPath_Cmd = 7,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000093 kDrawPaths_Cmd = 8,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000094 };
95
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000096 class DrawRecord : public DrawInfo {
97 public:
98 DrawRecord(const DrawInfo& info) : DrawInfo(info) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000099 const GrVertexBuffer* fVertexBuffer;
100 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 };
102
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000103 struct StencilPath : public ::SkNoncopyable {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000104 StencilPath();
105
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000106 SkAutoTUnref<const GrPath> fPath;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000107 SkPath::FillType fFill;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000108 };
109
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000110 struct DrawPath : public ::SkNoncopyable {
111 DrawPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000112
113 SkAutoTUnref<const GrPath> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000114 SkPath::FillType fFill;
115 GrDeviceCoordTexture fDstCopy;
116 };
117
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000118 struct DrawPaths : public ::SkNoncopyable {
119 DrawPaths();
120 ~DrawPaths();
121
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000122 int fPathCount;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000123 const GrPath** fPaths;
124 SkMatrix* fTransforms;
125 SkPath::FillType fFill;
126 SkStrokeRec::Style fStroke;
127 GrDeviceCoordTexture fDstCopy;
128 };
129
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000130 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000131 struct Clear : public ::SkNoncopyable {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000132 Clear() : fRenderTarget(NULL) {}
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000133 ~Clear() { SkSafeUnref(fRenderTarget); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000134
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000135 SkIRect fRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000136 GrColor fColor;
robertphillips@google.com56ce48a2013-10-31 21:44:25 +0000137 bool fCanIgnoreRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000138 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000139 };
140
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000141 struct CopySurface : public ::SkNoncopyable {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000142 SkAutoTUnref<GrSurface> fDst;
143 SkAutoTUnref<GrSurface> fSrc;
144 SkIRect fSrcRect;
145 SkIPoint fDstPoint;
146 };
147
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000148 // overrides from GrDrawTarget
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000149 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000150 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000151 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000152 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000153 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000154
155 virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
156 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000157 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
commit-bot@chromium.orgecc45362014-03-28 21:31:34 +0000158 virtual void onDrawPaths(int, const GrPath**, const SkMatrix*,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000159 SkPath::FillType, SkStrokeRec::Style,
160 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000161
jvanverth@google.coma6338982013-01-31 21:34:25 +0000162 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000163 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000164 void** vertices) SK_OVERRIDE;
165 virtual bool onReserveIndexSpace(int indexCount,
166 void** indices) SK_OVERRIDE;
167 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
168 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000169 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000170 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000171 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000172 int indexCount) SK_OVERRIDE;
173 virtual void releaseVertexArray() SK_OVERRIDE;
174 virtual void releaseIndexArray() SK_OVERRIDE;
175 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000176 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000177 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000178 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000179 virtual bool onCopySurface(GrSurface* dst,
180 GrSurface* src,
181 const SkIRect& srcRect,
182 const SkIPoint& dstPoint) SK_OVERRIDE;
183 virtual bool onCanCopySurface(GrSurface* dst,
184 GrSurface* src,
185 const SkIRect& srcRect,
186 const SkIPoint& dstPoint) SK_OVERRIDE;
187
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000188 bool quickInsideClip(const SkRect& devBounds);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000189
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000190 virtual void didAddGpuTraceMarker() SK_OVERRIDE {}
191 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE {}
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000192
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000193 // Attempts to concat instances from info onto the previous draw. info must represent an
194 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
195 int concatInstancedDraw(const DrawInfo& info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000196
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000197 // we lazily record state and clip changes in order to skip clips and states that have no
198 // effect.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000199 bool needsNewState() const;
200 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000201
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000202 // these functions record a command
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000203 void recordState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000204 void recordClip();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000205 DrawRecord* recordDraw(const DrawInfo&);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000206 StencilPath* recordStencilPath();
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000207 DrawPath* recordDrawPath();
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000208 DrawPaths* recordDrawPaths();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000209 Clear* recordClear();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000210 CopySurface* recordCopySurface();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000211
bsalomon@google.com116ad842013-04-09 15:38:19 +0000212 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000213 enum {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000214 kCmdPreallocCnt = 32,
bsalomonbce3d6d2014-07-02 07:54:42 -0700215 kDrawPreallocCnt = 16,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000216 kStencilPathPreallocCnt = 8,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000217 kDrawPathPreallocCnt = 8,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000218 kDrawPathsPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000219 kStatePreallocCnt = 8,
220 kClipPreallocCnt = 8,
bsalomonbce3d6d2014-07-02 07:54:42 -0700221 kClearPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000222 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000223 kCopySurfacePreallocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000224 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000225
bsalomonbce3d6d2014-07-02 07:54:42 -0700226 typedef GrTAllocator<DrawRecord> DrawAllocator;
227 typedef GrTAllocator<StencilPath> StencilPathAllocator;
228 typedef GrTAllocator<DrawPath> DrawPathAllocator;
229 typedef GrTAllocator<DrawPaths> DrawPathsAllocator;
230 typedef GrTAllocator<GrDrawState::DeferredState> StateAllocator;
231 typedef GrTAllocator<Clear> ClearAllocator;
232 typedef GrTAllocator<CopySurface> CopySurfaceAllocator;
233 typedef GrTAllocator<SkClipStack> ClipAllocator;
234 typedef GrTAllocator<SkIPoint> ClipOriginAllocator;
235
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000236 GrSTAllocator<kDrawPreallocCnt, DrawRecord> fDraws;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000237 GrSTAllocator<kStencilPathPreallocCnt, StencilPath> fStencilPaths;
238 GrSTAllocator<kDrawPathPreallocCnt, DrawPath> fDrawPath;
239 GrSTAllocator<kDrawPathsPreallocCnt, DrawPaths> fDrawPaths;
bsalomon@google.comca432082013-01-23 19:53:46 +0000240 GrSTAllocator<kStatePreallocCnt, GrDrawState::DeferredState> fStates;
241 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000242 GrSTAllocator<kCopySurfacePreallocCnt, CopySurface> fCopySurfaces;
243 GrSTAllocator<kClipPreallocCnt, SkClipStack> fClips;
244 GrSTAllocator<kClipPreallocCnt, SkIPoint> fClipOrigins;
bsalomonbce3d6d2014-07-02 07:54:42 -0700245
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000246 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
bsalomon@google.com97805382012-03-13 14:32:07 +0000247
bsalomonbce3d6d2014-07-02 07:54:42 -0700248 SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds;
249
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000250 GrDrawTarget* fDstGpu;
bsalomon@google.com97805382012-03-13 14:32:07 +0000251
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000252 bool fClipSet;
253
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000254 enum ClipProxyState {
255 kUnknown_ClipProxyState,
256 kValid_ClipProxyState,
257 kInvalid_ClipProxyState
258 };
259 ClipProxyState fClipProxyState;
260 SkRect fClipProxy;
261
bsalomon@google.com934c5702012-03-20 21:17:58 +0000262 GrVertexBufferAllocPool& fVertexPool;
263
264 GrIndexBufferAllocPool& fIndexPool;
265
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000266 struct GeometryPoolState {
267 const GrVertexBuffer* fPoolVertexBuffer;
268 int fPoolStartVertex;
269 const GrIndexBuffer* fPoolIndexBuffer;
270 int fPoolStartIndex;
271 // caller may conservatively over reserve vertices / indices.
272 // we release unused space back to allocator if possible
273 // can only do this if there isn't an intervening pushGeometrySource()
274 size_t fUsedPoolVertexBytes;
275 size_t fUsedPoolIndexBytes;
276 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000277 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000278
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000279 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000280
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000281 void addToCmdBuffer(uint8_t cmd);
282
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000283 bool fFlushing;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000284 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000285
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000286 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000287};
288
289#endif