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/GrTypes.h" 15 #include "src/gpu/GrBuffer.h" 16 #include "src/gpu/GrManagedResource.h" 17 #include "src/gpu/GrRefCnt.h" 18 #include "src/gpu/GrSurface.h" 19 #include "src/gpu/mtl/GrMtlRenderCommandEncoder.h" 20 #include "src/gpu/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); hasWork()38 bool hasWork() { return fHasWork; } 39 addFinishedCallback(sk_sp<GrRefCntedCallback> callback)40 void addFinishedCallback(sk_sp<GrRefCntedCallback> 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 addCompletedHandler(MTLCommandBufferHandler block)53 void addCompletedHandler(MTLCommandBufferHandler block) { 54 [fCmdBuffer addCompletedHandler:block]; 55 } 56 addResource(sk_sp<const GrManagedResource> resource)57 void addResource(sk_sp<const GrManagedResource> resource) { 58 // Disable generic resource tracking for now 59 // SkASSERT(resource); 60 // fTrackedResources.push_back(std::move(resource)); 61 } 62 addGrBuffer(sk_sp<const GrBuffer> buffer)63 void addGrBuffer(sk_sp<const GrBuffer> buffer) { 64 fTrackedGrBuffers.push_back(std::move(buffer)); 65 } 66 addGrSurface(sk_sp<const GrSurface> surface)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 waitUntilCompleted()74 void waitUntilCompleted() { 75 [fCmdBuffer waitUntilCompleted]; 76 } isCompleted()77 bool isCompleted() { 78 return fCmdBuffer.status == MTLCommandBufferStatusCompleted || 79 fCmdBuffer.status == MTLCommandBufferStatusError; 80 } callFinishedCallbacks()81 void callFinishedCallbacks() { fFinishedCallbacks.reset(); } 82 pushDebugGroup(NSString * string)83 void pushDebugGroup(NSString* string) { 84 if (@available(macOS 10.13, iOS 11.0, *)) { 85 [fCmdBuffer pushDebugGroup:string]; 86 } 87 } 88 popDebugGroup()89 void popDebugGroup() { 90 if (@available(macOS 10.13, iOS 11.0, *)) { 91 [fCmdBuffer popDebugGroup]; 92 } 93 } 94 95 private: GrMtlCommandBuffer(id<MTLCommandBuffer> cmdBuffer)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 SkSTArray<kInitialTrackedResourcesCount, sk_sp<const GrManagedResource>> fTrackedResources; 108 SkSTArray<kInitialTrackedResourcesCount, sk_sp<const GrBuffer>> fTrackedGrBuffers; 109 SkSTArray<16, gr_cb<const GrSurface>> fTrackedGrSurfaces; 110 111 id<MTLCommandBuffer> fCmdBuffer; 112 id<MTLBlitCommandEncoder> fActiveBlitCommandEncoder; 113 std::unique_ptr<GrMtlRenderCommandEncoder> fActiveRenderCommandEncoder; 114 MTLRenderPassDescriptor* fPreviousRenderPassDescriptor; 115 bool fHasWork; 116 117 SkTArray<sk_sp<GrRefCntedCallback>> fFinishedCallbacks; 118 119 }; 120 121 GR_NORETAIN_END 122 123 #endif 124