blob: 4200e3594881f26cb6df1f0ef60ea736d5d6eb7a [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; }
Greg Daniel42dbca52020-11-20 10:22:43 -050035 GrLoadOp colorLoadOp() const override { return GrLoadOp::kLoad; }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070036
37 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>*,
Chris Dalton75125072020-11-24 09:30:51 -070038 int* /*startVertex*/) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070039 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 }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070043 return fStaticVertexData;
44 }
45
46 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
Chris Dalton75125072020-11-24 09:30:51 -070047 sk_sp<const GrBuffer>*, int* /*startVertex*/,
Chris Dalton90ad0fe2020-11-09 14:13:39 -070048 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 }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070053 *actualVertexCount = sizeof(fStaticVertexData) / vertexSize;
54 return fStaticVertexData;
55 }
56
57 GrDrawIndirectCommand* makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
Chris Dalton75125072020-11-24 09:30:51 -070058 size_t* /*offsetInBytes*/) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070059 int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndirectData);
60 if (drawCount > staticBufferCount) {
61 SK_ABORT("FATAL: wanted %i static drawIndirect elements; only have %i.\n",
62 drawCount, staticBufferCount);
63 }
64 return fStaticDrawIndirectData;
65 }
66
Chris Dalton75125072020-11-24 09:30:51 -070067 void putBackIndirectDraws(int count) override { /* no-op */ }
68
Chris Dalton90ad0fe2020-11-09 14:13:39 -070069 GrDrawIndexedIndirectCommand* makeDrawIndexedIndirectSpace(
Chris Dalton75125072020-11-24 09:30:51 -070070 int drawCount, sk_sp<const GrBuffer>* buffer, size_t* /*offsetInBytes*/) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070071 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
Chris Dalton75125072020-11-24 09:30:51 -070079 void putBackIndexedIndirectDraws(int count) override { /* no-op */ }
80
Chris Dalton90ad0fe2020-11-09 14:13:39 -070081#define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); }
82 UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int,
83 const GrSurfaceProxy* const[], GrPrimitiveType))
84 UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*))
85 UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*))
86 UNIMPL(void putBackIndices(int))
Robert Phillips5c809642020-11-20 12:28:45 -050087 UNIMPL(GrRenderTargetProxy* rtProxy() const)
Adlai Hollere2296f72020-11-19 13:41:26 -050088 UNIMPL(const GrSurfaceProxyView& writeView() const)
Chris Dalton90ad0fe2020-11-09 14:13:39 -070089 UNIMPL(const GrAppliedClip* appliedClip() const)
90 UNIMPL(GrStrikeCache* strikeCache() const)
91 UNIMPL(GrAtlasManager* atlasManager() const)
92 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray())
93 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
94#undef UNIMPL
95
96private:
97 sk_sp<GrDirectContext> fMockContext;
98 char fStaticVertexData[4 * 1024 * 1024];
99 GrDrawIndirectCommand fStaticDrawIndirectData[32];
100 GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32];
101 SkSTArenaAllocWithReset<1024 * 1024> fAllocator;
102 GrXferProcessor::DstProxyView fDstProxyView;
103};
104
105#endif