epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * 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.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #ifndef GrInOrderDrawBuffer_DEFINED |
| 12 | #define GrInOrderDrawBuffer_DEFINED |
| 13 | |
| 14 | #include "GrDrawTarget.h" |
| 15 | #include "GrAllocPool.h" |
| 16 | #include "GrAllocator.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 17 | #include "GrPath.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 18 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 19 | #include "SkClipStack.h" |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 20 | #include "SkStrokeRec.h" |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 21 | #include "SkTemplates.h" |
| 22 | |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 23 | class GrGpu; |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 24 | class GrIndexBufferAllocPool; |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 25 | class GrVertexBufferAllocPool; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 26 | |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 27 | /** |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 28 | * 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. |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 36 | */ |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 37 | class GrInOrderDrawBuffer : public GrDrawTarget { |
| 38 | public: |
| 39 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Creates a GrInOrderDrawBuffer |
| 42 | * |
bsalomon@google.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 43 | * @param gpu the gpu object where this will be played back |
| 44 | * (possible indirectly). GrResources used with the draw |
| 45 | * buffer are created by this gpu object. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 46 | * @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.com | 471d471 | 2011-08-23 15:45:25 +0000 | [diff] [blame] | 51 | GrInOrderDrawBuffer(const GrGpu* gpu, |
| 52 | GrVertexBufferAllocPool* vertexPool, |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 53 | GrIndexBufferAllocPool* indexPool); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | |
| 55 | virtual ~GrInOrderDrawBuffer(); |
| 56 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 57 | /** |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 58 | * 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.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 60 | */ |
| 61 | void reset(); |
| 62 | |
| 63 | /** |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 64 | * This plays the queued up draws to another target. It also resets this object (i.e. flushing |
| 65 | * 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.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 68 | * |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 69 | * @return false if the playback trivially drew nothing because nothing was recorded. |
bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 70 | * |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 71 | * @param target the target to receive the playback |
| 72 | */ |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 73 | bool flushTo(GrDrawTarget* target); |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 76 | * This function allows the draw buffer to automatically flush itself to another target. This |
| 77 | * means the buffer may internally call this->flushTo(target) when it is safe to do so. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 78 | * |
bsalomon@google.com | 55e4a20 | 2013-01-11 13:54:21 +0000 | [diff] [blame] | 79 | * When the auto flush target is set to NULL (as it initially is) the draw buffer will never |
| 80 | * automatically flush itself. |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 81 | */ |
| 82 | void setAutoFlushTarget(GrDrawTarget* target); |
| 83 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 84 | // overrides from GrDrawTarget |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 85 | virtual bool geometryHints(int* vertexCount, |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 86 | int* indexCount) const SK_OVERRIDE; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 87 | virtual void clear(const GrIRect* rect, |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 88 | GrColor color, |
| 89 | GrRenderTarget* renderTarget = NULL) SK_OVERRIDE; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 90 | virtual void drawRect(const GrRect& rect, |
| 91 | const SkMatrix* matrix, |
jvanverth@google.com | 3976825 | 2013-02-14 15:25:44 +0000 | [diff] [blame^] | 92 | const GrRect* srcRect, |
| 93 | const SkMatrix* srcMatrix, |
| 94 | int stage) SK_OVERRIDE; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 96 | protected: |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 97 | virtual void clipWillBeSet(const GrClipData* newClip) SK_OVERRIDE; |
| 98 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | private: |
bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 100 | enum Cmd { |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 101 | kDraw_Cmd = 1, |
| 102 | kStencilPath_Cmd = 2, |
| 103 | kSetState_Cmd = 3, |
| 104 | kSetClip_Cmd = 4, |
| 105 | kClear_Cmd = 5, |
bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 108 | class DrawRecord : public DrawInfo { |
| 109 | public: |
| 110 | DrawRecord(const DrawInfo& info) : DrawInfo(info) {} |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 111 | const GrVertexBuffer* fVertexBuffer; |
| 112 | const GrIndexBuffer* fIndexBuffer; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 115 | struct StencilPath { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 116 | StencilPath(); |
| 117 | |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 118 | SkAutoTUnref<const GrPath> fPath; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 119 | SkStrokeRec fStroke; |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 120 | SkPath::FillType fFill; |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 123 | struct Clear { |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 124 | Clear() : fRenderTarget(NULL) {} |
| 125 | ~Clear() { GrSafeUnref(fRenderTarget); } |
| 126 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 127 | GrIRect fRect; |
| 128 | GrColor fColor; |
| 129 | GrRenderTarget* fRenderTarget; |
bsalomon@google.com | 0b335c1 | 2011-04-25 19:17:44 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 132 | // overrides from GrDrawTarget |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 133 | virtual void onDraw(const DrawInfo&) SK_OVERRIDE; |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 134 | virtual void onStencilPath(const GrPath*, const SkStrokeRec& stroke, SkPath::FillType) SK_OVERRIDE; |
jvanverth@google.com | a633898 | 2013-01-31 21:34:25 +0000 | [diff] [blame] | 135 | virtual bool onReserveVertexSpace(size_t vertexSize, |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 136 | int vertexCount, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 137 | void** vertices) SK_OVERRIDE; |
| 138 | virtual bool onReserveIndexSpace(int indexCount, |
| 139 | void** indices) SK_OVERRIDE; |
| 140 | virtual void releaseReservedVertexSpace() SK_OVERRIDE; |
| 141 | virtual void releaseReservedIndexSpace() SK_OVERRIDE; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 142 | virtual void onSetVertexSourceToArray(const void* vertexArray, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 143 | int vertexCount) SK_OVERRIDE; |
bsalomon@google.com | bcdbbe6 | 2011-04-12 15:40:00 +0000 | [diff] [blame] | 144 | virtual void onSetIndexSourceToArray(const void* indexArray, |
bsalomon@google.com | 13f1b6f | 2012-05-31 12:52:43 +0000 | [diff] [blame] | 145 | int indexCount) SK_OVERRIDE; |
| 146 | virtual void releaseVertexArray() SK_OVERRIDE; |
| 147 | virtual void releaseIndexArray() SK_OVERRIDE; |
| 148 | virtual void geometrySourceWillPush() SK_OVERRIDE; |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 149 | virtual void geometrySourceWillPop(const GeometrySrcState& restoredState) SK_OVERRIDE; |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 150 | virtual void willReserveVertexAndIndexSpace(int vertexCount, |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 151 | int indexCount) SK_OVERRIDE; |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 152 | bool quickInsideClip(const SkRect& devBounds); |
bsalomon@google.com | 02ddc8b | 2013-01-28 15:35:28 +0000 | [diff] [blame] | 153 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 154 | // Attempts to concat instances from info onto the previous draw. info must represent an |
| 155 | // instanced draw. The caller must have already recorded a new draw state and clip if necessary. |
| 156 | int concatInstancedDraw(const DrawInfo& info); |
bsalomon@google.com | 1c13c96 | 2011-02-14 16:51:21 +0000 | [diff] [blame] | 157 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 158 | // we lazily record state and clip changes in order to skip clips and states that have no |
| 159 | // effect. |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 160 | bool needsNewState() const; |
| 161 | bool needsNewClip() const; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 162 | |
bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 163 | // these functions record a command |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 164 | void recordState(); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 165 | void recordClip(); |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 166 | DrawRecord* recordDraw(const DrawInfo&); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 167 | StencilPath* recordStencilPath(); |
| 168 | Clear* recordClear(); |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 169 | |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 170 | enum { |
bsalomon@google.com | a4f6b10 | 2012-06-26 21:04:22 +0000 | [diff] [blame] | 171 | kCmdPreallocCnt = 32, |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 172 | kDrawPreallocCnt = 8, |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 173 | kStencilPathPreallocCnt = 8, |
bsalomon@google.com | 4b90c62 | 2011-09-28 17:52:15 +0000 | [diff] [blame] | 174 | kStatePreallocCnt = 8, |
| 175 | kClipPreallocCnt = 8, |
| 176 | kClearPreallocCnt = 4, |
| 177 | kGeoPoolStatePreAllocCnt = 4, |
| 178 | }; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 179 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 180 | SkAutoTUnref<const GrGpu> fGpu; |
| 181 | |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 182 | SkSTArray<kCmdPreallocCnt, uint8_t, true> fCmds; |
bsalomon@google.com | 74749cd | 2013-01-30 16:12:41 +0000 | [diff] [blame] | 183 | GrSTAllocator<kDrawPreallocCnt, DrawRecord> fDraws; |
bsalomon@google.com | ca43208 | 2013-01-23 19:53:46 +0000 | [diff] [blame] | 184 | GrSTAllocator<kStatePreallocCnt, StencilPath> fStencilPaths; |
| 185 | GrSTAllocator<kStatePreallocCnt, GrDrawState::DeferredState> fStates; |
| 186 | GrSTAllocator<kClearPreallocCnt, Clear> fClears; |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 187 | |
robertphillips@google.com | 641f8b1 | 2012-07-31 19:15:58 +0000 | [diff] [blame] | 188 | GrSTAllocator<kClipPreallocCnt, SkClipStack> fClips; |
robertphillips@google.com | beb1af7 | 2012-07-26 18:52:16 +0000 | [diff] [blame] | 189 | GrSTAllocator<kClipPreallocCnt, SkIPoint> fClipOrigins; |
bsalomon@google.com | 9780538 | 2012-03-13 14:32:07 +0000 | [diff] [blame] | 190 | |
| 191 | GrDrawTarget* fAutoFlushTarget; |
| 192 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 193 | bool fClipSet; |
| 194 | |
bsalomon@google.com | d62e88e | 2013-02-01 14:19:27 +0000 | [diff] [blame] | 195 | enum ClipProxyState { |
| 196 | kUnknown_ClipProxyState, |
| 197 | kValid_ClipProxyState, |
| 198 | kInvalid_ClipProxyState |
| 199 | }; |
| 200 | ClipProxyState fClipProxyState; |
| 201 | SkRect fClipProxy; |
| 202 | |
bsalomon@google.com | 934c570 | 2012-03-20 21:17:58 +0000 | [diff] [blame] | 203 | GrVertexBufferAllocPool& fVertexPool; |
| 204 | |
| 205 | GrIndexBufferAllocPool& fIndexPool; |
| 206 | |
bsalomon@google.com | 25fb21f | 2011-06-21 18:17:25 +0000 | [diff] [blame] | 207 | struct GeometryPoolState { |
| 208 | const GrVertexBuffer* fPoolVertexBuffer; |
| 209 | int fPoolStartVertex; |
| 210 | const GrIndexBuffer* fPoolIndexBuffer; |
| 211 | int fPoolStartIndex; |
| 212 | // caller may conservatively over reserve vertices / indices. |
| 213 | // we release unused space back to allocator if possible |
| 214 | // can only do this if there isn't an intervening pushGeometrySource() |
| 215 | size_t fUsedPoolVertexBytes; |
| 216 | size_t fUsedPoolIndexBytes; |
| 217 | }; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 218 | SkSTArray<kGeoPoolStatePreAllocCnt, GeometryPoolState> fGeoPoolStateStack; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 219 | |
robertphillips@google.com | c82a8b7 | 2012-06-21 20:15:48 +0000 | [diff] [blame] | 220 | bool fFlushing; |
| 221 | |
bsalomon@google.com | 86afc2a | 2011-02-16 16:12:19 +0000 | [diff] [blame] | 222 | typedef GrDrawTarget INHERITED; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | #endif |