xref: /aosp_15_r20/external/skia/src/gpu/ganesh/mock/GrMockOpTarget.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 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 GrMockOpTarget_DEFINED
9 #define GrMockOpTarget_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/gpu/ganesh/GrDirectContext.h"
13 #include "include/private/base/SkAssert.h"
14 #include "include/private/base/SkTArray.h"
15 #include "include/private/gpu/ganesh/GrTypesPriv.h"
16 #include "src/base/SkArenaAlloc.h"
17 #include "src/gpu/ganesh/GrAppliedClip.h"
18 #include "src/gpu/ganesh/GrBuffer.h"
19 #include "src/gpu/ganesh/GrDirectContextPriv.h"
20 #include "src/gpu/ganesh/GrDrawIndirectCommand.h"
21 #include "src/gpu/ganesh/GrDstProxyView.h"
22 #include "src/gpu/ganesh/GrGpu.h"
23 #include "src/gpu/ganesh/GrGpuBuffer.h"
24 #include "src/gpu/ganesh/GrMeshDrawTarget.h"
25 #include "src/gpu/ganesh/GrSimpleMesh.h"
26 #include "src/gpu/ganesh/GrXferProcessor.h"
27 
28 #include <cstddef>
29 #include <cstdint>
30 #include <utility>
31 
32 class GrAtlasManager;
33 class GrCaps;
34 class GrDeferredUploadTarget;
35 class GrGeometryProcessor;
36 class GrRenderTargetProxy;
37 class GrResourceProvider;
38 class GrSurfaceProxy;
39 class GrSurfaceProxyView;
40 class GrThreadSafeCache;
41 
42 namespace skgpu { namespace ganesh { class SmallPathAtlasMgr; } }
43 namespace sktext { namespace gpu { class StrikeCache; } }
44 
45 
46 // This is a mock GrMeshDrawTarget implementation that just gives back pointers into
47 // pre-allocated CPU buffers, rather than allocating and mapping GPU buffers.
48 class GrMockOpTarget : public GrMeshDrawTarget {
49 public:
GrMockOpTarget(sk_sp<GrDirectContext> mockContext)50     GrMockOpTarget(sk_sp<GrDirectContext> mockContext) : fMockContext(std::move(mockContext)) {
51         fStaticVertexBuffer = fMockContext->priv().getGpu()->createBuffer(
52                 sizeof(fStaticVertexData), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
53         fStaticIndirectBuffer = fMockContext->priv().getGpu()->createBuffer(
54                 sizeof(fStaticIndirectData), GrGpuBufferType::kDrawIndirect,
55                 kDynamic_GrAccessPattern);
56     }
mockContext()57     const GrDirectContext* mockContext() const { return fMockContext.get(); }
caps()58     const GrCaps& caps() const override { return *fMockContext->priv().caps(); }
threadSafeCache()59     GrThreadSafeCache* threadSafeCache() const override {
60         return fMockContext->priv().threadSafeCache();
61     }
resourceProvider()62     GrResourceProvider* resourceProvider() const override {
63         return fMockContext->priv().resourceProvider();
64     }
65 #ifndef SK_ENABLE_OPTIMIZE_SIZE
smallPathAtlasManager()66     skgpu::ganesh::SmallPathAtlasMgr* smallPathAtlasManager() const override { return nullptr; }
67 #endif
resetAllocator()68     void resetAllocator() { fAllocator.reset(); }
allocator()69     SkArenaAlloc* allocator() override { return &fAllocator; }
putBackVertices(int vertices,size_t vertexStride)70     void putBackVertices(int vertices, size_t vertexStride) override { /* no-op */ }
detachAppliedClip()71     GrAppliedClip detachAppliedClip() override { return GrAppliedClip::Disabled(); }
dstProxyView()72     const GrDstProxyView& dstProxyView() const override { return fDstProxyView; }
renderPassBarriers()73     GrXferBarrierFlags renderPassBarriers() const override { return GrXferBarrierFlags::kNone; }
colorLoadOp()74     GrLoadOp colorLoadOp() const override { return GrLoadOp::kLoad; }
75 
makeVertexSpace(size_t vertexSize,int vertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex)76     void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>* buffer,
77                           int* startVertex) override {
78         if (vertexSize * vertexCount > sizeof(fStaticVertexData)) {
79             SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
80                      vertexSize * vertexCount, sizeof(fStaticVertexData));
81         }
82         *buffer = fStaticVertexBuffer;
83         *startVertex = 0;
84         return fStaticVertexData;
85     }
86 
makeVertexSpaceAtLeast(size_t vertexSize,int minVertexCount,int fallbackVertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex,int * actualVertexCount)87     void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
88                                  sk_sp<const GrBuffer>* buffer, int* startVertex,
89                                  int* actualVertexCount) override {
90         if (vertexSize * minVertexCount > sizeof(fStaticVertexData)) {
91             SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
92                      vertexSize * minVertexCount, sizeof(fStaticVertexData));
93         }
94         *buffer = fStaticVertexBuffer;
95         *startVertex = 0;
96         *actualVertexCount = sizeof(fStaticVertexData) / vertexSize;
97         return fStaticVertexData;
98     }
99 
makeDrawIndirectSpace(int drawCount,sk_sp<const GrBuffer> * buffer,size_t * offsetInBytes)100     GrDrawIndirectWriter makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
101                                                size_t* offsetInBytes) override {
102         if (sizeof(GrDrawIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) {
103             SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n",
104                      sizeof(GrDrawIndirectCommand) * drawCount, sizeof(fStaticIndirectData));
105         }
106         *buffer = fStaticIndirectBuffer;
107         *offsetInBytes = 0;
108         return fStaticIndirectData;
109     }
110 
putBackIndirectDraws(int count)111     void putBackIndirectDraws(int count) override { /* no-op */ }
112 
makeDrawIndexedIndirectSpace(int drawCount,sk_sp<const GrBuffer> * buffer,size_t * offsetInBytes)113     GrDrawIndexedIndirectWriter makeDrawIndexedIndirectSpace(int drawCount,
114                                                              sk_sp<const GrBuffer>* buffer,
115                                                              size_t* offsetInBytes) override {
116         if (sizeof(GrDrawIndexedIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) {
117             SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n",
118                      sizeof(GrDrawIndexedIndirectCommand) * drawCount, sizeof(fStaticIndirectData));
119         }
120         *buffer = fStaticIndirectBuffer;
121         *offsetInBytes = 0;
122         return fStaticIndirectData;
123     }
124 
putBackIndexedIndirectDraws(int count)125     void putBackIndexedIndirectDraws(int count) override { /* no-op */ }
126 
127     // Call these methods to see what got written after the previous call to make*Space.
peekStaticVertexData()128     const void* peekStaticVertexData() const { return fStaticVertexData; }
peekStaticIndirectData()129     const void* peekStaticIndirectData() const { return fStaticIndirectData; }
130 
131 #define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); }
132     UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int,
133                            const GrSurfaceProxy* const[], GrPrimitiveType))
134     UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*))
135     UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*))
136     UNIMPL(void putBackIndices(int))
137     UNIMPL(GrRenderTargetProxy* rtProxy() const)
138     UNIMPL(const GrSurfaceProxyView& writeView() const)
139     UNIMPL(const GrAppliedClip* appliedClip() const)
140     UNIMPL(bool usesMSAASurface() const)
141     UNIMPL(sktext::gpu::StrikeCache* strikeCache() const)
142     UNIMPL(GrAtlasManager* atlasManager() const)
143     UNIMPL(skia_private::TArray<GrSurfaceProxy*, true>* sampledProxyArray())
144     UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
145 #undef UNIMPL
146 
147 private:
148     sk_sp<GrDirectContext> fMockContext;
149     char fStaticVertexData[6 * 1024 * 1024];
150     sk_sp<GrGpuBuffer> fStaticVertexBuffer;
151     char fStaticIndirectData[sizeof(GrDrawIndexedIndirectCommand) * 32];
152     sk_sp<GrGpuBuffer> fStaticIndirectBuffer;
153     SkSTArenaAllocWithReset<1024 * 1024> fAllocator;
154     GrDstProxyView fDstProxyView;
155 };
156 
157 #endif
158