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 | |
Robert Phillips | 03e4c95 | 2019-11-26 16:20:22 -0500 | [diff] [blame] | 11 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrAppliedClip.h" |
| 13 | #include "src/gpu/GrGeometryProcessor.h" |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 14 | #include "src/gpu/GrSimpleMesh.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "src/gpu/ops/GrDrawOp.h" |
Brian Osman | 7d8f82b | 2018-11-08 10:24:09 -0500 | [diff] [blame] | 16 | #include <type_traits> |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 17 | |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 18 | class GrAtlasManager; |
Brian Salomon | 54d212e | 2017-03-21 14:22:38 -0400 | [diff] [blame] | 19 | class GrCaps; |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 20 | class GrStrikeCache; |
Brian Salomon | 742e31d | 2016-12-07 17:06:19 -0500 | [diff] [blame] | 21 | class GrOpFlushState; |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 22 | |
| 23 | /** |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 24 | * Base class for mesh-drawing GrDrawOps. |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 25 | */ |
Brian Salomon | dad2923 | 2016-12-01 16:40:24 -0500 | [diff] [blame] | 26 | class GrMeshDrawOp : public GrDrawOp { |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 27 | public: |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 28 | /** Abstract interface that represents a destination for a GrMeshDrawOp. */ |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 29 | class Target; |
| 30 | |
Robert Phillips | bbd459d | 2019-10-29 14:40:03 -0400 | [diff] [blame] | 31 | static bool CanUpgradeAAOnMerge(GrAAType aa1, GrAAType aa2) { |
| 32 | return (aa1 == GrAAType::kNone && aa2 == GrAAType::kCoverage) || |
| 33 | (aa1 == GrAAType::kCoverage && aa2 == GrAAType::kNone); |
| 34 | } |
| 35 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 36 | protected: |
| 37 | GrMeshDrawOp(uint32_t classID); |
| 38 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 39 | void createProgramInfo(const GrCaps* caps, |
| 40 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 41 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 42 | GrAppliedClip&& appliedClip, |
| 43 | const GrXferProcessor::DstProxyView& dstProxyView) { |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 44 | this->onCreateProgramInfo(caps, arena, writeView, std::move(appliedClip), dstProxyView); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void createProgramInfo(Target* target); |
| 48 | |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 49 | /** Helper for rendering repeating meshes using a patterned index buffer. This class creates the |
| 50 | space for the vertices and flushes the draws to the GrMeshDrawOp::Target. */ |
| 51 | class PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 52 | public: |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 53 | PatternHelper(Target*, GrPrimitiveType, size_t vertexStride, |
| 54 | sk_sp<const GrBuffer> indexBuffer, int verticesPerRepetition, |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 55 | int indicesPerRepetition, int repeatCount, int maxRepetitions); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 56 | |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 57 | /** Called to issue draws to the GrMeshDrawOp::Target.*/ |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 58 | void recordDraw(Target*, const GrGeometryProcessor*) const; |
| 59 | void recordDraw(Target*, const GrGeometryProcessor*, |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 60 | const GrSurfaceProxy* const primProcProxies[]) const; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 61 | |
| 62 | void* vertices() const { return fVertices; } |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 63 | GrSimpleMesh* mesh() { return fMesh; } |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 64 | |
| 65 | protected: |
| 66 | PatternHelper() = default; |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 67 | void init(Target*, GrPrimitiveType, size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, |
Robert Phillips | ee08d52 | 2019-10-28 16:34:44 -0400 | [diff] [blame] | 68 | int verticesPerRepetition, int indicesPerRepetition, int repeatCount, |
| 69 | int maxRepetitions); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 70 | |
| 71 | private: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 72 | void* fVertices = nullptr; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 73 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | cea290f | 2019-11-06 11:21:03 -0500 | [diff] [blame] | 74 | GrPrimitiveType fPrimitiveType; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 75 | }; |
| 76 | |
Robert Phillips | e94cdd2 | 2019-11-04 14:15:58 -0500 | [diff] [blame] | 77 | /** A specialization of InstanceHelper for quad rendering. |
| 78 | * It only draws non-antialiased indexed quads. |
| 79 | */ |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 80 | class QuadHelper : private PatternHelper { |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 81 | public: |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 82 | QuadHelper() = delete; |
| 83 | QuadHelper(Target* target, size_t vertexStride, int quadsToDraw); |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 84 | |
Robert Phillips | ce97857 | 2020-02-28 11:56:44 -0500 | [diff] [blame] | 85 | using PatternHelper::mesh; |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 86 | using PatternHelper::recordDraw; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 87 | using PatternHelper::vertices; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 88 | |
| 89 | private: |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 90 | typedef PatternHelper INHERITED; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 91 | }; |
| 92 | |
Robert Phillips | bbd459d | 2019-10-29 14:40:03 -0400 | [diff] [blame] | 93 | static bool CombinedQuadCountWillOverflow(GrAAType aaType, |
| 94 | bool willBeUpgradedToAA, |
| 95 | int combinedQuadCount) { |
| 96 | bool willBeAA = (aaType == GrAAType::kCoverage) || willBeUpgradedToAA; |
| 97 | |
| 98 | return combinedQuadCount > (willBeAA ? GrResourceProvider::MaxNumAAQuads() |
| 99 | : GrResourceProvider::MaxNumNonAAQuads()); |
| 100 | } |
| 101 | |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 102 | virtual void onPrePrepareDraws(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 103 | const GrSurfaceProxyView* writeView, |
Chris Dalton | dbb833b | 2020-03-17 12:15:46 -0600 | [diff] [blame] | 104 | GrAppliedClip*, |
| 105 | const GrXferProcessor::DstProxyView&); |
| 106 | |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 107 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 108 | virtual GrProgramInfo* programInfo() = 0; |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 109 | // This method is responsible for creating all the programInfos required |
| 110 | // by this op. |
| 111 | virtual void onCreateProgramInfo(const GrCaps*, |
| 112 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 113 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 114 | GrAppliedClip&&, |
| 115 | const GrXferProcessor::DstProxyView&) = 0; |
| 116 | |
Robert Phillips | df70f15 | 2019-11-15 14:57:05 -0500 | [diff] [blame] | 117 | void onPrePrepare(GrRecordingContext* context, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 118 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 119 | GrAppliedClip* clip, |
| 120 | const GrXferProcessor::DstProxyView& dstProxyView) final { |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 121 | this->onPrePrepareDraws(context, writeView, clip, dstProxyView); |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame] | 122 | } |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 123 | void onPrepare(GrOpFlushState* state) final; |
Robert Phillips | 7327c9d | 2019-10-08 16:32:56 -0400 | [diff] [blame] | 124 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 125 | virtual void onPrepareDraws(Target*) = 0; |
Brian Salomon | d3ccb0a | 2017-04-03 10:38:00 -0400 | [diff] [blame] | 126 | typedef GrDrawOp INHERITED; |
| 127 | }; |
| 128 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 129 | class GrMeshDrawOp::Target { |
| 130 | public: |
| 131 | virtual ~Target() {} |
| 132 | |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 133 | /** Adds a draw of a mesh. 'primProcProxies' must have |
| 134 | * GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers. |
| 135 | */ |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 136 | virtual void recordDraw(const GrGeometryProcessor*, |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 137 | const GrSimpleMesh[], |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 138 | int meshCnt, |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 139 | const GrSurfaceProxy* const primProcProxies[], |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 140 | GrPrimitiveType) = 0; |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 143 | * Helper for drawing GrSimpleMesh(es) with zero primProc textures. |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 144 | */ |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 145 | void recordDraw(const GrGeometryProcessor* gp, |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 146 | const GrSimpleMesh meshes[], |
Robert Phillips | 6c59fe4 | 2020-02-27 09:30:37 -0500 | [diff] [blame] | 147 | int meshCnt, |
Robert Phillips | cea290f | 2019-11-06 11:21:03 -0500 | [diff] [blame] | 148 | GrPrimitiveType primitiveType) { |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 149 | this->recordDraw(gp, meshes, meshCnt, nullptr, primitiveType); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 150 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Makes space for vertex data. The returned pointer is the location where vertex data |
| 154 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 155 | * the buffer (in 'vertexSize' units) where the data will be placed. |
| 156 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 157 | virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 158 | int* startVertex) = 0; |
| 159 | |
| 160 | /** |
| 161 | * Makes space for index data. The returned pointer is the location where index data |
| 162 | * should be written. On return the buffer that will hold the data as well as an offset into |
| 163 | * the buffer (in uint16_t units) where the data will be placed. |
| 164 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 165 | 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] | 166 | |
| 167 | /** |
| 168 | * This is similar to makeVertexSpace. It allows the caller to use up to 'actualVertexCount' |
| 169 | * vertices in the returned pointer, which may exceed 'minVertexCount'. |
| 170 | * 'fallbackVertexCount' is the maximum number of vertices that should be allocated if a new |
| 171 | * buffer is allocated on behalf of this request. |
| 172 | */ |
| 173 | virtual void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 174 | int fallbackVertexCount, sk_sp<const GrBuffer>*, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 175 | int* startVertex, int* actualVertexCount) = 0; |
| 176 | |
| 177 | /** |
| 178 | * This is similar to makeIndexSpace. It allows the caller to use up to 'actualIndexCount' |
| 179 | * indices in the returned pointer, which may exceed 'minIndexCount'. |
| 180 | * 'fallbackIndexCount' is the maximum number of indices that should be allocated if a new |
| 181 | * buffer is allocated on behalf of this request. |
| 182 | */ |
| 183 | virtual uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 184 | sk_sp<const GrBuffer>*, int* startIndex, |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 185 | int* actualIndexCount) = 0; |
| 186 | |
| 187 | /** Helpers for ops which over-allocate and then return excess data to the pool. */ |
| 188 | virtual void putBackIndices(int indices) = 0; |
| 189 | virtual void putBackVertices(int vertices, size_t vertexStride) = 0; |
| 190 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 191 | GrSimpleMesh* allocMesh() { return this->allocator()->make<GrSimpleMesh>(); } |
| 192 | GrSimpleMesh* allocMeshes(int n) { return this->allocator()->makeArray<GrSimpleMesh>(n); } |
Chris Dalton | 304e14d | 2020-03-17 14:29:06 -0600 | [diff] [blame] | 193 | const GrSurfaceProxy** allocPrimProcProxyPtrs(int n) { |
| 194 | return this->allocator()->makeArray<const GrSurfaceProxy*>(n); |
Robert Phillips | 61fc799 | 2019-10-22 11:58:17 -0400 | [diff] [blame] | 195 | } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 196 | |
| 197 | virtual GrRenderTargetProxy* proxy() const = 0; |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 198 | virtual const GrSurfaceProxyView* writeView() const = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 199 | |
Robert Phillips | 8053c97 | 2019-11-21 10:44:53 -0500 | [diff] [blame] | 200 | virtual const GrAppliedClip* appliedClip() const = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 201 | virtual GrAppliedClip detachAppliedClip() = 0; |
| 202 | |
Greg Daniel | 524e28b | 2019-11-01 11:48:53 -0400 | [diff] [blame] | 203 | virtual const GrXferProcessor::DstProxyView& dstProxyView() const = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 204 | |
| 205 | virtual GrResourceProvider* resourceProvider() const = 0; |
Brian Salomon | 238069b | 2018-07-11 15:58:57 -0400 | [diff] [blame] | 206 | uint32_t contextUniqueID() const { return this->resourceProvider()->contextUniqueID(); } |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 207 | |
Herb Derby | 081e6f3 | 2019-01-16 13:46:02 -0500 | [diff] [blame] | 208 | virtual GrStrikeCache* glyphCache() const = 0; |
Robert Phillips | 5a66efb | 2018-03-07 15:13:18 -0500 | [diff] [blame] | 209 | virtual GrAtlasManager* atlasManager() const = 0; |
Robert Phillips | c4039ea | 2018-03-01 11:36:45 -0500 | [diff] [blame] | 210 | |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 211 | // This should be called during onPrepare of a GrOp. The caller should add any proxies to the |
| 212 | // array it will use that it did not access during a call to visitProxies. This is usually the |
| 213 | // case for atlases. |
Michael Ludwig | fcdd061 | 2019-11-25 08:34:31 -0500 | [diff] [blame] | 214 | virtual SkTArray<GrSurfaceProxy*, true>* sampledProxyArray() = 0; |
Greg Daniel | b20d7e5 | 2019-09-03 13:54:39 -0400 | [diff] [blame] | 215 | |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 216 | virtual const GrCaps& caps() const = 0; |
| 217 | |
| 218 | virtual GrDeferredUploadTarget* deferredUploadTarget() = 0; |
| 219 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 220 | virtual SkArenaAlloc* allocator() = 0; |
Brian Salomon | 29b60c9 | 2017-10-31 14:42:10 -0400 | [diff] [blame] | 221 | }; |
| 222 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 223 | #endif |