/* * Copyright 2022 Google LLC * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "src/gpu/graphite/mtl/MtlQueueManager.h" #include "src/gpu/graphite/GpuWorkSubmission.h" #include "src/gpu/graphite/mtl/MtlCommandBuffer.h" #include "src/gpu/graphite/mtl/MtlResourceProvider.h" #include "src/gpu/graphite/mtl/MtlSharedContext.h" namespace skgpu::graphite { MtlQueueManager::MtlQueueManager(sk_cfp> queue, const SharedContext* sharedContext) : QueueManager(sharedContext) , fQueue(std::move(queue)) { } const MtlSharedContext* MtlQueueManager::mtlSharedContext() const { return static_cast(fSharedContext); } std::unique_ptr MtlQueueManager::getNewCommandBuffer( ResourceProvider* resourceProvider, Protected) { MtlResourceProvider* mtlResourceProvider = static_cast(resourceProvider); auto cmdBuffer = MtlCommandBuffer::Make(fQueue.get(), this->mtlSharedContext(), mtlResourceProvider); return cmdBuffer; } class MtlWorkSubmission final : public GpuWorkSubmission { public: MtlWorkSubmission(std::unique_ptr cmdBuffer, QueueManager* queueManager) : GpuWorkSubmission(std::move(cmdBuffer), queueManager) {} ~MtlWorkSubmission() override {} private: bool onIsFinished(const SharedContext*) override { return static_cast(this->commandBuffer())->isFinished(); } void onWaitUntilFinished(const SharedContext*) override { return static_cast(this->commandBuffer())->waitUntilFinished(); } }; QueueManager::OutstandingSubmission MtlQueueManager::onSubmitToGpu() { SkASSERT(fCurrentCommandBuffer); MtlCommandBuffer* mtlCmdBuffer = static_cast(fCurrentCommandBuffer.get()); if (!mtlCmdBuffer->commit()) { fCurrentCommandBuffer->callFinishedProcs(/*success=*/false); return nullptr; } std::unique_ptr submission( new MtlWorkSubmission(std::move(fCurrentCommandBuffer), this)); return submission; } #if defined(GPU_TEST_UTILS) void MtlQueueManager::startCapture() { if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { // TODO: add newer Metal interface as well MTLCaptureManager* captureManager = [MTLCaptureManager sharedCaptureManager]; if (captureManager.isCapturing) { return; } if (@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)) { MTLCaptureDescriptor* captureDescriptor = [[MTLCaptureDescriptor alloc] init]; captureDescriptor.captureObject = fQueue.get(); NSError *error; if (![captureManager startCaptureWithDescriptor: captureDescriptor error:&error]) { NSLog(@"Failed to start capture, error %@", error); } } else { [captureManager startCaptureWithCommandQueue: fQueue.get()]; } } } void MtlQueueManager::stopCapture() { if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { MTLCaptureManager* captureManager = [MTLCaptureManager sharedCaptureManager]; if (captureManager.isCapturing) { [captureManager stopCapture]; } } } #endif } // namespace skgpu::graphite