blob: 532842a3b40636b4a1677a97860d612b84af4182 [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
bsalomonf90a02b2014-11-26 12:28:00 -080045 DrawToken getCurrentDrawToken() { 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:
joshualitt2c93efe2014-11-06 12:57:13 -080054 typedef GrClipMaskManager::ScissorState ScissorState;
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
cdalton6819df32014-10-15 13:43:48 -070065 struct Cmd : ::SkNoncopyable {
66 Cmd(uint8_t type) : fType(type) {}
67 virtual ~Cmd() {}
68
cdalton3fc6a2f2014-11-13 11:54:20 -080069 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) = 0;
cdalton6819df32014-10-15 13:43:48 -070070
71 uint8_t fType;
72 };
73
74 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -080075 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -070076
bsalomon932f8662014-11-24 06:47:48 -080077 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070078
joshualitt2c93efe2014-11-06 12:57:13 -080079 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +000080 };
81
cdalton6819df32014-10-15 13:43:48 -070082 struct StencilPath : public Cmd {
83 StencilPath(const GrPath* path) : Cmd(kStencilPath_Cmd), fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +000084
bsalomonb3e3a952014-09-19 11:10:40 -070085 const GrPath* path() const { return fPath.get(); }
86
bsalomon932f8662014-11-24 06:47:48 -080087 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -070088
joshualitt2c93efe2014-11-06 12:57:13 -080089 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -070090
91 private:
bsalomonbcf0a522014-10-08 08:40:09 -070092 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000093 };
94
cdalton6819df32014-10-15 13:43:48 -070095 struct DrawPath : public Cmd {
96 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000097
bsalomonb3e3a952014-09-19 11:10:40 -070098 const GrPath* path() const { return fPath.get(); }
99
bsalomon932f8662014-11-24 06:47:48 -0800100 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700101
joshualitt2c93efe2014-11-06 12:57:13 -0800102 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700103
104 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700105 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000106 };
107
cdalton6819df32014-10-15 13:43:48 -0700108 struct DrawPaths : public Cmd {
109 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700110
111 const GrPathRange* pathRange() const { return fPathRange.get(); }
112
bsalomon932f8662014-11-24 06:47:48 -0800113 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700114
cdalton3fc6a2f2014-11-13 11:54:20 -0800115 int fIndicesLocation;
cdalton55b24af2014-11-25 11:00:56 -0800116 PathIndexType fIndexType;
cdalton3fc6a2f2014-11-13 11:54:20 -0800117 int fTransformsLocation;
cdalton55b24af2014-11-25 11:00:56 -0800118 PathTransformType fTransformType;
119 int fCount;
joshualitt2c93efe2014-11-06 12:57:13 -0800120 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700121
122 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700123 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000124 };
125
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000126 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700127 struct Clear : public Cmd {
128 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
129
bsalomonb3e3a952014-09-19 11:10:40 -0700130 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000131
bsalomon932f8662014-11-24 06:47:48 -0800132 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700133
bsalomonb3e3a952014-09-19 11:10:40 -0700134 SkIRect fRect;
135 GrColor fColor;
136 bool fCanIgnoreRect;
137
138 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700139 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000140 };
141
joshualitt6db519c2014-10-29 08:48:18 -0700142 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
143 struct ClearStencilClip : public Cmd {
144 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
145
146 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
147
bsalomon932f8662014-11-24 06:47:48 -0800148 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700149
150 SkIRect fRect;
151 bool fInsideClip;
152
153 private:
154 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
155 };
156
cdalton6819df32014-10-15 13:43:48 -0700157 struct CopySurface : public Cmd {
158 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700159
160 GrSurface* dst() const { return fDst.get(); }
161 GrSurface* src() const { return fSrc.get(); }
162
bsalomon932f8662014-11-24 06:47:48 -0800163 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700164
bsalomonb3e3a952014-09-19 11:10:40 -0700165 SkIPoint fDstPoint;
166 SkIRect fSrcRect;
167
168 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700169 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
170 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000171 };
172
cdalton6819df32014-10-15 13:43:48 -0700173 struct SetState : public Cmd {
bsalomon932f8662014-11-24 06:47:48 -0800174 SetState(const GrDrawState& drawState, GrGpu* gpu, const ScissorState& scissor,
175 const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType drawType)
176 : Cmd(kSetState_Cmd)
177 , fState(drawState, gpu, scissor, dstCopy, drawType) {}
cdalton6819df32014-10-15 13:43:48 -0700178
bsalomon932f8662014-11-24 06:47:48 -0800179 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700180
bsalomon932f8662014-11-24 06:47:48 -0800181 const GrOptDrawState fState;
182 GrGpu::DrawType fDrawType;
bsalomonf0480b12014-07-02 12:11:24 -0700183 };
184
cdalton6819df32014-10-15 13:43:48 -0700185 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
186 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
187
bsalomon371bcbc2014-12-01 08:19:34 -0800188 void onReset() SK_OVERRIDE;
189 void onFlush() SK_OVERRIDE;
190
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000191 // overrides from GrDrawTarget
bsalomonae59b772014-11-19 08:23:49 -0800192 void onDraw(const GrDrawState&,
193 const DrawInfo&,
joshualitt9176e2c2014-11-20 07:28:52 -0800194 const ScissorState&,
195 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800196 void onDrawRect(GrDrawState*,
197 const SkRect& rect,
198 const SkRect* localRect,
199 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000200
bsalomonae59b772014-11-19 08:23:49 -0800201 void onStencilPath(const GrDrawState&,
202 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800203 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800204 const GrStencilSettings&) SK_OVERRIDE;
205 void onDrawPath(const GrDrawState&,
206 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800207 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800208 const GrStencilSettings&,
209 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
210 void onDrawPaths(const GrDrawState&,
211 const GrPathRange*,
cdalton55b24af2014-11-25 11:00:56 -0800212 const void* indices,
213 PathIndexType,
214 const float transformValues[],
bsalomonae59b772014-11-19 08:23:49 -0800215 PathTransformType,
cdalton55b24af2014-11-25 11:00:56 -0800216 int count,
joshualitt54e0c122014-11-19 09:38:51 -0800217 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800218 const GrStencilSettings&,
219 const GrDeviceCoordTexture*) SK_OVERRIDE;
220 void onClear(const SkIRect* rect,
221 GrColor color,
222 bool canIgnoreRect,
223 GrRenderTarget* renderTarget) SK_OVERRIDE;
bsalomonf90a02b2014-11-26 12:28:00 -0800224 bool onCopySurface(GrSurface* dst,
225 GrSurface* src,
226 const SkIRect& srcRect,
227 const SkIPoint& dstPoint) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000228
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000229 // Attempts to concat instances from info onto the previous draw. info must represent an
230 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt54e0c122014-11-19 09:38:51 -0800231 int concatInstancedDraw(const GrDrawState&, const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000232
bsalomonae59b772014-11-19 08:23:49 -0800233 // Determines whether the current draw operation requires a new GrOptDrawState and if so
234 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
235 // recorded.
236 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
237 GrGpu::DrawType,
joshualitt54e0c122014-11-19 09:38:51 -0800238 const GrClipMaskManager::ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800239 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700240 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700241 void recordClipIfNecessary();
242 // Records any trace markers for a command after adding it to the buffer.
243 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700244
245 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000246
bsalomon@google.com116ad842013-04-09 15:38:19 +0000247 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000248 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800249 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton55b24af2014-11-25 11:00:56 -0800250 kPathIdxBufferMinReserve = 2 * 64, // 64 uint16_t's
251 kPathXformBufferMinReserve = 2 * 64, // 64 two-float transforms
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000252 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000253
bsalomonae59b772014-11-19 08:23:49 -0800254 CmdBuffer fCmdBuffer;
bsalomon932f8662014-11-24 06:47:48 -0800255 const GrOptDrawState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800256 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
cdalton55b24af2014-11-25 11:00:56 -0800257 SkTDArray<char> fPathIndexBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800258 SkTDArray<float> fPathTransformBuffer;
bsalomonae59b772014-11-19 08:23:49 -0800259 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000260
bsalomon371bcbc2014-12-01 08:19:34 -0800261 typedef GrFlushToGpuDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000262};
263
264#endif