bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 1 | /* |
| 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 Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 8 | #ifndef GrMeshDrawOp_DEFINED |
| 9 | #define GrMeshDrawOp_DEFINED |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 10 | |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 11 | #include "GrDrawOp.h" |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 12 | #include "GrGeometryProcessor.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 13 | #include "GrMesh.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 14 | #include "GrPendingProgramElement.h" |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 15 | |
| 16 | #include "SkTLList.h" |
| 17 | |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 18 | class GrCaps; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 19 | class GrOpFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 20 | |
| 21 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 22 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 23 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 24 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 25 | public: |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 26 | /** Abstract interface that represents a destination for a GrMeshDrawOp. */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 27 | class Target; |
| 28 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 29 | protected: |
| 30 | GrMeshDrawOp(uint32_t classID); |
| 31 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 32 | /** 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 Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 35 | public: |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 36 | PatternHelper(GrPrimitiveType primitiveType) : fMesh(primitiveType) {} |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 37 | /** Returns the allocated storage for the vertices. The caller should populate the vertices |
| 38 | before calling recordDraws(). */ |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 39 | void* init(Target*, size_t vertexStride, const GrBuffer*, int verticesPerRepetition, |
| 40 | int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 41 | |
| 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 Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 53 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 54 | public: |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 55 | QuadHelper() : INHERITED(GrPrimitiveType::kTriangles) {} |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 56 | /** 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 Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 61 | using PatternHelper::recordDraw; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 62 | |
| 63 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 64 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | private: |
| 68 | void onPrepare(GrOpFlushState* state) final; |
| 69 | void onExecute(GrOpFlushState* state) final; |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 70 | virtual void onPrepareDraws(Target*) = 0; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 71 | typedef GrDrawOp INHERITED; |
| 72 | }; |
| 73 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 74 | class GrMeshDrawOp::Target { |
| 75 | public: |
| 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 Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 135 | */ |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 136 | 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 | |
| 159 | private: |
| 160 | virtual SkArenaAlloc* pipelineArena() = 0; |
| 161 | }; |
| 162 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 163 | #endif |