jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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 | */ |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 8 | #ifndef VulkanWindowContext_DEFINED |
| 9 | #define VulkanWindowContext_DEFINED |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 10 | |
djsollen | 9207cae | 2016-06-10 07:50:00 -0700 | [diff] [blame] | 11 | #include "SkTypes.h" // required to pull in any SkUserConfig defines |
| 12 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 13 | #ifdef SK_VULKAN |
| 14 | |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 15 | #include "vk/GrVkVulkan.h" |
| 16 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | #include "vk/GrVkBackendContext.h" |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 18 | #include "vk/GrVkInterface.h" |
| 19 | #include "vk/VkTestUtils.h" |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 20 | #include "WindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 21 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 22 | class GrRenderTarget; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 23 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 24 | namespace sk_app { |
| 25 | |
| 26 | class VulkanWindowContext : public WindowContext { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 27 | public: |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 28 | ~VulkanWindowContext() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 29 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 30 | sk_sp<SkSurface> getBackbufferSurface() override; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 31 | void swapBuffers() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 32 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 33 | bool isValid() override { return fDevice != VK_NULL_HANDLE; } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 34 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 35 | void resize(int w, int h) override { |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 36 | this->createSwapchain(w, h, fDisplayParams); |
| 37 | } |
| 38 | |
liyuqian | 796c5bb | 2016-05-09 08:49:29 -0700 | [diff] [blame] | 39 | void setDisplayParams(const DisplayParams& params) override { |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 40 | this->destroyContext(); |
| 41 | fDisplayParams = params; |
| 42 | this->initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 43 | } |
| 44 | |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 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. */ |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 48 | using CanPresentFn = sk_gpu_test::CanPresentFn; |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 49 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 50 | VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn, |
| 51 | PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 52 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 53 | private: |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 54 | void initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 66 | bool createSwapchain(int width, int height, const DisplayParams& params); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 67 | void createBuffers(VkFormat format, SkColorType colorType); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 68 | void destroyBuffers(); |
| 69 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 70 | VkInstance fInstance = VK_NULL_HANDLE; |
| 71 | VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE; |
| 72 | VkDevice fDevice = VK_NULL_HANDLE; |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 73 | VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 74 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 75 | // Create functions |
| 76 | CreateVkSurfaceFn fCreateVkSurfaceFn; |
| 77 | CanPresentFn fCanPresentFn; |
| 78 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 79 | // Vulkan GetProcAddr functions |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 80 | PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr; |
| 81 | PFN_vkGetDeviceProcAddr fGetDeviceProcAddr = nullptr; |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 82 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 83 | // WSI interface functions |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 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; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 89 | |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 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; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 95 | |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 96 | PFN_vkDestroyInstance fDestroyInstance = nullptr; |
| 97 | PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr; |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 98 | PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 99 | PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr; |
| 100 | PFN_vkDestroyDevice fDestroyDevice = nullptr; |
| 101 | PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 102 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 103 | sk_sp<const GrVkInterface> fInterface; |
| 104 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 105 | VkSurfaceKHR fSurface; |
| 106 | VkSwapchainKHR fSwapchain; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 107 | uint32_t fGraphicsQueueIndex; |
| 108 | VkQueue fGraphicsQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 109 | uint32_t fPresentQueueIndex; |
| 110 | VkQueue fPresentQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 111 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 112 | uint32_t fImageCount; |
| 113 | VkImage* fImages; // images in the swapchain |
| 114 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 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; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 121 | } // namespace sk_app |
| 122 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 123 | #endif // SK_VULKAN |
| 124 | |
| 125 | #endif |