1 #ifndef SWAP_CHAIN_STATE_VK_H
2 #define SWAP_CHAIN_STATE_VK_H
3 
4 #include <functional>
5 #include <memory>
6 #include <optional>
7 #include <type_traits>
8 #include <unordered_set>
9 #include <vector>
10 
11 #include "goldfish_vk_dispatch.h"
12 
13 namespace gfxstream {
14 namespace vk {
15 
16 struct SwapchainCreateInfoWrapper {
17     VkSwapchainCreateInfoKHR mCreateInfo;
18     std::vector<uint32_t> mQueueFamilyIndices;
19 
20     SwapchainCreateInfoWrapper(const SwapchainCreateInfoWrapper&);
21     SwapchainCreateInfoWrapper(SwapchainCreateInfoWrapper&&) = delete;
22     SwapchainCreateInfoWrapper& operator=(const SwapchainCreateInfoWrapper&);
23     SwapchainCreateInfoWrapper& operator=(SwapchainCreateInfoWrapper&&) = delete;
24 
25     SwapchainCreateInfoWrapper(const VkSwapchainCreateInfoKHR&);
26 
27     void setQueueFamilyIndices(const std::vector<uint32_t>& queueFamilyIndices);
28 };
29 
30 // Assert SwapchainCreateInfoWrapper is a copy only class.
31 static_assert(std::is_copy_assignable_v<SwapchainCreateInfoWrapper> &&
32               std::is_copy_constructible_v<SwapchainCreateInfoWrapper> &&
33               !std::is_move_constructible_v<SwapchainCreateInfoWrapper> &&
34               !std::is_move_assignable_v<SwapchainCreateInfoWrapper>);
35 
36 class SwapChainStateVk {
37    public:
38     static std::vector<const char*> getRequiredInstanceExtensions();
39     static std::vector<const char*> getRequiredDeviceExtensions();
40     static bool validateQueueFamilyProperties(const VulkanDispatch&, VkPhysicalDevice, VkSurfaceKHR,
41                                               uint32_t queueFamilyIndex);
42     static std::optional<SwapchainCreateInfoWrapper> createSwapChainCi(
43         const VulkanDispatch&, VkSurfaceKHR, VkPhysicalDevice, uint32_t width, uint32_t height,
44         const std::unordered_set<uint32_t>& queueFamilyIndices);
45 
46     SwapChainStateVk() = delete;
47     SwapChainStateVk(const SwapChainStateVk&) = delete;
48     SwapChainStateVk& operator = (const SwapChainStateVk&) = delete;
49 
50     static std::unique_ptr<SwapChainStateVk> createSwapChainVk(const VulkanDispatch&, VkDevice,
51                                                                const VkSwapchainCreateInfoKHR&);
52 
53     ~SwapChainStateVk();
54     VkFormat getFormat();
55     VkExtent2D getImageExtent() const;
56     const std::vector<VkImage>& getVkImages() const;
57     const std::vector<VkImageView>& getVkImageViews() const;
58     VkSwapchainKHR getSwapChain() const;
59 
60    private:
61     explicit SwapChainStateVk(const VulkanDispatch&, VkDevice);
62 
63     VkResult initSwapChainStateVk(const VkSwapchainCreateInfoKHR& swapChainCi);
64     const static VkFormat k_vkFormat = VK_FORMAT_B8G8R8A8_UNORM;
65     const static VkColorSpaceKHR k_vkColorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR;
66 
67     const VulkanDispatch& m_vk;
68     VkDevice m_vkDevice;
69     VkSwapchainKHR m_vkSwapChain;
70     VkExtent2D m_vkImageExtent;
71     std::vector<VkImage> m_vkImages;
72     std::vector<VkImageView> m_vkImageViews;
73 };
74 
75 }  // namespace vk
76 }  // namespace gfxstream
77 
78 #endif