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