xref: /aosp_15_r20/external/skia/src/gpu/ganesh/GrMeshDrawTarget.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2021 Google LLC
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 GrMeshDrawTarget_DEFINED
9 #define GrMeshDrawTarget_DEFINED
10 
11 #include "include/core/SkRefCnt.h"  // IWYU pragma: keep
12 #include "include/private/base/SkTArray.h"
13 #include "src/base/SkArenaAlloc.h"
14 #include "src/gpu/ganesh/GrAppliedClip.h"
15 #include "src/gpu/ganesh/GrDrawIndirectCommand.h"
16 #include "src/gpu/ganesh/GrSimpleMesh.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 class GrAtlasManager;
22 class GrBuffer;
23 class GrCaps;
24 class GrDeferredUploadTarget;
25 class GrDstProxyView;
26 class GrGeometryProcessor;
27 class GrRenderTargetProxy;
28 class GrResourceProvider;
29 class GrSurfaceProxy;
30 class GrSurfaceProxyView;
31 class GrThreadSafeCache;
32 enum class GrLoadOp;
33 enum class GrPrimitiveType : uint8_t;
34 enum class GrXferBarrierFlags;
35 
36 namespace skgpu::ganesh {
37 class SmallPathAtlasMgr;
38 }
39 
40 namespace skgpu {
41 struct IndexWriter;
42 struct VertexWriter;
43 }  // namespace skgpu
44 
45 namespace sktext::gpu {
46 class StrikeCache;
47 }
48 
49 /*
50  * Abstract interface that supports creating vertices, indices, and meshes, as well as
51  * invoking GPU draw operations.
52  */
53 class GrMeshDrawTarget {
54 public:
~GrMeshDrawTarget()55     virtual ~GrMeshDrawTarget() {}
56 
57     /** Adds a draw of a mesh. 'primProcProxies' must have
58      * GrGeometryProcessor::numTextureSamplers() entries. Can be null if no samplers.
59      */
60     virtual void recordDraw(const GrGeometryProcessor*,
61                             const GrSimpleMesh[],
62                             int meshCnt,
63                             const GrSurfaceProxy* const primProcProxies[],
64                             GrPrimitiveType) = 0;
65 
66     /**
67      * Helper for drawing GrSimpleMesh(es) with zero primProc textures.
68      */
recordDraw(const GrGeometryProcessor * gp,const GrSimpleMesh meshes[],int meshCnt,GrPrimitiveType primitiveType)69     void recordDraw(const GrGeometryProcessor* gp,
70                     const GrSimpleMesh meshes[],
71                     int meshCnt,
72                     GrPrimitiveType primitiveType) {
73         this->recordDraw(gp, meshes, meshCnt, nullptr, primitiveType);
74     }
75 
76     /**
77      * Makes space for vertex data. The returned pointer is the location where vertex data
78      * should be written. On return the buffer that will hold the data as well as an offset into
79      * the buffer (in 'vertexSize' units) where the data will be placed.
80      */
81     virtual void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*,
82                                   int* startVertex) = 0;
83 
84     /**
85      * Makes space for index data. The returned pointer is the location where index data
86      * should be written. On return the buffer that will hold the data as well as an offset into
87      * the buffer (in uint16_t units) where the data will be placed.
88      */
89     virtual uint16_t* makeIndexSpace(int indexCount, sk_sp<const GrBuffer>*, int* startIndex) = 0;
90 
91     /**
92      * This is similar to makeVertexSpace. It allows the caller to use up to 'actualVertexCount'
93      * vertices in the returned pointer, which may exceed 'minVertexCount'.
94      * 'fallbackVertexCount' is the maximum number of vertices that should be allocated if a new
95      * buffer is allocated on behalf of this request.
96      */
97     virtual void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount,
98                                          int fallbackVertexCount, sk_sp<const GrBuffer>*,
99                                          int* startVertex, int* actualVertexCount) = 0;
100 
101     /**
102      * This is similar to makeIndexSpace. It allows the caller to use up to 'actualIndexCount'
103      * indices in the returned pointer, which may exceed 'minIndexCount'.
104      * 'fallbackIndexCount' is the maximum number of indices that should be allocated if a new
105      * buffer is allocated on behalf of this request.
106      */
107     virtual uint16_t* makeIndexSpaceAtLeast(int minIndexCount, int fallbackIndexCount,
108                                             sk_sp<const GrBuffer>*, int* startIndex,
109                                             int* actualIndexCount) = 0;
110 
111     /**
112      * Makes space for elements in a draw-indirect buffer. Upon success, the returned pointer is a
113      * CPU mapping where the data should be written.
114      */
115     virtual GrDrawIndirectWriter makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
116                                                        size_t* offsetInBytes) = 0;
117 
118     /**
119      * Makes space for elements in a draw-indexed-indirect buffer. Upon success, the returned
120      * pointer is a CPU mapping where the data should be written.
121      */
122     virtual GrDrawIndexedIndirectWriter makeDrawIndexedIndirectSpace(int drawCount,
123                                                                      sk_sp<const GrBuffer>*,
124                                                                      size_t* offsetInBytes) = 0;
125 
126     /** Helpers for ops that only need to use the VertexWriter to fill the data directly. */
127     skgpu::VertexWriter makeVertexWriter(size_t vertexSize, int vertexCount,
128                                          sk_sp<const GrBuffer>*, int* startVertex);
129     skgpu::IndexWriter makeIndexWriter(int indexCount, sk_sp<const GrBuffer>*, int* startIndex);
130     skgpu::VertexWriter makeVertexWriterAtLeast(size_t vertexSize, int minVertexCount,
131                                                 int fallbackVertexCount, sk_sp<const GrBuffer>*,
132                                                 int* startVertex, int* actualVertexCount);
133     skgpu::IndexWriter makeIndexWriterAtLeast(int minIndexCount, int fallbackIndexCount,
134                                               sk_sp<const GrBuffer>*, int* startIndex,
135                                               int* actualIndexCount);
136 
137     /** Helpers for ops which over-allocate and then return excess data to the pool. */
138     virtual void putBackIndices(int indices) = 0;
139     virtual void putBackVertices(int vertices, size_t vertexStride) = 0;
140     virtual void putBackIndirectDraws(int count) = 0;
141     virtual void putBackIndexedIndirectDraws(int count) = 0;
142 
allocMesh()143     GrSimpleMesh* allocMesh() { return this->allocator()->make<GrSimpleMesh>(); }
allocMeshes(int n)144     GrSimpleMesh* allocMeshes(int n) { return this->allocator()->makeArray<GrSimpleMesh>(n); }
allocPrimProcProxyPtrs(int n)145     const GrSurfaceProxy** allocPrimProcProxyPtrs(int n) {
146         return this->allocator()->makeArray<const GrSurfaceProxy*>(n);
147     }
148 
149     virtual GrRenderTargetProxy* rtProxy() const = 0;
150     virtual const GrSurfaceProxyView& writeView() const = 0;
151 
152     virtual const GrAppliedClip* appliedClip() const = 0;
153     virtual GrAppliedClip detachAppliedClip() = 0;
154 
155     virtual const GrDstProxyView& dstProxyView() const = 0;
156     virtual bool usesMSAASurface() const = 0;
157 
158     virtual GrXferBarrierFlags renderPassBarriers() const = 0;
159 
160     virtual GrLoadOp colorLoadOp() const = 0;
161 
162     virtual GrThreadSafeCache* threadSafeCache() const = 0;
163     virtual GrResourceProvider* resourceProvider() const = 0;
164     uint32_t contextUniqueID() const;
165 
166     virtual sktext::gpu::StrikeCache* strikeCache() const = 0;
167     virtual GrAtlasManager* atlasManager() const = 0;
168 #if !defined(SK_ENABLE_OPTIMIZE_SIZE)
169     virtual skgpu::ganesh::SmallPathAtlasMgr* smallPathAtlasManager() const = 0;
170 #endif
171 
172     // This should be called during onPrepare of a GrOp. The caller should add any proxies to the
173     // array it will use that it did not access during a call to visitProxies. This is usually the
174     // case for atlases.
175     virtual skia_private::TArray<GrSurfaceProxy*, true>* sampledProxyArray() = 0;
176 
177     virtual const GrCaps& caps() const = 0;
178 
179     virtual GrDeferredUploadTarget* deferredUploadTarget() = 0;
180 
181     virtual SkArenaAlloc* allocator() = 0;
182 };
183 
184 #endif
185