Skia
2D Graphics Library
GrBackendSemaphore.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 GrBackendSemaphore_DEFINED
9 #define GrBackendSemaphore_DEFINED
10 
11 #include "include/gpu/GrTypes.h" // IWYU pragma: keep
12 #include "include/private/base/SkAPI.h"
13 #include "include/private/base/SkAnySubclass.h"
14 
15 #ifdef SK_METAL
16 #include "include/gpu/mtl/GrMtlTypes.h"
17 #endif
18 
19 #ifdef SK_VULKAN
20 #include "include/private/gpu/vk/SkiaVulkan.h"
21 #endif
22 
23 #ifdef SK_DIRECT3D
24 #include "include/private/gpu/ganesh/GrD3DTypesMinimal.h"
25 #endif
26 
27 #include <cstddef>
28 
29 class GrBackendSemaphoreData;
30 
34 class SK_API GrBackendSemaphore {
35 public:
36  // The GrBackendSemaphore cannot be used until either init* is called, which will set the
37  // appropriate GrBackend.
42 
43 #ifdef SK_VULKAN
44  void initVulkan(VkSemaphore semaphore) {
45  fBackend = GrBackendApi::kVulkan;
46  fVkSemaphore = semaphore;
47 
48  fIsInitialized = true;
49  }
50 
51  VkSemaphore vkSemaphore() const {
52  if (!fIsInitialized || GrBackendApi::kVulkan != fBackend) {
53  return VK_NULL_HANDLE;
54  }
55  return fVkSemaphore;
56  }
57 #endif
58 
59 #ifdef SK_METAL
60  // It is the creator's responsibility to ref the MTLEvent passed in here, via __bridge_retained.
61  // The other end will wrap this BackendSemaphore and take the ref, via __bridge_transfer.
62  void initMetal(GrMTLHandle event, uint64_t value) {
63  fBackend = GrBackendApi::kMetal;
64  fMtlEvent = event;
65  fMtlValue = value;
66 
67  fIsInitialized = true;
68  }
69 
70  GrMTLHandle mtlSemaphore() const {
71  if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
72  return nullptr;
73  }
74  return fMtlEvent;
75  }
76 
77  uint64_t mtlValue() const {
78  if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
79  return 0;
80  }
81  return fMtlValue;
82  }
83 
84 #endif
85 
86 #ifdef SK_DIRECT3D
87  void initDirect3D(const GrD3DFenceInfo& info) {
88  fBackend = GrBackendApi::kDirect3D;
89  this->assignD3DFenceInfo(info);
90  fIsInitialized = true;
91  }
92 #endif
93 
94  bool isInitialized() const { return fIsInitialized; }
95  GrBackendApi backend() const { return fBackend; }
96 
97 #ifdef SK_DIRECT3D
98  bool getD3DFenceInfo(GrD3DFenceInfo* outInfo) const;
99 #endif
100 
101 private:
102  friend class GrBackendSemaphorePriv;
103  friend class GrBackendSemaphoreData;
104  // Size determined by looking at the GrBackendSemaphoreData subclasses, then
105  // guessing-and-checking. Compiler will complain if this is too small - in that case,
106  // just increase the number.
107  inline constexpr static size_t kMaxSubclassSize = 16;
108  using AnySemaphoreData = SkAnySubclass<GrBackendSemaphoreData, kMaxSubclassSize>;
109 
110  template <typename SemaphoreData>
111  GrBackendSemaphore(GrBackendApi api, SemaphoreData data) : fBackend(api), fIsInitialized(true) {
112  fSemaphoreData.emplace<SemaphoreData>(data);
113  }
114 
115 #ifdef SK_DIRECT3D
116  void assignD3DFenceInfo(const GrD3DFenceInfo& info);
117 #endif
118 
119  GrBackendApi fBackend;
120  AnySemaphoreData fSemaphoreData;
121 
122  union {
123  void* fPlaceholder; // TODO(293490566)
124 #ifdef SK_VULKAN
125  VkSemaphore fVkSemaphore;
126 #endif
127 #ifdef SK_METAL
128  GrMTLHandle fMtlEvent; // Expected to be an id<MTLEvent>
129 #endif
130 #ifdef SK_DIRECT3D
131  GrD3DFenceInfo* fD3DFenceInfo;
132 #endif
133  };
134 #ifdef SK_METAL
135  uint64_t fMtlValue;
136 #endif
137  bool fIsInitialized;
138 };
139 
140 #endif
GrBackendApi
Possible 3D APIs that may be used by Ganesh.
Definition: GrTypes.h:96
Wrapper class for passing into and receiving data from Ganesh about a backend semaphore object.
Definition: GrBackendSemaphore.h:34
void * fPlaceholder
Definition: GrBackendSemaphore.h:123
GrBackendSemaphore(const GrBackendSemaphore &)
GrBackendApi backend() const
Definition: GrBackendSemaphore.h:95
bool isInitialized() const
Definition: GrBackendSemaphore.h:94
GrBackendSemaphore & operator=(const GrBackendSemaphore &)