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