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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkTypes.h" |
djsollen | 9207cae | 2016-06-10 07:50:00 -0700 | [diff] [blame] | 12 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 13 | #ifdef SK_VULKAN |
| 14 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/gpu/vk/GrVkVulkan.h" |
Greg Daniel | 54bfb18 | 2018-11-20 17:12:36 -0500 | [diff] [blame] | 16 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/gpu/vk/GrVkBackendContext.h" |
| 18 | #include "src/gpu/vk/GrVkInterface.h" |
| 19 | #include "tools/gpu/vk/VkTestUtils.h" |
| 20 | #include "tools/sk_app/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 |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 59 | VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | BackbufferInfo* getAvailableBackbuffer(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 63 | bool createSwapchain(int width, int height, const DisplayParams& params); |
Brian Salomon | e4e0aad | 2020-10-09 17:11:29 -0400 | [diff] [blame] | 64 | bool createBuffers(VkFormat format, VkImageUsageFlags, SkColorType colorType, VkSharingMode); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 65 | void destroyBuffers(); |
| 66 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 67 | VkInstance fInstance = VK_NULL_HANDLE; |
| 68 | VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE; |
| 69 | VkDevice fDevice = VK_NULL_HANDLE; |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 70 | VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 71 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 72 | // Create functions |
| 73 | CreateVkSurfaceFn fCreateVkSurfaceFn; |
| 74 | CanPresentFn fCanPresentFn; |
| 75 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 76 | // Vulkan GetProcAddr functions |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 77 | PFN_vkGetInstanceProcAddr fGetInstanceProcAddr = nullptr; |
| 78 | PFN_vkGetDeviceProcAddr fGetDeviceProcAddr = nullptr; |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 79 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 80 | // WSI interface functions |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 81 | PFN_vkDestroySurfaceKHR fDestroySurfaceKHR = nullptr; |
| 82 | PFN_vkGetPhysicalDeviceSurfaceSupportKHR fGetPhysicalDeviceSurfaceSupportKHR = nullptr; |
| 83 | PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR fGetPhysicalDeviceSurfaceCapabilitiesKHR =nullptr; |
| 84 | PFN_vkGetPhysicalDeviceSurfaceFormatsKHR fGetPhysicalDeviceSurfaceFormatsKHR = nullptr; |
| 85 | PFN_vkGetPhysicalDeviceSurfacePresentModesKHR fGetPhysicalDeviceSurfacePresentModesKHR =nullptr; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 86 | |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 87 | PFN_vkCreateSwapchainKHR fCreateSwapchainKHR = nullptr; |
| 88 | PFN_vkDestroySwapchainKHR fDestroySwapchainKHR = nullptr; |
| 89 | PFN_vkGetSwapchainImagesKHR fGetSwapchainImagesKHR = nullptr; |
| 90 | PFN_vkAcquireNextImageKHR fAcquireNextImageKHR = nullptr; |
| 91 | PFN_vkQueuePresentKHR fQueuePresentKHR = nullptr; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 92 | |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 93 | PFN_vkDestroyInstance fDestroyInstance = nullptr; |
| 94 | PFN_vkDeviceWaitIdle fDeviceWaitIdle = nullptr; |
Greg Daniel | a31f4e5 | 2018-08-01 16:48:52 -0400 | [diff] [blame] | 95 | PFN_vkDestroyDebugReportCallbackEXT fDestroyDebugReportCallbackEXT = nullptr; |
Greg Daniel | c8cd45a | 2018-07-12 10:02:37 -0400 | [diff] [blame] | 96 | PFN_vkQueueWaitIdle fQueueWaitIdle = nullptr; |
| 97 | PFN_vkDestroyDevice fDestroyDevice = nullptr; |
| 98 | PFN_vkGetDeviceQueue fGetDeviceQueue = nullptr; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 99 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 100 | sk_sp<const GrVkInterface> fInterface; |
| 101 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 102 | VkSurfaceKHR fSurface; |
| 103 | VkSwapchainKHR fSwapchain; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 104 | uint32_t fGraphicsQueueIndex; |
| 105 | VkQueue fGraphicsQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 106 | uint32_t fPresentQueueIndex; |
| 107 | VkQueue fPresentQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 108 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 109 | uint32_t fImageCount; |
| 110 | VkImage* fImages; // images in the swapchain |
| 111 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 112 | sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 113 | BackbufferInfo* fBackbuffers; |
| 114 | uint32_t fCurrentBackbufferIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 115 | }; |
| 116 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 117 | } // namespace sk_app |
| 118 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 119 | #endif // SK_VULKAN |
| 120 | |
| 121 | #endif |