blob: 8cd91d37e1412fc6f1cd26e2a4fadf0a1a867ad2 [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"
joshualitt7bc18b72015-02-03 16:41:41 -080012#include "GrPipeline.h"
joshualitt4d8da812015-01-28 12:53:54 -080013#include "GrGpu.h"
14#include "GrTRecorder.h"
15
16/*
17 * GrBatch instances use this object to allocate space for their geometry and to issue the draws
18 * that render their batch.
19 */
20
joshualitt7bc18b72015-02-03 16:41:41 -080021class GrIndexBufferAllocPool;
22class GrVertexBufferAllocPool;
23
joshualitt4d8da812015-01-28 12:53:54 -080024class GrBatchTarget : public SkNoncopyable {
25public:
26 GrBatchTarget(GrGpu* gpu,
27 GrVertexBufferAllocPool* vpool,
28 GrIndexBufferAllocPool* ipool)
29 : fGpu(gpu)
30 , fVertexPool(vpool)
31 , fIndexPool(ipool)
32 , fFlushBuffer(kFlushBufferInitialSizeInBytes)
joshualitt7bc18b72015-02-03 16:41:41 -080033 , fIter(fFlushBuffer)
34 , fNumberOfDraws(0) {}
joshualitt4d8da812015-01-28 12:53:54 -080035
36 typedef GrDrawTarget::DrawInfo DrawInfo;
37 void initDraw(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline) {
38 GrNEW_APPEND_TO_RECORDER(fFlushBuffer, BufferedFlush, (primProc, pipeline));
joshualitt7bc18b72015-02-03 16:41:41 -080039 fNumberOfDraws++;
joshualitt4d8da812015-01-28 12:53:54 -080040 }
41
42 void draw(const GrDrawTarget::DrawInfo& draw) {
43 fFlushBuffer.back().fDraws.push_back(draw);
44 }
45
46 // TODO this is temporary until batch is everywhere
47 //void flush();
joshualitt7bc18b72015-02-03 16:41:41 -080048 void resetNumberOfDraws() { fNumberOfDraws = 0; }
49 int numberOfDraws() const { return fNumberOfDraws; }
joshualitt4d8da812015-01-28 12:53:54 -080050 void preFlush() { fIter = FlushBuffer::Iter(fFlushBuffer); }
joshualitt7bc18b72015-02-03 16:41:41 -080051 void flushNext(int n) {
52 for (; n > 0; n--) {
53 SkDEBUGCODE(bool verify =) fIter.next();
54 SkASSERT(verify);
55 GrProgramDesc desc;
56 BufferedFlush* bf = fIter.get();
57 const GrPipeline* pipeline = bf->fPipeline;
58 const GrPrimitiveProcessor* primProc = bf->fPrimitiveProcessor.get();
59 fGpu->buildProgramDesc(&desc, *primProc, *pipeline, bf->fBatchTracker);
60
61 GrGpu::DrawArgs args(primProc, pipeline, &desc, &bf->fBatchTracker);
62
63 int drawCount = bf->fDraws.count();
64 const SkSTArray<1, DrawInfo, true>& draws = bf->fDraws;
65 for (int i = 0; i < drawCount; i++) {
66 fGpu->draw(args, draws[i]);
67 }
68 }
69 }
joshualitt4d8da812015-01-28 12:53:54 -080070 void postFlush() { SkASSERT(!fIter.next()); fFlushBuffer.reset(); }
71
72 // TODO This goes away when everything uses batch
73 GrBatchTracker* currentBatchTracker() {
74 SkASSERT(!fFlushBuffer.empty());
75 return &fFlushBuffer.back().fBatchTracker;
76 }
77
joshualitt7bc18b72015-02-03 16:41:41 -080078 const GrDrawTargetCaps& caps() const { return *fGpu->caps(); }
79
joshualitt4d8da812015-01-28 12:53:54 -080080 GrVertexBufferAllocPool* vertexPool() { return fVertexPool; }
81 GrIndexBufferAllocPool* indexPool() { return fIndexPool; }
82
83private:
84 GrGpu* fGpu;
85 GrVertexBufferAllocPool* fVertexPool;
86 GrIndexBufferAllocPool* fIndexPool;
87
88 typedef void* TBufferAlign; // This wouldn't be enough align if a command used long double.
89
90 struct BufferedFlush {
91 BufferedFlush(const GrPrimitiveProcessor* primProc, const GrPipeline* pipeline)
92 : fPrimitiveProcessor(primProc)
joshualitt7bc18b72015-02-03 16:41:41 -080093 , fPipeline(pipeline) {}
joshualitt4d8da812015-01-28 12:53:54 -080094 typedef GrPendingProgramElement<const GrPrimitiveProcessor> ProgramPrimitiveProcessor;
95 ProgramPrimitiveProcessor fPrimitiveProcessor;
96 const GrPipeline* fPipeline;
97 GrBatchTracker fBatchTracker;
joshualitt7bc18b72015-02-03 16:41:41 -080098 SkSTArray<1, DrawInfo, true> fDraws;
joshualitt4d8da812015-01-28 12:53:54 -080099 };
100
101 enum {
102 kFlushBufferInitialSizeInBytes = 8 * sizeof(BufferedFlush),
joshualitt4d8da812015-01-28 12:53:54 -0800103 };
104
105 typedef GrTRecorder<BufferedFlush, TBufferAlign> FlushBuffer;
106
107 FlushBuffer fFlushBuffer;
108 // TODO this is temporary
109 FlushBuffer::Iter fIter;
joshualitt7bc18b72015-02-03 16:41:41 -0800110 int fNumberOfDraws;
joshualitt4d8da812015-01-28 12:53:54 -0800111};
112
113#endif