blob: 37f5c9b1427af2c742c426c774023b1fe48c9d0f [file] [log] [blame]
joshualitt4d8da812015-01-28 12:53:54 -08001/*
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
joshualitt658d55c2015-02-03 06:56:44 -080020class GrIndexBufferAllocPool;
21class GrVertexBufferAllocPool;
22
joshualitt4d8da812015-01-28 12:53:54 -080023class GrBatchTarget : public SkNoncopyable {
24public:
25 GrBatchTarget(GrGpu* gpu,
26 GrVertexBufferAllocPool* vpool,
27 GrIndexBufferAllocPool* ipool)
28 : fGpu(gpu)
29 , fVertexPool(vpool)
30 , fIndexPool(ipool)
31 , fFlushBuffer(kFlushBufferInitialSizeInBytes)
joshualitt658d55c2015-02-03 06:56:44 -080032 , fIter(fFlushBuffer)
33 , fNumberOfDraws(0) {}
joshualitt4d8da812015-01-28 12:53:54 -080034
35 typedef GrDrawTarget::DrawInfo DrawInfo;
36 void initDraw(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) {
37 GrNEW_APPEND_TO_RECORDER(fFlushBuffer, BufferedFlush, (primProc, pipeline));
joshualitt658d55c2015-02-03 06:56:44 -080038 fNumberOfDraws++;
joshualitt4d8da812015-01-28 12:53:54 -080039 }
40
41 void draw(const GrDrawTarget::DrawInfo& draw) {
42 fFlushBuffer.back().fDraws.push_back(draw);
43 }
44
45 // TODO this is temporary until batch is everywhere
46 //void flush();
joshualitt658d55c2015-02-03 06:56:44 -080047 void resetNumberOfDraws() { fNumberOfDraws = 0; }
48 int numberOfDraws() const { return fNumberOfDraws; }
joshualitt4d8da812015-01-28 12:53:54 -080049 void preFlush() { fIter = FlushBuffer::Iter(fFlushBuffer); }
joshualitt658d55c2015-02-03 06:56:44 -080050 void flushNext(int n);
joshualitt4d8da812015-01-28 12:53:54 -080051 void postFlush() { SkASSERT(!fIter.next()); fFlushBuffer.reset(); }
52
53 // TODO This goes away when everything uses batch
54 GrBatchTracker* currentBatchTracker() {
55 SkASSERT(!fFlushBuffer.empty());
56 return &fFlushBuffer.back().fBatchTracker;
57 }
58
joshualitt658d55c2015-02-03 06:56:44 -080059 const GrDrawTargetCaps& caps() const { return *fGpu->caps(); }
60
joshualitt4d8da812015-01-28 12:53:54 -080061 GrVertexBufferAllocPool* vertexPool() { return fVertexPool; }
62 GrIndexBufferAllocPool* indexPool() { return fIndexPool; }
63
64private:
65 GrGpu* fGpu;
66 GrVertexBufferAllocPool* fVertexPool;
67 GrIndexBufferAllocPool* fIndexPool;
68
69 typedef void* TBufferAlign; // This wouldn't be enough align if a command used long double.
70
71 struct BufferedFlush {
72 BufferedFlush(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline)
73 : fPrimitiveProcessor(primProc)
joshualitt658d55c2015-02-03 06:56:44 -080074 , fPipeline(pipeline) {}
joshualitt4d8da812015-01-28 12:53:54 -080075 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
76 ProgramPrimitiveProcessor fPrimitiveProcessor;
77 const GrPipeline* fPipeline;
78 GrBatchTracker fBatchTracker;
79 SkSTArray<4, DrawInfo, true> fDraws;
80 };
81
82 enum {
83 kFlushBufferInitialSizeInBytes = 8 * sizeof(BufferedFlush),
joshualitt4d8da812015-01-28 12:53:54 -080084 };
85
86 typedef GrTRecorder<BufferedFlush, TBufferAlign> FlushBuffer;
87
88 FlushBuffer fFlushBuffer;
89 // TODO this is temporary
90 FlushBuffer::Iter fIter;
joshualitt658d55c2015-02-03 06:56:44 -080091 int fNumberOfDraws;
joshualitt4d8da812015-01-28 12:53:54 -080092};
93
94#endif