blob: e86a5288d4f80ad4e2a633e1b26be190c46f6892 [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 */
joshualitt6db519c2014-10-29 08:48:18 -070040class GrInOrderDrawBuffer : public GrClipTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000041public:
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;
joshualitt6db519c2014-10-29 08:48:18 -070078
joshualitta7024152014-11-03 14:16:35 -080079 virtual bool copySurface(GrSurface* dst,
80 GrSurface* src,
81 const SkIRect& srcRect,
82 const SkIPoint& dstPoint) SK_OVERRIDE;
83
84 virtual bool canCopySurface(GrSurface* dst,
85 GrSurface* src,
86 const SkIRect& srcRect,
87 const SkIPoint& dstPoint) SK_OVERRIDE;
88
joshualitt6db519c2014-10-29 08:48:18 -070089 virtual void clearStencilClip(const SkIRect& rect,
90 bool insideClip,
91 GrRenderTarget* renderTarget) SK_OVERRIDE;
92
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000093 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000094
bsalomonf2703d82014-10-28 14:33:06 -070095 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000096
reed@google.comac10a2d2010-12-22 21:39:39 +000097private:
joshualitt2c93efe2014-11-06 12:57:13 -080098 typedef GrClipMaskManager::ScissorState ScissorState;
cdalton6819df32014-10-15 13:43:48 -070099 enum {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000100 kDraw_Cmd = 1,
101 kStencilPath_Cmd = 2,
102 kSetState_Cmd = 3,
joshualitt2c93efe2014-11-06 12:57:13 -0800103 kClear_Cmd = 4,
104 kCopySurface_Cmd = 5,
105 kDrawPath_Cmd = 6,
106 kDrawPaths_Cmd = 7,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000107 };
108
cdalton6819df32014-10-15 13:43:48 -0700109 struct Cmd : ::SkNoncopyable {
110 Cmd(uint8_t type) : fType(type) {}
111 virtual ~Cmd() {}
112
joshualitt3322fa42014-11-07 08:48:51 -0800113 virtual void execute(GrGpu*) = 0;
cdalton6819df32014-10-15 13:43:48 -0700114
115 uint8_t fType;
116 };
117
118 struct Draw : public Cmd {
joshualitt2c93efe2014-11-06 12:57:13 -0800119 Draw(const DrawInfo& info,
120 const ScissorState& scissorState,
121 const GrVertexBuffer* vb,
122 const GrIndexBuffer* ib)
cdalton6819df32014-10-15 13:43:48 -0700123 : Cmd(kDraw_Cmd)
124 , fInfo(info)
joshualitt2c93efe2014-11-06 12:57:13 -0800125 , fScissorState(scissorState)
bsalomon45725db2014-09-19 11:48:02 -0700126 , fVertexBuffer(vb)
127 , fIndexBuffer(ib) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700128
129 const GrVertexBuffer* vertexBuffer() const { return fVertexBuffer.get(); }
130 const GrIndexBuffer* indexBuffer() const { return fIndexBuffer.get(); }
131
joshualitt3322fa42014-11-07 08:48:51 -0800132 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700133
joshualitt2c93efe2014-11-06 12:57:13 -0800134 DrawInfo fInfo;
135 ScissorState fScissorState;
cdalton6819df32014-10-15 13:43:48 -0700136
bsalomonb3e3a952014-09-19 11:10:40 -0700137 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700138 GrPendingIOResource<const GrVertexBuffer, kRead_GrIOType> fVertexBuffer;
139 GrPendingIOResource<const GrIndexBuffer, kRead_GrIOType> fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +0000140 };
141
cdalton6819df32014-10-15 13:43:48 -0700142 struct StencilPath : public Cmd {
143 StencilPath(const GrPath* path) : Cmd(kStencilPath_Cmd), fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000144
bsalomonb3e3a952014-09-19 11:10:40 -0700145 const GrPath* path() const { return fPath.get(); }
146
joshualitt3322fa42014-11-07 08:48:51 -0800147 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700148
joshualitt2c93efe2014-11-06 12:57:13 -0800149 ScissorState fScissorState;
150 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700151
152 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700153 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000154 };
155
cdalton6819df32014-10-15 13:43:48 -0700156 struct DrawPath : public Cmd {
157 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000158
bsalomonb3e3a952014-09-19 11:10:40 -0700159 const GrPath* path() const { return fPath.get(); }
160
joshualitt3322fa42014-11-07 08:48:51 -0800161 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700162
joshualitt2c93efe2014-11-06 12:57:13 -0800163 GrDeviceCoordTexture fDstCopy;
164 ScissorState fScissorState;
165 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700166
167 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700168 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000169 };
170
cdalton6819df32014-10-15 13:43:48 -0700171 struct DrawPaths : public Cmd {
172 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700173
174 const GrPathRange* pathRange() const { return fPathRange.get(); }
cdalton6819df32014-10-15 13:43:48 -0700175 uint32_t* indices() { return reinterpret_cast<uint32_t*>(CmdBuffer::GetDataForItem(this)); }
176 float* transforms() { return reinterpret_cast<float*>(&this->indices()[fCount]); }
bsalomonb3e3a952014-09-19 11:10:40 -0700177
joshualitt3322fa42014-11-07 08:48:51 -0800178 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700179
joshualitt2c93efe2014-11-06 12:57:13 -0800180 size_t fCount;
181 PathTransformType fTransformsType;
182 GrDeviceCoordTexture fDstCopy;
183 ScissorState fScissorState;
184 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700185
186 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700187 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000188 };
189
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000190 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700191 struct Clear : public Cmd {
192 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
193
bsalomonb3e3a952014-09-19 11:10:40 -0700194 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000195
joshualitt3322fa42014-11-07 08:48:51 -0800196 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700197
bsalomonb3e3a952014-09-19 11:10:40 -0700198 SkIRect fRect;
199 GrColor fColor;
200 bool fCanIgnoreRect;
201
202 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700203 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000204 };
205
joshualitt6db519c2014-10-29 08:48:18 -0700206 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
207 struct ClearStencilClip : public Cmd {
208 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
209
210 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
211
joshualitt3322fa42014-11-07 08:48:51 -0800212 virtual void execute(GrGpu*);
joshualitt6db519c2014-10-29 08:48:18 -0700213
214 SkIRect fRect;
215 bool fInsideClip;
216
217 private:
218 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
219 };
220
cdalton6819df32014-10-15 13:43:48 -0700221 struct CopySurface : public Cmd {
222 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700223
224 GrSurface* dst() const { return fDst.get(); }
225 GrSurface* src() const { return fSrc.get(); }
226
joshualitt3322fa42014-11-07 08:48:51 -0800227 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700228
bsalomonb3e3a952014-09-19 11:10:40 -0700229 SkIPoint fDstPoint;
230 SkIRect fSrcRect;
231
232 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700233 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
234 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000235 };
236
cdalton6819df32014-10-15 13:43:48 -0700237 struct SetState : public Cmd {
238 SetState(const GrDrawState& state) : Cmd(kSetState_Cmd), fState(state) {}
239
joshualitt3322fa42014-11-07 08:48:51 -0800240 virtual void execute(GrGpu*);
cdalton6819df32014-10-15 13:43:48 -0700241
242 GrDrawState fState;
bsalomonf0480b12014-07-02 12:11:24 -0700243 };
244
cdalton6819df32014-10-15 13:43:48 -0700245 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
246 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
247
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000248 // overrides from GrDrawTarget
joshualitt2c93efe2014-11-06 12:57:13 -0800249 virtual void onDraw(const DrawInfo&, const GrClipMaskManager::ScissorState&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000250 virtual void onDrawRect(const SkRect& rect,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000251 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000252 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000253
joshualitt2c93efe2014-11-06 12:57:13 -0800254 virtual void onStencilPath(const GrPath*,
255 const GrClipMaskManager::ScissorState&,
256 const GrStencilSettings&) SK_OVERRIDE;
257 virtual void onDrawPath(const GrPath*,
258 const GrClipMaskManager::ScissorState&,
259 const GrStencilSettings&,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000260 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
cdaltonb85a0aa2014-07-21 15:32:44 -0700261 virtual void onDrawPaths(const GrPathRange*,
joshualitt2c93efe2014-11-06 12:57:13 -0800262 const uint32_t indices[],
263 int count,
264 const float transforms[],
265 PathTransformType,
266 const GrClipMaskManager::ScissorState&,
267 const GrStencilSettings&,
268 const GrDeviceCoordTexture*) SK_OVERRIDE;
bsalomon63b21962014-11-05 07:05:34 -0800269 virtual void onClear(const SkIRect* rect,
270 GrColor color,
271 bool canIgnoreRect,
272 GrRenderTarget* renderTarget) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000273
jvanverth@google.coma6338982013-01-31 21:34:25 +0000274 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000275 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000276 void** vertices) SK_OVERRIDE;
277 virtual bool onReserveIndexSpace(int indexCount,
278 void** indices) SK_OVERRIDE;
279 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
280 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000281 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000282 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000283 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000284 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000285
commit-bot@chromium.org2a05de02014-03-25 15:17:32 +0000286 virtual void didAddGpuTraceMarker() SK_OVERRIDE {}
287 virtual void didRemoveGpuTraceMarker() SK_OVERRIDE {}
commit-bot@chromium.orga3baf3b2014-02-21 18:45:30 +0000288
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000289 // Attempts to concat instances from info onto the previous draw. info must represent an
290 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt2c93efe2014-11-06 12:57:13 -0800291 int concatInstancedDraw(const DrawInfo& info, const GrClipMaskManager::ScissorState&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000292
bsalomon838f62d2014-08-05 07:15:57 -0700293 // Determines whether the current draw operation requieres a new drawstate and if so records it.
294 void recordStateIfNecessary();
295 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700296 void recordClipIfNecessary();
297 // Records any trace markers for a command after adding it to the buffer.
298 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700299
300 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000301
bsalomon@google.com116ad842013-04-09 15:38:19 +0000302 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000303 enum {
cdalton6819df32014-10-15 13:43:48 -0700304 kCmdBufferInitialSizeInBytes = 64 * 1024,
305 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000306 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
cdalton6819df32014-10-15 13:43:48 -0700308 CmdBuffer fCmdBuffer;
309 GrDrawState* fLastState;
cdalton6819df32014-10-15 13:43:48 -0700310 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
joshualitt3322fa42014-11-07 08:48:51 -0800311 GrGpu* fDstGpu;
joshualitt2c93efe2014-11-06 12:57:13 -0800312 GrVertexBufferAllocPool& fVertexPool;
313 GrIndexBufferAllocPool& fIndexPool;
bsalomon@google.com934c5702012-03-20 21:17:58 +0000314
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000315 struct GeometryPoolState {
bsalomonb3e3a952014-09-19 11:10:40 -0700316 const GrVertexBuffer* fPoolVertexBuffer;
317 int fPoolStartVertex;
318 const GrIndexBuffer* fPoolIndexBuffer;
319 int fPoolStartIndex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000320 // caller may conservatively over reserve vertices / indices.
321 // we release unused space back to allocator if possible
322 // can only do this if there isn't an intervening pushGeometrySource()
bsalomonb3e3a952014-09-19 11:10:40 -0700323 size_t fUsedPoolVertexBytes;
324 size_t fUsedPoolIndexBytes;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000325 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000326
bsalomonb3e3a952014-09-19 11:10:40 -0700327 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000328
bsalomonb3e3a952014-09-19 11:10:40 -0700329 GeoPoolStateStack fGeoPoolStateStack;
330 bool fFlushing;
331 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000332
joshualitt6db519c2014-10-29 08:48:18 -0700333 typedef GrClipTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000334};
335
336#endif