blob: 286c545eedde47dfb9f3199f6b55ac2fcdc31576 [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"
egdaniel8dd688b2015-01-22 10:16:09 -080015#include "GrPipeline.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000016#include "GrPath.h"
cdalton6819df32014-10-15 13:43:48 -070017#include "GrTRecorder.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
bsalomon@google.com1c13c962011-02-14 16:51:21 +000019/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000020 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
21 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
22 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
23 * references to the buffers. It is the callers responsibility to ensure that the data is still
24 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
25 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
26 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
27 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000028 */
bsalomon371bcbc2014-12-01 08:19:34 -080029class GrInOrderDrawBuffer : public GrFlushToGpuDrawTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000030public:
31
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000032 /**
33 * Creates a GrInOrderDrawBuffer
34 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000035 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000036 * @param vertexPool pool where vertices for queued draws will be saved when
37 * the vertex source is either reserved or array.
38 * @param indexPool pool where indices for queued draws will be saved when
39 * the index source is either reserved or array.
40 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000041 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000042 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000043 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000044
bsalomonf90a02b2014-11-26 12:28:00 -080045 ~GrInOrderDrawBuffer() SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000046
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000047 // tracking for draws
tfarina912ed6e2014-12-14 15:20:10 -080048 DrawToken getCurrentDrawToken() SK_OVERRIDE { return DrawToken(this, fDrawID); }
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000049
bsalomonf90a02b2014-11-26 12:28:00 -080050 void clearStencilClip(const SkIRect& rect,
51 bool insideClip,
52 GrRenderTarget* renderTarget) SK_OVERRIDE;
joshualitta7024152014-11-03 14:16:35 -080053
bsalomonf90a02b2014-11-26 12:28:00 -080054 void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000055
robertphillips54fac8b2015-02-16 09:35:50 -080056protected:
joshualitt4d8da812015-01-28 12:53:54 -080057 void willReserveVertexAndIndexSpace(int vertexCount,
58 size_t vertexStride,
59 int indexCount);
60
reed@google.comac10a2d2010-12-22 21:39:39 +000061private:
joshualitt873ad0e2015-01-20 09:08:51 -080062 typedef GrGpu::DrawArgs DrawArgs;
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000063
joshualitt873ad0e2015-01-20 09:08:51 -080064 struct SetState;
65
cdalton6819df32014-10-15 13:43:48 -070066 struct Cmd : ::SkNoncopyable {
robertphillipse5e72f12015-02-17 09:14:33 -080067 enum {
68 kDraw_Cmd = 1,
69 kStencilPath_Cmd = 2,
70 kSetState_Cmd = 3,
71 kClear_Cmd = 4,
72 kCopySurface_Cmd = 5,
73 kDrawPath_Cmd = 6,
74 kDrawPaths_Cmd = 7,
75 kDrawBatch_Cmd = 8,
76 };
77
cdalton6819df32014-10-15 13:43:48 -070078 Cmd(uint8_t type) : fType(type) {}
79 virtual ~Cmd() {}
80
joshualitt873ad0e2015-01-20 09:08:51 -080081 virtual void execute(GrInOrderDrawBuffer*, const SetState*) = 0;
cdalton6819df32014-10-15 13:43:48 -070082
robertphillipse5e72f12015-02-17 09:14:33 -080083 uint8_t type() const { return fType & kCmdMask; }
84
85 bool isTraced() const { return SkToBool(fType & kTraceCmdBit); }
86 void makeTraced() { fType |= kTraceCmdBit; }
87
88 private:
89 static const int kCmdMask = 0x7F;
90 static const int kTraceCmdBit = 0x80;
91
cdalton6819df32014-10-15 13:43:48 -070092 uint8_t fType;
93 };
94
95 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -080096 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -070097
joshualitt873ad0e2015-01-20 09:08:51 -080098 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070099
joshualitt2c93efe2014-11-06 12:57:13 -0800100 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +0000101 };
102
cdalton6819df32014-10-15 13:43:48 -0700103 struct StencilPath : public Cmd {
bsalomon3e791242014-12-17 13:43:13 -0800104 StencilPath(const GrPath* path, GrRenderTarget* rt)
105 : Cmd(kStencilPath_Cmd)
106 , fRenderTarget(rt)
107 , fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000108
bsalomonb3e3a952014-09-19 11:10:40 -0700109 const GrPath* path() const { return fPath.get(); }
110
joshualitt873ad0e2015-01-20 09:08:51 -0800111 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700112
bsalomon3e791242014-12-17 13:43:13 -0800113 SkMatrix fViewMatrix;
114 bool fUseHWAA;
115 GrStencilSettings fStencil;
116 GrScissorState fScissor;
bsalomonb3e3a952014-09-19 11:10:40 -0700117 private:
bsalomon3e791242014-12-17 13:43:13 -0800118 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
119 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000120 };
121
cdalton6819df32014-10-15 13:43:48 -0700122 struct DrawPath : public Cmd {
123 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000124
bsalomonb3e3a952014-09-19 11:10:40 -0700125 const GrPath* path() const { return fPath.get(); }
126
joshualitt873ad0e2015-01-20 09:08:51 -0800127 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700128
joshualitt2c93efe2014-11-06 12:57:13 -0800129 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700130
131 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700132 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000133 };
134
cdalton6819df32014-10-15 13:43:48 -0700135 struct DrawPaths : public Cmd {
136 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700137
138 const GrPathRange* pathRange() const { return fPathRange.get(); }
139
joshualitt873ad0e2015-01-20 09:08:51 -0800140 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700141
cdalton3fc6a2f2014-11-13 11:54:20 -0800142 int fIndicesLocation;
cdalton55b24af2014-11-25 11:00:56 -0800143 PathIndexType fIndexType;
cdalton3fc6a2f2014-11-13 11:54:20 -0800144 int fTransformsLocation;
cdalton55b24af2014-11-25 11:00:56 -0800145 PathTransformType fTransformType;
146 int fCount;
joshualitt2c93efe2014-11-06 12:57:13 -0800147 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700148
149 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700150 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000151 };
152
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000153 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700154 struct Clear : public Cmd {
155 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
156
bsalomonb3e3a952014-09-19 11:10:40 -0700157 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000158
joshualitt873ad0e2015-01-20 09:08:51 -0800159 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700160
bsalomonb3e3a952014-09-19 11:10:40 -0700161 SkIRect fRect;
162 GrColor fColor;
163 bool fCanIgnoreRect;
164
165 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700166 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000167 };
168
joshualitt6db519c2014-10-29 08:48:18 -0700169 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
170 struct ClearStencilClip : public Cmd {
171 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
172
173 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
174
joshualitt873ad0e2015-01-20 09:08:51 -0800175 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700176
177 SkIRect fRect;
178 bool fInsideClip;
179
180 private:
181 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
182 };
183
cdalton6819df32014-10-15 13:43:48 -0700184 struct CopySurface : public Cmd {
185 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700186
187 GrSurface* dst() const { return fDst.get(); }
188 GrSurface* src() const { return fSrc.get(); }
189
joshualitt873ad0e2015-01-20 09:08:51 -0800190 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700191
bsalomonb3e3a952014-09-19 11:10:40 -0700192 SkIPoint fDstPoint;
193 SkIRect fSrcRect;
194
195 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700196 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
197 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000198 };
199
egdaniel8dd688b2015-01-22 10:16:09 -0800200 // TODO: rename to SetPipeline once pp, batch tracker, and desc are removed
cdalton6819df32014-10-15 13:43:48 -0700201 struct SetState : public Cmd {
egdaniele36914c2015-02-13 09:00:33 -0800202 // TODO get rid of the prim proc parameter when we use batch everywhere
203 SetState(const GrPrimitiveProcessor* primProc = NULL)
bsalomon932f8662014-11-24 06:47:48 -0800204 : Cmd(kSetState_Cmd)
egdaniele36914c2015-02-13 09:00:33 -0800205 , fPrimitiveProcessor(primProc) {}
cdalton6819df32014-10-15 13:43:48 -0700206
egdaniele36914c2015-02-13 09:00:33 -0800207 ~SetState() { reinterpret_cast<GrPipeline*>(fPipeline.get())->~GrPipeline(); }
208
209 // This function is only for getting the location in memory where we will create our
210 // pipeline object.
211 GrPipeline* pipelineLocation() { return reinterpret_cast<GrPipeline*>(fPipeline.get()); }
212
213 const GrPipeline* getPipeline() const {
214 return reinterpret_cast<const GrPipeline*>(fPipeline.get());
215 }
joshualitt4d8da812015-01-28 12:53:54 -0800216
joshualitt873ad0e2015-01-20 09:08:51 -0800217 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700218
joshualitt873ad0e2015-01-20 09:08:51 -0800219 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
egdaniele36914c2015-02-13 09:00:33 -0800220 ProgramPrimitiveProcessor fPrimitiveProcessor;
221 SkAlignedSStorage<sizeof(GrPipeline)> fPipeline;
222 GrProgramDesc fDesc;
223 GrBatchTracker fBatchTracker;
bsalomonf0480b12014-07-02 12:11:24 -0700224 };
225
joshualitt4d8da812015-01-28 12:53:54 -0800226 struct DrawBatch : public Cmd {
227 DrawBatch(GrBatch* batch) : Cmd(kDrawBatch_Cmd), fBatch(SkRef(batch)) {
228 SkASSERT(!batch->isUsed());
229 }
230
231 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
232
233 // TODO it wouldn't be too hard to let batches allocate in the cmd buffer
234 SkAutoTUnref<GrBatch> fBatch;
235 };
236
cdalton6819df32014-10-15 13:43:48 -0700237 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
238 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
239
bsalomon371bcbc2014-12-01 08:19:34 -0800240 void onReset() SK_OVERRIDE;
241 void onFlush() SK_OVERRIDE;
242
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000243 // overrides from GrDrawTarget
egdaniele36914c2015-02-13 09:00:33 -0800244 void onDraw(const GrGeometryProcessor*, const DrawInfo&, const PipelineInfo&) SK_OVERRIDE;
245 void onDrawBatch(GrBatch*, const PipelineInfo&) SK_OVERRIDE;
egdaniel8dd688b2015-01-22 10:16:09 -0800246 void onDrawRect(GrPipelineBuilder*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800247 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800248 const SkMatrix& viewMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800249 const SkRect& rect,
250 const SkRect* localRect,
251 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000252
egdaniel8dd688b2015-01-22 10:16:09 -0800253 void onStencilPath(const GrPipelineBuilder&,
joshualitt56995b52014-12-11 15:44:02 -0800254 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800255 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800256 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800257 const GrStencilSettings&) SK_OVERRIDE;
egdaniele36914c2015-02-13 09:00:33 -0800258 void onDrawPath(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800259 const GrPath*,
bsalomonae59b772014-11-19 08:23:49 -0800260 const GrStencilSettings&,
egdaniele36914c2015-02-13 09:00:33 -0800261 const PipelineInfo&) SK_OVERRIDE;
262 void onDrawPaths(const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800263 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800264 const void* indices,
265 PathIndexType,
266 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800267 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800268 int count,
bsalomonae59b772014-11-19 08:23:49 -0800269 const GrStencilSettings&,
egdaniele36914c2015-02-13 09:00:33 -0800270 const PipelineInfo&) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800271 void onClear(const SkIRect* rect,
272 GrColor color,
273 bool canIgnoreRect,
274 GrRenderTarget* renderTarget) SK_OVERRIDE;
bsalomonf90a02b2014-11-26 12:28:00 -0800275 bool onCopySurface(GrSurface* dst,
276 GrSurface* src,
277 const SkIRect& srcRect,
278 const SkIPoint& dstPoint) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000279
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000280 // Attempts to concat instances from info onto the previous draw. info must represent an
281 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
egdaniele36914c2015-02-13 09:00:33 -0800282 int concatInstancedDraw(const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000283
egdaniel8dd688b2015-01-22 10:16:09 -0800284 // Determines whether the current draw operation requires a new GrPipeline and if so
285 // records it. If the draw can be skipped false is returned and no new GrPipeline is
bsalomonae59b772014-11-19 08:23:49 -0800286 // recorded.
joshualitt4d8da812015-01-28 12:53:54 -0800287 // TODO delete the primproc variant when we have batches everywhere
egdaniele36914c2015-02-13 09:00:33 -0800288 bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(const GrPrimitiveProcessor*,
289 const PipelineInfo&);
290 bool SK_WARN_UNUSED_RESULT setupPipelineAndShouldDraw(GrBatch*, const PipelineInfo&);
joshualitt4d8da812015-01-28 12:53:54 -0800291
bsalomon838f62d2014-08-05 07:15:57 -0700292 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700293 void recordClipIfNecessary();
294 // Records any trace markers for a command after adding it to the buffer.
295 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700296
mtklein72c9faa2015-01-09 10:06:39 -0800297 bool isIssued(uint32_t drawID) SK_OVERRIDE { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000298
joshualitt4d8da812015-01-28 12:53:54 -0800299 GrBatchTarget* getBatchTarget() { return &fBatchTarget; }
300
bsalomon@google.com116ad842013-04-09 15:38:19 +0000301 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000302 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800303 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton55b24af2014-11-25 11:00:56 -0800304 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
305 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000306 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
bsalomonae59b772014-11-19 08:23:49 -0800308 CmdBuffer fCmdBuffer;
joshualitt873ad0e2015-01-20 09:08:51 -0800309 SetState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800310 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
cdalton55b24af2014-11-25 11:00:56 -0800311 SkTDArray<char> fPathIndexBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800312 SkTDArray<float> fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800313 uint32_t fDrawID;
joshualitt4d8da812015-01-28 12:53:54 -0800314 GrBatchTarget fBatchTarget;
315 // TODO hack until batch is everywhere
316 DrawBatch* fDrawBatch;
317
joshualitt70f00042015-02-06 15:53:59 -0800318 // This will go away when everything uses batch. However, in the short term anything which
319 // might be put into the GrInOrderDrawBuffer needs to make sure it closes the last batch
320 void closeBatch() {
321 if (fDrawBatch) {
322 fBatchTarget.resetNumberOfDraws();
323 fDrawBatch->execute(this, fPrevState);
324 fDrawBatch->fBatch->setNumberOfDraws(fBatchTarget.numberOfDraws());
325 fDrawBatch = NULL;
326 }
327 }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000328
bsalomon371bcbc2014-12-01 08:19:34 -0800329 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000330};
331
332#endif