blob: 8aab5b3de86b58eaa45cc48d4d0d8a5e97cac919 [file] [log] [blame]
Jim Van Verth066ceb12019-08-28 14:35:55 -04001/*
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 Verth066ceb12019-08-28 14:35:55 -04008#include "src/gpu/mtl/GrMtlSemaphore.h"
9
Greg Daniel301015c2019-11-18 14:06:46 -050010#include "src/gpu/mtl/GrMtlGpu.h"
11
Jim Van Verth36b86af2020-08-24 17:16:46 +000012#if !__has_feature(objc_arc)
13#error This file must be compiled with Arc. Use -fobjc-arc flag
14#endif
15
Adlai Holler194b2392021-03-29 11:20:43 -040016GR_NORETAIN_BEGIN
17
Greg Daniel301015c2019-11-18 14:06:46 -050018std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::Make(GrMtlGpu* gpu) {
Jim Van Vertha4311862019-09-10 14:03:10 -040019 if (@available(macOS 10.14, iOS 12.0, *)) {
Jim Van Verth36b86af2020-08-24 17:16:46 +000020 id<MTLEvent> event = [gpu->device() newEvent];
Jim Van Vertha4311862019-09-10 14:03:10 -040021 uint64_t value = 1; // seems like a reasonable starting point
Jim Van Verth36b86af2020-08-24 17:16:46 +000022 return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(event, value));
Jim Van Vertha4311862019-09-10 14:03:10 -040023 } else {
24 return nullptr;
25 }
Jim Van Verth066ceb12019-08-28 14:35:55 -040026}
27
Greg Daniel301015c2019-11-18 14:06:46 -050028std::unique_ptr<GrMtlSemaphore> GrMtlSemaphore::MakeWrapped(GrMTLHandle event,
29 uint64_t value) {
Jim Van Verth36b86af2020-08-24 17:16:46 +000030 // The GrMtlSemaphore will have strong ownership at this point.
31 // The GrMTLHandle will subsequently only have weak ownership.
Jim Van Vertha4311862019-09-10 14:03:10 -040032 if (@available(macOS 10.14, iOS 12.0, *)) {
Jim Van Verth36b86af2020-08-24 17:16:46 +000033 id<MTLEvent> mtlEvent = (__bridge_transfer id<MTLEvent>)event;
34 return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(mtlEvent, value));
Jim Van Vertha4311862019-09-10 14:03:10 -040035 } else {
36 return nullptr;
37 }
Jim Van Verth066ceb12019-08-28 14:35:55 -040038}
39
Jim Van Verth36b86af2020-08-24 17:16:46 +000040GrMtlSemaphore::GrMtlSemaphore(id<MTLEvent> event, uint64_t value)
41 : fEvent(event), fValue(value) {
Jim Van Verth066ceb12019-08-28 14:35:55 -040042}
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).
Jim Van Vertha4311862019-09-10 14:03:10 -040048 if (@available(macOS 10.14, iOS 12.0, *)) {
Jim Van Verth36b86af2020-08-24 17:16:46 +000049 GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent);
Jim Van Vertha4311862019-09-10 14:03:10 -040050 backendSemaphore.initMetal(handle, fValue);
51 }
Jim Van Verth066ceb12019-08-28 14:35:55 -040052 return backendSemaphore;
53}
Adlai Holler194b2392021-03-29 11:20:43 -040054
55GR_NORETAIN_END