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#include "src/gpu/mtl/GrMtlSemaphore.h" 9 10#include "src/gpu/mtl/GrMtlGpu.h" 11 12#if !__has_feature(objc_arc) 13#error This file must be compiled with Arc. Use -fobjc-arc flag 14#endif 15 16GR_NORETAIN_BEGIN 17 18std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::Make(GrMtlGpu* gpu) { 19 if (@available(macOS 10.14, iOS 12.0, *)) { 20 id<MTLEvent> event = [gpu->device() newEvent]; 21 uint64_t value = 1; // seems like a reasonable starting point 22 return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(event, value)); 23 } else { 24 return nullptr; 25 } 26} 27 28std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::MakeWrapped(GrMTLHandle event, 29 uint64_t value) { 30 // The GrMtlSemaphore will have strong ownership at this point. 31 // The GrMTLHandle will subsequently only have weak ownership. 32 if (@available(macOS 10.14, iOS 12.0, *)) { 33 id<MTLEvent> mtlEvent = (__bridge_transfer id<MTLEvent>)event; 34 return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(mtlEvent, value)); 35 } else { 36 return nullptr; 37 } 38} 39 40GrMtlSemaphore::GrMtlSemaphore(id<MTLEvent> event, uint64_t value) 41 : fEvent(event), fValue(value) { 42} 43 44GrBackendSemaphore GrMtlSemaphore::backendSemaphore() const { 45 GrBackendSemaphore backendSemaphore; 46 // The GrMtlSemaphore and the GrBackendSemaphore will have strong ownership at this point. 47 // Whoever uses the GrBackendSemaphore will subsquently steal this ref (see MakeWrapped, above). 48 if (@available(macOS 10.14, iOS 12.0, *)) { 49 GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent); 50 backendSemaphore.initMetal(handle, fValue); 51 } 52 return backendSemaphore; 53} 54 55GR_NORETAIN_END 56