blob: 9d9f23cd1f95d9269ffea7344fcb9d039dce0937 [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
11#include "GrDrawTarget.h"
joshualittd53a8272014-11-10 16:03:14 -080012#include "GrGpu.h"
bsalomonb3e3a952014-09-19 11:10:40 -070013#include "GrIndexBuffer.h"
bsalomonae59b772014-11-19 08:23:49 -080014#include "GrOptDrawState.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000015#include "GrPath.h"
bsalomonb3e3a952014-09-19 11:10:40 -070016#include "GrPathRange.h"
bsalomonae59b772014-11-19 08:23:49 -080017#include "GrRenderTarget.h"
bsalomonb3e3a952014-09-19 11:10:40 -070018#include "GrSurface.h"
cdalton6819df32014-10-15 13:43:48 -070019#include "GrTRecorder.h"
bsalomonb3e3a952014-09-19 11:10:40 -070020#include "GrVertexBuffer.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000021
robertphillips@google.com641f8b12012-07-31 19:15:58 +000022#include "SkClipStack.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000023#include "SkTemplates.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000024#include "SkTypes.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000025
bsalomon@google.com1c13c962011-02-14 16:51:21 +000026class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000027class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000028
bsalomon@google.com1c13c962011-02-14 16:51:21 +000029/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000030 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
31 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
32 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
33 * references to the buffers. It is the callers responsibility to ensure that the data is still
34 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
35 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
36 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
37 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000038 */
joshualitt6db519c2014-10-29 08:48:18 -070039class GrInOrderDrawBuffer : public GrClipTarget {
reed@google.comac10a2d2010-12-22 21:39:39 +000040public:
41
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000042 /**
43 * Creates a GrInOrderDrawBuffer
44 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000045 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000046 * @param vertexPool pool where vertices for queued draws will be saved when
47 * the vertex source is either reserved or array.
48 * @param indexPool pool where indices for queued draws will be saved when
49 * the index source is either reserved or array.
50 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000051 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000052 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000053 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000054
55 virtual ~GrInOrderDrawBuffer();
56
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000057 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000058 * Empties the draw buffer of any queued up draws. This must not be called while inside an
59 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000060 */
61 void reset();
62
63 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000064 * This plays the queued up draws to its GrGpu target. It also resets this object (i.e. flushing
bsalomon@google.com55e4a202013-01-11 13:54:21 +000065 * is destructive). This buffer must not have an active reserved vertex or index source. Any
66 * reserved geometry on the target will be finalized because it's geometry source will be pushed
67 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000068 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000069 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000070
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000071 // tracking for draws
72 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
73
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000074 // overrides from GrDrawTarget
joshualitt9853cce2014-11-17 14:22:48 -080075 virtual bool geometryHints(size_t vertexStride,
76 int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000077 int* indexCount) const SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -070078
joshualitta7024152014-11-03 14:16:35 -080079 virtual bool copySurface(GrSurface* dst,
80 GrSurface* src,
81 const SkIRect& srcRect,
82 const SkIPoint& dstPoint) SK_OVERRIDE;
83
joshualitt9853cce2014-11-17 14:22:48 -080084 virtual bool canCopySurface(const GrSurface* dst,
85 const GrSurface* src,
joshualitta7024152014-11-03 14:16:35 -080086 const SkIRect& srcRect,
87 const SkIPoint& dstPoint) SK_OVERRIDE;
88
joshualitt6db519c2014-10-29 08:48:18 -070089 virtual void clearStencilClip(const SkIRect& rect,
90 bool insideClip,
91 GrRenderTarget* renderTarget) SK_OVERRIDE;
92
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +000093 virtual void discard(GrRenderTarget*) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000094
bsalomonf2703d82014-10-28 14:33:06 -070095 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrSurfaceDesc* desc) SK_OVERRIDE;
bsalomon@google.comeb851172013-04-15 13:51:00 +000096
reed@google.comac10a2d2010-12-22 21:39:39 +000097private:
joshualitt2c93efe2014-11-06 12:57:13 -080098 typedef GrClipMaskManager::ScissorState ScissorState;
cdalton6819df32014-10-15 13:43:48 -070099 enum {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000100 kDraw_Cmd = 1,
101 kStencilPath_Cmd = 2,
102 kSetState_Cmd = 3,
joshualitt2c93efe2014-11-06 12:57:13 -0800103 kClear_Cmd = 4,
104 kCopySurface_Cmd = 5,
105 kDrawPath_Cmd = 6,
106 kDrawPaths_Cmd = 7,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000107 };
108
cdalton6819df32014-10-15 13:43:48 -0700109 struct Cmd : ::SkNoncopyable {
110 Cmd(uint8_t type) : fType(type) {}
111 virtual ~Cmd() {}
112
cdalton3fc6a2f2014-11-13 11:54:20 -0800113 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) = 0;
cdalton6819df32014-10-15 13:43:48 -0700114
115 uint8_t fType;
116 };
117
118 struct Draw : public Cmd {
joshualitt54e0c122014-11-19 09:38:51 -0800119 Draw(const DrawInfo& info) : Cmd(kDraw_Cmd), fInfo(info) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700120
bsalomon932f8662014-11-24 06:47:48 -0800121 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700122
joshualitt2c93efe2014-11-06 12:57:13 -0800123 DrawInfo fInfo;
reed@google.comac10a2d2010-12-22 21:39:39 +0000124 };
125
cdalton6819df32014-10-15 13:43:48 -0700126 struct StencilPath : public Cmd {
127 StencilPath(const GrPath* path) : Cmd(kStencilPath_Cmd), fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000128
bsalomonb3e3a952014-09-19 11:10:40 -0700129 const GrPath* path() const { return fPath.get(); }
130
bsalomon932f8662014-11-24 06:47:48 -0800131 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700132
joshualitt2c93efe2014-11-06 12:57:13 -0800133 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700134
135 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700136 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000137 };
138
cdalton6819df32014-10-15 13:43:48 -0700139 struct DrawPath : public Cmd {
140 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000141
bsalomonb3e3a952014-09-19 11:10:40 -0700142 const GrPath* path() const { return fPath.get(); }
143
bsalomon932f8662014-11-24 06:47:48 -0800144 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700145
joshualitt2c93efe2014-11-06 12:57:13 -0800146 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700147
148 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700149 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000150 };
151
cdalton6819df32014-10-15 13:43:48 -0700152 struct DrawPaths : public Cmd {
153 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700154
155 const GrPathRange* pathRange() const { return fPathRange.get(); }
156
bsalomon932f8662014-11-24 06:47:48 -0800157 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700158
cdalton3fc6a2f2014-11-13 11:54:20 -0800159 int fIndicesLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800160 size_t fCount;
cdalton3fc6a2f2014-11-13 11:54:20 -0800161 int fTransformsLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800162 PathTransformType fTransformsType;
joshualitt2c93efe2014-11-06 12:57:13 -0800163 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700164
165 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700166 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000167 };
168
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000169 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700170 struct Clear : public Cmd {
171 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
172
bsalomonb3e3a952014-09-19 11:10:40 -0700173 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000174
bsalomon932f8662014-11-24 06:47:48 -0800175 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700176
bsalomonb3e3a952014-09-19 11:10:40 -0700177 SkIRect fRect;
178 GrColor fColor;
179 bool fCanIgnoreRect;
180
181 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700182 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000183 };
184
joshualitt6db519c2014-10-29 08:48:18 -0700185 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
186 struct ClearStencilClip : public Cmd {
187 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
188
189 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
190
bsalomon932f8662014-11-24 06:47:48 -0800191 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
joshualitt6db519c2014-10-29 08:48:18 -0700192
193 SkIRect fRect;
194 bool fInsideClip;
195
196 private:
197 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
198 };
199
cdalton6819df32014-10-15 13:43:48 -0700200 struct CopySurface : public Cmd {
201 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700202
203 GrSurface* dst() const { return fDst.get(); }
204 GrSurface* src() const { return fSrc.get(); }
205
bsalomon932f8662014-11-24 06:47:48 -0800206 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700207
bsalomonb3e3a952014-09-19 11:10:40 -0700208 SkIPoint fDstPoint;
209 SkIRect fSrcRect;
210
211 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700212 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
213 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000214 };
215
cdalton6819df32014-10-15 13:43:48 -0700216 struct SetState : public Cmd {
bsalomon932f8662014-11-24 06:47:48 -0800217 SetState(const GrDrawState& drawState, GrGpu* gpu, const ScissorState& scissor,
218 const GrDeviceCoordTexture* dstCopy, GrGpu::DrawType drawType)
219 : Cmd(kSetState_Cmd)
220 , fState(drawState, gpu, scissor, dstCopy, drawType) {}
cdalton6819df32014-10-15 13:43:48 -0700221
bsalomon932f8662014-11-24 06:47:48 -0800222 void execute(GrInOrderDrawBuffer*, const GrOptDrawState*) SK_OVERRIDE;
cdalton6819df32014-10-15 13:43:48 -0700223
bsalomon932f8662014-11-24 06:47:48 -0800224 const GrOptDrawState fState;
225 GrGpu::DrawType fDrawType;
bsalomonf0480b12014-07-02 12:11:24 -0700226 };
227
cdalton6819df32014-10-15 13:43:48 -0700228 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
229 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
230
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000231 // overrides from GrDrawTarget
bsalomonae59b772014-11-19 08:23:49 -0800232 void onDraw(const GrDrawState&,
233 const DrawInfo&,
joshualitt9176e2c2014-11-20 07:28:52 -0800234 const ScissorState&,
235 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800236 void onDrawRect(GrDrawState*,
237 const SkRect& rect,
238 const SkRect* localRect,
239 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000240
bsalomonae59b772014-11-19 08:23:49 -0800241 void onStencilPath(const GrDrawState&,
242 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800243 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800244 const GrStencilSettings&) SK_OVERRIDE;
245 void onDrawPath(const GrDrawState&,
246 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800247 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800248 const GrStencilSettings&,
249 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
250 void onDrawPaths(const GrDrawState&,
251 const GrPathRange*,
252 const uint32_t indices[],
253 int count,
254 const float transforms[],
255 PathTransformType,
joshualitt54e0c122014-11-19 09:38:51 -0800256 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800257 const GrStencilSettings&,
258 const GrDeviceCoordTexture*) SK_OVERRIDE;
259 void onClear(const SkIRect* rect,
260 GrColor color,
261 bool canIgnoreRect,
262 GrRenderTarget* renderTarget) SK_OVERRIDE;
263 void setDrawBuffers(DrawInfo*) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000264
bsalomonae59b772014-11-19 08:23:49 -0800265 bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
266 bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
267 void releaseReservedVertexSpace() SK_OVERRIDE;
268 void releaseReservedIndexSpace() SK_OVERRIDE;
269 void geometrySourceWillPush() SK_OVERRIDE;
270 void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
271 void willReserveVertexAndIndexSpace(int vertexCount,
272 size_t vertexStride,
273 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000274
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000275 // Attempts to concat instances from info onto the previous draw. info must represent an
276 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt54e0c122014-11-19 09:38:51 -0800277 int concatInstancedDraw(const GrDrawState&, const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000278
bsalomonae59b772014-11-19 08:23:49 -0800279 // Determines whether the current draw operation requires a new GrOptDrawState and if so
280 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
281 // recorded.
282 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
283 GrGpu::DrawType,
joshualitt54e0c122014-11-19 09:38:51 -0800284 const GrClipMaskManager::ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800285 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700286 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700287 void recordClipIfNecessary();
288 // Records any trace markers for a command after adding it to the buffer.
289 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700290
291 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000292
bsalomon@google.com116ad842013-04-09 15:38:19 +0000293 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000294 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800295 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton3fc6a2f2014-11-13 11:54:20 -0800296 kPathIdxBufferMinReserve = 64,
297 kPathXformBufferMinReserve = 2 * kPathIdxBufferMinReserve,
cdalton6819df32014-10-15 13:43:48 -0700298 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000299 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000300
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000301 struct GeometryPoolState {
bsalomonb3e3a952014-09-19 11:10:40 -0700302 const GrVertexBuffer* fPoolVertexBuffer;
303 int fPoolStartVertex;
304 const GrIndexBuffer* fPoolIndexBuffer;
305 int fPoolStartIndex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000306 // caller may conservatively over reserve vertices / indices.
307 // we release unused space back to allocator if possible
308 // can only do this if there isn't an intervening pushGeometrySource()
bsalomonb3e3a952014-09-19 11:10:40 -0700309 size_t fUsedPoolVertexBytes;
310 size_t fUsedPoolIndexBytes;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000311 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000312
bsalomonb3e3a952014-09-19 11:10:40 -0700313 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000314
bsalomonae59b772014-11-19 08:23:49 -0800315 CmdBuffer fCmdBuffer;
bsalomon932f8662014-11-24 06:47:48 -0800316 const GrOptDrawState* fPrevState;
bsalomonae59b772014-11-19 08:23:49 -0800317 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
318 GrGpu* fDstGpu;
319 GrVertexBufferAllocPool& fVertexPool;
320 GrIndexBufferAllocPool& fIndexPool;
321 SkTDArray<uint32_t> fPathIndexBuffer;
322 SkTDArray<float> fPathTransformBuffer;
323 GeoPoolStateStack fGeoPoolStateStack;
324 bool fFlushing;
325 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000326
joshualitt6db519c2014-10-29 08:48:18 -0700327 typedef GrClipTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000328};
329
330#endif