• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
12 
13 #include "include/gpu/gl/GrGLTypes.h"
14 
15 #ifdef SK_METAL
16 #include "include/gpu/mtl/GrMtlTypes.h"
17 #endif
18 
19 #ifdef SK_VULKAN
20 #include "include/gpu/vk/GrVkTypes.h"
21 #endif
22 
23 #ifdef SK_DIRECT3D
24 #include "include/private/GrD3DTypesMinimal.h"
25 #endif
26 
27 /**
28  * Wrapper class for passing into and receiving data from Ganesh about a backend semaphore object.
29  */
30 class GrBackendSemaphore {
31 public:
32     // For convenience we just set the backend here to OpenGL. The GrBackendSemaphore cannot be used
33     // until either init* is called, which will set the appropriate GrBackend.
GrBackendSemaphore()34     GrBackendSemaphore()
35             : fBackend(GrBackendApi::kOpenGL), fGLSync(nullptr), fIsInitialized(false) {}
36 
37 #ifdef SK_DIRECT3D
38     // We only need to specify these if Direct3D is enabled, because it requires special copy
39     // characteristics.
40     ~GrBackendSemaphore();
41     GrBackendSemaphore(const GrBackendSemaphore&);
42     GrBackendSemaphore& operator=(const GrBackendSemaphore&);
43 #endif
44 
initGL(GrGLsync sync)45     void initGL(GrGLsync sync) {
46         fBackend = GrBackendApi::kOpenGL;
47         fGLSync = sync;
48         fIsInitialized = true;
49     }
50 
51 #ifdef SK_VULKAN
initVulkan(VkSemaphore semaphore)52     void initVulkan(VkSemaphore semaphore) {
53         fBackend = GrBackendApi::kVulkan;
54         fVkSemaphore = semaphore;
55 
56         fIsInitialized = true;
57     }
58 
vkSemaphore()59     VkSemaphore vkSemaphore() const {
60         if (!fIsInitialized || GrBackendApi::kVulkan != fBackend) {
61             return VK_NULL_HANDLE;
62         }
63         return fVkSemaphore;
64     }
65 #endif
66 
67 #ifdef SK_METAL
68     // It is the creator's responsibility to ref the MTLEvent passed in here, via __bridge_retained.
69     // The other end will wrap this BackendSemaphore and take the ref, via __bridge_transfer.
initMetal(GrMTLHandle event,uint64_t value)70     void initMetal(GrMTLHandle event, uint64_t value) {
71         fBackend = GrBackendApi::kMetal;
72         fMtlEvent = event;
73         fMtlValue = value;
74 
75         fIsInitialized = true;
76     }
77 
mtlSemaphore()78     GrMTLHandle mtlSemaphore() const {
79         if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
80             return nullptr;
81         }
82         return fMtlEvent;
83     }
84 
mtlValue()85     uint64_t mtlValue() const {
86         if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
87             return 0;
88         }
89         return fMtlValue;
90     }
91 
92 #endif
93 
94 #ifdef SK_DIRECT3D
initDirect3D(const GrD3DFenceInfo & info)95     void initDirect3D(const GrD3DFenceInfo& info) {
96         fBackend = GrBackendApi::kDirect3D;
97         this->assignD3DFenceInfo(info);
98         fIsInitialized = true;
99     }
100 #endif
101 
isInitialized()102     bool isInitialized() const { return fIsInitialized; }
103 
glSync()104     GrGLsync glSync() const {
105         if (!fIsInitialized || GrBackendApi::kOpenGL != fBackend) {
106             return nullptr;
107         }
108         return fGLSync;
109     }
110 
111 
112 #ifdef SK_DIRECT3D
113     bool getD3DFenceInfo(GrD3DFenceInfo* outInfo) const;
114 #endif
115 
116 private:
117 #ifdef SK_DIRECT3D
118     void assignD3DFenceInfo(const GrD3DFenceInfo& info);
119 #endif
120 
121     GrBackendApi fBackend;
122     union {
123         GrGLsync    fGLSync;
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
141