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#include "include/gpu/mtl/GrMtlTypes.h"
15#include "include/gpu/vk/GrVkTypes.h"
16
17/**
18 * Wrapper class for passing into and receiving data from Ganesh about a backend semaphore object.
19 */
20class GrBackendSemaphore {
21public:
22 // For convenience we just set the backend here to OpenGL. The GrBackendSemaphore cannot be used
23 // until either initGL or initVulkan are called which will set the appropriate GrBackend.
24 GrBackendSemaphore() : fBackend(GrBackendApi::kOpenGL), fGLSync(0), fIsInitialized(false) {}
25
26 void initGL(GrGLsync sync) {
27 fBackend = GrBackendApi::kOpenGL;
28 fGLSync = sync;
29 fIsInitialized = true;
30 }
31
32 void initVulkan(VkSemaphore semaphore) {
33 fBackend = GrBackendApi::kVulkan;
34 fVkSemaphore = semaphore;
35#ifdef SK_VULKAN
36 fIsInitialized = true;
37#else
38 fIsInitialized = false;
39#endif
40 }
41
42 // It is the creator's responsibility to ref the MTLEvent passed in here, via __bridge_retained.
43 // The other end will wrap this BackendSemaphore and take the ref, via __bridge_transfer.
44 void initMetal(GrMTLHandle event, uint64_t value) {
45 fBackend = GrBackendApi::kMetal;
46 fMtlEvent = event;
47 fMtlValue = value;
48#ifdef SK_METAL
49 fIsInitialized = true;
50#else
51 fIsInitialized = false;
52#endif
53 }
54
55 bool isInitialized() const { return fIsInitialized; }
56
57 GrGLsync glSync() const {
58 if (!fIsInitialized || GrBackendApi::kOpenGL != fBackend) {
59 return 0;
60 }
61 return fGLSync;
62 }
63
64 VkSemaphore vkSemaphore() const {
65 if (!fIsInitialized || GrBackendApi::kVulkan != fBackend) {
66 return VK_NULL_HANDLE;
67 }
68 return fVkSemaphore;
69 }
70
71 GrMTLHandle mtlSemaphore() const {
72 if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
73 return nullptr;
74 }
75 return fMtlEvent;
76 }
77
78 uint64_t mtlValue() const {
79 if (!fIsInitialized || GrBackendApi::kMetal != fBackend) {
80 return 0;
81 }
82 return fMtlValue;
83 }
84
85private:
86 GrBackendApi fBackend;
87 union {
88 GrGLsync fGLSync;
89 VkSemaphore fVkSemaphore;
90 GrMTLHandle fMtlEvent; // Expected to be an id<MTLEvent>
91 };
92 uint64_t fMtlValue;
93 bool fIsInitialized;
94};
95
96#endif
97