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#ifndef GrMtlSemaphore_DEFINED 9cb93a386Sopenharmony_ci#define GrMtlSemaphore_DEFINED 10cb93a386Sopenharmony_ci 11cb93a386Sopenharmony_ci#include "include/gpu/GrBackendSemaphore.h" 12cb93a386Sopenharmony_ci#include "include/private/GrTypesPriv.h" 13cb93a386Sopenharmony_ci#include "src/gpu/GrManagedResource.h" 14cb93a386Sopenharmony_ci#include "src/gpu/GrSemaphore.h" 15cb93a386Sopenharmony_ci#include "src/gpu/mtl/GrMtlUtil.h" 16cb93a386Sopenharmony_ci 17cb93a386Sopenharmony_ci#include <Metal/Metal.h> 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_ciclass GrMtlGpu; 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ciclass GrMtlEvent : public GrManagedResource { 22cb93a386Sopenharmony_cipublic: 23cb93a386Sopenharmony_ci static sk_sp<GrMtlEvent> Make(GrMtlGpu* gpu); 24cb93a386Sopenharmony_ci 25cb93a386Sopenharmony_ci static sk_sp<GrMtlEvent> MakeWrapped(GrMTLHandle event); 26cb93a386Sopenharmony_ci 27cb93a386Sopenharmony_ci ~GrMtlEvent() override {} 28cb93a386Sopenharmony_ci 29cb93a386Sopenharmony_ci id<MTLEvent> mtlEvent() const SK_API_AVAILABLE(macos(10.14), ios(12.0)) { return fMtlEvent; } 30cb93a386Sopenharmony_ci 31cb93a386Sopenharmony_ci#ifdef SK_TRACE_MANAGED_RESOURCES 32cb93a386Sopenharmony_ci /** output a human-readable dump of this resource's information 33cb93a386Sopenharmony_ci */ 34cb93a386Sopenharmony_ci void dumpInfo() const override { 35cb93a386Sopenharmony_ci if (@available(macOS 10.14, iOS 12.0, *)) { 36cb93a386Sopenharmony_ci SkDebugf("GrMtlEvent: %p (%ld refs)\n", fMtlEvent, 37cb93a386Sopenharmony_ci CFGetRetainCount((CFTypeRef)fMtlEvent)); 38cb93a386Sopenharmony_ci } 39cb93a386Sopenharmony_ci } 40cb93a386Sopenharmony_ci#endif 41cb93a386Sopenharmony_ci 42cb93a386Sopenharmony_ci void freeGPUData() const override { 43cb93a386Sopenharmony_ci if (@available(macOS 10.14, iOS 12.0, *)) { 44cb93a386Sopenharmony_ci fMtlEvent = nil; 45cb93a386Sopenharmony_ci } 46cb93a386Sopenharmony_ci } 47cb93a386Sopenharmony_ci 48cb93a386Sopenharmony_ciprivate: 49cb93a386Sopenharmony_ci GrMtlEvent(id<MTLEvent> mtlEvent) SK_API_AVAILABLE(macos(10.14), ios(12.0)) 50cb93a386Sopenharmony_ci : fMtlEvent(mtlEvent) {} 51cb93a386Sopenharmony_ci 52cb93a386Sopenharmony_ci mutable id<MTLEvent> fMtlEvent SK_API_AVAILABLE(macos(10.14), ios(12.0)); 53cb93a386Sopenharmony_ci}; 54cb93a386Sopenharmony_ci 55cb93a386Sopenharmony_ciclass GrMtlSemaphore : public GrSemaphore { 56cb93a386Sopenharmony_cipublic: 57cb93a386Sopenharmony_ci static std::unique_ptr<GrMtlSemaphore> Make(GrMtlGpu* gpu) { 58cb93a386Sopenharmony_ci sk_sp<GrMtlEvent> event = GrMtlEvent::Make(gpu); 59cb93a386Sopenharmony_ci if (!event) { 60cb93a386Sopenharmony_ci return nullptr; 61cb93a386Sopenharmony_ci } 62cb93a386Sopenharmony_ci return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(std::move(event), 1)); 63cb93a386Sopenharmony_ci } 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ci static std::unique_ptr<GrMtlSemaphore> MakeWrapped(GrMTLHandle mtlEvent, uint64_t value) { 66cb93a386Sopenharmony_ci sk_sp<GrMtlEvent> event = GrMtlEvent::MakeWrapped(mtlEvent); 67cb93a386Sopenharmony_ci if (!event) { 68cb93a386Sopenharmony_ci return nullptr; 69cb93a386Sopenharmony_ci } 70cb93a386Sopenharmony_ci return std::unique_ptr<GrMtlSemaphore>(new GrMtlSemaphore(std::move(event), value)); 71cb93a386Sopenharmony_ci } 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci ~GrMtlSemaphore() override {} 74cb93a386Sopenharmony_ci 75cb93a386Sopenharmony_ci sk_sp<GrMtlEvent> event() { return fEvent; } 76cb93a386Sopenharmony_ci uint64_t value() const { return fValue; } 77cb93a386Sopenharmony_ci 78cb93a386Sopenharmony_ci GrBackendSemaphore backendSemaphore() const override; 79cb93a386Sopenharmony_ci 80cb93a386Sopenharmony_ciprivate: 81cb93a386Sopenharmony_ci GrMtlSemaphore(sk_sp<GrMtlEvent> event, uint64_t value) 82cb93a386Sopenharmony_ci : fEvent(std::move(event)) 83cb93a386Sopenharmony_ci , fValue(value) {} 84cb93a386Sopenharmony_ci 85cb93a386Sopenharmony_ci void setIsOwned() override {} 86cb93a386Sopenharmony_ci 87cb93a386Sopenharmony_ci sk_sp<GrMtlEvent> fEvent; 88cb93a386Sopenharmony_ci uint64_t fValue; 89cb93a386Sopenharmony_ci 90cb93a386Sopenharmony_ci using INHERITED = GrSemaphore; 91cb93a386Sopenharmony_ci}; 92cb93a386Sopenharmony_ci 93cb93a386Sopenharmony_ci#endif 94