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 | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 11 | #include "GrAppliedClip.h" |
Brian Salomon | 9afd371 | 2016-12-01 10:59:09 -0500 | [diff] [blame] | 12 | #include "GrDrawOp.h" |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 13 | #include "GrGeometryProcessor.h" |
egdaniel | 0e1853c | 2016-03-17 11:35:45 -0700 | [diff] [blame] | 14 | #include "GrMesh.h" |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 15 | #include <type_traits> |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 16 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 17 | class GrAtlasManager; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 18 | class GrCaps; |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 19 | class GrStrikeCache; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 20 | class GrOpFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 21 | |
| 22 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 23 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 25 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 26 | public: |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 27 | /** Abstract interface that represents a destination for a GrMeshDrawOp. */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 28 | class Target; |
| 29 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 30 | protected: |
| 31 | GrMeshDrawOp(uint32_t classID); |
| 32 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 33 | /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the |
| 34 | space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */ |
| 35 | class PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 36 | public: |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 37 | PatternHelper(Target*, GrPrimitiveType, size_t vertexStride, |
| 38 | sk_sp<const GrBuffer> indexBuffer, int verticesPerRepetition, |
| 39 | int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 40 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 41 | /** Called to issue draws to the GrMeshDrawOp::Target.*/ |
| 42 | void recordDraw(Target*, sk_sp<const GrGeometryProcessor>, const GrPipeline*, |
| 43 | const GrPipeline::FixedDynamicState*) const; |
| 44 | |
| 45 | void* vertices() const { return fVertices; } |
| 46 | |
| 47 | protected: |
| 48 | PatternHelper() = default; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 49 | void init(Target*, GrPrimitiveType, size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 50 | int verticesPerRepetition, int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 51 | |
| 52 | private: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 53 | void* fVertices = nullptr; |
| 54 | GrMesh* fMesh = nullptr; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | static const int kVerticesPerQuad = 4; |
| 58 | static const int kIndicesPerQuad = 6; |
| 59 | |
| 60 | /** A specialization of InstanceHelper for quad rendering. */ |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 61 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 62 | public: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 63 | QuadHelper() = delete; |
| 64 | QuadHelper(Target* target, size_t vertexStride, int quadsToDraw); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 65 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 66 | using PatternHelper::recordDraw; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 67 | using PatternHelper::vertices; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 68 | |
| 69 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 70 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | private: |
| 74 | void onPrepare(GrOpFlushState* state) final; |
Brian Salomon | 588cec7 | 2018-11-14 13:56:37 -0500 | [diff] [blame] | 75 | void onExecute(GrOpFlushState* state, const SkRect& chainBounds) final; |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 76 | virtual void onPrepareDraws(Target*) = 0; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 77 | typedef GrDrawOp INHERITED; |
| 78 | }; |
| 79 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 80 | class GrMeshDrawOp::Target { |
| 81 | public: |
| 82 | virtual ~Target() {} |
| 83 | |
| 84 | /** Adds a draw of a mesh. */ |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 85 | virtual void draw(sk_sp<const GrGeometryProcessor>, |
| 86 | const GrPipeline*, |
| 87 | const GrPipeline::FixedDynamicState*, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 88 | const GrPipeline::DynamicStateArrays*, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 89 | const GrMesh[], |
| 90 | int meshCount) = 0; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 91 | /** Helper for drawing a single GrMesh. */ |
| 92 | void draw(sk_sp<const GrGeometryProcessor> gp, |
| 93 | const GrPipeline* pipeline, |
| 94 | const GrPipeline::FixedDynamicState* fixedDynamicState, |
| 95 | const GrMesh* mesh) { |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 96 | this->draw(std::move(gp), pipeline, fixedDynamicState, nullptr, mesh, 1); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 97 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Makes space for vertex data. The returned pointer is the location where vertex data |
| 101 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 102 | * the buffer (in 'vertexSize' units) where the data will be placed. |
| 103 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 104 | virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 105 | int* startVertex) = 0; |
| 106 | |
| 107 | /** |
| 108 | * Makes space for index data. The returned pointer is the location where index data |
| 109 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 110 | * the buffer (in uint16_t units) where the data will be placed. |
| 111 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 112 | virtual uint16_t* makeIndexSpace(int indexCount, sk_sp<const GrBuffer>*, int* startIndex) = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * This is similar to makeVertexSpace. It allows the caller to use up to 'actualVertexCount' |
| 116 | * vertices in the returned pointer, which may exceed 'minVertexCount'. |
| 117 | * 'fallbackVertexCount' is the maximum number of vertices that should be allocated if a new |
| 118 | * buffer is allocated on behalf of this request. |
| 119 | */ |
| 120 | virtual void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 121 | int fallbackVertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 122 | int* startVertex, int* actualVertexCount) = 0; |
| 123 | |
| 124 | /** |
| 125 | * This is similar to makeIndexSpace. It allows the caller to use up to 'actualIndexCount' |
| 126 | * indices in the returned pointer, which may exceed 'minIndexCount'. |
| 127 | * 'fallbackIndexCount' is the maximum number of indices that should be allocated if a new |
| 128 | * buffer is allocated on behalf of this request. |
| 129 | */ |
| 130 | virtual uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 131 | sk_sp<const GrBuffer>*, int* startIndex, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 132 | int* actualIndexCount) = 0; |
| 133 | |
| 134 | /** Helpers for ops which over-allocate and then return excess data to the pool. */ |
| 135 | virtual void putBackIndices(int indices) = 0; |
| 136 | virtual void putBackVertices(int vertices, size_t vertexStride) = 0; |
| 137 | |
| 138 | /** |
| 139 | * Allocate space for a pipeline. The target ensures this pipeline lifetime is at least |
| 140 | * as long as any deferred execution of draws added via draw(). |
| 141 | * @tparam Args |
| 142 | * @param args |
| 143 | * @return |
| 144 | */ |
| 145 | template <typename... Args> |
| 146 | GrPipeline* allocPipeline(Args&&... args) { |
| 147 | return this->pipelineArena()->make<GrPipeline>(std::forward<Args>(args)...); |
| 148 | } |
| 149 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 150 | GrMesh* allocMesh(GrPrimitiveType primitiveType) { |
| 151 | return this->pipelineArena()->make<GrMesh>(primitiveType); |
| 152 | } |
| 153 | |
| 154 | GrMesh* allocMeshes(int n) { return this->pipelineArena()->makeArray<GrMesh>(n); } |
| 155 | |
| 156 | GrPipeline::FixedDynamicState* allocFixedDynamicState(const SkIRect& rect, |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 157 | int numPrimitiveProcessorTextures = 0); |
| 158 | |
| 159 | GrPipeline::DynamicStateArrays* allocDynamicStateArrays(int numMeshes, |
| 160 | int numPrimitiveProcessorTextures, |
| 161 | bool allocScissors); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 162 | |
| 163 | GrTextureProxy** allocPrimitiveProcessorTextureArray(int n) { |
| 164 | SkASSERT(n > 0); |
| 165 | return this->pipelineArena()->makeArrayDefault<GrTextureProxy*>(n); |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Once we have C++17 structured bindings make this just be a tuple because then we can do: |
| 169 | // auto [pipeline, fixedDynamicState] = target->makePipeline(...); |
| 170 | // in addition to: |
| 171 | // std::tie(flushInfo.fPipeline, flushInfo.fFixedState) = target->makePipeline(...); |
| 172 | struct PipelineAndFixedDynamicState { |
| 173 | const GrPipeline* fPipeline; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 174 | GrPipeline::FixedDynamicState* fFixedDynamicState; |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 175 | }; |
| 176 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 177 | /** |
| 178 | * Helper that makes a pipeline targeting the op's render target that incorporates the op's |
Brian Salomon | 4934890 | 2018-06-26 09:12:38 -0400 | [diff] [blame] | 179 | * GrAppliedClip and uses a fixed dynamic state. |
Brian Salomon | 7dc6e75 | 2017-11-02 11:34:51 -0400 | [diff] [blame] | 180 | */ |
Brian Salomon | f513682 | 2018-08-03 09:09:36 -0400 | [diff] [blame] | 181 | PipelineAndFixedDynamicState makePipeline(uint32_t pipelineFlags, GrProcessorSet&&, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 182 | GrAppliedClip&&, |
| 183 | int numPrimitiveProcessorTextures = 0); |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 184 | |
| 185 | virtual GrRenderTargetProxy* proxy() const = 0; |
| 186 | |
| 187 | virtual GrAppliedClip detachAppliedClip() = 0; |
| 188 | |
| 189 | virtual const GrXferProcessor::DstProxy& dstProxy() const = 0; |
| 190 | |
| 191 | virtual GrResourceProvider* resourceProvider() const = 0; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 192 | uint32_t contextUniqueID() const { return this->resourceProvider()->contextUniqueID(); } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 193 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 194 | virtual GrStrikeCache* glyphCache() const = 0; |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 195 | virtual GrAtlasManager* atlasManager() const = 0; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 196 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 197 | virtual const GrCaps& caps() const = 0; |
| 198 | |
| 199 | virtual GrDeferredUploadTarget* deferredUploadTarget() = 0; |
| 200 | |
| 201 | private: |
| 202 | virtual SkArenaAlloc* pipelineArena() = 0; |
| 203 | }; |
| 204 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 205 | #endif |