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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrAppliedClip.h" |
| 12 | #include "src/gpu/GrGeometryProcessor.h" |
| 13 | #include "src/gpu/GrMesh.h" |
| 14 | #include "src/gpu/ops/GrDrawOp.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.*/ |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 42 | void recordDraw(Target*, sk_sp<const GrGeometryProcessor>) const; |
| 43 | void recordDraw(Target*, sk_sp<const GrGeometryProcessor>, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 44 | const GrPipeline::FixedDynamicState*) const; |
| 45 | |
| 46 | void* vertices() const { return fVertices; } |
| 47 | |
| 48 | protected: |
| 49 | PatternHelper() = default; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 50 | void init(Target*, GrPrimitiveType, size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 51 | int verticesPerRepetition, int indicesPerRepetition, int repeatCount); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 52 | |
| 53 | private: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 54 | void* fVertices = nullptr; |
| 55 | GrMesh* fMesh = nullptr; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static const int kVerticesPerQuad = 4; |
| 59 | static const int kIndicesPerQuad = 6; |
| 60 | |
| 61 | /** A specialization of InstanceHelper for quad rendering. */ |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 62 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 63 | public: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 64 | QuadHelper() = delete; |
| 65 | QuadHelper(Target* target, size_t vertexStride, int quadsToDraw); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 66 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 67 | using PatternHelper::recordDraw; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 68 | using PatternHelper::vertices; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 69 | |
| 70 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 71 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | private: |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame^] | 75 | void onPrePrepare(GrRecordingContext* context, const GrAppliedClip* clip) final { |
| 76 | this->onPrePrepareDraws(context, clip); |
| 77 | } |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 78 | void onPrepare(GrOpFlushState* state) final; |
Robert Phillips | 7327c9d | 2019-10-08 16:32:56 -0400 | [diff] [blame] | 79 | |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 80 | // Only the GrTextureOp currently overrides this virtual |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame^] | 81 | virtual void onPrePrepareDraws(GrRecordingContext*, const GrAppliedClip*) {} |
Robert Phillips | 29f3854 | 2019-10-16 09:20:25 -0400 | [diff] [blame] | 82 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 83 | virtual void onPrepareDraws(Target*) = 0; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 84 | typedef GrDrawOp INHERITED; |
| 85 | }; |
| 86 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 87 | class GrMeshDrawOp::Target { |
| 88 | public: |
| 89 | virtual ~Target() {} |
| 90 | |
| 91 | /** Adds a draw of a mesh. */ |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 92 | virtual void recordDraw( |
| 93 | sk_sp<const GrGeometryProcessor>, const GrMesh[], int meshCnt, |
| 94 | const GrPipeline::FixedDynamicState*, const GrPipeline::DynamicStateArrays*) = 0; |
| 95 | |
| 96 | /** |
| 97 | * Helper for drawing GrMesh(es) with zero primProc textures and no dynamic state besides the |
| 98 | * scissor clip. |
| 99 | */ |
| 100 | void recordDraw(sk_sp<const GrGeometryProcessor> gp, const GrMesh meshes[], int meshCnt = 1) { |
| 101 | static constexpr int kZeroPrimProcTextures = 0; |
| 102 | auto fixedDynamicState = this->makeFixedDynamicState(kZeroPrimProcTextures); |
| 103 | this->recordDraw(std::move(gp), meshes, meshCnt, fixedDynamicState, nullptr); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 104 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Makes space for vertex data. The returned pointer is the location where vertex data |
| 108 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 109 | * the buffer (in 'vertexSize' units) where the data will be placed. |
| 110 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 111 | virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 112 | int* startVertex) = 0; |
| 113 | |
| 114 | /** |
| 115 | * Makes space for index data. The returned pointer is the location where index data |
| 116 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 117 | * the buffer (in uint16_t units) where the data will be placed. |
| 118 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 119 | 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] | 120 | |
| 121 | /** |
| 122 | * This is similar to makeVertexSpace. It allows the caller to use up to 'actualVertexCount' |
| 123 | * vertices in the returned pointer, which may exceed 'minVertexCount'. |
| 124 | * 'fallbackVertexCount' is the maximum number of vertices that should be allocated if a new |
| 125 | * buffer is allocated on behalf of this request. |
| 126 | */ |
| 127 | virtual void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 128 | int fallbackVertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 129 | int* startVertex, int* actualVertexCount) = 0; |
| 130 | |
| 131 | /** |
| 132 | * This is similar to makeIndexSpace. It allows the caller to use up to 'actualIndexCount' |
| 133 | * indices in the returned pointer, which may exceed 'minIndexCount'. |
| 134 | * 'fallbackIndexCount' is the maximum number of indices that should be allocated if a new |
| 135 | * buffer is allocated on behalf of this request. |
| 136 | */ |
| 137 | virtual uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 138 | sk_sp<const GrBuffer>*, int* startIndex, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 139 | int* actualIndexCount) = 0; |
| 140 | |
| 141 | /** Helpers for ops which over-allocate and then return excess data to the pool. */ |
| 142 | virtual void putBackIndices(int indices) = 0; |
| 143 | virtual void putBackVertices(int vertices, size_t vertexStride) = 0; |
| 144 | |
Chris Dalton | 35a3abe | 2019-02-25 23:11:33 +0000 | [diff] [blame] | 145 | GrMesh* allocMesh(GrPrimitiveType primitiveType) { |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 146 | return this->allocator()->make<GrMesh>(primitiveType); |
Chris Dalton | 35a3abe | 2019-02-25 23:11:33 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 149 | GrMesh* allocMeshes(int n) { return this->allocator()->makeArray<GrMesh>(n); } |
Brian Salomon | f723264 | 2018-09-19 08:58:08 -0400 | [diff] [blame] | 150 | |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame^] | 151 | static GrPipeline::DynamicStateArrays* AllocDynamicStateArrays(SkArenaAlloc*, |
| 152 | int numMeshes, |
| 153 | int numPrimitiveProcTextures, |
| 154 | bool allocScissors); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 155 | |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame^] | 156 | static GrPipeline::FixedDynamicState* MakeFixedDynamicState(SkArenaAlloc*, |
| 157 | const GrAppliedClip* clip, |
| 158 | int numPrimitiveProcessorTextures); |
| 159 | |
| 160 | |
| 161 | GrPipeline::FixedDynamicState* makeFixedDynamicState(int numPrimitiveProcessorTextures) { |
| 162 | return MakeFixedDynamicState(this->allocator(), this->appliedClip(), |
| 163 | numPrimitiveProcessorTextures); |
| 164 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 165 | |
| 166 | virtual GrRenderTargetProxy* proxy() const = 0; |
| 167 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 168 | virtual const GrAppliedClip* appliedClip() = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 169 | virtual GrAppliedClip detachAppliedClip() = 0; |
| 170 | |
| 171 | virtual const GrXferProcessor::DstProxy& dstProxy() const = 0; |
| 172 | |
| 173 | virtual GrResourceProvider* resourceProvider() const = 0; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 174 | uint32_t contextUniqueID() const { return this->resourceProvider()->contextUniqueID(); } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 175 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 176 | virtual GrStrikeCache* glyphCache() const = 0; |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 177 | virtual GrAtlasManager* atlasManager() const = 0; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 178 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 179 | // This should be called during onPrepare of a GrOp. The caller should add any proxies to the |
| 180 | // array it will use that it did not access during a call to visitProxies. This is usually the |
| 181 | // case for atlases. |
| 182 | virtual SkTArray<GrTextureProxy*, true>* sampledProxyArray() = 0; |
| 183 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 184 | virtual const GrCaps& caps() const = 0; |
| 185 | |
| 186 | virtual GrDeferredUploadTarget* deferredUploadTarget() = 0; |
| 187 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 188 | virtual SkArenaAlloc* allocator() = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 189 | }; |
| 190 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 191 | #endif |