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/ganesh/GrAppliedClip.h" 13 #include "src/gpu/ganesh/GrDirectContextPriv.h" 14 #include "src/gpu/ganesh/GrDstProxyView.h" 15 #include "src/gpu/ganesh/GrGpu.h" 16 #include "src/gpu/ganesh/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 } 37 #ifndef SK_ENABLE_OPTIMIZE_SIZE smallPathAtlasManager()38 skgpu::v1::SmallPathAtlasMgr* smallPathAtlasManager() const override { return nullptr; } 39 #endif resetAllocator()40 void resetAllocator() { fAllocator.reset(); } allocator()41 SkArenaAlloc* allocator() override { return &fAllocator; } putBackVertices(int vertices,size_t vertexStride)42 void putBackVertices(int vertices, size_t vertexStride) override { /* no-op */ } detachAppliedClip()43 GrAppliedClip detachAppliedClip() override { return GrAppliedClip::Disabled(); } dstProxyView()44 const GrDstProxyView& dstProxyView() const override { return fDstProxyView; } renderPassBarriers()45 GrXferBarrierFlags renderPassBarriers() const override { return GrXferBarrierFlags::kNone; } colorLoadOp()46 GrLoadOp colorLoadOp() const override { return GrLoadOp::kLoad; } 47 makeVertexSpace(size_t vertexSize,int vertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex)48 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>* buffer, 49 int* startVertex) override { 50 if (vertexSize * vertexCount > sizeof(fStaticVertexData)) { 51 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n", 52 vertexSize * vertexCount, sizeof(fStaticVertexData)); 53 } 54 *buffer = fStaticVertexBuffer; 55 *startVertex = 0; 56 return fStaticVertexData; 57 } 58 makeVertexSpaceAtLeast(size_t vertexSize,int minVertexCount,int fallbackVertexCount,sk_sp<const GrBuffer> * buffer,int * startVertex,int * actualVertexCount)59 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount, 60 sk_sp<const GrBuffer>* buffer, int* startVertex, 61 int* actualVertexCount) override { 62 if (vertexSize * minVertexCount > sizeof(fStaticVertexData)) { 63 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n", 64 vertexSize * minVertexCount, sizeof(fStaticVertexData)); 65 } 66 *buffer = fStaticVertexBuffer; 67 *startVertex = 0; 68 *actualVertexCount = sizeof(fStaticVertexData) / vertexSize; 69 return fStaticVertexData; 70 } 71 makeDrawIndirectSpace(int drawCount,sk_sp<const GrBuffer> * buffer,size_t * offsetInBytes)72 GrDrawIndirectWriter makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer, 73 size_t* offsetInBytes) override { 74 if (sizeof(GrDrawIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) { 75 SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n", 76 sizeof(GrDrawIndirectCommand) * drawCount, sizeof(fStaticIndirectData)); 77 } 78 *buffer = fStaticIndirectBuffer; 79 *offsetInBytes = 0; 80 return fStaticIndirectData; 81 } 82 putBackIndirectDraws(int count)83 void putBackIndirectDraws(int count) override { /* no-op */ } 84 makeDrawIndexedIndirectSpace(int drawCount,sk_sp<const GrBuffer> * buffer,size_t * offsetInBytes)85 GrDrawIndexedIndirectWriter makeDrawIndexedIndirectSpace(int drawCount, 86 sk_sp<const GrBuffer>* buffer, 87 size_t* offsetInBytes) override { 88 if (sizeof(GrDrawIndexedIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) { 89 SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n", 90 sizeof(GrDrawIndexedIndirectCommand) * drawCount, sizeof(fStaticIndirectData)); 91 } 92 *buffer = fStaticIndirectBuffer; 93 *offsetInBytes = 0; 94 return fStaticIndirectData; 95 } 96 putBackIndexedIndirectDraws(int count)97 void putBackIndexedIndirectDraws(int count) override { /* no-op */ } 98 99 // Call these methods to see what got written after the previous call to make*Space. peekStaticVertexData()100 const void* peekStaticVertexData() const { return fStaticVertexData; } peekStaticIndirectData()101 const void* peekStaticIndirectData() const { return fStaticIndirectData; } 102 103 #define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); } 104 UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int, 105 const GrSurfaceProxy* const[], GrPrimitiveType)) 106 UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*)) 107 UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*)) 108 UNIMPL(void putBackIndices(int)) 109 UNIMPL(GrRenderTargetProxy* rtProxy() const) 110 UNIMPL(const GrSurfaceProxyView& writeView() const) 111 UNIMPL(const GrAppliedClip* appliedClip() const) 112 UNIMPL(bool usesMSAASurface() const) 113 UNIMPL(sktext::gpu::StrikeCache* strikeCache() const) 114 UNIMPL(GrAtlasManager* atlasManager() const) 115 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray()) 116 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget()) 117 #undef UNIMPL 118 119 private: 120 sk_sp<GrDirectContext> fMockContext; 121 char fStaticVertexData[6 * 1024 * 1024]; 122 sk_sp<GrGpuBuffer> fStaticVertexBuffer; 123 char fStaticIndirectData[sizeof(GrDrawIndexedIndirectCommand) * 32]; 124 sk_sp<GrGpuBuffer> fStaticIndirectBuffer; 125 SkSTArenaAllocWithReset<1024 * 1024> fAllocator; 126 GrDstProxyView fDstProxyView; 127 }; 128 129 #endif 130