blob: 421a4a9ab9dc8a371fefcd559db6cf83e38cba46 [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"
bsalomonb3e3a952014-09-19 11:10:40 -070014#include "GrIndexBuffer.h"
15#include "GrRenderTarget.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000016#include "GrPath.h"
bsalomonb3e3a952014-09-19 11:10:40 -070017#include "GrPathRange.h"
18#include "GrSurface.h"
cdalton6819df32014-10-15 13:43:48 -070019#include "GrTRecorder.h"
bsalomonb3e3a952014-09-19 11:10:40 -070020#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
robertphillips@google.com641f8b12012-07-31 19:15:58 +000022#include "SkClipStack.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000023#include "SkTemplates.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000024#include "SkTypes.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000025
bsalomon@google.com471d4712011-08-23 15:45:25 +000026class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000028class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000029
bsalomon@google.com1c13c962011-02-14 16:51:21 +000030/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000031 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
32 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
33 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
34 * references to the buffers. It is the callers responsibility to ensure that the data is still
35 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
36 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
37 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
38 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000039 */
reed@google.comac10a2d2010-12-22 21:39:39 +000040class GrInOrderDrawBuffer : public GrDrawTarget {
41public:
42
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000043 /**
44 * Creates a GrInOrderDrawBuffer
45 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000046 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000047 * @param vertexPool pool where vertices for queued draws will be saved when
48 * the vertex source is either reserved or array.
49 * @param indexPool pool where indices for queued draws will be saved when
50 * the index source is either reserved or array.
51 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000052 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000053 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000054 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000055
56 virtual ~GrInOrderDrawBuffer();
57
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000058 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000059 * Empties the draw buffer of any queued up draws. This must not be called while inside an
60 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000061 */
62 void reset();
63
64 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000065 * 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 +000066 * is destructive). This buffer must not have an active reserved vertex or index source. Any
67 * reserved geometry on the target will be finalized because it's geometry source will be pushed
68 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000069 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000070 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000071
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000072 // tracking for draws
73 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
74
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000075 // overrides from GrDrawTarget
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000076 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000077 int* indexCount) const SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000078 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000079 GrColor color,
robertphillips@google.com56ce48a2013-10-31 21:44:25 +000080 bool canIgnoreRect,
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000081 GrRenderTarget* renderTarget) SK_OVERRIDE;
82
83 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000084
bsalomonf2703d82014-10-28 14:33:06 -070085 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000086
bsalomon@google.com97805382012-03-13 14:32:07 +000087protected:
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +000088 virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
89
reed@google.comac10a2d2010-12-22 21:39:39 +000090private:
cdalton6819df32014-10-15 13:43:48 -070091 enum {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000092 kDraw_Cmd = 1,
93 kStencilPath_Cmd = 2,
94 kSetState_Cmd = 3,
95 kSetClip_Cmd = 4,
96 kClear_Cmd = 5,
bsalomon@google.com116ad842013-04-09 15:38:19 +000097 kCopySurface_Cmd = 6,
commit-bot@chromium.org32184d82013-10-09 15:14:18 +000098 kDrawPath_Cmd = 7,
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +000099 kDrawPaths_Cmd = 8,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000100 };
101
cdalton6819df32014-10-15 13:43:48 -0700102 struct Cmd : ::SkNoncopyable {
103 Cmd(uint8_t type) : fType(type) {}
104 virtual ~Cmd() {}
105
106 virtual void execute(GrDrawTarget*) = 0;
107
108 uint8_t fType;
109 };
110
111 struct Draw : public Cmd {
bsalomonb3e3a952014-09-19 11:10:40 -0700112 Draw(const DrawInfo& info, const GrVertexBuffer* vb, const GrIndexBuffer* ib)
cdalton6819df32014-10-15 13:43:48 -0700113 : Cmd(kDraw_Cmd)
114 , fInfo(info)
bsalomon45725db2014-09-19 11:48:02 -0700115 , fVertexBuffer(vb)
116 , fIndexBuffer(ib) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700117
118 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
119 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); }
120
cdalton6819df32014-10-15 13:43:48 -0700121 virtual void execute(GrDrawTarget*);
122
123 DrawInfo fInfo;
124
bsalomonb3e3a952014-09-19 11:10:40 -0700125 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700126 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer;
127 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 };
129
cdalton6819df32014-10-15 13:43:48 -0700130 struct StencilPath : public Cmd {
131 StencilPath(const GrPath* path) : Cmd(kStencilPath_Cmd), fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000132
bsalomonb3e3a952014-09-19 11:10:40 -0700133 const GrPath* path() const { return fPath.get(); }
134
cdalton6819df32014-10-15 13:43:48 -0700135 virtual void execute(GrDrawTarget*);
136
bsalomonb3e3a952014-09-19 11:10:40 -0700137 SkPath::FillType fFill;
138
139 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700140 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000141 };
142
cdalton6819df32014-10-15 13:43:48 -0700143 struct DrawPath : public Cmd {
144 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000145
bsalomonb3e3a952014-09-19 11:10:40 -0700146 const GrPath* path() const { return fPath.get(); }
147
cdalton6819df32014-10-15 13:43:48 -0700148 virtual void execute(GrDrawTarget*);
149
bsalomonb3e3a952014-09-19 11:10:40 -0700150 SkPath::FillType fFill;
151 GrDeviceCoordTexture fDstCopy;
152
153 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700154 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000155 };
156
cdalton6819df32014-10-15 13:43:48 -0700157 struct DrawPaths : public Cmd {
158 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700159
160 const GrPathRange* pathRange() const { return fPathRange.get(); }
cdalton6819df32014-10-15 13:43:48 -0700161 uint32_t* indices() { return reinterpret_cast<uint32_t*>(CmdBuffer::GetDataForItem(this)); }
162 float* transforms() { return reinterpret_cast<float*>(&this->indices()[fCount]); }
bsalomonb3e3a952014-09-19 11:10:40 -0700163
cdalton6819df32014-10-15 13:43:48 -0700164 virtual void execute(GrDrawTarget*);
165
bsalomonb3e3a952014-09-19 11:10:40 -0700166 size_t fCount;
bsalomonb3e3a952014-09-19 11:10:40 -0700167 PathTransformType fTransformsType;
168 SkPath::FillType fFill;
169 GrDeviceCoordTexture fDstCopy;
170
171 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700172 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000173 };
174
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000175 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700176 struct Clear : public Cmd {
177 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
178
bsalomonb3e3a952014-09-19 11:10:40 -0700179 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000180
cdalton6819df32014-10-15 13:43:48 -0700181 virtual void execute(GrDrawTarget*);
182
bsalomonb3e3a952014-09-19 11:10:40 -0700183 SkIRect fRect;
184 GrColor fColor;
185 bool fCanIgnoreRect;
186
187 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700188 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000189 };
190
cdalton6819df32014-10-15 13:43:48 -0700191 struct CopySurface : public Cmd {
192 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700193
194 GrSurface* dst() const { return fDst.get(); }
195 GrSurface* src() const { return fSrc.get(); }
196
cdalton6819df32014-10-15 13:43:48 -0700197 virtual void execute(GrDrawTarget*);
198
bsalomonb3e3a952014-09-19 11:10:40 -0700199 SkIPoint fDstPoint;
200 SkIRect fSrcRect;
201
202 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700203 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
204 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000205 };
206
cdalton6819df32014-10-15 13:43:48 -0700207 struct SetState : public Cmd {
208 SetState(const GrDrawState& state) : Cmd(kSetState_Cmd), fState(state) {}
209
210 virtual void execute(GrDrawTarget*);
211
212 GrDrawState fState;
bsalomonf0480b12014-07-02 12:11:24 -0700213 };
214
cdalton6819df32014-10-15 13:43:48 -0700215 struct SetClip : public Cmd {
216 SetClip(const GrClipData* clipData)
217 : Cmd(kSetClip_Cmd),
218 fStackStorage(*clipData->fClipStack) {
219 fClipData.fClipStack = &fStackStorage;
220 fClipData.fOrigin = clipData->fOrigin;
221 }
222
223 virtual void execute(GrDrawTarget*);
224
225 GrClipData fClipData;
226
227 private:
228 SkClipStack fStackStorage;
229 };
230
231 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
232 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
233
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000234 // overrides from GrDrawTarget
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000235 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000236 virtual void onDrawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000237 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000238 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000239
240 virtual void onStencilPath(const GrPath*, SkPath::FillType) SK_OVERRIDE;
241 virtual void onDrawPath(const GrPath*, SkPath::FillType,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000242 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
cdaltonb85a0aa2014-07-21 15:32:44 -0700243 virtual void onDrawPaths(const GrPathRange*,
244 const uint32_t indices[], int count,
245 const float transforms[], PathTransformType,
246 SkPath::FillType, const GrDeviceCoordTexture*) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000247
jvanverth@google.coma6338982013-01-31 21:34:25 +0000248 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000249 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000250 void** vertices) SK_OVERRIDE;
251 virtual bool onReserveIndexSpace(int indexCount,
252 void** indices) SK_OVERRIDE;
253 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
254 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000255 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000256 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000257 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000258 int indexCount) SK_OVERRIDE;
259 virtual void releaseVertexArray() SK_OVERRIDE;
260 virtual void releaseIndexArray() SK_OVERRIDE;
261 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000262 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000263 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000264 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000265 virtual bool onCopySurface(GrSurface* dst,
266 GrSurface* src,
267 const SkIRect& srcRect,
268 const SkIPoint& dstPoint) SK_OVERRIDE;
269 virtual bool onCanCopySurface(GrSurface* dst,
270 GrSurface* src,
271 const SkIRect& srcRect,
272 const SkIPoint& dstPoint) SK_OVERRIDE;
273
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000274 bool quickInsideClip(const SkRect& devBounds);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000275
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000276 virtual void didAddGpuTraceMarker() SK_OVERRIDE {}
277 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE {}
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000278
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000279 // Attempts to concat instances from info onto the previous draw. info must represent an
280 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
281 int concatInstancedDraw(const DrawInfo& info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000282
bsalomon838f62d2014-08-05 07:15:57 -0700283 // Determines whether the current draw operation requieres a new drawstate and if so records it.
284 void recordStateIfNecessary();
285 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700286 void recordClipIfNecessary();
287 // Records any trace markers for a command after adding it to the buffer.
288 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700289
290 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000291
bsalomon@google.com116ad842013-04-09 15:38:19 +0000292 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000293 enum {
cdalton6819df32014-10-15 13:43:48 -0700294 kCmdBufferInitialSizeInBytes = 64 * 1024,
295 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000296 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000297
cdalton6819df32014-10-15 13:43:48 -0700298 CmdBuffer fCmdBuffer;
299 GrDrawState* fLastState;
300 GrClipData* fLastClip;
bsalomonbce3d6d2014-07-02 07:54:42 -0700301
cdalton6819df32014-10-15 13:43:48 -0700302 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
303 GrDrawTarget* fDstGpu;
304 bool fClipSet;
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000305
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000306 enum ClipProxyState {
307 kUnknown_ClipProxyState,
308 kValid_ClipProxyState,
309 kInvalid_ClipProxyState
310 };
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000311
bsalomonb3e3a952014-09-19 11:10:40 -0700312 ClipProxyState fClipProxyState;
313 SkRect fClipProxy;
314 GrVertexBufferAllocPool& fVertexPool;
315 GrIndexBufferAllocPool& fIndexPool;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000316
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000317 struct GeometryPoolState {
bsalomonb3e3a952014-09-19 11:10:40 -0700318 const GrVertexBuffer* fPoolVertexBuffer;
319 int fPoolStartVertex;
320 const GrIndexBuffer* fPoolIndexBuffer;
321 int fPoolStartIndex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000322 // caller may conservatively over reserve vertices / indices.
323 // we release unused space back to allocator if possible
324 // can only do this if there isn't an intervening pushGeometrySource()
bsalomonb3e3a952014-09-19 11:10:40 -0700325 size_t fUsedPoolVertexBytes;
326 size_t fUsedPoolIndexBytes;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000327 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000328
bsalomonb3e3a952014-09-19 11:10:40 -0700329 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000330
bsalomonb3e3a952014-09-19 11:10:40 -0700331 GeoPoolStateStack fGeoPoolStateStack;
332 bool fFlushing;
333 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000334
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000335 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000336};
337
338#endif