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