blob: 7a8be0195d4080b594bd8994d5ab878830879d3e [file] [log] [blame]
Chris Dalton90ad0fe2020-11-09 14:13:39 -07001/*
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.
17class GrMockOpTarget : public GrMeshDrawOp::Target {
18public:
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; }
35
36 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*,
37 int* startVertex) override {
38 if (vertexSize * vertexCount > sizeof(fStaticVertexData)) {
39 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
40 vertexSize * vertexCount, sizeof(fStaticVertexData));
41 }
42 *startVertex = 0;
43 return fStaticVertexData;
44 }
45
46 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
47 sk_sp<const GrBuffer>*, int* startVertex,
48 int* actualVertexCount) override {
49 if (vertexSize * minVertexCount > sizeof(fStaticVertexData)) {
50 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
51 vertexSize * minVertexCount, sizeof(fStaticVertexData));
52 }
53 *startVertex = 0;
54 *actualVertexCount = sizeof(fStaticVertexData) / vertexSize;
55 return fStaticVertexData;
56 }
57
58 GrDrawIndirectCommand* makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
59 size_t* offsetInBytes) override {
60 int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndirectData);
61 if (drawCount > staticBufferCount) {
62 SK_ABORT("FATAL: wanted %i static drawIndirect elements; only have %i.\n",
63 drawCount, staticBufferCount);
64 }
65 return fStaticDrawIndirectData;
66 }
67
68 GrDrawIndexedIndirectCommand* makeDrawIndexedIndirectSpace(
69 int drawCount, sk_sp<const GrBuffer>* buffer, size_t* offsetInBytes) override {
70 int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndexedIndirectData);
71 if (drawCount > staticBufferCount) {
72 SK_ABORT("FATAL: wanted %i static drawIndexedIndirect elements; only have %i.\n",
73 drawCount, staticBufferCount);
74 }
75 return fStaticDrawIndexedIndirectData;
76 }
77
78#define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); }
79 UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int,
80 const GrSurfaceProxy* const[], GrPrimitiveType))
81 UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*))
82 UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*))
83 UNIMPL(void putBackIndices(int))
84 UNIMPL(GrRenderTargetProxy* proxy() const)
Adlai Hollere2296f72020-11-19 13:41:26 -050085 UNIMPL(const GrSurfaceProxyView& writeView() const)
Chris Dalton90ad0fe2020-11-09 14:13:39 -070086 UNIMPL(const GrAppliedClip* appliedClip() const)
87 UNIMPL(GrStrikeCache* strikeCache() const)
88 UNIMPL(GrAtlasManager* atlasManager() const)
89 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray())
90 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
91#undef UNIMPL
92
93private:
94 sk_sp<GrDirectContext> fMockContext;
95 char fStaticVertexData[4 * 1024 * 1024];
96 GrDrawIndirectCommand fStaticDrawIndirectData[32];
97 GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32];
98 SkSTArenaAllocWithReset<1024 * 1024> fAllocator;
99 GrXferProcessor::DstProxyView fDstProxyView;
100};
101
102#endif