• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/gpu/vk/VkTestUtils.h"
15 #include "tools/window/WindowContext.h"
16 
17 class GrRenderTarget;
18 
19 namespace skgpu { struct VulkanInterface; }
20 
21 namespace skwindow::internal {
22 
23 class GraphiteVulkanWindowContext : public WindowContext {
24 public:
25     ~GraphiteVulkanWindowContext() override;
26 
27     sk_sp<SkSurface> getBackbufferSurface() 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 DisplayParams & params)35     void setDisplayParams(const DisplayParams& 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     GraphiteVulkanWindowContext(const DisplayParams&, 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 DisplayParams& params);
60     bool createBuffers(VkFormat format, VkImageUsageFlags, SkColorType colorType, VkSharingMode);
61     void destroyBuffers();
62     void onSwapBuffers() override;
63 
64     VkInstance fInstance = VK_NULL_HANDLE;
65     VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE;
66     VkDevice fDevice = VK_NULL_HANDLE;
67     VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE;
68 
69     // Create functions
70     CreateVkSurfaceFn fCreateVkSurfaceFn;
71     CanPresentFn      fCanPresentFn;
72 
73     PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr;
74 
75     // WSI interface functions
76     PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr;
77     PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr;
78     PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr;
79     PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr;
80     PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr;
81 
82     PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr;
83     PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr;
84     PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr;
85     PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr;
86     PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr;
87 
88     PFN_vkDestroyInstance fDestroyInstance = nullptr;
89     PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr;
90     PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr;
91     PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr;
92     PFN_vkDestroyDevice fDestroyDevice = nullptr;
93     PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr;
94 
95     sk_sp<const skgpu::VulkanInterface> fInterface;
96 
97     VkSurfaceKHR      fSurface;
98     VkSwapchainKHR    fSwapchain;
99     uint32_t          fGraphicsQueueIndex;
100     VkQueue           fGraphicsQueue;
101     uint32_t          fPresentQueueIndex;
102     VkQueue           fPresentQueue;
103 
104     uint32_t               fImageCount;
105     VkImage*               fImages;         // images in the swapchain
106     VkImageLayout*         fImageLayouts;   // layouts of these images when not color attachment
107     sk_sp<SkSurface>*      fSurfaces;       // surfaces client renders to (may not be based on rts)
108     BackbufferInfo*        fBackbuffers;
109     uint32_t               fCurrentBackbufferIndex;
110     VkSemaphore            fWaitSemaphore = VK_NULL_HANDLE;
111 };
112 
113 }  // namespace skwindow::internal
114 
115 #endif // SK_VULKAN
116 
117 #endif // GraphiteVulkanWindowContext_DEFINED
118