blob: 12f7eedd12bb6f9cfdf9b4b05f7b44e8ff74392c [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
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
16sk_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
22sk_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
34GrMtlSemaphore::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
40void GrMtlSemaphore::onRelease() {
41 fEvent = nil;
42 INHERITED::onRelease();
43}
44
45void GrMtlSemaphore::onAbandon() {
46 fEvent = nil;
47 INHERITED::onAbandon();
48}
49
50GrBackendSemaphore 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