1 2 /* 3 * Copyright 2016 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #ifndef VulkanWindowContext_DEFINED 9 #define VulkanWindowContext_DEFINED 10 11 #include "SkTypes.h" // required to pull in any SkUserConfig defines 12 13 #ifdef SK_VULKAN 14 15 #include "vk/GrVkVulkan.h" 16 17 #include "vk/GrVkBackendContext.h" 18 #include "vk/GrVkInterface.h" 19 #include "vk/VkTestUtils.h" 20 #include "WindowContext.h" 21 22 class GrRenderTarget; 23 24 namespace sk_app { 25 26 class VulkanWindowContext : public WindowContext { 27 public: 28 ~VulkanWindowContext() override; 29 30 sk_sp<SkSurface> getBackbufferSurface() override; 31 void swapBuffers() override; 32 isValid()33 bool isValid() override { return fDevice != VK_NULL_HANDLE; } 34 resize(int w,int h)35 void resize(int w, int h) override { 36 this->createSwapchain(w, h, fDisplayParams); 37 } 38 setDisplayParams(const DisplayParams & params)39 void setDisplayParams(const DisplayParams& params) override { 40 this->destroyContext(); 41 fDisplayParams = params; 42 this->initializeContext(); 43 } 44 45 /** Platform specific function that creates a VkSurfaceKHR for a window */ 46 using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>; 47 /** Platform specific function that determines whether presentation will succeed. */ 48 using CanPresentFn = sk_gpu_test::CanPresentFn; 49 50 VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn, 51 PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr); 52 53 private: 54 void initializeContext(); 55 void destroyContext(); 56 57 struct BackbufferInfo { 58 uint32_t fImageIndex; // image this is associated with 59 VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image 60 VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done 61 VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render 62 VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU 63 }; 64 65 BackbufferInfo* getAvailableBackbuffer(); 66 bool createSwapchain(int width, int height, const DisplayParams& params); 67 void createBuffers(VkFormat format, SkColorType colorType); 68 void destroyBuffers(); 69 70 VkInstance fInstance = VK_NULL_HANDLE; 71 VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE; 72 VkDevice fDevice = VK_NULL_HANDLE; 73 VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE; 74 75 // Create functions 76 CreateVkSurfaceFn fCreateVkSurfaceFn; 77 CanPresentFn fCanPresentFn; 78 79 // Vulkan GetProcAddr functions 80 PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr; 81 PFN_vkGetDeviceProcAddr fGetDeviceProcAddr = nullptr; 82 83 // WSI interface functions 84 PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr; 85 PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr; 86 PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr; 87 PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr; 88 PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr; 89 90 PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr; 91 PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr; 92 PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr; 93 PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr; 94 PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr; 95 96 PFN_vkDestroyInstance fDestroyInstance = nullptr; 97 PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr; 98 PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr; 99 PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr; 100 PFN_vkDestroyDevice fDestroyDevice = nullptr; 101 PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr; 102 103 sk_sp<const GrVkInterface> fInterface; 104 105 VkSurfaceKHR fSurface; 106 VkSwapchainKHR fSwapchain; 107 uint32_t fGraphicsQueueIndex; 108 VkQueue fGraphicsQueue; 109 uint32_t fPresentQueueIndex; 110 VkQueue fPresentQueue; 111 112 uint32_t fImageCount; 113 VkImage* fImages; // images in the swapchain 114 VkImageLayout* fImageLayouts; // layouts of these images when not color attachment 115 sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) 116 VkCommandPool fCommandPool; 117 BackbufferInfo* fBackbuffers; 118 uint32_t fCurrentBackbufferIndex; 119 }; 120 121 } // namespace sk_app 122 123 #endif // SK_VULKAN 124 125 #endif 126