Debian Codegraph @

tools/window/VulkanWindowContext.h

raw
1 /*
2 * Copyright 2023 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 #ifndef VulkanWindowContext_DEFINED
8 #define VulkanWindowContext_DEFINED
9
10 #include "include/core/SkTypes.h"
11
12 #include "include/core/SkSurface.h"
13 #include "include/gpu/vk/VulkanBackendContext.h"
14 #include "tools/gpu/vk/VkTestUtils.h"
15 #include "tools/window/WindowContext.h"
16
17 class GrRenderTarget;
18
19 namespace skgpu { struct VulkanInterface; }
20 namespace skwindow::internal {
21
22 class VulkanWindowContext : public WindowContext {
23 public:
24 ~VulkanWindowContext() override;
25
26 sk_sp<SkSurface> getBackbufferSurface() override;
27
28 bool isValid() override { return fDevice != VK_NULL_HANDLE; }
29
30 void resize(int w, int h) override { this->createSwapchain(w, h); }
31
32 void setDisplayParams(std::unique_ptr<const DisplayParams> params) override {
33 this->destroyContext();
34 fDisplayParams = std::move(params);
35 this->initializeContext();
36 }
37
38 /** Platform specific function that creates a VkSurfaceKHR for a window */
39 using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>;
40 /** Platform specific function that determines whether presentation will succeed. */
41 using CanPresentFn = sk_gpu_test::CanPresentFn;
42
43 VulkanWindowContext(std::unique_ptr<const DisplayParams>,
44 CreateVkSurfaceFn,
45 CanPresentFn,
46 PFN_vkGetInstanceProcAddr);
47
48 private:
49 void initializeContext();
50 void destroyContext();
51
52 bool createSwapchain(int width, int height);
53 bool populateSwapchainImages(VkFormat, VkImageUsageFlags, SkColorType, VkSharingMode);
54
55 /**
56 * Swap backbuffers/frames, presenting the next available image.
57 */
58 void onSwapBuffers() override;
59
60 /**
61 * Define private method to submit work to the GPU (rather than using Window::submitToGpu) in
62 * order to properly configure semaphores. Submits work to render the image at
63 * fCurrentImageIndex.
64 */
65 void submitToGpu();
66
67 /**
68 * Reset the container of images (and destroy each image's associated rendering semaphore).
69 */
70 void resetSwapchainImages();
71
72 /**
73 * Define a struct which represents a swapchain image to be presented. Each SwapchainImage
74 * contains the native VkImage, its image layout while not being used as a color attachment
75 * (which we initialize as VK_IMAGE_LAYOUT_UNDEFINED), a semaphore for signaling render
76 * completion, and an SkSurface the client renders to.
77 */
78 struct SwapchainImage {
79 VkImage fVkImage = VK_NULL_HANDLE;
80 VkImageLayout fImageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
81 VkSemaphore fRenderCompletionSemaphore = VK_NULL_HANDLE;
82 sk_sp<SkSurface> fSurface = nullptr; /* May not be based on rts */
83 };
84
85 skia_private::AutoTArray<SwapchainImage> fImages;
86 uint32_t fCurrentImageIndex; /* Index of image currently being presented */
87 VkSemaphore fAcquireSemaphore = VK_NULL_HANDLE; /* Semaphore to signal image acquisition */
88 sk_sp<const skgpu::VulkanInterface> fInterface;
89
90 /* Vulkan driver structs + info */
91 VkInstance fInstance = VK_NULL_HANDLE;
92 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
93 VkDevice fDevice = VK_NULL_HANDLE;
94 VkDebugUtilsMessengerEXT fDebugMessenger = VK_NULL_HANDLE;
95 VkSurfaceKHR fDeviceSurface;
96 VkSwapchainKHR fSwapchain;
97 uint32_t fGraphicsQueueIndex;
98 VkQueue fGraphicsQueue;
99 uint32_t fPresentQueueIndex;
100 VkQueue fPresentQueue;
101
102 /* Create functions */
103 CreateVkSurfaceFn fCreateVkSurfaceFn;
104 CanPresentFn fCanPresentFn;
105 PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr;
106
107 /* WSI interface functions */
108 PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr;
109 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr;
110 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =
111 nullptr;
112 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
113 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =
114 nullptr;
115 PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr;
116 PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr;
117 PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr;
118 PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr;
119 PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr;
120
121 PFN_vkDestroyInstance fDestroyInstance = nullptr;
122 PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr;
123 PFN_vkDestroyDebugUtilsMessengerEXT fDestroyDebugUtilsMessengerEXT = nullptr;
124 PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr;
125 PFN_vkDestroyDevice fDestroyDevice = nullptr;
126 PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr;
127 };
128
129 } // namespace skwindow::internal
130
131 #endif