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 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 15 | #include "vk/GrVkBackendContext.h" |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 16 | #include "vk/GrVkInterface.h" |
| 17 | #include "vk/VkTestUtils.h" |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 18 | #include "WindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 19 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 20 | class GrRenderTarget; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 21 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 22 | namespace sk_app { |
| 23 | |
| 24 | class VulkanWindowContext : public WindowContext { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 25 | public: |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 26 | ~VulkanWindowContext() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 27 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 28 | sk_sp<SkSurface> getBackbufferSurface() override; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 29 | void swapBuffers() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 30 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 31 | bool isValid() override { return fDevice != VK_NULL_HANDLE; } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 32 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 33 | void resize(int w, int h) override { |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 34 | this->createSwapchain(w, h, fDisplayParams); |
| 35 | } |
| 36 | |
liyuqian | 796c5bb | 2016-05-09 08:49:29 -0700 | [diff] [blame] | 37 | void setDisplayParams(const DisplayParams& params) override { |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 38 | this->destroyContext(); |
| 39 | fDisplayParams = params; |
| 40 | this->initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 41 | } |
| 42 | |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 43 | /** Platform specific function that creates a VkSurfaceKHR for a window */ |
| 44 | using CreateVkSurfaceFn = std::function<VkSurfaceKHR(VkInstance)>; |
| 45 | /** Platform specific function that determines whether presentation will succeed. */ |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 46 | using CanPresentFn = sk_gpu_test::CanPresentFn; |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 47 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 48 | VulkanWindowContext(const DisplayParams&, CreateVkSurfaceFn, CanPresentFn, |
| 49 | PFN_vkGetInstanceProcAddr, PFN_vkGetDeviceProcAddr); |
bsalomon | d1bdd1f | 2016-07-26 12:02:50 -0700 | [diff] [blame] | 50 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 51 | private: |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 52 | void initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 53 | void destroyContext(); |
| 54 | |
| 55 | struct BackbufferInfo { |
| 56 | uint32_t fImageIndex; // image this is associated with |
| 57 | VkSemaphore fAcquireSemaphore; // we signal on this for acquisition of image |
| 58 | VkSemaphore fRenderSemaphore; // we wait on this for rendering to be done |
| 59 | VkCommandBuffer fTransitionCmdBuffers[2]; // to transition layout between present and render |
| 60 | VkFence fUsageFences[2]; // used to ensure this data is no longer used on GPU |
| 61 | }; |
| 62 | |
| 63 | BackbufferInfo* getAvailableBackbuffer(); |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 64 | bool createSwapchain(int width, int height, const DisplayParams& params); |
Greg Daniel | faa095e | 2017-12-19 13:15:02 -0500 | [diff] [blame] | 65 | void createBuffers(VkFormat format, SkColorType colorType); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 66 | void destroyBuffers(); |
| 67 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 68 | VkInstance fInstance = VK_NULL_HANDLE; |
| 69 | VkPhysicalDevice fPhysicalDevice = VK_NULL_HANDLE; |
| 70 | VkDevice fDevice = VK_NULL_HANDLE; |
Greg Daniel | 37329b3 | 2018-07-02 20:16:44 +0000 | [diff] [blame] | 71 | VkDebugReportCallbackEXT fDebugCallback = VK_NULL_HANDLE; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 72 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 73 | // simple wrapper class that exists only to initialize a pointer to NULL |
| 74 | template <typename FNPTR_TYPE> class VkPtr { |
| 75 | public: |
| 76 | VkPtr() : fPtr(NULL) {} |
| 77 | VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; } |
| 78 | operator FNPTR_TYPE() const { return fPtr; } |
| 79 | private: |
| 80 | FNPTR_TYPE fPtr; |
| 81 | }; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 82 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 83 | // Create functions |
| 84 | CreateVkSurfaceFn fCreateVkSurfaceFn; |
| 85 | CanPresentFn fCanPresentFn; |
| 86 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 87 | // Vulkan GetProcAddr functions |
| 88 | VkPtr<PFN_vkGetInstanceProcAddr> fGetInstanceProcAddr; |
| 89 | VkPtr<PFN_vkGetDeviceProcAddr> fGetDeviceProcAddr; |
| 90 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 91 | // WSI interface functions |
| 92 | VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR; |
| 93 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR; |
| 94 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 95 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR; |
| 96 | VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR; |
| 97 | |
| 98 | VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR; |
| 99 | VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR; |
| 100 | VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR; |
| 101 | VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR; |
| 102 | VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 103 | |
| 104 | VkPtr<PFN_vkDestroyInstance> fDestroyInstance; |
| 105 | VkPtr<PFN_vkDeviceWaitIdle> fDeviceWaitIdle; |
| 106 | VkPtr<PFN_vkQueueWaitIdle> fQueueWaitIdle; |
| 107 | VkPtr<PFN_vkDestroyDevice> fDestroyDevice; |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 108 | VkPtr<PFN_vkGetDeviceQueue> fGetDeviceQueue; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 109 | |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 110 | sk_sp<const GrVkInterface> fInterface; |
| 111 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 112 | VkSurfaceKHR fSurface; |
| 113 | VkSwapchainKHR fSwapchain; |
Greg Daniel | f730c18 | 2018-07-02 20:15:37 +0000 | [diff] [blame] | 114 | uint32_t fGraphicsQueueIndex; |
| 115 | VkQueue fGraphicsQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 116 | uint32_t fPresentQueueIndex; |
| 117 | VkQueue fPresentQueue; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 118 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 119 | uint32_t fImageCount; |
| 120 | VkImage* fImages; // images in the swapchain |
| 121 | VkImageLayout* fImageLayouts; // layouts of these images when not color attachment |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 122 | sk_sp<SkSurface>* fSurfaces; // surfaces client renders to (may not be based on rts) |
| 123 | VkCommandPool fCommandPool; |
| 124 | BackbufferInfo* fBackbuffers; |
| 125 | uint32_t fCurrentBackbufferIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 128 | } // namespace sk_app |
| 129 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 130 | #endif // SK_VULKAN |
| 131 | |
| 132 | #endif |