blob: 58761694b1e1904212d17b3ad8e6a408571f3a75 [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
bsalomon371bcbc2014-12-01 08:19:34 -080011#include "GrFlushToGpuDrawTarget.h"
joshualitt4d8da812015-01-28 12:53:54 -080012
13#include "GrBatch.h"
14#include "GrBatchTarget.h"
robertphillips9888b222015-02-27 08:50:34 -080015#include "SkChunkAlloc.h"
egdaniel8dd688b2015-01-22 10:16:09 -080016#include "GrPipeline.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000017#include "GrPath.h"
cdalton6819df32014-10-15 13:43:48 -070018#include "GrTRecorder.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000019
bsalomon@google.com1c13c962011-02-14 16:51:21 +000020/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000021 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
22 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
23 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
24 * references to the buffers. It is the callers responsibility to ensure that the data is still
25 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
26 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
27 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
28 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000029 */
bsalomon371bcbc2014-12-01 08:19:34 -080030class GrInOrderDrawBuffer : public GrFlushToGpuDrawTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000031public:
32
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000033 /**
34 * Creates a GrInOrderDrawBuffer
35 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000036 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000037 * @param vertexPool pool where vertices for queued draws will be saved when
38 * the vertex source is either reserved or array.
39 * @param indexPool pool where indices for queued draws will be saved when
40 * the index source is either reserved or array.
41 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000042 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000043 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000044 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000045
bsalomonf90a02b2014-11-26 12:28:00 -080046 ~GrInOrderDrawBuffer() SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000047
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000048 // tracking for draws
tfarina912ed6e2014-12-14 15:20:10 -080049 DrawToken getCurrentDrawToken() SK_OVERRIDE { return DrawToken(this, fDrawID); }
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000050
bsalomonf90a02b2014-11-26 12:28:00 -080051 void clearStencilClip(const SkIRect& rect,
52 bool insideClip,
53 GrRenderTarget* renderTarget) SK_OVERRIDE;
joshualitta7024152014-11-03 14:16:35 -080054
bsalomonf90a02b2014-11-26 12:28:00 -080055 void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000056
robertphillips54fac8b2015-02-16 09:35:50 -080057protected:
joshualitt4d8da812015-01-28 12:53:54 -080058 void willReserveVertexAndIndexSpace(int vertexCount,
59 size_t vertexStride,
60 int indexCount);
61
reed@google.comac10a2d2010-12-22 21:39:39 +000062private:
joshualitt873ad0e2015-01-20 09:08:51 -080063 typedef GrGpu::DrawArgs DrawArgs;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000064
joshualitt873ad0e2015-01-20 09:08:51 -080065 struct SetState;
66
cdalton6819df32014-10-15 13:43:48 -070067 struct Cmd : ::SkNoncopyable {
robertphillipse5e72f12015-02-17 09:14:33 -080068 enum {
69 kDraw_Cmd = 1,
70 kStencilPath_Cmd = 2,
71 kSetState_Cmd = 3,
72 kClear_Cmd = 4,
73 kCopySurface_Cmd = 5,
74 kDrawPath_Cmd = 6,
75 kDrawPaths_Cmd = 7,
76 kDrawBatch_Cmd = 8,
77 };
78
cdalton6819df32014-10-15 13:43:48 -070079 Cmd(uint8_t type) : fType(type) {}
80 virtual ~Cmd() {}
81
robertphillips9888b222015-02-27 08:50:34 -080082 virtual void execute(GrGpu*, const SetState*) = 0;
cdalton6819df32014-10-15 13:43:48 -070083
robertphillipse5e72f12015-02-17 09:14:33 -080084 uint8_t type() const { return fType & kCmdMask; }
85
86 bool isTraced() const { return SkToBool(fType & kTraceCmdBit); }
87 void makeTraced() { fType |= kTraceCmdBit; }
88
89 private:
90 static const int kCmdMask = 0x7F;
91 static const int kTraceCmdBit = 0x80;
92
cdalton6819df32014-10-15 13:43:48 -070093 uint8_t fType;
94 };
95
96 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -080097 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -070098
robertphillips9888b222015-02-27 08:50:34 -080099 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700100
joshualitt2c93efe2014-11-06 12:57:13 -0800101 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +0000102 };
103
cdalton6819df32014-10-15 13:43:48 -0700104 struct StencilPath : public Cmd {
bsalomon3e791242014-12-17 13:43:13 -0800105 StencilPath(const GrPath* path, GrRenderTarget* rt)
106 : Cmd(kStencilPath_Cmd)
107 , fRenderTarget(rt)
108 , fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000109
bsalomonb3e3a952014-09-19 11:10:40 -0700110 const GrPath* path() const { return fPath.get(); }
111
robertphillips9888b222015-02-27 08:50:34 -0800112 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700113
bsalomon3e791242014-12-17 13:43:13 -0800114 SkMatrix fViewMatrix;
115 bool fUseHWAA;
116 GrStencilSettings fStencil;
117 GrScissorState fScissor;
bsalomonb3e3a952014-09-19 11:10:40 -0700118 private:
bsalomon3e791242014-12-17 13:43:13 -0800119 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
120 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000121 };
122
cdalton6819df32014-10-15 13:43:48 -0700123 struct DrawPath : public Cmd {
124 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000125
bsalomonb3e3a952014-09-19 11:10:40 -0700126 const GrPath* path() const { return fPath.get(); }
127
robertphillips9888b222015-02-27 08:50:34 -0800128 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700129
joshualitt2c93efe2014-11-06 12:57:13 -0800130 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700131
132 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700133 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000134 };
135
cdalton6819df32014-10-15 13:43:48 -0700136 struct DrawPaths : public Cmd {
137 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700138
139 const GrPathRange* pathRange() const { return fPathRange.get(); }
140
robertphillips9888b222015-02-27 08:50:34 -0800141 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700142
robertphillips9888b222015-02-27 08:50:34 -0800143 char* fIndices;
cdalton55b24af2014-11-25 11:00:56 -0800144 PathIndexType fIndexType;
robertphillips9888b222015-02-27 08:50:34 -0800145 float* fTransforms;
cdalton55b24af2014-11-25 11:00:56 -0800146 PathTransformType fTransformType;
147 int fCount;
joshualitt2c93efe2014-11-06 12:57:13 -0800148 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700149
150 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700151 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000152 };
153
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000154 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700155 struct Clear : public Cmd {
156 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
157
bsalomonb3e3a952014-09-19 11:10:40 -0700158 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000159
robertphillips9888b222015-02-27 08:50:34 -0800160 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700161
bsalomonb3e3a952014-09-19 11:10:40 -0700162 SkIRect fRect;
163 GrColor fColor;
164 bool fCanIgnoreRect;
165
166 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700167 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000168 };
169
joshualitt6db519c2014-10-29 08:48:18 -0700170 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
171 struct ClearStencilClip : public Cmd {
172 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
173
174 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
175
robertphillips9888b222015-02-27 08:50:34 -0800176 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700177
178 SkIRect fRect;
179 bool fInsideClip;
180
181 private:
182 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
183 };
184
cdalton6819df32014-10-15 13:43:48 -0700185 struct CopySurface : public Cmd {
186 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700187
188 GrSurface* dst() const { return fDst.get(); }
189 GrSurface* src() const { return fSrc.get(); }
190
robertphillips9888b222015-02-27 08:50:34 -0800191 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700192
bsalomonb3e3a952014-09-19 11:10:40 -0700193 SkIPoint fDstPoint;
194 SkIRect fSrcRect;
195
196 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700197 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
198 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000199 };
200
egdaniel8dd688b2015-01-22 10:16:09 -0800201 // TODO: rename to SetPipeline once pp, batch tracker, and desc are removed
cdalton6819df32014-10-15 13:43:48 -0700202 struct SetState : public Cmd {
egdaniele36914c2015-02-13 09:00:33 -0800203 // TODO get rid of the prim proc parameter when we use batch everywhere
204 SetState(const GrPrimitiveProcessor* primProc = NULL)
bsalomon932f8662014-11-24 06:47:48 -0800205 : Cmd(kSetState_Cmd)
egdaniele36914c2015-02-13 09:00:33 -0800206 , fPrimitiveProcessor(primProc) {}
cdalton6819df32014-10-15 13:43:48 -0700207
egdaniele36914c2015-02-13 09:00:33 -0800208 ~SetState() { reinterpret_cast<GrPipeline*>(fPipeline.get())->~GrPipeline(); }
209
210 // This function is only for getting the location in memory where we will create our
211 // pipeline object.
212 GrPipeline* pipelineLocation() { return reinterpret_cast<GrPipeline*>(fPipeline.get()); }
213
214 const GrPipeline* getPipeline() const {
215 return reinterpret_cast<const GrPipeline*>(fPipeline.get());
216 }
joshualitt4d8da812015-01-28 12:53:54 -0800217
robertphillips9888b222015-02-27 08:50:34 -0800218 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700219
joshualitt873ad0e2015-01-20 09:08:51 -0800220 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
egdaniele36914c2015-02-13 09:00:33 -0800221 ProgramPrimitiveProcessor fPrimitiveProcessor;
222 SkAlignedSStorage<sizeof(GrPipeline)> fPipeline;
223 GrProgramDesc fDesc;
224 GrBatchTracker fBatchTracker;
bsalomonf0480b12014-07-02 12:11:24 -0700225 };
226
joshualitt4d8da812015-01-28 12:53:54 -0800227 struct DrawBatch : public Cmd {
robertphillips9888b222015-02-27 08:50:34 -0800228 DrawBatch(GrBatch* batch, GrBatchTarget* batchTarget)
229 : Cmd(kDrawBatch_Cmd)
230 , fBatch(SkRef(batch))
231 , fBatchTarget(batchTarget) {
joshualitt4d8da812015-01-28 12:53:54 -0800232 SkASSERT(!batch->isUsed());
233 }
234
robertphillips9888b222015-02-27 08:50:34 -0800235 void execute(GrGpu*, const SetState*) SK_OVERRIDE;
joshualitt4d8da812015-01-28 12:53:54 -0800236
237 // TODO it wouldn't be too hard to let batches allocate in the cmd buffer
238 SkAutoTUnref<GrBatch> fBatch;
robertphillips9888b222015-02-27 08:50:34 -0800239
240 private:
241 GrBatchTarget* fBatchTarget;
joshualitt4d8da812015-01-28 12:53:54 -0800242 };
243
cdalton6819df32014-10-15 13:43:48 -0700244 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
245 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
246
bsalomon371bcbc2014-12-01 08:19:34 -0800247 void onReset() SK_OVERRIDE;
248 void onFlush() SK_OVERRIDE;
249
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000250 // overrides from GrDrawTarget
egdaniele36914c2015-02-13 09:00:33 -0800251 void onDraw(const GrGeometryProcessor*, const DrawInfo&, const PipelineInfo&) SK_OVERRIDE;
252 void onDrawBatch(GrBatch*, const PipelineInfo&) SK_OVERRIDE;
egdaniel8dd688b2015-01-22 10:16:09 -0800253 void onDrawRect(GrPipelineBuilder*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800254 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800255 const SkMatrix& viewMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800256 const SkRect& rect,
257 const SkRect* localRect,
258 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000259
egdaniel8dd688b2015-01-22 10:16:09 -0800260 void onStencilPath(const GrPipelineBuilder&,
joshualitt56995b52014-12-11 15:44:02 -0800261 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800262 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800263 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800264 const GrStencilSettings&) SK_OVERRIDE;
egdaniele36914c2015-02-13 09:00:33 -0800265 void onDrawPath(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800266 const GrPath*,
bsalomonae59b772014-11-19 08:23:49 -0800267 const GrStencilSettings&,
egdaniele36914c2015-02-13 09:00:33 -0800268 const PipelineInfo&) SK_OVERRIDE;
269 void onDrawPaths(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800270 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800271 const void* indices,
272 PathIndexType,
273 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800274 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800275 int count,
bsalomonae59b772014-11-19 08:23:49 -0800276 const GrStencilSettings&,
egdaniele36914c2015-02-13 09:00:33 -0800277 const PipelineInfo&) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800278 void onClear(const SkIRect* rect,
279 GrColor color,
280 bool canIgnoreRect,
281 GrRenderTarget* renderTarget) SK_OVERRIDE;
bsalomonf90a02b2014-11-26 12:28:00 -0800282 bool onCopySurface(GrSurface* dst,
283 GrSurface* src,
284 const SkIRect& srcRect,
285 const SkIPoint& dstPoint) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000286
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000287 // Attempts to concat instances from info onto the previous draw. info must represent an
288 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
egdaniele36914c2015-02-13 09:00:33 -0800289 int concatInstancedDraw(const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000290
egdaniel8dd688b2015-01-22 10:16:09 -0800291 // Determines whether the current draw operation requires a new GrPipeline and if so
292 // records it. If the draw can be skipped false is returned and no new GrPipeline is
bsalomonae59b772014-11-19 08:23:49 -0800293 // recorded.
joshualitt4d8da812015-01-28 12:53:54 -0800294 // TODO delete the primproc variant when we have batches everywhere
egdaniele36914c2015-02-13 09:00:33 -0800295 bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(const GrPrimitiveProcessor*,
296 const PipelineInfo&);
297 bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(GrBatch*, const PipelineInfo&);
joshualitt4d8da812015-01-28 12:53:54 -0800298
bsalomon838f62d2014-08-05 07:15:57 -0700299 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700300 void recordClipIfNecessary();
301 // Records any trace markers for a command after adding it to the buffer.
302 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700303
mtklein72c9faa2015-01-09 10:06:39 -0800304 bool isIssued(uint32_t drawID) SK_OVERRIDE { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000305
joshualitt4d8da812015-01-28 12:53:54 -0800306 GrBatchTarget* getBatchTarget() { return &fBatchTarget; }
307
bsalomon@google.com116ad842013-04-09 15:38:19 +0000308 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000309 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800310 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton55b24af2014-11-25 11:00:56 -0800311 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
312 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000313 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000314
bsalomonae59b772014-11-19 08:23:49 -0800315 CmdBuffer fCmdBuffer;
joshualitt873ad0e2015-01-20 09:08:51 -0800316 SetState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800317 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
robertphillips9888b222015-02-27 08:50:34 -0800318 SkChunkAlloc fPathIndexBuffer;
319 SkChunkAlloc fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800320 uint32_t fDrawID;
joshualitt4d8da812015-01-28 12:53:54 -0800321 GrBatchTarget fBatchTarget;
322 // TODO hack until batch is everywhere
323 DrawBatch* fDrawBatch;
324
joshualitt70f00042015-02-06 15:53:59 -0800325 // This will go away when everything uses batch. However, in the short term anything which
326 // might be put into the GrInOrderDrawBuffer needs to make sure it closes the last batch
327 void closeBatch() {
328 if (fDrawBatch) {
329 fBatchTarget.resetNumberOfDraws();
robertphillips9888b222015-02-27 08:50:34 -0800330 fDrawBatch->execute(this->getGpu(), fPrevState);
joshualitt70f00042015-02-06 15:53:59 -0800331 fDrawBatch->fBatch->setNumberOfDraws(fBatchTarget.numberOfDraws());
332 fDrawBatch = NULL;
333 }
334 }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000335
bsalomon371bcbc2014-12-01 08:19:34 -0800336 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000337};
338
339#endif