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" |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 16 | #include "WindowContext.h" |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 17 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 18 | class GrRenderTarget; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 19 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 20 | namespace sk_app { |
| 21 | |
| 22 | class VulkanWindowContext : public WindowContext { |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 23 | public: |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 24 | ~VulkanWindowContext() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 25 | |
jvanverth | af236b5 | 2016-05-20 06:01:06 -0700 | [diff] [blame] | 26 | sk_sp<SkSurface> getBackbufferSurface() override; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 27 | void swapBuffers() override; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 28 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 29 | bool isValid() override { return SkToBool(fBackendContext.get()); } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 30 | |
bsalomon | ccde4ab | 2016-07-27 08:50:12 -0700 | [diff] [blame] | 31 | void resize(int w, int h) override { |
brianosman | 05de216 | 2016-05-06 13:28:57 -0700 | [diff] [blame] | 32 | this->createSwapchain(w, h, fDisplayParams); |
| 33 | } |
| 34 | |
liyuqian | 796c5bb | 2016-05-09 08:49:29 -0700 | [diff] [blame] | 35 | void setDisplayParams(const DisplayParams& params) override { |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 36 | this->destroyContext(); |
| 37 | fDisplayParams = params; |
| 38 | this->initializeContext(); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Greg Daniel | 1f05f44 | 2016-10-27 16:37:17 -0400 | [diff] [blame] | 41 | GrBackendContext getBackendContext() override { |
| 42 | return (GrBackendContext) fBackendContext.get(); |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 43 | } |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 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. */ |
| 48 | using CanPresentFn = GrVkBackendContext::CanPresentFn; |
| 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); |
egdaniel | 58a8d92 | 2016-04-21 08:03:10 -0700 | [diff] [blame] | 67 | void createBuffers(VkFormat format); |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 68 | void destroyBuffers(); |
| 69 | |
Hal Canary | 1b612a8 | 2016-11-03 16:26:13 -0400 | [diff] [blame] | 70 | sk_sp<const GrVkBackendContext> fBackendContext; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 71 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 72 | // simple wrapper class that exists only to initialize a pointer to NULL |
| 73 | template <typename FNPTR_TYPE> class VkPtr { |
| 74 | public: |
| 75 | VkPtr() : fPtr(NULL) {} |
| 76 | VkPtr operator=(FNPTR_TYPE ptr) { fPtr = ptr; return *this; } |
| 77 | operator FNPTR_TYPE() const { return fPtr; } |
| 78 | private: |
| 79 | FNPTR_TYPE fPtr; |
| 80 | }; |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 81 | |
Jim Van Verth | fbdc080 | 2017-05-02 16:15:53 -0400 | [diff] [blame] | 82 | // Create functions |
| 83 | CreateVkSurfaceFn fCreateVkSurfaceFn; |
| 84 | CanPresentFn fCanPresentFn; |
| 85 | |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 86 | // Vulkan GetProcAddr functions |
| 87 | VkPtr<PFN_vkGetInstanceProcAddr> fGetInstanceProcAddr; |
| 88 | VkPtr<PFN_vkGetDeviceProcAddr> fGetDeviceProcAddr; |
| 89 | |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 90 | // WSI interface functions |
| 91 | VkPtr<PFN_vkDestroySurfaceKHR> fDestroySurfaceKHR; |
| 92 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceSupportKHR> fGetPhysicalDeviceSurfaceSupportKHR; |
| 93 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceCapabilitiesKHR> fGetPhysicalDeviceSurfaceCapabilitiesKHR; |
| 94 | VkPtr<PFN_vkGetPhysicalDeviceSurfaceFormatsKHR> fGetPhysicalDeviceSurfaceFormatsKHR; |
| 95 | VkPtr<PFN_vkGetPhysicalDeviceSurfacePresentModesKHR> fGetPhysicalDeviceSurfacePresentModesKHR; |
| 96 | |
| 97 | VkPtr<PFN_vkCreateSwapchainKHR> fCreateSwapchainKHR; |
| 98 | VkPtr<PFN_vkDestroySwapchainKHR> fDestroySwapchainKHR; |
| 99 | VkPtr<PFN_vkGetSwapchainImagesKHR> fGetSwapchainImagesKHR; |
| 100 | VkPtr<PFN_vkAcquireNextImageKHR> fAcquireNextImageKHR; |
| 101 | VkPtr<PFN_vkQueuePresentKHR> fQueuePresentKHR; |
Greg Daniel | 35970ec | 2017-11-10 10:03:05 -0500 | [diff] [blame] | 102 | VkPtr<PFN_vkGetDeviceQueue> fGetDeviceQueue; |
jvanverth | b0d4352 | 2016-04-21 11:46:23 -0700 | [diff] [blame] | 103 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 104 | VkSurfaceKHR fSurface; |
| 105 | VkSwapchainKHR fSwapchain; |
| 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) |
| 113 | VkCommandPool fCommandPool; |
| 114 | BackbufferInfo* fBackbuffers; |
| 115 | uint32_t fCurrentBackbufferIndex; |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
jvanverth | a8d0d6c | 2016-05-05 12:32:03 -0700 | [diff] [blame] | 118 | } // namespace sk_app |
| 119 | |
jvanverth | 9f37246 | 2016-04-06 06:08:59 -0700 | [diff] [blame] | 120 | #endif // SK_VULKAN |
| 121 | |
| 122 | #endif |