blob: bca9494eb196d802a24e84028d18cc8f869ff816 [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 Dalton641ff3b2020-11-24 11:04:23 -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 Dalton641ff3b2020-11-24 11:04:23 -070043 *startVertex = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070044 return fStaticVertexData;
45 }
46
47 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
Chris Dalton641ff3b2020-11-24 11:04:23 -070048 sk_sp<const GrBuffer>*, int* startVertex,
Chris Dalton90ad0fe2020-11-09 14:13:39 -070049 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 }
Chris Dalton641ff3b2020-11-24 11:04:23 -070054 *startVertex = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070055 *actualVertexCount = sizeof(fStaticVertexData) / vertexSize;
56 return fStaticVertexData;
57 }
58
59 GrDrawIndirectCommand* makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
Chris Dalton641ff3b2020-11-24 11:04:23 -070060 size_t* offsetInBytes) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070061 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 }
Chris Dalton641ff3b2020-11-24 11:04:23 -070066 *offsetInBytes = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070067 return fStaticDrawIndirectData;
68 }
69
Chris Dalton75125072020-11-24 09:30:51 -070070 void putBackIndirectDraws(int count) override { /* no-op */ }
71
Chris Dalton90ad0fe2020-11-09 14:13:39 -070072 GrDrawIndexedIndirectCommand* makeDrawIndexedIndirectSpace(
Chris Dalton641ff3b2020-11-24 11:04:23 -070073 int drawCount, sk_sp<const GrBuffer>* buffer, size_t* offsetInBytes) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070074 int staticBufferCount = (int)SK_ARRAY_COUNT(fStaticDrawIndexedIndirectData);
75 if (drawCount > staticBufferCount) {
76 SK_ABORT("FATAL: wanted %i static drawIndexedIndirect elements; only have %i.\n",
77 drawCount, staticBufferCount);
78 }
Chris Dalton641ff3b2020-11-24 11:04:23 -070079 *offsetInBytes = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070080 return fStaticDrawIndexedIndirectData;
81 }
82
Chris Dalton75125072020-11-24 09:30:51 -070083 void putBackIndexedIndirectDraws(int count) override { /* no-op */ }
84
Chris Dalton90ad0fe2020-11-09 14:13:39 -070085#define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); }
86 UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int,
87 const GrSurfaceProxy* const[], GrPrimitiveType))
88 UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*))
89 UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*))
90 UNIMPL(void putBackIndices(int))
Robert Phillips5c809642020-11-20 12:28:45 -050091 UNIMPL(GrRenderTargetProxy* rtProxy() const)
Adlai Hollere2296f72020-11-19 13:41:26 -050092 UNIMPL(const GrSurfaceProxyView& writeView() const)
Chris Dalton90ad0fe2020-11-09 14:13:39 -070093 UNIMPL(const GrAppliedClip* appliedClip() const)
94 UNIMPL(GrStrikeCache* strikeCache() const)
95 UNIMPL(GrAtlasManager* atlasManager() const)
96 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray())
97 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
98#undef UNIMPL
99
100private:
101 sk_sp<GrDirectContext> fMockContext;
102 char fStaticVertexData[4 * 1024 * 1024];
103 GrDrawIndirectCommand fStaticDrawIndirectData[32];
104 GrDrawIndexedIndirectCommand fStaticDrawIndexedIndirectData[32];
105 SkSTArenaAllocWithReset<1024 * 1024> fAllocator;
106 GrXferProcessor::DstProxyView fDstProxyView;
107};
108
109#endif