Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame^] | 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/GrMtlGpu.h" |
| 9 | #include "src/gpu/mtl/GrMtlSemaphore.h" |
| 10 | |
| 11 | #if !__has_feature(objc_arc) |
| 12 | #error This file must be compiled with Arc. Use -fobjc-arc flag |
| 13 | #endif |
| 14 | |
| 15 | #ifdef GR_METAL_SDK_SUPPORTS_EVENTS |
| 16 | sk_sp<GrMtlSemaphore> GrMtlSemaphore::Make(GrMtlGpu* gpu, bool isOwned) { |
| 17 | id<MTLEvent> event = [gpu->device() newEvent]; |
| 18 | uint64_t value = 1; // seems like a reasonable starting point |
| 19 | return sk_sp<GrMtlSemaphore>(new GrMtlSemaphore(gpu, event, value, isOwned)); |
| 20 | } |
| 21 | |
| 22 | sk_sp<GrMtlSemaphore> GrMtlSemaphore::MakeWrapped(GrMtlGpu* gpu, |
| 23 | GrMTLHandle event, |
| 24 | uint64_t value, |
| 25 | GrWrapOwnership ownership) { |
| 26 | // The GrMtlSemaphore will have strong ownership at this point. |
| 27 | // The GrMTLHandle will subsequently only have weak ownership. |
| 28 | id<MTLEvent> mtlEvent = (__bridge_transfer id<MTLEvent>)event; |
| 29 | auto sema = sk_sp<GrMtlSemaphore>(new GrMtlSemaphore(gpu, mtlEvent, value, |
| 30 | kBorrow_GrWrapOwnership != ownership)); |
| 31 | return sema; |
| 32 | } |
| 33 | |
| 34 | GrMtlSemaphore::GrMtlSemaphore(GrMtlGpu* gpu, id<MTLEvent> event, uint64_t value, bool isOwned) |
| 35 | : INHERITED(gpu), fEvent(event), fValue(value) { |
| 36 | isOwned ? this->registerWithCache(SkBudgeted::kNo) |
| 37 | : this->registerWithCacheWrapped(GrWrapCacheable::kNo); |
| 38 | } |
| 39 | |
| 40 | void GrMtlSemaphore::onRelease() { |
| 41 | fEvent = nil; |
| 42 | INHERITED::onRelease(); |
| 43 | } |
| 44 | |
| 45 | void GrMtlSemaphore::onAbandon() { |
| 46 | fEvent = nil; |
| 47 | INHERITED::onAbandon(); |
| 48 | } |
| 49 | |
| 50 | GrBackendSemaphore GrMtlSemaphore::backendSemaphore() const { |
| 51 | GrBackendSemaphore backendSemaphore; |
| 52 | // The GrMtlSemaphore and the GrBackendSemaphore will have strong ownership at this point. |
| 53 | // Whoever uses the GrBackendSemaphore will subsquently steal this ref (see MakeWrapped, above). |
| 54 | GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent); |
| 55 | backendSemaphore.initMetal(handle, fValue); |
| 56 | return backendSemaphore; |
| 57 | } |
| 58 | #endif |