• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef SK_VULKAN
13 
14 #include "include/gpu/vk/VulkanBackendContext.h"
15 #include "tools/gpu/vk/VkTestUtils.h"
16 #include "tools/window/WindowContext.h"
17 
18 class GrRenderTarget;
19 
20 namespace skgpu { struct VulkanInterface; }
21 
22 namespace skwindow::internal {
23 
24 class VulkanWindowContext : public WindowContext {
25 public:
26     ~VulkanWindowContext() override;
27 
28     sk_sp<SkSurface> getBackbufferSurface() override;
29 
isValid()30     bool isValid() override { return fDevice != VK_NULL_HANDLE; }
31 
resize(int w,int h)32     void resize(int w, int h) override {
33         this->createSwapchain(w, h, fDisplayParams);
34     }
35 
setDisplayParams(const DisplayParams & params)36     void setDisplayParams(const DisplayParams& params) override {
37         this->destroyContext();
38         fDisplayParams = params;
39         this->initializeContext();
40     }
41 
42     /** Platform specific function that creates a VkSurfaceKHR for a window */
43     using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>;
44     /** Platform specific function that determines whether presentation will succeed. */
45     using CanPresentFn = sk_gpu_test::CanPresentFn;
46 
47     VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn,
48                         PFN_vkGetInstanceProcAddr);
49 
50 private:
51     void initializeContext();
52     void destroyContext();
53 
54     struct BackbufferInfo {
55         uint32_t        fImageIndex;          // image this is associated with
56         VkSemaphore     fRenderSemaphore;     // we wait on this for rendering to be done
57     };
58 
59     BackbufferInfo* getAvailableBackbuffer();
60     bool createSwapchain(int width, int height, const DisplayParams& params);
61     bool createBuffers(VkFormat format, VkImageUsageFlags, SkColorType colorType, VkSharingMode);
62     void destroyBuffers();
63     void onSwapBuffers() override;
64 
65     VkInstance fInstance = VK_NULL_HANDLE;
66     VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
67     VkDevice fDevice = VK_NULL_HANDLE;
68     VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE;
69 
70     // Create functions
71     CreateVkSurfaceFn fCreateVkSurfaceFn;
72     CanPresentFn      fCanPresentFn;
73 
74     PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr;
75 
76     // WSI interface functions
77     PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr;
78     PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr;
79     PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr;
80     PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
81     PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr;
82 
83     PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr;
84     PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr;
85     PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr;
86     PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr;
87     PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr;
88 
89     PFN_vkDestroyInstance fDestroyInstance = nullptr;
90     PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr;
91     PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr;
92     PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr;
93     PFN_vkDestroyDevice fDestroyDevice = nullptr;
94     PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr;
95 
96     sk_sp<const skgpu::VulkanInterface> fInterface;
97 
98     VkSurfaceKHR      fSurface;
99     VkSwapchainKHR    fSwapchain;
100     uint32_t          fGraphicsQueueIndex;
101     VkQueue           fGraphicsQueue;
102     uint32_t          fPresentQueueIndex;
103     VkQueue           fPresentQueue;
104 
105     uint32_t               fImageCount;
106     VkImage*               fImages;         // images in the swapchain
107     VkImageLayout*         fImageLayouts;   // layouts of these images when not color attachment
108     sk_sp<SkSurface>*      fSurfaces;       // surfaces client renders to (may not be based on rts)
109     BackbufferInfo*        fBackbuffers;
110     uint32_t               fCurrentBackbufferIndex;
111 };
112 
113 }  // namespace skwindow::internal
114 
115 #endif // SK_VULKAN
116 
117 #endif
118