blob: ed5cdd42e6501934be73d79a325cc1ba5e0ace85 [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
cdalton3fc6a2f2014-11-13 11:54:20 -0800121 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
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
cdalton3fc6a2f2014-11-13 11:54:20 -0800131 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
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
cdalton3fc6a2f2014-11-13 11:54:20 -0800144 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700145
joshualitt2c93efe2014-11-06 12:57:13 -0800146 GrDeviceCoordTexture fDstCopy;
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 GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000151 };
152
cdalton6819df32014-10-15 13:43:48 -0700153 struct DrawPaths : public Cmd {
154 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700155
156 const GrPathRange* pathRange() const { return fPathRange.get(); }
157
cdalton3fc6a2f2014-11-13 11:54:20 -0800158 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700159
cdalton3fc6a2f2014-11-13 11:54:20 -0800160 int fIndicesLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800161 size_t fCount;
cdalton3fc6a2f2014-11-13 11:54:20 -0800162 int fTransformsLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800163 PathTransformType fTransformsType;
164 GrDeviceCoordTexture fDstCopy;
joshualitt2c93efe2014-11-06 12:57:13 -0800165 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700166
167 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700168 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000169 };
170
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000171 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700172 struct Clear : public Cmd {
173 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
174
bsalomonb3e3a952014-09-19 11:10:40 -0700175 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000176
cdalton3fc6a2f2014-11-13 11:54:20 -0800177 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700178
bsalomonb3e3a952014-09-19 11:10:40 -0700179 SkIRect fRect;
180 GrColor fColor;
181 bool fCanIgnoreRect;
182
183 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700184 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000185 };
186
joshualitt6db519c2014-10-29 08:48:18 -0700187 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
188 struct ClearStencilClip : public Cmd {
189 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
190
191 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
192
cdalton3fc6a2f2014-11-13 11:54:20 -0800193 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
joshualitt6db519c2014-10-29 08:48:18 -0700194
195 SkIRect fRect;
196 bool fInsideClip;
197
198 private:
199 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
200 };
201
cdalton6819df32014-10-15 13:43:48 -0700202 struct CopySurface : public Cmd {
203 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700204
205 GrSurface* dst() const { return fDst.get(); }
206 GrSurface* src() const { return fSrc.get(); }
207
cdalton3fc6a2f2014-11-13 11:54:20 -0800208 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700209
bsalomonb3e3a952014-09-19 11:10:40 -0700210 SkIPoint fDstPoint;
211 SkIRect fSrcRect;
212
213 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700214 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
215 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000216 };
217
cdalton6819df32014-10-15 13:43:48 -0700218 struct SetState : public Cmd {
bsalomonae59b772014-11-19 08:23:49 -0800219 SetState(const GrOptDrawState* state) : Cmd(kSetState_Cmd), fState(SkRef(state)) {}
cdalton6819df32014-10-15 13:43:48 -0700220
cdalton3fc6a2f2014-11-13 11:54:20 -0800221 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700222
bsalomonae59b772014-11-19 08:23:49 -0800223 SkAutoTUnref<const GrOptDrawState> fState;
224 GrGpu::DrawType fDrawType;
225 GrDeviceCoordTexture fDstCopy;
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&,
joshualitt54e0c122014-11-19 09:38:51 -0800234 const ScissorState&) SK_OVERRIDE;
bsalomonae59b772014-11-19 08:23:49 -0800235 void onDrawRect(GrDrawState*,
236 const SkRect& rect,
237 const SkRect* localRect,
238 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000239
bsalomonae59b772014-11-19 08:23:49 -0800240 void onStencilPath(const GrDrawState&,
241 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800242 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800243 const GrStencilSettings&) SK_OVERRIDE;
244 void onDrawPath(const GrDrawState&,
245 const GrPath*,
joshualitt54e0c122014-11-19 09:38:51 -0800246 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800247 const GrStencilSettings&,
248 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
249 void onDrawPaths(const GrDrawState&,
250 const GrPathRange*,
251 const uint32_t indices[],
252 int count,
253 const float transforms[],
254 PathTransformType,
joshualitt54e0c122014-11-19 09:38:51 -0800255 const ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800256 const GrStencilSettings&,
257 const GrDeviceCoordTexture*) SK_OVERRIDE;
258 void onClear(const SkIRect* rect,
259 GrColor color,
260 bool canIgnoreRect,
261 GrRenderTarget* renderTarget) SK_OVERRIDE;
262 void setDrawBuffers(DrawInfo*) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000263
bsalomonae59b772014-11-19 08:23:49 -0800264 bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
265 bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
266 void releaseReservedVertexSpace() SK_OVERRIDE;
267 void releaseReservedIndexSpace() SK_OVERRIDE;
268 void geometrySourceWillPush() SK_OVERRIDE;
269 void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
270 void willReserveVertexAndIndexSpace(int vertexCount,
271 size_t vertexStride,
272 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000273
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000274 // Attempts to concat instances from info onto the previous draw. info must represent an
275 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt54e0c122014-11-19 09:38:51 -0800276 int concatInstancedDraw(const GrDrawState&, const DrawInfo&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000277
bsalomonae59b772014-11-19 08:23:49 -0800278 // Determines whether the current draw operation requires a new GrOptDrawState and if so
279 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
280 // recorded.
281 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
282 GrGpu::DrawType,
joshualitt54e0c122014-11-19 09:38:51 -0800283 const GrClipMaskManager::ScissorState&,
bsalomonae59b772014-11-19 08:23:49 -0800284 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700285 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700286 void recordClipIfNecessary();
287 // Records any trace markers for a command after adding it to the buffer.
288 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700289
290 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000291
bsalomon@google.com116ad842013-04-09 15:38:19 +0000292 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000293 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800294 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton3fc6a2f2014-11-13 11:54:20 -0800295 kPathIdxBufferMinReserve = 64,
296 kPathXformBufferMinReserve = 2 * kPathIdxBufferMinReserve,
cdalton6819df32014-10-15 13:43:48 -0700297 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000298 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000299
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000300 struct GeometryPoolState {
bsalomonb3e3a952014-09-19 11:10:40 -0700301 const GrVertexBuffer* fPoolVertexBuffer;
302 int fPoolStartVertex;
303 const GrIndexBuffer* fPoolIndexBuffer;
304 int fPoolStartIndex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000305 // caller may conservatively over reserve vertices / indices.
306 // we release unused space back to allocator if possible
307 // can only do this if there isn't an intervening pushGeometrySource()
bsalomonb3e3a952014-09-19 11:10:40 -0700308 size_t fUsedPoolVertexBytes;
309 size_t fUsedPoolIndexBytes;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000310 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000311
bsalomonb3e3a952014-09-19 11:10:40 -0700312 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000313
bsalomonae59b772014-11-19 08:23:49 -0800314 CmdBuffer fCmdBuffer;
315 SkAutoTUnref<const GrOptDrawState> fLastState;
316 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
317 GrGpu* fDstGpu;
318 GrVertexBufferAllocPool& fVertexPool;
319 GrIndexBufferAllocPool& fIndexPool;
320 SkTDArray<uint32_t> fPathIndexBuffer;
321 SkTDArray<float> fPathTransformBuffer;
322 GeoPoolStateStack fGeoPoolStateStack;
323 bool fFlushing;
324 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000325
joshualitt6db519c2014-10-29 08:48:18 -0700326 typedef GrClipTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000327};
328
329#endif