/* * Copyright 2021 Google LLC * * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ #include "experimental/graphite/src/mtl/MtlGpu.h" #include "experimental/graphite/src/Caps.h" #include "experimental/graphite/src/mtl/MtlCommandBuffer.h" #include "experimental/graphite/src/mtl/MtlResourceProvider.h" namespace skgpu::mtl { sk_sp Gpu::Make(const BackendContext& context) { sk_cfp> device = sk_ret_cfp((id)(context.fDevice.get())); sk_cfp> queue = sk_ret_cfp((id)(context.fQueue.get())); sk_sp caps(new Caps(device.get())); return sk_sp(new Gpu(std::move(device), std::move(queue), std::move(caps))); } Gpu::Gpu(sk_cfp> device, sk_cfp> queue, sk_sp caps) : skgpu::Gpu(std::move(caps)) , fDevice(std::move(device)) , fQueue(std::move(queue)) { fResourceProvider.reset(new ResourceProvider(this)); } Gpu::~Gpu() { } class WorkSubmission final : public skgpu::GpuWorkSubmission { public: WorkSubmission(sk_sp cmdBuffer) : fCommandBuffer(std::move(cmdBuffer)) {} ~WorkSubmission() override {} bool isFinished() override { return fCommandBuffer->isFinished(); } void waitUntilFinished(const skgpu::Gpu*) override { return fCommandBuffer->waitUntilFinished(); } private: sk_sp fCommandBuffer; }; bool Gpu::onSubmit(sk_sp commandBuffer) { SkASSERT(commandBuffer); sk_sp& mtlCmdBuffer = (sk_sp&)(commandBuffer); if (!mtlCmdBuffer->commit()) { return false; } std::unique_ptr submission(new WorkSubmission(mtlCmdBuffer)); new (fOutstandingSubmissions.push_back()) OutstandingSubmission(std::move(submission)); return true; } #if GRAPHITE_TEST_UTILS void Gpu::testingOnly_startCapture() { if (@available(macOS 10.13, iOS 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, *)) { 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 Gpu::testingOnly_endCapture() { if (@available(macOS 10.13, iOS 11.0, *)) { MTLCaptureManager* captureManager = [MTLCaptureManager sharedCaptureManager]; if (captureManager.isCapturing) { [captureManager stopCapture]; } } } #endif } // namespace skgpu::mtl