xref: /aosp_15_r20/external/skia/src/gpu/graphite/vk/VulkanBackendSemaphore.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2024 Google LLC
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 #include "include/core/SkString.h"
8 #include "include/gpu/graphite/BackendSemaphore.h"
9 #include "include/private/gpu/vk/SkiaVulkan.h"
10 #include "src/gpu/graphite/BackendSemaphorePriv.h"
11 
12 #include <cstdint>
13 
14 namespace skgpu::graphite {
15 
16 class VulkanBackendSemaphoreData final : public BackendSemaphoreData {
17 public:
VulkanBackendSemaphoreData(VkSemaphore sem)18     VulkanBackendSemaphoreData(VkSemaphore sem) : fVkSemaphore(sem) {}
19 
20 #if defined(SK_DEBUG)
type() const21     skgpu::BackendApi type() const override { return skgpu::BackendApi::kVulkan; }
22 #endif
23 
semaphore() const24     VkSemaphore semaphore() const { return fVkSemaphore; }
25 
26 private:
27     VkSemaphore fVkSemaphore;
28 
copyTo(AnyBackendSemaphoreData & dstData) const29     void copyTo(AnyBackendSemaphoreData& dstData) const override {
30         // Don't assert that dstData has a Vulkan type() because it could be
31         // uninitialized and that assert would fail.
32         dstData.emplace<VulkanBackendSemaphoreData>(fVkSemaphore);
33     }
34 };
35 
get_and_cast_data(const BackendSemaphore & sem)36 static const VulkanBackendSemaphoreData* get_and_cast_data(const BackendSemaphore& sem) {
37     auto data = BackendSemaphorePriv::GetData(sem);
38     SkASSERT(!data || data->type() == skgpu::BackendApi::kVulkan);
39     return static_cast<const VulkanBackendSemaphoreData*>(data);
40 }
41 
42 namespace BackendSemaphores {
MakeVulkan(VkSemaphore sem)43 BackendSemaphore MakeVulkan(VkSemaphore sem) {
44     return BackendSemaphorePriv::Make(skgpu::BackendApi::kVulkan, VulkanBackendSemaphoreData(sem));
45 }
46 
GetVkSemaphore(const BackendSemaphore & sem)47 VkSemaphore GetVkSemaphore(const BackendSemaphore& sem) {
48     if (!sem.isValid() || sem.backend() != skgpu::BackendApi::kVulkan) {
49         return VK_NULL_HANDLE;
50     }
51     const VulkanBackendSemaphoreData* vkData = get_and_cast_data(sem);
52     SkASSERT(vkData);
53     return vkData->semaphore();
54 }
55 
56 }  // namespace BackendSemaphores
57 
58 }  // namespace skgpu::graphite
59