joshualitt | 4d8da81 | 2015-01-28 12:53:54 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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. |
| 6 | */ |
| 7 | |
| 8 | #ifndef GrBatchBuffer_DEFINED |
| 9 | #define GrBatchBuffer_DEFINED |
| 10 | |
| 11 | #include "GrPendingProgramElement.h" |
| 12 | #include "GrGpu.h" |
| 13 | #include "GrTRecorder.h" |
| 14 | |
| 15 | /* |
| 16 | * GrBatch instances use this object to allocate space for their geometry and to issue the draws |
| 17 | * that render their batch. |
| 18 | */ |
| 19 | |
| 20 | class GrBatchTarget : public SkNoncopyable { |
| 21 | public: |
| 22 | GrBatchTarget(GrGpu* gpu, |
| 23 | GrVertexBufferAllocPool* vpool, |
| 24 | GrIndexBufferAllocPool* ipool) |
| 25 | : fGpu(gpu) |
| 26 | , fVertexPool(vpool) |
| 27 | , fIndexPool(ipool) |
| 28 | , fFlushBuffer(kFlushBufferInitialSizeInBytes) |
| 29 | , fIter(fFlushBuffer) {} |
| 30 | |
| 31 | typedef GrDrawTarget::DrawInfo DrawInfo; |
| 32 | void initDraw(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) { |
| 33 | GrNEW_APPEND_TO_RECORDER(fFlushBuffer, BufferedFlush, (primProc, pipeline)); |
| 34 | } |
| 35 | |
| 36 | void draw(const GrDrawTarget::DrawInfo& draw) { |
| 37 | fFlushBuffer.back().fDraws.push_back(draw); |
| 38 | } |
| 39 | |
| 40 | // TODO this is temporary until batch is everywhere |
| 41 | //void flush(); |
| 42 | void preFlush() { fIter = FlushBuffer::Iter(fFlushBuffer); } |
| 43 | void flushNext(); |
| 44 | void postFlush() { SkASSERT(!fIter.next()); fFlushBuffer.reset(); } |
| 45 | |
| 46 | // TODO This goes away when everything uses batch |
| 47 | GrBatchTracker* currentBatchTracker() { |
| 48 | SkASSERT(!fFlushBuffer.empty()); |
| 49 | return &fFlushBuffer.back().fBatchTracker; |
| 50 | } |
| 51 | |
| 52 | GrVertexBufferAllocPool* vertexPool() { return fVertexPool; } |
| 53 | GrIndexBufferAllocPool* indexPool() { return fIndexPool; } |
| 54 | |
| 55 | private: |
| 56 | GrGpu* fGpu; |
| 57 | GrVertexBufferAllocPool* fVertexPool; |
| 58 | GrIndexBufferAllocPool* fIndexPool; |
| 59 | |
| 60 | typedef void* TBufferAlign; // This wouldn't be enough align if a command used long double. |
| 61 | |
| 62 | struct BufferedFlush { |
| 63 | BufferedFlush(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) |
| 64 | : fPrimitiveProcessor(primProc) |
| 65 | , fPipeline(pipeline) |
| 66 | , fDraws(kDrawRecorderInitialSizeInBytes) {} |
| 67 | typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor; |
| 68 | ProgramPrimitiveProcessor fPrimitiveProcessor; |
| 69 | const GrPipeline* fPipeline; |
| 70 | GrBatchTracker fBatchTracker; |
| 71 | SkSTArray<4, DrawInfo, true> fDraws; |
| 72 | }; |
| 73 | |
| 74 | enum { |
| 75 | kFlushBufferInitialSizeInBytes = 8 * sizeof(BufferedFlush), |
| 76 | kDrawRecorderInitialSizeInBytes = 8 * sizeof(DrawInfo), |
| 77 | }; |
| 78 | |
| 79 | typedef GrTRecorder<BufferedFlush, TBufferAlign> FlushBuffer; |
| 80 | |
| 81 | FlushBuffer fFlushBuffer; |
| 82 | // TODO this is temporary |
| 83 | FlushBuffer::Iter fIter; |
| 84 | }; |
| 85 | |
| 86 | #endif |