blob: 58b239baa481ca23dbf87e77aa12a99344da3e70 [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 {
joshualitt7eb8c7b2014-11-18 14:24:27 -0800119 Draw(const DrawInfo& info, const ScissorState& scissorState)
cdalton6819df32014-10-15 13:43:48 -0700120 : Cmd(kDraw_Cmd)
121 , fInfo(info)
joshualitt7eb8c7b2014-11-18 14:24:27 -0800122 , fScissorState(scissorState){}
bsalomonb3e3a952014-09-19 11:10:40 -0700123
cdalton3fc6a2f2014-11-13 11:54:20 -0800124 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700125
joshualitt2c93efe2014-11-06 12:57:13 -0800126 DrawInfo fInfo;
127 ScissorState fScissorState;
reed@google.comac10a2d2010-12-22 21:39:39 +0000128 };
129
cdalton6819df32014-10-15 13:43:48 -0700130 struct StencilPath : public Cmd {
131 StencilPath(const GrPath* path) : Cmd(kStencilPath_Cmd), fPath(path) {}
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000132
bsalomonb3e3a952014-09-19 11:10:40 -0700133 const GrPath* path() const { return fPath.get(); }
134
cdalton3fc6a2f2014-11-13 11:54:20 -0800135 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700136
joshualitt2c93efe2014-11-06 12:57:13 -0800137 ScissorState fScissorState;
138 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700139
140 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700141 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000142 };
143
cdalton6819df32014-10-15 13:43:48 -0700144 struct DrawPath : public Cmd {
145 DrawPath(const GrPath* path) : Cmd(kDrawPath_Cmd), fPath(path) {}
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000146
bsalomonb3e3a952014-09-19 11:10:40 -0700147 const GrPath* path() const { return fPath.get(); }
148
cdalton3fc6a2f2014-11-13 11:54:20 -0800149 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700150
joshualitt2c93efe2014-11-06 12:57:13 -0800151 GrDeviceCoordTexture fDstCopy;
152 ScissorState fScissorState;
153 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700154
155 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700156 GrPendingIOResource<const GrPath, kRead_GrIOType> fPath;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000157 };
158
cdalton6819df32014-10-15 13:43:48 -0700159 struct DrawPaths : public Cmd {
160 DrawPaths(const GrPathRange* pathRange) : Cmd(kDrawPaths_Cmd), fPathRange(pathRange) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700161
162 const GrPathRange* pathRange() const { return fPathRange.get(); }
163
cdalton3fc6a2f2014-11-13 11:54:20 -0800164 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700165
cdalton3fc6a2f2014-11-13 11:54:20 -0800166 int fIndicesLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800167 size_t fCount;
cdalton3fc6a2f2014-11-13 11:54:20 -0800168 int fTransformsLocation;
joshualitt2c93efe2014-11-06 12:57:13 -0800169 PathTransformType fTransformsType;
170 GrDeviceCoordTexture fDstCopy;
171 ScissorState fScissorState;
172 GrStencilSettings fStencilSettings;
bsalomonb3e3a952014-09-19 11:10:40 -0700173
174 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700175 GrPendingIOResource<const GrPathRange, kRead_GrIOType> fPathRange;
commit-bot@chromium.org9b62aa12014-03-25 11:59:40 +0000176 };
177
commit-bot@chromium.org28361fa2014-03-28 16:08:05 +0000178 // This is also used to record a discard by setting the color to GrColor_ILLEGAL
cdalton6819df32014-10-15 13:43:48 -0700179 struct Clear : public Cmd {
180 Clear(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
181
bsalomonb3e3a952014-09-19 11:10:40 -0700182 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000183
cdalton3fc6a2f2014-11-13 11:54:20 -0800184 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700185
bsalomonb3e3a952014-09-19 11:10:40 -0700186 SkIRect fRect;
187 GrColor fColor;
188 bool fCanIgnoreRect;
189
190 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700191 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000192 };
193
joshualitt6db519c2014-10-29 08:48:18 -0700194 // This command is ONLY used by the clip mask manager to clear the stencil clip bits
195 struct ClearStencilClip : public Cmd {
196 ClearStencilClip(GrRenderTarget* rt) : Cmd(kClear_Cmd), fRenderTarget(rt) {}
197
198 GrRenderTarget* renderTarget() const { return fRenderTarget.get(); }
199
cdalton3fc6a2f2014-11-13 11:54:20 -0800200 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
joshualitt6db519c2014-10-29 08:48:18 -0700201
202 SkIRect fRect;
203 bool fInsideClip;
204
205 private:
206 GrPendingIOResource<GrRenderTarget, kWrite_GrIOType> fRenderTarget;
207 };
208
cdalton6819df32014-10-15 13:43:48 -0700209 struct CopySurface : public Cmd {
210 CopySurface(GrSurface* dst, GrSurface* src) : Cmd(kCopySurface_Cmd), fDst(dst), fSrc(src) {}
bsalomonb3e3a952014-09-19 11:10:40 -0700211
212 GrSurface* dst() const { return fDst.get(); }
213 GrSurface* src() const { return fSrc.get(); }
214
cdalton3fc6a2f2014-11-13 11:54:20 -0800215 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700216
bsalomonb3e3a952014-09-19 11:10:40 -0700217 SkIPoint fDstPoint;
218 SkIRect fSrcRect;
219
220 private:
bsalomonbcf0a522014-10-08 08:40:09 -0700221 GrPendingIOResource<GrSurface, kWrite_GrIOType> fDst;
222 GrPendingIOResource<GrSurface, kRead_GrIOType> fSrc;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000223 };
224
cdalton6819df32014-10-15 13:43:48 -0700225 struct SetState : public Cmd {
bsalomonae59b772014-11-19 08:23:49 -0800226 SetState(const GrOptDrawState* state) : Cmd(kSetState_Cmd), fState(SkRef(state)) {}
cdalton6819df32014-10-15 13:43:48 -0700227
cdalton3fc6a2f2014-11-13 11:54:20 -0800228 virtual void execute(GrInOrderDrawBuffer*, const GrOptDrawState*);
cdalton6819df32014-10-15 13:43:48 -0700229
bsalomonae59b772014-11-19 08:23:49 -0800230 SkAutoTUnref<const GrOptDrawState> fState;
231 GrGpu::DrawType fDrawType;
232 GrDeviceCoordTexture fDstCopy;
bsalomonf0480b12014-07-02 12:11:24 -0700233 };
234
cdalton6819df32014-10-15 13:43:48 -0700235 typedef void* TCmdAlign; // This wouldn't be enough align if a command used long double.
236 typedef GrTRecorder<Cmd, TCmdAlign> CmdBuffer;
237
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000238 // overrides from GrDrawTarget
bsalomonae59b772014-11-19 08:23:49 -0800239 void onDraw(const GrDrawState&,
240 const DrawInfo&,
241 const GrClipMaskManager::ScissorState&) SK_OVERRIDE;
242 void onDrawRect(GrDrawState*,
243 const SkRect& rect,
244 const SkRect* localRect,
245 const SkMatrix* localMatrix) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000246
bsalomonae59b772014-11-19 08:23:49 -0800247 void onStencilPath(const GrDrawState&,
248 const GrPath*,
249 const GrClipMaskManager::ScissorState&,
250 const GrStencilSettings&) SK_OVERRIDE;
251 void onDrawPath(const GrDrawState&,
252 const GrPath*,
253 const GrClipMaskManager::ScissorState&,
254 const GrStencilSettings&,
255 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
256 void onDrawPaths(const GrDrawState&,
257 const GrPathRange*,
258 const uint32_t indices[],
259 int count,
260 const float transforms[],
261 PathTransformType,
262 const GrClipMaskManager::ScissorState&,
263 const GrStencilSettings&,
264 const GrDeviceCoordTexture*) SK_OVERRIDE;
265 void onClear(const SkIRect* rect,
266 GrColor color,
267 bool canIgnoreRect,
268 GrRenderTarget* renderTarget) SK_OVERRIDE;
269 void setDrawBuffers(DrawInfo*) SK_OVERRIDE;
commit-bot@chromium.org32184d82013-10-09 15:14:18 +0000270
bsalomonae59b772014-11-19 08:23:49 -0800271 bool onReserveVertexSpace(size_t vertexSize, int vertexCount, void** vertices) SK_OVERRIDE;
272 bool onReserveIndexSpace(int indexCount, void** indices) SK_OVERRIDE;
273 void releaseReservedVertexSpace() SK_OVERRIDE;
274 void releaseReservedIndexSpace() SK_OVERRIDE;
275 void geometrySourceWillPush() SK_OVERRIDE;
276 void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
277 void willReserveVertexAndIndexSpace(int vertexCount,
278 size_t vertexStride,
279 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000280
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000281 // Attempts to concat instances from info onto the previous draw. info must represent an
282 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
joshualitt9853cce2014-11-17 14:22:48 -0800283 int concatInstancedDraw(const GrDrawState&,
284 const DrawInfo&,
285 const GrClipMaskManager::ScissorState&);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000286
bsalomonae59b772014-11-19 08:23:49 -0800287 // Determines whether the current draw operation requires a new GrOptDrawState and if so
288 // records it. If the draw can be skipped false is returned and no new GrOptDrawState is
289 // recorded.
290 bool SK_WARN_UNUSED_RESULT recordStateAndShouldDraw(const GrDrawState&,
291 GrGpu::DrawType,
292 const GrDeviceCoordTexture*);
bsalomon838f62d2014-08-05 07:15:57 -0700293 // We lazily record clip changes in order to skip clips that have no effect.
cdalton6819df32014-10-15 13:43:48 -0700294 void recordClipIfNecessary();
295 // Records any trace markers for a command after adding it to the buffer.
296 void recordTraceMarkersIfNecessary();
bsalomonb3e3a952014-09-19 11:10:40 -0700297
298 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
bsalomon@google.com934c5702012-03-20 21:17:58 +0000299
bsalomon@google.com116ad842013-04-09 15:38:19 +0000300 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000301 enum {
cdaltonc4650ee2014-11-07 12:51:18 -0800302 kCmdBufferInitialSizeInBytes = 8 * 1024,
cdalton3fc6a2f2014-11-13 11:54:20 -0800303 kPathIdxBufferMinReserve = 64,
304 kPathXformBufferMinReserve = 2 * kPathIdxBufferMinReserve,
cdalton6819df32014-10-15 13:43:48 -0700305 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000306 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000307
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000308 struct GeometryPoolState {
bsalomonb3e3a952014-09-19 11:10:40 -0700309 const GrVertexBuffer* fPoolVertexBuffer;
310 int fPoolStartVertex;
311 const GrIndexBuffer* fPoolIndexBuffer;
312 int fPoolStartIndex;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000313 // caller may conservatively over reserve vertices / indices.
314 // we release unused space back to allocator if possible
315 // can only do this if there isn't an intervening pushGeometrySource()
bsalomonb3e3a952014-09-19 11:10:40 -0700316 size_t fUsedPoolVertexBytes;
317 size_t fUsedPoolIndexBytes;
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000318 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000319
bsalomonb3e3a952014-09-19 11:10:40 -0700320 typedef SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> GeoPoolStateStack;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000321
bsalomonae59b772014-11-19 08:23:49 -0800322 CmdBuffer fCmdBuffer;
323 SkAutoTUnref<const GrOptDrawState> fLastState;
324 SkTArray<GrTraceMarkerSet, false> fGpuCmdMarkers;
325 GrGpu* fDstGpu;
326 GrVertexBufferAllocPool& fVertexPool;
327 GrIndexBufferAllocPool& fIndexPool;
328 SkTDArray<uint32_t> fPathIndexBuffer;
329 SkTDArray<float> fPathTransformBuffer;
330 GeoPoolStateStack fGeoPoolStateStack;
331 bool fFlushing;
332 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000333
joshualitt6db519c2014-10-29 08:48:18 -0700334 typedef GrClipTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000335};
336
337#endif