blob: ba5e79073e342007fa9a4337b67c06bb4f685ee5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@google.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
epoger@google.comec3ed6a2011-07-28 14:26:00 +000010
reed@google.comac10a2d2010-12-22 21:39:39 +000011#ifndef GrInOrderDrawBuffer_DEFINED
12#define GrInOrderDrawBuffer_DEFINED
13
14#include "GrDrawTarget.h"
15#include "GrAllocPool.h"
16#include "GrAllocator.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000017#include "GrPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000018
robertphillips@google.com641f8b12012-07-31 19:15:58 +000019#include "SkClipStack.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000020#include "SkStrokeRec.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000021#include "SkTemplates.h"
22
bsalomon@google.com471d4712011-08-23 15:45:25 +000023class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000024class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000025class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000026
bsalomon@google.com1c13c962011-02-14 16:51:21 +000027/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000028 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
29 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
30 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
31 * references to the buffers. It is the callers responsibility to ensure that the data is still
32 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
33 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
34 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
35 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000036 */
reed@google.comac10a2d2010-12-22 21:39:39 +000037class GrInOrderDrawBuffer : public GrDrawTarget {
38public:
39
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000040 /**
41 * Creates a GrInOrderDrawBuffer
42 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000043 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000044 * @param vertexPool pool where vertices for queued draws will be saved when
45 * the vertex source is either reserved or array.
46 * @param indexPool pool where indices for queued draws will be saved when
47 * the index source is either reserved or array.
48 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000049 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000050 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000051 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000052
53 virtual ~GrInOrderDrawBuffer();
54
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000055 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000056 * Empties the draw buffer of any queued up draws. This must not be called while inside an
57 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000058 */
59 void reset();
60
61 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000062 * 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 +000063 * is destructive). This buffer must not have an active reserved vertex or index source. Any
64 * reserved geometry on the target will be finalized because it's geometry source will be pushed
65 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000066 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000067 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000068
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000069 // tracking for draws
70 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
71
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000072 // overrides from GrDrawTarget
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000073 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000074 int* indexCount) const SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000075 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000076 GrColor color,
77 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000078
bsalomon@google.comeb851172013-04-15 13:51:00 +000079 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
80
bsalomon@google.com97805382012-03-13 14:32:07 +000081protected:
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +000082 virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
83
reed@google.comac10a2d2010-12-22 21:39:39 +000084private:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000085 enum Cmd {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000086 kDraw_Cmd = 1,
87 kStencilPath_Cmd = 2,
88 kSetState_Cmd = 3,
89 kSetClip_Cmd = 4,
90 kClear_Cmd = 5,
bsalomon@google.com116ad842013-04-09 15:38:19 +000091 kCopySurface_Cmd = 6,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000092 };
93
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000094 class DrawRecord : public DrawInfo {
95 public:
96 DrawRecord(const DrawInfo& info) : DrawInfo(info) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000097 const GrVertexBuffer* fVertexBuffer;
98 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000099 };
100
bsalomon@google.com116ad842013-04-09 15:38:19 +0000101 struct StencilPath : GrNoncopyable {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000102 StencilPath();
103
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000104 SkAutoTUnref<const GrPath> fPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000105 SkStrokeRec fStroke;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000106 SkPath::FillType fFill;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000107 };
108
bsalomon@google.com116ad842013-04-09 15:38:19 +0000109 struct Clear : GrNoncopyable {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000110 Clear() : fRenderTarget(NULL) {}
111 ~Clear() { GrSafeUnref(fRenderTarget); }
112
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000113 SkIRect fRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000114 GrColor fColor;
115 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000116 };
117
bsalomon@google.com116ad842013-04-09 15:38:19 +0000118 struct CopySurface : GrNoncopyable {
119 SkAutoTUnref<GrSurface> fDst;
120 SkAutoTUnref<GrSurface> fSrc;
121 SkIRect fSrcRect;
122 SkIPoint fDstPoint;
123 };
124
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000125 // overrides from GrDrawTarget
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000126 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000127 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000128 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000129 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000130 const SkMatrix* localMatrix) SK_OVERRIDE;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000131 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType) SK_OVERRIDE;
jvanverth@google.coma6338982013-01-31 21:34:25 +0000132 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000133 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000134 void** vertices) SK_OVERRIDE;
135 virtual bool onReserveIndexSpace(int indexCount,
136 void** indices) SK_OVERRIDE;
137 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
138 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000139 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000140 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000141 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000142 int indexCount) SK_OVERRIDE;
143 virtual void releaseVertexArray() SK_OVERRIDE;
144 virtual void releaseIndexArray() SK_OVERRIDE;
145 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000146 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000147 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000148 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000149 virtual bool onCopySurface(GrSurface* dst,
150 GrSurface* src,
151 const SkIRect& srcRect,
152 const SkIPoint& dstPoint) SK_OVERRIDE;
153 virtual bool onCanCopySurface(GrSurface* dst,
154 GrSurface* src,
155 const SkIRect& srcRect,
156 const SkIPoint& dstPoint) SK_OVERRIDE;
157
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000158 bool quickInsideClip(const SkRect& devBounds);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000159
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000160 // Attempts to concat instances from info onto the previous draw. info must represent an
161 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
162 int concatInstancedDraw(const DrawInfo& info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000163
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000164 // we lazily record state and clip changes in order to skip clips and states that have no
165 // effect.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000166 bool needsNewState() const;
167 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000168
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000169 // these functions record a command
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000170 void recordState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000171 void recordClip();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000172 DrawRecord* recordDraw(const DrawInfo&);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000173 StencilPath* recordStencilPath();
174 Clear* recordClear();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000175 CopySurface* recordCopySurface();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000176
bsalomon@google.com116ad842013-04-09 15:38:19 +0000177 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000178 enum {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000179 kCmdPreallocCnt = 32,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000180 kDrawPreallocCnt = 8,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000181 kStencilPathPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000182 kStatePreallocCnt = 8,
183 kClipPreallocCnt = 8,
184 kClearPreallocCnt = 4,
185 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000186 kCopySurfacePreallocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000187 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000188
bsalomon@google.comca432082013-01-23 19:53:46 +0000189 SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000190 GrSTAllocator<kDrawPreallocCnt, DrawRecord> fDraws;
bsalomon@google.comca432082013-01-23 19:53:46 +0000191 GrSTAllocator<kStatePreallocCnt, StencilPath> fStencilPaths;
192 GrSTAllocator<kStatePreallocCnt, GrDrawState::DeferredState> fStates;
193 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000194 GrSTAllocator<kCopySurfacePreallocCnt, CopySurface> fCopySurfaces;
195 GrSTAllocator<kClipPreallocCnt, SkClipStack> fClips;
196 GrSTAllocator<kClipPreallocCnt, SkIPoint> fClipOrigins;
bsalomon@google.com97805382012-03-13 14:32:07 +0000197
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000198 GrDrawTarget* fDstGpu;
bsalomon@google.com97805382012-03-13 14:32:07 +0000199
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000200 bool fClipSet;
201
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000202 enum ClipProxyState {
203 kUnknown_ClipProxyState,
204 kValid_ClipProxyState,
205 kInvalid_ClipProxyState
206 };
207 ClipProxyState fClipProxyState;
208 SkRect fClipProxy;
209
bsalomon@google.com934c5702012-03-20 21:17:58 +0000210 GrVertexBufferAllocPool& fVertexPool;
211
212 GrIndexBufferAllocPool& fIndexPool;
213
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000214 struct GeometryPoolState {
215 const GrVertexBuffer* fPoolVertexBuffer;
216 int fPoolStartVertex;
217 const GrIndexBuffer* fPoolIndexBuffer;
218 int fPoolStartIndex;
219 // caller may conservatively over reserve vertices / indices.
220 // we release unused space back to allocator if possible
221 // can only do this if there isn't an intervening pushGeometrySource()
222 size_t fUsedPoolVertexBytes;
223 size_t fUsedPoolIndexBytes;
224 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000225 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000226
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000227 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
228
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000229 bool fFlushing;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000230 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000231
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000232 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000233};
234
235#endif