blob: 1b8831cfe86c537e6ba00af4f11a8506d0970f81 [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
Brian Salomon53e4c3c2016-12-21 11:38:53 -05008#ifndef GrMeshDrawOp_DEFINED
9#define GrMeshDrawOp_DEFINED
bsalomon16b99132015-08-13 14:55:50 -070010
Brian Salomon9afd3712016-12-01 10:59:09 -050011#include "GrDrawOp.h"
bsalomon342bfc22016-04-01 06:06:20 -070012#include "GrGeometryProcessor.h"
egdaniel0e1853c2016-03-17 11:35:45 -070013#include "GrMesh.h"
bsalomon75398562015-08-17 12:55:38 -070014#include "GrPendingProgramElement.h"
bsalomon75398562015-08-17 12:55:38 -070015
16#include "SkTLList.h"
17
Brian Salomon54d212e2017-03-21 14:22:38 -040018class GrCaps;
Brian Salomon742e31d2016-12-07 17:06:19 -050019class GrOpFlushState;
bsalomon16b99132015-08-13 14:55:50 -070020
21/**
Brian Salomondad29232016-12-01 16:40:24 -050022 * Base class for mesh-drawing GrDrawOps.
bsalomon16b99132015-08-13 14:55:50 -070023 */
Brian Salomondad29232016-12-01 16:40:24 -050024class GrMeshDrawOp : public GrDrawOp {
bsalomon16b99132015-08-13 14:55:50 -070025public:
Brian Salomon29b60c92017-10-31 14:42:10 -040026 /** Abstract interface that represents a destination for a GrMeshDrawOp. */
bsalomon75398562015-08-17 12:55:38 -070027 class Target;
28
Brian Salomond3ccb0a2017-04-03 10:38:00 -040029protected:
30 GrMeshDrawOp(uint32_t classID);
31
Chris Daltonff926502017-05-03 14:36:54 -040032 /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the
33 space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */
34 class PatternHelper {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040035 public:
Chris Daltonbca46e22017-05-15 11:03:26 -060036 PatternHelper(GrPrimitiveType primitiveType) : fMesh(primitiveType) {}
Brian Salomond3ccb0a2017-04-03 10:38:00 -040037 /** Returns the allocated storage for the vertices. The caller should populate the vertices
38 before calling recordDraws(). */
Chris Daltonbca46e22017-05-15 11:03:26 -060039 void* init(Target*, size_t vertexStride, const GrBuffer*, int verticesPerRepetition,
40 int indicesPerRepetition, int repeatCount);
Brian Salomond3ccb0a2017-04-03 10:38:00 -040041
42 /** Call after init() to issue draws to the GrMeshDrawOp::Target.*/
43 void recordDraw(Target*, const GrGeometryProcessor*, const GrPipeline*);
44
45 private:
46 GrMesh fMesh;
47 };
48
49 static const int kVerticesPerQuad = 4;
50 static const int kIndicesPerQuad = 6;
51
52 /** A specialization of InstanceHelper for quad rendering. */
Chris Daltonff926502017-05-03 14:36:54 -040053 class QuadHelper : private PatternHelper {
Brian Salomond3ccb0a2017-04-03 10:38:00 -040054 public:
Chris Dalton3809bab2017-06-13 10:55:06 -060055 QuadHelper() : INHERITED(GrPrimitiveType::kTriangles) {}
Brian Salomond3ccb0a2017-04-03 10:38:00 -040056 /** Finds the cached quad index buffer and reserves vertex space. Returns nullptr on failure
57 and on success a pointer to the vertex data that the caller should populate before
58 calling recordDraws(). */
59 void* init(Target*, size_t vertexStride, int quadsToDraw);
60
Chris Daltonff926502017-05-03 14:36:54 -040061 using PatternHelper::recordDraw;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040062
63 private:
Chris Daltonff926502017-05-03 14:36:54 -040064 typedef PatternHelper INHERITED;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040065 };
66
67private:
68 void onPrepare(GrOpFlushState* state) final;
69 void onExecute(GrOpFlushState* state) final;
Brian Salomon91326c32017-08-09 16:02:19 -040070 virtual void onPrepareDraws(Target*) = 0;
Brian Salomond3ccb0a2017-04-03 10:38:00 -040071 typedef GrDrawOp INHERITED;
72};
73
Brian Salomon29b60c92017-10-31 14:42:10 -040074class GrMeshDrawOp::Target {
75public:
76 virtual ~Target() {}
77
78 /** Adds a draw of a mesh. */
79 virtual void draw(const GrGeometryProcessor*, const GrPipeline*, const GrMesh&) = 0;
80
81 /**
82 * Makes space for vertex data. The returned pointer is the location where vertex data
83 * should be written. On return the buffer that will hold the data as well as an offset into
84 * the buffer (in 'vertexSize' units) where the data will be placed.
85 */
86 virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, const GrBuffer**,
87 int* startVertex) = 0;
88
89 /**
90 * Makes space for index data. The returned pointer is the location where index data
91 * should be written. On return the buffer that will hold the data as well as an offset into
92 * the buffer (in uint16_t units) where the data will be placed.
93 */
94 virtual uint16_t* makeIndexSpace(int indexCount, const GrBuffer**, int* startIndex) = 0;
95
96 /**
97 * This is similar to makeVertexSpace. It allows the caller to use up to 'actualVertexCount'
98 * vertices in the returned pointer, which may exceed 'minVertexCount'.
99 * 'fallbackVertexCount' is the maximum number of vertices that should be allocated if a new
100 * buffer is allocated on behalf of this request.
101 */
102 virtual void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
103 int fallbackVertexCount, const GrBuffer**,
104 int* startVertex, int* actualVertexCount) = 0;
105
106 /**
107 * This is similar to makeIndexSpace. It allows the caller to use up to 'actualIndexCount'
108 * indices in the returned pointer, which may exceed 'minIndexCount'.
109 * 'fallbackIndexCount' is the maximum number of indices that should be allocated if a new
110 * buffer is allocated on behalf of this request.
111 */
112 virtual uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
113 const GrBuffer**, int* startIndex,
114 int* actualIndexCount) = 0;
115
116 /** Helpers for ops which over-allocate and then return excess data to the pool. */
117 virtual void putBackIndices(int indices) = 0;
118 virtual void putBackVertices(int vertices, size_t vertexStride) = 0;
119
120 /**
121 * Allocate space for a pipeline. The target ensures this pipeline lifetime is at least
122 * as long as any deferred execution of draws added via draw().
123 * @tparam Args
124 * @param args
125 * @return
126 */
127 template <typename... Args>
128 GrPipeline* allocPipeline(Args&&... args) {
129 return this->pipelineArena()->make<GrPipeline>(std::forward<Args>(args)...);
130 }
131
132 /**
133 * Helper that makes a pipeline targeting the op's render target that incorporates the op's
134 * GrAppliedClip.
Brian Salomon7dc6e752017-11-02 11:34:51 -0400135 */
Brian Salomon29b60c92017-10-31 14:42:10 -0400136 GrPipeline* makePipeline(uint32_t pipelineFlags, GrProcessorSet&& processorSet,
137 GrAppliedClip&& clip) {
138 GrPipeline::InitArgs pipelineArgs;
139 pipelineArgs.fFlags = pipelineFlags;
140 pipelineArgs.fProxy = this->proxy();
141 pipelineArgs.fDstProxy = this->dstProxy();
142 pipelineArgs.fCaps = &this->caps();
143 pipelineArgs.fResourceProvider = this->resourceProvider();
144 return this->allocPipeline(pipelineArgs, std::move(processorSet), std::move(clip));
145 }
146
147 virtual GrRenderTargetProxy* proxy() const = 0;
148
149 virtual GrAppliedClip detachAppliedClip() = 0;
150
151 virtual const GrXferProcessor::DstProxy& dstProxy() const = 0;
152
153 virtual GrResourceProvider* resourceProvider() const = 0;
154
155 virtual const GrCaps& caps() const = 0;
156
157 virtual GrDeferredUploadTarget* deferredUploadTarget() = 0;
158
159private:
160 virtual SkArenaAlloc* pipelineArena() = 0;
161};
162
bsalomon16b99132015-08-13 14:55:50 -0700163#endif