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