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 | |
Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame] | 8 | #include "src/gpu/mtl/GrMtlSemaphore.h" |
| 9 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 10 | #include "src/gpu/mtl/GrMtlGpu.h" |
| 11 | |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 12 | #if !__has_feature(objc_arc) |
| 13 | #error This file must be compiled with Arc. Use -fobjc-arc flag |
| 14 | #endif |
| 15 | |
Adlai Holler | 194b239 | 2021-03-29 11:20:43 -0400 | [diff] [blame^] | 16 | GR_NORETAIN_BEGIN |
| 17 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 18 | std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::Make(GrMtlGpu* gpu) { |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 19 | if (@available(macOS 10.14, iOS 12.0, *)) { |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 20 | id<MTLEvent> event = [gpu->device() newEvent]; |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 21 | uint64_t value = 1; // seems like a reasonable starting point |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 22 | return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(event, value)); |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 23 | } else { |
| 24 | return nullptr; |
| 25 | } |
Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Greg Daniel | 301015c | 2019-11-18 14:06:46 -0500 | [diff] [blame] | 28 | std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::MakeWrapped(GrMTLHandle event, |
| 29 | uint64_t value) { |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 30 | // The GrMtlSemaphore will have strong ownership at this point. |
| 31 | // The GrMTLHandle will subsequently only have weak ownership. |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 32 | if (@available(macOS 10.14, iOS 12.0, *)) { |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 33 | id<MTLEvent> mtlEvent = (__bridge_transfer id<MTLEvent>)event; |
| 34 | return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(mtlEvent, value)); |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 35 | } else { |
| 36 | return nullptr; |
| 37 | } |
Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 40 | GrMtlSemaphore::GrMtlSemaphore(id<MTLEvent> event, uint64_t value) |
| 41 | : fEvent(event), fValue(value) { |
Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | GrBackendSemaphore 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). |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 48 | if (@available(macOS 10.14, iOS 12.0, *)) { |
Jim Van Verth | 36b86af | 2020-08-24 17:16:46 +0000 | [diff] [blame] | 49 | GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent); |
Jim Van Verth | a431186 | 2019-09-10 14:03:10 -0400 | [diff] [blame] | 50 | backendSemaphore.initMetal(handle, fValue); |
| 51 | } |
Jim Van Verth | 066ceb1 | 2019-08-28 14:35:55 -0400 | [diff] [blame] | 52 | return backendSemaphore; |
| 53 | } |
Adlai Holler | 194b239 | 2021-03-29 11:20:43 -0400 | [diff] [blame^] | 54 | |
| 55 | GR_NORETAIN_END |