blob: ea1279bc578e82c0aac1944d5a81ed8abe2c980b [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"
bsalomonae59b772014-11-19 08:23:49 -080012#include "GrOptDrawState.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000013#include "GrPath.h"
cdalton6819df32014-10-15 13:43:48 -070014#include "GrTRecorder.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
bsalomon@google.com1c13c962011-02-14 16:51:21 +000016/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000017 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
18 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
19 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
20 * references to the buffers. It is the callers responsibility to ensure that the data is still
21 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
22 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
23 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
24 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000025 */
bsalomon371bcbc2014-12-01 08:19:34 -080026class GrInOrderDrawBuffer : public GrFlushToGpuDrawTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000027public:
28
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000029 /**
30 * Creates a GrInOrderDrawBuffer
31 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000032 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000033 * @param vertexPool pool where vertices for queued draws will be saved when
34 * the vertex source is either reserved or array.
35 * @param indexPool pool where indices for queued draws will be saved when
36 * the index source is either reserved or array.
37 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000038 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000039 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000040 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000041
bsalomonf90a02b2014-11-26 12:28:00 -080042 ~GrInOrderDrawBuffer() SK_OVERRIDE;
reed@google.comac10a2d2010-12-22 21:39:39 +000043
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000044 // tracking for draws
tfarina912ed6e2014-12-14 15:20:10 -080045 DrawToken getCurrentDrawToken() SK_OVERRIDE { return DrawToken(this, fDrawID); }
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000046
bsalomonf90a02b2014-11-26 12:28:00 -080047 void clearStencilClip(const SkIRect& rect,
48 bool insideClip,
49 GrRenderTarget* renderTarget) SK_OVERRIDE;
joshualitta7024152014-11-03 14:16:35 -080050
bsalomonf90a02b2014-11-26 12:28:00 -080051 void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000052
reed@google.comac10a2d2010-12-22 21:39:39 +000053private:
joshualitt873ad0e2015-01-20 09:08:51 -080054 typedef GrGpu::DrawArgs DrawArgs;
cdalton6819df32014-10-15 13:43:48 -070055 enum {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000056 kDraw_Cmd = 1,
57 kStencilPath_Cmd = 2,
58 kSetState_Cmd = 3,
joshualitt2c93efe2014-11-06 12:57:13 -080059 kClear_Cmd = 4,
60 kCopySurface_Cmd = 5,
61 kDrawPath_Cmd = 6,
62 kDrawPaths_Cmd = 7,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000063 };
64
joshualitt873ad0e2015-01-20 09:08:51 -080065 struct SetState;
66
cdalton6819df32014-10-15 13:43:48 -070067 struct Cmd : ::SkNoncopyable {
68 Cmd(uint8_t type) : fType(type) {}
69 virtual ~Cmd() {}
70
joshualitt873ad0e2015-01-20 09:08:51 -080071 virtual void execute(GrInOrderDrawBuffer*, const SetState*) = 0;
cdalton6819df32014-10-15 13:43:48 -070072
73 uint8_t fType;
74 };
75
76 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -080077 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -070078
joshualitt873ad0e2015-01-20 09:08:51 -080079 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070080
joshualitt2c93efe2014-11-06 12:57:13 -080081 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +000082 };
83
cdalton6819df32014-10-15 13:43:48 -070084 struct StencilPath : public Cmd {
bsalomon3e791242014-12-17 13:43:13 -080085 StencilPath(const GrPath* path, GrRenderTarget* rt)
86 : Cmd(kStencilPath_Cmd)
87 , fRenderTarget(rt)
88 , fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +000089
bsalomonb3e3a952014-09-19 11:10:40 -070090 const GrPath* path() const { return fPath.get(); }
91
joshualitt873ad0e2015-01-20 09:08:51 -080092 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070093
bsalomon3e791242014-12-17 13:43:13 -080094 SkMatrix fViewMatrix;
95 bool fUseHWAA;
96 GrStencilSettings fStencil;
97 GrScissorState fScissor;
bsalomonb3e3a952014-09-19 11:10:40 -070098 private:
bsalomon3e791242014-12-17 13:43:13 -080099 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
100 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000101 };
102
cdalton6819df32014-10-15 13:43:48 -0700103 struct DrawPath : public Cmd {
104 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000105
bsalomonb3e3a952014-09-19 11:10:40 -0700106 const GrPath* path() const { return fPath.get(); }
107
joshualitt873ad0e2015-01-20 09:08:51 -0800108 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700109
joshualitt2c93efe2014-11-06 12:57:13 -0800110 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700111
112 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700113 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000114 };
115
cdalton6819df32014-10-15 13:43:48 -0700116 struct DrawPaths : public Cmd {
117 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700118
119 const GrPathRange* pathRange() const { return fPathRange.get(); }
120
joshualitt873ad0e2015-01-20 09:08:51 -0800121 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700122
cdalton3fc6a2f2014-11-13 11:54:20 -0800123 int fIndicesLocation;
cdalton55b24af2014-11-25 11:00:56 -0800124 PathIndexType fIndexType;
cdalton3fc6a2f2014-11-13 11:54:20 -0800125 int fTransformsLocation;
cdalton55b24af2014-11-25 11:00:56 -0800126 PathTransformType fTransformType;
127 int fCount;
joshualitt2c93efe2014-11-06 12:57:13 -0800128 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700129
130 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700131 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000132 };
133
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000134 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700135 struct Clear : public Cmd {
136 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
137
bsalomonb3e3a952014-09-19 11:10:40 -0700138 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000139
joshualitt873ad0e2015-01-20 09:08:51 -0800140 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700141
bsalomonb3e3a952014-09-19 11:10:40 -0700142 SkIRect fRect;
143 GrColor fColor;
144 bool fCanIgnoreRect;
145
146 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700147 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000148 };
149
joshualitt6db519c2014-10-29 08:48:18 -0700150 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
151 struct ClearStencilClip : public Cmd {
152 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
153
154 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
155
joshualitt873ad0e2015-01-20 09:08:51 -0800156 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700157
158 SkIRect fRect;
159 bool fInsideClip;
160
161 private:
162 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
163 };
164
cdalton6819df32014-10-15 13:43:48 -0700165 struct CopySurface : public Cmd {
166 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700167
168 GrSurface* dst() const { return fDst.get(); }
169 GrSurface* src() const { return fSrc.get(); }
170
joshualitt873ad0e2015-01-20 09:08:51 -0800171 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700172
bsalomonb3e3a952014-09-19 11:10:40 -0700173 SkIPoint fDstPoint;
174 SkIRect fSrcRect;
175
176 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700177 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
178 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000179 };
180
cdalton6819df32014-10-15 13:43:48 -0700181 struct SetState : public Cmd {
joshualitt71c92602015-01-14 08:12:47 -0800182 SetState(const GrDrawState& drawState, const GrPrimitiveProcessor* primProc,
183 const GrDrawTargetCaps& caps,
joshualitt17e73142015-01-21 11:52:36 -0800184 const GrScissorState& scissor, const GrDeviceCoordTexture* dstCopy)
bsalomon932f8662014-11-24 06:47:48 -0800185 : Cmd(kSetState_Cmd)
joshualitt873ad0e2015-01-20 09:08:51 -0800186 , fPrimitiveProcessor(primProc)
joshualitt17e73142015-01-21 11:52:36 -0800187 , fState(drawState, primProc, caps, scissor, dstCopy) {}
cdalton6819df32014-10-15 13:43:48 -0700188
joshualitt873ad0e2015-01-20 09:08:51 -0800189 void execute(GrInOrderDrawBuffer*, const SetState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700190
joshualitt873ad0e2015-01-20 09:08:51 -0800191 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
192 ProgramPrimitiveProcessor fPrimitiveProcessor;
193 const GrOptDrawState fState;
194 GrProgramDesc fDesc;
195 GrBatchTracker fBatchTracker;
bsalomonf0480b12014-07-02 12:11:24 -0700196 };
197
cdalton6819df32014-10-15 13:43:48 -0700198 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
199 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
200
bsalomon371bcbc2014-12-01 08:19:34 -0800201 void onReset() SK_OVERRIDE;
202 void onFlush() SK_OVERRIDE;
203
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000204 // overrides from GrDrawTarget
bsalomonae59b772014-11-19 08:23:49 -0800205 void onDraw(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800206 const GrGeometryProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800207 const DrawInfo&,
bsalomon3e791242014-12-17 13:43:13 -0800208 const GrScissorState&,
joshualitt9176e2c2014-11-20 07:28:52 -0800209 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800210 void onDrawRect(GrDrawState*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800211 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800212 const SkMatrix& viewMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800213 const SkRect& rect,
214 const SkRect* localRect,
215 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000216
bsalomonae59b772014-11-19 08:23:49 -0800217 void onStencilPath(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800218 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800219 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800220 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800221 const GrStencilSettings&) SK_OVERRIDE;
222 void onDrawPath(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800223 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800224 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800225 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800226 const GrStencilSettings&,
227 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
228 void onDrawPaths(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800229 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800230 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800231 const void* indices,
232 PathIndexType,
233 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800234 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800235 int count,
bsalomon3e791242014-12-17 13:43:13 -0800236 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800237 const GrStencilSettings&,
238 const GrDeviceCoordTexture*) SK_OVERRIDE;
239 void onClear(const SkIRect* rect,
240 GrColor color,
241 bool canIgnoreRect,
242 GrRenderTarget* renderTarget) SK_OVERRIDE;
bsalomonf90a02b2014-11-26 12:28:00 -0800243 bool onCopySurface(GrSurface* dst,
244 GrSurface* src,
245 const SkIRect& srcRect,
246 const SkIPoint& dstPoint) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000247
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000248 // Attempts to concat instances from info onto the previous draw. info must represent an
249 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt54e0c122014-11-19 09:38:51 -0800250 int concatInstancedDraw(const GrDrawState&, const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000251
bsalomonae59b772014-11-19 08:23:49 -0800252 // Determines whether the current draw operation requires a new GrOptDrawState and if so
253 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
254 // recorded.
255 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
joshualitt71c92602015-01-14 08:12:47 -0800256 const GrPrimitiveProcessor*,
bsalomon3e791242014-12-17 13:43:13 -0800257 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800258 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700259 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700260 void recordClipIfNecessary();
261 // Records any trace markers for a command after adding it to the buffer.
262 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700263
mtklein72c9faa2015-01-09 10:06:39 -0800264 bool isIssued(uint32_t drawID) SK_OVERRIDE { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000265
bsalomon@google.com116ad842013-04-09 15:38:19 +0000266 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000267 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800268 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton55b24af2014-11-25 11:00:56 -0800269 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
270 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000271 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000272
bsalomonae59b772014-11-19 08:23:49 -0800273 CmdBuffer fCmdBuffer;
joshualitt873ad0e2015-01-20 09:08:51 -0800274 SetState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800275 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
cdalton55b24af2014-11-25 11:00:56 -0800276 SkTDArray<char> fPathIndexBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800277 SkTDArray<float> fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800278 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000279
bsalomon371bcbc2014-12-01 08:19:34 -0800280 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000281};
282
283#endif