Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 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/ops/GrMeshDrawOp.h" |
| 14 | |
| 15 | // This is a mock GrMeshDrawOp::Target implementation that just gives back pointers into |
| 16 | // pre-allocated CPU buffers, rather than allocating and mapping GPU buffers. |
| 17 | class GrMockOpTarget : public GrMeshDrawOp::Target { |
| 18 | public: |
| 19 | GrMockOpTarget(sk_sp<GrDirectContext> mockContext) : fMockContext(std::move(mockContext)) {} |
| 20 | const GrDirectContext* mockContext() const { return fMockContext.get(); } |
| 21 | const GrCaps& caps() const override { return *fMockContext->priv().caps(); } |
| 22 | GrThreadSafeCache* threadSafeCache() const override { |
| 23 | return fMockContext->priv().threadSafeCache(); |
| 24 | } |
| 25 | GrResourceProvider* resourceProvider() const override { |
| 26 | return fMockContext->priv().resourceProvider(); |
| 27 | } |
| 28 | GrSmallPathAtlasMgr* smallPathAtlasManager() const override { return nullptr; } |
| 29 | void resetAllocator() { fAllocator.reset(); } |
| 30 | SkArenaAlloc* allocator() override { return &fAllocator; } |
| 31 | void putBackVertices(int vertices, size_t vertexStride) override { /* no-op */ } |
| 32 | GrAppliedClip detachAppliedClip() override { return GrAppliedClip::Disabled(); } |
| 33 | const GrXferProcessor::DstProxyView& dstProxyView() const override { return fDstProxyView; } |
| 34 | GrXferBarrierFlags renderPassBarriers() const override { return GrXferBarrierFlags::kNone; } |
Greg Daniel | 42dbca5 | 2020-11-20 10:22:43 -0500 | [diff] [blame] | 35 | GrLoadOp colorLoadOp() const override { return GrLoadOp::kLoad; } |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 36 | |
| 37 | void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*, |
| 38 | int* startVertex) override { |
| 39 | if (vertexSize * vertexCount > sizeof(fStaticVertexData)) { |
| 40 | SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n", |
| 41 | vertexSize * vertexCount, sizeof(fStaticVertexData)); |
| 42 | } |
| 43 | *startVertex = 0; |
| 44 | return fStaticVertexData; |
| 45 | } |
| 46 | |
| 47 | void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount, |
| 48 | sk_sp<const GrBuffer>*, int* startVertex, |
| 49 | int* actualVertexCount) override { |
| 50 | if (vertexSize * minVertexCount > sizeof(fStaticVertexData)) { |
| 51 | SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n", |
| 52 | vertexSize * minVertexCount, sizeof(fStaticVertexData)); |
| 53 | } |
| 54 | *startVertex = 0; |
| 55 | *actualVertexCount = sizeof(fStaticVertexData) / vertexSize; |
| 56 | return fStaticVertexData; |
| 57 | } |
| 58 | |
| 59 | GrDrawIndirectCommand* makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer, |
| 60 | size_t* offsetInBytes) override { |
| 61 | int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndirectData); |
| 62 | if (drawCount > staticBufferCount) { |
| 63 | SK_ABORT("FATAL: wanted %i static drawIndirect elements; only have %i.\n", |
| 64 | drawCount, staticBufferCount); |
| 65 | } |
| 66 | return fStaticDrawIndirectData; |
| 67 | } |
| 68 | |
| 69 | GrDrawIndexedIndirectCommand* makeDrawIndexedIndirectSpace( |
| 70 | int drawCount, sk_sp<const GrBuffer>* buffer, size_t* offsetInBytes) override { |
| 71 | int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndexedIndirectData); |
| 72 | if (drawCount > staticBufferCount) { |
| 73 | SK_ABORT("FATAL: wanted %i static drawIndexedIndirect elements; only have %i.\n", |
| 74 | drawCount, staticBufferCount); |
| 75 | } |
| 76 | return fStaticDrawIndexedIndirectData; |
| 77 | } |
| 78 | |
| 79 | #define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); } |
| 80 | UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int, |
| 81 | const GrSurfaceProxy* const[], GrPrimitiveType)) |
| 82 | UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*)) |
| 83 | UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*)) |
| 84 | UNIMPL(void putBackIndices(int)) |
Robert Phillips | 5c80964 | 2020-11-20 12:28:45 -0500 | [diff] [blame^] | 85 | UNIMPL(GrRenderTargetProxy* rtProxy() const) |
Adlai Holler | e2296f7 | 2020-11-19 13:41:26 -0500 | [diff] [blame] | 86 | UNIMPL(const GrSurfaceProxyView& writeView() const) |
Chris Dalton | 90ad0fe | 2020-11-09 14:13:39 -0700 | [diff] [blame] | 87 | UNIMPL(const GrAppliedClip* appliedClip() const) |
| 88 | UNIMPL(GrStrikeCache* strikeCache() const) |
| 89 | UNIMPL(GrAtlasManager* atlasManager() const) |
| 90 | UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray()) |
| 91 | UNIMPL(GrDeferredUploadTarget* deferredUploadTarget()) |
| 92 | #undef UNIMPL |
| 93 | |
| 94 | private: |
| 95 | sk_sp<GrDirectContext> fMockContext; |
| 96 | char fStaticVertexData[4 * 1024 * 1024]; |
| 97 | GrDrawIndirectCommand fStaticDrawIndirectData[32]; |
| 98 | GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32]; |
| 99 | SkSTArenaAllocWithReset<1024 * 1024> fAllocator; |
| 100 | GrXferProcessor::DstProxyView fDstProxyView; |
| 101 | }; |
| 102 | |
| 103 | #endif |