blob: b5a18e3e922109ecb634a2cf023714ed449d1f75 [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"
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060013#include "src/gpu/GrGpu.h"
Chris Dalton90ad0fe2020-11-09 14:13:39 -070014#include "src/gpu/ops/GrMeshDrawOp.h"
15
16// This is a mock GrMeshDrawOp::Target implementation that just gives back pointers into
17// pre-allocated CPU buffers, rather than allocating and mapping GPU buffers.
18class GrMockOpTarget : public GrMeshDrawOp::Target {
19public:
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060020 GrMockOpTarget(sk_sp<GrDirectContext> mockContext) : fMockContext(std::move(mockContext)) {
21 fStaticVertexBuffer = fMockContext->priv().getGpu()->createBuffer(
22 sizeof(fStaticVertexData), GrGpuBufferType::kVertex, kDynamic_GrAccessPattern);
23 fStaticIndirectBuffer = fMockContext->priv().getGpu()->createBuffer(
24 sizeof(fStaticIndirectData), GrGpuBufferType::kDrawIndirect,
25 kDynamic_GrAccessPattern);
26 }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070027 const GrDirectContext* mockContext() const { return fMockContext.get(); }
28 const GrCaps& caps() const override { return *fMockContext->priv().caps(); }
29 GrThreadSafeCache* threadSafeCache() const override {
30 return fMockContext->priv().threadSafeCache();
31 }
32 GrResourceProvider* resourceProvider() const override {
33 return fMockContext->priv().resourceProvider();
34 }
35 GrSmallPathAtlasMgr* smallPathAtlasManager() const override { return nullptr; }
36 void resetAllocator() { fAllocator.reset(); }
37 SkArenaAlloc* allocator() override { return &fAllocator; }
38 void putBackVertices(int vertices, size_t vertexStride) override { /* no-op */ }
39 GrAppliedClip detachAppliedClip() override { return GrAppliedClip::Disabled(); }
John Stiles52cb1d02021-06-02 11:58:05 -040040 const GrDstProxyView& dstProxyView() const override { return fDstProxyView; }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070041 GrXferBarrierFlags renderPassBarriers() const override { return GrXferBarrierFlags::kNone; }
Greg Daniel42dbca52020-11-20 10:22:43 -050042 GrLoadOp colorLoadOp() const override { return GrLoadOp::kLoad; }
Chris Dalton90ad0fe2020-11-09 14:13:39 -070043
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060044 void* makeVertexSpace(size_t vertexSize, int vertexCount, sk_sp<const GrBuffer>* buffer,
Chris Dalton641ff3b2020-11-24 11:04:23 -070045 int* startVertex) override {
Chris Dalton90ad0fe2020-11-09 14:13:39 -070046 if (vertexSize * vertexCount > sizeof(fStaticVertexData)) {
47 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
48 vertexSize * vertexCount, sizeof(fStaticVertexData));
49 }
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060050 *buffer = fStaticVertexBuffer;
Chris Dalton641ff3b2020-11-24 11:04:23 -070051 *startVertex = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070052 return fStaticVertexData;
53 }
54
55 void* makeVertexSpaceAtLeast(size_t vertexSize, int minVertexCount, int fallbackVertexCount,
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060056 sk_sp<const GrBuffer>* buffer, int* startVertex,
Chris Dalton90ad0fe2020-11-09 14:13:39 -070057 int* actualVertexCount) override {
58 if (vertexSize * minVertexCount > sizeof(fStaticVertexData)) {
59 SK_ABORT("FATAL: wanted %zu bytes of static vertex data; only have %zu.\n",
60 vertexSize * minVertexCount, sizeof(fStaticVertexData));
61 }
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060062 *buffer = fStaticVertexBuffer;
Chris Dalton641ff3b2020-11-24 11:04:23 -070063 *startVertex = 0;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070064 *actualVertexCount = sizeof(fStaticVertexData) / vertexSize;
65 return fStaticVertexData;
66 }
67
Chris Daltona6a3d052021-02-07 20:56:36 -070068 GrDrawIndirectWriter makeDrawIndirectSpace(int drawCount, sk_sp<const GrBuffer>* buffer,
69 size_t* offsetInBytes) override {
70 if (sizeof(GrDrawIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) {
71 SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n",
72 sizeof(GrDrawIndirectCommand) * drawCount, sizeof(fStaticIndirectData));
Chris Dalton90ad0fe2020-11-09 14:13:39 -070073 }
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060074 *buffer = fStaticIndirectBuffer;
Chris Dalton641ff3b2020-11-24 11:04:23 -070075 *offsetInBytes = 0;
Chris Daltona6a3d052021-02-07 20:56:36 -070076 return fStaticIndirectData;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070077 }
78
Chris Dalton75125072020-11-24 09:30:51 -070079 void putBackIndirectDraws(int count) override { /* no-op */ }
80
Chris Daltona6a3d052021-02-07 20:56:36 -070081 GrDrawIndexedIndirectWriter makeDrawIndexedIndirectSpace(int drawCount,
82 sk_sp<const GrBuffer>* buffer,
83 size_t* offsetInBytes) override {
84 if (sizeof(GrDrawIndexedIndirectCommand) * drawCount > sizeof(fStaticIndirectData)) {
85 SK_ABORT("FATAL: wanted %zu bytes of static indirect data; only have %zu.\n",
86 sizeof(GrDrawIndexedIndirectCommand) * drawCount, sizeof(fStaticIndirectData));
Chris Dalton90ad0fe2020-11-09 14:13:39 -070087 }
Chris Dalton8ed7a8d2021-03-31 10:40:29 -060088 *buffer = fStaticIndirectBuffer;
Chris Dalton641ff3b2020-11-24 11:04:23 -070089 *offsetInBytes = 0;
Chris Daltona6a3d052021-02-07 20:56:36 -070090 return fStaticIndirectData;
Chris Dalton90ad0fe2020-11-09 14:13:39 -070091 }
92
Chris Dalton75125072020-11-24 09:30:51 -070093 void putBackIndexedIndirectDraws(int count) override { /* no-op */ }
94
Chris Daltona6a3d052021-02-07 20:56:36 -070095 // Call these methods to see what got written after the previous call to make*Space.
96 const void* peekStaticVertexData() const { return fStaticVertexData; }
97 const void* peekStaticIndirectData() const { return fStaticIndirectData; }
98
Chris Dalton90ad0fe2020-11-09 14:13:39 -070099#define UNIMPL(...) __VA_ARGS__ override { SK_ABORT("unimplemented."); }
100 UNIMPL(void recordDraw(const GrGeometryProcessor*, const GrSimpleMesh[], int,
101 const GrSurfaceProxy* const[], GrPrimitiveType))
102 UNIMPL(uint16_t* makeIndexSpace(int, sk_sp<const GrBuffer>*, int*))
103 UNIMPL(uint16_t* makeIndexSpaceAtLeast(int, int, sk_sp<const GrBuffer>*, int*, int*))
104 UNIMPL(void putBackIndices(int))
Robert Phillips5c809642020-11-20 12:28:45 -0500105 UNIMPL(GrRenderTargetProxy* rtProxy() const)
Adlai Hollere2296f72020-11-19 13:41:26 -0500106 UNIMPL(const GrSurfaceProxyView& writeView() const)
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700107 UNIMPL(const GrAppliedClip* appliedClip() const)
Chris Dalton2517ce32021-04-13 00:21:15 -0600108 UNIMPL(bool usesMSAASurface() const)
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700109 UNIMPL(GrStrikeCache* strikeCache() const)
110 UNIMPL(GrAtlasManager* atlasManager() const)
111 UNIMPL(SkTArray<GrSurfaceProxy*, true>* sampledProxyArray())
112 UNIMPL(GrDeferredUploadTarget* deferredUploadTarget())
113#undef UNIMPL
114
115private:
116 sk_sp<GrDirectContext> fMockContext;
Chris Daltonc2a17462020-12-09 16:46:22 -0700117 char fStaticVertexData[6 * 1024 * 1024];
Chris Dalton8ed7a8d2021-03-31 10:40:29 -0600118 sk_sp<GrGpuBuffer> fStaticVertexBuffer;
Chris Daltona6a3d052021-02-07 20:56:36 -0700119 char fStaticIndirectData[sizeof(GrDrawIndexedIndirectCommand) * 32];
Chris Dalton8ed7a8d2021-03-31 10:40:29 -0600120 sk_sp<GrGpuBuffer> fStaticIndirectBuffer;
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700121 SkSTArenaAllocWithReset<1024 * 1024> fAllocator;
John Stiles52cb1d02021-06-02 11:58:05 -0400122 GrDstProxyView fDstProxyView;
Chris Dalton90ad0fe2020-11-09 14:13:39 -0700123};
124
125#endif