1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VULKAN_SWAPCHAIN_VK_H
17 #define VULKAN_SWAPCHAIN_VK_H
18 
19 #include <cstdint>
20 #include <vulkan/vulkan_core.h>
21 
22 #include <base/containers/vector.h>
23 #include <render/device/gpu_resource_desc.h>
24 #include <render/namespace.h>
25 
26 #include "device/swapchain.h"
27 
28 RENDER_BEGIN_NAMESPACE()
29 class Device;
30 struct SwapchainCreateInfo;
31 
32 struct SwapchainImagesVk {
33     BASE_NS::vector<VkImage> images;
34     BASE_NS::vector<VkImageView> imageViews;
35 
36     uint32_t width { 0 };
37     uint32_t height { 0 };
38 
39     BASE_NS::vector<VkSemaphore> semaphores;
40 };
41 
42 struct SwapchainPlatformDataVk final {
43     VkSwapchainKHR swapchain { VK_NULL_HANDLE };
44     SwapchainImagesVk swapchainImages;
45 };
46 
47 class SwapchainVk final : public Swapchain {
48 public:
49     SwapchainVk(Device& device, const SwapchainCreateInfo& swapchainCreateInfo);
50     ~SwapchainVk();
51 
52     const SwapchainPlatformDataVk& GetPlatformData() const;
53     const GpuImageDesc& GetDesc() const override;
54     const GpuImageDesc& GetDescDepthBuffer() const override;
55 
56     uint32_t GetFlags() const override;
57     SurfaceTransformFlags GetSurfaceTransformFlags() const override;
58     uint64_t GetSurfaceHandle() const override;
59 
60     // only for locked backend usage to get always the next semaphore index in image acquire
61     uint32_t GetNextAcquireSwapchainSemaphoreIndex() const;
62 
63 private:
64     Device& device_;
65 
66     VkSurfaceKHR surface_ {};
67     bool ownsSurface_ { false };
68 
69     GpuImageDesc desc_;
70     GpuImageDesc descDepthBuffer_;
71     SwapchainPlatformDataVk plat_;
72     uint32_t flags_ { 0u };
73     SurfaceTransformFlags surfaceTransformFlags_ { 0u };
74 
75     // mutable object for locked backend usage only
76     mutable uint32_t currSemaphoreIdx_ { 0U };
77 };
78 RENDER_END_NAMESPACE()
79 
80 #endif // VULKAN_SWAPCHAIN_VK_H
81