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