1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2019 Google Inc.
3cb93a386Sopenharmony_ci *
4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci * found in the LICENSE file.
6cb93a386Sopenharmony_ci */
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlSemaphore.h"
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlGpu.h"
11cb93a386Sopenharmony_ci
12cb93a386Sopenharmony_ci#if !__has_feature(objc_arc)
13cb93a386Sopenharmony_ci#error This file must be compiled with Arc. Use -fobjc-arc flag
14cb93a386Sopenharmony_ci#endif
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_ciGR_NORETAIN_BEGIN
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_cisk_sp<GrMtlEvent> GrMtlEvent::Make(GrMtlGpu* gpu) {
19cb93a386Sopenharmony_ci    if (@available(macOS 10.14, iOS 12.0, *)) {
20cb93a386Sopenharmony_ci        id<MTLEvent> event = [gpu->device() newEvent];
21cb93a386Sopenharmony_ci        return sk_sp<GrMtlEvent>(new GrMtlEvent(event));
22cb93a386Sopenharmony_ci    } else {
23cb93a386Sopenharmony_ci        return nullptr;
24cb93a386Sopenharmony_ci    }
25cb93a386Sopenharmony_ci}
26cb93a386Sopenharmony_ci
27cb93a386Sopenharmony_cisk_sp<GrMtlEvent> GrMtlEvent::MakeWrapped(GrMTLHandle event) {
28cb93a386Sopenharmony_ci    // The GrMtlEvent will have strong ownership at this point.
29cb93a386Sopenharmony_ci    // The GrMTLHandle will subsequently only have weak ownership.
30cb93a386Sopenharmony_ci    if (@available(macOS 10.14, iOS 12.0, *)) {
31cb93a386Sopenharmony_ci        id<MTLEvent> mtlEvent = (__bridge_transfer id<MTLEvent>)event;
32cb93a386Sopenharmony_ci        return sk_sp<GrMtlEvent>(new GrMtlEvent(mtlEvent));
33cb93a386Sopenharmony_ci    } else {
34cb93a386Sopenharmony_ci        return nullptr;
35cb93a386Sopenharmony_ci    }
36cb93a386Sopenharmony_ci}
37cb93a386Sopenharmony_ci
38cb93a386Sopenharmony_ciGrBackendSemaphore GrMtlSemaphore::backendSemaphore() const {
39cb93a386Sopenharmony_ci    GrBackendSemaphore backendSemaphore;
40cb93a386Sopenharmony_ci    // The GrMtlSemaphore and the GrBackendSemaphore will have strong ownership at this point.
41cb93a386Sopenharmony_ci    // Whoever uses the GrBackendSemaphore will subsquently steal this ref (see MakeWrapped, above).
42cb93a386Sopenharmony_ci    if (@available(macOS 10.14, iOS 12.0, *)) {
43cb93a386Sopenharmony_ci        GrMTLHandle handle = (__bridge_retained GrMTLHandle)(fEvent->mtlEvent());
44cb93a386Sopenharmony_ci        backendSemaphore.initMetal(handle, fValue);
45cb93a386Sopenharmony_ci    }
46cb93a386Sopenharmony_ci    return backendSemaphore;
47cb93a386Sopenharmony_ci}
48cb93a386Sopenharmony_ci
49cb93a386Sopenharmony_ciGR_NORETAIN_END
50