blob: d415e495173cdc409895146b2da5becc37822e88 [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:
cdalton6819df32014-10-15 13:43:48 -070054 enum {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000055 kDraw_Cmd = 1,
56 kStencilPath_Cmd = 2,
57 kSetState_Cmd = 3,
joshualitt2c93efe2014-11-06 12:57:13 -080058 kClear_Cmd = 4,
59 kCopySurface_Cmd = 5,
60 kDrawPath_Cmd = 6,
61 kDrawPaths_Cmd = 7,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000062 };
63
cdalton6819df32014-10-15 13:43:48 -070064 struct Cmd : ::SkNoncopyable {
65 Cmd(uint8_t type) : fType(type) {}
66 virtual ~Cmd() {}
67
cdalton3fc6a2f2014-11-13 11:54:20 -080068 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) = 0;
cdalton6819df32014-10-15 13:43:48 -070069
70 uint8_t fType;
71 };
72
73 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -080074 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -070075
bsalomon932f8662014-11-24 06:47:48 -080076 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070077
joshualitt2c93efe2014-11-06 12:57:13 -080078 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +000079 };
80
cdalton6819df32014-10-15 13:43:48 -070081 struct StencilPath : public Cmd {
bsalomon3e791242014-12-17 13:43:13 -080082 StencilPath(const GrPath* path, GrRenderTarget* rt)
83 : Cmd(kStencilPath_Cmd)
84 , fRenderTarget(rt)
85 , fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +000086
bsalomonb3e3a952014-09-19 11:10:40 -070087 const GrPath* path() const { return fPath.get(); }
88
bsalomon932f8662014-11-24 06:47:48 -080089 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070090
bsalomon3e791242014-12-17 13:43:13 -080091 SkMatrix fViewMatrix;
92 bool fUseHWAA;
93 GrStencilSettings fStencil;
94 GrScissorState fScissor;
bsalomonb3e3a952014-09-19 11:10:40 -070095 private:
bsalomon3e791242014-12-17 13:43:13 -080096 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
97 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000098 };
99
cdalton6819df32014-10-15 13:43:48 -0700100 struct DrawPath : public Cmd {
101 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000102
bsalomonb3e3a952014-09-19 11:10:40 -0700103 const GrPath* path() const { return fPath.get(); }
104
bsalomon932f8662014-11-24 06:47:48 -0800105 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700106
joshualitt2c93efe2014-11-06 12:57:13 -0800107 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700108
109 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700110 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000111 };
112
cdalton6819df32014-10-15 13:43:48 -0700113 struct DrawPaths : public Cmd {
114 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700115
116 const GrPathRange* pathRange() const { return fPathRange.get(); }
117
bsalomon932f8662014-11-24 06:47:48 -0800118 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700119
cdalton3fc6a2f2014-11-13 11:54:20 -0800120 int fIndicesLocation;
cdalton55b24af2014-11-25 11:00:56 -0800121 PathIndexType fIndexType;
cdalton3fc6a2f2014-11-13 11:54:20 -0800122 int fTransformsLocation;
cdalton55b24af2014-11-25 11:00:56 -0800123 PathTransformType fTransformType;
124 int fCount;
joshualitt2c93efe2014-11-06 12:57:13 -0800125 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700126
127 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700128 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000129 };
130
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000131 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700132 struct Clear : public Cmd {
133 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
134
bsalomonb3e3a952014-09-19 11:10:40 -0700135 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000136
bsalomon932f8662014-11-24 06:47:48 -0800137 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700138
bsalomonb3e3a952014-09-19 11:10:40 -0700139 SkIRect fRect;
140 GrColor fColor;
141 bool fCanIgnoreRect;
142
143 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700144 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000145 };
146
joshualitt6db519c2014-10-29 08:48:18 -0700147 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
148 struct ClearStencilClip : public Cmd {
149 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
150
151 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
152
bsalomon932f8662014-11-24 06:47:48 -0800153 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700154
155 SkIRect fRect;
156 bool fInsideClip;
157
158 private:
159 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
160 };
161
cdalton6819df32014-10-15 13:43:48 -0700162 struct CopySurface : public Cmd {
163 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700164
165 GrSurface* dst() const { return fDst.get(); }
166 GrSurface* src() const { return fSrc.get(); }
167
bsalomon932f8662014-11-24 06:47:48 -0800168 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700169
bsalomonb3e3a952014-09-19 11:10:40 -0700170 SkIPoint fDstPoint;
171 SkIRect fSrcRect;
172
173 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700174 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
175 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000176 };
177
cdalton6819df32014-10-15 13:43:48 -0700178 struct SetState : public Cmd {
joshualitt56995b52014-12-11 15:44:02 -0800179 SetState(const GrDrawState& drawState, const GrGeometryProcessor* gp,
180 const GrPathProcessor* pp, const GrDrawTargetCaps& caps,
bsalomon3e791242014-12-17 13:43:13 -0800181 const GrScissorState& scissor, const GrDeviceCoordTexture* dstCopy,
joshualitt56995b52014-12-11 15:44:02 -0800182 GrGpu::DrawType drawType)
bsalomon932f8662014-11-24 06:47:48 -0800183 : Cmd(kSetState_Cmd)
joshualitt56995b52014-12-11 15:44:02 -0800184 , fState(drawState, gp, pp, caps, scissor, dstCopy, drawType) {}
cdalton6819df32014-10-15 13:43:48 -0700185
bsalomon932f8662014-11-24 06:47:48 -0800186 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700187
joshualittdafa4d02014-12-04 08:59:10 -0800188 GrOptDrawState fState;
bsalomonf0480b12014-07-02 12:11:24 -0700189 };
190
cdalton6819df32014-10-15 13:43:48 -0700191 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
192 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
193
bsalomon371bcbc2014-12-01 08:19:34 -0800194 void onReset() SK_OVERRIDE;
195 void onFlush() SK_OVERRIDE;
196
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000197 // overrides from GrDrawTarget
bsalomonae59b772014-11-19 08:23:49 -0800198 void onDraw(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800199 const GrGeometryProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800200 const DrawInfo&,
bsalomon3e791242014-12-17 13:43:13 -0800201 const GrScissorState&,
joshualitt9176e2c2014-11-20 07:28:52 -0800202 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800203 void onDrawRect(GrDrawState*,
joshualitt2e3b3e32014-12-09 13:31:14 -0800204 GrColor,
joshualitt8059eb92014-12-29 15:10:07 -0800205 const SkMatrix& viewMatrix,
bsalomonae59b772014-11-19 08:23:49 -0800206 const SkRect& rect,
207 const SkRect* localRect,
208 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000209
bsalomonae59b772014-11-19 08:23:49 -0800210 void onStencilPath(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800211 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800212 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800213 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800214 const GrStencilSettings&) SK_OVERRIDE;
215 void onDrawPath(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800216 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800217 const GrPath*,
bsalomon3e791242014-12-17 13:43:13 -0800218 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800219 const GrStencilSettings&,
220 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
221 void onDrawPaths(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800222 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800223 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800224 const void* indices,
225 PathIndexType,
226 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800227 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800228 int count,
bsalomon3e791242014-12-17 13:43:13 -0800229 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800230 const GrStencilSettings&,
231 const GrDeviceCoordTexture*) SK_OVERRIDE;
232 void onClear(const SkIRect* rect,
233 GrColor color,
234 bool canIgnoreRect,
235 GrRenderTarget* renderTarget) SK_OVERRIDE;
bsalomonf90a02b2014-11-26 12:28:00 -0800236 bool onCopySurface(GrSurface* dst,
237 GrSurface* src,
238 const SkIRect& srcRect,
239 const SkIPoint& dstPoint) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000240
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000241 // Attempts to concat instances from info onto the previous draw. info must represent an
242 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt54e0c122014-11-19 09:38:51 -0800243 int concatInstancedDraw(const GrDrawState&, const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000244
bsalomonae59b772014-11-19 08:23:49 -0800245 // Determines whether the current draw operation requires a new GrOptDrawState and if so
246 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
247 // recorded.
248 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
joshualitt56995b52014-12-11 15:44:02 -0800249 const GrGeometryProcessor*,
250 const GrPathProcessor*,
bsalomonae59b772014-11-19 08:23:49 -0800251 GrGpu::DrawType,
bsalomon3e791242014-12-17 13:43:13 -0800252 const GrScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800253 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700254 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700255 void recordClipIfNecessary();
256 // Records any trace markers for a command after adding it to the buffer.
257 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700258
mtklein72c9faa2015-01-09 10:06:39 -0800259 bool isIssued(uint32_t drawID) SK_OVERRIDE { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000260
bsalomon@google.com116ad842013-04-09 15:38:19 +0000261 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000262 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800263 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton55b24af2014-11-25 11:00:56 -0800264 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
265 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000266 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000267
bsalomonae59b772014-11-19 08:23:49 -0800268 CmdBuffer fCmdBuffer;
joshualitt9b989322014-12-15 14:16:27 -0800269 GrOptDrawState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800270 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
cdalton55b24af2014-11-25 11:00:56 -0800271 SkTDArray<char> fPathIndexBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800272 SkTDArray<float> fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800273 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000274
bsalomon371bcbc2014-12-01 08:19:34 -0800275 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000276};
277
278#endif