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/mtl/GrMtlUtil.h" 17 18 class GrMtlGpu; 19 class GrMtlPipelineState; 20 class GrMtlOpsRenderPass; 21 22 GR_NORETAIN_BEGIN 23 24 class GrMtlCommandBuffer : public SkRefCnt { 25 public: 26 static sk_sp<GrMtlCommandBuffer> Make(id<MTLCommandQueue> queue); 27 ~GrMtlCommandBuffer() override; 28 29 bool commit(bool waitUntilCompleted); hasWork()30 bool hasWork() { return fHasWork; } 31 addFinishedCallback(sk_sp<GrRefCntedCallback> callback)32 void addFinishedCallback(sk_sp<GrRefCntedCallback> callback) { 33 fFinishedCallbacks.push_back(std::move(callback)); 34 } 35 36 id<MTLBlitCommandEncoder> getBlitCommandEncoder(); 37 id<MTLRenderCommandEncoder> getRenderCommandEncoder(MTLRenderPassDescriptor*, 38 const GrMtlPipelineState*, 39 GrMtlOpsRenderPass* opsRenderPass); 40 addCompletedHandler(MTLCommandBufferHandler block)41 void addCompletedHandler(MTLCommandBufferHandler block) { 42 [fCmdBuffer addCompletedHandler:block]; 43 } 44 addGrBuffer(sk_sp<const GrBuffer> buffer)45 void addGrBuffer(sk_sp<const GrBuffer> buffer) { 46 fTrackedGrBuffers.push_back(std::move(buffer)); 47 } 48 49 void encodeSignalEvent(id<MTLEvent>, uint64_t value) SK_API_AVAILABLE(macos(10.14), ios(12.0)); 50 void encodeWaitForEvent(id<MTLEvent>, uint64_t value) SK_API_AVAILABLE(macos(10.14), ios(12.0)); 51 waitUntilCompleted()52 void waitUntilCompleted() { 53 [fCmdBuffer waitUntilCompleted]; 54 } isCompleted()55 bool isCompleted() { 56 return fCmdBuffer.status == MTLCommandBufferStatusCompleted || 57 fCmdBuffer.status == MTLCommandBufferStatusError; 58 } callFinishedCallbacks()59 void callFinishedCallbacks() { fFinishedCallbacks.reset(); } 60 61 private: 62 static const int kInitialTrackedResourcesCount = 32; 63 GrMtlCommandBuffer(id<MTLCommandBuffer> cmdBuffer)64 GrMtlCommandBuffer(id<MTLCommandBuffer> cmdBuffer) 65 : fCmdBuffer(cmdBuffer) 66 , fActiveBlitCommandEncoder(nil) 67 , fActiveRenderCommandEncoder(nil) 68 , fPreviousRenderPassDescriptor(nil) 69 , fHasWork(false) {} 70 71 void endAllEncoding(); 72 73 id<MTLCommandBuffer> fCmdBuffer; 74 id<MTLBlitCommandEncoder> fActiveBlitCommandEncoder; 75 id<MTLRenderCommandEncoder> fActiveRenderCommandEncoder; 76 MTLRenderPassDescriptor* fPreviousRenderPassDescriptor; 77 bool fHasWork; 78 79 SkTArray<sk_sp<GrRefCntedCallback>> fFinishedCallbacks; 80 81 SkSTArray<kInitialTrackedResourcesCount, sk_sp<const GrBuffer>> fTrackedGrBuffers; 82 }; 83 84 GR_NORETAIN_END 85 86 #endif 87