Debian Codegraph @

tools/window/GraphiteNativeVulkanWindowContext.h

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