blob: b9e5ca87187c89bec1a6ae435c2f6f1911c09529 [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"
12#include "GrAllocPool.h"
13#include "GrAllocator.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000014#include "GrPath.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000015
robertphillips@google.com641f8b12012-07-31 19:15:58 +000016#include "SkClipStack.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000017#include "SkStrokeRec.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000018#include "SkTemplates.h"
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +000019#include "SkTypes.h"
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000020
bsalomon@google.com471d4712011-08-23 15:45:25 +000021class GrGpu;
bsalomon@google.com1c13c962011-02-14 16:51:21 +000022class GrIndexBufferAllocPool;
bsalomon@google.com471d4712011-08-23 15:45:25 +000023class GrVertexBufferAllocPool;
reed@google.comac10a2d2010-12-22 21:39:39 +000024
bsalomon@google.com1c13c962011-02-14 16:51:21 +000025/**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000026 * GrInOrderDrawBuffer is an implementation of GrDrawTarget that queues up draws for eventual
27 * playback into a GrGpu. In theory one draw buffer could playback into another. When index or
28 * vertex buffers are used as geometry sources it is the callers the draw buffer only holds
29 * references to the buffers. It is the callers responsibility to ensure that the data is still
30 * valid when the draw buffer is played back into a GrGpu. Similarly, it is the caller's
31 * responsibility to ensure that all referenced textures, buffers, and render-targets are associated
32 * in the GrGpu object that the buffer is played back into. The buffer requires VB and IB pools to
33 * store geometry.
skia.committer@gmail.com07d3a652013-04-10 07:01:15 +000034 */
reed@google.comac10a2d2010-12-22 21:39:39 +000035class GrInOrderDrawBuffer : public GrDrawTarget {
36public:
37
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000038 /**
39 * Creates a GrInOrderDrawBuffer
40 *
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000041 * @param gpu the gpu object that this draw buffer flushes to.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000042 * @param vertexPool pool where vertices for queued draws will be saved when
43 * the vertex source is either reserved or array.
44 * @param indexPool pool where indices for queued draws will be saved when
45 * the index source is either reserved or array.
46 */
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000047 GrInOrderDrawBuffer(GrGpu* gpu,
bsalomon@google.com471d4712011-08-23 15:45:25 +000048 GrVertexBufferAllocPool* vertexPool,
bsalomon@google.com1c13c962011-02-14 16:51:21 +000049 GrIndexBufferAllocPool* indexPool);
reed@google.comac10a2d2010-12-22 21:39:39 +000050
51 virtual ~GrInOrderDrawBuffer();
52
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000053 /**
bsalomon@google.com55e4a202013-01-11 13:54:21 +000054 * Empties the draw buffer of any queued up draws. This must not be called while inside an
55 * unbalanced pushGeometrySource(). The current draw state and clip are preserved.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000056 */
57 void reset();
58
59 /**
bsalomon@google.com6e4e6502013-02-25 20:12:45 +000060 * 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 +000061 * is destructive). This buffer must not have an active reserved vertex or index source. Any
62 * reserved geometry on the target will be finalized because it's geometry source will be pushed
63 * before flushing and popped afterwards.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000064 */
robertphillips@google.com1267fbd2013-07-03 18:37:27 +000065 void flush();
bsalomon@google.com97805382012-03-13 14:32:07 +000066
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +000067 // tracking for draws
68 virtual DrawToken getCurrentDrawToken() { return DrawToken(this, fDrawID); }
69
bsalomon@google.com86afc2a2011-02-16 16:12:19 +000070 // overrides from GrDrawTarget
jvanverth@google.comb75b0a02013-02-05 20:33:30 +000071 virtual bool geometryHints(int* vertexCount,
bsalomon@google.com97805382012-03-13 14:32:07 +000072 int* indexCount) const SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +000073 virtual void clear(const SkIRect* rect,
robertphillips@google.comc82a8b72012-06-21 20:15:48 +000074 GrColor color,
75 GrRenderTarget* renderTarget = NULL) SK_OVERRIDE;
bsalomon@google.com0b335c12011-04-25 19:17:44 +000076
bsalomon@google.comeb851172013-04-15 13:51:00 +000077 virtual void initCopySurfaceDstDesc(const GrSurface* src, GrTextureDesc* desc) SK_OVERRIDE;
78
bsalomon@google.com97805382012-03-13 14:32:07 +000079protected:
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +000080 virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE;
81
reed@google.comac10a2d2010-12-22 21:39:39 +000082private:
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000083 enum Cmd {
bsalomon@google.comded4f4b2012-06-28 18:48:06 +000084 kDraw_Cmd = 1,
85 kStencilPath_Cmd = 2,
86 kSetState_Cmd = 3,
87 kSetClip_Cmd = 4,
88 kClear_Cmd = 5,
bsalomon@google.com116ad842013-04-09 15:38:19 +000089 kCopySurface_Cmd = 6,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +000090 kFillPath_Cmd = 7,
bsalomon@google.coma4f6b102012-06-26 21:04:22 +000091 };
92
bsalomon@google.comd62e88e2013-02-01 14:19:27 +000093 class DrawRecord : public DrawInfo {
94 public:
95 DrawRecord(const DrawInfo& info) : DrawInfo(info) {}
bsalomon@google.com1c13c962011-02-14 16:51:21 +000096 const GrVertexBuffer* fVertexBuffer;
97 const GrIndexBuffer* fIndexBuffer;
reed@google.comac10a2d2010-12-22 21:39:39 +000098 };
99
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000100 struct StencilPath : public ::SkNoncopyable {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000101 StencilPath();
102
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000103 SkAutoTUnref<const GrPath> fPath;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000104 SkStrokeRec fStroke;
sugoi@google.com12b4e272012-12-06 20:13:11 +0000105 SkPath::FillType fFill;
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000106 };
107
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000108 struct FillPath : public ::SkNoncopyable {
109 FillPath();
110
111 SkAutoTUnref<const GrPath> fPath;
112 SkStrokeRec fStroke;
113 SkPath::FillType fFill;
114 GrDeviceCoordTexture fDstCopy;
115 };
116
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000117 struct Clear : public ::SkNoncopyable {
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000118 Clear() : fRenderTarget(NULL) {}
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +0000119 ~Clear() { SkSafeUnref(fRenderTarget); }
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000120
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000121 SkIRect fRect;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000122 GrColor fColor;
123 GrRenderTarget* fRenderTarget;
bsalomon@google.com0b335c12011-04-25 19:17:44 +0000124 };
125
commit-bot@chromium.orga0b40282013-09-18 13:00:55 +0000126 struct CopySurface : public ::SkNoncopyable {
bsalomon@google.com116ad842013-04-09 15:38:19 +0000127 SkAutoTUnref<GrSurface> fDst;
128 SkAutoTUnref<GrSurface> fSrc;
129 SkIRect fSrcRect;
130 SkIPoint fDstPoint;
131 };
132
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000133 // overrides from GrDrawTarget
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000134 virtual void onDraw(const DrawInfo&) SK_OVERRIDE;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000135 virtual void onDrawRect(const SkRect& rect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000136 const SkMatrix* matrix,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000137 const SkRect* localRect,
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000138 const SkMatrix* localMatrix) SK_OVERRIDE;
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000139 virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType) SK_OVERRIDE;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000140 virtual void onFillPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType,
141 const GrDeviceCoordTexture* dstCopy) SK_OVERRIDE;
jvanverth@google.coma6338982013-01-31 21:34:25 +0000142 virtual bool onReserveVertexSpace(size_t vertexSize,
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000143 int vertexCount,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000144 void** vertices) SK_OVERRIDE;
145 virtual bool onReserveIndexSpace(int indexCount,
146 void** indices) SK_OVERRIDE;
147 virtual void releaseReservedVertexSpace() SK_OVERRIDE;
148 virtual void releaseReservedIndexSpace() SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000149 virtual void onSetVertexSourceToArray(const void* vertexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000150 int vertexCount) SK_OVERRIDE;
bsalomon@google.combcdbbe62011-04-12 15:40:00 +0000151 virtual void onSetIndexSourceToArray(const void* indexArray,
bsalomon@google.com13f1b6f2012-05-31 12:52:43 +0000152 int indexCount) SK_OVERRIDE;
153 virtual void releaseVertexArray() SK_OVERRIDE;
154 virtual void releaseIndexArray() SK_OVERRIDE;
155 virtual void geometrySourceWillPush() SK_OVERRIDE;
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000156 virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE;
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000157 virtual void willReserveVertexAndIndexSpace(int vertexCount,
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000158 int indexCount) SK_OVERRIDE;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000159 virtual bool onCopySurface(GrSurface* dst,
160 GrSurface* src,
161 const SkIRect& srcRect,
162 const SkIPoint& dstPoint) SK_OVERRIDE;
163 virtual bool onCanCopySurface(GrSurface* dst,
164 GrSurface* src,
165 const SkIRect& srcRect,
166 const SkIPoint& dstPoint) SK_OVERRIDE;
167
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000168 bool quickInsideClip(const SkRect& devBounds);
bsalomon@google.com02ddc8b2013-01-28 15:35:28 +0000169
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000170 // Attempts to concat instances from info onto the previous draw. info must represent an
171 // instanced draw. The caller must have already recorded a new draw state and clip if necessary.
172 int concatInstancedDraw(const DrawInfo& info);
bsalomon@google.com1c13c962011-02-14 16:51:21 +0000173
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000174 // we lazily record state and clip changes in order to skip clips and states that have no
175 // effect.
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000176 bool needsNewState() const;
177 bool needsNewClip() const;
reed@google.comac10a2d2010-12-22 21:39:39 +0000178
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000179 // these functions record a command
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000180 void recordState();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000181 void recordClip();
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000182 DrawRecord* recordDraw(const DrawInfo&);
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000183 StencilPath* recordStencilPath();
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000184 FillPath* recordFillPath();
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000185 Clear* recordClear();
bsalomon@google.com116ad842013-04-09 15:38:19 +0000186 CopySurface* recordCopySurface();
bsalomon@google.com934c5702012-03-20 21:17:58 +0000187
bsalomon@google.com116ad842013-04-09 15:38:19 +0000188 // TODO: Use a single allocator for commands and records
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000189 enum {
bsalomon@google.coma4f6b102012-06-26 21:04:22 +0000190 kCmdPreallocCnt = 32,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000191 kDrawPreallocCnt = 8,
bsalomon@google.comded4f4b2012-06-28 18:48:06 +0000192 kStencilPathPreallocCnt = 8,
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000193 kFillPathPreallocCnt = 8,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000194 kStatePreallocCnt = 8,
195 kClipPreallocCnt = 8,
196 kClearPreallocCnt = 4,
197 kGeoPoolStatePreAllocCnt = 4,
bsalomon@google.com116ad842013-04-09 15:38:19 +0000198 kCopySurfacePreallocCnt = 4,
bsalomon@google.com4b90c622011-09-28 17:52:15 +0000199 };
reed@google.comac10a2d2010-12-22 21:39:39 +0000200
bsalomon@google.comca432082013-01-23 19:53:46 +0000201 SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds;
bsalomon@google.com74749cd2013-01-30 16:12:41 +0000202 GrSTAllocator<kDrawPreallocCnt, DrawRecord> fDraws;
bsalomon@google.comca432082013-01-23 19:53:46 +0000203 GrSTAllocator<kStatePreallocCnt, StencilPath> fStencilPaths;
commit-bot@chromium.orgc4dc0ad2013-10-09 14:11:33 +0000204 GrSTAllocator<kStatePreallocCnt, FillPath> fFillPaths;
bsalomon@google.comca432082013-01-23 19:53:46 +0000205 GrSTAllocator<kStatePreallocCnt, GrDrawState::DeferredState> fStates;
206 GrSTAllocator<kClearPreallocCnt, Clear> fClears;
bsalomon@google.com116ad842013-04-09 15:38:19 +0000207 GrSTAllocator<kCopySurfacePreallocCnt, CopySurface> fCopySurfaces;
208 GrSTAllocator<kClipPreallocCnt, SkClipStack> fClips;
209 GrSTAllocator<kClipPreallocCnt, SkIPoint> fClipOrigins;
bsalomon@google.com97805382012-03-13 14:32:07 +0000210
bsalomon@google.com6e4e6502013-02-25 20:12:45 +0000211 GrDrawTarget* fDstGpu;
bsalomon@google.com97805382012-03-13 14:32:07 +0000212
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000213 bool fClipSet;
214
bsalomon@google.comd62e88e2013-02-01 14:19:27 +0000215 enum ClipProxyState {
216 kUnknown_ClipProxyState,
217 kValid_ClipProxyState,
218 kInvalid_ClipProxyState
219 };
220 ClipProxyState fClipProxyState;
221 SkRect fClipProxy;
222
bsalomon@google.com934c5702012-03-20 21:17:58 +0000223 GrVertexBufferAllocPool& fVertexPool;
224
225 GrIndexBufferAllocPool& fIndexPool;
226
bsalomon@google.com25fb21f2011-06-21 18:17:25 +0000227 struct GeometryPoolState {
228 const GrVertexBuffer* fPoolVertexBuffer;
229 int fPoolStartVertex;
230 const GrIndexBuffer* fPoolIndexBuffer;
231 int fPoolStartIndex;
232 // caller may conservatively over reserve vertices / indices.
233 // we release unused space back to allocator if possible
234 // can only do this if there isn't an intervening pushGeometrySource()
235 size_t fUsedPoolVertexBytes;
236 size_t fUsedPoolIndexBytes;
237 };
bsalomon@google.com92669012011-09-27 19:10:05 +0000238 SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack;
reed@google.comac10a2d2010-12-22 21:39:39 +0000239
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000240 virtual bool isIssued(uint32_t drawID) { return drawID != fDrawID; }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000241
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000242 bool fFlushing;
commit-bot@chromium.orga8916ff2013-08-16 15:53:46 +0000243 uint32_t fDrawID;
robertphillips@google.comc82a8b72012-06-21 20:15:48 +0000244
bsalomon@google.com86afc2a2011-02-16 16:12:19 +0000245 typedef GrDrawTarget INHERITED;
reed@google.comac10a2d2010-12-22 21:39:39 +0000246};
247
248#endif