blob: 0ff5ac18bc316c1204436f28c2bc3f1c51b6a4f1 [file] [log] [blame]
bsalomon16b99132015-08-13 14:55:50 -07001/*
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 GrVertexBatch_DEFINED
9#define GrVertexBatch_DEFINED
10
11#include "GrDrawBatch.h"
bsalomon75398562015-08-17 12:55:38 -070012#include "GrPrimitiveProcessor.h"
13#include "GrPendingProgramElement.h"
14#include "GrVertices.h"
15
16#include "SkTLList.h"
17
18class GrBatchFlushState;
bsalomon16b99132015-08-13 14:55:50 -070019
20/**
21 * Base class for vertex-based GrBatches.
22 */
23class GrVertexBatch : public GrDrawBatch {
24public:
bsalomon75398562015-08-17 12:55:38 -070025 class Target;
26
bsalomon16b99132015-08-13 14:55:50 -070027 GrVertexBatch();
28
bsalomon16b99132015-08-13 14:55:50 -070029protected:
30 /** Helper for rendering instances using an instanced index index buffer. This class creates the
31 space for the vertices and flushes the draws to the batch target. */
32 class InstancedHelper {
33 public:
34 InstancedHelper() {}
35 /** Returns the allocated storage for the vertices. The caller should populate the before
36 vertices before calling issueDraws(). */
bsalomon75398562015-08-17 12:55:38 -070037 void* init(Target*, GrPrimitiveType, size_t vertexStride,
bsalomon16b99132015-08-13 14:55:50 -070038 const GrIndexBuffer*, int verticesPerInstance, int indicesPerInstance,
39 int instancesToDraw);
40
41 /** Call after init() to issue draws to the batch target.*/
bsalomon75398562015-08-17 12:55:38 -070042 void recordDraw(Target* target);
bsalomon16b99132015-08-13 14:55:50 -070043 private:
44 GrVertices fVertices;
45 };
46
47 static const int kVerticesPerQuad = 4;
48 static const int kIndicesPerQuad = 6;
49
50 /** A specialization of InstanceHelper for quad rendering. */
51 class QuadHelper : private InstancedHelper {
52 public:
53 QuadHelper() : INHERITED() {}
54 /** Finds the cached quad index buffer and reserves vertex space. Returns NULL on failure
55 and on sucess a pointer to the vertex data that the caller should populate before
56 calling issueDraws(). */
bsalomon75398562015-08-17 12:55:38 -070057 void* init(Target* batchTarget, size_t vertexStride, int quadsToDraw);
bsalomon16b99132015-08-13 14:55:50 -070058
bsalomon75398562015-08-17 12:55:38 -070059 using InstancedHelper::recordDraw;
bsalomon16b99132015-08-13 14:55:50 -070060 private:
61 typedef InstancedHelper INHERITED;
62 };
63
64private:
bsalomon53469832015-08-18 09:20:09 -070065 void onPrepare(GrBatchFlushState* state) final;
66 void onDraw(GrBatchFlushState* state) final;
67
bsalomon75398562015-08-17 12:55:38 -070068 virtual void onPrepareDraws(Target*) = 0;
69
70 // A set of contiguous draws with no inline uploads between them that all use the same
71 // primitive processor. All the draws in a DrawArray share a primitive processor and use the
72 // the batch's GrPipeline.
73 struct DrawArray {
74 SkSTArray<1, GrVertices, true> fDraws;
75 GrPendingProgramElement<const GrPrimitiveProcessor> fPrimitiveProcessor;
76 };
77
78 // Array of DrawArray. There may be inline uploads between each DrawArray and each DrawArray
79 // may use a different primitive processor.
80 SkTLList<DrawArray> fDrawArrays;
81
82 // What is this?
83 GrBatchTracker fBatchTracker;
84
bsalomon16b99132015-08-13 14:55:50 -070085 typedef GrDrawBatch INHERITED;
86};
87
88#endif