|
| 1 | +/* |
| 2 | + * Copyright 2026 Arm Limited and/or its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the BSD-style license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <executorch/backends/vulkan_shared/runtime/export.h> |
| 11 | + |
| 12 | +#include <vulkan/vulkan.h> |
| 13 | + |
| 14 | +#include <cstdint> |
| 15 | +#include <limits> |
| 16 | +#include <memory> |
| 17 | +#include <mutex> |
| 18 | +#include <string> |
| 19 | +#include <string_view> |
| 20 | +#include <utility> |
| 21 | +#include <vector> |
| 22 | + |
| 23 | +namespace executorch { |
| 24 | +namespace backends { |
| 25 | +namespace vulkan_shared { |
| 26 | + |
| 27 | +struct SharedVulkanContextKey final { |
| 28 | + std::string context_name; |
| 29 | + int group_id = 0; |
| 30 | + |
| 31 | + bool valid() const { |
| 32 | + return !context_name.empty(); |
| 33 | + } |
| 34 | + |
| 35 | + friend bool operator==( |
| 36 | + const SharedVulkanContextKey& lhs, |
| 37 | + const SharedVulkanContextKey& rhs) { |
| 38 | + return lhs.group_id == rhs.group_id && lhs.context_name == rhs.context_name; |
| 39 | + } |
| 40 | + |
| 41 | + friend bool operator!=( |
| 42 | + const SharedVulkanContextKey& lhs, |
| 43 | + const SharedVulkanContextKey& rhs) { |
| 44 | + return !(lhs == rhs); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +// The shared layer carries Vulkan handles but deliberately does not call Vulkan |
| 49 | +// entry points itself. Every registered context must provide a lifetime_anchor |
| 50 | +// whose lifetime guarantees that instance, physical_device, device, and queue |
| 51 | +// remain valid until the final SharedVulkanContext reference is released. The |
| 52 | +// anchor destructor may perform backend/application Vulkan teardown. |
| 53 | +struct SharedVulkanContextCreateInfo final { |
| 54 | + SharedVulkanContextKey key; |
| 55 | + VkInstance instance = VK_NULL_HANDLE; |
| 56 | + VkPhysicalDevice physical_device = VK_NULL_HANDLE; |
| 57 | + VkDevice device = VK_NULL_HANDLE; |
| 58 | + VkQueue queue = VK_NULL_HANDLE; |
| 59 | + uint32_t queue_family_index = std::numeric_limits<uint32_t>::max(); |
| 60 | + std::vector<std::string> enabled_device_extensions; |
| 61 | + std::shared_ptr<void> lifetime_anchor; |
| 62 | +}; |
| 63 | + |
| 64 | +class EXECUTORCH_VULKAN_SHARED_API SharedVulkanContext final { |
| 65 | + public: |
| 66 | + explicit SharedVulkanContext(SharedVulkanContextCreateInfo create_info); |
| 67 | + ~SharedVulkanContext(); |
| 68 | + |
| 69 | + SharedVulkanContext(const SharedVulkanContext&) = delete; |
| 70 | + SharedVulkanContext& operator=(const SharedVulkanContext&) = delete; |
| 71 | + |
| 72 | + const SharedVulkanContextKey& key() const; |
| 73 | + VkInstance instance() const; |
| 74 | + VkPhysicalDevice physical_device() const; |
| 75 | + VkDevice device() const; |
| 76 | + |
| 77 | + // Vulkan queue operations require external host synchronization. All |
| 78 | + // delegates sharing this context must issue queue operations through this |
| 79 | + // callback so they synchronize on the same mutex. The VkQueue must not be |
| 80 | + // retained and used after the callback returns. |
| 81 | + template <typename Fn> |
| 82 | + decltype(auto) with_locked_queue(Fn&& fn) const { |
| 83 | + std::lock_guard<std::mutex> lock(queue_mutex_); |
| 84 | + return std::forward<Fn>(fn)(create_info_.queue); |
| 85 | + } |
| 86 | + |
| 87 | + uint32_t queue_family_index() const; |
| 88 | + bool has_device_extension(std::string_view extension_name) const; |
| 89 | + bool is_valid() const; |
| 90 | + |
| 91 | + private: |
| 92 | + SharedVulkanContextCreateInfo create_info_; |
| 93 | + mutable std::mutex queue_mutex_; |
| 94 | +}; |
| 95 | + |
| 96 | +using SharedVulkanContextPtr = std::shared_ptr<SharedVulkanContext>; |
| 97 | + |
| 98 | +} // namespace vulkan_shared |
| 99 | +} // namespace backends |
| 100 | +} // namespace executorch |
0 commit comments