blob: b73907746f055fc5b322f1a688742fe4b93efda5 [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
20class GrBatchTarget : public SkNoncopyable {
21public:
22 GrBatchTarget(GrGpu* gpu,
23 GrVertexBufferAllocPool* vpool,
24 GrIndexBufferAllocPool* ipool)
25 : fGpu(gpu)
26 , fVertexPool(vpool)
27 , fIndexPool(ipool)
28 , fFlushBuffer(kFlushBufferInitialSizeInBytes)
joshualitt46c77f72015-02-03 08:40:22 -080029 , fIter(fFlushBuffer) {}
joshualitt4d8da812015-01-28 12:53:54 -080030
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); }
joshualitt46c77f72015-02-03 08:40:22 -080043 void flushNext();
joshualitt4d8da812015-01-28 12:53:54 -080044 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
55private:
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)
joshualitt46c77f72015-02-03 08:40:22 -080065 , fPipeline(pipeline)
66 , fDraws(kDrawRecorderInitialSizeInBytes) {}
joshualitt4d8da812015-01-28 12:53:54 -080067 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),
joshualitt46c77f72015-02-03 08:40:22 -080076 kDrawRecorderInitialSizeInBytes = 8 * sizeof(DrawInfo),
joshualitt4d8da812015-01-28 12:53:54 -080077 };
78
79 typedef GrTRecorder<BufferedFlush, TBufferAlign> FlushBuffer;
80
81 FlushBuffer fFlushBuffer;
82 // TODO this is temporary
83 FlushBuffer::Iter fIter;
joshualitt4d8da812015-01-28 12:53:54 -080084};
85
86#endif