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 | |
8 | #ifndef GrMeshDrawOp_DEFINED |
9 | #define GrMeshDrawOp_DEFINED |
10 | |
11 | #include "src/core/SkArenaAlloc.h" |
12 | #include "src/gpu/GrAppliedClip.h" |
13 | #include "src/gpu/GrGeometryProcessor.h" |
14 | #include "src/gpu/GrSimpleMesh.h" |
15 | #include "src/gpu/ops/GrDrawOp.h" |
16 | #include <type_traits> |
17 | |
18 | class GrAtlasManager; |
19 | class GrCaps; |
20 | class GrStrikeCache; |
21 | class GrOpFlushState; |
22 | |
23 | /** |
24 | * Base class for mesh-drawing GrDrawOps. |
25 | */ |
26 | class GrMeshDrawOp : public GrDrawOp { |
27 | public: |
28 | /** Abstract interface that represents a destination for a GrMeshDrawOp. */ |
29 | class Target; |
30 | |
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 | |
36 | protected: |
37 | GrMeshDrawOp(uint32_t classID); |
38 | |
39 | void createProgramInfo(const GrCaps* caps, |
40 | SkArenaAlloc* arena, |
41 | const GrSurfaceProxyView* writeView, |
42 | GrAppliedClip&& appliedClip, |
43 | const GrXferProcessor::DstProxyView& dstProxyView) { |
44 | this->onCreateProgramInfo(caps, arena, writeView, std::move(appliedClip), dstProxyView); |
45 | } |
46 | |
47 | void createProgramInfo(Target* target); |
48 | |
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 { |
52 | public: |
53 | PatternHelper(Target*, GrPrimitiveType, size_t vertexStride, |
54 | sk_sp<const GrBuffer> indexBuffer, int verticesPerRepetition, |
55 | int indicesPerRepetition, int repeatCount, int maxRepetitions); |
56 | |
57 | /** Called to issue draws to the GrMeshDrawOp::Target.*/ |
58 | void recordDraw(Target*, const GrGeometryProcessor*) const; |
59 | void recordDraw(Target*, const GrGeometryProcessor*, |
60 | const GrSurfaceProxy* const primProcProxies[]) const; |
61 | |
62 | void* vertices() const { return fVertices; } |
63 | GrSimpleMesh* mesh() { return fMesh; } |
64 | |
65 | protected: |
66 | PatternHelper() = default; |
67 | void init(Target*, GrPrimitiveType, size_t vertexStride, sk_sp<const GrBuffer> indexBuffer, |
68 | int verticesPerRepetition, int indicesPerRepetition, int repeatCount, |
69 | int maxRepetitions); |
70 | |
71 | private: |
72 | void* fVertices = nullptr; |
73 | GrSimpleMesh* fMesh = nullptr; |
74 | GrPrimitiveType fPrimitiveType; |
75 | }; |
76 | |
77 | /** A specialization of InstanceHelper for quad rendering. |
78 | * It only draws non-antialiased indexed quads. |
79 | */ |
80 | class QuadHelper : private PatternHelper { |
81 | public: |
82 | QuadHelper() = delete; |
83 | QuadHelper(Target* target, size_t vertexStride, int quadsToDraw); |
84 | |
85 | using PatternHelper::mesh; |
86 | using PatternHelper::recordDraw; |
87 | using PatternHelper::vertices; |
88 | |
89 | private: |
90 | typedef PatternHelper INHERITED; |
91 | }; |
92 | |
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 | |
102 | virtual void onPrePrepareDraws(GrRecordingContext*, |
103 | const GrSurfaceProxyView* writeView, |
104 | GrAppliedClip*, |
105 | const GrXferProcessor::DstProxyView&); |
106 | |
107 | private: |
108 | virtual GrProgramInfo* programInfo() = 0; |
109 | // This method is responsible for creating all the programInfos required |
110 | // by this op. |
111 | virtual void onCreateProgramInfo(const GrCaps*, |
112 | SkArenaAlloc*, |
113 | const GrSurfaceProxyView* writeView, |
114 | GrAppliedClip&&, |
115 | const GrXferProcessor::DstProxyView&) = 0; |
116 | |
117 | void onPrePrepare(GrRecordingContext* context, |
118 | const GrSurfaceProxyView* writeView, |
119 | GrAppliedClip* clip, |
120 | const GrXferProcessor::DstProxyView& dstProxyView) final { |
121 | this->onPrePrepareDraws(context, writeView, clip, dstProxyView); |
122 | } |
123 | void onPrepare(GrOpFlushState* state) final; |
124 | |
125 | virtual void onPrepareDraws(Target*) = 0; |
126 | typedef GrDrawOp INHERITED; |
127 | }; |
128 | |
129 | class GrMeshDrawOp::Target { |
130 | public: |
131 | virtual ~Target() {} |
132 | |
133 | /** Adds a draw of a mesh. 'primProcProxies' must have |
134 | * GrPrimitiveProcessor::numTextureSamplers() entries. Can be null if no samplers. |
135 | */ |
136 | virtual void recordDraw(const GrGeometryProcessor*, |
137 | const GrSimpleMesh[], |
138 | int meshCnt, |
139 | const GrSurfaceProxy* const primProcProxies[], |
140 | GrPrimitiveType) = 0; |
141 | |
142 | /** |
143 | * Helper for drawing GrSimpleMesh(es) with zero primProc textures. |
144 | */ |
145 | void recordDraw(const GrGeometryProcessor* gp, |
146 | const GrSimpleMesh meshes[], |
147 | int meshCnt, |
148 | GrPrimitiveType primitiveType) { |
149 | this->recordDraw(gp, meshes, meshCnt, nullptr, primitiveType); |
150 | } |
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 | */ |
157 | virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
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 | */ |
165 | virtual uint16_t* makeIndexSpace(int indexCount, sk_sp<const GrBuffer>*, int* startIndex) = 0; |
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, |
174 | int fallbackVertexCount, sk_sp<const GrBuffer>*, |
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, |
184 | sk_sp<const GrBuffer>*, int* startIndex, |
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 | |
191 | GrSimpleMesh* allocMesh() { return this->allocator()->make<GrSimpleMesh>(); } |
192 | GrSimpleMesh* allocMeshes(int n) { return this->allocator()->makeArray<GrSimpleMesh>(n); } |
193 | const GrSurfaceProxy** allocPrimProcProxyPtrs(int n) { |
194 | return this->allocator()->makeArray<const GrSurfaceProxy*>(n); |
195 | } |
196 | |
197 | virtual GrRenderTargetProxy* proxy() const = 0; |
198 | virtual const GrSurfaceProxyView* writeView() const = 0; |
199 | |
200 | virtual const GrAppliedClip* appliedClip() const = 0; |
201 | virtual GrAppliedClip detachAppliedClip() = 0; |
202 | |
203 | virtual const GrXferProcessor::DstProxyView& dstProxyView() const = 0; |
204 | |
205 | virtual GrResourceProvider* resourceProvider() const = 0; |
206 | uint32_t contextUniqueID() const { return this->resourceProvider()->contextUniqueID(); } |
207 | |
208 | virtual GrStrikeCache* strikeCache() const = 0; |
209 | virtual GrAtlasManager* atlasManager() const = 0; |
210 | |
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. |
214 | virtual SkTArray<GrSurfaceProxy*, true>* sampledProxyArray() = 0; |
215 | |
216 | virtual const GrCaps& caps() const = 0; |
217 | |
218 | virtual GrDeferredUploadTarget* deferredUploadTarget() = 0; |
219 | |
220 | virtual SkArenaAlloc* allocator() = 0; |
221 | }; |
222 | |
223 | #endif |
224 | |