Debian Codegraph @

src/gpu/ganesh/mtl/GrMtlCommandBuffer.h

raw
1 /*
2 * Copyright 2019 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 GrMtlCommandBuffer_DEFINED
9 #define GrMtlCommandBuffer_DEFINED
10
11 #import <Metal/Metal.h>
12
13 #include "include/core/SkRefCnt.h"
14 #include "include/gpu/ganesh/GrTypes.h"
15 #include "src/gpu/GpuRefCnt.h"
16 #include "src/gpu/ganesh/GrBuffer.h"
17 #include "src/gpu/ganesh/GrManagedResource.h"
18 #include "src/gpu/ganesh/GrSurface.h"
19 #include "src/gpu/ganesh/mtl/GrMtlRenderCommandEncoder.h"
20 #include "src/gpu/ganesh/mtl/GrMtlUtil.h"
21
22 class GrMtlEvent;
23 class GrMtlGpu;
24 class GrMtlPipelineState;
25 class GrMtlOpsRenderPass;
26 class GrMtlRenderCommandEncoder;
27
28 GR_NORETAIN_BEGIN
29
30 class GrMtlCommandBuffer : public SkRefCnt {
31 public:
32 static sk_sp<GrMtlCommandBuffer> Make(id<MTLCommandQueue> queue);
33 ~GrMtlCommandBuffer() override;
34
35 void releaseResources();
36
37 bool commit(bool waitUntilCompleted);
38 bool hasWork() { return fHasWork; }
39
40 void addFinishedCallback(sk_sp<skgpu::RefCntedCallback> callback) {
41 fFinishedCallbacks.push_back(std::move(callback));
42 }
43
44 id<MTLBlitCommandEncoder> getBlitCommandEncoder();
45 // Tries to reuse current renderCommandEncoder if possible
46 GrMtlRenderCommandEncoder* getRenderCommandEncoder(MTLRenderPassDescriptor*,
47 const GrMtlPipelineState*,
48 GrMtlOpsRenderPass* opsRenderPass);
49 // Replaces current renderCommandEncoder with new one
50 GrMtlRenderCommandEncoder* getRenderCommandEncoder(MTLRenderPassDescriptor*,
51 GrMtlOpsRenderPass*);
52
53 void addCompletedHandler(MTLCommandBufferHandler block) {
54 [fCmdBuffer addCompletedHandler:block];
55 }
56
57 void addResource(const sk_sp<const GrManagedResource>& resource) {
58 // Disable generic resource tracking for now
59 // SkASSERT(resource);
60 // fTrackedResources.push_back(std::move(resource));
61 }
62
63 void addGrBuffer(sk_sp<const GrBuffer> buffer) {
64 fTrackedGrBuffers.push_back(std::move(buffer));
65 }
66
67 void addGrSurface(sk_sp<const GrSurface> surface) {
68 fTrackedGrSurfaces.push_back(std::move(surface));
69 }
70
71 void encodeSignalEvent(sk_sp<GrMtlEvent>, uint64_t value);
72 void encodeWaitForEvent(sk_sp<GrMtlEvent>, uint64_t value);
73
74 void waitUntilCompleted() {
75 [fCmdBuffer waitUntilCompleted];
76 }
77 bool isCompleted() {
78 return fCmdBuffer.status == MTLCommandBufferStatusCompleted ||
79 fCmdBuffer.status == MTLCommandBufferStatusError;
80 }
81 void callFinishedCallbacks() { fFinishedCallbacks.clear(); }
82
83 void pushDebugGroup(NSString* string) {
84 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
85 [fCmdBuffer pushDebugGroup:string];
86 }
87 }
88
89 void popDebugGroup() {
90 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
91 [fCmdBuffer popDebugGroup];
92 }
93 }
94
95 private:
96 GrMtlCommandBuffer(id<MTLCommandBuffer> cmdBuffer)
97 : fCmdBuffer(cmdBuffer)
98 , fActiveBlitCommandEncoder(nil)
99 , fActiveRenderCommandEncoder(nil)
100 , fPreviousRenderPassDescriptor(nil)
101 , fHasWork(false) {}
102
103 void endAllEncoding();
104
105 static const int kInitialTrackedResourcesCount = 32;
106
107 skia_private::STArray<
108 kInitialTrackedResourcesCount, sk_sp<const GrManagedResource>> fTrackedResources;
109 skia_private::STArray<kInitialTrackedResourcesCount, sk_sp<const GrBuffer>> fTrackedGrBuffers;
110 skia_private::STArray<16, gr_cb<const GrSurface>> fTrackedGrSurfaces;
111
112 id<MTLCommandBuffer> fCmdBuffer;
113 id<MTLBlitCommandEncoder> fActiveBlitCommandEncoder;
114 std::unique_ptr<GrMtlRenderCommandEncoder> fActiveRenderCommandEncoder;
115 MTLRenderPassDescriptor* fPreviousRenderPassDescriptor;
116 bool fHasWork;
117
118 skia_private::TArray<sk_sp<skgpu::RefCntedCallback>> fFinishedCallbacks;
119
120 };
121
122 GR_NORETAIN_END
123
124 #endif